blob: 6a7f86b14bf00d7484a7e3931a4435e19badd115 [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 Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000020#include "llvm/ADT/ImmutableList.h"
21#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Zhongxing Xuff944a82008-12-22 01:52:37 +0000118 LiveVariables& L, bool purgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000119 StoreManagerCreator SMC,
120 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000121 : CoreEngine(cfg, CD, Ctx, *this),
122 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000123 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000125 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000127 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
129 RaiseSel(GetNullarySelector("raise", G.getContext())),
130 PurgeDead(purgeDead){}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000131
Ted Kremenek1a654b62008-06-20 21:45:25 +0000132GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000133 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
134 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000135
Ted Kremeneke448ab42008-05-01 18:33:28 +0000136
137 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000138}
139
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000140//===----------------------------------------------------------------------===//
141// Utility methods.
142//===----------------------------------------------------------------------===//
143
144// SaveAndRestore - A utility class that uses RIIA to save and restore
145// the value of a variable.
146template<typename T>
147struct VISIBILITY_HIDDEN SaveAndRestore {
148 SaveAndRestore(T& x) : X(x), old_value(x) {}
149 ~SaveAndRestore() { X = old_value; }
150 T get() { return old_value; }
151
152 T& X;
153 T old_value;
154};
155
Ted Kremenek186350f2008-04-23 20:12:28 +0000156// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
157// value of a variable is saved, and during the dstor the old value is
158// or'ed with the new value.
159struct VISIBILITY_HIDDEN SaveOr {
160 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
161 ~SaveOr() { X |= old_value; }
162
163 bool& X;
164 bool old_value;
165};
166
167
Ted Kremenekc0959972008-07-02 21:24:01 +0000168void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000169 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000170 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000171 (*I)->EmitWarnings(BR);
172 }
173
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000174 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000175 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000176 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000177 }
178}
179
180void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000181 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000182 tf->RegisterChecks(*this);
183 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000184}
185
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000186void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
187 if (!BatchAuditor)
188 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
189
190 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000191}
192
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000193const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000194 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000195}
196
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000197//===----------------------------------------------------------------------===//
198// Top-level transfer function logic (Dispatcher).
199//===----------------------------------------------------------------------===//
200
201void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
202
203 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000213
Ted Kremenek241677a2009-01-21 22:26:05 +0000214
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000215 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000216 SymbolReaper SymReaper(Liveness, SymMgr);
217 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
218 CurrentStmt, SymReaper)
219 : EntryNode->getState();
220
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000223
Ted Kremenek241677a2009-01-21 22:26:05 +0000224 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000225 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000226 else {
227 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000228 SaveOr OldHasGen(Builder->HasGeneratedNode);
229
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000230 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
231 Builder->PurgingDeadSymbols = true;
232
Ted Kremenek729a9a22008-07-17 23:15:45 +0000233 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000234 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
237 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 bool HasAutoGenerated = false;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243
244 NodeSet Dst;
245
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000246 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000247 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
248
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000249 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000250 Visit(S, *I, Dst);
251
252 // Do we need to auto-generate a node? We only need to do this to generate
253 // a node with a "cleaned" state; GRCoreEngine will actually handle
254 // auto-transitions for other cases.
255 if (Dst.size() == 1 && *Dst.begin() == EntryNode
256 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
257 HasAutoGenerated = true;
258 builder.generateNode(S, GetState(EntryNode), *I);
259 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000260 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000261
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000263 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000264 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000265
266 // FIXME: Consolidate.
267 StateMgr.CurrentStmt = 0;
268 CurrentStmt = 0;
269
Ted Kremenek846d4e92008-04-24 23:35:58 +0000270 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000271}
272
273void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
274
275 // FIXME: add metadata to the CFG so that we can disable
276 // this check when we KNOW that there is no block-level subexpression.
277 // The motivation is that this check requires a hashtable lookup.
278
279 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
280 Dst.Add(Pred);
281 return;
282 }
283
284 switch (S->getStmtClass()) {
285
286 default:
287 // Cases we intentionally have "default" handle:
288 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
289
290 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
291 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000292
293 case Stmt::ArraySubscriptExprClass:
294 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
295 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000296
297 case Stmt::AsmStmtClass:
298 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
299 break;
300
301 case Stmt::BinaryOperatorClass: {
302 BinaryOperator* B = cast<BinaryOperator>(S);
303
304 if (B->isLogicalOp()) {
305 VisitLogicalExpr(B, Pred, Dst);
306 break;
307 }
308 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000309 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000310 MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000311 break;
312 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000313
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000314 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
315 break;
316 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000317
Douglas Gregorb4609802008-11-14 16:09:21 +0000318 case Stmt::CallExprClass:
319 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000320 CallExpr* C = cast<CallExpr>(S);
321 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000322 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000323 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000324
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000325 // FIXME: ChooseExpr is really a constant. We need to fix
326 // the CFG do not model them as explicit control-flow.
327
328 case Stmt::ChooseExprClass: { // __builtin_choose_expr
329 ChooseExpr* C = cast<ChooseExpr>(S);
330 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
331 break;
332 }
333
334 case Stmt::CompoundAssignOperatorClass:
335 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
336 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000337
338 case Stmt::CompoundLiteralExprClass:
339 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
340 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000341
342 case Stmt::ConditionalOperatorClass: { // '?' operator
343 ConditionalOperator* C = cast<ConditionalOperator>(S);
344 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
345 break;
346 }
347
348 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000349 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000350 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000351 break;
352
353 case Stmt::DeclStmtClass:
354 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
355 break;
356
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000357 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000358 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000359 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 VisitCast(C, C->getSubExpr(), Pred, Dst);
361 break;
362 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000363
364 case Stmt::InitListExprClass:
365 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
366 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000368 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000369 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
370 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000371
372 case Stmt::ObjCIvarRefExprClass:
373 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
374 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000375
376 case Stmt::ObjCForCollectionStmtClass:
377 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
378 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000379
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000380 case Stmt::ObjCMessageExprClass: {
381 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
382 break;
383 }
384
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000385 case Stmt::ObjCAtThrowStmtClass: {
386 // FIXME: This is not complete. We basically treat @throw as
387 // an abort.
388 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
389 Builder->BuildSinks = true;
390 MakeNode(Dst, S, Pred, GetState(Pred));
391 break;
392 }
393
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000394 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000395 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396 break;
397
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000398 case Stmt::ReturnStmtClass:
399 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
400 break;
401
Sebastian Redl05189992008-11-11 17:56:53 +0000402 case Stmt::SizeOfAlignOfExprClass:
403 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000404 break;
405
406 case Stmt::StmtExprClass: {
407 StmtExpr* SE = cast<StmtExpr>(S);
408
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000409 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000410
411 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
412 // probably just remove these from the CFG.
413 assert (!SE->getSubStmt()->body_empty());
414
415 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000416 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000417 else
418 Dst.Add(Pred);
419
420 break;
421 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000422
423 case Stmt::StringLiteralClass:
424 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
425 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000426
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000427 case Stmt::UnaryOperatorClass:
428 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000429 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000430 }
431}
432
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000433void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000434
435 Ex = Ex->IgnoreParens();
436
437 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
438 Dst.Add(Pred);
439 return;
440 }
441
442 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000443
444 case Stmt::ArraySubscriptExprClass:
445 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
446 return;
447
448 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000449 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000450 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
451 return;
452
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000453 case Stmt::ObjCIvarRefExprClass:
454 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
455 return;
456
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000457 case Stmt::UnaryOperatorClass:
458 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
459 return;
460
461 case Stmt::MemberExprClass:
462 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
463 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000464
Ted Kremenek4f090272008-10-27 21:54:31 +0000465 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000466 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000467 return;
468
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000469 case Stmt::ObjCPropertyRefExprClass:
470 // FIXME: Property assignments are lvalues, but not really "locations".
471 // e.g.: self.x = something;
472 // Here the "self.x" really can translate to a method call (setter) when
473 // the assignment is made. Moreover, the entire assignment expression
474 // evaluate to whatever "something" is, not calling the "getter" for
475 // the property (which would make sense since it can have side effects).
476 // We'll probably treat this as a location, but not one that we can
477 // take the address of. Perhaps we need a new SVal class for cases
478 // like thsis?
479 // Note that we have a similar problem for bitfields, since they don't
480 // have "locations" in the sense that we can take their address.
481 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000482 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000483
484 case Stmt::StringLiteralClass: {
485 const GRState* St = GetState(Pred);
486 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000487 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000488 return;
489 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000490
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000491 default:
492 // Arbitrary subexpressions can return aggregate temporaries that
493 // can be used in a lvalue context. We need to enhance our support
494 // of such temporaries in both the environment and the store, so right
495 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000496 assert ((Ex->getType()->isAggregateType() ||
497 Ex->getType()->isUnionType()) &&
498 "Other kinds of expressions with non-aggregate/union types do"
499 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000500
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000501 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000502 }
503}
504
505//===----------------------------------------------------------------------===//
506// Block entrance. (Update counters).
507//===----------------------------------------------------------------------===//
508
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000509bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000510 GRBlockCounter BC) {
511
512 return BC.getNumVisited(B->getBlockID()) < 3;
513}
514
515//===----------------------------------------------------------------------===//
516// Branch processing.
517//===----------------------------------------------------------------------===//
518
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000519const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000520 Stmt* Terminator,
521 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000522
523 switch (Terminator->getStmtClass()) {
524 default:
525 return St;
526
527 case Stmt::BinaryOperatorClass: { // '&&' and '||'
528
529 BinaryOperator* B = cast<BinaryOperator>(Terminator);
530 BinaryOperator::Opcode Op = B->getOpcode();
531
532 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
533
534 // For &&, if we take the true branch, then the value of the whole
535 // expression is that of the RHS expression.
536 //
537 // For ||, if we take the false branch, then the value of the whole
538 // expression is that of the RHS expression.
539
540 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
541 (Op == BinaryOperator::LOr && !branchTaken)
542 ? B->getRHS() : B->getLHS();
543
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000544 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000545 }
546
547 case Stmt::ConditionalOperatorClass: { // ?:
548
549 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
550
551 // For ?, if branchTaken == true then the value is either the LHS or
552 // the condition itself. (GNU extension).
553
554 Expr* Ex;
555
556 if (branchTaken)
557 Ex = C->getLHS() ? C->getLHS() : C->getCond();
558 else
559 Ex = C->getRHS();
560
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000561 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000562 }
563
564 case Stmt::ChooseExprClass: { // ?:
565
566 ChooseExpr* C = cast<ChooseExpr>(Terminator);
567
568 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000569 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000570 }
571 }
572}
573
Ted Kremenekaf337412008-11-12 19:24:17 +0000574void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000575 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000576
Ted Kremeneke7d22112008-02-11 19:21:59 +0000577 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000578 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000579 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000580
Ted Kremenekb2331832008-02-15 22:29:00 +0000581 // Check for NULL conditions; e.g. "for(;;)"
582 if (!Condition) {
583 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000584 return;
585 }
586
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000587 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000588
589 switch (V.getBaseKind()) {
590 default:
591 break;
592
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000593 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000594 builder.generateNode(MarkBranch(PrevState, Term, true), true);
595 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000596 return;
597
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000598 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000599 NodeTy* N = builder.generateNode(PrevState, true);
600
601 if (N) {
602 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000603 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000604 }
605
606 builder.markInfeasible(false);
607 return;
608 }
609 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000610
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000611 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000612
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000613 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000614 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000615
616 if (isFeasible)
617 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000618 else
619 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000620
621 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000622
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000623 isFeasible = false;
624 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000625
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000626 if (isFeasible)
627 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000628 else
629 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000630}
631
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000632/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000633/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000634void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000635
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000636 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000637 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000638
639 // Three possibilities:
640 //
641 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000642 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000643 // (3) We have no clue about the label. Dispatch to all targets.
644 //
645
646 typedef IndirectGotoNodeBuilder::iterator iterator;
647
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000648 if (isa<loc::GotoLabel>(V)) {
649 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000650
651 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000652 if (I.getLabel() == L) {
653 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000654 return;
655 }
656 }
657
658 assert (false && "No block with label.");
659 return;
660 }
661
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000662 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000663 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000664 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000665 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000666 return;
667 }
668
669 // This is really a catch-all. We don't support symbolics yet.
670
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000671 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000672
673 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000674 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000675}
Ted Kremenekf233d482008-02-05 00:26:40 +0000676
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000677
678void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
679 NodeTy* Pred, NodeSet& Dst) {
680
681 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
682
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000683 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000684 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000685
686 assert (X.isUndef());
687
688 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
689
690 assert (SE);
691
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000692 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000693
694 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000695 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000696}
697
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000698/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
699/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000700void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
701 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000702 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000703 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000704 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000705
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000706 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000707 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000708 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000709 return;
710 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000711
Ted Kremenek72afb372009-01-17 01:54:16 +0000712 const GRState* DefaultSt = St;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000713 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000714
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000715 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000716 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000717
718 // Evaluate the LHS of the case value.
719 Expr::EvalResult V1;
720 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000721
Ted Kremenek72afb372009-01-17 01:54:16 +0000722 // Sanity checks. These go away in Release builds.
723 assert(b && V1.Val.isInt() && !V1.HasSideEffects
724 && "Case condition must evaluate to an integer constant.");
725 b = b; // silence unused variable warning
726 assert(V1.Val.getInt().getBitWidth() ==
727 getContext().getTypeSize(CondE->getType()));
728
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000729 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000730 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000731
732 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000733 b = E->Evaluate(V2, getContext());
734 assert(b && V2.Val.isInt() && !V2.HasSideEffects
735 && "Case condition must evaluate to an integer constant.");
736 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000737 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000738 else
739 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000740
741 // FIXME: Eventually we should replace the logic below with a range
742 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000743 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000744
Ted Kremenek14a11402008-03-17 22:17:56 +0000745 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000746 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000747 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000748
Ted Kremenek72afb372009-01-17 01:54:16 +0000749 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000750 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000751 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000752
753 if (isFeasible) {
754 builder.generateCaseStmtNode(I, StNew);
755
756 // If CondV evaluates to a constant, then we know that this
757 // is the *only* case that we can take, so stop evaluating the
758 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000759 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000760 return;
761 }
762
763 // Now "assume" that the case doesn't match. Add this state
764 // to the default state (if it is feasible).
765
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000766 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000767 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000768
Ted Kremenek5014ab12008-04-23 05:03:18 +0000769 if (isFeasible) {
770 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000771 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000772 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000773
Ted Kremenek14a11402008-03-17 22:17:56 +0000774 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000775 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000776 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777
Ted Kremenek72afb372009-01-17 01:54:16 +0000778 ++V1.Val.getInt();
779 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000780
781 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000782 }
783
784 // If we reach here, than we know that the default branch is
785 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000786 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000787}
788
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000789//===----------------------------------------------------------------------===//
790// Transfer functions: logical operations ('&&', '||').
791//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000792
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000793void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000794 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000795
Ted Kremenek05a23782008-02-26 19:05:15 +0000796 assert (B->getOpcode() == BinaryOperator::LAnd ||
797 B->getOpcode() == BinaryOperator::LOr);
798
799 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
800
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000801 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000802 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000803
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000804 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000805
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000806 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000807
808 assert (Ex);
809
810 if (Ex == B->getRHS()) {
811
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000812 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000813
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000814 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000815
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000816 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000817 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000818 return;
819 }
820
Ted Kremenek05a23782008-02-26 19:05:15 +0000821 // We took the RHS. Because the value of the '&&' or '||' expression must
822 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
823 // or 1. Alternatively, we could take a lazy approach, and calculate this
824 // value later when necessary. We don't have the machinery in place for
825 // this right now, and since most logical expressions are used for branches,
826 // the payoff is not likely to be large. Instead, we do eager evaluation.
827
828 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000829 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000830
831 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000832 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000833 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000834
835 isFeasible = false;
836 NewState = Assume(St, X, false, isFeasible);
837
838 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000839 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000840 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000841 }
842 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000843 // We took the LHS expression. Depending on whether we are '&&' or
844 // '||' we know what the value of the expression is via properties of
845 // the short-circuiting.
846
847 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000848 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000849 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000850}
Ted Kremenek05a23782008-02-26 19:05:15 +0000851
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000852//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000853// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000854//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000855
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000856void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
857 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000858
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000859 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000860
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000861 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000862
863 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
864
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000865 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000866
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000867 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000868 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000869 else
870 EvalLoad(Dst, Ex, Pred, St, V);
871 return;
872
873 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
874 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
875
876 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000877 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000878 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000879 return;
880
881 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000882 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000883 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000884 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000885 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000886 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000887
888 assert (false &&
889 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000890}
891
Ted Kremenek540cbe22008-04-22 04:56:29 +0000892/// VisitArraySubscriptExpr - Transfer function for array accesses
893void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000894 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000895
896 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000897 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000898 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000899 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000900
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000901 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000902 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000903 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000904
905 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000906 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000907 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000908
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000909 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000910 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000911 else
912 EvalLoad(Dst, A, *I2, St, V);
913 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000914 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000915}
916
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000917/// VisitMemberExpr - Transfer function for member expressions.
918void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000919 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000920
921 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000922 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000923
924 if (M->isArrow())
925 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
926 else
927 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
928
Douglas Gregor86f19402008-12-20 23:49:58 +0000929 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
930 if (!Field) // FIXME: skipping member expressions for non-fields
931 return;
932
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000933 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000934 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000935 // FIXME: Should we insert some assumption logic in here to determine
936 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000937 // later when using FieldOffset lvals (which we no longer have).
938 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000939
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000940 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000941 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000942 else
943 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000944 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000945}
946
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000947void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000948 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000949
950 assert (Builder && "GRStmtNodeBuilder must be defined.");
951
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000952 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8c354752008-12-16 22:02:27 +0000953 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000954
Ted Kremenek8c354752008-12-16 22:02:27 +0000955 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000956 return;
957
Ted Kremenek8c354752008-12-16 22:02:27 +0000958 St = GetState(Pred);
959
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960 // Proceed with the store.
961
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000962 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000963
Ted Kremenek186350f2008-04-23 20:12:28 +0000964 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000965 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000966 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000967
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000968 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000969 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000970
Ted Kremenek729a9a22008-07-17 23:15:45 +0000971 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000972
973 // Handle the case where no nodes where generated. Auto-generate that
974 // contains the updated state if we aren't generating sinks.
975
Ted Kremenekb0533962008-04-18 20:35:30 +0000976 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000977 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978 location, Val);
979}
980
981void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000982 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000983 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000984
Ted Kremenek8c354752008-12-16 22:02:27 +0000985 // Evaluate the location (checks for bad dereferences).
986 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000987
Ted Kremenek8c354752008-12-16 22:02:27 +0000988 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000989 return;
990
Ted Kremenek8c354752008-12-16 22:02:27 +0000991 St = GetState(Pred);
992
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000993 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000994 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000995
996 // FIXME: Currently symbolic analysis "generates" new symbols
997 // for the contents of values. We need a better approach.
998
999 // FIXME: The "CheckOnly" option exists only because Array and Field
1000 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001001 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +00001002
Ted Kremenek8c354752008-12-16 22:02:27 +00001003 if (CheckOnly) {
1004 Dst.Add(Pred);
1005 return;
1006 }
1007
1008 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001009 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001010 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001011 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001012 else {
1013 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
1014 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1015 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001016}
1017
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001018void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001019 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001020
1021 NodeSet TmpDst;
1022 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1023
1024 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1025 MakeNode(Dst, Ex, *I, (*I)->getState());
1026}
1027
Ted Kremenek8c354752008-12-16 22:02:27 +00001028GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
1029 const GRState* St,
1030 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001031
1032 // Check for loads/stores from/to undefined values.
1033 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001034 NodeTy* N =
1035 Builder->generateNode(Ex, St, Pred,
1036 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001037
Ted Kremenek8c354752008-12-16 22:02:27 +00001038 if (N) {
1039 N->markAsSink();
1040 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001041 }
1042
Ted Kremenek8c354752008-12-16 22:02:27 +00001043 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001044 }
1045
1046 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1047 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001048 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001049
1050 // During a load, one of two possible situations arise:
1051 // (1) A crash, because the location (pointer) was NULL.
1052 // (2) The location (pointer) is not NULL, and the dereference works.
1053 //
1054 // We add these assumptions.
1055
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001056 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001057
1058 // "Assume" that the pointer is not NULL.
1059
1060 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001061 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001062
1063 // "Assume" that the pointer is NULL.
1064
1065 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001066 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1067 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001068
1069 if (isFeasibleNull) {
1070
Ted Kremenek7360fda2008-09-18 23:09:54 +00001071 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001072 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001073 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1074
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001075 // We don't use "MakeNode" here because the node will be a sink
1076 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001077 NodeTy* NullNode =
1078 Builder->generateNode(Ex, StNull, Pred,
1079 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001080
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001081 if (NullNode) {
1082
1083 NullNode->markAsSink();
1084
1085 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1086 else ExplicitNullDeref.insert(NullNode);
1087 }
1088 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001089
1090 if (!isFeasibleNotNull)
1091 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001092
1093 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001094 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001095 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1096 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1097 // Get the index of the accessed element.
1098 SVal Idx = ER->getIndex();
1099 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001100 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1101 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001102
1103 bool isFeasibleInBound = false;
1104 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1105 true, isFeasibleInBound);
1106
1107 bool isFeasibleOutBound = false;
1108 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1109 false, isFeasibleOutBound);
1110
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001111 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001112 // Report warning. Make sink node manually.
1113 NodeTy* OOBNode =
1114 Builder->generateNode(Ex, StOutBound, Pred,
1115 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001116
1117 if (OOBNode) {
1118 OOBNode->markAsSink();
1119
1120 if (isFeasibleInBound)
1121 ImplicitOOBMemAccesses.insert(OOBNode);
1122 else
1123 ExplicitOOBMemAccesses.insert(OOBNode);
1124 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001125 }
1126
Ted Kremenek8c354752008-12-16 22:02:27 +00001127 if (!isFeasibleInBound)
1128 return 0;
1129
1130 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001131 }
1132 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001133
Ted Kremenek8c354752008-12-16 22:02:27 +00001134 // Generate a new node indicating the checks succeed.
1135 return Builder->generateNode(Ex, StNotNull, Pred,
1136 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001137}
1138
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001139//===----------------------------------------------------------------------===//
1140// Transfer function: Function calls.
1141//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001142void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001143 CallExpr::arg_iterator AI,
1144 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001145 NodeSet& Dst)
1146{
1147 // Determine the type of function we're calling (if available).
1148 const FunctionTypeProto *Proto = NULL;
1149 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1150 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1151 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1152
1153 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1154}
1155
1156void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1157 CallExpr::arg_iterator AI,
1158 CallExpr::arg_iterator AE,
1159 NodeSet& Dst, const FunctionTypeProto *Proto,
1160 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001161
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001162 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001163 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001164 // If the call argument is being bound to a reference parameter,
1165 // visit it as an lvalue, not an rvalue.
1166 bool VisitAsLvalue = false;
1167 if (Proto && ParamIdx < Proto->getNumArgs())
1168 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1169
1170 NodeSet DstTmp;
1171 if (VisitAsLvalue)
1172 VisitLValue(*AI, Pred, DstTmp);
1173 else
1174 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001175 ++AI;
1176
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001177 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001178 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001179
1180 return;
1181 }
1182
1183 // If we reach here we have processed all of the arguments. Evaluate
1184 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001185
Ted Kremenek994a09b2008-02-25 21:16:03 +00001186 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001187 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001188
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001189 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001190
Ted Kremenekde434242008-02-19 01:44:53 +00001191 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001192 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1193
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001194 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001195 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001196
Ted Kremeneka1354a52008-03-03 16:47:31 +00001197 // FIXME: Add support for symbolic function calls (calls involving
1198 // function pointer values that are symbolic).
1199
1200 // Check for undefined control-flow or calls to NULL.
1201
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001202 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001203 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001204
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001205 if (N) {
1206 N->markAsSink();
1207 BadCalls.insert(N);
1208 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001209
Ted Kremenekde434242008-02-19 01:44:53 +00001210 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001211 }
1212
1213 // Check for the "noreturn" attribute.
1214
1215 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1216
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001217 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001218
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001219 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001220
1221 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001222 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001223 else {
1224 // HACK: Some functions are not marked noreturn, and don't return.
1225 // Here are a few hardwired ones. If this takes too long, we can
1226 // potentially cache these results.
1227 const char* s = FD->getIdentifier()->getName();
1228 unsigned n = strlen(s);
1229
1230 switch (n) {
1231 default:
1232 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001233
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001234 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001235 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1236 break;
1237
1238 case 5:
1239 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001240 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001241 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001242 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001243 // FIXME: use Assume to inspect the possible symbolic value of
1244 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001245 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001246 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001247 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001248 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001249 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001250 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001251
1252 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001253 if (!memcmp(s, "Assert", 6)) {
1254 Builder->BuildSinks = true;
1255 break;
1256 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001257
1258 // FIXME: This is just a wrapper around throwing an exception.
1259 // Eventually inter-procedural analysis should handle this easily.
1260 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1261
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001262 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001263
1264 case 7:
1265 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1266 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001267
Ted Kremenekf47bb782008-04-30 17:54:04 +00001268 case 8:
1269 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1270 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001271
1272 case 12:
1273 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1274 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001275
Ted Kremenekf9683082008-09-19 02:30:47 +00001276 case 13:
1277 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1278 break;
1279
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001280 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001281 if (!memcmp(s, "dtrace_assfail", 14) ||
1282 !memcmp(s, "yy_fatal_error", 14))
1283 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001284 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001285
1286 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001287 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1288 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001289 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001290
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001291 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001292 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001293
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001294 }
1295 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001296
1297 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001298
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001299 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001300
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001301 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001302
Ted Kremenek186350f2008-04-23 20:12:28 +00001303 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001304 switch (id) {
1305 case Builtin::BI__builtin_expect: {
1306 // For __builtin_expect, just return the value of the subexpression.
1307 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001308 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001309 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001310 continue;
1311 }
1312
Ted Kremenekb3021332008-11-02 00:35:01 +00001313 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001314 // FIXME: Refactor into StoreManager itself?
1315 MemRegionManager& RM = getStateManager().getRegionManager();
1316 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001317 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001318
1319 // Set the extent of the region in bytes. This enables us to use the
1320 // SVal of the argument directly. If we save the extent in bits, we
1321 // cannot represent values like symbol*8.
1322 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1323 St = getStoreManager().setExtent(St, R, Extent);
1324
Ted Kremenekb3021332008-11-02 00:35:01 +00001325 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1326 continue;
1327 }
1328
Ted Kremenek55aea312008-03-05 22:59:42 +00001329 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001330 break;
1331 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001332 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001333
Ted Kremenek186350f2008-04-23 20:12:28 +00001334 // Check any arguments passed-by-value against being undefined.
1335
1336 bool badArg = false;
1337
1338 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1339 I != E; ++I) {
1340
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001341 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001342 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001343
Ted Kremenek186350f2008-04-23 20:12:28 +00001344 if (N) {
1345 N->markAsSink();
1346 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001347 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001348
Ted Kremenek186350f2008-04-23 20:12:28 +00001349 badArg = true;
1350 break;
1351 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001352 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001353
1354 if (badArg)
1355 continue;
1356
1357 // Dispatch to the plug-in transfer function.
1358
1359 unsigned size = Dst.size();
1360 SaveOr OldHasGen(Builder->HasGeneratedNode);
1361 EvalCall(Dst, CE, L, *DI);
1362
1363 // Handle the case where no nodes where generated. Auto-generate that
1364 // contains the updated state if we aren't generating sinks.
1365
1366 if (!Builder->BuildSinks && Dst.size() == size &&
1367 !Builder->HasGeneratedNode)
1368 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001369 }
1370}
1371
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001372//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001373// Transfer function: Objective-C ivar references.
1374//===----------------------------------------------------------------------===//
1375
1376void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1377 NodeTy* Pred, NodeSet& Dst,
1378 bool asLValue) {
1379
1380 Expr* Base = cast<Expr>(Ex->getBase());
1381 NodeSet Tmp;
1382 Visit(Base, Pred, Tmp);
1383
1384 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1385 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001386 SVal BaseVal = GetSVal(St, Base);
1387 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001388
1389 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001390 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001391 else
1392 EvalLoad(Dst, Ex, *I, St, location);
1393 }
1394}
1395
1396//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001397// Transfer function: Objective-C fast enumeration 'for' statements.
1398//===----------------------------------------------------------------------===//
1399
1400void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1401 NodeTy* Pred, NodeSet& Dst) {
1402
1403 // ObjCForCollectionStmts are processed in two places. This method
1404 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1405 // statements within a basic block. This transfer function does two things:
1406 //
1407 // (1) binds the next container value to 'element'. This creates a new
1408 // node in the ExplodedGraph.
1409 //
1410 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1411 // whether or not the container has any more elements. This value
1412 // will be tested in ProcessBranch. We need to explicitly bind
1413 // this value because a container can contain nil elements.
1414 //
1415 // FIXME: Eventually this logic should actually do dispatches to
1416 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1417 // This will require simulating a temporary NSFastEnumerationState, either
1418 // through an SVal or through the use of MemRegions. This value can
1419 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1420 // terminates we reclaim the temporary (it goes out of scope) and we
1421 // we can test if the SVal is 0 or if the MemRegion is null (depending
1422 // on what approach we take).
1423 //
1424 // For now: simulate (1) by assigning either a symbol or nil if the
1425 // container is empty. Thus this transfer function will by default
1426 // result in state splitting.
1427
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001428 Stmt* elem = S->getElement();
1429 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001430
1431 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001432 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001433 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001434 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1435 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1436 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001437 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001438
1439 NodeSet Tmp;
1440 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001441
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001442 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1443 const GRState* state = GetState(*I);
1444 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1445 }
1446}
1447
1448void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1449 NodeTy* Pred, NodeSet& Dst,
1450 SVal ElementV) {
1451
1452
Ted Kremenekaf337412008-11-12 19:24:17 +00001453
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001454 // Get the current state. Use 'EvalLocation' to determine if it is a null
1455 // pointer, etc.
1456 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001457
Ted Kremenek8c354752008-12-16 22:02:27 +00001458 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1459 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001460 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001461
1462 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001463
Ted Kremenekaf337412008-11-12 19:24:17 +00001464 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001465 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001466 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1467 GRStateRef hasElems = state.BindExpr(S, TrueV);
1468
Ted Kremenekaf337412008-11-12 19:24:17 +00001469 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001470 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1471 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001472
1473 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1474 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1475 // FIXME: The proper thing to do is to really iterate over the
1476 // container. We will do this with dispatch logic to the store.
1477 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001478 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001479 assert (Loc::IsLocType(T));
1480 unsigned Count = Builder->getCurrentBlockCount();
1481 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1482 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001483
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001484 // Bind the location to 'nil' on the false branch.
1485 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1486 noElems = noElems.BindLoc(ElementV, nilV);
1487 }
1488
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001489 // Create the new nodes.
1490 MakeNode(Dst, S, Pred, hasElems);
1491 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001492}
1493
1494//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001495// Transfer function: Objective-C message expressions.
1496//===----------------------------------------------------------------------===//
1497
1498void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1499 NodeSet& Dst){
1500
1501 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1502 Pred, Dst);
1503}
1504
1505void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001506 ObjCMessageExpr::arg_iterator AI,
1507 ObjCMessageExpr::arg_iterator AE,
1508 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001509 if (AI == AE) {
1510
1511 // Process the receiver.
1512
1513 if (Expr* Receiver = ME->getReceiver()) {
1514 NodeSet Tmp;
1515 Visit(Receiver, Pred, Tmp);
1516
1517 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1518 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1519
1520 return;
1521 }
1522
1523 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1524 return;
1525 }
1526
1527 NodeSet Tmp;
1528 Visit(*AI, Pred, Tmp);
1529
1530 ++AI;
1531
1532 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1533 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1534}
1535
1536void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1537 NodeTy* Pred,
1538 NodeSet& Dst) {
1539
1540 // FIXME: More logic for the processing the method call.
1541
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001542 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001543 bool RaisesException = false;
1544
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001545
1546 if (Expr* Receiver = ME->getReceiver()) {
1547
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001548 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001549
1550 // Check for undefined control-flow or calls to NULL.
1551
1552 if (L.isUndef()) {
1553 NodeTy* N = Builder->generateNode(ME, St, Pred);
1554
1555 if (N) {
1556 N->markAsSink();
1557 UndefReceivers.insert(N);
1558 }
1559
1560 return;
1561 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001562
1563 // Check if the "raise" message was sent.
1564 if (ME->getSelector() == RaiseSel)
1565 RaisesException = true;
1566 }
1567 else {
1568
1569 IdentifierInfo* ClsName = ME->getClassName();
1570 Selector S = ME->getSelector();
1571
1572 // Check for special instance methods.
1573
1574 if (!NSExceptionII) {
1575 ASTContext& Ctx = getContext();
1576
1577 NSExceptionII = &Ctx.Idents.get("NSException");
1578 }
1579
1580 if (ClsName == NSExceptionII) {
1581
1582 enum { NUM_RAISE_SELECTORS = 2 };
1583
1584 // Lazily create a cache of the selectors.
1585
1586 if (!NSExceptionInstanceRaiseSelectors) {
1587
1588 ASTContext& Ctx = getContext();
1589
1590 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1591
1592 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1593 unsigned idx = 0;
1594
1595 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001596 II.push_back(&Ctx.Idents.get("raise"));
1597 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001598 NSExceptionInstanceRaiseSelectors[idx++] =
1599 Ctx.Selectors.getSelector(II.size(), &II[0]);
1600
1601 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001602 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001603 NSExceptionInstanceRaiseSelectors[idx++] =
1604 Ctx.Selectors.getSelector(II.size(), &II[0]);
1605 }
1606
1607 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1608 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1609 RaisesException = true; break;
1610 }
1611 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001612 }
1613
1614 // Check for any arguments that are uninitialized/undefined.
1615
1616 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1617 I != E; ++I) {
1618
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001619 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001620
1621 // Generate an error node for passing an uninitialized/undefined value
1622 // as an argument to a message expression. This node is a sink.
1623 NodeTy* N = Builder->generateNode(ME, St, Pred);
1624
1625 if (N) {
1626 N->markAsSink();
1627 MsgExprUndefArgs[N] = *I;
1628 }
1629
1630 return;
1631 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001632 }
1633
1634 // Check if we raise an exception. For now treat these as sinks. Eventually
1635 // we will want to handle exceptions properly.
1636
1637 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1638
1639 if (RaisesException)
1640 Builder->BuildSinks = true;
1641
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001642 // Dispatch to plug-in transfer function.
1643
1644 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001645 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001646
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001647 EvalObjCMessageExpr(Dst, ME, Pred);
1648
1649 // Handle the case where no nodes where generated. Auto-generate that
1650 // contains the updated state if we aren't generating sinks.
1651
Ted Kremenekb0533962008-04-18 20:35:30 +00001652 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001653 MakeNode(Dst, ME, Pred, St);
1654}
1655
1656//===----------------------------------------------------------------------===//
1657// Transfer functions: Miscellaneous statements.
1658//===----------------------------------------------------------------------===//
1659
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001660void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1661 QualType PtrTy,
1662 Expr* CastE, NodeTy* Pred,
1663 NodeSet& Dst) {
1664 if (!V.isUnknownOrUndef()) {
1665 // FIXME: Determine if the number of bits of the target type is
1666 // equal or exceeds the number of bits to store the pointer value.
1667 // If not, flag an error.
1668 unsigned bits = getContext().getTypeSize(PtrTy);
1669 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1670 }
1671
1672 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
1673}
1674
1675
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001676void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001677 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001678 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001679 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001680
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001681 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001682 T = ExCast->getTypeAsWritten();
1683
Zhongxing Xued340f72008-10-22 08:02:16 +00001684 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001685 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001686 else
1687 Visit(Ex, Pred, S1);
1688
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001689 // Check for casting to "void".
1690 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001691
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001692 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001693 Dst.Add(*I1);
1694
Ted Kremenek874d63f2008-01-24 02:02:54 +00001695 return;
1696 }
1697
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001698 // FIXME: The rest of this should probably just go into EvalCall, and
1699 // let the transfer function object be responsible for constructing
1700 // nodes.
1701
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001702 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001703 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001704 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001705 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001706
1707 // Unknown?
1708
1709 if (V.isUnknown()) {
1710 Dst.Add(N);
1711 continue;
1712 }
1713
1714 // Undefined?
1715
1716 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001717 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001718 continue;
1719 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001720
1721 // For const casts, just propagate the value.
1722 ASTContext& C = getContext();
1723
1724 if (C.getCanonicalType(T).getUnqualifiedType() ==
1725 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001726 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001727 continue;
1728 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001729
1730 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001731 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001732 VisitCastPointerToInteger(V, St, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001733 continue;
1734 }
1735
1736 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001737 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1738 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001739 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001740 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001741 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001742 continue;
1743 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001744
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001745 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001746 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001747 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001748 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001749
1750 // Are we casting from an array to a pointer? If so just pass on
1751 // the decayed value.
1752 if (T->isPointerType()) {
1753 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
1754 continue;
1755 }
1756
1757 // Are we casting from an array to an integer? If so, cast the decayed
1758 // pointer value to an integer.
1759 assert(T->isIntegerType());
1760 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1761 QualType PointerTy = getContext().getPointerType(ElemTy);
1762 VisitCastPointerToInteger(V, St, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001763 continue;
1764 }
1765
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001766 // Check for casts from a region to a specific type.
1767 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001768 assert(Loc::IsLocType(T));
1769 assert(Loc::IsLocType(ExTy));
1770
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001771 const MemRegion* R = RV->getRegion();
1772 StoreManager& StoreMgr = getStoreManager();
1773
1774 // Delegate to store manager to get the result of casting a region
1775 // to a different type.
1776 const StoreManager::CastResult& Res = StoreMgr.CastRegion(St, R, T);
1777
1778 // Inspect the result. If the MemRegion* returned is NULL, this
1779 // expression evaluates to UnknownVal.
1780 R = Res.getRegion();
1781 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1782
1783 // Generate the new node in the ExplodedGraph.
1784 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001785 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001786 }
1787
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001788 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001789 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001790 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001791}
1792
Ted Kremenek4f090272008-10-27 21:54:31 +00001793void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001794 NodeTy* Pred, NodeSet& Dst,
1795 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001796 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1797 NodeSet Tmp;
1798 Visit(ILE, Pred, Tmp);
1799
1800 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001801 const GRState* St = GetState(*I);
1802 SVal ILV = GetSVal(St, ILE);
1803 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001804
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001805 if (asLValue)
1806 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1807 else
1808 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001809 }
1810}
1811
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001812void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001813
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001814 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001815 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001816
1817 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001818 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001819
Ted Kremenekefd59942008-12-08 22:47:34 +00001820 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001821 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001822
1823 // FIXME: static variables may have an initializer, but the second
1824 // time a function is called those values may not be current.
1825 NodeSet Tmp;
1826
Ted Kremenekaf337412008-11-12 19:24:17 +00001827 if (InitEx)
1828 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001829
1830 if (Tmp.empty())
1831 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001832
1833 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001834 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001835 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001836
1837 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001838 if (InitEx) {
1839 SVal InitVal = GetSVal(St, InitEx);
1840 QualType T = VD->getType();
1841
1842 // Recover some path-sensitivity if a scalar value evaluated to
1843 // UnknownVal.
1844 if (InitVal.isUnknown()) {
1845 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001846 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001847 InitVal = loc::SymbolVal(Sym);
1848 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001849 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001850 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001851 InitVal = nonloc::SymbolVal(Sym);
1852 }
1853 }
1854
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001855 St = StateMgr.BindDecl(St, VD, InitVal);
1856 } else
1857 St = StateMgr.BindDeclWithNoInit(St, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001858
1859 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1860 QualType T = getContext().getCanonicalType(VD->getType());
1861 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1862 // FIXME: Handle multi-dimensional VLAs.
1863
1864 Expr* SE = VLA->getSizeExpr();
1865 SVal Size = GetSVal(St, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001866
1867 if (Size.isUndef()) {
1868 if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
1869 N->markAsSink();
1870 ExplicitBadSizedVLA.insert(N);
1871 }
1872 continue;
1873 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001874
1875 bool isFeasibleZero = false;
1876 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1877
1878 bool isFeasibleNotZero = false;
1879 St = Assume(St, Size, true, isFeasibleNotZero);
1880
1881 if (isFeasibleZero) {
1882 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1883 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001884 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1885 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001886 }
1887 }
1888
1889 if (!isFeasibleNotZero)
1890 continue;
1891 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001892
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001893 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001894 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001895}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001896
Ted Kremenekf75b1862008-10-30 17:47:32 +00001897namespace {
1898 // This class is used by VisitInitListExpr as an item in a worklist
1899 // for processing the values contained in an InitListExpr.
1900class VISIBILITY_HIDDEN InitListWLItem {
1901public:
1902 llvm::ImmutableList<SVal> Vals;
1903 GRExprEngine::NodeTy* N;
1904 InitListExpr::reverse_iterator Itr;
1905
1906 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1907 InitListExpr::reverse_iterator itr)
1908 : Vals(vals), N(n), Itr(itr) {}
1909};
1910}
1911
1912
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001913void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1914 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001915
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001916 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001917 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001918 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001919
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001920 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001921
Ted Kremeneka49e3672008-10-30 23:14:36 +00001922 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001923
Ted Kremeneka49e3672008-10-30 23:14:36 +00001924 // Handle base case where the initializer has no elements.
1925 // e.g: static int* myArray[] = {};
1926 if (NumInitElements == 0) {
1927 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1928 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1929 return;
1930 }
1931
1932 // Create a worklist to process the initializers.
1933 llvm::SmallVector<InitListWLItem, 10> WorkList;
1934 WorkList.reserve(NumInitElements);
1935 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001936 InitListExpr::reverse_iterator ItrEnd = E->rend();
1937
Ted Kremeneka49e3672008-10-30 23:14:36 +00001938 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001939 while (!WorkList.empty()) {
1940 InitListWLItem X = WorkList.back();
1941 WorkList.pop_back();
1942
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001943 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001944 Visit(*X.Itr, X.N, Tmp);
1945
1946 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001947
Ted Kremenekf75b1862008-10-30 17:47:32 +00001948 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1949 // Get the last initializer value.
1950 state = GetState(*NI);
1951 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1952
1953 // Construct the new list of values by prepending the new value to
1954 // the already constructed list.
1955 llvm::ImmutableList<SVal> NewVals =
1956 getBasicVals().consVals(InitV, X.Vals);
1957
1958 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001959 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001960 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001961
Ted Kremenekf75b1862008-10-30 17:47:32 +00001962 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001963 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001964 }
1965 else {
1966 // Still some initializer values to go. Push them onto the worklist.
1967 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1968 }
1969 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001970 }
Ted Kremenek87903072008-10-30 18:34:31 +00001971
1972 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001973 }
1974
Ted Kremenek062e2f92008-11-13 06:10:40 +00001975 if (T->isUnionType() || T->isVectorType()) {
1976 // FIXME: to be implemented.
1977 // Note: That vectors can return true for T->isIntegerType()
1978 MakeNode(Dst, E, Pred, state);
1979 return;
1980 }
1981
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001982 if (Loc::IsLocType(T) || T->isIntegerType()) {
1983 assert (E->getNumInits() == 1);
1984 NodeSet Tmp;
1985 Expr* Init = E->getInit(0);
1986 Visit(Init, Pred, Tmp);
1987 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1988 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001989 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001990 }
1991 return;
1992 }
1993
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001994
1995 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1996 assert(0 && "unprocessed InitListExpr type");
1997}
Ted Kremenekf233d482008-02-05 00:26:40 +00001998
Sebastian Redl05189992008-11-11 17:56:53 +00001999/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2000void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2001 NodeTy* Pred,
2002 NodeSet& Dst) {
2003 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002004 uint64_t amt;
2005
2006 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002007 if (T == getContext().VoidTy) {
2008 // sizeof(void) == 1 byte.
2009 amt = 1;
2010 }
2011 else if (!T.getTypePtr()->isConstantSizeType()) {
2012 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002013 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002014 }
2015 else if (T->isObjCInterfaceType()) {
2016 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2017 // the compiler has laid out its representation. Just report Unknown
2018 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002019 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002020 }
2021 else {
2022 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002023 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002024 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002025 }
2026 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002027 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002028
Ted Kremenek0e561a32008-03-21 21:30:14 +00002029 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002030 BindExpr(GetState(Pred), Ex,
2031 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002032}
2033
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002034
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002035void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002036 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002037
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002038 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002039
2040 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002041 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002042
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002043 case UnaryOperator::Deref: {
2044
2045 Expr* Ex = U->getSubExpr()->IgnoreParens();
2046 NodeSet Tmp;
2047 Visit(Ex, Pred, Tmp);
2048
2049 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002050
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002051 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002052 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002053
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002054 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002055 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002056 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002057 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002058 }
2059
2060 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002061 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002062
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002063 case UnaryOperator::Real: {
2064
2065 Expr* Ex = U->getSubExpr()->IgnoreParens();
2066 NodeSet Tmp;
2067 Visit(Ex, Pred, Tmp);
2068
2069 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2070
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002071 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002072 if (Ex->getType()->isAnyComplexType()) {
2073 // Just report "Unknown."
2074 Dst.Add(*I);
2075 continue;
2076 }
2077
2078 // For all other types, UnaryOperator::Real is an identity operation.
2079 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002080 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002081 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002082 }
2083
2084 return;
2085 }
2086
2087 case UnaryOperator::Imag: {
2088
2089 Expr* Ex = U->getSubExpr()->IgnoreParens();
2090 NodeSet Tmp;
2091 Visit(Ex, Pred, Tmp);
2092
2093 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002094 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002095 if (Ex->getType()->isAnyComplexType()) {
2096 // Just report "Unknown."
2097 Dst.Add(*I);
2098 continue;
2099 }
2100
2101 // For all other types, UnaryOperator::Float returns 0.
2102 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002103 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002104 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002105 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002106 }
2107
2108 return;
2109 }
2110
2111 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002112 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002113 Dst.Add(Pred);
2114 return;
2115
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002116 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002117 case UnaryOperator::Extension: {
2118
2119 // Unary "+" is a no-op, similar to a parentheses. We still have places
2120 // where it may be a block-level expression, so we need to
2121 // generate an extra node that just propagates the value of the
2122 // subexpression.
2123
2124 Expr* Ex = U->getSubExpr()->IgnoreParens();
2125 NodeSet Tmp;
2126 Visit(Ex, Pred, Tmp);
2127
2128 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002129 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002130 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002131 }
2132
2133 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002134 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002135
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002136 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002137
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002138 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002139 Expr* Ex = U->getSubExpr()->IgnoreParens();
2140 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002141 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002142
2143 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002144 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002145 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002146 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002147 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002148 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002149
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002150 return;
2151 }
2152
2153 case UnaryOperator::LNot:
2154 case UnaryOperator::Minus:
2155 case UnaryOperator::Not: {
2156
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002157 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002158 Expr* Ex = U->getSubExpr()->IgnoreParens();
2159 NodeSet Tmp;
2160 Visit(Ex, Pred, Tmp);
2161
2162 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002163 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002164
2165 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002166 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002167
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002168 if (V.isUnknownOrUndef()) {
2169 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2170 continue;
2171 }
2172
Ted Kremenek60595da2008-11-15 04:01:56 +00002173// QualType DstT = getContext().getCanonicalType(U->getType());
2174// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2175//
2176// if (DstT != SrcT) // Perform promotions.
2177// V = EvalCast(V, DstT);
2178//
2179// if (V.isUnknownOrUndef()) {
2180// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2181// continue;
2182// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002183
2184 switch (U->getOpcode()) {
2185 default:
2186 assert(false && "Invalid Opcode.");
2187 break;
2188
2189 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002190 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002191 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002192 break;
2193
2194 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002195 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002196 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002197 break;
2198
2199 case UnaryOperator::LNot:
2200
2201 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2202 //
2203 // Note: technically we do "E == 0", but this is the same in the
2204 // transfer functions as "0 == E".
2205
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002206 if (isa<Loc>(V)) {
2207 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2208 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002209 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002210 }
2211 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002212 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002213#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002214 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2215 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002216#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002217 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002218 continue;
2219#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002220 }
2221
2222 break;
2223 }
2224
2225 MakeNode(Dst, U, *I, St);
2226 }
2227
2228 return;
2229 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002230 }
2231
2232 // Handle ++ and -- (both pre- and post-increment).
2233
2234 assert (U->isIncrementDecrementOp());
2235 NodeSet Tmp;
2236 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002237 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002238
2239 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2240
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002241 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002242 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002243
2244 // Perform a load.
2245 NodeSet Tmp2;
2246 EvalLoad(Tmp2, Ex, *I, St, V1);
2247
2248 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2249
2250 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002251 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002252
2253 // Propagate unknown and undefined values.
2254 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002255 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002256 continue;
2257 }
2258
Ted Kremenek443003b2008-02-21 19:29:23 +00002259 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002260
2261 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2262 : BinaryOperator::Sub;
2263
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002264 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002265 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002266
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002267 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002268 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002269 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002270 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002271}
2272
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002273void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2274 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2275}
2276
2277void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2278 AsmStmt::outputs_iterator I,
2279 AsmStmt::outputs_iterator E,
2280 NodeTy* Pred, NodeSet& Dst) {
2281 if (I == E) {
2282 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2283 return;
2284 }
2285
2286 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002287 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002288
2289 ++I;
2290
2291 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2292 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2293}
2294
2295void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2296 AsmStmt::inputs_iterator I,
2297 AsmStmt::inputs_iterator E,
2298 NodeTy* Pred, NodeSet& Dst) {
2299 if (I == E) {
2300
2301 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002302 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002303
2304 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2305 // which interprets the inline asm and stores proper results in the
2306 // outputs.
2307
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002308 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002309
2310 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2311 OE = A->end_outputs(); OI != OE; ++OI) {
2312
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002313 SVal X = GetSVal(St, *OI);
2314 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002315
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002316 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002317 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002318 }
2319
Ted Kremenek0e561a32008-03-21 21:30:14 +00002320 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002321 return;
2322 }
2323
2324 NodeSet Tmp;
2325 Visit(*I, Pred, Tmp);
2326
2327 ++I;
2328
2329 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2330 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2331}
2332
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002333void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2334 assert (Builder && "GRStmtNodeBuilder must be defined.");
2335
2336 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002337
Ted Kremenek186350f2008-04-23 20:12:28 +00002338 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2339 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002340
Ted Kremenek729a9a22008-07-17 23:15:45 +00002341 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002342
Ted Kremenekb0533962008-04-18 20:35:30 +00002343 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002344
Ted Kremenekb0533962008-04-18 20:35:30 +00002345 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002346 MakeNode(Dst, S, Pred, GetState(Pred));
2347}
2348
Ted Kremenek02737ed2008-03-31 15:02:58 +00002349void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2350
2351 Expr* R = S->getRetValue();
2352
2353 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002354 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002355 return;
2356 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002357
Ted Kremenek5917d782008-11-21 00:27:44 +00002358 NodeSet Tmp;
2359 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002360
Ted Kremenek5917d782008-11-21 00:27:44 +00002361 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2362 SVal X = GetSVal((*I)->getState(), R);
2363
2364 // Check if we return the address of a stack variable.
2365 if (isa<loc::MemRegionVal>(X)) {
2366 // Determine if the value is on the stack.
2367 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002368
Ted Kremenek5917d782008-11-21 00:27:44 +00002369 if (R && getStateManager().hasStackStorage(R)) {
2370 // Create a special node representing the error.
2371 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2372 N->markAsSink();
2373 RetsStackAddr.insert(N);
2374 }
2375 continue;
2376 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002377 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002378 // Check if we return an undefined value.
2379 else if (X.isUndef()) {
2380 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2381 N->markAsSink();
2382 RetsUndef.insert(N);
2383 }
2384 continue;
2385 }
2386
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002387 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002388 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002389}
Ted Kremenek55deb972008-03-25 00:34:37 +00002390
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002391//===----------------------------------------------------------------------===//
2392// Transfer functions: Binary operators.
2393//===----------------------------------------------------------------------===//
2394
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002395const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2396 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002397
2398 // Divide by undefined? (potentially zero)
2399
2400 if (Denom.isUndef()) {
2401 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2402
2403 if (DivUndef) {
2404 DivUndef->markAsSink();
2405 ExplicitBadDivides.insert(DivUndef);
2406 }
2407
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002408 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002409 }
2410
2411 // Check for divide/remainder-by-zero.
2412 // First, "assume" that the denominator is 0 or undefined.
2413
2414 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002415 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002416
2417 // Second, "assume" that the denominator cannot be 0.
2418
2419 bool isFeasibleNotZero = false;
2420 St = Assume(St, Denom, true, isFeasibleNotZero);
2421
2422 // Create the node for the divide-by-zero (if it occurred).
2423
2424 if (isFeasibleZero)
2425 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2426 DivZeroNode->markAsSink();
2427
2428 if (isFeasibleNotZero)
2429 ImplicitBadDivides.insert(DivZeroNode);
2430 else
2431 ExplicitBadDivides.insert(DivZeroNode);
2432
2433 }
2434
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002435 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002436}
2437
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002438void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002439 GRExprEngine::NodeTy* Pred,
2440 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002441
2442 NodeSet Tmp1;
2443 Expr* LHS = B->getLHS()->IgnoreParens();
2444 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002445
Ted Kremenek759623e2008-12-06 02:39:30 +00002446 // FIXME: Add proper support for ObjCKVCRefExpr.
2447 if (isa<ObjCKVCRefExpr>(LHS)) {
2448 Visit(RHS, Pred, Dst);
2449 return;
2450 }
2451
2452
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002453 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002454 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002455 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002456 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002457
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002458 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002459
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002460 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002461
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002462 // Process the RHS.
2463
2464 NodeSet Tmp2;
2465 Visit(RHS, *I1, Tmp2);
2466
2467 // With both the LHS and RHS evaluated, process the operation itself.
2468
2469 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002470
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002471 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002472 const GRState* OldSt = St;
2473
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002474 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002475 BinaryOperator::Opcode Op = B->getOpcode();
2476
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002477 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002478
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002479 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002480
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002481 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002482 // FIXME: Handle structs.
2483 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002484
Ted Kremenek062e2f92008-11-13 06:10:40 +00002485 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2486 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002487 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002488 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002489
Ted Kremenek062e2f92008-11-13 06:10:40 +00002490 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002491 ? cast<SVal>(loc::SymbolVal(Sym))
2492 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002493 }
2494
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002495 // Simulate the effects of a "store": bind the value of the RHS
2496 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002497
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002498 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002499 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002500 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002501
2502 case BinaryOperator::Div:
2503 case BinaryOperator::Rem:
2504
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002505 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002506 if (RHS->getType()->isIntegerType() &&
2507 RHS->getType()->isScalarType()) {
2508
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002509 St = CheckDivideZero(B, St, *I2, RightV);
2510 if (!St) continue;
2511 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002512
2513 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002514
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002515 default: {
2516
2517 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002518 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002519
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002520 // Process non-assignements except commas or short-circuited
2521 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002522
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002523 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002524
2525 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002526 if (OldSt != St) {
2527 // Generate a new node if we have already created a new state.
2528 MakeNode(Dst, B, *I2, St);
2529 }
2530 else
2531 Dst.Add(*I2);
2532
Ted Kremenek89063af2008-02-21 19:15:37 +00002533 continue;
2534 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002535
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002536 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002537
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002538 // The operands were *not* undefined, but the result is undefined.
2539 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002540
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002541 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002542 UndefNode->markAsSink();
2543 UndefResults.insert(UndefNode);
2544 }
2545
2546 continue;
2547 }
2548
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002549 // Otherwise, create a new node.
2550
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002551 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002552 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002553 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002554 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002555
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002556 assert (B->isCompoundAssignmentOp());
2557
Ted Kremenek934e3e92008-10-27 23:02:39 +00002558 if (Op >= BinaryOperator::AndAssign) {
2559 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2560 BinaryOperator::And));
2561 }
2562 else {
2563 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2564 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002565
2566 // Perform a load (the LHS). This performs the checks for
2567 // null dereferences, and so on.
2568 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002569 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002570 EvalLoad(Tmp3, LHS, *I2, St, location);
2571
2572 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2573
2574 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002575 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002576
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002577 // Check for divide-by-zero.
2578 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002579 && RHS->getType()->isIntegerType()
2580 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002581
2582 // CheckDivideZero returns a new state where the denominator
2583 // is assumed to be non-zero.
2584 St = CheckDivideZero(B, St, *I3, RightV);
2585
2586 if (!St)
2587 continue;
2588 }
2589
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002590 // Propagate undefined values (left-side).
2591 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002592 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002593 continue;
2594 }
2595
2596 // Propagate unknown values (left and right-side).
2597 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002598 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002599 UnknownVal());
2600 continue;
2601 }
2602
2603 // At this point:
2604 //
2605 // The LHS is not Undef/Unknown.
2606 // The RHS is not Unknown.
2607
2608 // Get the computation type.
2609 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002610 CTy = getContext().getCanonicalType(CTy);
2611
2612 QualType LTy = getContext().getCanonicalType(LHS->getType());
2613 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002614
2615 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002616 if (LTy != CTy) V = EvalCast(V, CTy);
2617 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002618
2619 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002620 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002621 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002622 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002623 continue;
2624 }
2625
Ted Kremenek60595da2008-11-15 04:01:56 +00002626 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002627 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002628
2629 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002630 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002631 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2632 UndefNode->markAsSink();
2633 UndefResults.insert(UndefNode);
2634 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002635 continue;
2636 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002637
2638 // EXPERIMENTAL: "Conjured" symbols.
2639 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002640
2641 SVal LHSVal;
2642
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002643 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2644 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002645
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002646 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002647
Ted Kremenek60595da2008-11-15 04:01:56 +00002648 // The symbolic value is actually for the type of the left-hand side
2649 // expression, not the computation type, as this is the value the
2650 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002651 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002652 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002653 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002654 : cast<SVal>(nonloc::SymbolVal(Sym));
2655
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002656 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002657 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002658 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002659 else {
2660 // The left-hand side may bind to a different value then the
2661 // computation type.
2662 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2663 }
2664
2665 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002666 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002667 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002668 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002669}
Ted Kremenekee985462008-01-16 18:18:48 +00002670
2671//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002672// Transfer-function Helpers.
2673//===----------------------------------------------------------------------===//
2674
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002675void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002676 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002677 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002678 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002679
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002680 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002681 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2682
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002683 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002684 MakeNode(Dst, Ex, Pred, *I);
2685}
2686
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002687void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002688 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002689 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002690
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002691 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002692 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002693}
2694
2695//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002696// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002697//===----------------------------------------------------------------------===//
2698
Ted Kremenekaa66a322008-01-16 21:46:15 +00002699#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002700static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002701static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002702
Ted Kremenekaa66a322008-01-16 21:46:15 +00002703namespace llvm {
2704template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002705struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002706 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002707
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002708 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2709
2710 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002711 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002712 GraphPrintCheckerState->isUndefDeref(N) ||
2713 GraphPrintCheckerState->isUndefStore(N) ||
2714 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002715 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2716 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002717 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002718 GraphPrintCheckerState->isBadCall(N) ||
2719 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002720 return "color=\"red\",style=\"filled\"";
2721
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002722 if (GraphPrintCheckerState->isNoReturnCall(N))
2723 return "color=\"blue\",style=\"filled\"";
2724
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002725 return "";
2726 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002727
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002728 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002729 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002730
2731 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002732 ProgramPoint Loc = N->getLocation();
2733
2734 switch (Loc.getKind()) {
2735 case ProgramPoint::BlockEntranceKind:
2736 Out << "Block Entrance: B"
2737 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2738 break;
2739
2740 case ProgramPoint::BlockExitKind:
2741 assert (false);
2742 break;
2743
Ted Kremenekaa66a322008-01-16 21:46:15 +00002744 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002745 if (isa<PostStmt>(Loc)) {
2746 const PostStmt& L = cast<PostStmt>(Loc);
2747 Stmt* S = L.getStmt();
2748 SourceLocation SLoc = S->getLocStart();
2749
2750 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2751 llvm::raw_os_ostream OutS(Out);
2752 S->printPretty(OutS);
2753 OutS.flush();
2754
2755 if (SLoc.isFileID()) {
2756 Out << "\\lline="
2757 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2758 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2759 }
2760
2761 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2762 Out << "\\|Implicit-Null Dereference.\\l";
2763 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2764 Out << "\\|Explicit-Null Dereference.\\l";
2765 else if (GraphPrintCheckerState->isUndefDeref(N))
2766 Out << "\\|Dereference of undefialied value.\\l";
2767 else if (GraphPrintCheckerState->isUndefStore(N))
2768 Out << "\\|Store to Undefined Loc.";
2769 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2770 Out << "\\|Explicit divide-by zero or undefined value.";
2771 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2772 Out << "\\|Implicit divide-by zero or undefined value.";
2773 else if (GraphPrintCheckerState->isUndefResult(N))
2774 Out << "\\|Result of operation is undefined.";
2775 else if (GraphPrintCheckerState->isNoReturnCall(N))
2776 Out << "\\|Call to function marked \"noreturn\".";
2777 else if (GraphPrintCheckerState->isBadCall(N))
2778 Out << "\\|Call to NULL/Undefined.";
2779 else if (GraphPrintCheckerState->isUndefArg(N))
2780 Out << "\\|Argument in call is undefined";
2781
2782 break;
2783 }
2784
Ted Kremenekaa66a322008-01-16 21:46:15 +00002785 const BlockEdge& E = cast<BlockEdge>(Loc);
2786 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2787 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002788
2789 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002790
2791 SourceLocation SLoc = T->getLocStart();
2792
Ted Kremenekb38911f2008-01-30 23:03:39 +00002793 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002794
Ted Kremeneka95d3752008-09-13 05:16:45 +00002795 llvm::raw_os_ostream OutS(Out);
2796 E.getSrc()->printTerminator(OutS);
2797 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002798
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002799 if (SLoc.isFileID()) {
2800 Out << "\\lline="
2801 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2802 << GraphPrintSourceManager->getColumnNumber(SLoc);
2803 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002804
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002805 if (isa<SwitchStmt>(T)) {
2806 Stmt* Label = E.getDst()->getLabel();
2807
2808 if (Label) {
2809 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2810 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002811 llvm::raw_os_ostream OutS(Out);
2812 C->getLHS()->printPretty(OutS);
2813 OutS.flush();
2814
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002815 if (Stmt* RHS = C->getRHS()) {
2816 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002817 RHS->printPretty(OutS);
2818 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002819 }
2820
2821 Out << ":";
2822 }
2823 else {
2824 assert (isa<DefaultStmt>(Label));
2825 Out << "\\ldefault:";
2826 }
2827 }
2828 else
2829 Out << "\\l(implicit) default:";
2830 }
2831 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002832 // FIXME
2833 }
2834 else {
2835 Out << "\\lCondition: ";
2836 if (*E.getSrc()->succ_begin() == E.getDst())
2837 Out << "true";
2838 else
2839 Out << "false";
2840 }
2841
2842 Out << "\\l";
2843 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002844
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002845 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2846 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002847 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002848 }
2849 }
2850
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002851 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002852
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002853 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2854 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002855
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002856 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002857 return Out.str();
2858 }
2859};
2860} // end llvm namespace
2861#endif
2862
Ted Kremenekffe0f432008-03-07 22:58:01 +00002863#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002864
2865template <typename ITERATOR>
2866GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2867
2868template <>
2869GRExprEngine::NodeTy*
2870GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2871 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2872 return I->first;
2873}
2874
Ted Kremenekffe0f432008-03-07 22:58:01 +00002875template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002876static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002877 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002878
Ted Kremenekd4527582008-09-16 18:44:52 +00002879 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002880
2881 for ( ; I != E; ++I ) {
2882 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002883 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002884
Ted Kremenekd4527582008-09-16 18:44:52 +00002885 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002886 continue;
2887
Ted Kremenekd4527582008-09-16 18:44:52 +00002888 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002889 Sources.push_back(N);
2890 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002891}
2892#endif
2893
2894void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002895#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002896 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002897 std::vector<NodeTy*> Src;
2898
2899 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002900 AddSources(Src, null_derefs_begin(), null_derefs_end());
2901 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2902 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2903 AddSources(Src, undef_results_begin(), undef_results_end());
2904 AddSources(Src, bad_calls_begin(), bad_calls_end());
2905 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002906 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002907
Ted Kremenekcb612922008-04-18 19:23:43 +00002908 // The new way.
2909 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2910 (*I)->GetErrorNodes(Src);
2911
2912
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002913 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002914 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002915 else {
2916 GraphPrintCheckerState = this;
2917 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002918
Ted Kremenekffe0f432008-03-07 22:58:01 +00002919 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002920
2921 GraphPrintCheckerState = NULL;
2922 GraphPrintSourceManager = NULL;
2923 }
2924#endif
2925}
2926
2927void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2928#ifndef NDEBUG
2929 GraphPrintCheckerState = this;
2930 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002931
Ted Kremenek493d7a22008-03-11 18:25:33 +00002932 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2933
2934 if (!TrimmedG)
2935 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2936 else {
2937 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2938 delete TrimmedG;
2939 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002940
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002941 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002942 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002943#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002944}