blob: d34e8a3ed3a17e0d584948b412805134452b4831 [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);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000359
360 if (SE->getSubStmt()->body_empty()) {
361 // Empty statement expression.
362 assert(SE->getType() == getContext().VoidTy
363 && "Empty statement expression must have void type.");
364 Dst.Add(Pred);
365 break;
366 }
367
368 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
369 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000370 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000371 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000372 else
373 Dst.Add(Pred);
374
375 break;
376 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000377
378 case Stmt::StringLiteralClass:
379 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
380 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000381
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000382 case Stmt::UnaryOperatorClass:
383 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000384 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000385 }
386}
387
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000388void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000389
390 Ex = Ex->IgnoreParens();
391
392 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
393 Dst.Add(Pred);
394 return;
395 }
396
397 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000398
399 case Stmt::ArraySubscriptExprClass:
400 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
401 return;
402
403 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000404 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000405 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
406 return;
407
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000408 case Stmt::ObjCIvarRefExprClass:
409 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
410 return;
411
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000412 case Stmt::UnaryOperatorClass:
413 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
414 return;
415
416 case Stmt::MemberExprClass:
417 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
418 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000419
Ted Kremenek4f090272008-10-27 21:54:31 +0000420 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000421 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000422 return;
423
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000424 case Stmt::ObjCPropertyRefExprClass:
425 // FIXME: Property assignments are lvalues, but not really "locations".
426 // e.g.: self.x = something;
427 // Here the "self.x" really can translate to a method call (setter) when
428 // the assignment is made. Moreover, the entire assignment expression
429 // evaluate to whatever "something" is, not calling the "getter" for
430 // the property (which would make sense since it can have side effects).
431 // We'll probably treat this as a location, but not one that we can
432 // take the address of. Perhaps we need a new SVal class for cases
433 // like thsis?
434 // Note that we have a similar problem for bitfields, since they don't
435 // have "locations" in the sense that we can take their address.
436 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000437 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000438
439 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000440 const GRState* state = GetState(Pred);
441 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
442 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000443 return;
444 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000445
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000446 default:
447 // Arbitrary subexpressions can return aggregate temporaries that
448 // can be used in a lvalue context. We need to enhance our support
449 // of such temporaries in both the environment and the store, so right
450 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000451 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000452 "Other kinds of expressions with non-aggregate/union types do"
453 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000454
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000455 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000456 }
457}
458
459//===----------------------------------------------------------------------===//
460// Block entrance. (Update counters).
461//===----------------------------------------------------------------------===//
462
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000463bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000464 GRBlockCounter BC) {
465
466 return BC.getNumVisited(B->getBlockID()) < 3;
467}
468
469//===----------------------------------------------------------------------===//
470// Branch processing.
471//===----------------------------------------------------------------------===//
472
Ted Kremeneka8538d92009-02-13 01:45:31 +0000473const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000474 Stmt* Terminator,
475 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000476
477 switch (Terminator->getStmtClass()) {
478 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000479 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000480
481 case Stmt::BinaryOperatorClass: { // '&&' and '||'
482
483 BinaryOperator* B = cast<BinaryOperator>(Terminator);
484 BinaryOperator::Opcode Op = B->getOpcode();
485
486 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
487
488 // For &&, if we take the true branch, then the value of the whole
489 // expression is that of the RHS expression.
490 //
491 // For ||, if we take the false branch, then the value of the whole
492 // expression is that of the RHS expression.
493
494 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
495 (Op == BinaryOperator::LOr && !branchTaken)
496 ? B->getRHS() : B->getLHS();
497
Ted Kremeneka8538d92009-02-13 01:45:31 +0000498 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000499 }
500
501 case Stmt::ConditionalOperatorClass: { // ?:
502
503 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
504
505 // For ?, if branchTaken == true then the value is either the LHS or
506 // the condition itself. (GNU extension).
507
508 Expr* Ex;
509
510 if (branchTaken)
511 Ex = C->getLHS() ? C->getLHS() : C->getCond();
512 else
513 Ex = C->getRHS();
514
Ted Kremeneka8538d92009-02-13 01:45:31 +0000515 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000516 }
517
518 case Stmt::ChooseExprClass: { // ?:
519
520 ChooseExpr* C = cast<ChooseExpr>(Terminator);
521
522 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000523 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000524 }
525 }
526}
527
Ted Kremenekaf337412008-11-12 19:24:17 +0000528void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000529 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000530
Ted Kremeneke7d22112008-02-11 19:21:59 +0000531 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000532 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000533 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000534
Ted Kremenekb2331832008-02-15 22:29:00 +0000535 // Check for NULL conditions; e.g. "for(;;)"
536 if (!Condition) {
537 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000538 return;
539 }
540
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000541 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000542
543 switch (V.getBaseKind()) {
544 default:
545 break;
546
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000547 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000548 builder.generateNode(MarkBranch(PrevState, Term, true), true);
549 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000550 return;
551
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000552 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000553 NodeTy* N = builder.generateNode(PrevState, true);
554
555 if (N) {
556 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000557 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000558 }
559
560 builder.markInfeasible(false);
561 return;
562 }
563 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000564
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000565 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000566
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000567 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000568 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000569
570 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000571 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000572 else
573 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000574
575 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000576
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000577 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000578 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000579
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000580 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000581 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000582 else
583 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000584}
585
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000586/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000587/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000588void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000589
Ted Kremeneka8538d92009-02-13 01:45:31 +0000590 const GRState* state = builder.getState();
591 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000592
593 // Three possibilities:
594 //
595 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000596 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000597 // (3) We have no clue about the label. Dispatch to all targets.
598 //
599
600 typedef IndirectGotoNodeBuilder::iterator iterator;
601
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000602 if (isa<loc::GotoLabel>(V)) {
603 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000604
605 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000606 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000607 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000608 return;
609 }
610 }
611
612 assert (false && "No block with label.");
613 return;
614 }
615
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000616 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000617 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000618 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000619 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000620 return;
621 }
622
623 // This is really a catch-all. We don't support symbolics yet.
624
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000625 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000626
627 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000628 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000629}
Ted Kremenekf233d482008-02-05 00:26:40 +0000630
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000631
632void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
633 NodeTy* Pred, NodeSet& Dst) {
634
635 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
636
Ted Kremeneka8538d92009-02-13 01:45:31 +0000637 const GRState* state = GetState(Pred);
638 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000639
640 assert (X.isUndef());
641
642 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
643
644 assert (SE);
645
Ted Kremeneka8538d92009-02-13 01:45:31 +0000646 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000647
648 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000649 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000650}
651
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000652/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
653/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000654void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
655 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000656 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000657 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000658 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000659
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000660 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000661 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000662 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000663 return;
664 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000665
Ted Kremeneka8538d92009-02-13 01:45:31 +0000666 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000667 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000668
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000669 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000670 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000671
672 // Evaluate the LHS of the case value.
673 Expr::EvalResult V1;
674 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000675
Ted Kremenek72afb372009-01-17 01:54:16 +0000676 // Sanity checks. These go away in Release builds.
677 assert(b && V1.Val.isInt() && !V1.HasSideEffects
678 && "Case condition must evaluate to an integer constant.");
679 b = b; // silence unused variable warning
680 assert(V1.Val.getInt().getBitWidth() ==
681 getContext().getTypeSize(CondE->getType()));
682
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000683 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000684 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000685
686 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000687 b = E->Evaluate(V2, getContext());
688 assert(b && V2.Val.isInt() && !V2.HasSideEffects
689 && "Case condition must evaluate to an integer constant.");
690 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000691 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000692 else
693 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000694
695 // FIXME: Eventually we should replace the logic below with a range
696 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000697 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000698
Ted Kremenek14a11402008-03-17 22:17:56 +0000699 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000700 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000701 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000702
Ted Kremenek72afb372009-01-17 01:54:16 +0000703 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000704 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000705 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706
707 if (isFeasible) {
708 builder.generateCaseStmtNode(I, StNew);
709
710 // If CondV evaluates to a constant, then we know that this
711 // is the *only* case that we can take, so stop evaluating the
712 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000713 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000714 return;
715 }
716
717 // Now "assume" that the case doesn't match. Add this state
718 // to the default state (if it is feasible).
719
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000720 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000721 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722
Ted Kremenek5014ab12008-04-23 05:03:18 +0000723 if (isFeasible) {
724 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000725 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000726 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
Ted Kremenek14a11402008-03-17 22:17:56 +0000728 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000729 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000730 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000731
Ted Kremenek72afb372009-01-17 01:54:16 +0000732 ++V1.Val.getInt();
733 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000734
735 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000736 }
737
738 // If we reach here, than we know that the default branch is
739 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000740 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000741}
742
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000743//===----------------------------------------------------------------------===//
744// Transfer functions: logical operations ('&&', '||').
745//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000746
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000747void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000748 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000749
Ted Kremenek05a23782008-02-26 19:05:15 +0000750 assert (B->getOpcode() == BinaryOperator::LAnd ||
751 B->getOpcode() == BinaryOperator::LOr);
752
753 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
754
Ted Kremeneka8538d92009-02-13 01:45:31 +0000755 const GRState* state = GetState(Pred);
756 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000757
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000758 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000759
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000760 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000761
762 assert (Ex);
763
764 if (Ex == B->getRHS()) {
765
Ted Kremeneka8538d92009-02-13 01:45:31 +0000766 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000767
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000768 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000769
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000770 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000771 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000772 return;
773 }
774
Ted Kremenek05a23782008-02-26 19:05:15 +0000775 // We took the RHS. Because the value of the '&&' or '||' expression must
776 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
777 // or 1. Alternatively, we could take a lazy approach, and calculate this
778 // value later when necessary. We don't have the machinery in place for
779 // this right now, and since most logical expressions are used for branches,
780 // the payoff is not likely to be large. Instead, we do eager evaluation.
781
782 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000783 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000784
785 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000786 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000787 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000788
789 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000790 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000791
792 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000793 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000794 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000795 }
796 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000797 // We took the LHS expression. Depending on whether we are '&&' or
798 // '||' we know what the value of the expression is via properties of
799 // the short-circuiting.
800
801 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000802 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000803 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000804}
Ted Kremenek05a23782008-02-26 19:05:15 +0000805
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000806//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000807// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000808//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000809
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000810void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
811 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000812
Ted Kremeneka8538d92009-02-13 01:45:31 +0000813 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000814
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000815 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000816
817 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
818
Ted Kremeneka8538d92009-02-13 01:45:31 +0000819 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000820
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000821 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000822 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000823 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000824 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000825 return;
826
827 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
828 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
829
830 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000831 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000832 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000833 return;
834
835 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000836 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000837 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000838 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000839 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000840 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000841
842 assert (false &&
843 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000844}
845
Ted Kremenek540cbe22008-04-22 04:56:29 +0000846/// VisitArraySubscriptExpr - Transfer function for array accesses
847void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000848 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000849
850 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000851 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000852 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000853
854 if (Base->getType()->isVectorType()) {
855 // For vector types get its lvalue.
856 // FIXME: This may not be correct. Is the rvalue of a vector its location?
857 // In fact, I think this is just a hack. We need to get the right
858 // semantics.
859 VisitLValue(Base, Pred, Tmp);
860 }
861 else
862 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000863
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000864 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000865 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000866 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000867
868 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000869 const GRState* state = GetState(*I2);
870 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
871 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000872
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000873 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000874 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000875 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000876 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000877 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000878 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000879}
880
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000881/// VisitMemberExpr - Transfer function for member expressions.
882void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000883 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000884
885 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000886 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000887
888 if (M->isArrow())
889 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
890 else
891 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
892
Douglas Gregor86f19402008-12-20 23:49:58 +0000893 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
894 if (!Field) // FIXME: skipping member expressions for non-fields
895 return;
896
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000897 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000898 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000899 // FIXME: Should we insert some assumption logic in here to determine
900 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000901 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000902 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000903
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000904 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000905 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000906 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000907 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000908 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000909}
910
Ted Kremeneka8538d92009-02-13 01:45:31 +0000911/// EvalBind - Handle the semantics of binding a value to a specific location.
912/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
913void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
914 const GRState* state, SVal location, SVal Val) {
915
Ted Kremenek41573eb2009-02-14 01:43:44 +0000916 const GRState* newState = 0;
917
918 if (location.isUnknown()) {
919 // We know that the new state will be the same as the old state since
920 // the location of the binding is "unknown". Consequently, there
921 // is no reason to just create a new node.
922 newState = state;
923 }
924 else {
925 // We are binding to a value other than 'unknown'. Perform the binding
926 // using the StoreManager.
927 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
928 }
Ted Kremeneka8538d92009-02-13 01:45:31 +0000929
Ted Kremenek41573eb2009-02-14 01:43:44 +0000930 // The next thing to do is check if the GRTransferFuncs object wants to
931 // update the state based on the new binding. If the GRTransferFunc object
932 // doesn't do anything, just auto-propagate the current state.
933 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
934 newState != state);
935
936 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000937}
938
939/// EvalStore - Handle the semantics of a store via an assignment.
940/// @param Dst The node set to store generated state nodes
941/// @param Ex The expression representing the location of the store
942/// @param state The current simulation state
943/// @param location The location to store the value
944/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000945void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000946 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000947
948 assert (Builder && "GRStmtNodeBuilder must be defined.");
949
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000950 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000951 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000952
Ted Kremenek8c354752008-12-16 22:02:27 +0000953 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000954 return;
Ted Kremenekb0533962008-04-18 20:35:30 +0000955
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000956 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +0000957 state = GetState(Pred);
958
959 // Proceed with the store.
960 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
961 Builder->PointKind = ProgramPoint::PostStoreKind;
962 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000963}
964
965void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000966 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000967
Ted Kremenek8c354752008-12-16 22:02:27 +0000968 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000969 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000970
Ted Kremenek8c354752008-12-16 22:02:27 +0000971 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000972 return;
973
Ted Kremeneka8538d92009-02-13 01:45:31 +0000974 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +0000975
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000976 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000977 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978
979 // FIXME: Currently symbolic analysis "generates" new symbols
980 // for the contents of values. We need a better approach.
981
Ted Kremenek8c354752008-12-16 22:02:27 +0000982 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +0000983 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000984 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000985 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000986 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000987 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
988 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +0000989 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000990}
991
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000992void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +0000993 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000994
995 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000996 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000997
998 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
999 MakeNode(Dst, Ex, *I, (*I)->getState());
1000}
1001
Ted Kremenek8c354752008-12-16 22:02:27 +00001002GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001003 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001004 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001005
1006 // Check for loads/stores from/to undefined values.
1007 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001008 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001009 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001010 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001011
Ted Kremenek8c354752008-12-16 22:02:27 +00001012 if (N) {
1013 N->markAsSink();
1014 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001015 }
1016
Ted Kremenek8c354752008-12-16 22:02:27 +00001017 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001018 }
1019
1020 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1021 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001022 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023
1024 // During a load, one of two possible situations arise:
1025 // (1) A crash, because the location (pointer) was NULL.
1026 // (2) The location (pointer) is not NULL, and the dereference works.
1027 //
1028 // We add these assumptions.
1029
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001030 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001031
1032 // "Assume" that the pointer is not NULL.
1033
1034 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001035 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001036
1037 // "Assume" that the pointer is NULL.
1038
1039 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001040 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001041 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001042
1043 if (isFeasibleNull) {
1044
Ted Kremenek7360fda2008-09-18 23:09:54 +00001045 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001046 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001047 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1048
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001049 // We don't use "MakeNode" here because the node will be a sink
1050 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001051 NodeTy* NullNode =
1052 Builder->generateNode(Ex, StNull, Pred,
1053 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001054
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001055 if (NullNode) {
1056
1057 NullNode->markAsSink();
1058
1059 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1060 else ExplicitNullDeref.insert(NullNode);
1061 }
1062 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001063
1064 if (!isFeasibleNotNull)
1065 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001066
1067 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001068 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001069 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1070 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1071 // Get the index of the accessed element.
1072 SVal Idx = ER->getIndex();
1073 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001074 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1075 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001076
1077 bool isFeasibleInBound = false;
1078 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1079 true, isFeasibleInBound);
1080
1081 bool isFeasibleOutBound = false;
1082 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1083 false, isFeasibleOutBound);
1084
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001085 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001086 // Report warning. Make sink node manually.
1087 NodeTy* OOBNode =
1088 Builder->generateNode(Ex, StOutBound, Pred,
1089 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001090
1091 if (OOBNode) {
1092 OOBNode->markAsSink();
1093
1094 if (isFeasibleInBound)
1095 ImplicitOOBMemAccesses.insert(OOBNode);
1096 else
1097 ExplicitOOBMemAccesses.insert(OOBNode);
1098 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001099 }
1100
Ted Kremenek8c354752008-12-16 22:02:27 +00001101 if (!isFeasibleInBound)
1102 return 0;
1103
1104 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001105 }
1106 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001107
Ted Kremenek8c354752008-12-16 22:02:27 +00001108 // Generate a new node indicating the checks succeed.
1109 return Builder->generateNode(Ex, StNotNull, Pred,
1110 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001111}
1112
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001113//===----------------------------------------------------------------------===//
1114// Transfer function: Function calls.
1115//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001116void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001117 CallExpr::arg_iterator AI,
1118 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001119 NodeSet& Dst)
1120{
1121 // Determine the type of function we're calling (if available).
1122 const FunctionTypeProto *Proto = NULL;
1123 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1124 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1125 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1126
1127 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1128}
1129
1130void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1131 CallExpr::arg_iterator AI,
1132 CallExpr::arg_iterator AE,
1133 NodeSet& Dst, const FunctionTypeProto *Proto,
1134 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001135
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001136 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001137 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001138 // If the call argument is being bound to a reference parameter,
1139 // visit it as an lvalue, not an rvalue.
1140 bool VisitAsLvalue = false;
1141 if (Proto && ParamIdx < Proto->getNumArgs())
1142 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1143
1144 NodeSet DstTmp;
1145 if (VisitAsLvalue)
1146 VisitLValue(*AI, Pred, DstTmp);
1147 else
1148 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001149 ++AI;
1150
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001151 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001152 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001153
1154 return;
1155 }
1156
1157 // If we reach here we have processed all of the arguments. Evaluate
1158 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001159
Ted Kremenek994a09b2008-02-25 21:16:03 +00001160 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001161 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001162
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001163 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001164
Ted Kremenekde434242008-02-19 01:44:53 +00001165 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001166 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1167
Ted Kremeneka8538d92009-02-13 01:45:31 +00001168 const GRState* state = GetState(*DI);
1169 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001170
Ted Kremeneka1354a52008-03-03 16:47:31 +00001171 // FIXME: Add support for symbolic function calls (calls involving
1172 // function pointer values that are symbolic).
1173
1174 // Check for undefined control-flow or calls to NULL.
1175
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001176 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001177 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001178
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001179 if (N) {
1180 N->markAsSink();
1181 BadCalls.insert(N);
1182 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001183
Ted Kremenekde434242008-02-19 01:44:53 +00001184 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001185 }
1186
1187 // Check for the "noreturn" attribute.
1188
1189 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1190
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001191 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001192
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001193 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001194
1195 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001196 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001197 else {
1198 // HACK: Some functions are not marked noreturn, and don't return.
1199 // Here are a few hardwired ones. If this takes too long, we can
1200 // potentially cache these results.
1201 const char* s = FD->getIdentifier()->getName();
1202 unsigned n = strlen(s);
1203
1204 switch (n) {
1205 default:
1206 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001207
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001208 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001209 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1210 break;
1211
1212 case 5:
1213 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001214 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001215 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001216 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001217 // FIXME: use Assume to inspect the possible symbolic value of
1218 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001219 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001220 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001221 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001222 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001223 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001224 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001225
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001226 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001227 if (!memcmp(s, "Assert", 6)) {
1228 Builder->BuildSinks = true;
1229 break;
1230 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001231
1232 // FIXME: This is just a wrapper around throwing an exception.
1233 // Eventually inter-procedural analysis should handle this easily.
1234 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1235
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001236 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001237
1238 case 7:
1239 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1240 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001241
Ted Kremenekf47bb782008-04-30 17:54:04 +00001242 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001243 if (!memcmp(s ,"db_error", 8) ||
1244 !memcmp(s, "__assert", 8))
1245 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001246 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001247
1248 case 12:
1249 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1250 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001251
Ted Kremenekf9683082008-09-19 02:30:47 +00001252 case 13:
1253 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1254 break;
1255
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001256 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001257 if (!memcmp(s, "dtrace_assfail", 14) ||
1258 !memcmp(s, "yy_fatal_error", 14))
1259 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001260 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001261
1262 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001263 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001264 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1265 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001266 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001267
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001268 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001269 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001270
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001271 }
1272 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001273
1274 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001275
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001276 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001277
Douglas Gregor3c385e52009-02-14 18:57:46 +00001278 if (unsigned id
1279 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001280 switch (id) {
1281 case Builtin::BI__builtin_expect: {
1282 // For __builtin_expect, just return the value of the subexpression.
1283 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001284 SVal X = GetSVal(state, *(CE->arg_begin()));
1285 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001286 continue;
1287 }
1288
Ted Kremenekb3021332008-11-02 00:35:01 +00001289 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001290 // FIXME: Refactor into StoreManager itself?
1291 MemRegionManager& RM = getStateManager().getRegionManager();
1292 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001293 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001294
1295 // Set the extent of the region in bytes. This enables us to use the
1296 // SVal of the argument directly. If we save the extent in bits, we
1297 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001298 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1299 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001300
Ted Kremeneka8538d92009-02-13 01:45:31 +00001301 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001302 continue;
1303 }
1304
Ted Kremenek55aea312008-03-05 22:59:42 +00001305 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001306 break;
1307 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001308 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001309
Ted Kremenek186350f2008-04-23 20:12:28 +00001310 // Check any arguments passed-by-value against being undefined.
1311
1312 bool badArg = false;
1313
1314 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1315 I != E; ++I) {
1316
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001317 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001318 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001319
Ted Kremenek186350f2008-04-23 20:12:28 +00001320 if (N) {
1321 N->markAsSink();
1322 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001323 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001324
Ted Kremenek186350f2008-04-23 20:12:28 +00001325 badArg = true;
1326 break;
1327 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001328 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001329
1330 if (badArg)
1331 continue;
1332
1333 // Dispatch to the plug-in transfer function.
1334
1335 unsigned size = Dst.size();
1336 SaveOr OldHasGen(Builder->HasGeneratedNode);
1337 EvalCall(Dst, CE, L, *DI);
1338
1339 // Handle the case where no nodes where generated. Auto-generate that
1340 // contains the updated state if we aren't generating sinks.
1341
1342 if (!Builder->BuildSinks && Dst.size() == size &&
1343 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001344 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001345 }
1346}
1347
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001348//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001349// Transfer function: Objective-C ivar references.
1350//===----------------------------------------------------------------------===//
1351
1352void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1353 NodeTy* Pred, NodeSet& Dst,
1354 bool asLValue) {
1355
1356 Expr* Base = cast<Expr>(Ex->getBase());
1357 NodeSet Tmp;
1358 Visit(Base, Pred, Tmp);
1359
1360 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001361 const GRState* state = GetState(*I);
1362 SVal BaseVal = GetSVal(state, Base);
1363 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001364
1365 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001366 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001367 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001368 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001369 }
1370}
1371
1372//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001373// Transfer function: Objective-C fast enumeration 'for' statements.
1374//===----------------------------------------------------------------------===//
1375
1376void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1377 NodeTy* Pred, NodeSet& Dst) {
1378
1379 // ObjCForCollectionStmts are processed in two places. This method
1380 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1381 // statements within a basic block. This transfer function does two things:
1382 //
1383 // (1) binds the next container value to 'element'. This creates a new
1384 // node in the ExplodedGraph.
1385 //
1386 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1387 // whether or not the container has any more elements. This value
1388 // will be tested in ProcessBranch. We need to explicitly bind
1389 // this value because a container can contain nil elements.
1390 //
1391 // FIXME: Eventually this logic should actually do dispatches to
1392 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1393 // This will require simulating a temporary NSFastEnumerationState, either
1394 // through an SVal or through the use of MemRegions. This value can
1395 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1396 // terminates we reclaim the temporary (it goes out of scope) and we
1397 // we can test if the SVal is 0 or if the MemRegion is null (depending
1398 // on what approach we take).
1399 //
1400 // For now: simulate (1) by assigning either a symbol or nil if the
1401 // container is empty. Thus this transfer function will by default
1402 // result in state splitting.
1403
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001404 Stmt* elem = S->getElement();
1405 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001406
1407 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001408 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001409 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001410 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1411 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1412 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001413 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001414
1415 NodeSet Tmp;
1416 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001417
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001418 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1419 const GRState* state = GetState(*I);
1420 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1421 }
1422}
1423
1424void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1425 NodeTy* Pred, NodeSet& Dst,
1426 SVal ElementV) {
1427
1428
Ted Kremenekaf337412008-11-12 19:24:17 +00001429
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001430 // Get the current state. Use 'EvalLocation' to determine if it is a null
1431 // pointer, etc.
1432 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001433
Ted Kremenek8c354752008-12-16 22:02:27 +00001434 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1435 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001436 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001437
1438 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001439
Ted Kremenekaf337412008-11-12 19:24:17 +00001440 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001441 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001442 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1443 GRStateRef hasElems = state.BindExpr(S, TrueV);
1444
Ted Kremenekaf337412008-11-12 19:24:17 +00001445 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001446 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1447 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001448
1449 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1450 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1451 // FIXME: The proper thing to do is to really iterate over the
1452 // container. We will do this with dispatch logic to the store.
1453 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001454 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001455 assert (Loc::IsLocType(T));
1456 unsigned Count = Builder->getCurrentBlockCount();
1457 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1458 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001459
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001460 // Bind the location to 'nil' on the false branch.
1461 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1462 noElems = noElems.BindLoc(ElementV, nilV);
1463 }
1464
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001465 // Create the new nodes.
1466 MakeNode(Dst, S, Pred, hasElems);
1467 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001468}
1469
1470//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001471// Transfer function: Objective-C message expressions.
1472//===----------------------------------------------------------------------===//
1473
1474void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1475 NodeSet& Dst){
1476
1477 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1478 Pred, Dst);
1479}
1480
1481void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001482 ObjCMessageExpr::arg_iterator AI,
1483 ObjCMessageExpr::arg_iterator AE,
1484 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001485 if (AI == AE) {
1486
1487 // Process the receiver.
1488
1489 if (Expr* Receiver = ME->getReceiver()) {
1490 NodeSet Tmp;
1491 Visit(Receiver, Pred, Tmp);
1492
1493 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1494 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1495
1496 return;
1497 }
1498
1499 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1500 return;
1501 }
1502
1503 NodeSet Tmp;
1504 Visit(*AI, Pred, Tmp);
1505
1506 ++AI;
1507
1508 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1509 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1510}
1511
1512void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1513 NodeTy* Pred,
1514 NodeSet& Dst) {
1515
1516 // FIXME: More logic for the processing the method call.
1517
Ted Kremeneka8538d92009-02-13 01:45:31 +00001518 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001519 bool RaisesException = false;
1520
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001521
1522 if (Expr* Receiver = ME->getReceiver()) {
1523
Ted Kremeneka8538d92009-02-13 01:45:31 +00001524 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001525
Ted Kremenek21fe8372009-02-19 04:06:22 +00001526 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001527 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001528 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001529
1530 if (N) {
1531 N->markAsSink();
1532 UndefReceivers.insert(N);
1533 }
1534
1535 return;
1536 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001537
Ted Kremenek21fe8372009-02-19 04:06:22 +00001538 // "Assume" that the receiver is not NULL.
1539 bool isFeasibleNotNull = false;
1540 Assume(state, L, true, isFeasibleNotNull);
1541
1542 // "Assume" that the receiver is NULL.
1543 bool isFeasibleNull = false;
1544 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1545
1546 if (isFeasibleNull) {
1547 // Check if the receiver was nil and the return value a struct.
1548 if (ME->getType()->isRecordType()) {
1549 // The [0 ...] expressions will return garbage. Flag either an
1550 // explicit or implicit error. Because of the structure of this
1551 // function we currently do not bifurfacte the state graph at
1552 // this point.
1553 // FIXME: We should bifurcate and fill the returned struct with
1554 // garbage.
1555 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1556 N->markAsSink();
1557 if (isFeasibleNotNull)
1558 NilReceiverStructRetImplicit.insert(N);
1559 else
1560 NilReceiverStructRetExplicit.insert(N);
1561 }
1562 }
1563 }
1564
Ted Kremeneke448ab42008-05-01 18:33:28 +00001565 // Check if the "raise" message was sent.
1566 if (ME->getSelector() == RaiseSel)
1567 RaisesException = true;
1568 }
1569 else {
1570
1571 IdentifierInfo* ClsName = ME->getClassName();
1572 Selector S = ME->getSelector();
1573
1574 // Check for special instance methods.
1575
1576 if (!NSExceptionII) {
1577 ASTContext& Ctx = getContext();
1578
1579 NSExceptionII = &Ctx.Idents.get("NSException");
1580 }
1581
1582 if (ClsName == NSExceptionII) {
1583
1584 enum { NUM_RAISE_SELECTORS = 2 };
1585
1586 // Lazily create a cache of the selectors.
1587
1588 if (!NSExceptionInstanceRaiseSelectors) {
1589
1590 ASTContext& Ctx = getContext();
1591
1592 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1593
1594 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1595 unsigned idx = 0;
1596
1597 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001598 II.push_back(&Ctx.Idents.get("raise"));
1599 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001600 NSExceptionInstanceRaiseSelectors[idx++] =
1601 Ctx.Selectors.getSelector(II.size(), &II[0]);
1602
1603 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001604 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001605 NSExceptionInstanceRaiseSelectors[idx++] =
1606 Ctx.Selectors.getSelector(II.size(), &II[0]);
1607 }
1608
1609 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1610 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1611 RaisesException = true; break;
1612 }
1613 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001614 }
1615
1616 // Check for any arguments that are uninitialized/undefined.
1617
1618 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1619 I != E; ++I) {
1620
Ted Kremeneka8538d92009-02-13 01:45:31 +00001621 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001622
1623 // Generate an error node for passing an uninitialized/undefined value
1624 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001625 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001626
1627 if (N) {
1628 N->markAsSink();
1629 MsgExprUndefArgs[N] = *I;
1630 }
1631
1632 return;
1633 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001634 }
1635
1636 // Check if we raise an exception. For now treat these as sinks. Eventually
1637 // we will want to handle exceptions properly.
1638
1639 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1640
1641 if (RaisesException)
1642 Builder->BuildSinks = true;
1643
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001644 // Dispatch to plug-in transfer function.
1645
1646 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001647 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001648
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001649 EvalObjCMessageExpr(Dst, ME, Pred);
1650
1651 // Handle the case where no nodes where generated. Auto-generate that
1652 // contains the updated state if we aren't generating sinks.
1653
Ted Kremenekb0533962008-04-18 20:35:30 +00001654 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001655 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001656}
1657
1658//===----------------------------------------------------------------------===//
1659// Transfer functions: Miscellaneous statements.
1660//===----------------------------------------------------------------------===//
1661
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001662void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1663 QualType PtrTy,
1664 Expr* CastE, NodeTy* Pred,
1665 NodeSet& Dst) {
1666 if (!V.isUnknownOrUndef()) {
1667 // FIXME: Determine if the number of bits of the target type is
1668 // equal or exceeds the number of bits to store the pointer value.
1669 // If not, flag an error.
1670 unsigned bits = getContext().getTypeSize(PtrTy);
1671 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1672 }
1673
1674 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
1675}
1676
1677
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001678void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001679 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001680 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001681 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001682
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001683 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001684 T = ExCast->getTypeAsWritten();
1685
Zhongxing Xued340f72008-10-22 08:02:16 +00001686 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001687 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001688 else
1689 Visit(Ex, Pred, S1);
1690
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001691 // Check for casting to "void".
1692 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001693
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001694 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001695 Dst.Add(*I1);
1696
Ted Kremenek874d63f2008-01-24 02:02:54 +00001697 return;
1698 }
1699
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001700 // FIXME: The rest of this should probably just go into EvalCall, and
1701 // let the transfer function object be responsible for constructing
1702 // nodes.
1703
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001704 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001705 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001706 const GRState* state = GetState(N);
1707 SVal V = GetSVal(state, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001708
1709 // Unknown?
1710
1711 if (V.isUnknown()) {
1712 Dst.Add(N);
1713 continue;
1714 }
1715
1716 // Undefined?
1717
1718 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001719 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001720 continue;
1721 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001722
1723 // For const casts, just propagate the value.
1724 ASTContext& C = getContext();
1725
1726 if (C.getCanonicalType(T).getUnqualifiedType() ==
1727 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001728 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001729 continue;
1730 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001731
1732 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001733 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001734 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001735 continue;
1736 }
1737
1738 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001739 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1740 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001741 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001742 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001743 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001744 continue;
1745 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001746
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001747 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001748 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001749 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001750 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001751
1752 // Are we casting from an array to a pointer? If so just pass on
1753 // the decayed value.
1754 if (T->isPointerType()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001755 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001756 continue;
1757 }
1758
1759 // Are we casting from an array to an integer? If so, cast the decayed
1760 // pointer value to an integer.
1761 assert(T->isIntegerType());
1762 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1763 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001764 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001765 continue;
1766 }
1767
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001768 // Check for casts from a region to a specific type.
1769 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001770 assert(Loc::IsLocType(T));
1771 assert(Loc::IsLocType(ExTy));
1772
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001773 const MemRegion* R = RV->getRegion();
1774 StoreManager& StoreMgr = getStoreManager();
1775
1776 // Delegate to store manager to get the result of casting a region
1777 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001778 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001779
1780 // Inspect the result. If the MemRegion* returned is NULL, this
1781 // expression evaluates to UnknownVal.
1782 R = Res.getRegion();
1783 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1784
1785 // Generate the new node in the ExplodedGraph.
1786 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001787 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001788 }
1789
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001790 // All other cases.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001791 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
1792 EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001793 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001794}
1795
Ted Kremenek4f090272008-10-27 21:54:31 +00001796void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001797 NodeTy* Pred, NodeSet& Dst,
1798 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001799 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1800 NodeSet Tmp;
1801 Visit(ILE, Pred, Tmp);
1802
1803 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001804 const GRState* state = GetState(*I);
1805 SVal ILV = GetSVal(state, ILE);
1806 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001807
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001808 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001809 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001810 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001811 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001812 }
1813}
1814
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001815void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001816
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001817 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001818 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001819
1820 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001821 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001822
Ted Kremenekefd59942008-12-08 22:47:34 +00001823 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001824 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001825
1826 // FIXME: static variables may have an initializer, but the second
1827 // time a function is called those values may not be current.
1828 NodeSet Tmp;
1829
Ted Kremenekaf337412008-11-12 19:24:17 +00001830 if (InitEx)
1831 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001832
1833 if (Tmp.empty())
1834 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001835
1836 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001837 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001838 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001839
Ted Kremenek5b8d9012009-02-14 01:54:57 +00001840 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1841 QualType T = getContext().getCanonicalType(VD->getType());
1842 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1843 // FIXME: Handle multi-dimensional VLAs.
1844
1845 Expr* SE = VLA->getSizeExpr();
1846 SVal Size = GetSVal(state, SE);
1847
1848 if (Size.isUndef()) {
1849 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
1850 N->markAsSink();
1851 ExplicitBadSizedVLA.insert(N);
1852 }
1853 continue;
1854 }
1855
1856 bool isFeasibleZero = false;
1857 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
1858
1859 bool isFeasibleNotZero = false;
1860 state = Assume(state, Size, true, isFeasibleNotZero);
1861
1862 if (isFeasibleZero) {
1863 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1864 N->markAsSink();
1865 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1866 else ExplicitBadSizedVLA.insert(N);
1867 }
1868 }
1869
1870 if (!isFeasibleNotZero)
1871 continue;
1872 }
1873
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001874 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001875 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001876 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00001877 QualType T = VD->getType();
1878
1879 // Recover some path-sensitivity if a scalar value evaluated to
1880 // UnknownVal.
1881 if (InitVal.isUnknown()) {
1882 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001883 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001884 InitVal = loc::SymbolVal(Sym);
1885 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001886 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001887 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001888 InitVal = nonloc::SymbolVal(Sym);
1889 }
1890 }
1891
Ted Kremeneka8538d92009-02-13 01:45:31 +00001892 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00001893
1894 // The next thing to do is check if the GRTransferFuncs object wants to
1895 // update the state based on the new binding. If the GRTransferFunc
1896 // object doesn't do anything, just auto-propagate the current state.
1897 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
1898 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
1899 InitVal);
1900 }
1901 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001902 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00001903 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00001904 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001905 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001906}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001907
Ted Kremenekf75b1862008-10-30 17:47:32 +00001908namespace {
1909 // This class is used by VisitInitListExpr as an item in a worklist
1910 // for processing the values contained in an InitListExpr.
1911class VISIBILITY_HIDDEN InitListWLItem {
1912public:
1913 llvm::ImmutableList<SVal> Vals;
1914 GRExprEngine::NodeTy* N;
1915 InitListExpr::reverse_iterator Itr;
1916
1917 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1918 InitListExpr::reverse_iterator itr)
1919 : Vals(vals), N(n), Itr(itr) {}
1920};
1921}
1922
1923
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001924void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1925 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001926
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001927 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001928 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001929 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001930
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001931 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001932
Ted Kremeneka49e3672008-10-30 23:14:36 +00001933 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001934
Ted Kremeneka49e3672008-10-30 23:14:36 +00001935 // Handle base case where the initializer has no elements.
1936 // e.g: static int* myArray[] = {};
1937 if (NumInitElements == 0) {
1938 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1939 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1940 return;
1941 }
1942
1943 // Create a worklist to process the initializers.
1944 llvm::SmallVector<InitListWLItem, 10> WorkList;
1945 WorkList.reserve(NumInitElements);
1946 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001947 InitListExpr::reverse_iterator ItrEnd = E->rend();
1948
Ted Kremeneka49e3672008-10-30 23:14:36 +00001949 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001950 while (!WorkList.empty()) {
1951 InitListWLItem X = WorkList.back();
1952 WorkList.pop_back();
1953
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001954 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001955 Visit(*X.Itr, X.N, Tmp);
1956
1957 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001958
Ted Kremenekf75b1862008-10-30 17:47:32 +00001959 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1960 // Get the last initializer value.
1961 state = GetState(*NI);
1962 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1963
1964 // Construct the new list of values by prepending the new value to
1965 // the already constructed list.
1966 llvm::ImmutableList<SVal> NewVals =
1967 getBasicVals().consVals(InitV, X.Vals);
1968
1969 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001970 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001971 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001972
Ted Kremenekf75b1862008-10-30 17:47:32 +00001973 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001974 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001975 }
1976 else {
1977 // Still some initializer values to go. Push them onto the worklist.
1978 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1979 }
1980 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001981 }
Ted Kremenek87903072008-10-30 18:34:31 +00001982
1983 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001984 }
1985
Ted Kremenek062e2f92008-11-13 06:10:40 +00001986 if (T->isUnionType() || T->isVectorType()) {
1987 // FIXME: to be implemented.
1988 // Note: That vectors can return true for T->isIntegerType()
1989 MakeNode(Dst, E, Pred, state);
1990 return;
1991 }
1992
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001993 if (Loc::IsLocType(T) || T->isIntegerType()) {
1994 assert (E->getNumInits() == 1);
1995 NodeSet Tmp;
1996 Expr* Init = E->getInit(0);
1997 Visit(Init, Pred, Tmp);
1998 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1999 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002000 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002001 }
2002 return;
2003 }
2004
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002005
2006 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2007 assert(0 && "unprocessed InitListExpr type");
2008}
Ted Kremenekf233d482008-02-05 00:26:40 +00002009
Sebastian Redl05189992008-11-11 17:56:53 +00002010/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2011void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2012 NodeTy* Pred,
2013 NodeSet& Dst) {
2014 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002015 uint64_t amt;
2016
2017 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002018 if (T == getContext().VoidTy) {
2019 // sizeof(void) == 1 byte.
2020 amt = 1;
2021 }
2022 else if (!T.getTypePtr()->isConstantSizeType()) {
2023 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002024 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002025 }
2026 else if (T->isObjCInterfaceType()) {
2027 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2028 // the compiler has laid out its representation. Just report Unknown
2029 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002030 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002031 }
2032 else {
2033 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002034 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002035 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002036 }
2037 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002038 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002039
Ted Kremenek0e561a32008-03-21 21:30:14 +00002040 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002041 BindExpr(GetState(Pred), Ex,
2042 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002043}
2044
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002045
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002046void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002047 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002048
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002049 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002050
2051 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002052 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002053
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002054 case UnaryOperator::Deref: {
2055
2056 Expr* Ex = U->getSubExpr()->IgnoreParens();
2057 NodeSet Tmp;
2058 Visit(Ex, Pred, Tmp);
2059
2060 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002061
Ted Kremeneka8538d92009-02-13 01:45:31 +00002062 const GRState* state = GetState(*I);
2063 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002064
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002065 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002066 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002067 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002068 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002069 }
2070
2071 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002072 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002073
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002074 case UnaryOperator::Real: {
2075
2076 Expr* Ex = U->getSubExpr()->IgnoreParens();
2077 NodeSet Tmp;
2078 Visit(Ex, Pred, Tmp);
2079
2080 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2081
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002082 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002083 if (Ex->getType()->isAnyComplexType()) {
2084 // Just report "Unknown."
2085 Dst.Add(*I);
2086 continue;
2087 }
2088
2089 // For all other types, UnaryOperator::Real is an identity operation.
2090 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002091 const GRState* state = GetState(*I);
2092 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002093 }
2094
2095 return;
2096 }
2097
2098 case UnaryOperator::Imag: {
2099
2100 Expr* Ex = U->getSubExpr()->IgnoreParens();
2101 NodeSet Tmp;
2102 Visit(Ex, Pred, Tmp);
2103
2104 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002105 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002106 if (Ex->getType()->isAnyComplexType()) {
2107 // Just report "Unknown."
2108 Dst.Add(*I);
2109 continue;
2110 }
2111
2112 // For all other types, UnaryOperator::Float returns 0.
2113 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002114 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002115 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002116 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002117 }
2118
2119 return;
2120 }
2121
2122 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002123 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002124 Dst.Add(Pred);
2125 return;
2126
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002127 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002128 case UnaryOperator::Extension: {
2129
2130 // Unary "+" is a no-op, similar to a parentheses. We still have places
2131 // where it may be a block-level expression, so we need to
2132 // generate an extra node that just propagates the value of the
2133 // subexpression.
2134
2135 Expr* Ex = U->getSubExpr()->IgnoreParens();
2136 NodeSet Tmp;
2137 Visit(Ex, Pred, Tmp);
2138
2139 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002140 const GRState* state = GetState(*I);
2141 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002142 }
2143
2144 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002145 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002146
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002147 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002148
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002149 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002150 Expr* Ex = U->getSubExpr()->IgnoreParens();
2151 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002152 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002153
2154 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002155 const GRState* state = GetState(*I);
2156 SVal V = GetSVal(state, Ex);
2157 state = BindExpr(state, U, V);
2158 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002159 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002160
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002161 return;
2162 }
2163
2164 case UnaryOperator::LNot:
2165 case UnaryOperator::Minus:
2166 case UnaryOperator::Not: {
2167
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002168 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002169 Expr* Ex = U->getSubExpr()->IgnoreParens();
2170 NodeSet Tmp;
2171 Visit(Ex, Pred, Tmp);
2172
2173 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002174 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002175
2176 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002177 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002178
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002179 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002180 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002181 continue;
2182 }
2183
Ted Kremenek60595da2008-11-15 04:01:56 +00002184// QualType DstT = getContext().getCanonicalType(U->getType());
2185// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2186//
2187// if (DstT != SrcT) // Perform promotions.
2188// V = EvalCast(V, DstT);
2189//
2190// if (V.isUnknownOrUndef()) {
2191// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2192// continue;
2193// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002194
2195 switch (U->getOpcode()) {
2196 default:
2197 assert(false && "Invalid Opcode.");
2198 break;
2199
2200 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002201 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002202 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002203 break;
2204
2205 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002206 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002207 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002208 break;
2209
2210 case UnaryOperator::LNot:
2211
2212 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2213 //
2214 // Note: technically we do "E == 0", but this is the same in the
2215 // transfer functions as "0 == E".
2216
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002217 if (isa<Loc>(V)) {
2218 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2219 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002220 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002221 }
2222 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002223 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002224#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002225 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002226 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002227#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002228 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002229 continue;
2230#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002231 }
2232
2233 break;
2234 }
2235
Ted Kremeneka8538d92009-02-13 01:45:31 +00002236 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002237 }
2238
2239 return;
2240 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002241 }
2242
2243 // Handle ++ and -- (both pre- and post-increment).
2244
2245 assert (U->isIncrementDecrementOp());
2246 NodeSet Tmp;
2247 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002248 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002249
2250 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2251
Ted Kremeneka8538d92009-02-13 01:45:31 +00002252 const GRState* state = GetState(*I);
2253 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002254
2255 // Perform a load.
2256 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002257 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002258
2259 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2260
Ted Kremeneka8538d92009-02-13 01:45:31 +00002261 state = GetState(*I2);
2262 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002263
2264 // Propagate unknown and undefined values.
2265 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002266 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002267 continue;
2268 }
2269
Ted Kremenek443003b2008-02-21 19:29:23 +00002270 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002271
2272 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2273 : BinaryOperator::Sub;
2274
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002275 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Ted Kremeneka8538d92009-02-13 01:45:31 +00002276 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002277
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002278 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002279 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002280 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002281 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002282}
2283
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002284void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2285 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2286}
2287
2288void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2289 AsmStmt::outputs_iterator I,
2290 AsmStmt::outputs_iterator E,
2291 NodeTy* Pred, NodeSet& Dst) {
2292 if (I == E) {
2293 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2294 return;
2295 }
2296
2297 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002298 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002299
2300 ++I;
2301
2302 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2303 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2304}
2305
2306void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2307 AsmStmt::inputs_iterator I,
2308 AsmStmt::inputs_iterator E,
2309 NodeTy* Pred, NodeSet& Dst) {
2310 if (I == E) {
2311
2312 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002313 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002314
2315 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2316 // which interprets the inline asm and stores proper results in the
2317 // outputs.
2318
Ted Kremeneka8538d92009-02-13 01:45:31 +00002319 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002320
2321 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2322 OE = A->end_outputs(); OI != OE; ++OI) {
2323
Ted Kremeneka8538d92009-02-13 01:45:31 +00002324 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002325 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002326
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002327 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002328 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002329 }
2330
Ted Kremeneka8538d92009-02-13 01:45:31 +00002331 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002332 return;
2333 }
2334
2335 NodeSet Tmp;
2336 Visit(*I, Pred, Tmp);
2337
2338 ++I;
2339
2340 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2341 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2342}
2343
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002344void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2345 assert (Builder && "GRStmtNodeBuilder must be defined.");
2346
2347 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002348
Ted Kremenek186350f2008-04-23 20:12:28 +00002349 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2350 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002351
Ted Kremenek729a9a22008-07-17 23:15:45 +00002352 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002353
Ted Kremenekb0533962008-04-18 20:35:30 +00002354 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002355
Ted Kremenekb0533962008-04-18 20:35:30 +00002356 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002357 MakeNode(Dst, S, Pred, GetState(Pred));
2358}
2359
Ted Kremenek02737ed2008-03-31 15:02:58 +00002360void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2361
2362 Expr* R = S->getRetValue();
2363
2364 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002365 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002366 return;
2367 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002368
Ted Kremenek5917d782008-11-21 00:27:44 +00002369 NodeSet Tmp;
2370 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002371
Ted Kremenek5917d782008-11-21 00:27:44 +00002372 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2373 SVal X = GetSVal((*I)->getState(), R);
2374
2375 // Check if we return the address of a stack variable.
2376 if (isa<loc::MemRegionVal>(X)) {
2377 // Determine if the value is on the stack.
2378 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002379
Ted Kremenek5917d782008-11-21 00:27:44 +00002380 if (R && getStateManager().hasStackStorage(R)) {
2381 // Create a special node representing the error.
2382 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2383 N->markAsSink();
2384 RetsStackAddr.insert(N);
2385 }
2386 continue;
2387 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002388 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002389 // Check if we return an undefined value.
2390 else if (X.isUndef()) {
2391 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2392 N->markAsSink();
2393 RetsUndef.insert(N);
2394 }
2395 continue;
2396 }
2397
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002398 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002399 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002400}
Ted Kremenek55deb972008-03-25 00:34:37 +00002401
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002402//===----------------------------------------------------------------------===//
2403// Transfer functions: Binary operators.
2404//===----------------------------------------------------------------------===//
2405
Ted Kremeneka8538d92009-02-13 01:45:31 +00002406const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002407 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002408
2409 // Divide by undefined? (potentially zero)
2410
2411 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002412 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002413
2414 if (DivUndef) {
2415 DivUndef->markAsSink();
2416 ExplicitBadDivides.insert(DivUndef);
2417 }
2418
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002419 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002420 }
2421
2422 // Check for divide/remainder-by-zero.
2423 // First, "assume" that the denominator is 0 or undefined.
2424
2425 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002426 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002427
2428 // Second, "assume" that the denominator cannot be 0.
2429
2430 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002431 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002432
2433 // Create the node for the divide-by-zero (if it occurred).
2434
2435 if (isFeasibleZero)
2436 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2437 DivZeroNode->markAsSink();
2438
2439 if (isFeasibleNotZero)
2440 ImplicitBadDivides.insert(DivZeroNode);
2441 else
2442 ExplicitBadDivides.insert(DivZeroNode);
2443
2444 }
2445
Ted Kremeneka8538d92009-02-13 01:45:31 +00002446 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002447}
2448
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002449void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002450 GRExprEngine::NodeTy* Pred,
2451 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002452
2453 NodeSet Tmp1;
2454 Expr* LHS = B->getLHS()->IgnoreParens();
2455 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002456
Ted Kremenek759623e2008-12-06 02:39:30 +00002457 // FIXME: Add proper support for ObjCKVCRefExpr.
2458 if (isa<ObjCKVCRefExpr>(LHS)) {
2459 Visit(RHS, Pred, Dst);
2460 return;
2461 }
2462
2463
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002464 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002465 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002466 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002467 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002468
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002469 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002470
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002471 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002472
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002473 // Process the RHS.
2474
2475 NodeSet Tmp2;
2476 Visit(RHS, *I1, Tmp2);
2477
2478 // With both the LHS and RHS evaluated, process the operation itself.
2479
2480 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002481
Ted Kremeneka8538d92009-02-13 01:45:31 +00002482 const GRState* state = GetState(*I2);
2483 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002484
Ted Kremeneka8538d92009-02-13 01:45:31 +00002485 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002486 BinaryOperator::Opcode Op = B->getOpcode();
2487
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002488 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002489
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002490 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002491
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002492 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002493 // FIXME: Handle structs.
2494 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002495
Ted Kremenek062e2f92008-11-13 06:10:40 +00002496 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2497 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002498 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002499 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002500
Ted Kremenek062e2f92008-11-13 06:10:40 +00002501 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002502 ? cast<SVal>(loc::SymbolVal(Sym))
2503 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002504 }
2505
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002506 // Simulate the effects of a "store": bind the value of the RHS
2507 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002508
Ted Kremeneka8538d92009-02-13 01:45:31 +00002509 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2510 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002511 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002512 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002513
2514 case BinaryOperator::Div:
2515 case BinaryOperator::Rem:
2516
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002517 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002518 if (RHS->getType()->isIntegerType() &&
2519 RHS->getType()->isScalarType()) {
2520
Ted Kremeneka8538d92009-02-13 01:45:31 +00002521 state = CheckDivideZero(B, state, *I2, RightV);
2522 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002523 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002524
2525 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002526
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002527 default: {
2528
2529 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002530 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002531
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002532 // Process non-assignements except commas or short-circuited
2533 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002534
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002535 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002536
2537 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002538 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002539 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002540 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002541 }
2542 else
2543 Dst.Add(*I2);
2544
Ted Kremenek89063af2008-02-21 19:15:37 +00002545 continue;
2546 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002547
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002548 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002549
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002550 // The operands were *not* undefined, but the result is undefined.
2551 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002552
Ted Kremeneka8538d92009-02-13 01:45:31 +00002553 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002554 UndefNode->markAsSink();
2555 UndefResults.insert(UndefNode);
2556 }
2557
2558 continue;
2559 }
2560
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002561 // Otherwise, create a new node.
2562
Ted Kremeneka8538d92009-02-13 01:45:31 +00002563 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002564 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002565 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002566 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002567
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002568 assert (B->isCompoundAssignmentOp());
2569
Ted Kremenekcad29962009-02-07 00:52:24 +00002570 switch (Op) {
2571 default:
2572 assert(0 && "Invalid opcode for compound assignment.");
2573 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2574 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2575 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2576 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2577 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2578 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2579 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2580 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2581 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2582 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002583 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002584
2585 // Perform a load (the LHS). This performs the checks for
2586 // null dereferences, and so on.
2587 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002588 SVal location = GetSVal(state, LHS);
2589 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002590
2591 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2592
Ted Kremeneka8538d92009-02-13 01:45:31 +00002593 state = GetState(*I3);
2594 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002595
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002596 // Check for divide-by-zero.
2597 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002598 && RHS->getType()->isIntegerType()
2599 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002600
2601 // CheckDivideZero returns a new state where the denominator
2602 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002603 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002604
Ted Kremeneka8538d92009-02-13 01:45:31 +00002605 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002606 continue;
2607 }
2608
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002609 // Propagate undefined values (left-side).
2610 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002611 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002612 continue;
2613 }
2614
2615 // Propagate unknown values (left and right-side).
2616 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002617 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2618 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002619 continue;
2620 }
2621
2622 // At this point:
2623 //
2624 // The LHS is not Undef/Unknown.
2625 // The RHS is not Unknown.
2626
2627 // Get the computation type.
2628 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002629 CTy = getContext().getCanonicalType(CTy);
2630
2631 QualType LTy = getContext().getCanonicalType(LHS->getType());
2632 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002633
2634 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002635 if (LTy != CTy) V = EvalCast(V, CTy);
2636 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002637
2638 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002639 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002640 // Propagate undefined values (right-side).
Ted Kremeneka8538d92009-02-13 01:45:31 +00002641 EvalStore(Dst,B, LHS, *I3, BindExpr(state, B, RightV), location,
2642 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002643 continue;
2644 }
2645
Ted Kremenek60595da2008-11-15 04:01:56 +00002646 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002647 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002648
2649 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002650 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002651 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002652 UndefNode->markAsSink();
2653 UndefResults.insert(UndefNode);
2654 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002655 continue;
2656 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002657
2658 // EXPERIMENTAL: "Conjured" symbols.
2659 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002660
2661 SVal LHSVal;
2662
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002663 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2664 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002665
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002666 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002667
Ted Kremenek60595da2008-11-15 04:01:56 +00002668 // The symbolic value is actually for the type of the left-hand side
2669 // expression, not the computation type, as this is the value the
2670 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002671 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002672 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002673 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002674 : cast<SVal>(nonloc::SymbolVal(Sym));
2675
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002676 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002677 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002678 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002679 else {
2680 // The left-hand side may bind to a different value then the
2681 // computation type.
2682 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2683 }
2684
Ted Kremeneka8538d92009-02-13 01:45:31 +00002685 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2686 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002687 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002688 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002689 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002690}
Ted Kremenekee985462008-01-16 18:18:48 +00002691
2692//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002693// Transfer-function Helpers.
2694//===----------------------------------------------------------------------===//
2695
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002696void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002697 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002698 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002699 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002700
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002701 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002702 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2703
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002704 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002705 MakeNode(Dst, Ex, Pred, *I);
2706}
2707
Ted Kremeneka8538d92009-02-13 01:45:31 +00002708void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002709 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002710 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002711
Ted Kremeneka8538d92009-02-13 01:45:31 +00002712 GRStateSet::AutoPopulate AP(OStates, state);
2713 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002714}
2715
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002716SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R) {
2717
2718 if (L.isUndef() || R.isUndef())
2719 return UndefinedVal();
2720
2721 if (L.isUnknown() || R.isUnknown())
2722 return UnknownVal();
2723
2724 if (isa<Loc>(L)) {
2725 if (isa<Loc>(R))
2726 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2727 else
2728 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2729 }
2730
2731 if (isa<Loc>(R)) {
2732 // Support pointer arithmetic where the increment/decrement operand
2733 // is on the left and the pointer on the right.
2734
2735 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2736
2737 // Commute the operands.
2738 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2739 cast<NonLoc>(L));
2740 }
2741 else
2742 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
2743 cast<NonLoc>(R));
2744}
2745
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002746//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002747// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002748//===----------------------------------------------------------------------===//
2749
Ted Kremenekaa66a322008-01-16 21:46:15 +00002750#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002751static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002752static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002753
Ted Kremenekaa66a322008-01-16 21:46:15 +00002754namespace llvm {
2755template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002756struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002757 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002758
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002759 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2760
2761 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002762 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002763 GraphPrintCheckerState->isUndefDeref(N) ||
2764 GraphPrintCheckerState->isUndefStore(N) ||
2765 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002766 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2767 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002768 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002769 GraphPrintCheckerState->isBadCall(N) ||
2770 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002771 return "color=\"red\",style=\"filled\"";
2772
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002773 if (GraphPrintCheckerState->isNoReturnCall(N))
2774 return "color=\"blue\",style=\"filled\"";
2775
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002776 return "";
2777 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002778
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002779 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002780 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002781
2782 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002783 ProgramPoint Loc = N->getLocation();
2784
2785 switch (Loc.getKind()) {
2786 case ProgramPoint::BlockEntranceKind:
2787 Out << "Block Entrance: B"
2788 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2789 break;
2790
2791 case ProgramPoint::BlockExitKind:
2792 assert (false);
2793 break;
2794
Ted Kremenekaa66a322008-01-16 21:46:15 +00002795 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002796 if (isa<PostStmt>(Loc)) {
2797 const PostStmt& L = cast<PostStmt>(Loc);
2798 Stmt* S = L.getStmt();
2799 SourceLocation SLoc = S->getLocStart();
2800
2801 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2802 llvm::raw_os_ostream OutS(Out);
2803 S->printPretty(OutS);
2804 OutS.flush();
2805
2806 if (SLoc.isFileID()) {
2807 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002808 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2809 << " col="
2810 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2811 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00002812 }
2813
2814 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2815 Out << "\\|Implicit-Null Dereference.\\l";
2816 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2817 Out << "\\|Explicit-Null Dereference.\\l";
2818 else if (GraphPrintCheckerState->isUndefDeref(N))
2819 Out << "\\|Dereference of undefialied value.\\l";
2820 else if (GraphPrintCheckerState->isUndefStore(N))
2821 Out << "\\|Store to Undefined Loc.";
2822 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2823 Out << "\\|Explicit divide-by zero or undefined value.";
2824 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2825 Out << "\\|Implicit divide-by zero or undefined value.";
2826 else if (GraphPrintCheckerState->isUndefResult(N))
2827 Out << "\\|Result of operation is undefined.";
2828 else if (GraphPrintCheckerState->isNoReturnCall(N))
2829 Out << "\\|Call to function marked \"noreturn\".";
2830 else if (GraphPrintCheckerState->isBadCall(N))
2831 Out << "\\|Call to NULL/Undefined.";
2832 else if (GraphPrintCheckerState->isUndefArg(N))
2833 Out << "\\|Argument in call is undefined";
2834
2835 break;
2836 }
2837
Ted Kremenekaa66a322008-01-16 21:46:15 +00002838 const BlockEdge& E = cast<BlockEdge>(Loc);
2839 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2840 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002841
2842 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002843
2844 SourceLocation SLoc = T->getLocStart();
2845
Ted Kremenekb38911f2008-01-30 23:03:39 +00002846 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002847
Ted Kremeneka95d3752008-09-13 05:16:45 +00002848 llvm::raw_os_ostream OutS(Out);
2849 E.getSrc()->printTerminator(OutS);
2850 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002851
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002852 if (SLoc.isFileID()) {
2853 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002854 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2855 << " col="
2856 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002857 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002858
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002859 if (isa<SwitchStmt>(T)) {
2860 Stmt* Label = E.getDst()->getLabel();
2861
2862 if (Label) {
2863 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2864 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002865 llvm::raw_os_ostream OutS(Out);
2866 C->getLHS()->printPretty(OutS);
2867 OutS.flush();
2868
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002869 if (Stmt* RHS = C->getRHS()) {
2870 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002871 RHS->printPretty(OutS);
2872 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002873 }
2874
2875 Out << ":";
2876 }
2877 else {
2878 assert (isa<DefaultStmt>(Label));
2879 Out << "\\ldefault:";
2880 }
2881 }
2882 else
2883 Out << "\\l(implicit) default:";
2884 }
2885 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002886 // FIXME
2887 }
2888 else {
2889 Out << "\\lCondition: ";
2890 if (*E.getSrc()->succ_begin() == E.getDst())
2891 Out << "true";
2892 else
2893 Out << "false";
2894 }
2895
2896 Out << "\\l";
2897 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002898
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002899 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2900 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002901 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002902 }
2903 }
2904
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002905 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002906
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002907 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2908 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002909
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002910 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002911 return Out.str();
2912 }
2913};
2914} // end llvm namespace
2915#endif
2916
Ted Kremenekffe0f432008-03-07 22:58:01 +00002917#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002918
2919template <typename ITERATOR>
2920GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2921
2922template <>
2923GRExprEngine::NodeTy*
2924GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2925 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2926 return I->first;
2927}
2928
Ted Kremenekffe0f432008-03-07 22:58:01 +00002929template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002930static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002931 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002932
Ted Kremenekd4527582008-09-16 18:44:52 +00002933 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002934
2935 for ( ; I != E; ++I ) {
2936 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002937 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002938
Ted Kremenekd4527582008-09-16 18:44:52 +00002939 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002940 continue;
2941
Ted Kremenekd4527582008-09-16 18:44:52 +00002942 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002943 Sources.push_back(N);
2944 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002945}
2946#endif
2947
2948void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002949#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002950 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002951 std::vector<NodeTy*> Src;
2952
Ted Kremenekcf118d42009-02-04 23:49:09 +00002953 // FIXME: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002954 AddSources(Src, null_derefs_begin(), null_derefs_end());
2955 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2956 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2957 AddSources(Src, undef_results_begin(), undef_results_end());
2958 AddSources(Src, bad_calls_begin(), bad_calls_end());
2959 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002960 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002961
Ted Kremenekcf118d42009-02-04 23:49:09 +00002962 // FIXME: Enhance BugReporter to have a clean way to query if a node
2963 // is involved in an error... and what kind.
Ted Kremenekcb612922008-04-18 19:23:43 +00002964
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002965 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002966 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002967 else {
2968 GraphPrintCheckerState = this;
2969 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002970
Ted Kremenekffe0f432008-03-07 22:58:01 +00002971 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002972
2973 GraphPrintCheckerState = NULL;
2974 GraphPrintSourceManager = NULL;
2975 }
2976#endif
2977}
2978
2979void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2980#ifndef NDEBUG
2981 GraphPrintCheckerState = this;
2982 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002983
Ted Kremenekcf118d42009-02-04 23:49:09 +00002984 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00002985
Ted Kremenekcf118d42009-02-04 23:49:09 +00002986 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00002987 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00002988 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00002989 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00002990
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002991 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002992 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002993#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002994}