blob: e392de6aab281e2073e726b95bd6f83f0b7b8278 [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) {
Douglas Gregor68584ed2009-06-18 16:11:24 +00001485 if (FD->getAttr<NoReturnAttr>(getContext()) ||
1486 FD->getAttr<AnalyzerNoReturnAttr>(getContext()))
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001487 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001488 else {
1489 // HACK: Some functions are not marked noreturn, and don't return.
1490 // Here are a few hardwired ones. If this takes too long, we can
1491 // potentially cache these results.
1492 const char* s = FD->getIdentifier()->getName();
1493 unsigned n = strlen(s);
1494
1495 switch (n) {
1496 default:
1497 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001498
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001499 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001500 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1501 break;
1502
1503 case 5:
1504 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001505 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001506 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001507 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001508 // FIXME: use Assume to inspect the possible symbolic value of
1509 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001510 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001511 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001512 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001513 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001514 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001515 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001516
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001517 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001518 if (!memcmp(s, "Assert", 6)) {
1519 Builder->BuildSinks = true;
1520 break;
1521 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001522
1523 // FIXME: This is just a wrapper around throwing an exception.
1524 // Eventually inter-procedural analysis should handle this easily.
1525 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1526
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001527 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001528
1529 case 7:
1530 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1531 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001532
Ted Kremenekf47bb782008-04-30 17:54:04 +00001533 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001534 if (!memcmp(s ,"db_error", 8) ||
1535 !memcmp(s, "__assert", 8))
1536 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001537 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001538
1539 case 12:
1540 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1541 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001542
Ted Kremenekf9683082008-09-19 02:30:47 +00001543 case 13:
1544 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1545 break;
1546
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001547 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001548 if (!memcmp(s, "dtrace_assfail", 14) ||
1549 !memcmp(s, "yy_fatal_error", 14))
1550 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001551 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001552
1553 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001554 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001555 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1556 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001557 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001558
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001559 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001560 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001561
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001562 }
1563 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001564
1565 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001566
Zhongxing Xu369f4472009-04-20 05:24:46 +00001567 if (FD) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001568
Zhongxing Xu369f4472009-04-20 05:24:46 +00001569 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001570 switch (id) {
1571 case Builtin::BI__builtin_expect: {
1572 // For __builtin_expect, just return the value of the subexpression.
1573 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001574 SVal X = GetSVal(state, *(CE->arg_begin()));
1575 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001576 continue;
1577 }
1578
Ted Kremenekb3021332008-11-02 00:35:01 +00001579 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001580 // FIXME: Refactor into StoreManager itself?
1581 MemRegionManager& RM = getStateManager().getRegionManager();
1582 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001583 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001584
1585 // Set the extent of the region in bytes. This enables us to use the
1586 // SVal of the argument directly. If we save the extent in bits, we
1587 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001588 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1589 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001590
Ted Kremeneka8538d92009-02-13 01:45:31 +00001591 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001592 continue;
1593 }
1594
Ted Kremenek55aea312008-03-05 22:59:42 +00001595 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001596 break;
1597 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001598 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001599
Ted Kremenek186350f2008-04-23 20:12:28 +00001600 // Check any arguments passed-by-value against being undefined.
1601
1602 bool badArg = false;
1603
1604 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1605 I != E; ++I) {
1606
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001607 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001608 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001609
Ted Kremenek186350f2008-04-23 20:12:28 +00001610 if (N) {
1611 N->markAsSink();
1612 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001613 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001614
Ted Kremenek186350f2008-04-23 20:12:28 +00001615 badArg = true;
1616 break;
1617 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001618 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001619
1620 if (badArg)
1621 continue;
1622
1623 // Dispatch to the plug-in transfer function.
1624
1625 unsigned size = Dst.size();
1626 SaveOr OldHasGen(Builder->HasGeneratedNode);
1627 EvalCall(Dst, CE, L, *DI);
1628
1629 // Handle the case where no nodes where generated. Auto-generate that
1630 // contains the updated state if we aren't generating sinks.
1631
1632 if (!Builder->BuildSinks && Dst.size() == size &&
1633 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001634 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001635 }
1636}
1637
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001638//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001639// Transfer function: Objective-C ivar references.
1640//===----------------------------------------------------------------------===//
1641
Ted Kremenekf5cae632009-02-28 20:50:43 +00001642static std::pair<const void*,const void*> EagerlyAssumeTag
1643 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1644
Ted Kremenekb2939022009-02-25 23:32:10 +00001645void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001646 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1647 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001648
1649 // Test if the previous node was as the same expression. This can happen
1650 // when the expression fails to evaluate to anything meaningful and
1651 // (as an optimization) we don't generate a node.
1652 ProgramPoint P = Pred->getLocation();
1653 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1654 Dst.Add(Pred);
1655 continue;
1656 }
1657
Ted Kremenek48af2a92009-02-25 22:32:02 +00001658 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001659 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001660 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001661 // First assume that the condition is true.
1662 bool isFeasible = false;
1663 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1664 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001665 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1666 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001667 stateTrue, Pred));
1668 }
1669
1670 // Next, assume that the condition is false.
1671 isFeasible = false;
1672 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1673 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001674 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1675 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001676 stateFalse, Pred));
1677 }
1678 }
1679 else
1680 Dst.Add(Pred);
1681 }
1682}
1683
1684//===----------------------------------------------------------------------===//
1685// Transfer function: Objective-C ivar references.
1686//===----------------------------------------------------------------------===//
1687
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001688void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1689 NodeTy* Pred, NodeSet& Dst,
1690 bool asLValue) {
1691
1692 Expr* Base = cast<Expr>(Ex->getBase());
1693 NodeSet Tmp;
1694 Visit(Base, Pred, Tmp);
1695
1696 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001697 const GRState* state = GetState(*I);
1698 SVal BaseVal = GetSVal(state, Base);
1699 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001700
1701 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001702 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001703 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001704 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001705 }
1706}
1707
1708//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001709// Transfer function: Objective-C fast enumeration 'for' statements.
1710//===----------------------------------------------------------------------===//
1711
1712void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1713 NodeTy* Pred, NodeSet& Dst) {
1714
1715 // ObjCForCollectionStmts are processed in two places. This method
1716 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1717 // statements within a basic block. This transfer function does two things:
1718 //
1719 // (1) binds the next container value to 'element'. This creates a new
1720 // node in the ExplodedGraph.
1721 //
1722 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1723 // whether or not the container has any more elements. This value
1724 // will be tested in ProcessBranch. We need to explicitly bind
1725 // this value because a container can contain nil elements.
1726 //
1727 // FIXME: Eventually this logic should actually do dispatches to
1728 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1729 // This will require simulating a temporary NSFastEnumerationState, either
1730 // through an SVal or through the use of MemRegions. This value can
1731 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1732 // terminates we reclaim the temporary (it goes out of scope) and we
1733 // we can test if the SVal is 0 or if the MemRegion is null (depending
1734 // on what approach we take).
1735 //
1736 // For now: simulate (1) by assigning either a symbol or nil if the
1737 // container is empty. Thus this transfer function will by default
1738 // result in state splitting.
1739
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001740 Stmt* elem = S->getElement();
1741 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001742
1743 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001744 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001745 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001746 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1747 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1748 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001749 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001750
1751 NodeSet Tmp;
1752 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001753
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001754 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1755 const GRState* state = GetState(*I);
1756 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1757 }
1758}
1759
1760void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1761 NodeTy* Pred, NodeSet& Dst,
1762 SVal ElementV) {
1763
1764
Ted Kremenekaf337412008-11-12 19:24:17 +00001765
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001766 // Get the current state. Use 'EvalLocation' to determine if it is a null
1767 // pointer, etc.
1768 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001769
Ted Kremenek8c354752008-12-16 22:02:27 +00001770 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1771 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001772 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001773
Ted Kremenekb65be702009-06-18 01:23:53 +00001774 const GRState *state = GetState(Pred);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001775
Ted Kremenekaf337412008-11-12 19:24:17 +00001776 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001777 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001778 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
Ted Kremenekb65be702009-06-18 01:23:53 +00001779 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenekaf337412008-11-12 19:24:17 +00001780
Ted Kremenekaf337412008-11-12 19:24:17 +00001781 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001782 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
Ted Kremenekb65be702009-06-18 01:23:53 +00001783 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001784
1785 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1786 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1787 // FIXME: The proper thing to do is to really iterate over the
1788 // container. We will do this with dispatch logic to the store.
1789 // For now, just 'conjure' up a symbolic value.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001790 QualType T = R->getValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001791 assert (Loc::IsLocType(T));
1792 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001793 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1794 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
Ted Kremenekb65be702009-06-18 01:23:53 +00001795 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001796
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001797 // Bind the location to 'nil' on the false branch.
1798 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
Ted Kremenekb65be702009-06-18 01:23:53 +00001799 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001800 }
1801
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001802 // Create the new nodes.
1803 MakeNode(Dst, S, Pred, hasElems);
1804 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001805}
1806
1807//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001808// Transfer function: Objective-C message expressions.
1809//===----------------------------------------------------------------------===//
1810
1811void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1812 NodeSet& Dst){
1813
1814 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1815 Pred, Dst);
1816}
1817
1818void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001819 ObjCMessageExpr::arg_iterator AI,
1820 ObjCMessageExpr::arg_iterator AE,
1821 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001822 if (AI == AE) {
1823
1824 // Process the receiver.
1825
1826 if (Expr* Receiver = ME->getReceiver()) {
1827 NodeSet Tmp;
1828 Visit(Receiver, Pred, Tmp);
1829
1830 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1831 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1832
1833 return;
1834 }
1835
1836 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1837 return;
1838 }
1839
1840 NodeSet Tmp;
1841 Visit(*AI, Pred, Tmp);
1842
1843 ++AI;
1844
1845 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1846 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1847}
1848
1849void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1850 NodeTy* Pred,
1851 NodeSet& Dst) {
1852
1853 // FIXME: More logic for the processing the method call.
1854
Ted Kremeneka8538d92009-02-13 01:45:31 +00001855 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001856 bool RaisesException = false;
1857
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001858
1859 if (Expr* Receiver = ME->getReceiver()) {
1860
Ted Kremeneka8538d92009-02-13 01:45:31 +00001861 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001862
Ted Kremenek21fe8372009-02-19 04:06:22 +00001863 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001864 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001865 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001866
1867 if (N) {
1868 N->markAsSink();
1869 UndefReceivers.insert(N);
1870 }
1871
1872 return;
1873 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001874
Ted Kremenek21fe8372009-02-19 04:06:22 +00001875 // "Assume" that the receiver is not NULL.
1876 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001877 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001878
1879 // "Assume" that the receiver is NULL.
1880 bool isFeasibleNull = false;
1881 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1882
1883 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001884 QualType RetTy = ME->getType();
1885
Ted Kremenek21fe8372009-02-19 04:06:22 +00001886 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001887 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001888 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001889 // The [0 ...] expressions will return garbage. Flag either an
1890 // explicit or implicit error. Because of the structure of this
1891 // function we currently do not bifurfacte the state graph at
1892 // this point.
1893 // FIXME: We should bifurcate and fill the returned struct with
1894 // garbage.
1895 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1896 N->markAsSink();
1897 if (isFeasibleNotNull)
1898 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001899 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001900 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001901 }
1902 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001903 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001904 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001905 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001906 if (RetTy != Ctx.VoidTy) {
1907 if (BR.getParentMap().isConsumedExpr(ME)) {
1908 // sizeof(void *)
1909 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1910 // sizeof(return type)
1911 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001912
Ted Kremenekfe630b92009-04-09 05:45:56 +00001913 if(voidPtrSize < returnTypeSize) {
1914 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1915 N->markAsSink();
1916 if(isFeasibleNotNull)
1917 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001918 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001919 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001920 }
1921 }
1922 else if (!isFeasibleNotNull) {
1923 // Handle the safe cases where the return value is 0 if the
1924 // receiver is nil.
1925 //
1926 // FIXME: For now take the conservative approach that we only
1927 // return null values if we *know* that the receiver is nil.
1928 // This is because we can have surprises like:
1929 //
1930 // ... = [[NSScreens screens] objectAtIndex:0];
1931 //
1932 // What can happen is that [... screens] could return nil, but
1933 // it most likely isn't nil. We should assume the semantics
1934 // of this case unless we have *a lot* more knowledge.
1935 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001936 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001937 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001938 return;
1939 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001940 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001941 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001942 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001943 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001944 // of this method should assume that the receiver is not nil.
1945 if (!StNotNull)
1946 return;
1947
Ted Kremenekda9ae602009-04-08 18:51:08 +00001948 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001949 }
1950
Ted Kremeneke448ab42008-05-01 18:33:28 +00001951 // Check if the "raise" message was sent.
1952 if (ME->getSelector() == RaiseSel)
1953 RaisesException = true;
1954 }
1955 else {
1956
1957 IdentifierInfo* ClsName = ME->getClassName();
1958 Selector S = ME->getSelector();
1959
1960 // Check for special instance methods.
1961
1962 if (!NSExceptionII) {
1963 ASTContext& Ctx = getContext();
1964
1965 NSExceptionII = &Ctx.Idents.get("NSException");
1966 }
1967
1968 if (ClsName == NSExceptionII) {
1969
1970 enum { NUM_RAISE_SELECTORS = 2 };
1971
1972 // Lazily create a cache of the selectors.
1973
1974 if (!NSExceptionInstanceRaiseSelectors) {
1975
1976 ASTContext& Ctx = getContext();
1977
1978 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1979
1980 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1981 unsigned idx = 0;
1982
1983 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001984 II.push_back(&Ctx.Idents.get("raise"));
1985 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001986 NSExceptionInstanceRaiseSelectors[idx++] =
1987 Ctx.Selectors.getSelector(II.size(), &II[0]);
1988
1989 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001990 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001991 NSExceptionInstanceRaiseSelectors[idx++] =
1992 Ctx.Selectors.getSelector(II.size(), &II[0]);
1993 }
1994
1995 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1996 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1997 RaisesException = true; break;
1998 }
1999 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002000 }
2001
2002 // Check for any arguments that are uninitialized/undefined.
2003
2004 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
2005 I != E; ++I) {
2006
Ted Kremeneka8538d92009-02-13 01:45:31 +00002007 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002008
2009 // Generate an error node for passing an uninitialized/undefined value
2010 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002011 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002012
2013 if (N) {
2014 N->markAsSink();
2015 MsgExprUndefArgs[N] = *I;
2016 }
2017
2018 return;
2019 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00002020 }
2021
2022 // Check if we raise an exception. For now treat these as sinks. Eventually
2023 // we will want to handle exceptions properly.
2024
2025 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2026
2027 if (RaisesException)
2028 Builder->BuildSinks = true;
2029
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002030 // Dispatch to plug-in transfer function.
2031
2032 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00002033 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002034
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002035 EvalObjCMessageExpr(Dst, ME, Pred);
2036
2037 // Handle the case where no nodes where generated. Auto-generate that
2038 // contains the updated state if we aren't generating sinks.
2039
Ted Kremenekb0533962008-04-18 20:35:30 +00002040 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002041 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002042}
2043
2044//===----------------------------------------------------------------------===//
2045// Transfer functions: Miscellaneous statements.
2046//===----------------------------------------------------------------------===//
2047
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002048void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2049 QualType PtrTy,
2050 Expr* CastE, NodeTy* Pred,
2051 NodeSet& Dst) {
2052 if (!V.isUnknownOrUndef()) {
2053 // FIXME: Determine if the number of bits of the target type is
2054 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00002055 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00002056 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
2057 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002058 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002059 else
2060 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002061}
2062
2063
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002064void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002065 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002066 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002067 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002068
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002069 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002070 T = ExCast->getTypeAsWritten();
2071
Zhongxing Xued340f72008-10-22 08:02:16 +00002072 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002073 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002074 else
2075 Visit(Ex, Pred, S1);
2076
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002077 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00002078 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002079 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002080 Dst.Add(*I1);
2081
Ted Kremenek874d63f2008-01-24 02:02:54 +00002082 return;
2083 }
2084
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002085 // FIXME: The rest of this should probably just go into EvalCall, and
2086 // let the transfer function object be responsible for constructing
2087 // nodes.
2088
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002089 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00002090 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002091 const GRState* state = GetState(N);
2092 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00002093 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002094
2095 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002096 if (V.isUnknown()) {
2097 Dst.Add(N);
2098 continue;
2099 }
2100
2101 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00002102 if (V.isUndef())
2103 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002104
2105 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002106 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00002107 C.getCanonicalType(ExTy).getUnqualifiedType())
2108 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002109
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002110 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002111 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002112 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002113 continue;
2114 }
2115
2116 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00002117 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002118 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002119 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002120 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00002121 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002122 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002123 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002124
Ted Kremenekc5302912009-03-05 20:22:13 +00002125 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00002126 }
2127
2128 // Just pass through function and block pointers.
2129 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2130 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00002131 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002132 }
2133
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002134 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00002135 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002136 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00002137 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002138
2139 // Are we casting from an array to a pointer? If so just pass on
2140 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00002141 if (T->isPointerType())
2142 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002143
2144 // Are we casting from an array to an integer? If so, cast the decayed
2145 // pointer value to an integer.
2146 assert(T->isIntegerType());
2147 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2148 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002149 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00002150 continue;
2151 }
2152
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002153 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002154 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2155 // FIXME: For TypedViewRegions, we should handle the case where the
2156 // underlying symbolic pointer is a function pointer or
2157 // block pointer.
2158
2159 // FIXME: We should handle the case where we strip off view layers to get
2160 // to a desugared type.
2161
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002162 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00002163 // We get a symbolic function pointer for a dereference of a function
2164 // pointer, but it is of function type. Example:
2165
2166 // struct FPRec {
2167 // void (*my_func)(int * x);
2168 // };
2169 //
2170 // int bar(int x);
2171 //
2172 // int f1_a(struct FPRec* foo) {
2173 // int x;
2174 // (*foo->my_func)(&x);
2175 // return bar(x)+1; // no-warning
2176 // }
2177
2178 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002179
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002180 const MemRegion* R = RV->getRegion();
2181 StoreManager& StoreMgr = getStoreManager();
2182
2183 // Delegate to store manager to get the result of casting a region
2184 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002185 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002186
2187 // Inspect the result. If the MemRegion* returned is NULL, this
2188 // expression evaluates to UnknownVal.
2189 R = Res.getRegion();
2190 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2191
2192 // Generate the new node in the ExplodedGraph.
2193 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002194 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002195 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002196 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002197 DispatchCast: {
2198 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2199 EvalCast(V, CastE->getType())));
2200 continue;
2201 }
2202
2203 PassThrough: {
2204 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2205 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002206 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002207}
2208
Ted Kremenek4f090272008-10-27 21:54:31 +00002209void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002210 NodeTy* Pred, NodeSet& Dst,
2211 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002212 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2213 NodeSet Tmp;
2214 Visit(ILE, Pred, Tmp);
2215
2216 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002217 const GRState* state = GetState(*I);
2218 SVal ILV = GetSVal(state, ILE);
2219 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002220
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002221 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002222 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002223 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002224 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002225 }
2226}
2227
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002228void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002229
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002230 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002231 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002232
2233 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002234 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002235
Ted Kremenekefd59942008-12-08 22:47:34 +00002236 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002237 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002238
2239 // FIXME: static variables may have an initializer, but the second
2240 // time a function is called those values may not be current.
2241 NodeSet Tmp;
2242
Ted Kremenekaf337412008-11-12 19:24:17 +00002243 if (InitEx)
2244 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002245
2246 if (Tmp.empty())
2247 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002248
2249 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002250 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002251 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002252
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002253 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2254 QualType T = getContext().getCanonicalType(VD->getType());
2255 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2256 // FIXME: Handle multi-dimensional VLAs.
2257
2258 Expr* SE = VLA->getSizeExpr();
2259 SVal Size = GetSVal(state, SE);
2260
2261 if (Size.isUndef()) {
2262 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2263 N->markAsSink();
2264 ExplicitBadSizedVLA.insert(N);
2265 }
2266 continue;
2267 }
2268
2269 bool isFeasibleZero = false;
2270 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2271
2272 bool isFeasibleNotZero = false;
2273 state = Assume(state, Size, true, isFeasibleNotZero);
2274
2275 if (isFeasibleZero) {
2276 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2277 N->markAsSink();
2278 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2279 else ExplicitBadSizedVLA.insert(N);
2280 }
2281 }
2282
2283 if (!isFeasibleNotZero)
2284 continue;
2285 }
2286
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002287 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002288 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002289 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002290 QualType T = VD->getType();
2291
2292 // Recover some path-sensitivity if a scalar value evaluated to
2293 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002294 if (InitVal.isUnknown() ||
2295 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002296 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002297 }
2298
Ted Kremeneka8538d92009-02-13 01:45:31 +00002299 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002300
2301 // The next thing to do is check if the GRTransferFuncs object wants to
2302 // update the state based on the new binding. If the GRTransferFunc
2303 // object doesn't do anything, just auto-propagate the current state.
2304 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2305 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2306 InitVal);
2307 }
2308 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002309 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002310 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002311 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002312 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002313}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002314
Ted Kremenekf75b1862008-10-30 17:47:32 +00002315namespace {
2316 // This class is used by VisitInitListExpr as an item in a worklist
2317 // for processing the values contained in an InitListExpr.
2318class VISIBILITY_HIDDEN InitListWLItem {
2319public:
2320 llvm::ImmutableList<SVal> Vals;
2321 GRExprEngine::NodeTy* N;
2322 InitListExpr::reverse_iterator Itr;
2323
2324 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2325 InitListExpr::reverse_iterator itr)
2326 : Vals(vals), N(n), Itr(itr) {}
2327};
2328}
2329
2330
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002331void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2332 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002333
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002334 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002335 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002336 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002337
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002338 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002339
Ted Kremeneka49e3672008-10-30 23:14:36 +00002340 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002341
Ted Kremeneka49e3672008-10-30 23:14:36 +00002342 // Handle base case where the initializer has no elements.
2343 // e.g: static int* myArray[] = {};
2344 if (NumInitElements == 0) {
2345 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2346 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2347 return;
2348 }
2349
2350 // Create a worklist to process the initializers.
2351 llvm::SmallVector<InitListWLItem, 10> WorkList;
2352 WorkList.reserve(NumInitElements);
2353 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002354 InitListExpr::reverse_iterator ItrEnd = E->rend();
2355
Ted Kremeneka49e3672008-10-30 23:14:36 +00002356 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002357 while (!WorkList.empty()) {
2358 InitListWLItem X = WorkList.back();
2359 WorkList.pop_back();
2360
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002361 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002362 Visit(*X.Itr, X.N, Tmp);
2363
2364 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002365
Ted Kremenekf75b1862008-10-30 17:47:32 +00002366 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2367 // Get the last initializer value.
2368 state = GetState(*NI);
2369 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2370
2371 // Construct the new list of values by prepending the new value to
2372 // the already constructed list.
2373 llvm::ImmutableList<SVal> NewVals =
2374 getBasicVals().consVals(InitV, X.Vals);
2375
2376 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002377 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002378 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002379
Ted Kremenekf75b1862008-10-30 17:47:32 +00002380 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002381 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002382 }
2383 else {
2384 // Still some initializer values to go. Push them onto the worklist.
2385 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2386 }
2387 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002388 }
Ted Kremenek87903072008-10-30 18:34:31 +00002389
2390 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002391 }
2392
Ted Kremenek062e2f92008-11-13 06:10:40 +00002393 if (T->isUnionType() || T->isVectorType()) {
2394 // FIXME: to be implemented.
2395 // Note: That vectors can return true for T->isIntegerType()
2396 MakeNode(Dst, E, Pred, state);
2397 return;
2398 }
2399
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002400 if (Loc::IsLocType(T) || T->isIntegerType()) {
2401 assert (E->getNumInits() == 1);
2402 NodeSet Tmp;
2403 Expr* Init = E->getInit(0);
2404 Visit(Init, Pred, Tmp);
2405 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2406 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002407 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002408 }
2409 return;
2410 }
2411
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002412
2413 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2414 assert(0 && "unprocessed InitListExpr type");
2415}
Ted Kremenekf233d482008-02-05 00:26:40 +00002416
Sebastian Redl05189992008-11-11 17:56:53 +00002417/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2418void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2419 NodeTy* Pred,
2420 NodeSet& Dst) {
2421 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002422 uint64_t amt;
2423
2424 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002425 if (T == getContext().VoidTy) {
2426 // sizeof(void) == 1 byte.
2427 amt = 1;
2428 }
2429 else if (!T.getTypePtr()->isConstantSizeType()) {
2430 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002431 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002432 }
2433 else if (T->isObjCInterfaceType()) {
2434 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2435 // the compiler has laid out its representation. Just report Unknown
2436 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002437 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002438 }
2439 else {
2440 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002441 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002442 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002443 }
2444 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002445 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002446
Ted Kremenek0e561a32008-03-21 21:30:14 +00002447 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002448 BindExpr(GetState(Pred), Ex,
2449 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002450}
2451
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002452
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002453void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002454 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002455
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002456 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002457
2458 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002459 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002460
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002461 case UnaryOperator::Deref: {
2462
2463 Expr* Ex = U->getSubExpr()->IgnoreParens();
2464 NodeSet Tmp;
2465 Visit(Ex, Pred, Tmp);
2466
2467 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002468
Ted Kremeneka8538d92009-02-13 01:45:31 +00002469 const GRState* state = GetState(*I);
2470 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002471
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002472 if (asLValue)
Ted Kremenek7090d542009-05-07 18:27:16 +00002473 MakeNode(Dst, U, *I, BindExpr(state, U, location),
2474 ProgramPoint::PostLValueKind);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002475 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002476 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002477 }
2478
2479 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002480 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002481
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002482 case UnaryOperator::Real: {
2483
2484 Expr* Ex = U->getSubExpr()->IgnoreParens();
2485 NodeSet Tmp;
2486 Visit(Ex, Pred, Tmp);
2487
2488 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2489
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002490 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002491 if (Ex->getType()->isAnyComplexType()) {
2492 // Just report "Unknown."
2493 Dst.Add(*I);
2494 continue;
2495 }
2496
2497 // For all other types, UnaryOperator::Real is an identity operation.
2498 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002499 const GRState* state = GetState(*I);
2500 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002501 }
2502
2503 return;
2504 }
2505
2506 case UnaryOperator::Imag: {
2507
2508 Expr* Ex = U->getSubExpr()->IgnoreParens();
2509 NodeSet Tmp;
2510 Visit(Ex, Pred, Tmp);
2511
2512 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002513 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002514 if (Ex->getType()->isAnyComplexType()) {
2515 // Just report "Unknown."
2516 Dst.Add(*I);
2517 continue;
2518 }
2519
2520 // For all other types, UnaryOperator::Float returns 0.
2521 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002522 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002523 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002524 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002525 }
2526
2527 return;
2528 }
2529
2530 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002531 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002532 Dst.Add(Pred);
2533 return;
2534
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002535 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002536 case UnaryOperator::Extension: {
2537
2538 // Unary "+" is a no-op, similar to a parentheses. We still have places
2539 // where it may be a block-level expression, so we need to
2540 // generate an extra node that just propagates the value of the
2541 // subexpression.
2542
2543 Expr* Ex = U->getSubExpr()->IgnoreParens();
2544 NodeSet Tmp;
2545 Visit(Ex, Pred, Tmp);
2546
2547 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002548 const GRState* state = GetState(*I);
2549 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002550 }
2551
2552 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002553 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002554
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002555 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002556
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002557 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002558 Expr* Ex = U->getSubExpr()->IgnoreParens();
2559 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002560 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002561
2562 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002563 const GRState* state = GetState(*I);
2564 SVal V = GetSVal(state, Ex);
2565 state = BindExpr(state, U, V);
2566 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002567 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002568
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002569 return;
2570 }
2571
2572 case UnaryOperator::LNot:
2573 case UnaryOperator::Minus:
2574 case UnaryOperator::Not: {
2575
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002576 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002577 Expr* Ex = U->getSubExpr()->IgnoreParens();
2578 NodeSet Tmp;
2579 Visit(Ex, Pred, Tmp);
2580
2581 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002582 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002583
2584 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002585 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002586
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002587 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002588 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002589 continue;
2590 }
2591
Ted Kremenek60595da2008-11-15 04:01:56 +00002592// QualType DstT = getContext().getCanonicalType(U->getType());
2593// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2594//
2595// if (DstT != SrcT) // Perform promotions.
2596// V = EvalCast(V, DstT);
2597//
2598// if (V.isUnknownOrUndef()) {
2599// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2600// continue;
2601// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002602
2603 switch (U->getOpcode()) {
2604 default:
2605 assert(false && "Invalid Opcode.");
2606 break;
2607
2608 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002609 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002610 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002611 break;
2612
2613 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002614 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002615 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002616 break;
2617
2618 case UnaryOperator::LNot:
2619
2620 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2621 //
2622 // Note: technically we do "E == 0", but this is the same in the
2623 // transfer functions as "0 == E".
2624
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002625 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002626 Loc X = Loc::MakeNull(getBasicVals());
Zhongxing Xu262fd032009-05-20 09:00:16 +00002627 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002628 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002629 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002630 }
2631 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002632 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002633#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002634 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002635 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002636#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002637 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2638 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002639 continue;
2640#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002641 }
2642
2643 break;
2644 }
2645
Ted Kremeneka8538d92009-02-13 01:45:31 +00002646 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002647 }
2648
2649 return;
2650 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002651 }
2652
2653 // Handle ++ and -- (both pre- and post-increment).
2654
2655 assert (U->isIncrementDecrementOp());
2656 NodeSet Tmp;
2657 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002658 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002659
2660 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2661
Ted Kremeneka8538d92009-02-13 01:45:31 +00002662 const GRState* state = GetState(*I);
2663 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002664
2665 // Perform a load.
2666 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002667 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002668
2669 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2670
Ted Kremeneka8538d92009-02-13 01:45:31 +00002671 state = GetState(*I2);
2672 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002673
2674 // Propagate unknown and undefined values.
2675 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002676 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002677 continue;
2678 }
2679
Ted Kremenek21028dd2009-03-11 03:54:24 +00002680 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002681 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2682 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002683
Zhongxing Xu262fd032009-05-20 09:00:16 +00002684 SVal Result = EvalBinOp(state, Op, V2, MakeConstantVal(1U, U),
2685 U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002686
2687 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002688 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002689 Result = ValMgr.getConjuredSymbolVal(Ex,
2690 Builder->getCurrentBlockCount());
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002691
2692 // If the value is a location, ++/-- should always preserve
2693 // non-nullness. Check if the original value was non-null, and if so propagate
2694 // that constraint.
2695 if (Loc::IsLocType(U->getType())) {
Zhongxing Xu262fd032009-05-20 09:00:16 +00002696 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002697 ValMgr.makeZeroVal(U->getType()),
2698 getContext().IntTy);
2699
2700 bool isFeasible = false;
2701 Assume(state, Constraint, true, isFeasible);
2702 if (!isFeasible) {
2703 // It isn't feasible for the original value to be null.
2704 // Propagate this constraint.
Zhongxing Xu262fd032009-05-20 09:00:16 +00002705 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002706 ValMgr.makeZeroVal(U->getType()),
2707 getContext().IntTy);
2708
2709 bool isFeasible = false;
2710 state = Assume(state, Constraint, false, isFeasible);
2711 assert(isFeasible && state);
2712 }
2713 }
2714 }
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002715
Ted Kremeneka8538d92009-02-13 01:45:31 +00002716 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002717
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002718 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002719 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002720 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002721 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002722}
2723
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002724void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2725 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2726}
2727
2728void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2729 AsmStmt::outputs_iterator I,
2730 AsmStmt::outputs_iterator E,
2731 NodeTy* Pred, NodeSet& Dst) {
2732 if (I == E) {
2733 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2734 return;
2735 }
2736
2737 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002738 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002739
2740 ++I;
2741
2742 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2743 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2744}
2745
2746void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2747 AsmStmt::inputs_iterator I,
2748 AsmStmt::inputs_iterator E,
2749 NodeTy* Pred, NodeSet& Dst) {
2750 if (I == E) {
2751
2752 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002753 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002754
2755 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2756 // which interprets the inline asm and stores proper results in the
2757 // outputs.
2758
Ted Kremeneka8538d92009-02-13 01:45:31 +00002759 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002760
2761 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2762 OE = A->end_outputs(); OI != OE; ++OI) {
2763
Ted Kremeneka8538d92009-02-13 01:45:31 +00002764 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002765 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002766
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002767 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002768 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002769 }
2770
Ted Kremeneka8538d92009-02-13 01:45:31 +00002771 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002772 return;
2773 }
2774
2775 NodeSet Tmp;
2776 Visit(*I, Pred, Tmp);
2777
2778 ++I;
2779
2780 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2781 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2782}
2783
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002784void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2785 assert (Builder && "GRStmtNodeBuilder must be defined.");
2786
2787 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002788
Ted Kremenek186350f2008-04-23 20:12:28 +00002789 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2790 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002791
Ted Kremenek729a9a22008-07-17 23:15:45 +00002792 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002793
Ted Kremenekb0533962008-04-18 20:35:30 +00002794 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002795
Ted Kremenekb0533962008-04-18 20:35:30 +00002796 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002797 MakeNode(Dst, S, Pred, GetState(Pred));
2798}
2799
Ted Kremenek02737ed2008-03-31 15:02:58 +00002800void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2801
2802 Expr* R = S->getRetValue();
2803
2804 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002805 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002806 return;
2807 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002808
Ted Kremenek5917d782008-11-21 00:27:44 +00002809 NodeSet Tmp;
2810 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002811
Ted Kremenek5917d782008-11-21 00:27:44 +00002812 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2813 SVal X = GetSVal((*I)->getState(), R);
2814
2815 // Check if we return the address of a stack variable.
2816 if (isa<loc::MemRegionVal>(X)) {
2817 // Determine if the value is on the stack.
2818 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002819
Ted Kremenek5917d782008-11-21 00:27:44 +00002820 if (R && getStateManager().hasStackStorage(R)) {
2821 // Create a special node representing the error.
2822 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2823 N->markAsSink();
2824 RetsStackAddr.insert(N);
2825 }
2826 continue;
2827 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002828 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002829 // Check if we return an undefined value.
2830 else if (X.isUndef()) {
2831 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2832 N->markAsSink();
2833 RetsUndef.insert(N);
2834 }
2835 continue;
2836 }
2837
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002838 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002839 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002840}
Ted Kremenek55deb972008-03-25 00:34:37 +00002841
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002842//===----------------------------------------------------------------------===//
2843// Transfer functions: Binary operators.
2844//===----------------------------------------------------------------------===//
2845
Ted Kremeneka8538d92009-02-13 01:45:31 +00002846const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002847 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002848
2849 // Divide by undefined? (potentially zero)
2850
2851 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002852 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002853
2854 if (DivUndef) {
2855 DivUndef->markAsSink();
2856 ExplicitBadDivides.insert(DivUndef);
2857 }
2858
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002859 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002860 }
2861
2862 // Check for divide/remainder-by-zero.
2863 // First, "assume" that the denominator is 0 or undefined.
2864
2865 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002866 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002867
2868 // Second, "assume" that the denominator cannot be 0.
2869
2870 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002871 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002872
2873 // Create the node for the divide-by-zero (if it occurred).
2874
2875 if (isFeasibleZero)
2876 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2877 DivZeroNode->markAsSink();
2878
2879 if (isFeasibleNotZero)
2880 ImplicitBadDivides.insert(DivZeroNode);
2881 else
2882 ExplicitBadDivides.insert(DivZeroNode);
2883
2884 }
2885
Ted Kremeneka8538d92009-02-13 01:45:31 +00002886 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002887}
2888
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002889void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002890 GRExprEngine::NodeTy* Pred,
2891 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002892
2893 NodeSet Tmp1;
2894 Expr* LHS = B->getLHS()->IgnoreParens();
2895 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002896
Ted Kremenek759623e2008-12-06 02:39:30 +00002897 // FIXME: Add proper support for ObjCKVCRefExpr.
2898 if (isa<ObjCKVCRefExpr>(LHS)) {
2899 Visit(RHS, Pred, Dst);
2900 return;
2901 }
2902
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002903 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002904 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002905 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002906 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002907
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002908 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002909
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002910 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002911
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002912 // Process the RHS.
2913
2914 NodeSet Tmp2;
2915 Visit(RHS, *I1, Tmp2);
2916
2917 // With both the LHS and RHS evaluated, process the operation itself.
2918
2919 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002920
Ted Kremeneka8538d92009-02-13 01:45:31 +00002921 const GRState* state = GetState(*I2);
2922 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002923
Ted Kremeneka8538d92009-02-13 01:45:31 +00002924 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002925 BinaryOperator::Opcode Op = B->getOpcode();
2926
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002927 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002928
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002929 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002930
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002931 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002932 // FIXME: Handle structs.
2933 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002934
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002935 if ((RightV.isUnknown() ||
2936 !getConstraintManager().canReasonAbout(RightV))
2937 && (Loc::IsLocType(T) ||
2938 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002939 unsigned Count = Builder->getCurrentBlockCount();
2940 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002941 }
2942
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002943 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002944 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002945 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2946 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002947 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002948 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002949
2950 case BinaryOperator::Div:
2951 case BinaryOperator::Rem:
2952
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002953 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002954 if (RHS->getType()->isIntegerType() &&
2955 RHS->getType()->isScalarType()) {
2956
Ted Kremeneka8538d92009-02-13 01:45:31 +00002957 state = CheckDivideZero(B, state, *I2, RightV);
2958 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002959 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002960
2961 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002962
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002963 default: {
2964
2965 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002966 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002967
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002968 // Process non-assignements except commas or short-circuited
2969 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002970
Zhongxing Xu262fd032009-05-20 09:00:16 +00002971 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002972
2973 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002974 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002975 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002976 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002977 }
2978 else
2979 Dst.Add(*I2);
2980
Ted Kremenek89063af2008-02-21 19:15:37 +00002981 continue;
2982 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002983
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002984 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002985
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002986 // The operands were *not* undefined, but the result is undefined.
2987 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002988
Ted Kremeneka8538d92009-02-13 01:45:31 +00002989 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002990 UndefNode->markAsSink();
2991 UndefResults.insert(UndefNode);
2992 }
2993
2994 continue;
2995 }
2996
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002997 // Otherwise, create a new node.
2998
Ted Kremeneka8538d92009-02-13 01:45:31 +00002999 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00003000 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00003001 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00003002 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00003003
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003004 assert (B->isCompoundAssignmentOp());
3005
Ted Kremenekcad29962009-02-07 00:52:24 +00003006 switch (Op) {
3007 default:
3008 assert(0 && "Invalid opcode for compound assignment.");
3009 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
3010 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
3011 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
3012 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
3013 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
3014 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
3015 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
3016 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
3017 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
3018 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00003019 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003020
3021 // Perform a load (the LHS). This performs the checks for
3022 // null dereferences, and so on.
3023 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00003024 SVal location = GetSVal(state, LHS);
3025 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003026
3027 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
3028
Ted Kremeneka8538d92009-02-13 01:45:31 +00003029 state = GetState(*I3);
3030 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003031
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003032 // Check for divide-by-zero.
3033 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00003034 && RHS->getType()->isIntegerType()
3035 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003036
3037 // CheckDivideZero returns a new state where the denominator
3038 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003039 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003040
Ted Kremeneka8538d92009-02-13 01:45:31 +00003041 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003042 continue;
3043 }
3044
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003045 // Propagate undefined values (left-side).
3046 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003047 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003048 continue;
3049 }
3050
3051 // Propagate unknown values (left and right-side).
3052 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00003053 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
3054 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003055 continue;
3056 }
3057
3058 // At this point:
3059 //
3060 // The LHS is not Undef/Unknown.
3061 // The RHS is not Unknown.
3062
3063 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00003064 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00003065 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00003066
3067 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3068 CLHSTy = getContext().getCanonicalType(CTy);
3069
Ted Kremenek60595da2008-11-15 04:01:56 +00003070 QualType LTy = getContext().getCanonicalType(LHS->getType());
3071 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00003072
3073 // Promote LHS.
3074 V = EvalCast(V, CLHSTy);
3075
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003076 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003077 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00003078 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00003079 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003080 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003081 continue;
3082 }
3083
Ted Kremenek60595da2008-11-15 04:01:56 +00003084 // Compute the result of the operation.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003085 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3086 B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003087
3088 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003089 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003090 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003091 UndefNode->markAsSink();
3092 UndefResults.insert(UndefNode);
3093 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003094 continue;
3095 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003096
3097 // EXPERIMENTAL: "Conjured" symbols.
3098 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00003099
3100 SVal LHSVal;
3101
Ted Kremenek276c6ac2009-03-11 02:24:48 +00003102 if ((Result.isUnknown() ||
3103 !getConstraintManager().canReasonAbout(Result))
3104 && (Loc::IsLocType(CTy)
3105 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00003106
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003107 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003108
Ted Kremenek60595da2008-11-15 04:01:56 +00003109 // The symbolic value is actually for the type of the left-hand side
3110 // expression, not the computation type, as this is the value the
3111 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00003112 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00003113
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00003114 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00003115 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003116 }
Ted Kremenek60595da2008-11-15 04:01:56 +00003117 else {
3118 // The left-hand side may bind to a different value then the
3119 // computation type.
3120 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3121 }
3122
Ted Kremeneka8538d92009-02-13 01:45:31 +00003123 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
3124 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003125 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00003126 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003127 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003128}
Ted Kremenekee985462008-01-16 18:18:48 +00003129
3130//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003131// Transfer-function Helpers.
3132//===----------------------------------------------------------------------===//
3133
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003134void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003135 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00003136 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003137 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003138
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003139 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003140 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003141
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003142 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003143 MakeNode(Dst, Ex, Pred, *I);
3144}
3145
Ted Kremeneka8538d92009-02-13 01:45:31 +00003146void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003147 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003148 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003149
Ted Kremeneka8538d92009-02-13 01:45:31 +00003150 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003151 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003152}
3153
Zhongxing Xu262fd032009-05-20 09:00:16 +00003154SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3155 SVal L, SVal R, QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003156
3157 if (L.isUndef() || R.isUndef())
3158 return UndefinedVal();
3159
3160 if (L.isUnknown() || R.isUnknown())
3161 return UnknownVal();
3162
3163 if (isa<Loc>(L)) {
3164 if (isa<Loc>(R))
3165 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3166 else
Zhongxing Xu262fd032009-05-20 09:00:16 +00003167 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003168 }
3169
3170 if (isa<Loc>(R)) {
3171 // Support pointer arithmetic where the increment/decrement operand
3172 // is on the left and the pointer on the right.
3173
3174 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3175
3176 // Commute the operands.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003177 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003178 }
3179 else
3180 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003181 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003182}
3183
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003184//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003185// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003186//===----------------------------------------------------------------------===//
3187
Ted Kremenekaa66a322008-01-16 21:46:15 +00003188#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003189static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003190static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003191
Ted Kremenekaa66a322008-01-16 21:46:15 +00003192namespace llvm {
3193template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003194struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003195 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003196
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003197 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3198
3199 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003200 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003201 GraphPrintCheckerState->isUndefDeref(N) ||
3202 GraphPrintCheckerState->isUndefStore(N) ||
3203 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003204 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3205 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003206 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003207 GraphPrintCheckerState->isBadCall(N) ||
3208 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003209 return "color=\"red\",style=\"filled\"";
3210
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003211 if (GraphPrintCheckerState->isNoReturnCall(N))
3212 return "color=\"blue\",style=\"filled\"";
3213
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003214 return "";
3215 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003216
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003217 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003218 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003219
3220 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003221 ProgramPoint Loc = N->getLocation();
3222
3223 switch (Loc.getKind()) {
3224 case ProgramPoint::BlockEntranceKind:
3225 Out << "Block Entrance: B"
3226 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3227 break;
3228
3229 case ProgramPoint::BlockExitKind:
3230 assert (false);
3231 break;
3232
Ted Kremenekaa66a322008-01-16 21:46:15 +00003233 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003234 if (isa<PostStmt>(Loc)) {
3235 const PostStmt& L = cast<PostStmt>(Loc);
3236 Stmt* S = L.getStmt();
3237 SourceLocation SLoc = S->getLocStart();
3238
3239 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3240 llvm::raw_os_ostream OutS(Out);
3241 S->printPretty(OutS);
3242 OutS.flush();
3243
3244 if (SLoc.isFileID()) {
3245 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003246 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3247 << " col="
3248 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3249 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003250 }
3251
Ted Kremenek7090d542009-05-07 18:27:16 +00003252 if (isa<PostLoad>(Loc))
3253 Out << "\\lPostLoad\\l;";
3254 else if (isa<PostStore>(Loc))
3255 Out << "\\lPostStore\\l";
3256 else if (isa<PostLValue>(Loc))
3257 Out << "\\lPostLValue\\l";
3258 else if (isa<PostLocationChecksSucceed>(Loc))
3259 Out << "\\lPostLocationChecksSucceed\\l";
3260 else if (isa<PostNullCheckFailed>(Loc))
3261 Out << "\\lPostNullCheckFailed\\l";
3262
Ted Kremenek8c354752008-12-16 22:02:27 +00003263 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3264 Out << "\\|Implicit-Null Dereference.\\l";
3265 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3266 Out << "\\|Explicit-Null Dereference.\\l";
3267 else if (GraphPrintCheckerState->isUndefDeref(N))
3268 Out << "\\|Dereference of undefialied value.\\l";
3269 else if (GraphPrintCheckerState->isUndefStore(N))
3270 Out << "\\|Store to Undefined Loc.";
3271 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3272 Out << "\\|Explicit divide-by zero or undefined value.";
3273 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3274 Out << "\\|Implicit divide-by zero or undefined value.";
3275 else if (GraphPrintCheckerState->isUndefResult(N))
3276 Out << "\\|Result of operation is undefined.";
3277 else if (GraphPrintCheckerState->isNoReturnCall(N))
3278 Out << "\\|Call to function marked \"noreturn\".";
3279 else if (GraphPrintCheckerState->isBadCall(N))
3280 Out << "\\|Call to NULL/Undefined.";
3281 else if (GraphPrintCheckerState->isUndefArg(N))
3282 Out << "\\|Argument in call is undefined";
3283
3284 break;
3285 }
3286
Ted Kremenekaa66a322008-01-16 21:46:15 +00003287 const BlockEdge& E = cast<BlockEdge>(Loc);
3288 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3289 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003290
3291 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003292
3293 SourceLocation SLoc = T->getLocStart();
3294
Ted Kremenekb38911f2008-01-30 23:03:39 +00003295 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003296
Ted Kremeneka95d3752008-09-13 05:16:45 +00003297 llvm::raw_os_ostream OutS(Out);
3298 E.getSrc()->printTerminator(OutS);
3299 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003300
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003301 if (SLoc.isFileID()) {
3302 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003303 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3304 << " col="
3305 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003306 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003307
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003308 if (isa<SwitchStmt>(T)) {
3309 Stmt* Label = E.getDst()->getLabel();
3310
3311 if (Label) {
3312 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3313 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003314 llvm::raw_os_ostream OutS(Out);
3315 C->getLHS()->printPretty(OutS);
3316 OutS.flush();
3317
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003318 if (Stmt* RHS = C->getRHS()) {
3319 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003320 RHS->printPretty(OutS);
3321 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003322 }
3323
3324 Out << ":";
3325 }
3326 else {
3327 assert (isa<DefaultStmt>(Label));
3328 Out << "\\ldefault:";
3329 }
3330 }
3331 else
3332 Out << "\\l(implicit) default:";
3333 }
3334 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003335 // FIXME
3336 }
3337 else {
3338 Out << "\\lCondition: ";
3339 if (*E.getSrc()->succ_begin() == E.getDst())
3340 Out << "true";
3341 else
3342 Out << "false";
3343 }
3344
3345 Out << "\\l";
3346 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003347
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003348 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3349 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003350 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003351 }
3352 }
3353
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003354 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003355
Ted Kremenekb65be702009-06-18 01:23:53 +00003356 const GRState *state = N->getState();
3357 state->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003358
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003359 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003360 return Out.str();
3361 }
3362};
3363} // end llvm namespace
3364#endif
3365
Ted Kremenekffe0f432008-03-07 22:58:01 +00003366#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003367template <typename ITERATOR>
3368GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3369
3370template <>
3371GRExprEngine::NodeTy*
3372GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3373 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3374 return I->first;
3375}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003376#endif
3377
3378void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003379#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003380 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003381 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003382
3383 // Flush any outstanding reports to make sure we cover all the nodes.
3384 // This does not cause them to get displayed.
3385 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3386 const_cast<BugType*>(*I)->FlushReports(BR);
3387
3388 // Iterate through the reports and get their nodes.
3389 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3390 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3391 const BugReportEquivClass& EQ = *I2;
3392 const BugReport &R = **EQ.begin();
3393 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3394 if (N) Src.push_back(N);
3395 }
3396 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003397
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003398 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003399 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003400 else {
3401 GraphPrintCheckerState = this;
3402 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003403
Ted Kremenekffe0f432008-03-07 22:58:01 +00003404 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003405
3406 GraphPrintCheckerState = NULL;
3407 GraphPrintSourceManager = NULL;
3408 }
3409#endif
3410}
3411
3412void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3413#ifndef NDEBUG
3414 GraphPrintCheckerState = this;
3415 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003416
Ted Kremenekcf118d42009-02-04 23:49:09 +00003417 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003418
Ted Kremenekcf118d42009-02-04 23:49:09 +00003419 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003420 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003421 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003422 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003423
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003424 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003425 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003426#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003427}