blob: b740bf029b6b64b0b9ca23f64a8d7b05dea148cf [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);
184 bool isFeasible = false;
185 const GRState *newState = Assume(state, Constraint, true,
186 isFeasible);
187 if (newState) state = newState;
188 }
189 }
190
191 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000192}
193
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000194//===----------------------------------------------------------------------===//
195// Top-level transfer function logic (Dispatcher).
196//===----------------------------------------------------------------------===//
197
198void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
199
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000200 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
201 S->getLocStart(),
202 "Error evaluating statement");
203
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000204 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000205 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000206
207 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000208 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000209 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000210
211 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000212 if (BatchAuditor)
213 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000214
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000215 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000216 SymbolReaper SymReaper(Liveness, SymMgr);
217 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
218 CurrentStmt, SymReaper)
219 : EntryNode->getState();
220
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000223
Ted Kremenek241677a2009-01-21 22:26:05 +0000224 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000225 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000226 else {
227 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000228 SaveOr OldHasGen(Builder->HasGeneratedNode);
229
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000230 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
231 Builder->PurgingDeadSymbols = true;
232
Ted Kremenek729a9a22008-07-17 23:15:45 +0000233 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000234 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
237 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 bool HasAutoGenerated = false;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243
244 NodeSet Dst;
245
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000246 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000247 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
248
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000249 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000250 Visit(S, *I, Dst);
251
252 // Do we need to auto-generate a node? We only need to do this to generate
253 // a node with a "cleaned" state; GRCoreEngine will actually handle
254 // auto-transitions for other cases.
255 if (Dst.size() == 1 && *Dst.begin() == EntryNode
256 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
257 HasAutoGenerated = true;
258 builder.generateNode(S, GetState(EntryNode), *I);
259 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000260 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000261
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000263 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000264 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000265
266 // FIXME: Consolidate.
267 StateMgr.CurrentStmt = 0;
268 CurrentStmt = 0;
269
Ted Kremenek846d4e92008-04-24 23:35:58 +0000270 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000271}
272
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000273void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
274 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
275 S->getLocStart(),
276 "Error evaluating statement");
277
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000278 // FIXME: add metadata to the CFG so that we can disable
279 // this check when we KNOW that there is no block-level subexpression.
280 // The motivation is that this check requires a hashtable lookup.
281
282 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
283 Dst.Add(Pred);
284 return;
285 }
286
287 switch (S->getStmtClass()) {
288
289 default:
290 // Cases we intentionally have "default" handle:
291 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
292
293 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
294 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000295
296 case Stmt::ArraySubscriptExprClass:
297 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
298 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000299
300 case Stmt::AsmStmtClass:
301 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
302 break;
303
304 case Stmt::BinaryOperatorClass: {
305 BinaryOperator* B = cast<BinaryOperator>(S);
306
307 if (B->isLogicalOp()) {
308 VisitLogicalExpr(B, Pred, Dst);
309 break;
310 }
311 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000312 const GRState* state = GetState(Pred);
313 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000314 break;
315 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000316
Ted Kremenek48af2a92009-02-25 22:32:02 +0000317 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
318 NodeSet Tmp;
319 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000320 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000321 }
322 else
323 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
324
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000325 break;
326 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000327
Douglas Gregorb4609802008-11-14 16:09:21 +0000328 case Stmt::CallExprClass:
329 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000330 CallExpr* C = cast<CallExpr>(S);
331 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000332 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000333 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000334
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000335 // FIXME: ChooseExpr is really a constant. We need to fix
336 // the CFG do not model them as explicit control-flow.
337
338 case Stmt::ChooseExprClass: { // __builtin_choose_expr
339 ChooseExpr* C = cast<ChooseExpr>(S);
340 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
341 break;
342 }
343
344 case Stmt::CompoundAssignOperatorClass:
345 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
346 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000347
348 case Stmt::CompoundLiteralExprClass:
349 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
350 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000351
352 case Stmt::ConditionalOperatorClass: { // '?' operator
353 ConditionalOperator* C = cast<ConditionalOperator>(S);
354 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
355 break;
356 }
357
358 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000359 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000360 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000361 break;
362
363 case Stmt::DeclStmtClass:
364 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
365 break;
366
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000367 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000368 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000369 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000370 VisitCast(C, C->getSubExpr(), Pred, Dst);
371 break;
372 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000373
374 case Stmt::InitListExprClass:
375 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
376 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000377
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000378 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000379 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
380 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000381
382 case Stmt::ObjCIvarRefExprClass:
383 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
384 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000385
386 case Stmt::ObjCForCollectionStmtClass:
387 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
388 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000389
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000390 case Stmt::ObjCMessageExprClass: {
391 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
392 break;
393 }
394
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000395 case Stmt::ObjCAtThrowStmtClass: {
396 // FIXME: This is not complete. We basically treat @throw as
397 // an abort.
398 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
399 Builder->BuildSinks = true;
400 MakeNode(Dst, S, Pred, GetState(Pred));
401 break;
402 }
403
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000404 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000405 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000406 break;
407
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000408 case Stmt::ReturnStmtClass:
409 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
410 break;
411
Sebastian Redl05189992008-11-11 17:56:53 +0000412 case Stmt::SizeOfAlignOfExprClass:
413 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000414 break;
415
416 case Stmt::StmtExprClass: {
417 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000418
419 if (SE->getSubStmt()->body_empty()) {
420 // Empty statement expression.
421 assert(SE->getType() == getContext().VoidTy
422 && "Empty statement expression must have void type.");
423 Dst.Add(Pred);
424 break;
425 }
426
427 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
428 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000429 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000430 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000431 else
432 Dst.Add(Pred);
433
434 break;
435 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000436
437 case Stmt::StringLiteralClass:
438 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
439 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000440
Ted Kremenek72374592009-03-18 23:49:26 +0000441 case Stmt::UnaryOperatorClass: {
442 UnaryOperator *U = cast<UnaryOperator>(S);
443 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
444 NodeSet Tmp;
445 VisitUnaryOperator(U, Pred, Tmp, false);
446 EvalEagerlyAssume(Dst, Tmp, U);
447 }
448 else
449 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000450 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000451 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000452 }
453}
454
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000455void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000456
457 Ex = Ex->IgnoreParens();
458
459 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
460 Dst.Add(Pred);
461 return;
462 }
463
464 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000465
466 case Stmt::ArraySubscriptExprClass:
467 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
468 return;
469
470 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000471 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000472 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
473 return;
474
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000475 case Stmt::ObjCIvarRefExprClass:
476 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
477 return;
478
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000479 case Stmt::UnaryOperatorClass:
480 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
481 return;
482
483 case Stmt::MemberExprClass:
484 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
485 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000486
Ted Kremenek4f090272008-10-27 21:54:31 +0000487 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000488 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000489 return;
490
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000491 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek868210e2009-04-21 23:53:32 +0000492 case Stmt::ObjCKVCRefExprClass:
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000493 // FIXME: Property assignments are lvalues, but not really "locations".
494 // e.g.: self.x = something;
495 // Here the "self.x" really can translate to a method call (setter) when
496 // the assignment is made. Moreover, the entire assignment expression
497 // evaluate to whatever "something" is, not calling the "getter" for
498 // the property (which would make sense since it can have side effects).
499 // We'll probably treat this as a location, but not one that we can
500 // take the address of. Perhaps we need a new SVal class for cases
501 // like thsis?
502 // Note that we have a similar problem for bitfields, since they don't
503 // have "locations" in the sense that we can take their address.
504 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000505 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000506
507 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000508 const GRState* state = GetState(Pred);
509 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
510 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000511 return;
512 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000513
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000514 default:
515 // Arbitrary subexpressions can return aggregate temporaries that
516 // can be used in a lvalue context. We need to enhance our support
517 // of such temporaries in both the environment and the store, so right
518 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000519 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000520 "Other kinds of expressions with non-aggregate/union types do"
521 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000522
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000523 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000524 }
525}
526
527//===----------------------------------------------------------------------===//
528// Block entrance. (Update counters).
529//===----------------------------------------------------------------------===//
530
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000531bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000532 GRBlockCounter BC) {
533
534 return BC.getNumVisited(B->getBlockID()) < 3;
535}
536
537//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +0000538// Generic node creation.
539//===----------------------------------------------------------------------===//
540
541GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
542 NodeTy* Pred,
543 const GRState* St,
544 ProgramPoint::Kind K,
545 const void *tag) {
546
547 assert (Builder && "GRStmtNodeBuilder not present.");
548 SaveAndRestore<const void*> OldTag(Builder->Tag);
549 Builder->Tag = tag;
550 return Builder->MakeNode(Dst, S, Pred, St, K);
551}
552
553//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000554// Branch processing.
555//===----------------------------------------------------------------------===//
556
Ted Kremeneka8538d92009-02-13 01:45:31 +0000557const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000558 Stmt* Terminator,
559 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000560
561 switch (Terminator->getStmtClass()) {
562 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000563 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000564
565 case Stmt::BinaryOperatorClass: { // '&&' and '||'
566
567 BinaryOperator* B = cast<BinaryOperator>(Terminator);
568 BinaryOperator::Opcode Op = B->getOpcode();
569
570 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
571
572 // For &&, if we take the true branch, then the value of the whole
573 // expression is that of the RHS expression.
574 //
575 // For ||, if we take the false branch, then the value of the whole
576 // expression is that of the RHS expression.
577
578 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
579 (Op == BinaryOperator::LOr && !branchTaken)
580 ? B->getRHS() : B->getLHS();
581
Ted Kremeneka8538d92009-02-13 01:45:31 +0000582 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000583 }
584
585 case Stmt::ConditionalOperatorClass: { // ?:
586
587 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
588
589 // For ?, if branchTaken == true then the value is either the LHS or
590 // the condition itself. (GNU extension).
591
592 Expr* Ex;
593
594 if (branchTaken)
595 Ex = C->getLHS() ? C->getLHS() : C->getCond();
596 else
597 Ex = C->getRHS();
598
Ted Kremeneka8538d92009-02-13 01:45:31 +0000599 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000600 }
601
602 case Stmt::ChooseExprClass: { // ?:
603
604 ChooseExpr* C = cast<ChooseExpr>(Terminator);
605
606 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000607 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000608 }
609 }
610}
611
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000612/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
613/// to try to recover some path-sensitivity for casts of symbolic
614/// integers that promote their values (which are currently not tracked well).
615/// This function returns the SVal bound to Condition->IgnoreCasts if all the
616// cast(s) did was sign-extend the original value.
617static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
618 Stmt* Condition, ASTContext& Ctx) {
619
620 Expr *Ex = dyn_cast<Expr>(Condition);
621 if (!Ex)
622 return UnknownVal();
623
624 uint64_t bits = 0;
625 bool bitsInit = false;
626
627 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
628 QualType T = CE->getType();
629
630 if (!T->isIntegerType())
631 return UnknownVal();
632
633 uint64_t newBits = Ctx.getTypeSize(T);
634 if (!bitsInit || newBits < bits) {
635 bitsInit = true;
636 bits = newBits;
637 }
638
639 Ex = CE->getSubExpr();
640 }
641
642 // We reached a non-cast. Is it a symbolic value?
643 QualType T = Ex->getType();
644
645 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
646 return UnknownVal();
647
648 return StateMgr.GetSVal(state, Ex);
649}
650
Ted Kremenekaf337412008-11-12 19:24:17 +0000651void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000652 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000653
Ted Kremeneke7d22112008-02-11 19:21:59 +0000654 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000655 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000656 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000657
Ted Kremenekb2331832008-02-15 22:29:00 +0000658 // Check for NULL conditions; e.g. "for(;;)"
659 if (!Condition) {
660 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000661 return;
662 }
663
Ted Kremenek21028dd2009-03-11 03:54:24 +0000664 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
665 Condition->getLocStart(),
666 "Error evaluating branch");
667
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000668 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000669
670 switch (V.getBaseKind()) {
671 default:
672 break;
673
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000674 case SVal::UnknownKind: {
675 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
676 if (Ex->getType()->isIntegerType()) {
677 // Try to recover some path-sensitivity. Right now casts of symbolic
678 // integers that promote their values are currently not tracked well.
679 // If 'Condition' is such an expression, try and recover the
680 // underlying value and use that instead.
681 SVal recovered = RecoverCastedSymbol(getStateManager(),
682 builder.getState(), Condition,
683 getContext());
684
685 if (!recovered.isUnknown()) {
686 V = recovered;
687 break;
688 }
689 }
690 }
691
Ted Kremenek58b33212008-02-26 19:40:44 +0000692 builder.generateNode(MarkBranch(PrevState, Term, true), true);
693 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000694 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000695 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000696
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000697 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000698 NodeTy* N = builder.generateNode(PrevState, true);
699
700 if (N) {
701 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000702 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000703 }
704
705 builder.markInfeasible(false);
706 return;
707 }
708 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000709
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000710 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000711
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000712 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000713 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000714
715 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000716 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000717 else
718 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000719
720 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000721
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000722 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000723 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000724
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000725 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000726 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000727 else
728 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000729}
730
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000731/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000732/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000733void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000734
Ted Kremeneka8538d92009-02-13 01:45:31 +0000735 const GRState* state = builder.getState();
736 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000737
738 // Three possibilities:
739 //
740 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000741 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000742 // (3) We have no clue about the label. Dispatch to all targets.
743 //
744
745 typedef IndirectGotoNodeBuilder::iterator iterator;
746
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000747 if (isa<loc::GotoLabel>(V)) {
748 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000749
750 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000751 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000752 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000753 return;
754 }
755 }
756
757 assert (false && "No block with label.");
758 return;
759 }
760
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000761 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000762 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000763 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000764 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000765 return;
766 }
767
768 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000769 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek754607e2008-02-13 00:24:44 +0000770
771 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000772 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000773}
Ted Kremenekf233d482008-02-05 00:26:40 +0000774
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000775
776void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
777 NodeTy* Pred, NodeSet& Dst) {
778
779 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
780
Ted Kremeneka8538d92009-02-13 01:45:31 +0000781 const GRState* state = GetState(Pred);
782 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000783
784 assert (X.isUndef());
785
786 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
787
788 assert (SE);
789
Ted Kremeneka8538d92009-02-13 01:45:31 +0000790 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000791
792 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000793 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000794}
795
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000796/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
797/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000798void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
799 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000800 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000801 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000802 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000803
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000804 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000805 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000806 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000807 return;
808 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000809
Ted Kremeneka8538d92009-02-13 01:45:31 +0000810 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000811 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000812
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000813 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000814 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000815
816 // Evaluate the LHS of the case value.
817 Expr::EvalResult V1;
818 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000819
Ted Kremenek72afb372009-01-17 01:54:16 +0000820 // Sanity checks. These go away in Release builds.
821 assert(b && V1.Val.isInt() && !V1.HasSideEffects
822 && "Case condition must evaluate to an integer constant.");
823 b = b; // silence unused variable warning
824 assert(V1.Val.getInt().getBitWidth() ==
825 getContext().getTypeSize(CondE->getType()));
826
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000827 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000828 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000829
830 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000831 b = E->Evaluate(V2, getContext());
832 assert(b && V2.Val.isInt() && !V2.HasSideEffects
833 && "Case condition must evaluate to an integer constant.");
834 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000835 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000836 else
837 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000838
839 // FIXME: Eventually we should replace the logic below with a range
840 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000841 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000842
Ted Kremenek14a11402008-03-17 22:17:56 +0000843 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000844 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu262fd032009-05-20 09:00:16 +0000845 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000846 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000847
Ted Kremenek72afb372009-01-17 01:54:16 +0000848 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000849 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000850 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000851
852 if (isFeasible) {
853 builder.generateCaseStmtNode(I, StNew);
854
855 // If CondV evaluates to a constant, then we know that this
856 // is the *only* case that we can take, so stop evaluating the
857 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000858 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000859 return;
860 }
861
862 // Now "assume" that the case doesn't match. Add this state
863 // to the default state (if it is feasible).
864
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000865 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000866 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000867
Ted Kremenek5014ab12008-04-23 05:03:18 +0000868 if (isFeasible) {
869 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000870 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000871 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000872
Ted Kremenek14a11402008-03-17 22:17:56 +0000873 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000874 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000875 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000876
Ted Kremenek72afb372009-01-17 01:54:16 +0000877 ++V1.Val.getInt();
878 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000879
880 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000881 }
882
883 // If we reach here, than we know that the default branch is
884 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000885 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000886}
887
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000888//===----------------------------------------------------------------------===//
889// Transfer functions: logical operations ('&&', '||').
890//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000891
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000892void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000893 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000894
Ted Kremenek05a23782008-02-26 19:05:15 +0000895 assert (B->getOpcode() == BinaryOperator::LAnd ||
896 B->getOpcode() == BinaryOperator::LOr);
897
898 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
899
Ted Kremeneka8538d92009-02-13 01:45:31 +0000900 const GRState* state = GetState(Pred);
901 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000902
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000903 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000904
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000905 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000906
907 assert (Ex);
908
909 if (Ex == B->getRHS()) {
910
Ted Kremeneka8538d92009-02-13 01:45:31 +0000911 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000912
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000913 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000914
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000915 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000916 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000917 return;
918 }
919
Ted Kremenek05a23782008-02-26 19:05:15 +0000920 // We took the RHS. Because the value of the '&&' or '||' expression must
921 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
922 // or 1. Alternatively, we could take a lazy approach, and calculate this
923 // value later when necessary. We don't have the machinery in place for
924 // this right now, and since most logical expressions are used for branches,
925 // the payoff is not likely to be large. Instead, we do eager evaluation.
926
927 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000928 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000929
930 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000931 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000932 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000933
934 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000935 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000936
937 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000938 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000939 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000940 }
941 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000942 // We took the LHS expression. Depending on whether we are '&&' or
943 // '||' we know what the value of the expression is via properties of
944 // the short-circuiting.
945
946 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000947 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000948 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000949}
Ted Kremenek05a23782008-02-26 19:05:15 +0000950
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000951//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000952// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000953//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000954
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000955void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
956 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000957
Ted Kremeneka8538d92009-02-13 01:45:31 +0000958 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000959
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000960 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000961
962 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
963
Ted Kremeneka8538d92009-02-13 01:45:31 +0000964 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000965
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000966 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +0000967 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
968 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000969 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000970 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000971 return;
972
973 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
974 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
975
976 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000977 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000978 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000979 return;
980
981 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000982 assert(asLValue);
Zhongxing Xu369f4472009-04-20 05:24:46 +0000983 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek7090d542009-05-07 18:27:16 +0000984 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V),
985 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000986 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000987 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000988
989 assert (false &&
990 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000991}
992
Ted Kremenek540cbe22008-04-22 04:56:29 +0000993/// VisitArraySubscriptExpr - Transfer function for array accesses
994void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000995 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000996
997 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000998 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000999 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +00001000
1001 if (Base->getType()->isVectorType()) {
1002 // For vector types get its lvalue.
1003 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1004 // In fact, I think this is just a hack. We need to get the right
1005 // semantics.
1006 VisitLValue(Base, Pred, Tmp);
1007 }
1008 else
1009 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001010
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001011 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001012 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001013 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001014
1015 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001016 const GRState* state = GetState(*I2);
Ted Kremenekf936f452009-05-04 06:18:28 +00001017 SVal V = StateMgr.GetLValue(state, A->getType(),
1018 GetSVal(state, Base),
Ted Kremeneka8538d92009-02-13 01:45:31 +00001019 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001020
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001021 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00001022 MakeNode(Dst, A, *I2, BindExpr(state, A, V),
1023 ProgramPoint::PostLValueKind);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001024 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001025 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001026 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001027 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001028}
1029
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001030/// VisitMemberExpr - Transfer function for member expressions.
1031void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001032 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001033
1034 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001035 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001036
1037 if (M->isArrow())
1038 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1039 else
1040 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1041
Douglas Gregor86f19402008-12-20 23:49:58 +00001042 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1043 if (!Field) // FIXME: skipping member expressions for non-fields
1044 return;
1045
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001046 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001047 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001048 // FIXME: Should we insert some assumption logic in here to determine
1049 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001050 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001051 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001052
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001053 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00001054 MakeNode(Dst, M, *I, BindExpr(state, M, L),
1055 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001056 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001057 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001058 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001059}
1060
Ted Kremeneka8538d92009-02-13 01:45:31 +00001061/// EvalBind - Handle the semantics of binding a value to a specific location.
1062/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1063void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1064 const GRState* state, SVal location, SVal Val) {
1065
Ted Kremenek41573eb2009-02-14 01:43:44 +00001066 const GRState* newState = 0;
1067
1068 if (location.isUnknown()) {
1069 // We know that the new state will be the same as the old state since
1070 // the location of the binding is "unknown". Consequently, there
1071 // is no reason to just create a new node.
1072 newState = state;
1073 }
1074 else {
1075 // We are binding to a value other than 'unknown'. Perform the binding
1076 // using the StoreManager.
1077 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1078 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001079
Ted Kremenek41573eb2009-02-14 01:43:44 +00001080 // The next thing to do is check if the GRTransferFuncs object wants to
1081 // update the state based on the new binding. If the GRTransferFunc object
1082 // doesn't do anything, just auto-propagate the current state.
1083 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1084 newState != state);
1085
1086 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001087}
1088
1089/// EvalStore - Handle the semantics of a store via an assignment.
1090/// @param Dst The node set to store generated state nodes
1091/// @param Ex The expression representing the location of the store
1092/// @param state The current simulation state
1093/// @param location The location to store the value
1094/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001095void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001096 const GRState* state, SVal location, SVal Val,
1097 const void *tag) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001098
1099 assert (Builder && "GRStmtNodeBuilder must be defined.");
1100
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001101 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001102 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001103
Ted Kremenek8c354752008-12-16 22:02:27 +00001104 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001105 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001106
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001107 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001108 state = GetState(Pred);
1109
1110 // Proceed with the store.
1111 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek1670e402009-04-11 00:11:10 +00001112 SaveAndRestore<const void*> OldTag(Builder->Tag);
1113 Builder->PointKind = ProgramPoint::PostStoreKind;
1114 Builder->Tag = tag;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001115 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116}
1117
1118void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001119 const GRState* state, SVal location,
1120 const void *tag) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001121
Ted Kremenek8c354752008-12-16 22:02:27 +00001122 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001123 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001124
Ted Kremenek8c354752008-12-16 22:02:27 +00001125 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001126 return;
1127
Ted Kremeneka8538d92009-02-13 01:45:31 +00001128 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001129
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001130 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001131 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001132
1133 // FIXME: Currently symbolic analysis "generates" new symbols
1134 // for the contents of values. We need a better approach.
1135
Ted Kremenek8c354752008-12-16 22:02:27 +00001136 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001137 // This is important. We must nuke the old binding.
Ted Kremenek1670e402009-04-11 00:11:10 +00001138 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K, tag);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001139 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001140 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001141 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
Ted Kremenek1670e402009-04-11 00:11:10 +00001142 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K, tag);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001143 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001144}
1145
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001146void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001147 const GRState* state, SVal location, SVal Val,
1148 const void *tag) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001149
1150 NodeSet TmpDst;
Ted Kremenek1670e402009-04-11 00:11:10 +00001151 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001152
1153 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek1670e402009-04-11 00:11:10 +00001154 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001155}
1156
Ted Kremenek8c354752008-12-16 22:02:27 +00001157GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001158 const GRState* state,
Ted Kremenek1670e402009-04-11 00:11:10 +00001159 SVal location,
1160 const void *tag) {
1161
1162 SaveAndRestore<const void*> OldTag(Builder->Tag);
1163 Builder->Tag = tag;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001164
1165 // Check for loads/stores from/to undefined values.
1166 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001167 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001168 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001169 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001170
Ted Kremenek8c354752008-12-16 22:02:27 +00001171 if (N) {
1172 N->markAsSink();
1173 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001174 }
1175
Ted Kremenek8c354752008-12-16 22:02:27 +00001176 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001177 }
1178
1179 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1180 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001181 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001182
1183 // During a load, one of two possible situations arise:
1184 // (1) A crash, because the location (pointer) was NULL.
1185 // (2) The location (pointer) is not NULL, and the dereference works.
1186 //
1187 // We add these assumptions.
1188
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001189 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001190
1191 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001192 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001193 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001194
1195 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001196 bool isFeasibleNull = false;
Ted Kremenekb65be702009-06-18 01:23:53 +00001197 const GRState *StNull = Assume(state, LV, false, isFeasibleNull);
Zhongxing Xua1718c72009-04-03 07:33:13 +00001198
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001199 if (isFeasibleNull) {
1200
Ted Kremenek7360fda2008-09-18 23:09:54 +00001201 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001202 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekb65be702009-06-18 01:23:53 +00001203 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001204
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001205 // We don't use "MakeNode" here because the node will be a sink
1206 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001207 NodeTy* NullNode =
1208 Builder->generateNode(Ex, StNull, Pred,
1209 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001210
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001211 if (NullNode) {
1212
1213 NullNode->markAsSink();
1214
1215 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1216 else ExplicitNullDeref.insert(NullNode);
1217 }
1218 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001219
1220 if (!isFeasibleNotNull)
1221 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001222
1223 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001224 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001225 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1226 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1227 // Get the index of the accessed element.
1228 SVal Idx = ER->getIndex();
1229 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001230 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1231 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001232
1233 bool isFeasibleInBound = false;
1234 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1235 true, isFeasibleInBound);
1236
1237 bool isFeasibleOutBound = false;
1238 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1239 false, isFeasibleOutBound);
1240
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001241 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001242 // Report warning. Make sink node manually.
1243 NodeTy* OOBNode =
1244 Builder->generateNode(Ex, StOutBound, Pred,
1245 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001246
1247 if (OOBNode) {
1248 OOBNode->markAsSink();
1249
1250 if (isFeasibleInBound)
1251 ImplicitOOBMemAccesses.insert(OOBNode);
1252 else
1253 ExplicitOOBMemAccesses.insert(OOBNode);
1254 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001255 }
1256
Ted Kremenek8c354752008-12-16 22:02:27 +00001257 if (!isFeasibleInBound)
1258 return 0;
1259
1260 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001261 }
1262 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001263
Ted Kremenek8c354752008-12-16 22:02:27 +00001264 // Generate a new node indicating the checks succeed.
1265 return Builder->generateNode(Ex, StNotNull, Pred,
1266 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001267}
1268
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001269//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001270// Transfer function: OSAtomics.
1271//
1272// FIXME: Eventually refactor into a more "plugin" infrastructure.
1273//===----------------------------------------------------------------------===//
1274
1275// Mac OS X:
1276// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1277// atomic.3.html
1278//
1279static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1280 GRExprEngine& Engine,
1281 GRStmtNodeBuilder<GRState>& Builder,
1282 CallExpr* CE, SVal L,
1283 ExplodedNode<GRState>* Pred) {
1284
1285 // Not enough arguments to match OSAtomicCompareAndSwap?
1286 if (CE->getNumArgs() != 3)
1287 return false;
1288
1289 ASTContext &C = Engine.getContext();
1290 Expr *oldValueExpr = CE->getArg(0);
1291 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1292
1293 Expr *newValueExpr = CE->getArg(1);
1294 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1295
1296 // Do the types of 'oldValue' and 'newValue' match?
1297 if (oldValueType != newValueType)
1298 return false;
1299
1300 Expr *theValueExpr = CE->getArg(2);
1301 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1302
1303 // theValueType not a pointer?
1304 if (!theValueType)
1305 return false;
1306
1307 QualType theValueTypePointee =
1308 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1309
1310 // The pointee must match newValueType and oldValueType.
1311 if (theValueTypePointee != newValueType)
1312 return false;
1313
1314 static unsigned magic_load = 0;
1315 static unsigned magic_store = 0;
1316
1317 const void *OSAtomicLoadTag = &magic_load;
1318 const void *OSAtomicStoreTag = &magic_store;
1319
1320 // Load 'theValue'.
1321 GRStateManager &StateMgr = Engine.getStateManager();
1322 const GRState *state = Pred->getState();
1323 ExplodedNodeSet<GRState> Tmp;
1324 SVal location = StateMgr.GetSVal(state, theValueExpr);
1325 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1326
1327 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1328 I != E; ++I) {
1329
1330 ExplodedNode<GRState> *N = *I;
1331 const GRState *stateLoad = N->getState();
1332 SVal theValueVal = StateMgr.GetSVal(stateLoad, theValueExpr);
1333 SVal oldValueVal = StateMgr.GetSVal(stateLoad, oldValueExpr);
1334
1335 // Perform the comparison.
Zhongxing Xu262fd032009-05-20 09:00:16 +00001336 SVal Cmp = Engine.EvalBinOp(stateLoad,
1337 BinaryOperator::EQ, theValueVal, oldValueVal,
Ted Kremenek1670e402009-04-11 00:11:10 +00001338 Engine.getContext().IntTy);
1339 bool isFeasible = false;
1340 const GRState *stateEqual = StateMgr.Assume(stateLoad, Cmp, true,
1341 isFeasible);
1342
1343 // Were they equal?
1344 if (isFeasible) {
1345 // Perform the store.
1346 ExplodedNodeSet<GRState> TmpStore;
1347 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
1348 StateMgr.GetSVal(stateEqual, newValueExpr),
1349 OSAtomicStoreTag);
1350
1351 // Now bind the result of the comparison.
1352 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1353 E2 = TmpStore.end(); I2 != E2; ++I2) {
1354 ExplodedNode<GRState> *predNew = *I2;
1355 const GRState *stateNew = predNew->getState();
1356 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
1357 Engine.MakeNode(Dst, CE, predNew, Engine.BindExpr(stateNew, CE, Res));
1358 }
1359 }
1360
1361 // Were they not equal?
1362 isFeasible = false;
1363 const GRState *stateNotEqual = StateMgr.Assume(stateLoad, Cmp, false,
1364 isFeasible);
1365
1366 if (isFeasible) {
1367 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
1368 Engine.MakeNode(Dst, CE, N, Engine.BindExpr(stateNotEqual, CE, Res));
1369 }
1370 }
1371
1372 return true;
1373}
1374
1375static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1376 GRExprEngine& Engine,
1377 GRStmtNodeBuilder<GRState>& Builder,
1378 CallExpr* CE, SVal L,
1379 ExplodedNode<GRState>* Pred) {
Zhongxing Xu369f4472009-04-20 05:24:46 +00001380 const FunctionDecl* FD = L.getAsFunctionDecl();
1381 if (!FD)
Ted Kremenek1670e402009-04-11 00:11:10 +00001382 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +00001383
Ted Kremenek1670e402009-04-11 00:11:10 +00001384 const char *FName = FD->getNameAsCString();
1385
1386 // Check for compare and swap.
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001387 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1388 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek1670e402009-04-11 00:11:10 +00001389 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001390
Ted Kremenek1670e402009-04-11 00:11:10 +00001391 // FIXME: Other atomics.
1392 return false;
1393}
1394
1395//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001396// Transfer function: Function calls.
1397//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001398
1399void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1400 assert (Builder && "GRStmtNodeBuilder must be defined.");
1401
1402 // FIXME: Allow us to chain together transfer functions.
1403 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1404 return;
1405
1406 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1407}
1408
Ted Kremenekde434242008-02-19 01:44:53 +00001409void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001410 CallExpr::arg_iterator AI,
1411 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001412 NodeSet& Dst)
1413{
1414 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001415 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001416 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1417 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001418 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001419
1420 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1421}
1422
1423void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1424 CallExpr::arg_iterator AI,
1425 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001426 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001427 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001428
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001429 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001430 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001431 // If the call argument is being bound to a reference parameter,
1432 // visit it as an lvalue, not an rvalue.
1433 bool VisitAsLvalue = false;
1434 if (Proto && ParamIdx < Proto->getNumArgs())
1435 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1436
1437 NodeSet DstTmp;
1438 if (VisitAsLvalue)
1439 VisitLValue(*AI, Pred, DstTmp);
1440 else
1441 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001442 ++AI;
1443
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001444 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001445 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001446
1447 return;
1448 }
1449
1450 // If we reach here we have processed all of the arguments. Evaluate
1451 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001452
Ted Kremenek994a09b2008-02-25 21:16:03 +00001453 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001454 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001455
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001456 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001457
Ted Kremenekde434242008-02-19 01:44:53 +00001458 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001459 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1460
Ted Kremeneka8538d92009-02-13 01:45:31 +00001461 const GRState* state = GetState(*DI);
1462 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001463
Ted Kremeneka1354a52008-03-03 16:47:31 +00001464 // FIXME: Add support for symbolic function calls (calls involving
1465 // function pointer values that are symbolic).
1466
1467 // Check for undefined control-flow or calls to NULL.
1468
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001469 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001470 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001471
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001472 if (N) {
1473 N->markAsSink();
1474 BadCalls.insert(N);
1475 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001476
Ted Kremenekde434242008-02-19 01:44:53 +00001477 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001478 }
1479
1480 // Check for the "noreturn" attribute.
1481
1482 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xu369f4472009-04-20 05:24:46 +00001483 const FunctionDecl* FD = L.getAsFunctionDecl();
1484 if (FD) {
Ted Kremenekb7252322009-04-10 00:01:14 +00001485 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001486 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001487 else {
1488 // HACK: Some functions are not marked noreturn, and don't return.
1489 // Here are a few hardwired ones. If this takes too long, we can
1490 // potentially cache these results.
1491 const char* s = FD->getIdentifier()->getName();
1492 unsigned n = strlen(s);
1493
1494 switch (n) {
1495 default:
1496 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001497
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001498 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001499 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1500 break;
1501
1502 case 5:
1503 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001504 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001505 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001506 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001507 // FIXME: use Assume to inspect the possible symbolic value of
1508 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001509 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001510 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001511 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001512 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001513 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001514 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001515
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001516 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001517 if (!memcmp(s, "Assert", 6)) {
1518 Builder->BuildSinks = true;
1519 break;
1520 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001521
1522 // FIXME: This is just a wrapper around throwing an exception.
1523 // Eventually inter-procedural analysis should handle this easily.
1524 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1525
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001526 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001527
1528 case 7:
1529 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1530 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001531
Ted Kremenekf47bb782008-04-30 17:54:04 +00001532 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001533 if (!memcmp(s ,"db_error", 8) ||
1534 !memcmp(s, "__assert", 8))
1535 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001536 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001537
1538 case 12:
1539 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1540 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001541
Ted Kremenekf9683082008-09-19 02:30:47 +00001542 case 13:
1543 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1544 break;
1545
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001546 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001547 if (!memcmp(s, "dtrace_assfail", 14) ||
1548 !memcmp(s, "yy_fatal_error", 14))
1549 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001550 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001551
1552 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001553 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001554 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1555 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001556 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001557
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001558 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001559 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001560
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001561 }
1562 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001563
1564 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001565
Zhongxing Xu369f4472009-04-20 05:24:46 +00001566 if (FD) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001567
Zhongxing Xu369f4472009-04-20 05:24:46 +00001568 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001569 switch (id) {
1570 case Builtin::BI__builtin_expect: {
1571 // For __builtin_expect, just return the value of the subexpression.
1572 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001573 SVal X = GetSVal(state, *(CE->arg_begin()));
1574 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001575 continue;
1576 }
1577
Ted Kremenekb3021332008-11-02 00:35:01 +00001578 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001579 // FIXME: Refactor into StoreManager itself?
1580 MemRegionManager& RM = getStateManager().getRegionManager();
1581 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001582 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001583
1584 // Set the extent of the region in bytes. This enables us to use the
1585 // SVal of the argument directly. If we save the extent in bits, we
1586 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001587 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1588 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001589
Ted Kremeneka8538d92009-02-13 01:45:31 +00001590 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001591 continue;
1592 }
1593
Ted Kremenek55aea312008-03-05 22:59:42 +00001594 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001595 break;
1596 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001597 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001598
Ted Kremenek186350f2008-04-23 20:12:28 +00001599 // Check any arguments passed-by-value against being undefined.
1600
1601 bool badArg = false;
1602
1603 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1604 I != E; ++I) {
1605
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001606 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001607 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001608
Ted Kremenek186350f2008-04-23 20:12:28 +00001609 if (N) {
1610 N->markAsSink();
1611 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001612 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001613
Ted Kremenek186350f2008-04-23 20:12:28 +00001614 badArg = true;
1615 break;
1616 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001617 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001618
1619 if (badArg)
1620 continue;
1621
1622 // Dispatch to the plug-in transfer function.
1623
1624 unsigned size = Dst.size();
1625 SaveOr OldHasGen(Builder->HasGeneratedNode);
1626 EvalCall(Dst, CE, L, *DI);
1627
1628 // Handle the case where no nodes where generated. Auto-generate that
1629 // contains the updated state if we aren't generating sinks.
1630
1631 if (!Builder->BuildSinks && Dst.size() == size &&
1632 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001633 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001634 }
1635}
1636
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001637//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001638// Transfer function: Objective-C ivar references.
1639//===----------------------------------------------------------------------===//
1640
Ted Kremenekf5cae632009-02-28 20:50:43 +00001641static std::pair<const void*,const void*> EagerlyAssumeTag
1642 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1643
Ted Kremenekb2939022009-02-25 23:32:10 +00001644void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001645 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1646 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001647
1648 // Test if the previous node was as the same expression. This can happen
1649 // when the expression fails to evaluate to anything meaningful and
1650 // (as an optimization) we don't generate a node.
1651 ProgramPoint P = Pred->getLocation();
1652 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1653 Dst.Add(Pred);
1654 continue;
1655 }
1656
Ted Kremenek48af2a92009-02-25 22:32:02 +00001657 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001658 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001659 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001660 // First assume that the condition is true.
1661 bool isFeasible = false;
1662 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1663 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001664 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1665 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001666 stateTrue, Pred));
1667 }
1668
1669 // Next, assume that the condition is false.
1670 isFeasible = false;
1671 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1672 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001673 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1674 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001675 stateFalse, Pred));
1676 }
1677 }
1678 else
1679 Dst.Add(Pred);
1680 }
1681}
1682
1683//===----------------------------------------------------------------------===//
1684// Transfer function: Objective-C ivar references.
1685//===----------------------------------------------------------------------===//
1686
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001687void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1688 NodeTy* Pred, NodeSet& Dst,
1689 bool asLValue) {
1690
1691 Expr* Base = cast<Expr>(Ex->getBase());
1692 NodeSet Tmp;
1693 Visit(Base, Pred, Tmp);
1694
1695 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001696 const GRState* state = GetState(*I);
1697 SVal BaseVal = GetSVal(state, Base);
1698 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001699
1700 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001701 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001702 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001703 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001704 }
1705}
1706
1707//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001708// Transfer function: Objective-C fast enumeration 'for' statements.
1709//===----------------------------------------------------------------------===//
1710
1711void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1712 NodeTy* Pred, NodeSet& Dst) {
1713
1714 // ObjCForCollectionStmts are processed in two places. This method
1715 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1716 // statements within a basic block. This transfer function does two things:
1717 //
1718 // (1) binds the next container value to 'element'. This creates a new
1719 // node in the ExplodedGraph.
1720 //
1721 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1722 // whether or not the container has any more elements. This value
1723 // will be tested in ProcessBranch. We need to explicitly bind
1724 // this value because a container can contain nil elements.
1725 //
1726 // FIXME: Eventually this logic should actually do dispatches to
1727 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1728 // This will require simulating a temporary NSFastEnumerationState, either
1729 // through an SVal or through the use of MemRegions. This value can
1730 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1731 // terminates we reclaim the temporary (it goes out of scope) and we
1732 // we can test if the SVal is 0 or if the MemRegion is null (depending
1733 // on what approach we take).
1734 //
1735 // For now: simulate (1) by assigning either a symbol or nil if the
1736 // container is empty. Thus this transfer function will by default
1737 // result in state splitting.
1738
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001739 Stmt* elem = S->getElement();
1740 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001741
1742 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001743 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001744 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001745 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1746 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1747 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001748 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001749
1750 NodeSet Tmp;
1751 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001752
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001753 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1754 const GRState* state = GetState(*I);
1755 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1756 }
1757}
1758
1759void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1760 NodeTy* Pred, NodeSet& Dst,
1761 SVal ElementV) {
1762
1763
Ted Kremenekaf337412008-11-12 19:24:17 +00001764
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001765 // Get the current state. Use 'EvalLocation' to determine if it is a null
1766 // pointer, etc.
1767 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001768
Ted Kremenek8c354752008-12-16 22:02:27 +00001769 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1770 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001771 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001772
Ted Kremenekb65be702009-06-18 01:23:53 +00001773 const GRState *state = GetState(Pred);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001774
Ted Kremenekaf337412008-11-12 19:24:17 +00001775 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001776 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001777 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
Ted Kremenekb65be702009-06-18 01:23:53 +00001778 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenekaf337412008-11-12 19:24:17 +00001779
Ted Kremenekaf337412008-11-12 19:24:17 +00001780 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001781 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
Ted Kremenekb65be702009-06-18 01:23:53 +00001782 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001783
1784 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1785 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1786 // FIXME: The proper thing to do is to really iterate over the
1787 // container. We will do this with dispatch logic to the store.
1788 // For now, just 'conjure' up a symbolic value.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001789 QualType T = R->getValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001790 assert (Loc::IsLocType(T));
1791 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001792 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1793 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
Ted Kremenekb65be702009-06-18 01:23:53 +00001794 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001795
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001796 // Bind the location to 'nil' on the false branch.
1797 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
Ted Kremenekb65be702009-06-18 01:23:53 +00001798 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001799 }
1800
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001801 // Create the new nodes.
1802 MakeNode(Dst, S, Pred, hasElems);
1803 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001804}
1805
1806//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001807// Transfer function: Objective-C message expressions.
1808//===----------------------------------------------------------------------===//
1809
1810void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1811 NodeSet& Dst){
1812
1813 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1814 Pred, Dst);
1815}
1816
1817void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001818 ObjCMessageExpr::arg_iterator AI,
1819 ObjCMessageExpr::arg_iterator AE,
1820 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001821 if (AI == AE) {
1822
1823 // Process the receiver.
1824
1825 if (Expr* Receiver = ME->getReceiver()) {
1826 NodeSet Tmp;
1827 Visit(Receiver, Pred, Tmp);
1828
1829 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1830 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1831
1832 return;
1833 }
1834
1835 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1836 return;
1837 }
1838
1839 NodeSet Tmp;
1840 Visit(*AI, Pred, Tmp);
1841
1842 ++AI;
1843
1844 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1845 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1846}
1847
1848void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1849 NodeTy* Pred,
1850 NodeSet& Dst) {
1851
1852 // FIXME: More logic for the processing the method call.
1853
Ted Kremeneka8538d92009-02-13 01:45:31 +00001854 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001855 bool RaisesException = false;
1856
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001857
1858 if (Expr* Receiver = ME->getReceiver()) {
1859
Ted Kremeneka8538d92009-02-13 01:45:31 +00001860 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001861
Ted Kremenek21fe8372009-02-19 04:06:22 +00001862 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001863 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001864 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001865
1866 if (N) {
1867 N->markAsSink();
1868 UndefReceivers.insert(N);
1869 }
1870
1871 return;
1872 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001873
Ted Kremenek21fe8372009-02-19 04:06:22 +00001874 // "Assume" that the receiver is not NULL.
1875 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001876 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001877
1878 // "Assume" that the receiver is NULL.
1879 bool isFeasibleNull = false;
1880 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1881
1882 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001883 QualType RetTy = ME->getType();
1884
Ted Kremenek21fe8372009-02-19 04:06:22 +00001885 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001886 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001887 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001888 // The [0 ...] expressions will return garbage. Flag either an
1889 // explicit or implicit error. Because of the structure of this
1890 // function we currently do not bifurfacte the state graph at
1891 // this point.
1892 // FIXME: We should bifurcate and fill the returned struct with
1893 // garbage.
1894 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1895 N->markAsSink();
1896 if (isFeasibleNotNull)
1897 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001898 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001899 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001900 }
1901 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001902 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001903 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001904 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001905 if (RetTy != Ctx.VoidTy) {
1906 if (BR.getParentMap().isConsumedExpr(ME)) {
1907 // sizeof(void *)
1908 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1909 // sizeof(return type)
1910 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001911
Ted Kremenekfe630b92009-04-09 05:45:56 +00001912 if(voidPtrSize < returnTypeSize) {
1913 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1914 N->markAsSink();
1915 if(isFeasibleNotNull)
1916 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001917 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001918 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001919 }
1920 }
1921 else if (!isFeasibleNotNull) {
1922 // Handle the safe cases where the return value is 0 if the
1923 // receiver is nil.
1924 //
1925 // FIXME: For now take the conservative approach that we only
1926 // return null values if we *know* that the receiver is nil.
1927 // This is because we can have surprises like:
1928 //
1929 // ... = [[NSScreens screens] objectAtIndex:0];
1930 //
1931 // What can happen is that [... screens] could return nil, but
1932 // it most likely isn't nil. We should assume the semantics
1933 // of this case unless we have *a lot* more knowledge.
1934 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001935 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001936 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001937 return;
1938 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001939 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001940 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001941 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001942 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001943 // of this method should assume that the receiver is not nil.
1944 if (!StNotNull)
1945 return;
1946
Ted Kremenekda9ae602009-04-08 18:51:08 +00001947 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001948 }
1949
Ted Kremeneke448ab42008-05-01 18:33:28 +00001950 // Check if the "raise" message was sent.
1951 if (ME->getSelector() == RaiseSel)
1952 RaisesException = true;
1953 }
1954 else {
1955
1956 IdentifierInfo* ClsName = ME->getClassName();
1957 Selector S = ME->getSelector();
1958
1959 // Check for special instance methods.
1960
1961 if (!NSExceptionII) {
1962 ASTContext& Ctx = getContext();
1963
1964 NSExceptionII = &Ctx.Idents.get("NSException");
1965 }
1966
1967 if (ClsName == NSExceptionII) {
1968
1969 enum { NUM_RAISE_SELECTORS = 2 };
1970
1971 // Lazily create a cache of the selectors.
1972
1973 if (!NSExceptionInstanceRaiseSelectors) {
1974
1975 ASTContext& Ctx = getContext();
1976
1977 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1978
1979 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1980 unsigned idx = 0;
1981
1982 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001983 II.push_back(&Ctx.Idents.get("raise"));
1984 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001985 NSExceptionInstanceRaiseSelectors[idx++] =
1986 Ctx.Selectors.getSelector(II.size(), &II[0]);
1987
1988 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001989 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001990 NSExceptionInstanceRaiseSelectors[idx++] =
1991 Ctx.Selectors.getSelector(II.size(), &II[0]);
1992 }
1993
1994 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1995 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1996 RaisesException = true; break;
1997 }
1998 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001999 }
2000
2001 // Check for any arguments that are uninitialized/undefined.
2002
2003 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2004 I != E; ++I) {
2005
Ted Kremeneka8538d92009-02-13 01:45:31 +00002006 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002007
2008 // Generate an error node for passing an uninitialized/undefined value
2009 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002010 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002011
2012 if (N) {
2013 N->markAsSink();
2014 MsgExprUndefArgs[N] = *I;
2015 }
2016
2017 return;
2018 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00002019 }
2020
2021 // Check if we raise an exception. For now treat these as sinks. Eventually
2022 // we will want to handle exceptions properly.
2023
2024 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2025
2026 if (RaisesException)
2027 Builder->BuildSinks = true;
2028
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002029 // Dispatch to plug-in transfer function.
2030
2031 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00002032 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002033
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002034 EvalObjCMessageExpr(Dst, ME, Pred);
2035
2036 // Handle the case where no nodes where generated. Auto-generate that
2037 // contains the updated state if we aren't generating sinks.
2038
Ted Kremenekb0533962008-04-18 20:35:30 +00002039 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002040 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002041}
2042
2043//===----------------------------------------------------------------------===//
2044// Transfer functions: Miscellaneous statements.
2045//===----------------------------------------------------------------------===//
2046
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002047void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2048 QualType PtrTy,
2049 Expr* CastE, NodeTy* Pred,
2050 NodeSet& Dst) {
2051 if (!V.isUnknownOrUndef()) {
2052 // FIXME: Determine if the number of bits of the target type is
2053 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00002054 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00002055 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2056 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002057 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002058 else
2059 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002060}
2061
2062
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002063void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002064 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002065 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002066 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002067
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002068 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002069 T = ExCast->getTypeAsWritten();
2070
Zhongxing Xued340f72008-10-22 08:02:16 +00002071 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002072 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002073 else
2074 Visit(Ex, Pred, S1);
2075
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002076 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00002077 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002078 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002079 Dst.Add(*I1);
2080
Ted Kremenek874d63f2008-01-24 02:02:54 +00002081 return;
2082 }
2083
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002084 // FIXME: The rest of this should probably just go into EvalCall, and
2085 // let the transfer function object be responsible for constructing
2086 // nodes.
2087
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002088 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00002089 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002090 const GRState* state = GetState(N);
2091 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00002092 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002093
2094 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002095 if (V.isUnknown()) {
2096 Dst.Add(N);
2097 continue;
2098 }
2099
2100 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00002101 if (V.isUndef())
2102 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002103
2104 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002105 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00002106 C.getCanonicalType(ExTy).getUnqualifiedType())
2107 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002108
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002109 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002110 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002111 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002112 continue;
2113 }
2114
2115 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00002116 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002117 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002118 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002119 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00002120 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002121 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002122 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002123
Ted Kremenekc5302912009-03-05 20:22:13 +00002124 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00002125 }
2126
2127 // Just pass through function and block pointers.
2128 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2129 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00002130 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002131 }
2132
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002133 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00002134 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002135 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00002136 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002137
2138 // Are we casting from an array to a pointer? If so just pass on
2139 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00002140 if (T->isPointerType())
2141 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002142
2143 // Are we casting from an array to an integer? If so, cast the decayed
2144 // pointer value to an integer.
2145 assert(T->isIntegerType());
2146 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2147 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002148 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00002149 continue;
2150 }
2151
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002152 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002153 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2154 // FIXME: For TypedViewRegions, we should handle the case where the
2155 // underlying symbolic pointer is a function pointer or
2156 // block pointer.
2157
2158 // FIXME: We should handle the case where we strip off view layers to get
2159 // to a desugared type.
2160
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002161 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00002162 // We get a symbolic function pointer for a dereference of a function
2163 // pointer, but it is of function type. Example:
2164
2165 // struct FPRec {
2166 // void (*my_func)(int * x);
2167 // };
2168 //
2169 // int bar(int x);
2170 //
2171 // int f1_a(struct FPRec* foo) {
2172 // int x;
2173 // (*foo->my_func)(&x);
2174 // return bar(x)+1; // no-warning
2175 // }
2176
2177 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002178
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002179 const MemRegion* R = RV->getRegion();
2180 StoreManager& StoreMgr = getStoreManager();
2181
2182 // Delegate to store manager to get the result of casting a region
2183 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002184 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002185
2186 // Inspect the result. If the MemRegion* returned is NULL, this
2187 // expression evaluates to UnknownVal.
2188 R = Res.getRegion();
2189 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2190
2191 // Generate the new node in the ExplodedGraph.
2192 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002193 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002194 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002195 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002196 DispatchCast: {
2197 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2198 EvalCast(V, CastE->getType())));
2199 continue;
2200 }
2201
2202 PassThrough: {
2203 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2204 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002205 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002206}
2207
Ted Kremenek4f090272008-10-27 21:54:31 +00002208void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002209 NodeTy* Pred, NodeSet& Dst,
2210 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002211 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2212 NodeSet Tmp;
2213 Visit(ILE, Pred, Tmp);
2214
2215 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002216 const GRState* state = GetState(*I);
2217 SVal ILV = GetSVal(state, ILE);
2218 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002219
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002220 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002221 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002222 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002223 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002224 }
2225}
2226
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002227void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002228
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002229 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002230 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002231
2232 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002233 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002234
Ted Kremenekefd59942008-12-08 22:47:34 +00002235 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002236 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002237
2238 // FIXME: static variables may have an initializer, but the second
2239 // time a function is called those values may not be current.
2240 NodeSet Tmp;
2241
Ted Kremenekaf337412008-11-12 19:24:17 +00002242 if (InitEx)
2243 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002244
2245 if (Tmp.empty())
2246 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002247
2248 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002249 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002250 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002251
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002252 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2253 QualType T = getContext().getCanonicalType(VD->getType());
2254 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2255 // FIXME: Handle multi-dimensional VLAs.
2256
2257 Expr* SE = VLA->getSizeExpr();
2258 SVal Size = GetSVal(state, SE);
2259
2260 if (Size.isUndef()) {
2261 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2262 N->markAsSink();
2263 ExplicitBadSizedVLA.insert(N);
2264 }
2265 continue;
2266 }
2267
2268 bool isFeasibleZero = false;
2269 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2270
2271 bool isFeasibleNotZero = false;
2272 state = Assume(state, Size, true, isFeasibleNotZero);
2273
2274 if (isFeasibleZero) {
2275 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2276 N->markAsSink();
2277 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2278 else ExplicitBadSizedVLA.insert(N);
2279 }
2280 }
2281
2282 if (!isFeasibleNotZero)
2283 continue;
2284 }
2285
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002286 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002287 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002288 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002289 QualType T = VD->getType();
2290
2291 // Recover some path-sensitivity if a scalar value evaluated to
2292 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002293 if (InitVal.isUnknown() ||
2294 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002295 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002296 }
2297
Ted Kremeneka8538d92009-02-13 01:45:31 +00002298 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002299
2300 // The next thing to do is check if the GRTransferFuncs object wants to
2301 // update the state based on the new binding. If the GRTransferFunc
2302 // object doesn't do anything, just auto-propagate the current state.
2303 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2304 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2305 InitVal);
2306 }
2307 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002308 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002309 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002310 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002311 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002312}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002313
Ted Kremenekf75b1862008-10-30 17:47:32 +00002314namespace {
2315 // This class is used by VisitInitListExpr as an item in a worklist
2316 // for processing the values contained in an InitListExpr.
2317class VISIBILITY_HIDDEN InitListWLItem {
2318public:
2319 llvm::ImmutableList<SVal> Vals;
2320 GRExprEngine::NodeTy* N;
2321 InitListExpr::reverse_iterator Itr;
2322
2323 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2324 InitListExpr::reverse_iterator itr)
2325 : Vals(vals), N(n), Itr(itr) {}
2326};
2327}
2328
2329
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002330void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2331 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002332
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002333 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002334 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002335 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002336
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002337 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002338
Ted Kremeneka49e3672008-10-30 23:14:36 +00002339 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002340
Ted Kremeneka49e3672008-10-30 23:14:36 +00002341 // Handle base case where the initializer has no elements.
2342 // e.g: static int* myArray[] = {};
2343 if (NumInitElements == 0) {
2344 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2345 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2346 return;
2347 }
2348
2349 // Create a worklist to process the initializers.
2350 llvm::SmallVector<InitListWLItem, 10> WorkList;
2351 WorkList.reserve(NumInitElements);
2352 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002353 InitListExpr::reverse_iterator ItrEnd = E->rend();
2354
Ted Kremeneka49e3672008-10-30 23:14:36 +00002355 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002356 while (!WorkList.empty()) {
2357 InitListWLItem X = WorkList.back();
2358 WorkList.pop_back();
2359
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002360 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002361 Visit(*X.Itr, X.N, Tmp);
2362
2363 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002364
Ted Kremenekf75b1862008-10-30 17:47:32 +00002365 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2366 // Get the last initializer value.
2367 state = GetState(*NI);
2368 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2369
2370 // Construct the new list of values by prepending the new value to
2371 // the already constructed list.
2372 llvm::ImmutableList<SVal> NewVals =
2373 getBasicVals().consVals(InitV, X.Vals);
2374
2375 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002376 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002377 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002378
Ted Kremenekf75b1862008-10-30 17:47:32 +00002379 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002380 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002381 }
2382 else {
2383 // Still some initializer values to go. Push them onto the worklist.
2384 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2385 }
2386 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002387 }
Ted Kremenek87903072008-10-30 18:34:31 +00002388
2389 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002390 }
2391
Ted Kremenek062e2f92008-11-13 06:10:40 +00002392 if (T->isUnionType() || T->isVectorType()) {
2393 // FIXME: to be implemented.
2394 // Note: That vectors can return true for T->isIntegerType()
2395 MakeNode(Dst, E, Pred, state);
2396 return;
2397 }
2398
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002399 if (Loc::IsLocType(T) || T->isIntegerType()) {
2400 assert (E->getNumInits() == 1);
2401 NodeSet Tmp;
2402 Expr* Init = E->getInit(0);
2403 Visit(Init, Pred, Tmp);
2404 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2405 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002406 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002407 }
2408 return;
2409 }
2410
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002411
2412 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2413 assert(0 && "unprocessed InitListExpr type");
2414}
Ted Kremenekf233d482008-02-05 00:26:40 +00002415
Sebastian Redl05189992008-11-11 17:56:53 +00002416/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2417void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2418 NodeTy* Pred,
2419 NodeSet& Dst) {
2420 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002421 uint64_t amt;
2422
2423 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002424 if (T == getContext().VoidTy) {
2425 // sizeof(void) == 1 byte.
2426 amt = 1;
2427 }
2428 else if (!T.getTypePtr()->isConstantSizeType()) {
2429 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002430 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002431 }
2432 else if (T->isObjCInterfaceType()) {
2433 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2434 // the compiler has laid out its representation. Just report Unknown
2435 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002436 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002437 }
2438 else {
2439 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002440 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002441 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002442 }
2443 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002444 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002445
Ted Kremenek0e561a32008-03-21 21:30:14 +00002446 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002447 BindExpr(GetState(Pred), Ex,
2448 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002449}
2450
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002451
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002452void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002453 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002454
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002455 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002456
2457 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002458 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002459
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002460 case UnaryOperator::Deref: {
2461
2462 Expr* Ex = U->getSubExpr()->IgnoreParens();
2463 NodeSet Tmp;
2464 Visit(Ex, Pred, Tmp);
2465
2466 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002467
Ted Kremeneka8538d92009-02-13 01:45:31 +00002468 const GRState* state = GetState(*I);
2469 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002470
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002471 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00002472 MakeNode(Dst, U, *I, BindExpr(state, U, location),
2473 ProgramPoint::PostLValueKind);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002474 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002475 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002476 }
2477
2478 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002479 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002480
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002481 case UnaryOperator::Real: {
2482
2483 Expr* Ex = U->getSubExpr()->IgnoreParens();
2484 NodeSet Tmp;
2485 Visit(Ex, Pred, Tmp);
2486
2487 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2488
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002489 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002490 if (Ex->getType()->isAnyComplexType()) {
2491 // Just report "Unknown."
2492 Dst.Add(*I);
2493 continue;
2494 }
2495
2496 // For all other types, UnaryOperator::Real is an identity operation.
2497 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002498 const GRState* state = GetState(*I);
2499 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002500 }
2501
2502 return;
2503 }
2504
2505 case UnaryOperator::Imag: {
2506
2507 Expr* Ex = U->getSubExpr()->IgnoreParens();
2508 NodeSet Tmp;
2509 Visit(Ex, Pred, Tmp);
2510
2511 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002512 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002513 if (Ex->getType()->isAnyComplexType()) {
2514 // Just report "Unknown."
2515 Dst.Add(*I);
2516 continue;
2517 }
2518
2519 // For all other types, UnaryOperator::Float returns 0.
2520 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002521 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002522 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002523 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002524 }
2525
2526 return;
2527 }
2528
2529 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002530 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002531 Dst.Add(Pred);
2532 return;
2533
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002534 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002535 case UnaryOperator::Extension: {
2536
2537 // Unary "+" is a no-op, similar to a parentheses. We still have places
2538 // where it may be a block-level expression, so we need to
2539 // generate an extra node that just propagates the value of the
2540 // subexpression.
2541
2542 Expr* Ex = U->getSubExpr()->IgnoreParens();
2543 NodeSet Tmp;
2544 Visit(Ex, Pred, Tmp);
2545
2546 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002547 const GRState* state = GetState(*I);
2548 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002549 }
2550
2551 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002552 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002553
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002554 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002555
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002556 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002557 Expr* Ex = U->getSubExpr()->IgnoreParens();
2558 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002559 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002560
2561 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002562 const GRState* state = GetState(*I);
2563 SVal V = GetSVal(state, Ex);
2564 state = BindExpr(state, U, V);
2565 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002566 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002567
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002568 return;
2569 }
2570
2571 case UnaryOperator::LNot:
2572 case UnaryOperator::Minus:
2573 case UnaryOperator::Not: {
2574
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002575 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002576 Expr* Ex = U->getSubExpr()->IgnoreParens();
2577 NodeSet Tmp;
2578 Visit(Ex, Pred, Tmp);
2579
2580 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002581 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002582
2583 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002584 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002585
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002586 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002587 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002588 continue;
2589 }
2590
Ted Kremenek60595da2008-11-15 04:01:56 +00002591// QualType DstT = getContext().getCanonicalType(U->getType());
2592// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2593//
2594// if (DstT != SrcT) // Perform promotions.
2595// V = EvalCast(V, DstT);
2596//
2597// if (V.isUnknownOrUndef()) {
2598// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2599// continue;
2600// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002601
2602 switch (U->getOpcode()) {
2603 default:
2604 assert(false && "Invalid Opcode.");
2605 break;
2606
2607 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002608 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002609 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002610 break;
2611
2612 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002613 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002614 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002615 break;
2616
2617 case UnaryOperator::LNot:
2618
2619 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2620 //
2621 // Note: technically we do "E == 0", but this is the same in the
2622 // transfer functions as "0 == E".
2623
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002624 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002625 Loc X = Loc::MakeNull(getBasicVals());
Zhongxing Xu262fd032009-05-20 09:00:16 +00002626 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002627 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002628 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002629 }
2630 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002631 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002632#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002633 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002634 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002635#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002636 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2637 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002638 continue;
2639#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002640 }
2641
2642 break;
2643 }
2644
Ted Kremeneka8538d92009-02-13 01:45:31 +00002645 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002646 }
2647
2648 return;
2649 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002650 }
2651
2652 // Handle ++ and -- (both pre- and post-increment).
2653
2654 assert (U->isIncrementDecrementOp());
2655 NodeSet Tmp;
2656 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002657 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002658
2659 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2660
Ted Kremeneka8538d92009-02-13 01:45:31 +00002661 const GRState* state = GetState(*I);
2662 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002663
2664 // Perform a load.
2665 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002666 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002667
2668 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2669
Ted Kremeneka8538d92009-02-13 01:45:31 +00002670 state = GetState(*I2);
2671 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002672
2673 // Propagate unknown and undefined values.
2674 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002675 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002676 continue;
2677 }
2678
Ted Kremenek21028dd2009-03-11 03:54:24 +00002679 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002680 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2681 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002682
Zhongxing Xu262fd032009-05-20 09:00:16 +00002683 SVal Result = EvalBinOp(state, Op, V2, MakeConstantVal(1U, U),
2684 U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002685
2686 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002687 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002688 Result = ValMgr.getConjuredSymbolVal(Ex,
2689 Builder->getCurrentBlockCount());
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002690
2691 // If the value is a location, ++/-- should always preserve
2692 // non-nullness. Check if the original value was non-null, and if so propagate
2693 // that constraint.
2694 if (Loc::IsLocType(U->getType())) {
Zhongxing Xu262fd032009-05-20 09:00:16 +00002695 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002696 ValMgr.makeZeroVal(U->getType()),
2697 getContext().IntTy);
2698
2699 bool isFeasible = false;
2700 Assume(state, Constraint, true, isFeasible);
2701 if (!isFeasible) {
2702 // It isn't feasible for the original value to be null.
2703 // Propagate this constraint.
Zhongxing Xu262fd032009-05-20 09:00:16 +00002704 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002705 ValMgr.makeZeroVal(U->getType()),
2706 getContext().IntTy);
2707
2708 bool isFeasible = false;
2709 state = Assume(state, Constraint, false, isFeasible);
2710 assert(isFeasible && state);
2711 }
2712 }
2713 }
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002714
Ted Kremeneka8538d92009-02-13 01:45:31 +00002715 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002716
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002717 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002718 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002719 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002720 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002721}
2722
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002723void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2724 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2725}
2726
2727void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2728 AsmStmt::outputs_iterator I,
2729 AsmStmt::outputs_iterator E,
2730 NodeTy* Pred, NodeSet& Dst) {
2731 if (I == E) {
2732 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2733 return;
2734 }
2735
2736 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002737 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002738
2739 ++I;
2740
2741 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2742 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2743}
2744
2745void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2746 AsmStmt::inputs_iterator I,
2747 AsmStmt::inputs_iterator E,
2748 NodeTy* Pred, NodeSet& Dst) {
2749 if (I == E) {
2750
2751 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002752 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002753
2754 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2755 // which interprets the inline asm and stores proper results in the
2756 // outputs.
2757
Ted Kremeneka8538d92009-02-13 01:45:31 +00002758 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002759
2760 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2761 OE = A->end_outputs(); OI != OE; ++OI) {
2762
Ted Kremeneka8538d92009-02-13 01:45:31 +00002763 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002764 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002765
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002766 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002767 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002768 }
2769
Ted Kremeneka8538d92009-02-13 01:45:31 +00002770 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002771 return;
2772 }
2773
2774 NodeSet Tmp;
2775 Visit(*I, Pred, Tmp);
2776
2777 ++I;
2778
2779 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2780 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2781}
2782
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002783void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2784 assert (Builder && "GRStmtNodeBuilder must be defined.");
2785
2786 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002787
Ted Kremenek186350f2008-04-23 20:12:28 +00002788 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2789 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002790
Ted Kremenek729a9a22008-07-17 23:15:45 +00002791 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002792
Ted Kremenekb0533962008-04-18 20:35:30 +00002793 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002794
Ted Kremenekb0533962008-04-18 20:35:30 +00002795 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002796 MakeNode(Dst, S, Pred, GetState(Pred));
2797}
2798
Ted Kremenek02737ed2008-03-31 15:02:58 +00002799void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2800
2801 Expr* R = S->getRetValue();
2802
2803 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002804 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002805 return;
2806 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002807
Ted Kremenek5917d782008-11-21 00:27:44 +00002808 NodeSet Tmp;
2809 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002810
Ted Kremenek5917d782008-11-21 00:27:44 +00002811 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2812 SVal X = GetSVal((*I)->getState(), R);
2813
2814 // Check if we return the address of a stack variable.
2815 if (isa<loc::MemRegionVal>(X)) {
2816 // Determine if the value is on the stack.
2817 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002818
Ted Kremenek5917d782008-11-21 00:27:44 +00002819 if (R && getStateManager().hasStackStorage(R)) {
2820 // Create a special node representing the error.
2821 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2822 N->markAsSink();
2823 RetsStackAddr.insert(N);
2824 }
2825 continue;
2826 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002827 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002828 // Check if we return an undefined value.
2829 else if (X.isUndef()) {
2830 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2831 N->markAsSink();
2832 RetsUndef.insert(N);
2833 }
2834 continue;
2835 }
2836
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002837 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002838 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002839}
Ted Kremenek55deb972008-03-25 00:34:37 +00002840
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002841//===----------------------------------------------------------------------===//
2842// Transfer functions: Binary operators.
2843//===----------------------------------------------------------------------===//
2844
Ted Kremeneka8538d92009-02-13 01:45:31 +00002845const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002846 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002847
2848 // Divide by undefined? (potentially zero)
2849
2850 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002851 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002852
2853 if (DivUndef) {
2854 DivUndef->markAsSink();
2855 ExplicitBadDivides.insert(DivUndef);
2856 }
2857
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002858 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002859 }
2860
2861 // Check for divide/remainder-by-zero.
2862 // First, "assume" that the denominator is 0 or undefined.
2863
2864 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002865 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002866
2867 // Second, "assume" that the denominator cannot be 0.
2868
2869 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002870 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002871
2872 // Create the node for the divide-by-zero (if it occurred).
2873
2874 if (isFeasibleZero)
2875 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2876 DivZeroNode->markAsSink();
2877
2878 if (isFeasibleNotZero)
2879 ImplicitBadDivides.insert(DivZeroNode);
2880 else
2881 ExplicitBadDivides.insert(DivZeroNode);
2882
2883 }
2884
Ted Kremeneka8538d92009-02-13 01:45:31 +00002885 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002886}
2887
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002888void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002889 GRExprEngine::NodeTy* Pred,
2890 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002891
2892 NodeSet Tmp1;
2893 Expr* LHS = B->getLHS()->IgnoreParens();
2894 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002895
Ted Kremenek759623e2008-12-06 02:39:30 +00002896 // FIXME: Add proper support for ObjCKVCRefExpr.
2897 if (isa<ObjCKVCRefExpr>(LHS)) {
2898 Visit(RHS, Pred, Dst);
2899 return;
2900 }
2901
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002902 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002903 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002904 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002905 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002906
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002907 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002908
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002909 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002910
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002911 // Process the RHS.
2912
2913 NodeSet Tmp2;
2914 Visit(RHS, *I1, Tmp2);
2915
2916 // With both the LHS and RHS evaluated, process the operation itself.
2917
2918 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002919
Ted Kremeneka8538d92009-02-13 01:45:31 +00002920 const GRState* state = GetState(*I2);
2921 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002922
Ted Kremeneka8538d92009-02-13 01:45:31 +00002923 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002924 BinaryOperator::Opcode Op = B->getOpcode();
2925
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002926 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002927
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002928 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002929
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002930 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002931 // FIXME: Handle structs.
2932 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002933
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002934 if ((RightV.isUnknown() ||
2935 !getConstraintManager().canReasonAbout(RightV))
2936 && (Loc::IsLocType(T) ||
2937 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002938 unsigned Count = Builder->getCurrentBlockCount();
2939 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002940 }
2941
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002942 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002943 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002944 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2945 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002946 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002947 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002948
2949 case BinaryOperator::Div:
2950 case BinaryOperator::Rem:
2951
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002952 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002953 if (RHS->getType()->isIntegerType() &&
2954 RHS->getType()->isScalarType()) {
2955
Ted Kremeneka8538d92009-02-13 01:45:31 +00002956 state = CheckDivideZero(B, state, *I2, RightV);
2957 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002958 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002959
2960 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002961
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002962 default: {
2963
2964 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002965 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002966
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002967 // Process non-assignements except commas or short-circuited
2968 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002969
Zhongxing Xu262fd032009-05-20 09:00:16 +00002970 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002971
2972 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002973 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002974 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002975 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002976 }
2977 else
2978 Dst.Add(*I2);
2979
Ted Kremenek89063af2008-02-21 19:15:37 +00002980 continue;
2981 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002982
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002983 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002984
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002985 // The operands were *not* undefined, but the result is undefined.
2986 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002987
Ted Kremeneka8538d92009-02-13 01:45:31 +00002988 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002989 UndefNode->markAsSink();
2990 UndefResults.insert(UndefNode);
2991 }
2992
2993 continue;
2994 }
2995
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002996 // Otherwise, create a new node.
2997
Ted Kremeneka8538d92009-02-13 01:45:31 +00002998 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002999 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00003000 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00003001 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00003002
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003003 assert (B->isCompoundAssignmentOp());
3004
Ted Kremenekcad29962009-02-07 00:52:24 +00003005 switch (Op) {
3006 default:
3007 assert(0 && "Invalid opcode for compound assignment.");
3008 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
3009 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
3010 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
3011 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
3012 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
3013 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
3014 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
3015 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
3016 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
3017 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00003018 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003019
3020 // Perform a load (the LHS). This performs the checks for
3021 // null dereferences, and so on.
3022 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00003023 SVal location = GetSVal(state, LHS);
3024 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003025
3026 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
3027
Ted Kremeneka8538d92009-02-13 01:45:31 +00003028 state = GetState(*I3);
3029 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003030
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003031 // Check for divide-by-zero.
3032 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00003033 && RHS->getType()->isIntegerType()
3034 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003035
3036 // CheckDivideZero returns a new state where the denominator
3037 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003038 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003039
Ted Kremeneka8538d92009-02-13 01:45:31 +00003040 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003041 continue;
3042 }
3043
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003044 // Propagate undefined values (left-side).
3045 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003046 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003047 continue;
3048 }
3049
3050 // Propagate unknown values (left and right-side).
3051 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003052 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
3053 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003054 continue;
3055 }
3056
3057 // At this point:
3058 //
3059 // The LHS is not Undef/Unknown.
3060 // The RHS is not Unknown.
3061
3062 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00003063 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00003064 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00003065
3066 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3067 CLHSTy = getContext().getCanonicalType(CTy);
3068
Ted Kremenek60595da2008-11-15 04:01:56 +00003069 QualType LTy = getContext().getCanonicalType(LHS->getType());
3070 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00003071
3072 // Promote LHS.
3073 V = EvalCast(V, CLHSTy);
3074
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003075 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003076 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00003077 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00003078 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003079 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003080 continue;
3081 }
3082
Ted Kremenek60595da2008-11-15 04:01:56 +00003083 // Compute the result of the operation.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003084 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3085 B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003086
3087 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003088 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003089 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003090 UndefNode->markAsSink();
3091 UndefResults.insert(UndefNode);
3092 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003093 continue;
3094 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003095
3096 // EXPERIMENTAL: "Conjured" symbols.
3097 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00003098
3099 SVal LHSVal;
3100
Ted Kremenek276c6ac2009-03-11 02:24:48 +00003101 if ((Result.isUnknown() ||
3102 !getConstraintManager().canReasonAbout(Result))
3103 && (Loc::IsLocType(CTy)
3104 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00003105
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003106 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003107
Ted Kremenek60595da2008-11-15 04:01:56 +00003108 // The symbolic value is actually for the type of the left-hand side
3109 // expression, not the computation type, as this is the value the
3110 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00003111 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00003112
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00003113 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00003114 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003115 }
Ted Kremenek60595da2008-11-15 04:01:56 +00003116 else {
3117 // The left-hand side may bind to a different value then the
3118 // computation type.
3119 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3120 }
3121
Ted Kremeneka8538d92009-02-13 01:45:31 +00003122 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3123 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003124 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00003125 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003126 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003127}
Ted Kremenekee985462008-01-16 18:18:48 +00003128
3129//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003130// Transfer-function Helpers.
3131//===----------------------------------------------------------------------===//
3132
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003133void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003134 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00003135 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003136 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003137
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003138 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003139 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003140
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003141 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003142 MakeNode(Dst, Ex, Pred, *I);
3143}
3144
Ted Kremeneka8538d92009-02-13 01:45:31 +00003145void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003146 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003147 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003148
Ted Kremeneka8538d92009-02-13 01:45:31 +00003149 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003150 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003151}
3152
Zhongxing Xu262fd032009-05-20 09:00:16 +00003153SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3154 SVal L, SVal R, QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003155
3156 if (L.isUndef() || R.isUndef())
3157 return UndefinedVal();
3158
3159 if (L.isUnknown() || R.isUnknown())
3160 return UnknownVal();
3161
3162 if (isa<Loc>(L)) {
3163 if (isa<Loc>(R))
3164 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3165 else
Zhongxing Xu262fd032009-05-20 09:00:16 +00003166 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003167 }
3168
3169 if (isa<Loc>(R)) {
3170 // Support pointer arithmetic where the increment/decrement operand
3171 // is on the left and the pointer on the right.
3172
3173 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3174
3175 // Commute the operands.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003176 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003177 }
3178 else
3179 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003180 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003181}
3182
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003183//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003184// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003185//===----------------------------------------------------------------------===//
3186
Ted Kremenekaa66a322008-01-16 21:46:15 +00003187#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003188static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003189static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003190
Ted Kremenekaa66a322008-01-16 21:46:15 +00003191namespace llvm {
3192template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003193struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003194 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003195
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003196 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3197
3198 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003199 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003200 GraphPrintCheckerState->isUndefDeref(N) ||
3201 GraphPrintCheckerState->isUndefStore(N) ||
3202 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003203 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3204 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003205 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003206 GraphPrintCheckerState->isBadCall(N) ||
3207 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003208 return "color=\"red\",style=\"filled\"";
3209
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003210 if (GraphPrintCheckerState->isNoReturnCall(N))
3211 return "color=\"blue\",style=\"filled\"";
3212
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003213 return "";
3214 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003215
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003216 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003217 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003218
3219 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003220 ProgramPoint Loc = N->getLocation();
3221
3222 switch (Loc.getKind()) {
3223 case ProgramPoint::BlockEntranceKind:
3224 Out << "Block Entrance: B"
3225 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3226 break;
3227
3228 case ProgramPoint::BlockExitKind:
3229 assert (false);
3230 break;
3231
Ted Kremenekaa66a322008-01-16 21:46:15 +00003232 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003233 if (isa<PostStmt>(Loc)) {
3234 const PostStmt& L = cast<PostStmt>(Loc);
3235 Stmt* S = L.getStmt();
3236 SourceLocation SLoc = S->getLocStart();
3237
3238 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3239 llvm::raw_os_ostream OutS(Out);
3240 S->printPretty(OutS);
3241 OutS.flush();
3242
3243 if (SLoc.isFileID()) {
3244 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003245 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3246 << " col="
3247 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3248 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003249 }
3250
Ted Kremenek7090d542009-05-07 18:27:16 +00003251 if (isa<PostLoad>(Loc))
3252 Out << "\\lPostLoad\\l;";
3253 else if (isa<PostStore>(Loc))
3254 Out << "\\lPostStore\\l";
3255 else if (isa<PostLValue>(Loc))
3256 Out << "\\lPostLValue\\l";
3257 else if (isa<PostLocationChecksSucceed>(Loc))
3258 Out << "\\lPostLocationChecksSucceed\\l";
3259 else if (isa<PostNullCheckFailed>(Loc))
3260 Out << "\\lPostNullCheckFailed\\l";
3261
Ted Kremenek8c354752008-12-16 22:02:27 +00003262 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3263 Out << "\\|Implicit-Null Dereference.\\l";
3264 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3265 Out << "\\|Explicit-Null Dereference.\\l";
3266 else if (GraphPrintCheckerState->isUndefDeref(N))
3267 Out << "\\|Dereference of undefialied value.\\l";
3268 else if (GraphPrintCheckerState->isUndefStore(N))
3269 Out << "\\|Store to Undefined Loc.";
3270 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3271 Out << "\\|Explicit divide-by zero or undefined value.";
3272 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3273 Out << "\\|Implicit divide-by zero or undefined value.";
3274 else if (GraphPrintCheckerState->isUndefResult(N))
3275 Out << "\\|Result of operation is undefined.";
3276 else if (GraphPrintCheckerState->isNoReturnCall(N))
3277 Out << "\\|Call to function marked \"noreturn\".";
3278 else if (GraphPrintCheckerState->isBadCall(N))
3279 Out << "\\|Call to NULL/Undefined.";
3280 else if (GraphPrintCheckerState->isUndefArg(N))
3281 Out << "\\|Argument in call is undefined";
3282
3283 break;
3284 }
3285
Ted Kremenekaa66a322008-01-16 21:46:15 +00003286 const BlockEdge& E = cast<BlockEdge>(Loc);
3287 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3288 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003289
3290 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003291
3292 SourceLocation SLoc = T->getLocStart();
3293
Ted Kremenekb38911f2008-01-30 23:03:39 +00003294 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003295
Ted Kremeneka95d3752008-09-13 05:16:45 +00003296 llvm::raw_os_ostream OutS(Out);
3297 E.getSrc()->printTerminator(OutS);
3298 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003299
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003300 if (SLoc.isFileID()) {
3301 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003302 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3303 << " col="
3304 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003305 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003306
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003307 if (isa<SwitchStmt>(T)) {
3308 Stmt* Label = E.getDst()->getLabel();
3309
3310 if (Label) {
3311 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3312 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003313 llvm::raw_os_ostream OutS(Out);
3314 C->getLHS()->printPretty(OutS);
3315 OutS.flush();
3316
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003317 if (Stmt* RHS = C->getRHS()) {
3318 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003319 RHS->printPretty(OutS);
3320 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003321 }
3322
3323 Out << ":";
3324 }
3325 else {
3326 assert (isa<DefaultStmt>(Label));
3327 Out << "\\ldefault:";
3328 }
3329 }
3330 else
3331 Out << "\\l(implicit) default:";
3332 }
3333 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003334 // FIXME
3335 }
3336 else {
3337 Out << "\\lCondition: ";
3338 if (*E.getSrc()->succ_begin() == E.getDst())
3339 Out << "true";
3340 else
3341 Out << "false";
3342 }
3343
3344 Out << "\\l";
3345 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003346
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003347 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3348 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003349 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003350 }
3351 }
3352
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003353 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003354
Ted Kremenekb65be702009-06-18 01:23:53 +00003355 const GRState *state = N->getState();
3356 state->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003357
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003358 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003359 return Out.str();
3360 }
3361};
3362} // end llvm namespace
3363#endif
3364
Ted Kremenekffe0f432008-03-07 22:58:01 +00003365#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003366template <typename ITERATOR>
3367GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3368
3369template <>
3370GRExprEngine::NodeTy*
3371GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3372 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3373 return I->first;
3374}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003375#endif
3376
3377void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003378#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003379 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003380 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003381
3382 // Flush any outstanding reports to make sure we cover all the nodes.
3383 // This does not cause them to get displayed.
3384 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3385 const_cast<BugType*>(*I)->FlushReports(BR);
3386
3387 // Iterate through the reports and get their nodes.
3388 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3389 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3390 const BugReportEquivClass& EQ = *I2;
3391 const BugReport &R = **EQ.begin();
3392 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3393 if (N) Src.push_back(N);
3394 }
3395 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003396
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003397 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003398 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003399 else {
3400 GraphPrintCheckerState = this;
3401 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003402
Ted Kremenekffe0f432008-03-07 22:58:01 +00003403 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003404
3405 GraphPrintCheckerState = NULL;
3406 GraphPrintSourceManager = NULL;
3407 }
3408#endif
3409}
3410
3411void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3412#ifndef NDEBUG
3413 GraphPrintCheckerState = this;
3414 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003415
Ted Kremenekcf118d42009-02-04 23:49:09 +00003416 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003417
Ted Kremenekcf118d42009-02-04 23:49:09 +00003418 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003419 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003420 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003421 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003422
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003423 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003424 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003425#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003426}