blob: f0e93de2193714ee3bd6150cc1a454e725cc9ce4 [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"
18
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000021#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000022#include "llvm/ADT/ImmutableList.h"
23#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000024#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000025
Ted Kremenek0f5f0592008-02-27 06:07:00 +000026#ifndef NDEBUG
27#include "llvm/Support/GraphWriter.h"
28#include <sstream>
29#endif
30
Ted Kremenekb387a3f2008-02-14 22:16:04 +000031using namespace clang;
32using llvm::dyn_cast;
33using llvm::cast;
34using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000035
Ted Kremeneke695e1c2008-04-15 23:06:53 +000036//===----------------------------------------------------------------------===//
37// Engine construction and deletion.
38//===----------------------------------------------------------------------===//
39
Ted Kremenekbdb435d2008-07-11 18:37:32 +000040namespace {
41
42class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
43 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
44 typedef llvm::DenseMap<void*,Checks> MapTy;
45
46 MapTy M;
47 Checks::Factory F;
48
49public:
50 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
51
52 virtual ~MappedBatchAuditor() {
53 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
54
55 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
56 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
57
58 GRSimpleAPICheck* check = *I;
59
60 if (AlreadyVisited.count(check))
61 continue;
62
63 AlreadyVisited.insert(check);
64 delete check;
65 }
66 }
67
68 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
69 assert (A && "Check cannot be null.");
70 void* key = reinterpret_cast<void*>((uintptr_t) C);
71 MapTy::iterator I = M.find(key);
72 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
73 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000074
Ted Kremenek4adc81e2008-08-13 04:27:00 +000075 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000076 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
77 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
78 MapTy::iterator MI = M.find(key);
79
80 if (MI == M.end())
81 return false;
82
83 bool isSink = false;
84
85 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000086 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +000087
88 return isSink;
89 }
90};
91
92} // end anonymous namespace
93
94//===----------------------------------------------------------------------===//
95// Engine construction and deletion.
96//===----------------------------------------------------------------------===//
97
Ted Kremeneke448ab42008-05-01 18:33:28 +000098static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
99 IdentifierInfo* II = &Ctx.Idents.get(name);
100 return Ctx.Selectors.getSelector(0, &II);
101}
102
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000103
Ted Kremenek8b233612008-07-02 20:13:38 +0000104GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000105 LiveVariables& L, BugReporterData& BRD,
106 bool purgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000107 StoreManagerCreator SMC,
108 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000109 : CoreEngine(cfg, CD, Ctx, *this),
110 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000111 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000112 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000113 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000114 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000115 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000116 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
117 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000118 PurgeDead(purgeDead),
119 BR(BRD, *this) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000120
Ted Kremenek1a654b62008-06-20 21:45:25 +0000121GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000122 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000123 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124}
125
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000126//===----------------------------------------------------------------------===//
127// Utility methods.
128//===----------------------------------------------------------------------===//
129
Ted Kremenek186350f2008-04-23 20:12:28 +0000130
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000131void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000132 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000133 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000134 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000135}
136
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000137void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
138 if (!BatchAuditor)
139 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
140
141 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000142}
143
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000144const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000145 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000146}
147
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000148//===----------------------------------------------------------------------===//
149// Top-level transfer function logic (Dispatcher).
150//===----------------------------------------------------------------------===//
151
152void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
153
154 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000155 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000156
157 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000158 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000159 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000160
161 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000162 if (BatchAuditor)
163 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000164
Ted Kremenek241677a2009-01-21 22:26:05 +0000165
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000166 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000167 SymbolReaper SymReaper(Liveness, SymMgr);
168 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
169 CurrentStmt, SymReaper)
170 : EntryNode->getState();
171
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000172 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000173 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000174
Ted Kremenek241677a2009-01-21 22:26:05 +0000175 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000176 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000177 else {
178 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000179 SaveOr OldHasGen(Builder->HasGeneratedNode);
180
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000181 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
182 Builder->PurgingDeadSymbols = true;
183
Ted Kremenek729a9a22008-07-17 23:15:45 +0000184 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000185 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000186
187 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
188 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000189 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000190
191 bool HasAutoGenerated = false;
192
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000193 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000194
195 NodeSet Dst;
196
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000197 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000198 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
199
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000200 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000201 Visit(S, *I, Dst);
202
203 // Do we need to auto-generate a node? We only need to do this to generate
204 // a node with a "cleaned" state; GRCoreEngine will actually handle
205 // auto-transitions for other cases.
206 if (Dst.size() == 1 && *Dst.begin() == EntryNode
207 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
208 HasAutoGenerated = true;
209 builder.generateNode(S, GetState(EntryNode), *I);
210 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000211 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000212
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000213 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000214 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000215 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000216
217 // FIXME: Consolidate.
218 StateMgr.CurrentStmt = 0;
219 CurrentStmt = 0;
220
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000222}
223
224void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
225
226 // FIXME: add metadata to the CFG so that we can disable
227 // this check when we KNOW that there is no block-level subexpression.
228 // The motivation is that this check requires a hashtable lookup.
229
230 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
231 Dst.Add(Pred);
232 return;
233 }
234
235 switch (S->getStmtClass()) {
236
237 default:
238 // Cases we intentionally have "default" handle:
239 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
240
241 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
242 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000243
244 case Stmt::ArraySubscriptExprClass:
245 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
246 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000247
248 case Stmt::AsmStmtClass:
249 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
250 break;
251
252 case Stmt::BinaryOperatorClass: {
253 BinaryOperator* B = cast<BinaryOperator>(S);
254
255 if (B->isLogicalOp()) {
256 VisitLogicalExpr(B, Pred, Dst);
257 break;
258 }
259 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000260 const GRState* state = GetState(Pred);
261 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262 break;
263 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000264
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000265 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
266 break;
267 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000268
Douglas Gregorb4609802008-11-14 16:09:21 +0000269 case Stmt::CallExprClass:
270 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000271 CallExpr* C = cast<CallExpr>(S);
272 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000273 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000274 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000275
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000276 // FIXME: ChooseExpr is really a constant. We need to fix
277 // the CFG do not model them as explicit control-flow.
278
279 case Stmt::ChooseExprClass: { // __builtin_choose_expr
280 ChooseExpr* C = cast<ChooseExpr>(S);
281 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
282 break;
283 }
284
285 case Stmt::CompoundAssignOperatorClass:
286 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
287 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000288
289 case Stmt::CompoundLiteralExprClass:
290 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
291 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000292
293 case Stmt::ConditionalOperatorClass: { // '?' operator
294 ConditionalOperator* C = cast<ConditionalOperator>(S);
295 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
296 break;
297 }
298
299 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000300 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000301 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000302 break;
303
304 case Stmt::DeclStmtClass:
305 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
306 break;
307
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000308 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000309 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000310 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000311 VisitCast(C, C->getSubExpr(), Pred, Dst);
312 break;
313 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000314
315 case Stmt::InitListExprClass:
316 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
317 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000318
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000319 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000320 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
321 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000322
323 case Stmt::ObjCIvarRefExprClass:
324 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
325 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000326
327 case Stmt::ObjCForCollectionStmtClass:
328 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
329 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000330
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000331 case Stmt::ObjCMessageExprClass: {
332 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
333 break;
334 }
335
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000336 case Stmt::ObjCAtThrowStmtClass: {
337 // FIXME: This is not complete. We basically treat @throw as
338 // an abort.
339 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
340 Builder->BuildSinks = true;
341 MakeNode(Dst, S, Pred, GetState(Pred));
342 break;
343 }
344
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000345 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000346 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000347 break;
348
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000349 case Stmt::ReturnStmtClass:
350 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
351 break;
352
Sebastian Redl05189992008-11-11 17:56:53 +0000353 case Stmt::SizeOfAlignOfExprClass:
354 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000355 break;
356
357 case Stmt::StmtExprClass: {
358 StmtExpr* SE = cast<StmtExpr>(S);
359
Ted Kremeneka8538d92009-02-13 01:45:31 +0000360 const GRState* state = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000361
362 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
363 // probably just remove these from the CFG.
364 assert (!SE->getSubStmt()->body_empty());
365
366 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Ted Kremeneka8538d92009-02-13 01:45:31 +0000367 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000368 else
369 Dst.Add(Pred);
370
371 break;
372 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000373
374 case Stmt::StringLiteralClass:
375 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
376 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000377
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000378 case Stmt::UnaryOperatorClass:
379 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000380 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000381 }
382}
383
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000384void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000385
386 Ex = Ex->IgnoreParens();
387
388 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
389 Dst.Add(Pred);
390 return;
391 }
392
393 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000394
395 case Stmt::ArraySubscriptExprClass:
396 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
397 return;
398
399 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000400 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000401 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
402 return;
403
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000404 case Stmt::ObjCIvarRefExprClass:
405 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
406 return;
407
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000408 case Stmt::UnaryOperatorClass:
409 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
410 return;
411
412 case Stmt::MemberExprClass:
413 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
414 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000415
Ted Kremenek4f090272008-10-27 21:54:31 +0000416 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000417 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000418 return;
419
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000420 case Stmt::ObjCPropertyRefExprClass:
421 // FIXME: Property assignments are lvalues, but not really "locations".
422 // e.g.: self.x = something;
423 // Here the "self.x" really can translate to a method call (setter) when
424 // the assignment is made. Moreover, the entire assignment expression
425 // evaluate to whatever "something" is, not calling the "getter" for
426 // the property (which would make sense since it can have side effects).
427 // We'll probably treat this as a location, but not one that we can
428 // take the address of. Perhaps we need a new SVal class for cases
429 // like thsis?
430 // Note that we have a similar problem for bitfields, since they don't
431 // have "locations" in the sense that we can take their address.
432 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000433 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000434
435 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000436 const GRState* state = GetState(Pred);
437 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
438 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000439 return;
440 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000441
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000442 default:
443 // Arbitrary subexpressions can return aggregate temporaries that
444 // can be used in a lvalue context. We need to enhance our support
445 // of such temporaries in both the environment and the store, so right
446 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000447 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000448 "Other kinds of expressions with non-aggregate/union types do"
449 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000450
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000451 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000452 }
453}
454
455//===----------------------------------------------------------------------===//
456// Block entrance. (Update counters).
457//===----------------------------------------------------------------------===//
458
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000459bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000460 GRBlockCounter BC) {
461
462 return BC.getNumVisited(B->getBlockID()) < 3;
463}
464
465//===----------------------------------------------------------------------===//
466// Branch processing.
467//===----------------------------------------------------------------------===//
468
Ted Kremeneka8538d92009-02-13 01:45:31 +0000469const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000470 Stmt* Terminator,
471 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000472
473 switch (Terminator->getStmtClass()) {
474 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000475 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000476
477 case Stmt::BinaryOperatorClass: { // '&&' and '||'
478
479 BinaryOperator* B = cast<BinaryOperator>(Terminator);
480 BinaryOperator::Opcode Op = B->getOpcode();
481
482 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
483
484 // For &&, if we take the true branch, then the value of the whole
485 // expression is that of the RHS expression.
486 //
487 // For ||, if we take the false branch, then the value of the whole
488 // expression is that of the RHS expression.
489
490 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
491 (Op == BinaryOperator::LOr && !branchTaken)
492 ? B->getRHS() : B->getLHS();
493
Ted Kremeneka8538d92009-02-13 01:45:31 +0000494 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000495 }
496
497 case Stmt::ConditionalOperatorClass: { // ?:
498
499 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
500
501 // For ?, if branchTaken == true then the value is either the LHS or
502 // the condition itself. (GNU extension).
503
504 Expr* Ex;
505
506 if (branchTaken)
507 Ex = C->getLHS() ? C->getLHS() : C->getCond();
508 else
509 Ex = C->getRHS();
510
Ted Kremeneka8538d92009-02-13 01:45:31 +0000511 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000512 }
513
514 case Stmt::ChooseExprClass: { // ?:
515
516 ChooseExpr* C = cast<ChooseExpr>(Terminator);
517
518 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000519 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000520 }
521 }
522}
523
Ted Kremenekaf337412008-11-12 19:24:17 +0000524void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000525 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000526
Ted Kremeneke7d22112008-02-11 19:21:59 +0000527 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000528 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000529 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000530
Ted Kremenekb2331832008-02-15 22:29:00 +0000531 // Check for NULL conditions; e.g. "for(;;)"
532 if (!Condition) {
533 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000534 return;
535 }
536
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000537 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000538
539 switch (V.getBaseKind()) {
540 default:
541 break;
542
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000543 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000544 builder.generateNode(MarkBranch(PrevState, Term, true), true);
545 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000546 return;
547
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000548 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000549 NodeTy* N = builder.generateNode(PrevState, true);
550
551 if (N) {
552 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000553 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000554 }
555
556 builder.markInfeasible(false);
557 return;
558 }
559 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000560
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000561 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000562
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000563 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000564 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000565
566 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000567 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000568 else
569 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000570
571 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000572
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000573 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000574 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000575
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000576 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000577 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000578 else
579 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000580}
581
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000582/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000583/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000584void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000585
Ted Kremeneka8538d92009-02-13 01:45:31 +0000586 const GRState* state = builder.getState();
587 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000588
589 // Three possibilities:
590 //
591 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000592 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000593 // (3) We have no clue about the label. Dispatch to all targets.
594 //
595
596 typedef IndirectGotoNodeBuilder::iterator iterator;
597
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000598 if (isa<loc::GotoLabel>(V)) {
599 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000600
601 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000602 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000603 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000604 return;
605 }
606 }
607
608 assert (false && "No block with label.");
609 return;
610 }
611
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000612 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000613 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000614 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000615 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000616 return;
617 }
618
619 // This is really a catch-all. We don't support symbolics yet.
620
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000621 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000622
623 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000624 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000625}
Ted Kremenekf233d482008-02-05 00:26:40 +0000626
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000627
628void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
629 NodeTy* Pred, NodeSet& Dst) {
630
631 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
632
Ted Kremeneka8538d92009-02-13 01:45:31 +0000633 const GRState* state = GetState(Pred);
634 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000635
636 assert (X.isUndef());
637
638 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
639
640 assert (SE);
641
Ted Kremeneka8538d92009-02-13 01:45:31 +0000642 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000643
644 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000645 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000646}
647
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000648/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
649/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000650void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
651 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000652 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000653 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000654 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000655
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000656 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000657 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000658 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000659 return;
660 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000661
Ted Kremeneka8538d92009-02-13 01:45:31 +0000662 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000663 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000664
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000665 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000666 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000667
668 // Evaluate the LHS of the case value.
669 Expr::EvalResult V1;
670 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000671
Ted Kremenek72afb372009-01-17 01:54:16 +0000672 // Sanity checks. These go away in Release builds.
673 assert(b && V1.Val.isInt() && !V1.HasSideEffects
674 && "Case condition must evaluate to an integer constant.");
675 b = b; // silence unused variable warning
676 assert(V1.Val.getInt().getBitWidth() ==
677 getContext().getTypeSize(CondE->getType()));
678
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000680 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000681
682 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000683 b = E->Evaluate(V2, getContext());
684 assert(b && V2.Val.isInt() && !V2.HasSideEffects
685 && "Case condition must evaluate to an integer constant.");
686 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000687 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000688 else
689 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000690
691 // FIXME: Eventually we should replace the logic below with a range
692 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000693 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000694
Ted Kremenek14a11402008-03-17 22:17:56 +0000695 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000696 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000697 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000698
Ted Kremenek72afb372009-01-17 01:54:16 +0000699 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000700 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000701 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000702
703 if (isFeasible) {
704 builder.generateCaseStmtNode(I, StNew);
705
706 // If CondV evaluates to a constant, then we know that this
707 // is the *only* case that we can take, so stop evaluating the
708 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000709 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000710 return;
711 }
712
713 // Now "assume" that the case doesn't match. Add this state
714 // to the default state (if it is feasible).
715
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000716 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000717 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000718
Ted Kremenek5014ab12008-04-23 05:03:18 +0000719 if (isFeasible) {
720 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000721 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000722 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000723
Ted Kremenek14a11402008-03-17 22:17:56 +0000724 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000725 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000726 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
Ted Kremenek72afb372009-01-17 01:54:16 +0000728 ++V1.Val.getInt();
729 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000730
731 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000732 }
733
734 // If we reach here, than we know that the default branch is
735 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000736 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000737}
738
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000739//===----------------------------------------------------------------------===//
740// Transfer functions: logical operations ('&&', '||').
741//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000742
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000743void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000744 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000745
Ted Kremenek05a23782008-02-26 19:05:15 +0000746 assert (B->getOpcode() == BinaryOperator::LAnd ||
747 B->getOpcode() == BinaryOperator::LOr);
748
749 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
750
Ted Kremeneka8538d92009-02-13 01:45:31 +0000751 const GRState* state = GetState(Pred);
752 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000753
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000754 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000755
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000756 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000757
758 assert (Ex);
759
760 if (Ex == B->getRHS()) {
761
Ted Kremeneka8538d92009-02-13 01:45:31 +0000762 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000763
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000764 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000765
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000766 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000767 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000768 return;
769 }
770
Ted Kremenek05a23782008-02-26 19:05:15 +0000771 // We took the RHS. Because the value of the '&&' or '||' expression must
772 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
773 // or 1. Alternatively, we could take a lazy approach, and calculate this
774 // value later when necessary. We don't have the machinery in place for
775 // this right now, and since most logical expressions are used for branches,
776 // the payoff is not likely to be large. Instead, we do eager evaluation.
777
778 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000779 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000780
781 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000782 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000783 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000784
785 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000786 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000787
788 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000789 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000790 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000791 }
792 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000793 // We took the LHS expression. Depending on whether we are '&&' or
794 // '||' we know what the value of the expression is via properties of
795 // the short-circuiting.
796
797 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000798 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000799 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000800}
Ted Kremenek05a23782008-02-26 19:05:15 +0000801
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000802//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000803// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000804//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000805
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000806void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
807 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000808
Ted Kremeneka8538d92009-02-13 01:45:31 +0000809 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000810
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000811 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000812
813 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
814
Ted Kremeneka8538d92009-02-13 01:45:31 +0000815 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000816
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000817 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000818 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000819 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000820 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000821 return;
822
823 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
824 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
825
826 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000827 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000828 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000829 return;
830
831 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000832 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000833 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000834 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000835 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000836 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000837
838 assert (false &&
839 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000840}
841
Ted Kremenek540cbe22008-04-22 04:56:29 +0000842/// VisitArraySubscriptExpr - Transfer function for array accesses
843void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000844 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000845
846 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000847 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000848 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000849 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000850
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000851 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000852 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000853 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000854
855 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000856 const GRState* state = GetState(*I2);
857 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
858 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000859
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000860 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000861 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000862 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000863 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000864 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000865 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000866}
867
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000868/// VisitMemberExpr - Transfer function for member expressions.
869void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000870 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000871
872 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000873 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000874
875 if (M->isArrow())
876 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
877 else
878 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
879
Douglas Gregor86f19402008-12-20 23:49:58 +0000880 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
881 if (!Field) // FIXME: skipping member expressions for non-fields
882 return;
883
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000884 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000885 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000886 // FIXME: Should we insert some assumption logic in here to determine
887 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000888 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000889 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000890
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000891 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000892 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000893 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000894 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000895 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000896}
897
Ted Kremeneka8538d92009-02-13 01:45:31 +0000898/// EvalBind - Handle the semantics of binding a value to a specific location.
899/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
900void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
901 const GRState* state, SVal location, SVal Val) {
902
Ted Kremenek41573eb2009-02-14 01:43:44 +0000903 const GRState* newState = 0;
904
905 if (location.isUnknown()) {
906 // We know that the new state will be the same as the old state since
907 // the location of the binding is "unknown". Consequently, there
908 // is no reason to just create a new node.
909 newState = state;
910 }
911 else {
912 // We are binding to a value other than 'unknown'. Perform the binding
913 // using the StoreManager.
914 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
915 }
Ted Kremeneka8538d92009-02-13 01:45:31 +0000916
Ted Kremenek41573eb2009-02-14 01:43:44 +0000917 // The next thing to do is check if the GRTransferFuncs object wants to
918 // update the state based on the new binding. If the GRTransferFunc object
919 // doesn't do anything, just auto-propagate the current state.
920 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
921 newState != state);
922
923 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000924}
925
926/// EvalStore - Handle the semantics of a store via an assignment.
927/// @param Dst The node set to store generated state nodes
928/// @param Ex The expression representing the location of the store
929/// @param state The current simulation state
930/// @param location The location to store the value
931/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000932void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000933 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000934
935 assert (Builder && "GRStmtNodeBuilder must be defined.");
936
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000937 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000938 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000939
Ted Kremenek8c354752008-12-16 22:02:27 +0000940 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000941 return;
Ted Kremenekb0533962008-04-18 20:35:30 +0000942
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000943 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +0000944 state = GetState(Pred);
945
946 // Proceed with the store.
947 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
948 Builder->PointKind = ProgramPoint::PostStoreKind;
949 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000950}
951
952void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000953 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000954
Ted Kremenek8c354752008-12-16 22:02:27 +0000955 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000956 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000957
Ted Kremenek8c354752008-12-16 22:02:27 +0000958 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000959 return;
960
Ted Kremeneka8538d92009-02-13 01:45:31 +0000961 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +0000962
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000963 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000964 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000965
966 // FIXME: Currently symbolic analysis "generates" new symbols
967 // for the contents of values. We need a better approach.
968
Ted Kremenek8c354752008-12-16 22:02:27 +0000969 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +0000970 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000971 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000972 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000973 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000974 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
975 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000976 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000977}
978
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000979void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000980 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000981
982 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000983 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000984
985 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
986 MakeNode(Dst, Ex, *I, (*I)->getState());
987}
988
Ted Kremenek8c354752008-12-16 22:02:27 +0000989GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000990 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +0000991 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000992
993 // Check for loads/stores from/to undefined values.
994 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +0000995 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +0000996 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +0000997 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000998
Ted Kremenek8c354752008-12-16 22:02:27 +0000999 if (N) {
1000 N->markAsSink();
1001 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001002 }
1003
Ted Kremenek8c354752008-12-16 22:02:27 +00001004 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001005 }
1006
1007 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1008 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001009 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001010
1011 // During a load, one of two possible situations arise:
1012 // (1) A crash, because the location (pointer) was NULL.
1013 // (2) The location (pointer) is not NULL, and the dereference works.
1014 //
1015 // We add these assumptions.
1016
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001017 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001018
1019 // "Assume" that the pointer is not NULL.
1020
1021 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001022 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023
1024 // "Assume" that the pointer is NULL.
1025
1026 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001027 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001028 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001029
1030 if (isFeasibleNull) {
1031
Ted Kremenek7360fda2008-09-18 23:09:54 +00001032 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001033 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001034 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1035
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001036 // We don't use "MakeNode" here because the node will be a sink
1037 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001038 NodeTy* NullNode =
1039 Builder->generateNode(Ex, StNull, Pred,
1040 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001041
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001042 if (NullNode) {
1043
1044 NullNode->markAsSink();
1045
1046 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1047 else ExplicitNullDeref.insert(NullNode);
1048 }
1049 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001050
1051 if (!isFeasibleNotNull)
1052 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001053
1054 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001055 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001056 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1057 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1058 // Get the index of the accessed element.
1059 SVal Idx = ER->getIndex();
1060 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001061 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1062 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001063
1064 bool isFeasibleInBound = false;
1065 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1066 true, isFeasibleInBound);
1067
1068 bool isFeasibleOutBound = false;
1069 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1070 false, isFeasibleOutBound);
1071
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001072 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001073 // Report warning. Make sink node manually.
1074 NodeTy* OOBNode =
1075 Builder->generateNode(Ex, StOutBound, Pred,
1076 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001077
1078 if (OOBNode) {
1079 OOBNode->markAsSink();
1080
1081 if (isFeasibleInBound)
1082 ImplicitOOBMemAccesses.insert(OOBNode);
1083 else
1084 ExplicitOOBMemAccesses.insert(OOBNode);
1085 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001086 }
1087
Ted Kremenek8c354752008-12-16 22:02:27 +00001088 if (!isFeasibleInBound)
1089 return 0;
1090
1091 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001092 }
1093 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001094
Ted Kremenek8c354752008-12-16 22:02:27 +00001095 // Generate a new node indicating the checks succeed.
1096 return Builder->generateNode(Ex, StNotNull, Pred,
1097 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001098}
1099
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001100//===----------------------------------------------------------------------===//
1101// Transfer function: Function calls.
1102//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001103void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001104 CallExpr::arg_iterator AI,
1105 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001106 NodeSet& Dst)
1107{
1108 // Determine the type of function we're calling (if available).
1109 const FunctionTypeProto *Proto = NULL;
1110 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1111 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1112 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1113
1114 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1115}
1116
1117void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1118 CallExpr::arg_iterator AI,
1119 CallExpr::arg_iterator AE,
1120 NodeSet& Dst, const FunctionTypeProto *Proto,
1121 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001122
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001123 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001124 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001125 // If the call argument is being bound to a reference parameter,
1126 // visit it as an lvalue, not an rvalue.
1127 bool VisitAsLvalue = false;
1128 if (Proto && ParamIdx < Proto->getNumArgs())
1129 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1130
1131 NodeSet DstTmp;
1132 if (VisitAsLvalue)
1133 VisitLValue(*AI, Pred, DstTmp);
1134 else
1135 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001136 ++AI;
1137
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001138 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001139 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001140
1141 return;
1142 }
1143
1144 // If we reach here we have processed all of the arguments. Evaluate
1145 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001146
Ted Kremenek994a09b2008-02-25 21:16:03 +00001147 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001148 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001149
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001150 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001151
Ted Kremenekde434242008-02-19 01:44:53 +00001152 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001153 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1154
Ted Kremeneka8538d92009-02-13 01:45:31 +00001155 const GRState* state = GetState(*DI);
1156 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001157
Ted Kremeneka1354a52008-03-03 16:47:31 +00001158 // FIXME: Add support for symbolic function calls (calls involving
1159 // function pointer values that are symbolic).
1160
1161 // Check for undefined control-flow or calls to NULL.
1162
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001163 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001164 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001165
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001166 if (N) {
1167 N->markAsSink();
1168 BadCalls.insert(N);
1169 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001170
Ted Kremenekde434242008-02-19 01:44:53 +00001171 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001172 }
1173
1174 // Check for the "noreturn" attribute.
1175
1176 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1177
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001178 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001179
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001180 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001181
1182 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001183 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001184 else {
1185 // HACK: Some functions are not marked noreturn, and don't return.
1186 // Here are a few hardwired ones. If this takes too long, we can
1187 // potentially cache these results.
1188 const char* s = FD->getIdentifier()->getName();
1189 unsigned n = strlen(s);
1190
1191 switch (n) {
1192 default:
1193 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001194
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001195 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001196 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1197 break;
1198
1199 case 5:
1200 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001201 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001202 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001203 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001204 // FIXME: use Assume to inspect the possible symbolic value of
1205 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001206 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001207 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001208 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001209 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001210 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001211 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001212
1213 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001214 if (!memcmp(s, "Assert", 6)) {
1215 Builder->BuildSinks = true;
1216 break;
1217 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001218
1219 // FIXME: This is just a wrapper around throwing an exception.
1220 // Eventually inter-procedural analysis should handle this easily.
1221 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1222
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001223 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001224
1225 case 7:
1226 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1227 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001228
Ted Kremenekf47bb782008-04-30 17:54:04 +00001229 case 8:
1230 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1231 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001232
1233 case 12:
1234 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1235 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001236
Ted Kremenekf9683082008-09-19 02:30:47 +00001237 case 13:
1238 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1239 break;
1240
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001241 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001242 if (!memcmp(s, "dtrace_assfail", 14) ||
1243 !memcmp(s, "yy_fatal_error", 14))
1244 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001245 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001246
1247 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001248 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1249 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001250 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001251
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001252 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001253 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001254
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001255 }
1256 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001257
1258 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001259
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001260 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001261
Douglas Gregor3e41d602009-02-13 23:20:09 +00001262 if (unsigned id = cast<loc::FuncVal>(L).getDecl()->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001263 switch (id) {
1264 case Builtin::BI__builtin_expect: {
1265 // For __builtin_expect, just return the value of the subexpression.
1266 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001267 SVal X = GetSVal(state, *(CE->arg_begin()));
1268 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001269 continue;
1270 }
1271
Ted Kremenekb3021332008-11-02 00:35:01 +00001272 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001273 // FIXME: Refactor into StoreManager itself?
1274 MemRegionManager& RM = getStateManager().getRegionManager();
1275 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001276 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001277
1278 // Set the extent of the region in bytes. This enables us to use the
1279 // SVal of the argument directly. If we save the extent in bits, we
1280 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001281 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1282 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001283
Ted Kremeneka8538d92009-02-13 01:45:31 +00001284 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001285 continue;
1286 }
1287
Ted Kremenek55aea312008-03-05 22:59:42 +00001288 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001289 break;
1290 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001291 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001292
Ted Kremenek186350f2008-04-23 20:12:28 +00001293 // Check any arguments passed-by-value against being undefined.
1294
1295 bool badArg = false;
1296
1297 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1298 I != E; ++I) {
1299
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001300 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001301 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001302
Ted Kremenek186350f2008-04-23 20:12:28 +00001303 if (N) {
1304 N->markAsSink();
1305 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001306 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001307
Ted Kremenek186350f2008-04-23 20:12:28 +00001308 badArg = true;
1309 break;
1310 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001311 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001312
1313 if (badArg)
1314 continue;
1315
1316 // Dispatch to the plug-in transfer function.
1317
1318 unsigned size = Dst.size();
1319 SaveOr OldHasGen(Builder->HasGeneratedNode);
1320 EvalCall(Dst, CE, L, *DI);
1321
1322 // Handle the case where no nodes where generated. Auto-generate that
1323 // contains the updated state if we aren't generating sinks.
1324
1325 if (!Builder->BuildSinks && Dst.size() == size &&
1326 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001327 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001328 }
1329}
1330
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001331//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001332// Transfer function: Objective-C ivar references.
1333//===----------------------------------------------------------------------===//
1334
1335void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1336 NodeTy* Pred, NodeSet& Dst,
1337 bool asLValue) {
1338
1339 Expr* Base = cast<Expr>(Ex->getBase());
1340 NodeSet Tmp;
1341 Visit(Base, Pred, Tmp);
1342
1343 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001344 const GRState* state = GetState(*I);
1345 SVal BaseVal = GetSVal(state, Base);
1346 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001347
1348 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001349 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001350 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001351 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001352 }
1353}
1354
1355//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001356// Transfer function: Objective-C fast enumeration 'for' statements.
1357//===----------------------------------------------------------------------===//
1358
1359void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1360 NodeTy* Pred, NodeSet& Dst) {
1361
1362 // ObjCForCollectionStmts are processed in two places. This method
1363 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1364 // statements within a basic block. This transfer function does two things:
1365 //
1366 // (1) binds the next container value to 'element'. This creates a new
1367 // node in the ExplodedGraph.
1368 //
1369 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1370 // whether or not the container has any more elements. This value
1371 // will be tested in ProcessBranch. We need to explicitly bind
1372 // this value because a container can contain nil elements.
1373 //
1374 // FIXME: Eventually this logic should actually do dispatches to
1375 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1376 // This will require simulating a temporary NSFastEnumerationState, either
1377 // through an SVal or through the use of MemRegions. This value can
1378 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1379 // terminates we reclaim the temporary (it goes out of scope) and we
1380 // we can test if the SVal is 0 or if the MemRegion is null (depending
1381 // on what approach we take).
1382 //
1383 // For now: simulate (1) by assigning either a symbol or nil if the
1384 // container is empty. Thus this transfer function will by default
1385 // result in state splitting.
1386
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001387 Stmt* elem = S->getElement();
1388 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001389
1390 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001391 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001392 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001393 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1394 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1395 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001396 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001397
1398 NodeSet Tmp;
1399 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001400
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001401 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1402 const GRState* state = GetState(*I);
1403 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1404 }
1405}
1406
1407void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1408 NodeTy* Pred, NodeSet& Dst,
1409 SVal ElementV) {
1410
1411
Ted Kremenekaf337412008-11-12 19:24:17 +00001412
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001413 // Get the current state. Use 'EvalLocation' to determine if it is a null
1414 // pointer, etc.
1415 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001416
Ted Kremenek8c354752008-12-16 22:02:27 +00001417 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1418 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001419 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001420
1421 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001422
Ted Kremenekaf337412008-11-12 19:24:17 +00001423 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001424 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001425 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1426 GRStateRef hasElems = state.BindExpr(S, TrueV);
1427
Ted Kremenekaf337412008-11-12 19:24:17 +00001428 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001429 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1430 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001431
1432 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1433 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1434 // FIXME: The proper thing to do is to really iterate over the
1435 // container. We will do this with dispatch logic to the store.
1436 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001437 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001438 assert (Loc::IsLocType(T));
1439 unsigned Count = Builder->getCurrentBlockCount();
1440 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1441 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001442
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001443 // Bind the location to 'nil' on the false branch.
1444 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1445 noElems = noElems.BindLoc(ElementV, nilV);
1446 }
1447
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001448 // Create the new nodes.
1449 MakeNode(Dst, S, Pred, hasElems);
1450 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001451}
1452
1453//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001454// Transfer function: Objective-C message expressions.
1455//===----------------------------------------------------------------------===//
1456
1457void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1458 NodeSet& Dst){
1459
1460 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1461 Pred, Dst);
1462}
1463
1464void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001465 ObjCMessageExpr::arg_iterator AI,
1466 ObjCMessageExpr::arg_iterator AE,
1467 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001468 if (AI == AE) {
1469
1470 // Process the receiver.
1471
1472 if (Expr* Receiver = ME->getReceiver()) {
1473 NodeSet Tmp;
1474 Visit(Receiver, Pred, Tmp);
1475
1476 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1477 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1478
1479 return;
1480 }
1481
1482 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1483 return;
1484 }
1485
1486 NodeSet Tmp;
1487 Visit(*AI, Pred, Tmp);
1488
1489 ++AI;
1490
1491 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1492 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1493}
1494
1495void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1496 NodeTy* Pred,
1497 NodeSet& Dst) {
1498
1499 // FIXME: More logic for the processing the method call.
1500
Ted Kremeneka8538d92009-02-13 01:45:31 +00001501 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001502 bool RaisesException = false;
1503
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001504
1505 if (Expr* Receiver = ME->getReceiver()) {
1506
Ted Kremeneka8538d92009-02-13 01:45:31 +00001507 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001508
1509 // Check for undefined control-flow or calls to NULL.
1510
1511 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001512 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001513
1514 if (N) {
1515 N->markAsSink();
1516 UndefReceivers.insert(N);
1517 }
1518
1519 return;
1520 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001521
1522 // Check if the "raise" message was sent.
1523 if (ME->getSelector() == RaiseSel)
1524 RaisesException = true;
1525 }
1526 else {
1527
1528 IdentifierInfo* ClsName = ME->getClassName();
1529 Selector S = ME->getSelector();
1530
1531 // Check for special instance methods.
1532
1533 if (!NSExceptionII) {
1534 ASTContext& Ctx = getContext();
1535
1536 NSExceptionII = &Ctx.Idents.get("NSException");
1537 }
1538
1539 if (ClsName == NSExceptionII) {
1540
1541 enum { NUM_RAISE_SELECTORS = 2 };
1542
1543 // Lazily create a cache of the selectors.
1544
1545 if (!NSExceptionInstanceRaiseSelectors) {
1546
1547 ASTContext& Ctx = getContext();
1548
1549 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1550
1551 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1552 unsigned idx = 0;
1553
1554 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001555 II.push_back(&Ctx.Idents.get("raise"));
1556 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001557 NSExceptionInstanceRaiseSelectors[idx++] =
1558 Ctx.Selectors.getSelector(II.size(), &II[0]);
1559
1560 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001561 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001562 NSExceptionInstanceRaiseSelectors[idx++] =
1563 Ctx.Selectors.getSelector(II.size(), &II[0]);
1564 }
1565
1566 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1567 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1568 RaisesException = true; break;
1569 }
1570 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001571 }
1572
1573 // Check for any arguments that are uninitialized/undefined.
1574
1575 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1576 I != E; ++I) {
1577
Ted Kremeneka8538d92009-02-13 01:45:31 +00001578 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001579
1580 // Generate an error node for passing an uninitialized/undefined value
1581 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001582 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001583
1584 if (N) {
1585 N->markAsSink();
1586 MsgExprUndefArgs[N] = *I;
1587 }
1588
1589 return;
1590 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001591 }
1592
1593 // Check if we raise an exception. For now treat these as sinks. Eventually
1594 // we will want to handle exceptions properly.
1595
1596 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1597
1598 if (RaisesException)
1599 Builder->BuildSinks = true;
1600
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001601 // Dispatch to plug-in transfer function.
1602
1603 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001604 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001605
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001606 EvalObjCMessageExpr(Dst, ME, Pred);
1607
1608 // Handle the case where no nodes where generated. Auto-generate that
1609 // contains the updated state if we aren't generating sinks.
1610
Ted Kremenekb0533962008-04-18 20:35:30 +00001611 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001612 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001613}
1614
1615//===----------------------------------------------------------------------===//
1616// Transfer functions: Miscellaneous statements.
1617//===----------------------------------------------------------------------===//
1618
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001619void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1620 QualType PtrTy,
1621 Expr* CastE, NodeTy* Pred,
1622 NodeSet& Dst) {
1623 if (!V.isUnknownOrUndef()) {
1624 // FIXME: Determine if the number of bits of the target type is
1625 // equal or exceeds the number of bits to store the pointer value.
1626 // If not, flag an error.
1627 unsigned bits = getContext().getTypeSize(PtrTy);
1628 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1629 }
1630
1631 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
1632}
1633
1634
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001635void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001636 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001637 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001638 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001639
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001640 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001641 T = ExCast->getTypeAsWritten();
1642
Zhongxing Xued340f72008-10-22 08:02:16 +00001643 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001644 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001645 else
1646 Visit(Ex, Pred, S1);
1647
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001648 // Check for casting to "void".
1649 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001650
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001651 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001652 Dst.Add(*I1);
1653
Ted Kremenek874d63f2008-01-24 02:02:54 +00001654 return;
1655 }
1656
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001657 // FIXME: The rest of this should probably just go into EvalCall, and
1658 // let the transfer function object be responsible for constructing
1659 // nodes.
1660
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001661 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001662 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001663 const GRState* state = GetState(N);
1664 SVal V = GetSVal(state, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001665
1666 // Unknown?
1667
1668 if (V.isUnknown()) {
1669 Dst.Add(N);
1670 continue;
1671 }
1672
1673 // Undefined?
1674
1675 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001676 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001677 continue;
1678 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001679
1680 // For const casts, just propagate the value.
1681 ASTContext& C = getContext();
1682
1683 if (C.getCanonicalType(T).getUnqualifiedType() ==
1684 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001685 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001686 continue;
1687 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001688
1689 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001690 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001691 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001692 continue;
1693 }
1694
1695 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001696 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1697 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001698 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001699 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001700 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001701 continue;
1702 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001703
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001704 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001705 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001706 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001707 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001708
1709 // Are we casting from an array to a pointer? If so just pass on
1710 // the decayed value.
1711 if (T->isPointerType()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001712 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001713 continue;
1714 }
1715
1716 // Are we casting from an array to an integer? If so, cast the decayed
1717 // pointer value to an integer.
1718 assert(T->isIntegerType());
1719 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1720 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001721 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001722 continue;
1723 }
1724
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001725 // Check for casts from a region to a specific type.
1726 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001727 assert(Loc::IsLocType(T));
1728 assert(Loc::IsLocType(ExTy));
1729
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001730 const MemRegion* R = RV->getRegion();
1731 StoreManager& StoreMgr = getStoreManager();
1732
1733 // Delegate to store manager to get the result of casting a region
1734 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001735 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001736
1737 // Inspect the result. If the MemRegion* returned is NULL, this
1738 // expression evaluates to UnknownVal.
1739 R = Res.getRegion();
1740 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1741
1742 // Generate the new node in the ExplodedGraph.
1743 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001744 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001745 }
1746
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001747 // All other cases.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001748 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
1749 EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001750 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001751}
1752
Ted Kremenek4f090272008-10-27 21:54:31 +00001753void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001754 NodeTy* Pred, NodeSet& Dst,
1755 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001756 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1757 NodeSet Tmp;
1758 Visit(ILE, Pred, Tmp);
1759
1760 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001761 const GRState* state = GetState(*I);
1762 SVal ILV = GetSVal(state, ILE);
1763 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001764
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001765 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001766 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001767 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001768 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001769 }
1770}
1771
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001772void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001773
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001774 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001775 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001776
1777 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001778 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001779
Ted Kremenekefd59942008-12-08 22:47:34 +00001780 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001781 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001782
1783 // FIXME: static variables may have an initializer, but the second
1784 // time a function is called those values may not be current.
1785 NodeSet Tmp;
1786
Ted Kremenekaf337412008-11-12 19:24:17 +00001787 if (InitEx)
1788 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001789
1790 if (Tmp.empty())
1791 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001792
1793 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001794 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001795 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001796
1797 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001798 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001799 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00001800 QualType T = VD->getType();
1801
1802 // Recover some path-sensitivity if a scalar value evaluated to
1803 // UnknownVal.
1804 if (InitVal.isUnknown()) {
1805 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001806 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001807 InitVal = loc::SymbolVal(Sym);
1808 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001809 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001810 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001811 InitVal = nonloc::SymbolVal(Sym);
1812 }
1813 }
1814
Ted Kremeneka8538d92009-02-13 01:45:31 +00001815 state = StateMgr.BindDecl(state, VD, InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001816 } else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001817 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001818
1819 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1820 QualType T = getContext().getCanonicalType(VD->getType());
1821 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1822 // FIXME: Handle multi-dimensional VLAs.
1823
1824 Expr* SE = VLA->getSizeExpr();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001825 SVal Size = GetSVal(state, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001826
1827 if (Size.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001828 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
Ted Kremenek159d2482008-12-09 00:44:16 +00001829 N->markAsSink();
1830 ExplicitBadSizedVLA.insert(N);
1831 }
1832 continue;
1833 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001834
1835 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001836 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
Ted Kremenekefd59942008-12-08 22:47:34 +00001837
1838 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001839 state = Assume(state, Size, true, isFeasibleNotZero);
Ted Kremenekefd59942008-12-08 22:47:34 +00001840
1841 if (isFeasibleZero) {
1842 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1843 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001844 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1845 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001846 }
1847 }
1848
1849 if (!isFeasibleNotZero)
1850 continue;
1851 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001852
Ted Kremeneka8538d92009-02-13 01:45:31 +00001853 MakeNode(Dst, DS, *I, state);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001854 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001855}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001856
Ted Kremenekf75b1862008-10-30 17:47:32 +00001857namespace {
1858 // This class is used by VisitInitListExpr as an item in a worklist
1859 // for processing the values contained in an InitListExpr.
1860class VISIBILITY_HIDDEN InitListWLItem {
1861public:
1862 llvm::ImmutableList<SVal> Vals;
1863 GRExprEngine::NodeTy* N;
1864 InitListExpr::reverse_iterator Itr;
1865
1866 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1867 InitListExpr::reverse_iterator itr)
1868 : Vals(vals), N(n), Itr(itr) {}
1869};
1870}
1871
1872
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001873void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1874 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001875
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001876 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001877 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001878 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001879
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001880 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001881
Ted Kremeneka49e3672008-10-30 23:14:36 +00001882 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001883
Ted Kremeneka49e3672008-10-30 23:14:36 +00001884 // Handle base case where the initializer has no elements.
1885 // e.g: static int* myArray[] = {};
1886 if (NumInitElements == 0) {
1887 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1888 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1889 return;
1890 }
1891
1892 // Create a worklist to process the initializers.
1893 llvm::SmallVector<InitListWLItem, 10> WorkList;
1894 WorkList.reserve(NumInitElements);
1895 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001896 InitListExpr::reverse_iterator ItrEnd = E->rend();
1897
Ted Kremeneka49e3672008-10-30 23:14:36 +00001898 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001899 while (!WorkList.empty()) {
1900 InitListWLItem X = WorkList.back();
1901 WorkList.pop_back();
1902
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001903 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001904 Visit(*X.Itr, X.N, Tmp);
1905
1906 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001907
Ted Kremenekf75b1862008-10-30 17:47:32 +00001908 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1909 // Get the last initializer value.
1910 state = GetState(*NI);
1911 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1912
1913 // Construct the new list of values by prepending the new value to
1914 // the already constructed list.
1915 llvm::ImmutableList<SVal> NewVals =
1916 getBasicVals().consVals(InitV, X.Vals);
1917
1918 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001919 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001920 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001921
Ted Kremenekf75b1862008-10-30 17:47:32 +00001922 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001923 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001924 }
1925 else {
1926 // Still some initializer values to go. Push them onto the worklist.
1927 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1928 }
1929 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001930 }
Ted Kremenek87903072008-10-30 18:34:31 +00001931
1932 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001933 }
1934
Ted Kremenek062e2f92008-11-13 06:10:40 +00001935 if (T->isUnionType() || T->isVectorType()) {
1936 // FIXME: to be implemented.
1937 // Note: That vectors can return true for T->isIntegerType()
1938 MakeNode(Dst, E, Pred, state);
1939 return;
1940 }
1941
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001942 if (Loc::IsLocType(T) || T->isIntegerType()) {
1943 assert (E->getNumInits() == 1);
1944 NodeSet Tmp;
1945 Expr* Init = E->getInit(0);
1946 Visit(Init, Pred, Tmp);
1947 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1948 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001949 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001950 }
1951 return;
1952 }
1953
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001954
1955 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1956 assert(0 && "unprocessed InitListExpr type");
1957}
Ted Kremenekf233d482008-02-05 00:26:40 +00001958
Sebastian Redl05189992008-11-11 17:56:53 +00001959/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1960void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1961 NodeTy* Pred,
1962 NodeSet& Dst) {
1963 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001964 uint64_t amt;
1965
1966 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001967 if (T == getContext().VoidTy) {
1968 // sizeof(void) == 1 byte.
1969 amt = 1;
1970 }
1971 else if (!T.getTypePtr()->isConstantSizeType()) {
1972 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00001973 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001974 }
1975 else if (T->isObjCInterfaceType()) {
1976 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1977 // the compiler has laid out its representation. Just report Unknown
1978 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00001979 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001980 }
1981 else {
1982 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00001983 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001984 }
Ted Kremenek87e80342008-03-15 03:13:20 +00001985 }
1986 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001987 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001988
Ted Kremenek0e561a32008-03-21 21:30:14 +00001989 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001990 BindExpr(GetState(Pred), Ex,
1991 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001992}
1993
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001994
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001995void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001996 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001997
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001998 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001999
2000 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002001 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002002
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002003 case UnaryOperator::Deref: {
2004
2005 Expr* Ex = U->getSubExpr()->IgnoreParens();
2006 NodeSet Tmp;
2007 Visit(Ex, Pred, Tmp);
2008
2009 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002010
Ted Kremeneka8538d92009-02-13 01:45:31 +00002011 const GRState* state = GetState(*I);
2012 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002013
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002014 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002015 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002016 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002017 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002018 }
2019
2020 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002021 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002022
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002023 case UnaryOperator::Real: {
2024
2025 Expr* Ex = U->getSubExpr()->IgnoreParens();
2026 NodeSet Tmp;
2027 Visit(Ex, Pred, Tmp);
2028
2029 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2030
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002031 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002032 if (Ex->getType()->isAnyComplexType()) {
2033 // Just report "Unknown."
2034 Dst.Add(*I);
2035 continue;
2036 }
2037
2038 // For all other types, UnaryOperator::Real is an identity operation.
2039 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002040 const GRState* state = GetState(*I);
2041 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002042 }
2043
2044 return;
2045 }
2046
2047 case UnaryOperator::Imag: {
2048
2049 Expr* Ex = U->getSubExpr()->IgnoreParens();
2050 NodeSet Tmp;
2051 Visit(Ex, Pred, Tmp);
2052
2053 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002054 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002055 if (Ex->getType()->isAnyComplexType()) {
2056 // Just report "Unknown."
2057 Dst.Add(*I);
2058 continue;
2059 }
2060
2061 // For all other types, UnaryOperator::Float returns 0.
2062 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002063 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002064 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002065 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002066 }
2067
2068 return;
2069 }
2070
2071 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002072 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002073 Dst.Add(Pred);
2074 return;
2075
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002076 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002077 case UnaryOperator::Extension: {
2078
2079 // Unary "+" is a no-op, similar to a parentheses. We still have places
2080 // where it may be a block-level expression, so we need to
2081 // generate an extra node that just propagates the value of the
2082 // subexpression.
2083
2084 Expr* Ex = U->getSubExpr()->IgnoreParens();
2085 NodeSet Tmp;
2086 Visit(Ex, Pred, Tmp);
2087
2088 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002089 const GRState* state = GetState(*I);
2090 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002091 }
2092
2093 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002094 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002095
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002096 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002097
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002098 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002099 Expr* Ex = U->getSubExpr()->IgnoreParens();
2100 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002101 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002102
2103 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002104 const GRState* state = GetState(*I);
2105 SVal V = GetSVal(state, Ex);
2106 state = BindExpr(state, U, V);
2107 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002108 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002109
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002110 return;
2111 }
2112
2113 case UnaryOperator::LNot:
2114 case UnaryOperator::Minus:
2115 case UnaryOperator::Not: {
2116
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002117 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002118 Expr* Ex = U->getSubExpr()->IgnoreParens();
2119 NodeSet Tmp;
2120 Visit(Ex, Pred, Tmp);
2121
2122 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002123 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002124
2125 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002126 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002127
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002128 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002129 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002130 continue;
2131 }
2132
Ted Kremenek60595da2008-11-15 04:01:56 +00002133// QualType DstT = getContext().getCanonicalType(U->getType());
2134// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2135//
2136// if (DstT != SrcT) // Perform promotions.
2137// V = EvalCast(V, DstT);
2138//
2139// if (V.isUnknownOrUndef()) {
2140// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2141// continue;
2142// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002143
2144 switch (U->getOpcode()) {
2145 default:
2146 assert(false && "Invalid Opcode.");
2147 break;
2148
2149 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002150 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002151 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002152 break;
2153
2154 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002155 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002156 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002157 break;
2158
2159 case UnaryOperator::LNot:
2160
2161 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2162 //
2163 // Note: technically we do "E == 0", but this is the same in the
2164 // transfer functions as "0 == E".
2165
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002166 if (isa<Loc>(V)) {
2167 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2168 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002169 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002170 }
2171 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002172 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002173#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002174 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002175 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002176#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002177 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002178 continue;
2179#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002180 }
2181
2182 break;
2183 }
2184
Ted Kremeneka8538d92009-02-13 01:45:31 +00002185 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002186 }
2187
2188 return;
2189 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002190 }
2191
2192 // Handle ++ and -- (both pre- and post-increment).
2193
2194 assert (U->isIncrementDecrementOp());
2195 NodeSet Tmp;
2196 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002197 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002198
2199 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2200
Ted Kremeneka8538d92009-02-13 01:45:31 +00002201 const GRState* state = GetState(*I);
2202 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002203
2204 // Perform a load.
2205 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002206 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002207
2208 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2209
Ted Kremeneka8538d92009-02-13 01:45:31 +00002210 state = GetState(*I2);
2211 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002212
2213 // Propagate unknown and undefined values.
2214 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002215 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002216 continue;
2217 }
2218
Ted Kremenek443003b2008-02-21 19:29:23 +00002219 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002220
2221 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2222 : BinaryOperator::Sub;
2223
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002224 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Ted Kremeneka8538d92009-02-13 01:45:31 +00002225 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002226
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002227 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002228 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002229 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002230 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002231}
2232
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002233void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2234 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2235}
2236
2237void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2238 AsmStmt::outputs_iterator I,
2239 AsmStmt::outputs_iterator E,
2240 NodeTy* Pred, NodeSet& Dst) {
2241 if (I == E) {
2242 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2243 return;
2244 }
2245
2246 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002247 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002248
2249 ++I;
2250
2251 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2252 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2253}
2254
2255void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2256 AsmStmt::inputs_iterator I,
2257 AsmStmt::inputs_iterator E,
2258 NodeTy* Pred, NodeSet& Dst) {
2259 if (I == E) {
2260
2261 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002262 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002263
2264 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2265 // which interprets the inline asm and stores proper results in the
2266 // outputs.
2267
Ted Kremeneka8538d92009-02-13 01:45:31 +00002268 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002269
2270 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2271 OE = A->end_outputs(); OI != OE; ++OI) {
2272
Ted Kremeneka8538d92009-02-13 01:45:31 +00002273 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002274 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002275
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002276 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002277 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002278 }
2279
Ted Kremeneka8538d92009-02-13 01:45:31 +00002280 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002281 return;
2282 }
2283
2284 NodeSet Tmp;
2285 Visit(*I, Pred, Tmp);
2286
2287 ++I;
2288
2289 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2290 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2291}
2292
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002293void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2294 assert (Builder && "GRStmtNodeBuilder must be defined.");
2295
2296 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002297
Ted Kremenek186350f2008-04-23 20:12:28 +00002298 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2299 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002300
Ted Kremenek729a9a22008-07-17 23:15:45 +00002301 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002302
Ted Kremenekb0533962008-04-18 20:35:30 +00002303 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002304
Ted Kremenekb0533962008-04-18 20:35:30 +00002305 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002306 MakeNode(Dst, S, Pred, GetState(Pred));
2307}
2308
Ted Kremenek02737ed2008-03-31 15:02:58 +00002309void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2310
2311 Expr* R = S->getRetValue();
2312
2313 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002314 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002315 return;
2316 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002317
Ted Kremenek5917d782008-11-21 00:27:44 +00002318 NodeSet Tmp;
2319 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002320
Ted Kremenek5917d782008-11-21 00:27:44 +00002321 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2322 SVal X = GetSVal((*I)->getState(), R);
2323
2324 // Check if we return the address of a stack variable.
2325 if (isa<loc::MemRegionVal>(X)) {
2326 // Determine if the value is on the stack.
2327 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002328
Ted Kremenek5917d782008-11-21 00:27:44 +00002329 if (R && getStateManager().hasStackStorage(R)) {
2330 // Create a special node representing the error.
2331 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2332 N->markAsSink();
2333 RetsStackAddr.insert(N);
2334 }
2335 continue;
2336 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002337 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002338 // Check if we return an undefined value.
2339 else if (X.isUndef()) {
2340 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2341 N->markAsSink();
2342 RetsUndef.insert(N);
2343 }
2344 continue;
2345 }
2346
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002347 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002348 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002349}
Ted Kremenek55deb972008-03-25 00:34:37 +00002350
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002351//===----------------------------------------------------------------------===//
2352// Transfer functions: Binary operators.
2353//===----------------------------------------------------------------------===//
2354
Ted Kremeneka8538d92009-02-13 01:45:31 +00002355const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002356 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002357
2358 // Divide by undefined? (potentially zero)
2359
2360 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002361 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002362
2363 if (DivUndef) {
2364 DivUndef->markAsSink();
2365 ExplicitBadDivides.insert(DivUndef);
2366 }
2367
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002368 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002369 }
2370
2371 // Check for divide/remainder-by-zero.
2372 // First, "assume" that the denominator is 0 or undefined.
2373
2374 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002375 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002376
2377 // Second, "assume" that the denominator cannot be 0.
2378
2379 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002380 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002381
2382 // Create the node for the divide-by-zero (if it occurred).
2383
2384 if (isFeasibleZero)
2385 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2386 DivZeroNode->markAsSink();
2387
2388 if (isFeasibleNotZero)
2389 ImplicitBadDivides.insert(DivZeroNode);
2390 else
2391 ExplicitBadDivides.insert(DivZeroNode);
2392
2393 }
2394
Ted Kremeneka8538d92009-02-13 01:45:31 +00002395 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002396}
2397
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002398void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002399 GRExprEngine::NodeTy* Pred,
2400 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002401
2402 NodeSet Tmp1;
2403 Expr* LHS = B->getLHS()->IgnoreParens();
2404 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002405
Ted Kremenek759623e2008-12-06 02:39:30 +00002406 // FIXME: Add proper support for ObjCKVCRefExpr.
2407 if (isa<ObjCKVCRefExpr>(LHS)) {
2408 Visit(RHS, Pred, Dst);
2409 return;
2410 }
2411
2412
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002413 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002414 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002415 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002416 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002417
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002418 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002419
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002420 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002421
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002422 // Process the RHS.
2423
2424 NodeSet Tmp2;
2425 Visit(RHS, *I1, Tmp2);
2426
2427 // With both the LHS and RHS evaluated, process the operation itself.
2428
2429 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002430
Ted Kremeneka8538d92009-02-13 01:45:31 +00002431 const GRState* state = GetState(*I2);
2432 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002433
Ted Kremeneka8538d92009-02-13 01:45:31 +00002434 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002435 BinaryOperator::Opcode Op = B->getOpcode();
2436
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002437 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002438
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002439 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002440
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002441 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002442 // FIXME: Handle structs.
2443 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002444
Ted Kremenek062e2f92008-11-13 06:10:40 +00002445 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2446 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002447 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002448 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002449
Ted Kremenek062e2f92008-11-13 06:10:40 +00002450 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002451 ? cast<SVal>(loc::SymbolVal(Sym))
2452 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002453 }
2454
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002455 // Simulate the effects of a "store": bind the value of the RHS
2456 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002457
Ted Kremeneka8538d92009-02-13 01:45:31 +00002458 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2459 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002460 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002461 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002462
2463 case BinaryOperator::Div:
2464 case BinaryOperator::Rem:
2465
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002466 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002467 if (RHS->getType()->isIntegerType() &&
2468 RHS->getType()->isScalarType()) {
2469
Ted Kremeneka8538d92009-02-13 01:45:31 +00002470 state = CheckDivideZero(B, state, *I2, RightV);
2471 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002472 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002473
2474 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002475
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002476 default: {
2477
2478 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002479 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002480
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002481 // Process non-assignements except commas or short-circuited
2482 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002483
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002484 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002485
2486 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002487 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002488 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002489 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002490 }
2491 else
2492 Dst.Add(*I2);
2493
Ted Kremenek89063af2008-02-21 19:15:37 +00002494 continue;
2495 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002496
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002497 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002498
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002499 // The operands were *not* undefined, but the result is undefined.
2500 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002501
Ted Kremeneka8538d92009-02-13 01:45:31 +00002502 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002503 UndefNode->markAsSink();
2504 UndefResults.insert(UndefNode);
2505 }
2506
2507 continue;
2508 }
2509
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002510 // Otherwise, create a new node.
2511
Ted Kremeneka8538d92009-02-13 01:45:31 +00002512 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002513 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002514 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002515 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002516
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002517 assert (B->isCompoundAssignmentOp());
2518
Ted Kremenekcad29962009-02-07 00:52:24 +00002519 switch (Op) {
2520 default:
2521 assert(0 && "Invalid opcode for compound assignment.");
2522 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2523 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2524 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2525 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2526 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2527 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2528 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2529 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2530 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2531 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002532 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002533
2534 // Perform a load (the LHS). This performs the checks for
2535 // null dereferences, and so on.
2536 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002537 SVal location = GetSVal(state, LHS);
2538 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002539
2540 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2541
Ted Kremeneka8538d92009-02-13 01:45:31 +00002542 state = GetState(*I3);
2543 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002544
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002545 // Check for divide-by-zero.
2546 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002547 && RHS->getType()->isIntegerType()
2548 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002549
2550 // CheckDivideZero returns a new state where the denominator
2551 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002552 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002553
Ted Kremeneka8538d92009-02-13 01:45:31 +00002554 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002555 continue;
2556 }
2557
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002558 // Propagate undefined values (left-side).
2559 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002560 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002561 continue;
2562 }
2563
2564 // Propagate unknown values (left and right-side).
2565 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002566 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2567 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002568 continue;
2569 }
2570
2571 // At this point:
2572 //
2573 // The LHS is not Undef/Unknown.
2574 // The RHS is not Unknown.
2575
2576 // Get the computation type.
2577 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002578 CTy = getContext().getCanonicalType(CTy);
2579
2580 QualType LTy = getContext().getCanonicalType(LHS->getType());
2581 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002582
2583 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002584 if (LTy != CTy) V = EvalCast(V, CTy);
2585 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002586
2587 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002588 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002589 // Propagate undefined values (right-side).
Ted Kremeneka8538d92009-02-13 01:45:31 +00002590 EvalStore(Dst,B, LHS, *I3, BindExpr(state, B, RightV), location,
2591 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002592 continue;
2593 }
2594
Ted Kremenek60595da2008-11-15 04:01:56 +00002595 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002596 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002597
2598 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002599 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002600 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002601 UndefNode->markAsSink();
2602 UndefResults.insert(UndefNode);
2603 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002604 continue;
2605 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002606
2607 // EXPERIMENTAL: "Conjured" symbols.
2608 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002609
2610 SVal LHSVal;
2611
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002612 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2613 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002614
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002615 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002616
Ted Kremenek60595da2008-11-15 04:01:56 +00002617 // The symbolic value is actually for the type of the left-hand side
2618 // expression, not the computation type, as this is the value the
2619 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002620 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002621 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002622 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002623 : cast<SVal>(nonloc::SymbolVal(Sym));
2624
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002625 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002626 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002627 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002628 else {
2629 // The left-hand side may bind to a different value then the
2630 // computation type.
2631 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2632 }
2633
Ted Kremeneka8538d92009-02-13 01:45:31 +00002634 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2635 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002636 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002637 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002638 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002639}
Ted Kremenekee985462008-01-16 18:18:48 +00002640
2641//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002642// Transfer-function Helpers.
2643//===----------------------------------------------------------------------===//
2644
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002645void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002646 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002647 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002648 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002649
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002650 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002651 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2652
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002653 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002654 MakeNode(Dst, Ex, Pred, *I);
2655}
2656
Ted Kremeneka8538d92009-02-13 01:45:31 +00002657void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002658 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002659 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002660
Ted Kremeneka8538d92009-02-13 01:45:31 +00002661 GRStateSet::AutoPopulate AP(OStates, state);
2662 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002663}
2664
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002665SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R) {
2666
2667 if (L.isUndef() || R.isUndef())
2668 return UndefinedVal();
2669
2670 if (L.isUnknown() || R.isUnknown())
2671 return UnknownVal();
2672
2673 if (isa<Loc>(L)) {
2674 if (isa<Loc>(R))
2675 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2676 else
2677 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2678 }
2679
2680 if (isa<Loc>(R)) {
2681 // Support pointer arithmetic where the increment/decrement operand
2682 // is on the left and the pointer on the right.
2683
2684 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2685
2686 // Commute the operands.
2687 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2688 cast<NonLoc>(L));
2689 }
2690 else
2691 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
2692 cast<NonLoc>(R));
2693}
2694
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002695//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002696// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002697//===----------------------------------------------------------------------===//
2698
Ted Kremenekaa66a322008-01-16 21:46:15 +00002699#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002700static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002701static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002702
Ted Kremenekaa66a322008-01-16 21:46:15 +00002703namespace llvm {
2704template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002705struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002706 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002707
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002708 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2709
2710 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002711 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002712 GraphPrintCheckerState->isUndefDeref(N) ||
2713 GraphPrintCheckerState->isUndefStore(N) ||
2714 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002715 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2716 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002717 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002718 GraphPrintCheckerState->isBadCall(N) ||
2719 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002720 return "color=\"red\",style=\"filled\"";
2721
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002722 if (GraphPrintCheckerState->isNoReturnCall(N))
2723 return "color=\"blue\",style=\"filled\"";
2724
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002725 return "";
2726 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002727
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002728 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002729 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002730
2731 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002732 ProgramPoint Loc = N->getLocation();
2733
2734 switch (Loc.getKind()) {
2735 case ProgramPoint::BlockEntranceKind:
2736 Out << "Block Entrance: B"
2737 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2738 break;
2739
2740 case ProgramPoint::BlockExitKind:
2741 assert (false);
2742 break;
2743
Ted Kremenekaa66a322008-01-16 21:46:15 +00002744 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002745 if (isa<PostStmt>(Loc)) {
2746 const PostStmt& L = cast<PostStmt>(Loc);
2747 Stmt* S = L.getStmt();
2748 SourceLocation SLoc = S->getLocStart();
2749
2750 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2751 llvm::raw_os_ostream OutS(Out);
2752 S->printPretty(OutS);
2753 OutS.flush();
2754
2755 if (SLoc.isFileID()) {
2756 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002757 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2758 << " col="
2759 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2760 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00002761 }
2762
2763 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2764 Out << "\\|Implicit-Null Dereference.\\l";
2765 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2766 Out << "\\|Explicit-Null Dereference.\\l";
2767 else if (GraphPrintCheckerState->isUndefDeref(N))
2768 Out << "\\|Dereference of undefialied value.\\l";
2769 else if (GraphPrintCheckerState->isUndefStore(N))
2770 Out << "\\|Store to Undefined Loc.";
2771 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2772 Out << "\\|Explicit divide-by zero or undefined value.";
2773 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2774 Out << "\\|Implicit divide-by zero or undefined value.";
2775 else if (GraphPrintCheckerState->isUndefResult(N))
2776 Out << "\\|Result of operation is undefined.";
2777 else if (GraphPrintCheckerState->isNoReturnCall(N))
2778 Out << "\\|Call to function marked \"noreturn\".";
2779 else if (GraphPrintCheckerState->isBadCall(N))
2780 Out << "\\|Call to NULL/Undefined.";
2781 else if (GraphPrintCheckerState->isUndefArg(N))
2782 Out << "\\|Argument in call is undefined";
2783
2784 break;
2785 }
2786
Ted Kremenekaa66a322008-01-16 21:46:15 +00002787 const BlockEdge& E = cast<BlockEdge>(Loc);
2788 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2789 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002790
2791 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002792
2793 SourceLocation SLoc = T->getLocStart();
2794
Ted Kremenekb38911f2008-01-30 23:03:39 +00002795 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002796
Ted Kremeneka95d3752008-09-13 05:16:45 +00002797 llvm::raw_os_ostream OutS(Out);
2798 E.getSrc()->printTerminator(OutS);
2799 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002800
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002801 if (SLoc.isFileID()) {
2802 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002803 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2804 << " col="
2805 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002806 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002807
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002808 if (isa<SwitchStmt>(T)) {
2809 Stmt* Label = E.getDst()->getLabel();
2810
2811 if (Label) {
2812 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2813 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002814 llvm::raw_os_ostream OutS(Out);
2815 C->getLHS()->printPretty(OutS);
2816 OutS.flush();
2817
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002818 if (Stmt* RHS = C->getRHS()) {
2819 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002820 RHS->printPretty(OutS);
2821 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002822 }
2823
2824 Out << ":";
2825 }
2826 else {
2827 assert (isa<DefaultStmt>(Label));
2828 Out << "\\ldefault:";
2829 }
2830 }
2831 else
2832 Out << "\\l(implicit) default:";
2833 }
2834 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002835 // FIXME
2836 }
2837 else {
2838 Out << "\\lCondition: ";
2839 if (*E.getSrc()->succ_begin() == E.getDst())
2840 Out << "true";
2841 else
2842 Out << "false";
2843 }
2844
2845 Out << "\\l";
2846 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002847
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002848 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2849 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002850 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002851 }
2852 }
2853
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002854 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002855
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002856 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2857 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002858
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002859 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002860 return Out.str();
2861 }
2862};
2863} // end llvm namespace
2864#endif
2865
Ted Kremenekffe0f432008-03-07 22:58:01 +00002866#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002867
2868template <typename ITERATOR>
2869GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2870
2871template <>
2872GRExprEngine::NodeTy*
2873GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2874 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2875 return I->first;
2876}
2877
Ted Kremenekffe0f432008-03-07 22:58:01 +00002878template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002879static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002880 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002881
Ted Kremenekd4527582008-09-16 18:44:52 +00002882 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002883
2884 for ( ; I != E; ++I ) {
2885 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002886 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002887
Ted Kremenekd4527582008-09-16 18:44:52 +00002888 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002889 continue;
2890
Ted Kremenekd4527582008-09-16 18:44:52 +00002891 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002892 Sources.push_back(N);
2893 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002894}
2895#endif
2896
2897void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002898#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002899 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002900 std::vector<NodeTy*> Src;
2901
Ted Kremenekcf118d42009-02-04 23:49:09 +00002902 // FIXME: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002903 AddSources(Src, null_derefs_begin(), null_derefs_end());
2904 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2905 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2906 AddSources(Src, undef_results_begin(), undef_results_end());
2907 AddSources(Src, bad_calls_begin(), bad_calls_end());
2908 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002909 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002910
Ted Kremenekcf118d42009-02-04 23:49:09 +00002911 // FIXME: Enhance BugReporter to have a clean way to query if a node
2912 // is involved in an error... and what kind.
Ted Kremenekcb612922008-04-18 19:23:43 +00002913
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002914 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002915 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002916 else {
2917 GraphPrintCheckerState = this;
2918 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002919
Ted Kremenekffe0f432008-03-07 22:58:01 +00002920 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002921
2922 GraphPrintCheckerState = NULL;
2923 GraphPrintSourceManager = NULL;
2924 }
2925#endif
2926}
2927
2928void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2929#ifndef NDEBUG
2930 GraphPrintCheckerState = this;
2931 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002932
Ted Kremenekcf118d42009-02-04 23:49:09 +00002933 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00002934
Ted Kremenekcf118d42009-02-04 23:49:09 +00002935 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00002936 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00002937 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00002938 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00002939
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002940 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002941 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002942#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002943}