blob: d9bdd8be0b69d50a8fcf6d805aeb74d76f209882 [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.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000496 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000497 "Other kinds of expressions with non-aggregate/union types do"
498 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000499
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000500 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000501 }
502}
503
504//===----------------------------------------------------------------------===//
505// Block entrance. (Update counters).
506//===----------------------------------------------------------------------===//
507
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000508bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000509 GRBlockCounter BC) {
510
511 return BC.getNumVisited(B->getBlockID()) < 3;
512}
513
514//===----------------------------------------------------------------------===//
515// Branch processing.
516//===----------------------------------------------------------------------===//
517
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000518const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000519 Stmt* Terminator,
520 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000521
522 switch (Terminator->getStmtClass()) {
523 default:
524 return St;
525
526 case Stmt::BinaryOperatorClass: { // '&&' and '||'
527
528 BinaryOperator* B = cast<BinaryOperator>(Terminator);
529 BinaryOperator::Opcode Op = B->getOpcode();
530
531 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
532
533 // For &&, if we take the true branch, then the value of the whole
534 // expression is that of the RHS expression.
535 //
536 // For ||, if we take the false branch, then the value of the whole
537 // expression is that of the RHS expression.
538
539 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
540 (Op == BinaryOperator::LOr && !branchTaken)
541 ? B->getRHS() : B->getLHS();
542
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000543 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000544 }
545
546 case Stmt::ConditionalOperatorClass: { // ?:
547
548 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
549
550 // For ?, if branchTaken == true then the value is either the LHS or
551 // the condition itself. (GNU extension).
552
553 Expr* Ex;
554
555 if (branchTaken)
556 Ex = C->getLHS() ? C->getLHS() : C->getCond();
557 else
558 Ex = C->getRHS();
559
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000560 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000561 }
562
563 case Stmt::ChooseExprClass: { // ?:
564
565 ChooseExpr* C = cast<ChooseExpr>(Terminator);
566
567 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000568 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000569 }
570 }
571}
572
Ted Kremenekaf337412008-11-12 19:24:17 +0000573void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000574 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000575
Ted Kremeneke7d22112008-02-11 19:21:59 +0000576 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000577 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000578 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000579
Ted Kremenekb2331832008-02-15 22:29:00 +0000580 // Check for NULL conditions; e.g. "for(;;)"
581 if (!Condition) {
582 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000583 return;
584 }
585
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000586 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000587
588 switch (V.getBaseKind()) {
589 default:
590 break;
591
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000592 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000593 builder.generateNode(MarkBranch(PrevState, Term, true), true);
594 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000595 return;
596
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000597 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000598 NodeTy* N = builder.generateNode(PrevState, true);
599
600 if (N) {
601 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000602 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000603 }
604
605 builder.markInfeasible(false);
606 return;
607 }
608 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000609
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000610 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000611
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000612 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000613 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000614
615 if (isFeasible)
616 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000617 else
618 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000619
620 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000621
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000622 isFeasible = false;
623 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000624
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000625 if (isFeasible)
626 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000627 else
628 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000629}
630
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000631/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000632/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000633void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000634
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000635 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000636 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000637
638 // Three possibilities:
639 //
640 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000641 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000642 // (3) We have no clue about the label. Dispatch to all targets.
643 //
644
645 typedef IndirectGotoNodeBuilder::iterator iterator;
646
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000647 if (isa<loc::GotoLabel>(V)) {
648 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000649
650 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000651 if (I.getLabel() == L) {
652 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000653 return;
654 }
655 }
656
657 assert (false && "No block with label.");
658 return;
659 }
660
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000661 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000662 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000663 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000664 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000665 return;
666 }
667
668 // This is really a catch-all. We don't support symbolics yet.
669
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000670 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000671
672 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000673 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000674}
Ted Kremenekf233d482008-02-05 00:26:40 +0000675
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000676
677void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
678 NodeTy* Pred, NodeSet& Dst) {
679
680 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
681
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000682 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000683 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000684
685 assert (X.isUndef());
686
687 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
688
689 assert (SE);
690
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000691 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000692
693 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000694 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000695}
696
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000697/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
698/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000699void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
700 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000701 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000702 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000703 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000704
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000705 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000707 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000708 return;
709 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000710
Ted Kremenek72afb372009-01-17 01:54:16 +0000711 const GRState* DefaultSt = St;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000712 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000713
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000714 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000715 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000716
717 // Evaluate the LHS of the case value.
718 Expr::EvalResult V1;
719 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000720
Ted Kremenek72afb372009-01-17 01:54:16 +0000721 // Sanity checks. These go away in Release builds.
722 assert(b && V1.Val.isInt() && !V1.HasSideEffects
723 && "Case condition must evaluate to an integer constant.");
724 b = b; // silence unused variable warning
725 assert(V1.Val.getInt().getBitWidth() ==
726 getContext().getTypeSize(CondE->getType()));
727
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000728 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000729 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000730
731 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000732 b = E->Evaluate(V2, getContext());
733 assert(b && V2.Val.isInt() && !V2.HasSideEffects
734 && "Case condition must evaluate to an integer constant.");
735 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000736 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000737 else
738 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000739
740 // FIXME: Eventually we should replace the logic below with a range
741 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000742 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000743
Ted Kremenek14a11402008-03-17 22:17:56 +0000744 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000745 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000746 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000747
Ted Kremenek72afb372009-01-17 01:54:16 +0000748 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000749 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000750 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751
752 if (isFeasible) {
753 builder.generateCaseStmtNode(I, StNew);
754
755 // If CondV evaluates to a constant, then we know that this
756 // is the *only* case that we can take, so stop evaluating the
757 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000758 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759 return;
760 }
761
762 // Now "assume" that the case doesn't match. Add this state
763 // to the default state (if it is feasible).
764
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000765 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000766 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000767
Ted Kremenek5014ab12008-04-23 05:03:18 +0000768 if (isFeasible) {
769 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000770 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000771 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000772
Ted Kremenek14a11402008-03-17 22:17:56 +0000773 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000774 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000775 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000776
Ted Kremenek72afb372009-01-17 01:54:16 +0000777 ++V1.Val.getInt();
778 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000779
780 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000781 }
782
783 // If we reach here, than we know that the default branch is
784 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000785 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000786}
787
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000788//===----------------------------------------------------------------------===//
789// Transfer functions: logical operations ('&&', '||').
790//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000791
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000792void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000793 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000794
Ted Kremenek05a23782008-02-26 19:05:15 +0000795 assert (B->getOpcode() == BinaryOperator::LAnd ||
796 B->getOpcode() == BinaryOperator::LOr);
797
798 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
799
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000800 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000801 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000802
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000803 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000804
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000805 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000806
807 assert (Ex);
808
809 if (Ex == B->getRHS()) {
810
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000811 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000812
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000813 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000814
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000815 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000816 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000817 return;
818 }
819
Ted Kremenek05a23782008-02-26 19:05:15 +0000820 // We took the RHS. Because the value of the '&&' or '||' expression must
821 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
822 // or 1. Alternatively, we could take a lazy approach, and calculate this
823 // value later when necessary. We don't have the machinery in place for
824 // this right now, and since most logical expressions are used for branches,
825 // the payoff is not likely to be large. Instead, we do eager evaluation.
826
827 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000828 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000829
830 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000831 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000832 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000833
834 isFeasible = false;
835 NewState = Assume(St, X, false, isFeasible);
836
837 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000838 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000839 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000840 }
841 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000842 // We took the LHS expression. Depending on whether we are '&&' or
843 // '||' we know what the value of the expression is via properties of
844 // the short-circuiting.
845
846 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000847 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000848 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000849}
Ted Kremenek05a23782008-02-26 19:05:15 +0000850
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000851//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000852// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000853//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000854
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000855void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
856 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000857
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000858 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000859
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000860 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000861
862 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
863
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000864 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000865
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000866 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000867 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000868 else
869 EvalLoad(Dst, Ex, Pred, St, V);
870 return;
871
872 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
873 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
874
875 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000876 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000877 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000878 return;
879
880 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000881 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000882 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000883 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000884 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000885 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000886
887 assert (false &&
888 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000889}
890
Ted Kremenek540cbe22008-04-22 04:56:29 +0000891/// VisitArraySubscriptExpr - Transfer function for array accesses
892void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000893 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000894
895 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000896 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000897 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000898 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000899
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000900 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000901 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000902 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000903
904 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000905 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000906 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000907
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000908 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000909 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000910 else
911 EvalLoad(Dst, A, *I2, St, V);
912 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000913 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000914}
915
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000916/// VisitMemberExpr - Transfer function for member expressions.
917void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000918 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000919
920 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000921 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000922
923 if (M->isArrow())
924 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
925 else
926 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
927
Douglas Gregor86f19402008-12-20 23:49:58 +0000928 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
929 if (!Field) // FIXME: skipping member expressions for non-fields
930 return;
931
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000932 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000933 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000934 // FIXME: Should we insert some assumption logic in here to determine
935 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000936 // later when using FieldOffset lvals (which we no longer have).
937 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000938
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000939 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000940 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000941 else
942 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000943 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000944}
945
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000946void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000947 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000948
949 assert (Builder && "GRStmtNodeBuilder must be defined.");
950
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000951 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8c354752008-12-16 22:02:27 +0000952 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953
Ted Kremenek8c354752008-12-16 22:02:27 +0000954 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000955 return;
956
Ted Kremenek8c354752008-12-16 22:02:27 +0000957 St = GetState(Pred);
958
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000959 // Proceed with the store.
960
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000961 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000962
Ted Kremenek186350f2008-04-23 20:12:28 +0000963 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000964 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000965 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000966
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000967 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000968 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000969
Ted Kremenek729a9a22008-07-17 23:15:45 +0000970 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000971
972 // Handle the case where no nodes where generated. Auto-generate that
973 // contains the updated state if we aren't generating sinks.
974
Ted Kremenekb0533962008-04-18 20:35:30 +0000975 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000976 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000977 location, Val);
978}
979
980void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000981 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000982 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000983
Ted Kremenek8c354752008-12-16 22:02:27 +0000984 // Evaluate the location (checks for bad dereferences).
985 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000986
Ted Kremenek8c354752008-12-16 22:02:27 +0000987 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000988 return;
989
Ted Kremenek8c354752008-12-16 22:02:27 +0000990 St = GetState(Pred);
991
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000992 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000993 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000994
995 // FIXME: Currently symbolic analysis "generates" new symbols
996 // for the contents of values. We need a better approach.
997
998 // FIXME: The "CheckOnly" option exists only because Array and Field
999 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001000 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +00001001
Ted Kremenek8c354752008-12-16 22:02:27 +00001002 if (CheckOnly) {
1003 Dst.Add(Pred);
1004 return;
1005 }
1006
1007 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001008 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001009 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001010 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001011 else {
1012 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
1013 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1014 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001015}
1016
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001017void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001018 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001019
1020 NodeSet TmpDst;
1021 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1022
1023 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1024 MakeNode(Dst, Ex, *I, (*I)->getState());
1025}
1026
Ted Kremenek8c354752008-12-16 22:02:27 +00001027GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
1028 const GRState* St,
1029 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001030
1031 // Check for loads/stores from/to undefined values.
1032 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001033 NodeTy* N =
1034 Builder->generateNode(Ex, St, Pred,
1035 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001036
Ted Kremenek8c354752008-12-16 22:02:27 +00001037 if (N) {
1038 N->markAsSink();
1039 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001040 }
1041
Ted Kremenek8c354752008-12-16 22:02:27 +00001042 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001043 }
1044
1045 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1046 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001047 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001048
1049 // During a load, one of two possible situations arise:
1050 // (1) A crash, because the location (pointer) was NULL.
1051 // (2) The location (pointer) is not NULL, and the dereference works.
1052 //
1053 // We add these assumptions.
1054
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001055 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001056
1057 // "Assume" that the pointer is not NULL.
1058
1059 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001060 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001061
1062 // "Assume" that the pointer is NULL.
1063
1064 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001065 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1066 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001067
1068 if (isFeasibleNull) {
1069
Ted Kremenek7360fda2008-09-18 23:09:54 +00001070 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001071 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001072 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1073
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001074 // We don't use "MakeNode" here because the node will be a sink
1075 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001076 NodeTy* NullNode =
1077 Builder->generateNode(Ex, StNull, Pred,
1078 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001079
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001080 if (NullNode) {
1081
1082 NullNode->markAsSink();
1083
1084 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1085 else ExplicitNullDeref.insert(NullNode);
1086 }
1087 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001088
1089 if (!isFeasibleNotNull)
1090 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001091
1092 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001093 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001094 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1095 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1096 // Get the index of the accessed element.
1097 SVal Idx = ER->getIndex();
1098 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001099 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1100 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001101
1102 bool isFeasibleInBound = false;
1103 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1104 true, isFeasibleInBound);
1105
1106 bool isFeasibleOutBound = false;
1107 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1108 false, isFeasibleOutBound);
1109
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001110 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001111 // Report warning. Make sink node manually.
1112 NodeTy* OOBNode =
1113 Builder->generateNode(Ex, StOutBound, Pred,
1114 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001115
1116 if (OOBNode) {
1117 OOBNode->markAsSink();
1118
1119 if (isFeasibleInBound)
1120 ImplicitOOBMemAccesses.insert(OOBNode);
1121 else
1122 ExplicitOOBMemAccesses.insert(OOBNode);
1123 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001124 }
1125
Ted Kremenek8c354752008-12-16 22:02:27 +00001126 if (!isFeasibleInBound)
1127 return 0;
1128
1129 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001130 }
1131 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001132
Ted Kremenek8c354752008-12-16 22:02:27 +00001133 // Generate a new node indicating the checks succeed.
1134 return Builder->generateNode(Ex, StNotNull, Pred,
1135 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001136}
1137
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001138//===----------------------------------------------------------------------===//
1139// Transfer function: Function calls.
1140//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001141void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001142 CallExpr::arg_iterator AI,
1143 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001144 NodeSet& Dst)
1145{
1146 // Determine the type of function we're calling (if available).
1147 const FunctionTypeProto *Proto = NULL;
1148 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1149 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1150 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1151
1152 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1153}
1154
1155void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1156 CallExpr::arg_iterator AI,
1157 CallExpr::arg_iterator AE,
1158 NodeSet& Dst, const FunctionTypeProto *Proto,
1159 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001160
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001161 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001162 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001163 // If the call argument is being bound to a reference parameter,
1164 // visit it as an lvalue, not an rvalue.
1165 bool VisitAsLvalue = false;
1166 if (Proto && ParamIdx < Proto->getNumArgs())
1167 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1168
1169 NodeSet DstTmp;
1170 if (VisitAsLvalue)
1171 VisitLValue(*AI, Pred, DstTmp);
1172 else
1173 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001174 ++AI;
1175
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001176 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001177 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001178
1179 return;
1180 }
1181
1182 // If we reach here we have processed all of the arguments. Evaluate
1183 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001184
Ted Kremenek994a09b2008-02-25 21:16:03 +00001185 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001186 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001187
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001188 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001189
Ted Kremenekde434242008-02-19 01:44:53 +00001190 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001191 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1192
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001193 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001194 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001195
Ted Kremeneka1354a52008-03-03 16:47:31 +00001196 // FIXME: Add support for symbolic function calls (calls involving
1197 // function pointer values that are symbolic).
1198
1199 // Check for undefined control-flow or calls to NULL.
1200
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001201 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001202 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001203
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001204 if (N) {
1205 N->markAsSink();
1206 BadCalls.insert(N);
1207 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001208
Ted Kremenekde434242008-02-19 01:44:53 +00001209 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001210 }
1211
1212 // Check for the "noreturn" attribute.
1213
1214 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1215
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001216 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001217
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001218 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001219
1220 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001221 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001222 else {
1223 // HACK: Some functions are not marked noreturn, and don't return.
1224 // Here are a few hardwired ones. If this takes too long, we can
1225 // potentially cache these results.
1226 const char* s = FD->getIdentifier()->getName();
1227 unsigned n = strlen(s);
1228
1229 switch (n) {
1230 default:
1231 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001232
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001233 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001234 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1235 break;
1236
1237 case 5:
1238 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001239 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001240 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001241 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001242 // FIXME: use Assume to inspect the possible symbolic value of
1243 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001244 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001245 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001246 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001247 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001248 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001249 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001250
1251 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001252 if (!memcmp(s, "Assert", 6)) {
1253 Builder->BuildSinks = true;
1254 break;
1255 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001256
1257 // FIXME: This is just a wrapper around throwing an exception.
1258 // Eventually inter-procedural analysis should handle this easily.
1259 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1260
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001261 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001262
1263 case 7:
1264 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1265 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001266
Ted Kremenekf47bb782008-04-30 17:54:04 +00001267 case 8:
1268 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1269 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001270
1271 case 12:
1272 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1273 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001274
Ted Kremenekf9683082008-09-19 02:30:47 +00001275 case 13:
1276 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1277 break;
1278
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001279 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001280 if (!memcmp(s, "dtrace_assfail", 14) ||
1281 !memcmp(s, "yy_fatal_error", 14))
1282 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001283 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001284
1285 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001286 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1287 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001288 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001289
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001290 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001291 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001292
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001293 }
1294 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001295
1296 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001297
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001298 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001299
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001300 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001301
Ted Kremenek186350f2008-04-23 20:12:28 +00001302 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001303 switch (id) {
1304 case Builtin::BI__builtin_expect: {
1305 // For __builtin_expect, just return the value of the subexpression.
1306 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001307 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001308 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001309 continue;
1310 }
1311
Ted Kremenekb3021332008-11-02 00:35:01 +00001312 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001313 // FIXME: Refactor into StoreManager itself?
1314 MemRegionManager& RM = getStateManager().getRegionManager();
1315 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001316 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001317
1318 // Set the extent of the region in bytes. This enables us to use the
1319 // SVal of the argument directly. If we save the extent in bits, we
1320 // cannot represent values like symbol*8.
1321 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1322 St = getStoreManager().setExtent(St, R, Extent);
1323
Ted Kremenekb3021332008-11-02 00:35:01 +00001324 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1325 continue;
1326 }
1327
Ted Kremenek55aea312008-03-05 22:59:42 +00001328 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001329 break;
1330 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001331 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001332
Ted Kremenek186350f2008-04-23 20:12:28 +00001333 // Check any arguments passed-by-value against being undefined.
1334
1335 bool badArg = false;
1336
1337 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1338 I != E; ++I) {
1339
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001340 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001341 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001342
Ted Kremenek186350f2008-04-23 20:12:28 +00001343 if (N) {
1344 N->markAsSink();
1345 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001346 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001347
Ted Kremenek186350f2008-04-23 20:12:28 +00001348 badArg = true;
1349 break;
1350 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001351 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001352
1353 if (badArg)
1354 continue;
1355
1356 // Dispatch to the plug-in transfer function.
1357
1358 unsigned size = Dst.size();
1359 SaveOr OldHasGen(Builder->HasGeneratedNode);
1360 EvalCall(Dst, CE, L, *DI);
1361
1362 // Handle the case where no nodes where generated. Auto-generate that
1363 // contains the updated state if we aren't generating sinks.
1364
1365 if (!Builder->BuildSinks && Dst.size() == size &&
1366 !Builder->HasGeneratedNode)
1367 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001368 }
1369}
1370
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001371//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001372// Transfer function: Objective-C ivar references.
1373//===----------------------------------------------------------------------===//
1374
1375void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1376 NodeTy* Pred, NodeSet& Dst,
1377 bool asLValue) {
1378
1379 Expr* Base = cast<Expr>(Ex->getBase());
1380 NodeSet Tmp;
1381 Visit(Base, Pred, Tmp);
1382
1383 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1384 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001385 SVal BaseVal = GetSVal(St, Base);
1386 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001387
1388 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001389 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001390 else
1391 EvalLoad(Dst, Ex, *I, St, location);
1392 }
1393}
1394
1395//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001396// Transfer function: Objective-C fast enumeration 'for' statements.
1397//===----------------------------------------------------------------------===//
1398
1399void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1400 NodeTy* Pred, NodeSet& Dst) {
1401
1402 // ObjCForCollectionStmts are processed in two places. This method
1403 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1404 // statements within a basic block. This transfer function does two things:
1405 //
1406 // (1) binds the next container value to 'element'. This creates a new
1407 // node in the ExplodedGraph.
1408 //
1409 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1410 // whether or not the container has any more elements. This value
1411 // will be tested in ProcessBranch. We need to explicitly bind
1412 // this value because a container can contain nil elements.
1413 //
1414 // FIXME: Eventually this logic should actually do dispatches to
1415 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1416 // This will require simulating a temporary NSFastEnumerationState, either
1417 // through an SVal or through the use of MemRegions. This value can
1418 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1419 // terminates we reclaim the temporary (it goes out of scope) and we
1420 // we can test if the SVal is 0 or if the MemRegion is null (depending
1421 // on what approach we take).
1422 //
1423 // For now: simulate (1) by assigning either a symbol or nil if the
1424 // container is empty. Thus this transfer function will by default
1425 // result in state splitting.
1426
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001427 Stmt* elem = S->getElement();
1428 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001429
1430 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001431 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001432 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001433 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1434 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1435 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001436 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001437
1438 NodeSet Tmp;
1439 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001440
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001441 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1442 const GRState* state = GetState(*I);
1443 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1444 }
1445}
1446
1447void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1448 NodeTy* Pred, NodeSet& Dst,
1449 SVal ElementV) {
1450
1451
Ted Kremenekaf337412008-11-12 19:24:17 +00001452
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001453 // Get the current state. Use 'EvalLocation' to determine if it is a null
1454 // pointer, etc.
1455 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001456
Ted Kremenek8c354752008-12-16 22:02:27 +00001457 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1458 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001459 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001460
1461 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001462
Ted Kremenekaf337412008-11-12 19:24:17 +00001463 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001464 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001465 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1466 GRStateRef hasElems = state.BindExpr(S, TrueV);
1467
Ted Kremenekaf337412008-11-12 19:24:17 +00001468 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001469 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1470 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001471
1472 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1473 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1474 // FIXME: The proper thing to do is to really iterate over the
1475 // container. We will do this with dispatch logic to the store.
1476 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001477 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001478 assert (Loc::IsLocType(T));
1479 unsigned Count = Builder->getCurrentBlockCount();
1480 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1481 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001482
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001483 // Bind the location to 'nil' on the false branch.
1484 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1485 noElems = noElems.BindLoc(ElementV, nilV);
1486 }
1487
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001488 // Create the new nodes.
1489 MakeNode(Dst, S, Pred, hasElems);
1490 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001491}
1492
1493//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001494// Transfer function: Objective-C message expressions.
1495//===----------------------------------------------------------------------===//
1496
1497void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1498 NodeSet& Dst){
1499
1500 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1501 Pred, Dst);
1502}
1503
1504void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001505 ObjCMessageExpr::arg_iterator AI,
1506 ObjCMessageExpr::arg_iterator AE,
1507 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001508 if (AI == AE) {
1509
1510 // Process the receiver.
1511
1512 if (Expr* Receiver = ME->getReceiver()) {
1513 NodeSet Tmp;
1514 Visit(Receiver, Pred, Tmp);
1515
1516 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1517 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1518
1519 return;
1520 }
1521
1522 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1523 return;
1524 }
1525
1526 NodeSet Tmp;
1527 Visit(*AI, Pred, Tmp);
1528
1529 ++AI;
1530
1531 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1532 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1533}
1534
1535void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1536 NodeTy* Pred,
1537 NodeSet& Dst) {
1538
1539 // FIXME: More logic for the processing the method call.
1540
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001541 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001542 bool RaisesException = false;
1543
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001544
1545 if (Expr* Receiver = ME->getReceiver()) {
1546
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001547 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001548
1549 // Check for undefined control-flow or calls to NULL.
1550
1551 if (L.isUndef()) {
1552 NodeTy* N = Builder->generateNode(ME, St, Pred);
1553
1554 if (N) {
1555 N->markAsSink();
1556 UndefReceivers.insert(N);
1557 }
1558
1559 return;
1560 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001561
1562 // Check if the "raise" message was sent.
1563 if (ME->getSelector() == RaiseSel)
1564 RaisesException = true;
1565 }
1566 else {
1567
1568 IdentifierInfo* ClsName = ME->getClassName();
1569 Selector S = ME->getSelector();
1570
1571 // Check for special instance methods.
1572
1573 if (!NSExceptionII) {
1574 ASTContext& Ctx = getContext();
1575
1576 NSExceptionII = &Ctx.Idents.get("NSException");
1577 }
1578
1579 if (ClsName == NSExceptionII) {
1580
1581 enum { NUM_RAISE_SELECTORS = 2 };
1582
1583 // Lazily create a cache of the selectors.
1584
1585 if (!NSExceptionInstanceRaiseSelectors) {
1586
1587 ASTContext& Ctx = getContext();
1588
1589 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1590
1591 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1592 unsigned idx = 0;
1593
1594 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001595 II.push_back(&Ctx.Idents.get("raise"));
1596 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001597 NSExceptionInstanceRaiseSelectors[idx++] =
1598 Ctx.Selectors.getSelector(II.size(), &II[0]);
1599
1600 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001601 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001602 NSExceptionInstanceRaiseSelectors[idx++] =
1603 Ctx.Selectors.getSelector(II.size(), &II[0]);
1604 }
1605
1606 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1607 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1608 RaisesException = true; break;
1609 }
1610 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001611 }
1612
1613 // Check for any arguments that are uninitialized/undefined.
1614
1615 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1616 I != E; ++I) {
1617
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001618 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001619
1620 // Generate an error node for passing an uninitialized/undefined value
1621 // as an argument to a message expression. This node is a sink.
1622 NodeTy* N = Builder->generateNode(ME, St, Pred);
1623
1624 if (N) {
1625 N->markAsSink();
1626 MsgExprUndefArgs[N] = *I;
1627 }
1628
1629 return;
1630 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001631 }
1632
1633 // Check if we raise an exception. For now treat these as sinks. Eventually
1634 // we will want to handle exceptions properly.
1635
1636 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1637
1638 if (RaisesException)
1639 Builder->BuildSinks = true;
1640
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001641 // Dispatch to plug-in transfer function.
1642
1643 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001644 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001645
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001646 EvalObjCMessageExpr(Dst, ME, Pred);
1647
1648 // Handle the case where no nodes where generated. Auto-generate that
1649 // contains the updated state if we aren't generating sinks.
1650
Ted Kremenekb0533962008-04-18 20:35:30 +00001651 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001652 MakeNode(Dst, ME, Pred, St);
1653}
1654
1655//===----------------------------------------------------------------------===//
1656// Transfer functions: Miscellaneous statements.
1657//===----------------------------------------------------------------------===//
1658
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001659void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1660 QualType PtrTy,
1661 Expr* CastE, NodeTy* Pred,
1662 NodeSet& Dst) {
1663 if (!V.isUnknownOrUndef()) {
1664 // FIXME: Determine if the number of bits of the target type is
1665 // equal or exceeds the number of bits to store the pointer value.
1666 // If not, flag an error.
1667 unsigned bits = getContext().getTypeSize(PtrTy);
1668 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1669 }
1670
1671 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
1672}
1673
1674
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001675void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001676 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001677 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001678 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001679
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001680 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001681 T = ExCast->getTypeAsWritten();
1682
Zhongxing Xued340f72008-10-22 08:02:16 +00001683 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001684 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001685 else
1686 Visit(Ex, Pred, S1);
1687
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001688 // Check for casting to "void".
1689 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001690
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001691 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001692 Dst.Add(*I1);
1693
Ted Kremenek874d63f2008-01-24 02:02:54 +00001694 return;
1695 }
1696
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001697 // FIXME: The rest of this should probably just go into EvalCall, and
1698 // let the transfer function object be responsible for constructing
1699 // nodes.
1700
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001701 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001702 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001703 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001704 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001705
1706 // Unknown?
1707
1708 if (V.isUnknown()) {
1709 Dst.Add(N);
1710 continue;
1711 }
1712
1713 // Undefined?
1714
1715 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001716 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001717 continue;
1718 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001719
1720 // For const casts, just propagate the value.
1721 ASTContext& C = getContext();
1722
1723 if (C.getCanonicalType(T).getUnqualifiedType() ==
1724 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001725 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001726 continue;
1727 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001728
1729 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001730 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001731 VisitCastPointerToInteger(V, St, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001732 continue;
1733 }
1734
1735 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001736 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1737 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001738 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001739 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001740 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001741 continue;
1742 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001743
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001744 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001745 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001746 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001747 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001748
1749 // Are we casting from an array to a pointer? If so just pass on
1750 // the decayed value.
1751 if (T->isPointerType()) {
1752 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
1753 continue;
1754 }
1755
1756 // Are we casting from an array to an integer? If so, cast the decayed
1757 // pointer value to an integer.
1758 assert(T->isIntegerType());
1759 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1760 QualType PointerTy = getContext().getPointerType(ElemTy);
1761 VisitCastPointerToInteger(V, St, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001762 continue;
1763 }
1764
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001765 // Check for casts from a region to a specific type.
1766 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001767 assert(Loc::IsLocType(T));
1768 assert(Loc::IsLocType(ExTy));
1769
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001770 const MemRegion* R = RV->getRegion();
1771 StoreManager& StoreMgr = getStoreManager();
1772
1773 // Delegate to store manager to get the result of casting a region
1774 // to a different type.
1775 const StoreManager::CastResult& Res = StoreMgr.CastRegion(St, R, T);
1776
1777 // Inspect the result. If the MemRegion* returned is NULL, this
1778 // expression evaluates to UnknownVal.
1779 R = Res.getRegion();
1780 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1781
1782 // Generate the new node in the ExplodedGraph.
1783 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001784 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001785 }
1786
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001787 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001788 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001789 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001790}
1791
Ted Kremenek4f090272008-10-27 21:54:31 +00001792void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001793 NodeTy* Pred, NodeSet& Dst,
1794 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001795 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1796 NodeSet Tmp;
1797 Visit(ILE, Pred, Tmp);
1798
1799 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001800 const GRState* St = GetState(*I);
1801 SVal ILV = GetSVal(St, ILE);
1802 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001803
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001804 if (asLValue)
1805 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1806 else
1807 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001808 }
1809}
1810
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001811void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001812
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001813 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001814 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001815
1816 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001817 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001818
Ted Kremenekefd59942008-12-08 22:47:34 +00001819 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001820 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001821
1822 // FIXME: static variables may have an initializer, but the second
1823 // time a function is called those values may not be current.
1824 NodeSet Tmp;
1825
Ted Kremenekaf337412008-11-12 19:24:17 +00001826 if (InitEx)
1827 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001828
1829 if (Tmp.empty())
1830 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001831
1832 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001833 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001834 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001835
1836 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001837 if (InitEx) {
1838 SVal InitVal = GetSVal(St, InitEx);
1839 QualType T = VD->getType();
1840
1841 // Recover some path-sensitivity if a scalar value evaluated to
1842 // UnknownVal.
1843 if (InitVal.isUnknown()) {
1844 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001845 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001846 InitVal = loc::SymbolVal(Sym);
1847 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001848 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001849 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001850 InitVal = nonloc::SymbolVal(Sym);
1851 }
1852 }
1853
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001854 St = StateMgr.BindDecl(St, VD, InitVal);
1855 } else
1856 St = StateMgr.BindDeclWithNoInit(St, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001857
1858 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1859 QualType T = getContext().getCanonicalType(VD->getType());
1860 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1861 // FIXME: Handle multi-dimensional VLAs.
1862
1863 Expr* SE = VLA->getSizeExpr();
1864 SVal Size = GetSVal(St, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001865
1866 if (Size.isUndef()) {
1867 if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
1868 N->markAsSink();
1869 ExplicitBadSizedVLA.insert(N);
1870 }
1871 continue;
1872 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001873
1874 bool isFeasibleZero = false;
1875 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1876
1877 bool isFeasibleNotZero = false;
1878 St = Assume(St, Size, true, isFeasibleNotZero);
1879
1880 if (isFeasibleZero) {
1881 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1882 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001883 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1884 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001885 }
1886 }
1887
1888 if (!isFeasibleNotZero)
1889 continue;
1890 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001891
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001892 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001893 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001894}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001895
Ted Kremenekf75b1862008-10-30 17:47:32 +00001896namespace {
1897 // This class is used by VisitInitListExpr as an item in a worklist
1898 // for processing the values contained in an InitListExpr.
1899class VISIBILITY_HIDDEN InitListWLItem {
1900public:
1901 llvm::ImmutableList<SVal> Vals;
1902 GRExprEngine::NodeTy* N;
1903 InitListExpr::reverse_iterator Itr;
1904
1905 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1906 InitListExpr::reverse_iterator itr)
1907 : Vals(vals), N(n), Itr(itr) {}
1908};
1909}
1910
1911
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001912void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1913 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001914
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001915 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001916 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001917 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001918
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001919 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001920
Ted Kremeneka49e3672008-10-30 23:14:36 +00001921 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001922
Ted Kremeneka49e3672008-10-30 23:14:36 +00001923 // Handle base case where the initializer has no elements.
1924 // e.g: static int* myArray[] = {};
1925 if (NumInitElements == 0) {
1926 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1927 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1928 return;
1929 }
1930
1931 // Create a worklist to process the initializers.
1932 llvm::SmallVector<InitListWLItem, 10> WorkList;
1933 WorkList.reserve(NumInitElements);
1934 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001935 InitListExpr::reverse_iterator ItrEnd = E->rend();
1936
Ted Kremeneka49e3672008-10-30 23:14:36 +00001937 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001938 while (!WorkList.empty()) {
1939 InitListWLItem X = WorkList.back();
1940 WorkList.pop_back();
1941
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001942 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001943 Visit(*X.Itr, X.N, Tmp);
1944
1945 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001946
Ted Kremenekf75b1862008-10-30 17:47:32 +00001947 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1948 // Get the last initializer value.
1949 state = GetState(*NI);
1950 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1951
1952 // Construct the new list of values by prepending the new value to
1953 // the already constructed list.
1954 llvm::ImmutableList<SVal> NewVals =
1955 getBasicVals().consVals(InitV, X.Vals);
1956
1957 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001958 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001959 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001960
Ted Kremenekf75b1862008-10-30 17:47:32 +00001961 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001962 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001963 }
1964 else {
1965 // Still some initializer values to go. Push them onto the worklist.
1966 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1967 }
1968 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001969 }
Ted Kremenek87903072008-10-30 18:34:31 +00001970
1971 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001972 }
1973
Ted Kremenek062e2f92008-11-13 06:10:40 +00001974 if (T->isUnionType() || T->isVectorType()) {
1975 // FIXME: to be implemented.
1976 // Note: That vectors can return true for T->isIntegerType()
1977 MakeNode(Dst, E, Pred, state);
1978 return;
1979 }
1980
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001981 if (Loc::IsLocType(T) || T->isIntegerType()) {
1982 assert (E->getNumInits() == 1);
1983 NodeSet Tmp;
1984 Expr* Init = E->getInit(0);
1985 Visit(Init, Pred, Tmp);
1986 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1987 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001988 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001989 }
1990 return;
1991 }
1992
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001993
1994 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1995 assert(0 && "unprocessed InitListExpr type");
1996}
Ted Kremenekf233d482008-02-05 00:26:40 +00001997
Sebastian Redl05189992008-11-11 17:56:53 +00001998/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1999void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2000 NodeTy* Pred,
2001 NodeSet& Dst) {
2002 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002003 uint64_t amt;
2004
2005 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002006 if (T == getContext().VoidTy) {
2007 // sizeof(void) == 1 byte.
2008 amt = 1;
2009 }
2010 else if (!T.getTypePtr()->isConstantSizeType()) {
2011 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002012 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002013 }
2014 else if (T->isObjCInterfaceType()) {
2015 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2016 // the compiler has laid out its representation. Just report Unknown
2017 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002018 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002019 }
2020 else {
2021 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002022 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002023 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002024 }
2025 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002026 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002027
Ted Kremenek0e561a32008-03-21 21:30:14 +00002028 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002029 BindExpr(GetState(Pred), Ex,
2030 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002031}
2032
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002033
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002034void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002035 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002036
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002037 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002038
2039 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002040 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002041
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002042 case UnaryOperator::Deref: {
2043
2044 Expr* Ex = U->getSubExpr()->IgnoreParens();
2045 NodeSet Tmp;
2046 Visit(Ex, Pred, Tmp);
2047
2048 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002049
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002050 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002051 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002052
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002053 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002054 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002055 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002056 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002057 }
2058
2059 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002060 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002061
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002062 case UnaryOperator::Real: {
2063
2064 Expr* Ex = U->getSubExpr()->IgnoreParens();
2065 NodeSet Tmp;
2066 Visit(Ex, Pred, Tmp);
2067
2068 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2069
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002070 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002071 if (Ex->getType()->isAnyComplexType()) {
2072 // Just report "Unknown."
2073 Dst.Add(*I);
2074 continue;
2075 }
2076
2077 // For all other types, UnaryOperator::Real is an identity operation.
2078 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002079 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002080 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002081 }
2082
2083 return;
2084 }
2085
2086 case UnaryOperator::Imag: {
2087
2088 Expr* Ex = U->getSubExpr()->IgnoreParens();
2089 NodeSet Tmp;
2090 Visit(Ex, Pred, Tmp);
2091
2092 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002093 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002094 if (Ex->getType()->isAnyComplexType()) {
2095 // Just report "Unknown."
2096 Dst.Add(*I);
2097 continue;
2098 }
2099
2100 // For all other types, UnaryOperator::Float returns 0.
2101 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002102 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002103 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002104 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002105 }
2106
2107 return;
2108 }
2109
2110 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002111 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002112 Dst.Add(Pred);
2113 return;
2114
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002115 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002116 case UnaryOperator::Extension: {
2117
2118 // Unary "+" is a no-op, similar to a parentheses. We still have places
2119 // where it may be a block-level expression, so we need to
2120 // generate an extra node that just propagates the value of the
2121 // subexpression.
2122
2123 Expr* Ex = U->getSubExpr()->IgnoreParens();
2124 NodeSet Tmp;
2125 Visit(Ex, Pred, Tmp);
2126
2127 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002128 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002129 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002130 }
2131
2132 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002133 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002134
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002135 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002136
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002137 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002138 Expr* Ex = U->getSubExpr()->IgnoreParens();
2139 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002140 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002141
2142 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002143 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002144 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002145 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002146 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002147 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002148
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002149 return;
2150 }
2151
2152 case UnaryOperator::LNot:
2153 case UnaryOperator::Minus:
2154 case UnaryOperator::Not: {
2155
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002156 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002157 Expr* Ex = U->getSubExpr()->IgnoreParens();
2158 NodeSet Tmp;
2159 Visit(Ex, Pred, Tmp);
2160
2161 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002162 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002163
2164 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002165 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002166
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002167 if (V.isUnknownOrUndef()) {
2168 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2169 continue;
2170 }
2171
Ted Kremenek60595da2008-11-15 04:01:56 +00002172// QualType DstT = getContext().getCanonicalType(U->getType());
2173// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2174//
2175// if (DstT != SrcT) // Perform promotions.
2176// V = EvalCast(V, DstT);
2177//
2178// if (V.isUnknownOrUndef()) {
2179// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2180// continue;
2181// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002182
2183 switch (U->getOpcode()) {
2184 default:
2185 assert(false && "Invalid Opcode.");
2186 break;
2187
2188 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002189 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002190 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002191 break;
2192
2193 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002194 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002195 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002196 break;
2197
2198 case UnaryOperator::LNot:
2199
2200 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2201 //
2202 // Note: technically we do "E == 0", but this is the same in the
2203 // transfer functions as "0 == E".
2204
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002205 if (isa<Loc>(V)) {
2206 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2207 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002208 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002209 }
2210 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002211 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002212#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002213 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2214 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002215#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002216 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002217 continue;
2218#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002219 }
2220
2221 break;
2222 }
2223
2224 MakeNode(Dst, U, *I, St);
2225 }
2226
2227 return;
2228 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002229 }
2230
2231 // Handle ++ and -- (both pre- and post-increment).
2232
2233 assert (U->isIncrementDecrementOp());
2234 NodeSet Tmp;
2235 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002236 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002237
2238 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2239
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002240 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002241 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002242
2243 // Perform a load.
2244 NodeSet Tmp2;
2245 EvalLoad(Tmp2, Ex, *I, St, V1);
2246
2247 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2248
2249 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002250 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002251
2252 // Propagate unknown and undefined values.
2253 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002254 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002255 continue;
2256 }
2257
Ted Kremenek443003b2008-02-21 19:29:23 +00002258 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002259
2260 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2261 : BinaryOperator::Sub;
2262
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002263 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002264 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002265
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002266 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002267 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002268 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002269 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002270}
2271
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002272void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2273 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2274}
2275
2276void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2277 AsmStmt::outputs_iterator I,
2278 AsmStmt::outputs_iterator E,
2279 NodeTy* Pred, NodeSet& Dst) {
2280 if (I == E) {
2281 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2282 return;
2283 }
2284
2285 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002286 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002287
2288 ++I;
2289
2290 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2291 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2292}
2293
2294void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2295 AsmStmt::inputs_iterator I,
2296 AsmStmt::inputs_iterator E,
2297 NodeTy* Pred, NodeSet& Dst) {
2298 if (I == E) {
2299
2300 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002301 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002302
2303 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2304 // which interprets the inline asm and stores proper results in the
2305 // outputs.
2306
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002307 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002308
2309 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2310 OE = A->end_outputs(); OI != OE; ++OI) {
2311
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002312 SVal X = GetSVal(St, *OI);
2313 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002314
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002315 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002316 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002317 }
2318
Ted Kremenek0e561a32008-03-21 21:30:14 +00002319 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002320 return;
2321 }
2322
2323 NodeSet Tmp;
2324 Visit(*I, Pred, Tmp);
2325
2326 ++I;
2327
2328 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2329 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2330}
2331
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002332void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2333 assert (Builder && "GRStmtNodeBuilder must be defined.");
2334
2335 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002336
Ted Kremenek186350f2008-04-23 20:12:28 +00002337 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2338 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002339
Ted Kremenek729a9a22008-07-17 23:15:45 +00002340 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002341
Ted Kremenekb0533962008-04-18 20:35:30 +00002342 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002343
Ted Kremenekb0533962008-04-18 20:35:30 +00002344 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002345 MakeNode(Dst, S, Pred, GetState(Pred));
2346}
2347
Ted Kremenek02737ed2008-03-31 15:02:58 +00002348void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2349
2350 Expr* R = S->getRetValue();
2351
2352 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002353 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002354 return;
2355 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002356
Ted Kremenek5917d782008-11-21 00:27:44 +00002357 NodeSet Tmp;
2358 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002359
Ted Kremenek5917d782008-11-21 00:27:44 +00002360 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2361 SVal X = GetSVal((*I)->getState(), R);
2362
2363 // Check if we return the address of a stack variable.
2364 if (isa<loc::MemRegionVal>(X)) {
2365 // Determine if the value is on the stack.
2366 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002367
Ted Kremenek5917d782008-11-21 00:27:44 +00002368 if (R && getStateManager().hasStackStorage(R)) {
2369 // Create a special node representing the error.
2370 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2371 N->markAsSink();
2372 RetsStackAddr.insert(N);
2373 }
2374 continue;
2375 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002376 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002377 // Check if we return an undefined value.
2378 else if (X.isUndef()) {
2379 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2380 N->markAsSink();
2381 RetsUndef.insert(N);
2382 }
2383 continue;
2384 }
2385
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002386 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002387 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002388}
Ted Kremenek55deb972008-03-25 00:34:37 +00002389
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002390//===----------------------------------------------------------------------===//
2391// Transfer functions: Binary operators.
2392//===----------------------------------------------------------------------===//
2393
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002394const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2395 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002396
2397 // Divide by undefined? (potentially zero)
2398
2399 if (Denom.isUndef()) {
2400 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2401
2402 if (DivUndef) {
2403 DivUndef->markAsSink();
2404 ExplicitBadDivides.insert(DivUndef);
2405 }
2406
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002407 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002408 }
2409
2410 // Check for divide/remainder-by-zero.
2411 // First, "assume" that the denominator is 0 or undefined.
2412
2413 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002414 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002415
2416 // Second, "assume" that the denominator cannot be 0.
2417
2418 bool isFeasibleNotZero = false;
2419 St = Assume(St, Denom, true, isFeasibleNotZero);
2420
2421 // Create the node for the divide-by-zero (if it occurred).
2422
2423 if (isFeasibleZero)
2424 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2425 DivZeroNode->markAsSink();
2426
2427 if (isFeasibleNotZero)
2428 ImplicitBadDivides.insert(DivZeroNode);
2429 else
2430 ExplicitBadDivides.insert(DivZeroNode);
2431
2432 }
2433
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002434 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002435}
2436
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002437void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002438 GRExprEngine::NodeTy* Pred,
2439 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002440
2441 NodeSet Tmp1;
2442 Expr* LHS = B->getLHS()->IgnoreParens();
2443 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002444
Ted Kremenek759623e2008-12-06 02:39:30 +00002445 // FIXME: Add proper support for ObjCKVCRefExpr.
2446 if (isa<ObjCKVCRefExpr>(LHS)) {
2447 Visit(RHS, Pred, Dst);
2448 return;
2449 }
2450
2451
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002452 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002453 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002454 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002455 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002456
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002457 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002458
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002459 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002460
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002461 // Process the RHS.
2462
2463 NodeSet Tmp2;
2464 Visit(RHS, *I1, Tmp2);
2465
2466 // With both the LHS and RHS evaluated, process the operation itself.
2467
2468 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002469
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002470 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002471 const GRState* OldSt = St;
2472
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002473 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002474 BinaryOperator::Opcode Op = B->getOpcode();
2475
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002476 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002477
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002478 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002479
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002480 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002481 // FIXME: Handle structs.
2482 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002483
Ted Kremenek062e2f92008-11-13 06:10:40 +00002484 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2485 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002486 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002487 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002488
Ted Kremenek062e2f92008-11-13 06:10:40 +00002489 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002490 ? cast<SVal>(loc::SymbolVal(Sym))
2491 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002492 }
2493
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002494 // Simulate the effects of a "store": bind the value of the RHS
2495 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002496
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002497 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002498 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002499 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002500
2501 case BinaryOperator::Div:
2502 case BinaryOperator::Rem:
2503
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002504 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002505 if (RHS->getType()->isIntegerType() &&
2506 RHS->getType()->isScalarType()) {
2507
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002508 St = CheckDivideZero(B, St, *I2, RightV);
2509 if (!St) continue;
2510 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002511
2512 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002513
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002514 default: {
2515
2516 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002517 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002518
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002519 // Process non-assignements except commas or short-circuited
2520 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002521
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002522 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002523
2524 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002525 if (OldSt != St) {
2526 // Generate a new node if we have already created a new state.
2527 MakeNode(Dst, B, *I2, St);
2528 }
2529 else
2530 Dst.Add(*I2);
2531
Ted Kremenek89063af2008-02-21 19:15:37 +00002532 continue;
2533 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002534
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002535 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002536
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002537 // The operands were *not* undefined, but the result is undefined.
2538 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002539
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002540 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002541 UndefNode->markAsSink();
2542 UndefResults.insert(UndefNode);
2543 }
2544
2545 continue;
2546 }
2547
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002548 // Otherwise, create a new node.
2549
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002550 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002551 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002552 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002553 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002554
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002555 assert (B->isCompoundAssignmentOp());
2556
Ted Kremenek934e3e92008-10-27 23:02:39 +00002557 if (Op >= BinaryOperator::AndAssign) {
2558 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2559 BinaryOperator::And));
2560 }
2561 else {
2562 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2563 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002564
2565 // Perform a load (the LHS). This performs the checks for
2566 // null dereferences, and so on.
2567 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002568 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002569 EvalLoad(Tmp3, LHS, *I2, St, location);
2570
2571 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2572
2573 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002574 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002575
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002576 // Check for divide-by-zero.
2577 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002578 && RHS->getType()->isIntegerType()
2579 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002580
2581 // CheckDivideZero returns a new state where the denominator
2582 // is assumed to be non-zero.
2583 St = CheckDivideZero(B, St, *I3, RightV);
2584
2585 if (!St)
2586 continue;
2587 }
2588
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002589 // Propagate undefined values (left-side).
2590 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002591 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002592 continue;
2593 }
2594
2595 // Propagate unknown values (left and right-side).
2596 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002597 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002598 UnknownVal());
2599 continue;
2600 }
2601
2602 // At this point:
2603 //
2604 // The LHS is not Undef/Unknown.
2605 // The RHS is not Unknown.
2606
2607 // Get the computation type.
2608 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002609 CTy = getContext().getCanonicalType(CTy);
2610
2611 QualType LTy = getContext().getCanonicalType(LHS->getType());
2612 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002613
2614 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002615 if (LTy != CTy) V = EvalCast(V, CTy);
2616 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002617
2618 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002619 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002620 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002621 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002622 continue;
2623 }
2624
Ted Kremenek60595da2008-11-15 04:01:56 +00002625 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002626 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002627
2628 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002629 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002630 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2631 UndefNode->markAsSink();
2632 UndefResults.insert(UndefNode);
2633 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002634 continue;
2635 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002636
2637 // EXPERIMENTAL: "Conjured" symbols.
2638 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002639
2640 SVal LHSVal;
2641
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002642 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2643 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002644
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002645 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002646
Ted Kremenek60595da2008-11-15 04:01:56 +00002647 // The symbolic value is actually for the type of the left-hand side
2648 // expression, not the computation type, as this is the value the
2649 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002650 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002651 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002652 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002653 : cast<SVal>(nonloc::SymbolVal(Sym));
2654
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002655 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002656 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002657 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002658 else {
2659 // The left-hand side may bind to a different value then the
2660 // computation type.
2661 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2662 }
2663
2664 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002665 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002666 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002667 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002668}
Ted Kremenekee985462008-01-16 18:18:48 +00002669
2670//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002671// Transfer-function Helpers.
2672//===----------------------------------------------------------------------===//
2673
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002674void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002675 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002676 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002677 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002678
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002679 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002680 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2681
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002682 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002683 MakeNode(Dst, Ex, Pred, *I);
2684}
2685
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002686void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002687 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002688 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002689
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002690 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002691 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002692}
2693
2694//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002695// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002696//===----------------------------------------------------------------------===//
2697
Ted Kremenekaa66a322008-01-16 21:46:15 +00002698#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002699static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002700static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002701
Ted Kremenekaa66a322008-01-16 21:46:15 +00002702namespace llvm {
2703template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002704struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002705 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002706
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002707 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2708
2709 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002710 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002711 GraphPrintCheckerState->isUndefDeref(N) ||
2712 GraphPrintCheckerState->isUndefStore(N) ||
2713 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002714 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2715 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002716 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002717 GraphPrintCheckerState->isBadCall(N) ||
2718 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002719 return "color=\"red\",style=\"filled\"";
2720
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002721 if (GraphPrintCheckerState->isNoReturnCall(N))
2722 return "color=\"blue\",style=\"filled\"";
2723
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002724 return "";
2725 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002726
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002727 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002728 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002729
2730 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002731 ProgramPoint Loc = N->getLocation();
2732
2733 switch (Loc.getKind()) {
2734 case ProgramPoint::BlockEntranceKind:
2735 Out << "Block Entrance: B"
2736 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2737 break;
2738
2739 case ProgramPoint::BlockExitKind:
2740 assert (false);
2741 break;
2742
Ted Kremenekaa66a322008-01-16 21:46:15 +00002743 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002744 if (isa<PostStmt>(Loc)) {
2745 const PostStmt& L = cast<PostStmt>(Loc);
2746 Stmt* S = L.getStmt();
2747 SourceLocation SLoc = S->getLocStart();
2748
2749 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2750 llvm::raw_os_ostream OutS(Out);
2751 S->printPretty(OutS);
2752 OutS.flush();
2753
2754 if (SLoc.isFileID()) {
2755 Out << "\\lline="
2756 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2757 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2758 }
2759
2760 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2761 Out << "\\|Implicit-Null Dereference.\\l";
2762 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2763 Out << "\\|Explicit-Null Dereference.\\l";
2764 else if (GraphPrintCheckerState->isUndefDeref(N))
2765 Out << "\\|Dereference of undefialied value.\\l";
2766 else if (GraphPrintCheckerState->isUndefStore(N))
2767 Out << "\\|Store to Undefined Loc.";
2768 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2769 Out << "\\|Explicit divide-by zero or undefined value.";
2770 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2771 Out << "\\|Implicit divide-by zero or undefined value.";
2772 else if (GraphPrintCheckerState->isUndefResult(N))
2773 Out << "\\|Result of operation is undefined.";
2774 else if (GraphPrintCheckerState->isNoReturnCall(N))
2775 Out << "\\|Call to function marked \"noreturn\".";
2776 else if (GraphPrintCheckerState->isBadCall(N))
2777 Out << "\\|Call to NULL/Undefined.";
2778 else if (GraphPrintCheckerState->isUndefArg(N))
2779 Out << "\\|Argument in call is undefined";
2780
2781 break;
2782 }
2783
Ted Kremenekaa66a322008-01-16 21:46:15 +00002784 const BlockEdge& E = cast<BlockEdge>(Loc);
2785 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2786 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002787
2788 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002789
2790 SourceLocation SLoc = T->getLocStart();
2791
Ted Kremenekb38911f2008-01-30 23:03:39 +00002792 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002793
Ted Kremeneka95d3752008-09-13 05:16:45 +00002794 llvm::raw_os_ostream OutS(Out);
2795 E.getSrc()->printTerminator(OutS);
2796 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002797
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002798 if (SLoc.isFileID()) {
2799 Out << "\\lline="
2800 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2801 << GraphPrintSourceManager->getColumnNumber(SLoc);
2802 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002803
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002804 if (isa<SwitchStmt>(T)) {
2805 Stmt* Label = E.getDst()->getLabel();
2806
2807 if (Label) {
2808 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2809 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002810 llvm::raw_os_ostream OutS(Out);
2811 C->getLHS()->printPretty(OutS);
2812 OutS.flush();
2813
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002814 if (Stmt* RHS = C->getRHS()) {
2815 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002816 RHS->printPretty(OutS);
2817 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002818 }
2819
2820 Out << ":";
2821 }
2822 else {
2823 assert (isa<DefaultStmt>(Label));
2824 Out << "\\ldefault:";
2825 }
2826 }
2827 else
2828 Out << "\\l(implicit) default:";
2829 }
2830 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002831 // FIXME
2832 }
2833 else {
2834 Out << "\\lCondition: ";
2835 if (*E.getSrc()->succ_begin() == E.getDst())
2836 Out << "true";
2837 else
2838 Out << "false";
2839 }
2840
2841 Out << "\\l";
2842 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002843
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002844 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2845 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002846 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002847 }
2848 }
2849
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002850 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002851
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002852 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2853 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002854
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002855 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002856 return Out.str();
2857 }
2858};
2859} // end llvm namespace
2860#endif
2861
Ted Kremenekffe0f432008-03-07 22:58:01 +00002862#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002863
2864template <typename ITERATOR>
2865GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2866
2867template <>
2868GRExprEngine::NodeTy*
2869GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2870 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2871 return I->first;
2872}
2873
Ted Kremenekffe0f432008-03-07 22:58:01 +00002874template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002875static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002876 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002877
Ted Kremenekd4527582008-09-16 18:44:52 +00002878 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002879
2880 for ( ; I != E; ++I ) {
2881 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002882 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002883
Ted Kremenekd4527582008-09-16 18:44:52 +00002884 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002885 continue;
2886
Ted Kremenekd4527582008-09-16 18:44:52 +00002887 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002888 Sources.push_back(N);
2889 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002890}
2891#endif
2892
2893void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002894#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002895 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002896 std::vector<NodeTy*> Src;
2897
2898 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002899 AddSources(Src, null_derefs_begin(), null_derefs_end());
2900 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2901 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2902 AddSources(Src, undef_results_begin(), undef_results_end());
2903 AddSources(Src, bad_calls_begin(), bad_calls_end());
2904 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002905 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002906
Ted Kremenekcb612922008-04-18 19:23:43 +00002907 // The new way.
2908 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2909 (*I)->GetErrorNodes(Src);
2910
2911
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002912 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002913 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002914 else {
2915 GraphPrintCheckerState = this;
2916 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002917
Ted Kremenekffe0f432008-03-07 22:58:01 +00002918 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002919
2920 GraphPrintCheckerState = NULL;
2921 GraphPrintSourceManager = NULL;
2922 }
2923#endif
2924}
2925
2926void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2927#ifndef NDEBUG
2928 GraphPrintCheckerState = this;
2929 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002930
Ted Kremenek493d7a22008-03-11 18:25:33 +00002931 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2932
2933 if (!TrimmedG)
2934 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2935 else {
2936 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2937 delete TrimmedG;
2938 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002939
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002940 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002941 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002942#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002943}