blob: 728ac93f6342c5dfe5e944401494ba1535e28360 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-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 Kremenek77349cb2008-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 Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000025#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000026#include "llvm/ADT/ImmutableList.h"
27#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000028#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000029
Ted Kremenek0f5f0592008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
32#include <sstream>
33#endif
34
Ted Kremenekb387a3f2008-02-14 22:16:04 +000035using namespace clang;
36using llvm::dyn_cast;
37using llvm::cast;
38using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000039
Ted Kremeneke695e1c2008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
41// Engine construction and deletion.
42//===----------------------------------------------------------------------===//
43
Ted Kremenekbdb435d2008-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 Kremenek536aa022009-03-30 17:53:05 +000052 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000053
54public:
Ted Kremenek536aa022009-03-30 17:53:05 +000055 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
56 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenekbdb435d2008-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 Kremenek536aa022009-03-30 17:53:05 +000074 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-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 Kremenek536aa022009-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 Kremenekcf118d42009-02-04 23:49:09 +000085
Ted Kremenek4adc81e2008-08-13 04:27:00 +000086 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-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 Kremenekbdb435d2008-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 Kremenek536aa022009-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 Kremenekbdb435d2008-07-11 18:37:32 +0000100
Ted Kremenekbdb435d2008-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 Kremeneke448ab42008-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 Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000118 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000119 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000120 StoreManagerCreator SMC,
121 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 : CoreEngine(cfg, CD, Ctx, *this),
123 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000124 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000125 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000126 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000127 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000128 ValMgr(StateMgr.getValueManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000129 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000130 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
131 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000132 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000133 BR(BRD, *this),
134 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000135
Ted Kremenek1a654b62008-06-20 21:45:25 +0000136GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000137 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000138 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000139}
140
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000141//===----------------------------------------------------------------------===//
142// Utility methods.
143//===----------------------------------------------------------------------===//
144
Ted Kremenek186350f2008-04-23 20:12:28 +0000145
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000146void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000147 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000148 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000149 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000150}
151
Ted Kremenekbdb435d2008-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 Kremenek50a6d0c2008-04-09 21:41:14 +0000157}
158
Ted Kremenek536aa022009-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 Kremenek4adc81e2008-08-13 04:27:00 +0000166const GRState* GRExprEngine::getInitialState() {
Ted Kremenek52e56022009-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)) {
180 SVal V = GetSVal(state, loc::MemRegionVal(R));
Zhongxing Xu262fd032009-05-20 09:00:16 +0000181 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek52e56022009-04-10 00:59:50 +0000182 ValMgr.makeZeroVal(T),
183 getContext().IntTy);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000184
185 if (const GRState *newState = state->assume(Constraint, true))
186 state = newState;
Ted Kremenek52e56022009-04-10 00:59:50 +0000187 }
188 }
189
190 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000191}
192
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000193//===----------------------------------------------------------------------===//
194// Top-level transfer function logic (Dispatcher).
195//===----------------------------------------------------------------------===//
196
197void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
198
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000199 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
200 S->getLocStart(),
201 "Error evaluating statement");
202
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000203 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000213
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000214 // Create the cleaned state.
Ted Kremenek241677a2009-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 Kremenek77d7ef82008-04-24 18:31:42 +0000220 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000222
Ted Kremenek241677a2009-01-21 22:26:05 +0000223 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000225 else {
226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000227 SaveOr OldHasGen(Builder->HasGeneratedNode);
228
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000229 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
230 Builder->PurgingDeadSymbols = true;
231
Ted Kremenek729a9a22008-07-17 23:15:45 +0000232 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000233 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000234
235 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
236 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000237 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000238
239 bool HasAutoGenerated = false;
240
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242
243 NodeSet Dst;
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
247
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000248 // Visit the statement.
Ted Kremenek846d4e92008-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 Kremenek77d7ef82008-04-24 18:31:42 +0000259 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000260
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000261 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000263 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000264
265 // FIXME: Consolidate.
266 StateMgr.CurrentStmt = 0;
267 CurrentStmt = 0;
268
Ted Kremenek846d4e92008-04-24 23:35:58 +0000269 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000270}
271
Ted Kremenek0bed8a12009-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 Kremeneke695e1c2008-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 Kremenek540cbe22008-04-22 04:56:29 +0000294
295 case Stmt::ArraySubscriptExprClass:
296 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
297 break;
Ted Kremeneke695e1c2008-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 Kremeneka8538d92009-02-13 01:45:31 +0000311 const GRState* state = GetState(Pred);
312 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000313 break;
314 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000315
Ted Kremenek48af2a92009-02-25 22:32:02 +0000316 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
317 NodeSet Tmp;
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000319 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000320 }
321 else
322 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
323
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324 break;
325 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000326
Douglas Gregorb4609802008-11-14 16:09:21 +0000327 case Stmt::CallExprClass:
328 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000329 CallExpr* C = cast<CallExpr>(S);
330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000331 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000332 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000333
Ted Kremeneke695e1c2008-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 Xuf22679e2008-11-07 10:38:33 +0000346
347 case Stmt::CompoundLiteralExprClass:
348 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
349 break;
Ted Kremeneke695e1c2008-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 Gregor1a49af92009-01-06 05:10:23 +0000358 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000359 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 break;
361
362 case Stmt::DeclStmtClass:
363 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
364 break;
365
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000366 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000367 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000368 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000369 VisitCast(C, C->getSubExpr(), Pred, Dst);
370 break;
371 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000372
373 case Stmt::InitListExprClass:
374 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
375 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000376
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000377 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000378 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
379 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000380
381 case Stmt::ObjCIvarRefExprClass:
382 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
383 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000384
385 case Stmt::ObjCForCollectionStmtClass:
386 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
387 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000388
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000389 case Stmt::ObjCMessageExprClass: {
390 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
391 break;
392 }
393
Ted Kremenekbbfd07a2008-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 Kremeneke695e1c2008-04-15 23:06:53 +0000403 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Sebastian Redl05189992008-11-11 17:56:53 +0000411 case Stmt::SizeOfAlignOfExprClass:
412 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-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 Kremeneka8538d92009-02-13 01:45:31 +0000428 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000429 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000430 else
431 Dst.Add(Pred);
432
433 break;
434 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000435
436 case Stmt::StringLiteralClass:
437 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
438 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000439
Ted Kremenek72374592009-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 Kremeneke695e1c2008-04-15 23:06:53 +0000449 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000450 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 }
452}
453
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000454void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-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 Kremenek1b8bd4d2008-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 Gregor1a49af92009-01-06 05:10:23 +0000470 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000471 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
472 return;
473
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000474 case Stmt::ObjCIvarRefExprClass:
475 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
476 return;
477
Ted Kremenek1b8bd4d2008-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 Kremenekb6b81d12008-10-17 17:24:14 +0000485
Ted Kremenek4f090272008-10-27 21:54:31 +0000486 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000487 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000488 return;
489
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000490 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek868210e2009-04-21 23:53:32 +0000491 case Stmt::ObjCKVCRefExprClass:
Ted Kremenekb6b81d12008-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 Kremenekc7df6d22008-10-18 04:08:49 +0000504 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000505
506 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000507 const GRState* state = GetState(Pred);
508 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
509 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000510 return;
511 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000512
Ted Kremenekf8cd1b22008-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 Gregord7eb8462009-01-30 17:31:00 +0000518 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000519 "Other kinds of expressions with non-aggregate/union types do"
520 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000521
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000522 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Block entrance. (Update counters).
528//===----------------------------------------------------------------------===//
529
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000530bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000531 GRBlockCounter BC) {
532
533 return BC.getNumVisited(B->getBlockID()) < 3;
534}
535
536//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-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 Kremeneke695e1c2008-04-15 23:06:53 +0000553// Branch processing.
554//===----------------------------------------------------------------------===//
555
Ted Kremeneka8538d92009-02-13 01:45:31 +0000556const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000557 Stmt* Terminator,
558 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000559
560 switch (Terminator->getStmtClass()) {
561 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 return state;
Ted Kremenek05a23782008-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 Kremeneka8538d92009-02-13 01:45:31 +0000581 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-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 Kremeneka8538d92009-02-13 01:45:31 +0000598 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-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 Kremeneka8538d92009-02-13 01:45:31 +0000606 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000607 }
608 }
609}
610
Ted Kremenek6ae8a362009-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 Kremenek23ec48c2009-06-18 23:58:37 +0000647 return state->getSVal(Ex);
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000648}
649
Ted Kremenekaf337412008-11-12 19:24:17 +0000650void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000651 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000652
Ted Kremeneke7d22112008-02-11 19:21:59 +0000653 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000654 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000655 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000656
Ted Kremenekb2331832008-02-15 22:29:00 +0000657 // Check for NULL conditions; e.g. "for(;;)"
658 if (!Condition) {
659 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000660 return;
661 }
662
Ted Kremenek21028dd2009-03-11 03:54:24 +0000663 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
664 Condition->getLocStart(),
665 "Error evaluating branch");
666
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000667 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000668
669 switch (V.getBaseKind()) {
670 default:
671 break;
672
Ted Kremenek6ae8a362009-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 Kremenek58b33212008-02-26 19:40:44 +0000691 builder.generateNode(MarkBranch(PrevState, Term, true), true);
692 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000693 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000694 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000695
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000696 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000697 NodeTy* N = builder.generateNode(PrevState, true);
698
699 if (N) {
700 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000701 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000702 }
703
704 builder.markInfeasible(false);
705 return;
706 }
707 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000708
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000709 // Process the true branch.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000710 if (const GRState *state = PrevState->assume(V, true))
Ted Kremeneka8538d92009-02-13 01:45:31 +0000711 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000712 else
713 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000714
715 // Process the false branch.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000716 if (const GRState *state = PrevState->assume(V, false))
Ted Kremeneka8538d92009-02-13 01:45:31 +0000717 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000718 else
719 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000720}
721
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000722/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000723/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000724void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000725
Ted Kremeneka8538d92009-02-13 01:45:31 +0000726 const GRState* state = builder.getState();
727 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000728
729 // Three possibilities:
730 //
731 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000732 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-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 Xu1c96b242008-10-17 05:57:07 +0000738 if (isa<loc::GotoLabel>(V)) {
739 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000740
741 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000742 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000743 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000744 return;
745 }
746 }
747
748 assert (false && "No block with label.");
749 return;
750 }
751
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000752 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000753 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000754 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000755 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000756 return;
757 }
758
759 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000760 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek754607e2008-02-13 00:24:44 +0000761
762 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000763 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000764}
Ted Kremenekf233d482008-02-05 00:26:40 +0000765
Ted Kremeneke695e1c2008-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 Kremeneka8538d92009-02-13 01:45:31 +0000772 const GRState* state = GetState(Pred);
773 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000774
775 assert (X.isUndef());
776
777 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
778
779 assert (SE);
780
Ted Kremeneka8538d92009-02-13 01:45:31 +0000781 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000782
783 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000784 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000785}
786
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000787/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
788/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000789void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
790 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000791 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000792 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000793 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000794
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000795 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000796 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000797 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000798 return;
799 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000800
Ted Kremeneka8538d92009-02-13 01:45:31 +0000801 const GRState* DefaultSt = state;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000802 bool defaultIsFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000803
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000804 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000805 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000806
807 // Evaluate the LHS of the case value.
808 Expr::EvalResult V1;
809 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000810
Ted Kremenek72afb372009-01-17 01:54:16 +0000811 // Sanity checks. These go away in Release builds.
812 assert(b && V1.Val.isInt() && !V1.HasSideEffects
813 && "Case condition must evaluate to an integer constant.");
814 b = b; // silence unused variable warning
815 assert(V1.Val.getInt().getBitWidth() ==
816 getContext().getTypeSize(CondE->getType()));
817
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000818 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000819 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000820
821 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000822 b = E->Evaluate(V2, getContext());
823 assert(b && V2.Val.isInt() && !V2.HasSideEffects
824 && "Case condition must evaluate to an integer constant.");
825 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000826 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000827 else
828 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000829
830 // FIXME: Eventually we should replace the logic below with a range
831 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000832 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000833
Ted Kremenek14a11402008-03-17 22:17:56 +0000834 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000835 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu262fd032009-05-20 09:00:16 +0000836 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000837 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000838
Ted Kremenek72afb372009-01-17 01:54:16 +0000839 // Now "assume" that the case matches.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000840 if (const GRState* stateNew = state->assume(Res, true)) {
841 builder.generateCaseStmtNode(I, stateNew);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000842
843 // If CondV evaluates to a constant, then we know that this
844 // is the *only* case that we can take, so stop evaluating the
845 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000846 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000847 return;
848 }
849
850 // Now "assume" that the case doesn't match. Add this state
851 // to the default state (if it is feasible).
Ted Kremeneka591bc02009-06-18 22:57:13 +0000852 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
853 defaultIsFeasible = true;
854 DefaultSt = stateNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000855 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000856
Ted Kremenek14a11402008-03-17 22:17:56 +0000857 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000858 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000859 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000860
Ted Kremenek72afb372009-01-17 01:54:16 +0000861 ++V1.Val.getInt();
862 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000863
864 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000865 }
866
867 // If we reach here, than we know that the default branch is
868 // possible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000869 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000870}
871
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000872//===----------------------------------------------------------------------===//
873// Transfer functions: logical operations ('&&', '||').
874//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000875
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000876void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000877 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000878
Ted Kremenek05a23782008-02-26 19:05:15 +0000879 assert (B->getOpcode() == BinaryOperator::LAnd ||
880 B->getOpcode() == BinaryOperator::LOr);
881
882 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
883
Ted Kremeneka8538d92009-02-13 01:45:31 +0000884 const GRState* state = GetState(Pred);
885 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000886
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000887 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000888
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000889 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000890
891 assert (Ex);
892
893 if (Ex == B->getRHS()) {
894
Ted Kremeneka8538d92009-02-13 01:45:31 +0000895 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000896
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000897 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000898
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000899 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000900 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000901 return;
902 }
903
Ted Kremenek05a23782008-02-26 19:05:15 +0000904 // We took the RHS. Because the value of the '&&' or '||' expression must
905 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
906 // or 1. Alternatively, we could take a lazy approach, and calculate this
907 // value later when necessary. We don't have the machinery in place for
908 // this right now, and since most logical expressions are used for branches,
Ted Kremeneka591bc02009-06-18 22:57:13 +0000909 // the payoff is not likely to be large. Instead, we do eager evaluation.
910 if (const GRState *newState = state->assume(X, true))
911 MakeNode(Dst, B, Pred, BindBlkExpr(newState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000912
Ted Kremeneka591bc02009-06-18 22:57:13 +0000913 if (const GRState *newState = state->assume(X, false))
914 MakeNode(Dst, B, Pred, BindBlkExpr(newState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000915 }
916 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000917 // We took the LHS expression. Depending on whether we are '&&' or
918 // '||' we know what the value of the expression is via properties of
919 // the short-circuiting.
Ted Kremenek05a23782008-02-26 19:05:15 +0000920 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000921 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000922 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000923}
Ted Kremenek05a23782008-02-26 19:05:15 +0000924
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000925//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000926// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000927//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000928
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000929void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
930 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000931
Ted Kremeneka8538d92009-02-13 01:45:31 +0000932 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000933
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000934 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000935
936 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
937
Ted Kremeneka8538d92009-02-13 01:45:31 +0000938 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000939
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000940 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +0000941 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
942 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000943 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000944 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000945 return;
946
947 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
948 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
949
950 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000951 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000952 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000953 return;
954
955 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000956 assert(asLValue);
Zhongxing Xu369f4472009-04-20 05:24:46 +0000957 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek7090d542009-05-07 18:27:16 +0000958 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
959 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000960 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000961 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000962
963 assert (false &&
964 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000965}
966
Ted Kremenek540cbe22008-04-22 04:56:29 +0000967/// VisitArraySubscriptExpr - Transfer function for array accesses
968void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000969 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000970
971 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000972 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000973 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000974
975 if (Base->getType()->isVectorType()) {
976 // For vector types get its lvalue.
977 // FIXME: This may not be correct. Is the rvalue of a vector its location?
978 // In fact, I think this is just a hack. We need to get the right
979 // semantics.
980 VisitLValue(Base, Pred, Tmp);
981 }
982 else
983 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000984
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000985 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000986 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000987 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000988
989 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000990 const GRState* state = GetState(*I2);
Ted Kremenekf936f452009-05-04 06:18:28 +0000991 SVal V = StateMgr.GetLValue(state, A->getType(),
992 GetSVal(state, Base),
Ted Kremeneka8538d92009-02-13 01:45:31 +0000993 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000994
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000995 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +0000996 MakeNode(Dst, A, *I2, BindExpr(state, A, V),
997 ProgramPoint::PostLValueKind);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000998 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000999 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001000 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001001 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001002}
1003
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001004/// VisitMemberExpr - Transfer function for member expressions.
1005void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001006 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001007
1008 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001009 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001010
1011 if (M->isArrow())
1012 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1013 else
1014 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1015
Douglas Gregor86f19402008-12-20 23:49:58 +00001016 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1017 if (!Field) // FIXME: skipping member expressions for non-fields
1018 return;
1019
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001020 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001021 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001022 // FIXME: Should we insert some assumption logic in here to determine
1023 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001024 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001025 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001026
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001027 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00001028 MakeNode(Dst, M, *I, BindExpr(state, M, L),
1029 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001030 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001031 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001032 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001033}
1034
Ted Kremeneka8538d92009-02-13 01:45:31 +00001035/// EvalBind - Handle the semantics of binding a value to a specific location.
1036/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1037void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1038 const GRState* state, SVal location, SVal Val) {
1039
Ted Kremenek41573eb2009-02-14 01:43:44 +00001040 const GRState* newState = 0;
1041
1042 if (location.isUnknown()) {
1043 // We know that the new state will be the same as the old state since
1044 // the location of the binding is "unknown". Consequently, there
1045 // is no reason to just create a new node.
1046 newState = state;
1047 }
1048 else {
1049 // We are binding to a value other than 'unknown'. Perform the binding
1050 // using the StoreManager.
1051 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1052 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001053
Ted Kremenek41573eb2009-02-14 01:43:44 +00001054 // The next thing to do is check if the GRTransferFuncs object wants to
1055 // update the state based on the new binding. If the GRTransferFunc object
1056 // doesn't do anything, just auto-propagate the current state.
1057 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1058 newState != state);
1059
1060 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001061}
1062
1063/// EvalStore - Handle the semantics of a store via an assignment.
1064/// @param Dst The node set to store generated state nodes
1065/// @param Ex The expression representing the location of the store
1066/// @param state The current simulation state
1067/// @param location The location to store the value
1068/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001069void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001070 const GRState* state, SVal location, SVal Val,
1071 const void *tag) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001072
1073 assert (Builder && "GRStmtNodeBuilder must be defined.");
1074
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001075 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001076 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001077
Ted Kremenek8c354752008-12-16 22:02:27 +00001078 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001079 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001080
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001081 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001082 state = GetState(Pred);
1083
1084 // Proceed with the store.
1085 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek1670e402009-04-11 00:11:10 +00001086 SaveAndRestore<const void*> OldTag(Builder->Tag);
1087 Builder->PointKind = ProgramPoint::PostStoreKind;
1088 Builder->Tag = tag;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001089 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001090}
1091
1092void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001093 const GRState* state, SVal location,
1094 const void *tag) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001095
Ted Kremenek8c354752008-12-16 22:02:27 +00001096 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001097 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001098
Ted Kremenek8c354752008-12-16 22:02:27 +00001099 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001100 return;
1101
Ted Kremeneka8538d92009-02-13 01:45:31 +00001102 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001103
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001104 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001105 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001106
1107 // FIXME: Currently symbolic analysis "generates" new symbols
1108 // for the contents of values. We need a better approach.
1109
Ted Kremenek8c354752008-12-16 22:02:27 +00001110 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001111 // This is important. We must nuke the old binding.
Ted Kremenek1670e402009-04-11 00:11:10 +00001112 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001113 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001114 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001115 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
Ted Kremenek1670e402009-04-11 00:11:10 +00001116 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001117 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001118}
1119
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001120void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001121 const GRState* state, SVal location, SVal Val,
1122 const void *tag) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001123
1124 NodeSet TmpDst;
Ted Kremenek1670e402009-04-11 00:11:10 +00001125 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001126
1127 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek1670e402009-04-11 00:11:10 +00001128 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001129}
1130
Ted Kremenek8c354752008-12-16 22:02:27 +00001131GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001132 const GRState* state,
Ted Kremenek1670e402009-04-11 00:11:10 +00001133 SVal location,
1134 const void *tag) {
1135
1136 SaveAndRestore<const void*> OldTag(Builder->Tag);
1137 Builder->Tag = tag;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001138
1139 // Check for loads/stores from/to undefined values.
1140 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001141 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001142 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001143 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001144
Ted Kremenek8c354752008-12-16 22:02:27 +00001145 if (N) {
1146 N->markAsSink();
1147 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001148 }
1149
Ted Kremenek8c354752008-12-16 22:02:27 +00001150 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001151 }
1152
1153 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1154 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001155 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001156
1157 // During a load, one of two possible situations arise:
1158 // (1) A crash, because the location (pointer) was NULL.
1159 // (2) The location (pointer) is not NULL, and the dereference works.
1160 //
1161 // We add these assumptions.
1162
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001163 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001164
1165 // "Assume" that the pointer is not NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001166 const GRState *StNotNull = state->assume(LV, true);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001167
1168 // "Assume" that the pointer is NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001169 const GRState *StNull = state->assume(LV, false);
Zhongxing Xua1718c72009-04-03 07:33:13 +00001170
Ted Kremeneka591bc02009-06-18 22:57:13 +00001171 if (StNull) {
Ted Kremenek7360fda2008-09-18 23:09:54 +00001172 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001173 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekb65be702009-06-18 01:23:53 +00001174 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001175
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001176 // We don't use "MakeNode" here because the node will be a sink
1177 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001178 NodeTy* NullNode =
1179 Builder->generateNode(Ex, StNull, Pred,
1180 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001181
Ted Kremeneka591bc02009-06-18 22:57:13 +00001182 if (NullNode) {
1183 NullNode->markAsSink();
1184 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001185 else ExplicitNullDeref.insert(NullNode);
1186 }
1187 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001188
Ted Kremeneka591bc02009-06-18 22:57:13 +00001189 if (!StNotNull)
1190 return NULL;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001191
1192 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001193 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001194 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1195 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1196 // Get the index of the accessed element.
1197 SVal Idx = ER->getIndex();
1198 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001199 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1200 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001201
Ted Kremeneka591bc02009-06-18 22:57:13 +00001202 const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
1203 true);
1204 const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
1205 false);
Zhongxing Xu60156f02008-11-08 03:45:42 +00001206
Ted Kremeneka591bc02009-06-18 22:57:13 +00001207 if (StOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001208 // Report warning. Make sink node manually.
1209 NodeTy* OOBNode =
1210 Builder->generateNode(Ex, StOutBound, Pred,
1211 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001212
1213 if (OOBNode) {
1214 OOBNode->markAsSink();
1215
Ted Kremeneka591bc02009-06-18 22:57:13 +00001216 if (StInBound)
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001217 ImplicitOOBMemAccesses.insert(OOBNode);
1218 else
1219 ExplicitOOBMemAccesses.insert(OOBNode);
1220 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001221 }
1222
Ted Kremeneka591bc02009-06-18 22:57:13 +00001223 if (!StInBound)
1224 return NULL;
1225
Ted Kremenek8c354752008-12-16 22:02:27 +00001226 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001227 }
1228 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001229
Ted Kremenek8c354752008-12-16 22:02:27 +00001230 // Generate a new node indicating the checks succeed.
1231 return Builder->generateNode(Ex, StNotNull, Pred,
1232 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001233}
1234
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001235//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001236// Transfer function: OSAtomics.
1237//
1238// FIXME: Eventually refactor into a more "plugin" infrastructure.
1239//===----------------------------------------------------------------------===//
1240
1241// Mac OS X:
1242// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1243// atomic.3.html
1244//
1245static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1246 GRExprEngine& Engine,
1247 GRStmtNodeBuilder<GRState>& Builder,
1248 CallExpr* CE, SVal L,
1249 ExplodedNode<GRState>* Pred) {
1250
1251 // Not enough arguments to match OSAtomicCompareAndSwap?
1252 if (CE->getNumArgs() != 3)
1253 return false;
1254
1255 ASTContext &C = Engine.getContext();
1256 Expr *oldValueExpr = CE->getArg(0);
1257 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1258
1259 Expr *newValueExpr = CE->getArg(1);
1260 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1261
1262 // Do the types of 'oldValue' and 'newValue' match?
1263 if (oldValueType != newValueType)
1264 return false;
1265
1266 Expr *theValueExpr = CE->getArg(2);
1267 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1268
1269 // theValueType not a pointer?
1270 if (!theValueType)
1271 return false;
1272
1273 QualType theValueTypePointee =
1274 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1275
1276 // The pointee must match newValueType and oldValueType.
1277 if (theValueTypePointee != newValueType)
1278 return false;
1279
1280 static unsigned magic_load = 0;
1281 static unsigned magic_store = 0;
1282
1283 const void *OSAtomicLoadTag = &magic_load;
1284 const void *OSAtomicStoreTag = &magic_store;
1285
1286 // Load 'theValue'.
Ted Kremenek1670e402009-04-11 00:11:10 +00001287 const GRState *state = Pred->getState();
1288 ExplodedNodeSet<GRState> Tmp;
Ted Kremenek23ec48c2009-06-18 23:58:37 +00001289 SVal location = state->getSVal(theValueExpr);
Ted Kremenek1670e402009-04-11 00:11:10 +00001290 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1291
1292 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1293 I != E; ++I) {
1294
1295 ExplodedNode<GRState> *N = *I;
1296 const GRState *stateLoad = N->getState();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001297 SVal theValueVal = stateLoad->getSVal(theValueExpr);
1298 SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
Ted Kremenek1670e402009-04-11 00:11:10 +00001299
1300 // Perform the comparison.
Zhongxing Xu262fd032009-05-20 09:00:16 +00001301 SVal Cmp = Engine.EvalBinOp(stateLoad,
1302 BinaryOperator::EQ, theValueVal, oldValueVal,
Ted Kremenek1670e402009-04-11 00:11:10 +00001303 Engine.getContext().IntTy);
Ted Kremeneka591bc02009-06-18 22:57:13 +00001304
1305 const GRState *stateEqual = stateLoad->assume(Cmp, true);
Ted Kremenek1670e402009-04-11 00:11:10 +00001306
1307 // Were they equal?
Ted Kremeneka591bc02009-06-18 22:57:13 +00001308 if (stateEqual) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001309 // Perform the store.
1310 ExplodedNodeSet<GRState> TmpStore;
1311 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenek23ec48c2009-06-18 23:58:37 +00001312 stateEqual->getSVal(newValueExpr), OSAtomicStoreTag);
Ted Kremenek1670e402009-04-11 00:11:10 +00001313
1314 // Now bind the result of the comparison.
1315 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1316 E2 = TmpStore.end(); I2 != E2; ++I2) {
1317 ExplodedNode<GRState> *predNew = *I2;
1318 const GRState *stateNew = predNew->getState();
1319 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
1320 Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
1321 }
1322 }
1323
1324 // Were they not equal?
Ted Kremeneka591bc02009-06-18 22:57:13 +00001325 if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001326 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
1327 Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
1328 }
1329 }
1330
1331 return true;
1332}
1333
1334static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1335 GRExprEngine& Engine,
1336 GRStmtNodeBuilder<GRState>& Builder,
1337 CallExpr* CE, SVal L,
1338 ExplodedNode<GRState>* Pred) {
Zhongxing Xu369f4472009-04-20 05:24:46 +00001339 const FunctionDecl* FD = L.getAsFunctionDecl();
1340 if (!FD)
Ted Kremenek1670e402009-04-11 00:11:10 +00001341 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +00001342
Ted Kremenek1670e402009-04-11 00:11:10 +00001343 const char *FName = FD->getNameAsCString();
1344
1345 // Check for compare and swap.
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001346 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1347 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek1670e402009-04-11 00:11:10 +00001348 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001349
Ted Kremenek1670e402009-04-11 00:11:10 +00001350 // FIXME: Other atomics.
1351 return false;
1352}
1353
1354//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001355// Transfer function: Function calls.
1356//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001357
1358void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1359 assert (Builder && "GRStmtNodeBuilder must be defined.");
1360
1361 // FIXME: Allow us to chain together transfer functions.
1362 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1363 return;
1364
1365 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1366}
1367
Ted Kremenekde434242008-02-19 01:44:53 +00001368void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001369 CallExpr::arg_iterator AI,
1370 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001371 NodeSet& Dst)
1372{
1373 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001374 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001375 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1376 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001377 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001378
1379 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1380}
1381
1382void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1383 CallExpr::arg_iterator AI,
1384 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001385 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001386 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001387
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001388 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001389 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001390 // If the call argument is being bound to a reference parameter,
1391 // visit it as an lvalue, not an rvalue.
1392 bool VisitAsLvalue = false;
1393 if (Proto && ParamIdx < Proto->getNumArgs())
1394 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1395
1396 NodeSet DstTmp;
1397 if (VisitAsLvalue)
1398 VisitLValue(*AI, Pred, DstTmp);
1399 else
1400 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001401 ++AI;
1402
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001403 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001404 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001405
1406 return;
1407 }
1408
1409 // If we reach here we have processed all of the arguments. Evaluate
1410 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001411
Ted Kremenek994a09b2008-02-25 21:16:03 +00001412 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001413 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001414
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001415 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001416
Ted Kremenekde434242008-02-19 01:44:53 +00001417 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001418 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1419
Ted Kremeneka8538d92009-02-13 01:45:31 +00001420 const GRState* state = GetState(*DI);
1421 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001422
Ted Kremeneka1354a52008-03-03 16:47:31 +00001423 // FIXME: Add support for symbolic function calls (calls involving
1424 // function pointer values that are symbolic).
1425
1426 // Check for undefined control-flow or calls to NULL.
1427
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001428 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001429 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001430
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001431 if (N) {
1432 N->markAsSink();
1433 BadCalls.insert(N);
1434 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001435
Ted Kremenekde434242008-02-19 01:44:53 +00001436 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001437 }
1438
1439 // Check for the "noreturn" attribute.
1440
1441 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xu369f4472009-04-20 05:24:46 +00001442 const FunctionDecl* FD = L.getAsFunctionDecl();
1443 if (FD) {
Douglas Gregor68584ed2009-06-18 16:11:24 +00001444 if (FD->getAttr<NoReturnAttr>(getContext()) ||
1445 FD->getAttr<AnalyzerNoReturnAttr>(getContext()))
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001446 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001447 else {
1448 // HACK: Some functions are not marked noreturn, and don't return.
1449 // Here are a few hardwired ones. If this takes too long, we can
1450 // potentially cache these results.
1451 const char* s = FD->getIdentifier()->getName();
1452 unsigned n = strlen(s);
1453
1454 switch (n) {
1455 default:
1456 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001457
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001458 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001459 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1460 break;
1461
1462 case 5:
1463 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001464 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001465 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001466 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001467 // FIXME: use Assume to inspect the possible symbolic value of
1468 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001469 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001470 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001471 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001472 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001473 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001474 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001475
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001476 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001477 if (!memcmp(s, "Assert", 6)) {
1478 Builder->BuildSinks = true;
1479 break;
1480 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001481
1482 // FIXME: This is just a wrapper around throwing an exception.
1483 // Eventually inter-procedural analysis should handle this easily.
1484 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1485
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001486 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001487
1488 case 7:
1489 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1490 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001491
Ted Kremenekf47bb782008-04-30 17:54:04 +00001492 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001493 if (!memcmp(s ,"db_error", 8) ||
1494 !memcmp(s, "__assert", 8))
1495 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001496 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001497
1498 case 12:
1499 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1500 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001501
Ted Kremenekf9683082008-09-19 02:30:47 +00001502 case 13:
1503 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1504 break;
1505
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001506 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001507 if (!memcmp(s, "dtrace_assfail", 14) ||
1508 !memcmp(s, "yy_fatal_error", 14))
1509 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001510 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001511
1512 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001513 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001514 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1515 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001516 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001517
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001518 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001519 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001520
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001521 }
1522 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001523
1524 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001525
Zhongxing Xu369f4472009-04-20 05:24:46 +00001526 if (FD) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001527
Zhongxing Xu369f4472009-04-20 05:24:46 +00001528 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001529 switch (id) {
1530 case Builtin::BI__builtin_expect: {
1531 // For __builtin_expect, just return the value of the subexpression.
1532 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001533 SVal X = GetSVal(state, *(CE->arg_begin()));
1534 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001535 continue;
1536 }
1537
Ted Kremenekb3021332008-11-02 00:35:01 +00001538 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001539 // FIXME: Refactor into StoreManager itself?
1540 MemRegionManager& RM = getStateManager().getRegionManager();
1541 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001542 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001543
1544 // Set the extent of the region in bytes. This enables us to use the
1545 // SVal of the argument directly. If we save the extent in bits, we
1546 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001547 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1548 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001549
Ted Kremeneka8538d92009-02-13 01:45:31 +00001550 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001551 continue;
1552 }
1553
Ted Kremenek55aea312008-03-05 22:59:42 +00001554 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001555 break;
1556 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001557 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001558
Ted Kremenek186350f2008-04-23 20:12:28 +00001559 // Check any arguments passed-by-value against being undefined.
1560
1561 bool badArg = false;
1562
1563 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1564 I != E; ++I) {
1565
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001566 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001567 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001568
Ted Kremenek186350f2008-04-23 20:12:28 +00001569 if (N) {
1570 N->markAsSink();
1571 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001572 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001573
Ted Kremenek186350f2008-04-23 20:12:28 +00001574 badArg = true;
1575 break;
1576 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001577 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001578
1579 if (badArg)
1580 continue;
1581
1582 // Dispatch to the plug-in transfer function.
1583
1584 unsigned size = Dst.size();
1585 SaveOr OldHasGen(Builder->HasGeneratedNode);
1586 EvalCall(Dst, CE, L, *DI);
1587
1588 // Handle the case where no nodes where generated. Auto-generate that
1589 // contains the updated state if we aren't generating sinks.
1590
1591 if (!Builder->BuildSinks && Dst.size() == size &&
1592 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001593 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001594 }
1595}
1596
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001597//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001598// Transfer function: Objective-C ivar references.
1599//===----------------------------------------------------------------------===//
1600
Ted Kremenekf5cae632009-02-28 20:50:43 +00001601static std::pair<const void*,const void*> EagerlyAssumeTag
1602 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1603
Ted Kremenekb2939022009-02-25 23:32:10 +00001604void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001605 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1606 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001607
1608 // Test if the previous node was as the same expression. This can happen
1609 // when the expression fails to evaluate to anything meaningful and
1610 // (as an optimization) we don't generate a node.
1611 ProgramPoint P = Pred->getLocation();
1612 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1613 Dst.Add(Pred);
1614 continue;
1615 }
1616
Ted Kremenek48af2a92009-02-25 22:32:02 +00001617 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001618 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001619 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001620 // First assume that the condition is true.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001621 if (const GRState *stateTrue = state->assume(V, true)) {
1622 stateTrue = stateTrue->bindExpr(Ex, MakeConstantVal(1U, Ex));
Ted Kremenekb2939022009-02-25 23:32:10 +00001623 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001624 stateTrue, Pred));
1625 }
1626
1627 // Next, assume that the condition is false.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001628 if (const GRState *stateFalse = state->assume(V, false)) {
1629 stateFalse = stateFalse->bindExpr(Ex, MakeConstantVal(0U, Ex));
Ted Kremenekb2939022009-02-25 23:32:10 +00001630 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001631 stateFalse, Pred));
1632 }
1633 }
1634 else
1635 Dst.Add(Pred);
1636 }
1637}
1638
1639//===----------------------------------------------------------------------===//
1640// Transfer function: Objective-C ivar references.
1641//===----------------------------------------------------------------------===//
1642
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001643void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1644 NodeTy* Pred, NodeSet& Dst,
1645 bool asLValue) {
1646
1647 Expr* Base = cast<Expr>(Ex->getBase());
1648 NodeSet Tmp;
1649 Visit(Base, Pred, Tmp);
1650
1651 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001652 const GRState* state = GetState(*I);
1653 SVal BaseVal = GetSVal(state, Base);
1654 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001655
1656 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001657 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001658 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001659 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001660 }
1661}
1662
1663//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001664// Transfer function: Objective-C fast enumeration 'for' statements.
1665//===----------------------------------------------------------------------===//
1666
1667void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1668 NodeTy* Pred, NodeSet& Dst) {
1669
1670 // ObjCForCollectionStmts are processed in two places. This method
1671 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1672 // statements within a basic block. This transfer function does two things:
1673 //
1674 // (1) binds the next container value to 'element'. This creates a new
1675 // node in the ExplodedGraph.
1676 //
1677 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1678 // whether or not the container has any more elements. This value
1679 // will be tested in ProcessBranch. We need to explicitly bind
1680 // this value because a container can contain nil elements.
1681 //
1682 // FIXME: Eventually this logic should actually do dispatches to
1683 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1684 // This will require simulating a temporary NSFastEnumerationState, either
1685 // through an SVal or through the use of MemRegions. This value can
1686 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1687 // terminates we reclaim the temporary (it goes out of scope) and we
1688 // we can test if the SVal is 0 or if the MemRegion is null (depending
1689 // on what approach we take).
1690 //
1691 // For now: simulate (1) by assigning either a symbol or nil if the
1692 // container is empty. Thus this transfer function will by default
1693 // result in state splitting.
1694
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001695 Stmt* elem = S->getElement();
1696 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001697
1698 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001699 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001700 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001701 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1702 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1703 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001704 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001705
1706 NodeSet Tmp;
1707 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001708
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001709 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1710 const GRState* state = GetState(*I);
1711 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1712 }
1713}
1714
1715void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1716 NodeTy* Pred, NodeSet& Dst,
1717 SVal ElementV) {
1718
1719
Ted Kremenekaf337412008-11-12 19:24:17 +00001720
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001721 // Get the current state. Use 'EvalLocation' to determine if it is a null
1722 // pointer, etc.
1723 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001724
Ted Kremenek8c354752008-12-16 22:02:27 +00001725 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1726 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001727 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001728
Ted Kremenekb65be702009-06-18 01:23:53 +00001729 const GRState *state = GetState(Pred);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001730
Ted Kremenekaf337412008-11-12 19:24:17 +00001731 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001732 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001733 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
Ted Kremenekb65be702009-06-18 01:23:53 +00001734 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenekaf337412008-11-12 19:24:17 +00001735
Ted Kremenekaf337412008-11-12 19:24:17 +00001736 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001737 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
Ted Kremenekb65be702009-06-18 01:23:53 +00001738 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001739
1740 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1741 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1742 // FIXME: The proper thing to do is to really iterate over the
1743 // container. We will do this with dispatch logic to the store.
1744 // For now, just 'conjure' up a symbolic value.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001745 QualType T = R->getValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001746 assert (Loc::IsLocType(T));
1747 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001748 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1749 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
Ted Kremenekb65be702009-06-18 01:23:53 +00001750 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001751
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001752 // Bind the location to 'nil' on the false branch.
1753 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
Ted Kremenekb65be702009-06-18 01:23:53 +00001754 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001755 }
1756
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001757 // Create the new nodes.
1758 MakeNode(Dst, S, Pred, hasElems);
1759 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001760}
1761
1762//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001763// Transfer function: Objective-C message expressions.
1764//===----------------------------------------------------------------------===//
1765
1766void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1767 NodeSet& Dst){
1768
1769 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1770 Pred, Dst);
1771}
1772
1773void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001774 ObjCMessageExpr::arg_iterator AI,
1775 ObjCMessageExpr::arg_iterator AE,
1776 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001777 if (AI == AE) {
1778
1779 // Process the receiver.
1780
1781 if (Expr* Receiver = ME->getReceiver()) {
1782 NodeSet Tmp;
1783 Visit(Receiver, Pred, Tmp);
1784
1785 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1786 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1787
1788 return;
1789 }
1790
1791 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1792 return;
1793 }
1794
1795 NodeSet Tmp;
1796 Visit(*AI, Pred, Tmp);
1797
1798 ++AI;
1799
1800 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1801 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1802}
1803
1804void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1805 NodeTy* Pred,
1806 NodeSet& Dst) {
1807
1808 // FIXME: More logic for the processing the method call.
1809
Ted Kremeneka8538d92009-02-13 01:45:31 +00001810 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001811 bool RaisesException = false;
1812
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001813
1814 if (Expr* Receiver = ME->getReceiver()) {
1815
Ted Kremeneka8538d92009-02-13 01:45:31 +00001816 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001817
Ted Kremenek21fe8372009-02-19 04:06:22 +00001818 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001819 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001820 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001821
1822 if (N) {
1823 N->markAsSink();
1824 UndefReceivers.insert(N);
1825 }
1826
1827 return;
1828 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001829
Ted Kremenek21fe8372009-02-19 04:06:22 +00001830 // "Assume" that the receiver is not NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001831 const GRState *StNotNull = state->assume(L, true);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001832
1833 // "Assume" that the receiver is NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001834 const GRState *StNull = state->assume(L, false);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001835
Ted Kremeneka591bc02009-06-18 22:57:13 +00001836 if (StNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001837 QualType RetTy = ME->getType();
1838
Ted Kremenek21fe8372009-02-19 04:06:22 +00001839 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001840 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001841 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001842 // The [0 ...] expressions will return garbage. Flag either an
1843 // explicit or implicit error. Because of the structure of this
1844 // function we currently do not bifurfacte the state graph at
1845 // this point.
1846 // FIXME: We should bifurcate and fill the returned struct with
1847 // garbage.
1848 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1849 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001850 if (StNotNull)
Ted Kremenek899b3de2009-04-08 03:07:17 +00001851 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001852 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001853 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001854 }
1855 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001856 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001857 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001858 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001859 if (RetTy != Ctx.VoidTy) {
1860 if (BR.getParentMap().isConsumedExpr(ME)) {
1861 // sizeof(void *)
1862 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1863 // sizeof(return type)
1864 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001865
Ted Kremenekfe630b92009-04-09 05:45:56 +00001866 if(voidPtrSize < returnTypeSize) {
1867 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1868 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001869 if(StNotNull)
Ted Kremenekfe630b92009-04-09 05:45:56 +00001870 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001871 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001872 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001873 }
1874 }
Ted Kremeneka591bc02009-06-18 22:57:13 +00001875 else if (!StNotNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001876 // Handle the safe cases where the return value is 0 if the
1877 // receiver is nil.
1878 //
1879 // FIXME: For now take the conservative approach that we only
1880 // return null values if we *know* that the receiver is nil.
1881 // This is because we can have surprises like:
1882 //
1883 // ... = [[NSScreens screens] objectAtIndex:0];
1884 //
1885 // What can happen is that [... screens] could return nil, but
1886 // it most likely isn't nil. We should assume the semantics
1887 // of this case unless we have *a lot* more knowledge.
1888 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001889 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001890 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001891 return;
1892 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001893 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001894 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001895 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001896 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001897 // of this method should assume that the receiver is not nil.
1898 if (!StNotNull)
1899 return;
1900
Ted Kremenekda9ae602009-04-08 18:51:08 +00001901 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001902 }
1903
Ted Kremeneke448ab42008-05-01 18:33:28 +00001904 // Check if the "raise" message was sent.
1905 if (ME->getSelector() == RaiseSel)
1906 RaisesException = true;
1907 }
1908 else {
1909
1910 IdentifierInfo* ClsName = ME->getClassName();
1911 Selector S = ME->getSelector();
1912
1913 // Check for special instance methods.
1914
1915 if (!NSExceptionII) {
1916 ASTContext& Ctx = getContext();
1917
1918 NSExceptionII = &Ctx.Idents.get("NSException");
1919 }
1920
1921 if (ClsName == NSExceptionII) {
1922
1923 enum { NUM_RAISE_SELECTORS = 2 };
1924
1925 // Lazily create a cache of the selectors.
1926
1927 if (!NSExceptionInstanceRaiseSelectors) {
1928
1929 ASTContext& Ctx = getContext();
1930
1931 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1932
1933 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1934 unsigned idx = 0;
1935
1936 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001937 II.push_back(&Ctx.Idents.get("raise"));
1938 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001939 NSExceptionInstanceRaiseSelectors[idx++] =
1940 Ctx.Selectors.getSelector(II.size(), &II[0]);
1941
1942 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001943 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001944 NSExceptionInstanceRaiseSelectors[idx++] =
1945 Ctx.Selectors.getSelector(II.size(), &II[0]);
1946 }
1947
1948 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1949 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1950 RaisesException = true; break;
1951 }
1952 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001953 }
1954
1955 // Check for any arguments that are uninitialized/undefined.
1956
1957 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1958 I != E; ++I) {
1959
Ted Kremeneka8538d92009-02-13 01:45:31 +00001960 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001961
1962 // Generate an error node for passing an uninitialized/undefined value
1963 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001964 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001965
1966 if (N) {
1967 N->markAsSink();
1968 MsgExprUndefArgs[N] = *I;
1969 }
1970
1971 return;
1972 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001973 }
1974
1975 // Check if we raise an exception. For now treat these as sinks. Eventually
1976 // we will want to handle exceptions properly.
1977
1978 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1979
1980 if (RaisesException)
1981 Builder->BuildSinks = true;
1982
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001983 // Dispatch to plug-in transfer function.
1984
1985 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001986 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001987
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001988 EvalObjCMessageExpr(Dst, ME, Pred);
1989
1990 // Handle the case where no nodes where generated. Auto-generate that
1991 // contains the updated state if we aren't generating sinks.
1992
Ted Kremenekb0533962008-04-18 20:35:30 +00001993 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001994 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001995}
1996
1997//===----------------------------------------------------------------------===//
1998// Transfer functions: Miscellaneous statements.
1999//===----------------------------------------------------------------------===//
2000
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002001void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2002 QualType PtrTy,
2003 Expr* CastE, NodeTy* Pred,
2004 NodeSet& Dst) {
2005 if (!V.isUnknownOrUndef()) {
2006 // FIXME: Determine if the number of bits of the target type is
2007 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00002008 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00002009 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2010 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002011 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002012 else
2013 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002014}
2015
2016
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002017void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002018 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002019 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002020 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002021
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002022 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002023 T = ExCast->getTypeAsWritten();
2024
Zhongxing Xued340f72008-10-22 08:02:16 +00002025 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002026 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002027 else
2028 Visit(Ex, Pred, S1);
2029
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002030 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00002031 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002032 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002033 Dst.Add(*I1);
2034
Ted Kremenek874d63f2008-01-24 02:02:54 +00002035 return;
2036 }
2037
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002038 // FIXME: The rest of this should probably just go into EvalCall, and
2039 // let the transfer function object be responsible for constructing
2040 // nodes.
2041
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002042 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00002043 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002044 const GRState* state = GetState(N);
2045 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00002046 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002047
2048 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002049 if (V.isUnknown()) {
2050 Dst.Add(N);
2051 continue;
2052 }
2053
2054 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00002055 if (V.isUndef())
2056 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002057
2058 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002059 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00002060 C.getCanonicalType(ExTy).getUnqualifiedType())
2061 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002062
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002063 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002064 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002065 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002066 continue;
2067 }
2068
2069 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00002070 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002071 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002072 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002073 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00002074 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002075 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002076 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002077
Ted Kremenekc5302912009-03-05 20:22:13 +00002078 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00002079 }
2080
2081 // Just pass through function and block pointers.
2082 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2083 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00002084 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002085 }
2086
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002087 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00002088 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002089 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00002090 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002091
2092 // Are we casting from an array to a pointer? If so just pass on
2093 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00002094 if (T->isPointerType())
2095 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002096
2097 // Are we casting from an array to an integer? If so, cast the decayed
2098 // pointer value to an integer.
2099 assert(T->isIntegerType());
2100 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2101 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002102 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00002103 continue;
2104 }
2105
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002106 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002107 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2108 // FIXME: For TypedViewRegions, we should handle the case where the
2109 // underlying symbolic pointer is a function pointer or
2110 // block pointer.
2111
2112 // FIXME: We should handle the case where we strip off view layers to get
2113 // to a desugared type.
2114
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002115 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00002116 // We get a symbolic function pointer for a dereference of a function
2117 // pointer, but it is of function type. Example:
2118
2119 // struct FPRec {
2120 // void (*my_func)(int * x);
2121 // };
2122 //
2123 // int bar(int x);
2124 //
2125 // int f1_a(struct FPRec* foo) {
2126 // int x;
2127 // (*foo->my_func)(&x);
2128 // return bar(x)+1; // no-warning
2129 // }
2130
2131 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002132
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002133 const MemRegion* R = RV->getRegion();
2134 StoreManager& StoreMgr = getStoreManager();
2135
2136 // Delegate to store manager to get the result of casting a region
2137 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002138 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002139
2140 // Inspect the result. If the MemRegion* returned is NULL, this
2141 // expression evaluates to UnknownVal.
2142 R = Res.getRegion();
2143 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2144
2145 // Generate the new node in the ExplodedGraph.
2146 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002147 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002148 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002149 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002150 DispatchCast: {
2151 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2152 EvalCast(V, CastE->getType())));
2153 continue;
2154 }
2155
2156 PassThrough: {
2157 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2158 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002159 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002160}
2161
Ted Kremenek4f090272008-10-27 21:54:31 +00002162void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002163 NodeTy* Pred, NodeSet& Dst,
2164 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002165 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2166 NodeSet Tmp;
2167 Visit(ILE, Pred, Tmp);
2168
2169 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002170 const GRState* state = GetState(*I);
2171 SVal ILV = GetSVal(state, ILE);
2172 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002173
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002174 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002175 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002176 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002177 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002178 }
2179}
2180
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002181void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002182
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002183 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002184 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002185
2186 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002187 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002188
Ted Kremenekefd59942008-12-08 22:47:34 +00002189 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002190 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002191
2192 // FIXME: static variables may have an initializer, but the second
2193 // time a function is called those values may not be current.
2194 NodeSet Tmp;
2195
Ted Kremenekaf337412008-11-12 19:24:17 +00002196 if (InitEx)
2197 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002198
2199 if (Tmp.empty())
2200 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002201
2202 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002203 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002204 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002205
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002206 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2207 QualType T = getContext().getCanonicalType(VD->getType());
2208 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2209 // FIXME: Handle multi-dimensional VLAs.
2210
2211 Expr* SE = VLA->getSizeExpr();
2212 SVal Size = GetSVal(state, SE);
2213
2214 if (Size.isUndef()) {
2215 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2216 N->markAsSink();
2217 ExplicitBadSizedVLA.insert(N);
2218 }
2219 continue;
2220 }
2221
Ted Kremeneka591bc02009-06-18 22:57:13 +00002222 const GRState* zeroState = state->assume(Size, false);
2223 state = state->assume(Size, true);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002224
Ted Kremeneka591bc02009-06-18 22:57:13 +00002225 if (zeroState) {
2226 if (NodeTy* N = Builder->generateNode(DS, zeroState, Pred)) {
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002227 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00002228 if (state)
2229 ImplicitBadSizedVLA.insert(N);
2230 else
2231 ExplicitBadSizedVLA.insert(N);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002232 }
2233 }
2234
Ted Kremeneka591bc02009-06-18 22:57:13 +00002235 if (!state)
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002236 continue;
2237 }
2238
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002239 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002240 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002241 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002242 QualType T = VD->getType();
2243
2244 // Recover some path-sensitivity if a scalar value evaluated to
2245 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002246 if (InitVal.isUnknown() ||
2247 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002248 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002249 }
2250
Ted Kremeneka8538d92009-02-13 01:45:31 +00002251 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002252
2253 // The next thing to do is check if the GRTransferFuncs object wants to
2254 // update the state based on the new binding. If the GRTransferFunc
2255 // object doesn't do anything, just auto-propagate the current state.
2256 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2257 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2258 InitVal);
2259 }
2260 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002261 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002262 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002263 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002264 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002265}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002266
Ted Kremenekf75b1862008-10-30 17:47:32 +00002267namespace {
2268 // This class is used by VisitInitListExpr as an item in a worklist
2269 // for processing the values contained in an InitListExpr.
2270class VISIBILITY_HIDDEN InitListWLItem {
2271public:
2272 llvm::ImmutableList<SVal> Vals;
2273 GRExprEngine::NodeTy* N;
2274 InitListExpr::reverse_iterator Itr;
2275
2276 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2277 InitListExpr::reverse_iterator itr)
2278 : Vals(vals), N(n), Itr(itr) {}
2279};
2280}
2281
2282
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002283void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2284 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002285
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002286 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002287 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002288 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002289
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002290 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002291
Ted Kremeneka49e3672008-10-30 23:14:36 +00002292 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002293
Ted Kremeneka49e3672008-10-30 23:14:36 +00002294 // Handle base case where the initializer has no elements.
2295 // e.g: static int* myArray[] = {};
2296 if (NumInitElements == 0) {
2297 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2298 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2299 return;
2300 }
2301
2302 // Create a worklist to process the initializers.
2303 llvm::SmallVector<InitListWLItem, 10> WorkList;
2304 WorkList.reserve(NumInitElements);
2305 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002306 InitListExpr::reverse_iterator ItrEnd = E->rend();
2307
Ted Kremeneka49e3672008-10-30 23:14:36 +00002308 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002309 while (!WorkList.empty()) {
2310 InitListWLItem X = WorkList.back();
2311 WorkList.pop_back();
2312
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002313 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002314 Visit(*X.Itr, X.N, Tmp);
2315
2316 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002317
Ted Kremenekf75b1862008-10-30 17:47:32 +00002318 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2319 // Get the last initializer value.
2320 state = GetState(*NI);
2321 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2322
2323 // Construct the new list of values by prepending the new value to
2324 // the already constructed list.
2325 llvm::ImmutableList<SVal> NewVals =
2326 getBasicVals().consVals(InitV, X.Vals);
2327
2328 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002329 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002330 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002331
Ted Kremenekf75b1862008-10-30 17:47:32 +00002332 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002333 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002334 }
2335 else {
2336 // Still some initializer values to go. Push them onto the worklist.
2337 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2338 }
2339 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002340 }
Ted Kremenek87903072008-10-30 18:34:31 +00002341
2342 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002343 }
2344
Ted Kremenek062e2f92008-11-13 06:10:40 +00002345 if (T->isUnionType() || T->isVectorType()) {
2346 // FIXME: to be implemented.
2347 // Note: That vectors can return true for T->isIntegerType()
2348 MakeNode(Dst, E, Pred, state);
2349 return;
2350 }
2351
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002352 if (Loc::IsLocType(T) || T->isIntegerType()) {
2353 assert (E->getNumInits() == 1);
2354 NodeSet Tmp;
2355 Expr* Init = E->getInit(0);
2356 Visit(Init, Pred, Tmp);
2357 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2358 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002359 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002360 }
2361 return;
2362 }
2363
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002364
2365 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2366 assert(0 && "unprocessed InitListExpr type");
2367}
Ted Kremenekf233d482008-02-05 00:26:40 +00002368
Sebastian Redl05189992008-11-11 17:56:53 +00002369/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2370void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2371 NodeTy* Pred,
2372 NodeSet& Dst) {
2373 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002374 uint64_t amt;
2375
2376 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002377 if (T == getContext().VoidTy) {
2378 // sizeof(void) == 1 byte.
2379 amt = 1;
2380 }
2381 else if (!T.getTypePtr()->isConstantSizeType()) {
2382 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002383 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002384 }
2385 else if (T->isObjCInterfaceType()) {
2386 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2387 // the compiler has laid out its representation. Just report Unknown
2388 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002389 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002390 }
2391 else {
2392 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002393 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002394 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002395 }
2396 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002397 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002398
Ted Kremenek0e561a32008-03-21 21:30:14 +00002399 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002400 BindExpr(GetState(Pred), Ex,
2401 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002402}
2403
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002404
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002405void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002406 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002407
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002408 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002409
2410 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002411 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002412
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002413 case UnaryOperator::Deref: {
2414
2415 Expr* Ex = U->getSubExpr()->IgnoreParens();
2416 NodeSet Tmp;
2417 Visit(Ex, Pred, Tmp);
2418
2419 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002420
Ted Kremeneka8538d92009-02-13 01:45:31 +00002421 const GRState* state = GetState(*I);
2422 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002423
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002424 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00002425 MakeNode(Dst, U, *I, BindExpr(state, U, location),
2426 ProgramPoint::PostLValueKind);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002427 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002428 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002429 }
2430
2431 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002432 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002433
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002434 case UnaryOperator::Real: {
2435
2436 Expr* Ex = U->getSubExpr()->IgnoreParens();
2437 NodeSet Tmp;
2438 Visit(Ex, Pred, Tmp);
2439
2440 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2441
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002442 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002443 if (Ex->getType()->isAnyComplexType()) {
2444 // Just report "Unknown."
2445 Dst.Add(*I);
2446 continue;
2447 }
2448
2449 // For all other types, UnaryOperator::Real is an identity operation.
2450 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002451 const GRState* state = GetState(*I);
2452 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002453 }
2454
2455 return;
2456 }
2457
2458 case UnaryOperator::Imag: {
2459
2460 Expr* Ex = U->getSubExpr()->IgnoreParens();
2461 NodeSet Tmp;
2462 Visit(Ex, Pred, Tmp);
2463
2464 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002465 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002466 if (Ex->getType()->isAnyComplexType()) {
2467 // Just report "Unknown."
2468 Dst.Add(*I);
2469 continue;
2470 }
2471
2472 // For all other types, UnaryOperator::Float returns 0.
2473 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002474 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002475 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002476 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002477 }
2478
2479 return;
2480 }
2481
2482 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002483 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002484 Dst.Add(Pred);
2485 return;
2486
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002487 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002488 case UnaryOperator::Extension: {
2489
2490 // Unary "+" is a no-op, similar to a parentheses. We still have places
2491 // where it may be a block-level expression, so we need to
2492 // generate an extra node that just propagates the value of the
2493 // subexpression.
2494
2495 Expr* Ex = U->getSubExpr()->IgnoreParens();
2496 NodeSet Tmp;
2497 Visit(Ex, Pred, Tmp);
2498
2499 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002500 const GRState* state = GetState(*I);
2501 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002502 }
2503
2504 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002505 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002506
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002507 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002508
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002509 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002510 Expr* Ex = U->getSubExpr()->IgnoreParens();
2511 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002512 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002513
2514 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002515 const GRState* state = GetState(*I);
2516 SVal V = GetSVal(state, Ex);
2517 state = BindExpr(state, U, V);
2518 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002519 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002520
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002521 return;
2522 }
2523
2524 case UnaryOperator::LNot:
2525 case UnaryOperator::Minus:
2526 case UnaryOperator::Not: {
2527
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002528 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002529 Expr* Ex = U->getSubExpr()->IgnoreParens();
2530 NodeSet Tmp;
2531 Visit(Ex, Pred, Tmp);
2532
2533 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002534 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002535
2536 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002537 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002538
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002539 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002540 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002541 continue;
2542 }
2543
Ted Kremenek60595da2008-11-15 04:01:56 +00002544// QualType DstT = getContext().getCanonicalType(U->getType());
2545// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2546//
2547// if (DstT != SrcT) // Perform promotions.
2548// V = EvalCast(V, DstT);
2549//
2550// if (V.isUnknownOrUndef()) {
2551// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2552// continue;
2553// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002554
2555 switch (U->getOpcode()) {
2556 default:
2557 assert(false && "Invalid Opcode.");
2558 break;
2559
2560 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002561 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002562 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002563 break;
2564
2565 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002566 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002567 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002568 break;
2569
2570 case UnaryOperator::LNot:
2571
2572 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2573 //
2574 // Note: technically we do "E == 0", but this is the same in the
2575 // transfer functions as "0 == E".
2576
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002577 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002578 Loc X = Loc::MakeNull(getBasicVals());
Zhongxing Xu262fd032009-05-20 09:00:16 +00002579 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002580 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002581 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002582 }
2583 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002584 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002585#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002586 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002587 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002588#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002589 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2590 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002591 continue;
2592#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002593 }
2594
2595 break;
2596 }
2597
Ted Kremeneka8538d92009-02-13 01:45:31 +00002598 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002599 }
2600
2601 return;
2602 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002603 }
2604
2605 // Handle ++ and -- (both pre- and post-increment).
2606
2607 assert (U->isIncrementDecrementOp());
2608 NodeSet Tmp;
2609 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002610 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002611
2612 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2613
Ted Kremeneka8538d92009-02-13 01:45:31 +00002614 const GRState* state = GetState(*I);
2615 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002616
2617 // Perform a load.
2618 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002619 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002620
2621 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2622
Ted Kremeneka8538d92009-02-13 01:45:31 +00002623 state = GetState(*I2);
2624 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002625
2626 // Propagate unknown and undefined values.
2627 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002628 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002629 continue;
2630 }
2631
Ted Kremenek21028dd2009-03-11 03:54:24 +00002632 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002633 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2634 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002635
Zhongxing Xu262fd032009-05-20 09:00:16 +00002636 SVal Result = EvalBinOp(state, Op, V2, MakeConstantVal(1U, U),
2637 U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002638
2639 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002640 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002641 Result = ValMgr.getConjuredSymbolVal(Ex,
2642 Builder->getCurrentBlockCount());
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002643
2644 // If the value is a location, ++/-- should always preserve
2645 // non-nullness. Check if the original value was non-null, and if so propagate
2646 // that constraint.
2647 if (Loc::IsLocType(U->getType())) {
Zhongxing Xu262fd032009-05-20 09:00:16 +00002648 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002649 ValMgr.makeZeroVal(U->getType()),
2650 getContext().IntTy);
2651
Ted Kremeneka591bc02009-06-18 22:57:13 +00002652 if (!state->assume(Constraint, true)) {
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002653 // It isn't feasible for the original value to be null.
2654 // Propagate this constraint.
Zhongxing Xu262fd032009-05-20 09:00:16 +00002655 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002656 ValMgr.makeZeroVal(U->getType()),
2657 getContext().IntTy);
2658
Ted Kremeneka591bc02009-06-18 22:57:13 +00002659 state = state->assume(Constraint, false);
2660 assert(state);
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002661 }
2662 }
2663 }
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002664
Ted Kremeneka591bc02009-06-18 22:57:13 +00002665 state = state->bindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002666
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002667 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002668 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002669 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002670 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002671}
2672
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002673void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2674 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2675}
2676
2677void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2678 AsmStmt::outputs_iterator I,
2679 AsmStmt::outputs_iterator E,
2680 NodeTy* Pred, NodeSet& Dst) {
2681 if (I == E) {
2682 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2683 return;
2684 }
2685
2686 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002687 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002688
2689 ++I;
2690
2691 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2692 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2693}
2694
2695void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2696 AsmStmt::inputs_iterator I,
2697 AsmStmt::inputs_iterator E,
2698 NodeTy* Pred, NodeSet& Dst) {
2699 if (I == E) {
2700
2701 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002702 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002703
2704 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2705 // which interprets the inline asm and stores proper results in the
2706 // outputs.
2707
Ted Kremeneka8538d92009-02-13 01:45:31 +00002708 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002709
2710 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2711 OE = A->end_outputs(); OI != OE; ++OI) {
2712
Ted Kremeneka8538d92009-02-13 01:45:31 +00002713 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002714 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002715
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002716 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002717 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002718 }
2719
Ted Kremeneka8538d92009-02-13 01:45:31 +00002720 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002721 return;
2722 }
2723
2724 NodeSet Tmp;
2725 Visit(*I, Pred, Tmp);
2726
2727 ++I;
2728
2729 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2730 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2731}
2732
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002733void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2734 assert (Builder && "GRStmtNodeBuilder must be defined.");
2735
2736 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002737
Ted Kremenek186350f2008-04-23 20:12:28 +00002738 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2739 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002740
Ted Kremenek729a9a22008-07-17 23:15:45 +00002741 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002742
Ted Kremenekb0533962008-04-18 20:35:30 +00002743 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002744
Ted Kremenekb0533962008-04-18 20:35:30 +00002745 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002746 MakeNode(Dst, S, Pred, GetState(Pred));
2747}
2748
Ted Kremenek02737ed2008-03-31 15:02:58 +00002749void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2750
2751 Expr* R = S->getRetValue();
2752
2753 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002754 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002755 return;
2756 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002757
Ted Kremenek5917d782008-11-21 00:27:44 +00002758 NodeSet Tmp;
2759 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002760
Ted Kremenek5917d782008-11-21 00:27:44 +00002761 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2762 SVal X = GetSVal((*I)->getState(), R);
2763
2764 // Check if we return the address of a stack variable.
2765 if (isa<loc::MemRegionVal>(X)) {
2766 // Determine if the value is on the stack.
2767 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002768
Ted Kremenek5917d782008-11-21 00:27:44 +00002769 if (R && getStateManager().hasStackStorage(R)) {
2770 // Create a special node representing the error.
2771 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2772 N->markAsSink();
2773 RetsStackAddr.insert(N);
2774 }
2775 continue;
2776 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002777 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002778 // Check if we return an undefined value.
2779 else if (X.isUndef()) {
2780 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2781 N->markAsSink();
2782 RetsUndef.insert(N);
2783 }
2784 continue;
2785 }
2786
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002787 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002788 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002789}
Ted Kremenek55deb972008-03-25 00:34:37 +00002790
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002791//===----------------------------------------------------------------------===//
2792// Transfer functions: Binary operators.
2793//===----------------------------------------------------------------------===//
2794
Ted Kremeneka8538d92009-02-13 01:45:31 +00002795const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002796 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002797
2798 // Divide by undefined? (potentially zero)
2799
2800 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002801 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002802
2803 if (DivUndef) {
2804 DivUndef->markAsSink();
2805 ExplicitBadDivides.insert(DivUndef);
2806 }
2807
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002808 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002809 }
2810
2811 // Check for divide/remainder-by-zero.
2812 // First, "assume" that the denominator is 0 or undefined.
Ted Kremeneka591bc02009-06-18 22:57:13 +00002813 const GRState* zeroState = state->assume(Denom, false);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002814
2815 // Second, "assume" that the denominator cannot be 0.
Ted Kremeneka591bc02009-06-18 22:57:13 +00002816 state = state->assume(Denom, true);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002817
Ted Kremeneka591bc02009-06-18 22:57:13 +00002818 // Create the node for the divide-by-zero (if it occurred).
2819 if (zeroState)
2820 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, zeroState, Pred)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002821 DivZeroNode->markAsSink();
2822
Ted Kremeneka591bc02009-06-18 22:57:13 +00002823 if (state)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002824 ImplicitBadDivides.insert(DivZeroNode);
2825 else
2826 ExplicitBadDivides.insert(DivZeroNode);
2827
2828 }
2829
Ted Kremeneka591bc02009-06-18 22:57:13 +00002830 return state;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002831}
2832
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002833void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002834 GRExprEngine::NodeTy* Pred,
2835 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002836
2837 NodeSet Tmp1;
2838 Expr* LHS = B->getLHS()->IgnoreParens();
2839 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002840
Ted Kremenek759623e2008-12-06 02:39:30 +00002841 // FIXME: Add proper support for ObjCKVCRefExpr.
2842 if (isa<ObjCKVCRefExpr>(LHS)) {
2843 Visit(RHS, Pred, Dst);
2844 return;
2845 }
2846
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002847 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002848 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002849 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002850 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002851
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002852 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002853
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002854 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002855
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002856 // Process the RHS.
2857
2858 NodeSet Tmp2;
2859 Visit(RHS, *I1, Tmp2);
2860
2861 // With both the LHS and RHS evaluated, process the operation itself.
2862
2863 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002864
Ted Kremeneka8538d92009-02-13 01:45:31 +00002865 const GRState* state = GetState(*I2);
2866 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002867
Ted Kremeneka8538d92009-02-13 01:45:31 +00002868 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002869 BinaryOperator::Opcode Op = B->getOpcode();
2870
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002871 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002872
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002873 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002874
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002875 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002876 // FIXME: Handle structs.
2877 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002878
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002879 if ((RightV.isUnknown() ||
2880 !getConstraintManager().canReasonAbout(RightV))
2881 && (Loc::IsLocType(T) ||
2882 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002883 unsigned Count = Builder->getCurrentBlockCount();
2884 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002885 }
2886
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002887 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002888 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002889 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2890 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002891 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002892 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002893
2894 case BinaryOperator::Div:
2895 case BinaryOperator::Rem:
2896
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002897 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002898 if (RHS->getType()->isIntegerType() &&
2899 RHS->getType()->isScalarType()) {
2900
Ted Kremeneka8538d92009-02-13 01:45:31 +00002901 state = CheckDivideZero(B, state, *I2, RightV);
2902 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002903 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002904
2905 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002906
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002907 default: {
2908
2909 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002910 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002911
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002912 // Process non-assignements except commas or short-circuited
2913 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002914
Zhongxing Xu262fd032009-05-20 09:00:16 +00002915 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002916
2917 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002918 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002919 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002920 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002921 }
2922 else
2923 Dst.Add(*I2);
2924
Ted Kremenek89063af2008-02-21 19:15:37 +00002925 continue;
2926 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002927
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002928 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002929
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002930 // The operands were *not* undefined, but the result is undefined.
2931 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002932
Ted Kremeneka8538d92009-02-13 01:45:31 +00002933 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002934 UndefNode->markAsSink();
2935 UndefResults.insert(UndefNode);
2936 }
2937
2938 continue;
2939 }
2940
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002941 // Otherwise, create a new node.
2942
Ted Kremeneka8538d92009-02-13 01:45:31 +00002943 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002944 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002945 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002946 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002947
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002948 assert (B->isCompoundAssignmentOp());
2949
Ted Kremenekcad29962009-02-07 00:52:24 +00002950 switch (Op) {
2951 default:
2952 assert(0 && "Invalid opcode for compound assignment.");
2953 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2954 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2955 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2956 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2957 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2958 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2959 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2960 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2961 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2962 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002963 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002964
2965 // Perform a load (the LHS). This performs the checks for
2966 // null dereferences, and so on.
2967 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002968 SVal location = GetSVal(state, LHS);
2969 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002970
2971 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2972
Ted Kremeneka8538d92009-02-13 01:45:31 +00002973 state = GetState(*I3);
2974 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002975
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002976 // Check for divide-by-zero.
2977 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002978 && RHS->getType()->isIntegerType()
2979 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002980
2981 // CheckDivideZero returns a new state where the denominator
2982 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002983 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002984
Ted Kremeneka8538d92009-02-13 01:45:31 +00002985 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002986 continue;
2987 }
2988
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002989 // Propagate undefined values (left-side).
2990 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002991 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002992 continue;
2993 }
2994
2995 // Propagate unknown values (left and right-side).
2996 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002997 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2998 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002999 continue;
3000 }
3001
3002 // At this point:
3003 //
3004 // The LHS is not Undef/Unknown.
3005 // The RHS is not Unknown.
3006
3007 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00003008 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00003009 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00003010
3011 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3012 CLHSTy = getContext().getCanonicalType(CTy);
3013
Ted Kremenek60595da2008-11-15 04:01:56 +00003014 QualType LTy = getContext().getCanonicalType(LHS->getType());
3015 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00003016
3017 // Promote LHS.
3018 V = EvalCast(V, CLHSTy);
3019
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003020 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003021 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00003022 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00003023 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003024 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003025 continue;
3026 }
3027
Ted Kremenek60595da2008-11-15 04:01:56 +00003028 // Compute the result of the operation.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003029 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3030 B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003031
3032 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003033 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003034 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003035 UndefNode->markAsSink();
3036 UndefResults.insert(UndefNode);
3037 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003038 continue;
3039 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003040
3041 // EXPERIMENTAL: "Conjured" symbols.
3042 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00003043
3044 SVal LHSVal;
3045
Ted Kremenek276c6ac2009-03-11 02:24:48 +00003046 if ((Result.isUnknown() ||
3047 !getConstraintManager().canReasonAbout(Result))
3048 && (Loc::IsLocType(CTy)
3049 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00003050
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003051 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003052
Ted Kremenek60595da2008-11-15 04:01:56 +00003053 // The symbolic value is actually for the type of the left-hand side
3054 // expression, not the computation type, as this is the value the
3055 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00003056 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00003057
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00003058 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00003059 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003060 }
Ted Kremenek60595da2008-11-15 04:01:56 +00003061 else {
3062 // The left-hand side may bind to a different value then the
3063 // computation type.
3064 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3065 }
3066
Ted Kremeneka8538d92009-02-13 01:45:31 +00003067 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3068 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003069 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00003070 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003071 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003072}
Ted Kremenekee985462008-01-16 18:18:48 +00003073
3074//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003075// Transfer-function Helpers.
3076//===----------------------------------------------------------------------===//
3077
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003078void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003079 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00003080 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003081 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003082
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003083 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003084 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003085
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003086 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003087 MakeNode(Dst, Ex, Pred, *I);
3088}
3089
Ted Kremeneka8538d92009-02-13 01:45:31 +00003090void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003091 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003092 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003093
Ted Kremeneka8538d92009-02-13 01:45:31 +00003094 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003095 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003096}
3097
Zhongxing Xu262fd032009-05-20 09:00:16 +00003098SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3099 SVal L, SVal R, QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003100
3101 if (L.isUndef() || R.isUndef())
3102 return UndefinedVal();
3103
3104 if (L.isUnknown() || R.isUnknown())
3105 return UnknownVal();
3106
3107 if (isa<Loc>(L)) {
3108 if (isa<Loc>(R))
3109 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3110 else
Zhongxing Xu262fd032009-05-20 09:00:16 +00003111 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003112 }
3113
3114 if (isa<Loc>(R)) {
3115 // Support pointer arithmetic where the increment/decrement operand
3116 // is on the left and the pointer on the right.
3117
3118 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3119
3120 // Commute the operands.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003121 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003122 }
3123 else
3124 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003125 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003126}
3127
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003128//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003129// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003130//===----------------------------------------------------------------------===//
3131
Ted Kremenekaa66a322008-01-16 21:46:15 +00003132#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003133static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003134static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003135
Ted Kremenekaa66a322008-01-16 21:46:15 +00003136namespace llvm {
3137template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003138struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003139 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003140
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003141 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3142
3143 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003144 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003145 GraphPrintCheckerState->isUndefDeref(N) ||
3146 GraphPrintCheckerState->isUndefStore(N) ||
3147 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003148 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3149 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003150 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003151 GraphPrintCheckerState->isBadCall(N) ||
3152 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003153 return "color=\"red\",style=\"filled\"";
3154
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003155 if (GraphPrintCheckerState->isNoReturnCall(N))
3156 return "color=\"blue\",style=\"filled\"";
3157
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003158 return "";
3159 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003160
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003161 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003162 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003163
3164 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003165 ProgramPoint Loc = N->getLocation();
3166
3167 switch (Loc.getKind()) {
3168 case ProgramPoint::BlockEntranceKind:
3169 Out << "Block Entrance: B"
3170 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3171 break;
3172
3173 case ProgramPoint::BlockExitKind:
3174 assert (false);
3175 break;
3176
Ted Kremenekaa66a322008-01-16 21:46:15 +00003177 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003178 if (isa<PostStmt>(Loc)) {
3179 const PostStmt& L = cast<PostStmt>(Loc);
3180 Stmt* S = L.getStmt();
3181 SourceLocation SLoc = S->getLocStart();
3182
3183 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3184 llvm::raw_os_ostream OutS(Out);
3185 S->printPretty(OutS);
3186 OutS.flush();
3187
3188 if (SLoc.isFileID()) {
3189 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003190 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3191 << " col="
3192 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3193 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003194 }
3195
Ted Kremenek7090d542009-05-07 18:27:16 +00003196 if (isa<PostLoad>(Loc))
3197 Out << "\\lPostLoad\\l;";
3198 else if (isa<PostStore>(Loc))
3199 Out << "\\lPostStore\\l";
3200 else if (isa<PostLValue>(Loc))
3201 Out << "\\lPostLValue\\l";
3202 else if (isa<PostLocationChecksSucceed>(Loc))
3203 Out << "\\lPostLocationChecksSucceed\\l";
3204 else if (isa<PostNullCheckFailed>(Loc))
3205 Out << "\\lPostNullCheckFailed\\l";
3206
Ted Kremenek8c354752008-12-16 22:02:27 +00003207 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3208 Out << "\\|Implicit-Null Dereference.\\l";
3209 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3210 Out << "\\|Explicit-Null Dereference.\\l";
3211 else if (GraphPrintCheckerState->isUndefDeref(N))
3212 Out << "\\|Dereference of undefialied value.\\l";
3213 else if (GraphPrintCheckerState->isUndefStore(N))
3214 Out << "\\|Store to Undefined Loc.";
3215 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3216 Out << "\\|Explicit divide-by zero or undefined value.";
3217 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3218 Out << "\\|Implicit divide-by zero or undefined value.";
3219 else if (GraphPrintCheckerState->isUndefResult(N))
3220 Out << "\\|Result of operation is undefined.";
3221 else if (GraphPrintCheckerState->isNoReturnCall(N))
3222 Out << "\\|Call to function marked \"noreturn\".";
3223 else if (GraphPrintCheckerState->isBadCall(N))
3224 Out << "\\|Call to NULL/Undefined.";
3225 else if (GraphPrintCheckerState->isUndefArg(N))
3226 Out << "\\|Argument in call is undefined";
3227
3228 break;
3229 }
3230
Ted Kremenekaa66a322008-01-16 21:46:15 +00003231 const BlockEdge& E = cast<BlockEdge>(Loc);
3232 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3233 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003234
3235 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003236
3237 SourceLocation SLoc = T->getLocStart();
3238
Ted Kremenekb38911f2008-01-30 23:03:39 +00003239 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003240
Ted Kremeneka95d3752008-09-13 05:16:45 +00003241 llvm::raw_os_ostream OutS(Out);
3242 E.getSrc()->printTerminator(OutS);
3243 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003244
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003245 if (SLoc.isFileID()) {
3246 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003247 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3248 << " col="
3249 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003250 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003251
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003252 if (isa<SwitchStmt>(T)) {
3253 Stmt* Label = E.getDst()->getLabel();
3254
3255 if (Label) {
3256 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3257 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003258 llvm::raw_os_ostream OutS(Out);
3259 C->getLHS()->printPretty(OutS);
3260 OutS.flush();
3261
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003262 if (Stmt* RHS = C->getRHS()) {
3263 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003264 RHS->printPretty(OutS);
3265 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003266 }
3267
3268 Out << ":";
3269 }
3270 else {
3271 assert (isa<DefaultStmt>(Label));
3272 Out << "\\ldefault:";
3273 }
3274 }
3275 else
3276 Out << "\\l(implicit) default:";
3277 }
3278 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003279 // FIXME
3280 }
3281 else {
3282 Out << "\\lCondition: ";
3283 if (*E.getSrc()->succ_begin() == E.getDst())
3284 Out << "true";
3285 else
3286 Out << "false";
3287 }
3288
3289 Out << "\\l";
3290 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003291
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003292 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3293 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003294 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003295 }
3296 }
3297
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003298 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003299
Ted Kremenekb65be702009-06-18 01:23:53 +00003300 const GRState *state = N->getState();
3301 state->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003302
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003303 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003304 return Out.str();
3305 }
3306};
3307} // end llvm namespace
3308#endif
3309
Ted Kremenekffe0f432008-03-07 22:58:01 +00003310#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003311template <typename ITERATOR>
3312GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3313
3314template <>
3315GRExprEngine::NodeTy*
3316GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3317 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3318 return I->first;
3319}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003320#endif
3321
3322void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003323#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003324 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003325 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003326
3327 // Flush any outstanding reports to make sure we cover all the nodes.
3328 // This does not cause them to get displayed.
3329 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3330 const_cast<BugType*>(*I)->FlushReports(BR);
3331
3332 // Iterate through the reports and get their nodes.
3333 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3334 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3335 const BugReportEquivClass& EQ = *I2;
3336 const BugReport &R = **EQ.begin();
3337 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3338 if (N) Src.push_back(N);
3339 }
3340 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003341
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003342 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003343 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003344 else {
3345 GraphPrintCheckerState = this;
3346 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003347
Ted Kremenekffe0f432008-03-07 22:58:01 +00003348 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003349
3350 GraphPrintCheckerState = NULL;
3351 GraphPrintSourceManager = NULL;
3352 }
3353#endif
3354}
3355
3356void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3357#ifndef NDEBUG
3358 GraphPrintCheckerState = this;
3359 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003360
Ted Kremenekcf118d42009-02-04 23:49:09 +00003361 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003362
Ted Kremenekcf118d42009-02-04 23:49:09 +00003363 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003364 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003365 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003366 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003367
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003368 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003369 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003370#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003371}