blob: 8656ef4a90eeb2f6b8c42e6c367f143205c5944c [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 Kremenekbdb435d2008-07-11 18:37:32 +0000214 // Create the cleaned state.
Zhongxing Xuff944a82008-12-22 01:52:37 +0000215 if (PurgeDead)
216 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt,
218 Liveness, DeadSymbols);
219 else
220 CleanedState = EntryNode->getState();
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000221
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000223 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000224
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000225 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000226 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000227 else {
228 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000229 SaveOr OldHasGen(Builder->HasGeneratedNode);
230
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000231 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
232 Builder->PurgingDeadSymbols = true;
233
Ted Kremenek729a9a22008-07-17 23:15:45 +0000234 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000235 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000236
237 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
238 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000239 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000240
241 bool HasAutoGenerated = false;
242
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000243 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000244
245 NodeSet Dst;
246
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000247 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000248 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
249
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000250 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000251 Visit(S, *I, Dst);
252
253 // Do we need to auto-generate a node? We only need to do this to generate
254 // a node with a "cleaned" state; GRCoreEngine will actually handle
255 // auto-transitions for other cases.
256 if (Dst.size() == 1 && *Dst.begin() == EntryNode
257 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
258 HasAutoGenerated = true;
259 builder.generateNode(S, GetState(EntryNode), *I);
260 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000261 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000263 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000264 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000265 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000266
267 // FIXME: Consolidate.
268 StateMgr.CurrentStmt = 0;
269 CurrentStmt = 0;
270
Ted Kremenek846d4e92008-04-24 23:35:58 +0000271 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000272}
273
274void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
275
276 // FIXME: add metadata to the CFG so that we can disable
277 // this check when we KNOW that there is no block-level subexpression.
278 // The motivation is that this check requires a hashtable lookup.
279
280 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
281 Dst.Add(Pred);
282 return;
283 }
284
285 switch (S->getStmtClass()) {
286
287 default:
288 // Cases we intentionally have "default" handle:
289 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
290
291 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
292 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000293
294 case Stmt::ArraySubscriptExprClass:
295 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
296 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000297
298 case Stmt::AsmStmtClass:
299 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
300 break;
301
302 case Stmt::BinaryOperatorClass: {
303 BinaryOperator* B = cast<BinaryOperator>(S);
304
305 if (B->isLogicalOp()) {
306 VisitLogicalExpr(B, Pred, Dst);
307 break;
308 }
309 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000310 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000311 MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000312 break;
313 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000314
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000315 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
316 break;
317 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000318
Douglas Gregorb4609802008-11-14 16:09:21 +0000319 case Stmt::CallExprClass:
320 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000321 CallExpr* C = cast<CallExpr>(S);
322 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000323 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000325
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000326 // FIXME: ChooseExpr is really a constant. We need to fix
327 // the CFG do not model them as explicit control-flow.
328
329 case Stmt::ChooseExprClass: { // __builtin_choose_expr
330 ChooseExpr* C = cast<ChooseExpr>(S);
331 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
332 break;
333 }
334
335 case Stmt::CompoundAssignOperatorClass:
336 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
337 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000338
339 case Stmt::CompoundLiteralExprClass:
340 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
341 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000342
343 case Stmt::ConditionalOperatorClass: { // '?' operator
344 ConditionalOperator* C = cast<ConditionalOperator>(S);
345 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
346 break;
347 }
348
349 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000350 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000351 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000352 break;
353
354 case Stmt::DeclStmtClass:
355 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
356 break;
357
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000358 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000359 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000360 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000361 VisitCast(C, C->getSubExpr(), Pred, Dst);
362 break;
363 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000364
365 case Stmt::InitListExprClass:
366 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
367 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000368
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000369 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000370 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
371 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000372
373 case Stmt::ObjCIvarRefExprClass:
374 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
375 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000376
377 case Stmt::ObjCForCollectionStmtClass:
378 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
379 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000380
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000381 case Stmt::ObjCMessageExprClass: {
382 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
383 break;
384 }
385
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000386 case Stmt::ObjCAtThrowStmtClass: {
387 // FIXME: This is not complete. We basically treat @throw as
388 // an abort.
389 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
390 Builder->BuildSinks = true;
391 MakeNode(Dst, S, Pred, GetState(Pred));
392 break;
393 }
394
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000395 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000396 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000397 break;
398
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000399 case Stmt::ReturnStmtClass:
400 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
401 break;
402
Sebastian Redl05189992008-11-11 17:56:53 +0000403 case Stmt::SizeOfAlignOfExprClass:
404 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 break;
406
407 case Stmt::StmtExprClass: {
408 StmtExpr* SE = cast<StmtExpr>(S);
409
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000410 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411
412 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
413 // probably just remove these from the CFG.
414 assert (!SE->getSubStmt()->body_empty());
415
416 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000417 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000418 else
419 Dst.Add(Pred);
420
421 break;
422 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000423
424 case Stmt::StringLiteralClass:
425 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
426 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000427
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000428 case Stmt::UnaryOperatorClass:
429 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000430 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000431 }
432}
433
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000434void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000435
436 Ex = Ex->IgnoreParens();
437
438 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
439 Dst.Add(Pred);
440 return;
441 }
442
443 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000444
445 case Stmt::ArraySubscriptExprClass:
446 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
447 return;
448
449 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000450 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
452 return;
453
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000454 case Stmt::ObjCIvarRefExprClass:
455 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
456 return;
457
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000458 case Stmt::UnaryOperatorClass:
459 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
460 return;
461
462 case Stmt::MemberExprClass:
463 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
464 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000465
Ted Kremenek4f090272008-10-27 21:54:31 +0000466 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000467 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000468 return;
469
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000470 case Stmt::ObjCPropertyRefExprClass:
471 // FIXME: Property assignments are lvalues, but not really "locations".
472 // e.g.: self.x = something;
473 // Here the "self.x" really can translate to a method call (setter) when
474 // the assignment is made. Moreover, the entire assignment expression
475 // evaluate to whatever "something" is, not calling the "getter" for
476 // the property (which would make sense since it can have side effects).
477 // We'll probably treat this as a location, but not one that we can
478 // take the address of. Perhaps we need a new SVal class for cases
479 // like thsis?
480 // Note that we have a similar problem for bitfields, since they don't
481 // have "locations" in the sense that we can take their address.
482 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000483 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000484
485 case Stmt::StringLiteralClass: {
486 const GRState* St = GetState(Pred);
487 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000488 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000489 return;
490 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000491
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000492 default:
493 // Arbitrary subexpressions can return aggregate temporaries that
494 // can be used in a lvalue context. We need to enhance our support
495 // of such temporaries in both the environment and the store, so right
496 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000497 assert ((Ex->getType()->isAggregateType() ||
498 Ex->getType()->isUnionType()) &&
499 "Other kinds of expressions with non-aggregate/union types do"
500 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000501
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000502 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000503 }
504}
505
506//===----------------------------------------------------------------------===//
507// Block entrance. (Update counters).
508//===----------------------------------------------------------------------===//
509
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000510bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000511 GRBlockCounter BC) {
512
513 return BC.getNumVisited(B->getBlockID()) < 3;
514}
515
516//===----------------------------------------------------------------------===//
517// Branch processing.
518//===----------------------------------------------------------------------===//
519
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000520const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000521 Stmt* Terminator,
522 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000523
524 switch (Terminator->getStmtClass()) {
525 default:
526 return St;
527
528 case Stmt::BinaryOperatorClass: { // '&&' and '||'
529
530 BinaryOperator* B = cast<BinaryOperator>(Terminator);
531 BinaryOperator::Opcode Op = B->getOpcode();
532
533 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
534
535 // For &&, if we take the true branch, then the value of the whole
536 // expression is that of the RHS expression.
537 //
538 // For ||, if we take the false branch, then the value of the whole
539 // expression is that of the RHS expression.
540
541 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
542 (Op == BinaryOperator::LOr && !branchTaken)
543 ? B->getRHS() : B->getLHS();
544
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000545 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000546 }
547
548 case Stmt::ConditionalOperatorClass: { // ?:
549
550 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
551
552 // For ?, if branchTaken == true then the value is either the LHS or
553 // the condition itself. (GNU extension).
554
555 Expr* Ex;
556
557 if (branchTaken)
558 Ex = C->getLHS() ? C->getLHS() : C->getCond();
559 else
560 Ex = C->getRHS();
561
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000562 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000563 }
564
565 case Stmt::ChooseExprClass: { // ?:
566
567 ChooseExpr* C = cast<ChooseExpr>(Terminator);
568
569 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000570 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000571 }
572 }
573}
574
Ted Kremenekaf337412008-11-12 19:24:17 +0000575void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000576 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000577
Ted Kremeneke7d22112008-02-11 19:21:59 +0000578 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000579 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000580 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000581
Ted Kremenekb2331832008-02-15 22:29:00 +0000582 // Check for NULL conditions; e.g. "for(;;)"
583 if (!Condition) {
584 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000585 return;
586 }
587
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000588 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000589
590 switch (V.getBaseKind()) {
591 default:
592 break;
593
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000594 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000595 builder.generateNode(MarkBranch(PrevState, Term, true), true);
596 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000597 return;
598
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000599 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000600 NodeTy* N = builder.generateNode(PrevState, true);
601
602 if (N) {
603 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000604 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000605 }
606
607 builder.markInfeasible(false);
608 return;
609 }
610 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000611
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000612 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000613
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000614 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000615 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000616
617 if (isFeasible)
618 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000619 else
620 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000621
622 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000623
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000624 isFeasible = false;
625 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000626
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000627 if (isFeasible)
628 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000629 else
630 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000631}
632
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000633/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000634/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000635void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000636
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000637 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000638 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000639
640 // Three possibilities:
641 //
642 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000643 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000644 // (3) We have no clue about the label. Dispatch to all targets.
645 //
646
647 typedef IndirectGotoNodeBuilder::iterator iterator;
648
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000649 if (isa<loc::GotoLabel>(V)) {
650 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000651
652 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000653 if (I.getLabel() == L) {
654 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000655 return;
656 }
657 }
658
659 assert (false && "No block with label.");
660 return;
661 }
662
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000663 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000664 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000665 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000666 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000667 return;
668 }
669
670 // This is really a catch-all. We don't support symbolics yet.
671
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000672 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000673
674 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000675 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000676}
Ted Kremenekf233d482008-02-05 00:26:40 +0000677
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000678
679void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
680 NodeTy* Pred, NodeSet& Dst) {
681
682 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
683
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000684 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000685 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000686
687 assert (X.isUndef());
688
689 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
690
691 assert (SE);
692
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000693 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000694
695 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000696 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000697}
698
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000699/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
700/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000701void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
702 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000703 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000704 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000705 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000707 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000708 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000709 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000710 return;
711 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000712
Ted Kremenek72afb372009-01-17 01:54:16 +0000713 const GRState* DefaultSt = St;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000714 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000715
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000716 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000717 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000718
719 // Evaluate the LHS of the case value.
720 Expr::EvalResult V1;
721 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722
Ted Kremenek72afb372009-01-17 01:54:16 +0000723 // Sanity checks. These go away in Release builds.
724 assert(b && V1.Val.isInt() && !V1.HasSideEffects
725 && "Case condition must evaluate to an integer constant.");
726 b = b; // silence unused variable warning
727 assert(V1.Val.getInt().getBitWidth() ==
728 getContext().getTypeSize(CondE->getType()));
729
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000730 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000731 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000732
733 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000734 b = E->Evaluate(V2, getContext());
735 assert(b && V2.Val.isInt() && !V2.HasSideEffects
736 && "Case condition must evaluate to an integer constant.");
737 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000739 else
740 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000741
742 // FIXME: Eventually we should replace the logic below with a range
743 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000744 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000745
Ted Kremenek14a11402008-03-17 22:17:56 +0000746 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000747 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000748 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000749
Ted Kremenek72afb372009-01-17 01:54:16 +0000750 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000751 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000752 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000753
754 if (isFeasible) {
755 builder.generateCaseStmtNode(I, StNew);
756
757 // If CondV evaluates to a constant, then we know that this
758 // is the *only* case that we can take, so stop evaluating the
759 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000760 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000761 return;
762 }
763
764 // Now "assume" that the case doesn't match. Add this state
765 // to the default state (if it is feasible).
766
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000767 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000768 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000769
Ted Kremenek5014ab12008-04-23 05:03:18 +0000770 if (isFeasible) {
771 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000772 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000773 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000774
Ted Kremenek14a11402008-03-17 22:17:56 +0000775 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000776 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000777 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000778
Ted Kremenek72afb372009-01-17 01:54:16 +0000779 ++V1.Val.getInt();
780 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000781
782 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000783 }
784
785 // If we reach here, than we know that the default branch is
786 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000787 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000788}
789
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000790//===----------------------------------------------------------------------===//
791// Transfer functions: logical operations ('&&', '||').
792//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000793
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000794void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000795 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000796
Ted Kremenek05a23782008-02-26 19:05:15 +0000797 assert (B->getOpcode() == BinaryOperator::LAnd ||
798 B->getOpcode() == BinaryOperator::LOr);
799
800 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
801
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000802 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000803 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000804
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000805 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000806
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000807 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000808
809 assert (Ex);
810
811 if (Ex == B->getRHS()) {
812
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000813 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000814
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000815 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000816
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000817 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000818 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000819 return;
820 }
821
Ted Kremenek05a23782008-02-26 19:05:15 +0000822 // We took the RHS. Because the value of the '&&' or '||' expression must
823 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
824 // or 1. Alternatively, we could take a lazy approach, and calculate this
825 // value later when necessary. We don't have the machinery in place for
826 // this right now, and since most logical expressions are used for branches,
827 // the payoff is not likely to be large. Instead, we do eager evaluation.
828
829 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000830 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000831
832 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000833 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000834 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000835
836 isFeasible = false;
837 NewState = Assume(St, X, false, isFeasible);
838
839 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000840 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000841 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000842 }
843 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000844 // We took the LHS expression. Depending on whether we are '&&' or
845 // '||' we know what the value of the expression is via properties of
846 // the short-circuiting.
847
848 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000849 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000850 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000851}
Ted Kremenek05a23782008-02-26 19:05:15 +0000852
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000853//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000854// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000855//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000856
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000857void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
858 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000859
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000860 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000861
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000862 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000863
864 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
865
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000866 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000867
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000868 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000869 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000870 else
871 EvalLoad(Dst, Ex, Pred, St, V);
872 return;
873
874 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
875 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
876
877 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000878 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000879 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000880 return;
881
882 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000883 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000884 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000885 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000886 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000887 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000888
889 assert (false &&
890 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000891}
892
Ted Kremenek540cbe22008-04-22 04:56:29 +0000893/// VisitArraySubscriptExpr - Transfer function for array accesses
894void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000895 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000896
897 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000898 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000899 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000900 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000901
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000902 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000903 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000904 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000905
906 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000907 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000908 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000909
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000910 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000911 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000912 else
913 EvalLoad(Dst, A, *I2, St, V);
914 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000915 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000916}
917
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000918/// VisitMemberExpr - Transfer function for member expressions.
919void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000920 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000921
922 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000923 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000924
925 if (M->isArrow())
926 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
927 else
928 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
929
Douglas Gregor86f19402008-12-20 23:49:58 +0000930 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
931 if (!Field) // FIXME: skipping member expressions for non-fields
932 return;
933
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000934 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000935 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000936 // FIXME: Should we insert some assumption logic in here to determine
937 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000938 // later when using FieldOffset lvals (which we no longer have).
939 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000940
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000941 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000942 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000943 else
944 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000945 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000946}
947
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000948void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000949 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000950
951 assert (Builder && "GRStmtNodeBuilder must be defined.");
952
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8c354752008-12-16 22:02:27 +0000954 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000955
Ted Kremenek8c354752008-12-16 22:02:27 +0000956 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000957 return;
958
Ted Kremenek8c354752008-12-16 22:02:27 +0000959 St = GetState(Pred);
960
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000961 // Proceed with the store.
962
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000963 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000964
Ted Kremenek186350f2008-04-23 20:12:28 +0000965 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000966 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000967 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000968
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000970 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000971
Ted Kremenek729a9a22008-07-17 23:15:45 +0000972 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000973
974 // Handle the case where no nodes where generated. Auto-generate that
975 // contains the updated state if we aren't generating sinks.
976
Ted Kremenekb0533962008-04-18 20:35:30 +0000977 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000978 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000979 location, Val);
980}
981
982void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000983 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000984 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000985
Ted Kremenek8c354752008-12-16 22:02:27 +0000986 // Evaluate the location (checks for bad dereferences).
987 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000988
Ted Kremenek8c354752008-12-16 22:02:27 +0000989 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000990 return;
991
Ted Kremenek8c354752008-12-16 22:02:27 +0000992 St = GetState(Pred);
993
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000994 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000995 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000996
997 // FIXME: Currently symbolic analysis "generates" new symbols
998 // for the contents of values. We need a better approach.
999
1000 // FIXME: The "CheckOnly" option exists only because Array and Field
1001 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001002 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +00001003
Ted Kremenek8c354752008-12-16 22:02:27 +00001004 if (CheckOnly) {
1005 Dst.Add(Pred);
1006 return;
1007 }
1008
1009 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001010 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001011 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001012 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001013 else {
1014 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
1015 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1016 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001017}
1018
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001019void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001020 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001021
1022 NodeSet TmpDst;
1023 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1024
1025 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1026 MakeNode(Dst, Ex, *I, (*I)->getState());
1027}
1028
Ted Kremenek8c354752008-12-16 22:02:27 +00001029GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
1030 const GRState* St,
1031 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001032
1033 // Check for loads/stores from/to undefined values.
1034 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001035 NodeTy* N =
1036 Builder->generateNode(Ex, St, Pred,
1037 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001038
Ted Kremenek8c354752008-12-16 22:02:27 +00001039 if (N) {
1040 N->markAsSink();
1041 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001042 }
1043
Ted Kremenek8c354752008-12-16 22:02:27 +00001044 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001045 }
1046
1047 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1048 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001049 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001050
1051 // During a load, one of two possible situations arise:
1052 // (1) A crash, because the location (pointer) was NULL.
1053 // (2) The location (pointer) is not NULL, and the dereference works.
1054 //
1055 // We add these assumptions.
1056
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001057 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001058
1059 // "Assume" that the pointer is not NULL.
1060
1061 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001062 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001063
1064 // "Assume" that the pointer is NULL.
1065
1066 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001067 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1068 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001069
1070 if (isFeasibleNull) {
1071
Ted Kremenek7360fda2008-09-18 23:09:54 +00001072 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001073 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001074 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1075
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001076 // We don't use "MakeNode" here because the node will be a sink
1077 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001078 NodeTy* NullNode =
1079 Builder->generateNode(Ex, StNull, Pred,
1080 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001081
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001082 if (NullNode) {
1083
1084 NullNode->markAsSink();
1085
1086 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1087 else ExplicitNullDeref.insert(NullNode);
1088 }
1089 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001090
1091 if (!isFeasibleNotNull)
1092 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001093
1094 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001095 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001096 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1097 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1098 // Get the index of the accessed element.
1099 SVal Idx = ER->getIndex();
1100 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001101 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1102 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001103
1104 bool isFeasibleInBound = false;
1105 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1106 true, isFeasibleInBound);
1107
1108 bool isFeasibleOutBound = false;
1109 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1110 false, isFeasibleOutBound);
1111
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001112 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001113 // Report warning. Make sink node manually.
1114 NodeTy* OOBNode =
1115 Builder->generateNode(Ex, StOutBound, Pred,
1116 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001117
1118 if (OOBNode) {
1119 OOBNode->markAsSink();
1120
1121 if (isFeasibleInBound)
1122 ImplicitOOBMemAccesses.insert(OOBNode);
1123 else
1124 ExplicitOOBMemAccesses.insert(OOBNode);
1125 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001126 }
1127
Ted Kremenek8c354752008-12-16 22:02:27 +00001128 if (!isFeasibleInBound)
1129 return 0;
1130
1131 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001132 }
1133 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001134
Ted Kremenek8c354752008-12-16 22:02:27 +00001135 // Generate a new node indicating the checks succeed.
1136 return Builder->generateNode(Ex, StNotNull, Pred,
1137 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001138}
1139
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001140//===----------------------------------------------------------------------===//
1141// Transfer function: Function calls.
1142//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001143void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001144 CallExpr::arg_iterator AI,
1145 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001146 NodeSet& Dst)
1147{
1148 // Determine the type of function we're calling (if available).
1149 const FunctionTypeProto *Proto = NULL;
1150 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1151 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1152 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1153
1154 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1155}
1156
1157void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1158 CallExpr::arg_iterator AI,
1159 CallExpr::arg_iterator AE,
1160 NodeSet& Dst, const FunctionTypeProto *Proto,
1161 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001162
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001163 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001164 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001165 // If the call argument is being bound to a reference parameter,
1166 // visit it as an lvalue, not an rvalue.
1167 bool VisitAsLvalue = false;
1168 if (Proto && ParamIdx < Proto->getNumArgs())
1169 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1170
1171 NodeSet DstTmp;
1172 if (VisitAsLvalue)
1173 VisitLValue(*AI, Pred, DstTmp);
1174 else
1175 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001176 ++AI;
1177
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001178 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001179 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001180
1181 return;
1182 }
1183
1184 // If we reach here we have processed all of the arguments. Evaluate
1185 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001186
Ted Kremenek994a09b2008-02-25 21:16:03 +00001187 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001188 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001189
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001190 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001191
Ted Kremenekde434242008-02-19 01:44:53 +00001192 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001193 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1194
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001195 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001196 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001197
Ted Kremeneka1354a52008-03-03 16:47:31 +00001198 // FIXME: Add support for symbolic function calls (calls involving
1199 // function pointer values that are symbolic).
1200
1201 // Check for undefined control-flow or calls to NULL.
1202
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001203 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001204 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001205
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001206 if (N) {
1207 N->markAsSink();
1208 BadCalls.insert(N);
1209 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001210
Ted Kremenekde434242008-02-19 01:44:53 +00001211 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001212 }
1213
1214 // Check for the "noreturn" attribute.
1215
1216 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1217
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001218 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001219
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001220 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001221
1222 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001223 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001224 else {
1225 // HACK: Some functions are not marked noreturn, and don't return.
1226 // Here are a few hardwired ones. If this takes too long, we can
1227 // potentially cache these results.
1228 const char* s = FD->getIdentifier()->getName();
1229 unsigned n = strlen(s);
1230
1231 switch (n) {
1232 default:
1233 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001234
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001235 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001236 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1237 break;
1238
1239 case 5:
1240 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001241 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001242 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001243 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001244 // FIXME: use Assume to inspect the possible symbolic value of
1245 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001246 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001247 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001248 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001249 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001250 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001251 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001252
1253 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001254 if (!memcmp(s, "Assert", 6)) {
1255 Builder->BuildSinks = true;
1256 break;
1257 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001258
1259 // FIXME: This is just a wrapper around throwing an exception.
1260 // Eventually inter-procedural analysis should handle this easily.
1261 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1262
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001263 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001264
1265 case 7:
1266 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1267 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001268
Ted Kremenekf47bb782008-04-30 17:54:04 +00001269 case 8:
1270 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1271 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001272
1273 case 12:
1274 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1275 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001276
Ted Kremenekf9683082008-09-19 02:30:47 +00001277 case 13:
1278 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1279 break;
1280
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001281 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001282 if (!memcmp(s, "dtrace_assfail", 14) ||
1283 !memcmp(s, "yy_fatal_error", 14))
1284 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001285 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001286
1287 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001288 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1289 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001290 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001291
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001292 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001293 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001294
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001295 }
1296 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001297
1298 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001299
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001300 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001301
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001302 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001303
Ted Kremenek186350f2008-04-23 20:12:28 +00001304 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001305 switch (id) {
1306 case Builtin::BI__builtin_expect: {
1307 // For __builtin_expect, just return the value of the subexpression.
1308 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001309 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001310 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001311 continue;
1312 }
1313
Ted Kremenekb3021332008-11-02 00:35:01 +00001314 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001315 // FIXME: Refactor into StoreManager itself?
1316 MemRegionManager& RM = getStateManager().getRegionManager();
1317 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001318 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001319
1320 // Set the extent of the region in bytes. This enables us to use the
1321 // SVal of the argument directly. If we save the extent in bits, we
1322 // cannot represent values like symbol*8.
1323 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1324 St = getStoreManager().setExtent(St, R, Extent);
1325
Ted Kremenekb3021332008-11-02 00:35:01 +00001326 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1327 continue;
1328 }
1329
Ted Kremenek55aea312008-03-05 22:59:42 +00001330 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001331 break;
1332 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001333 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001334
Ted Kremenek186350f2008-04-23 20:12:28 +00001335 // Check any arguments passed-by-value against being undefined.
1336
1337 bool badArg = false;
1338
1339 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1340 I != E; ++I) {
1341
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001342 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001343 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001344
Ted Kremenek186350f2008-04-23 20:12:28 +00001345 if (N) {
1346 N->markAsSink();
1347 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001348 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001349
Ted Kremenek186350f2008-04-23 20:12:28 +00001350 badArg = true;
1351 break;
1352 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001353 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001354
1355 if (badArg)
1356 continue;
1357
1358 // Dispatch to the plug-in transfer function.
1359
1360 unsigned size = Dst.size();
1361 SaveOr OldHasGen(Builder->HasGeneratedNode);
1362 EvalCall(Dst, CE, L, *DI);
1363
1364 // Handle the case where no nodes where generated. Auto-generate that
1365 // contains the updated state if we aren't generating sinks.
1366
1367 if (!Builder->BuildSinks && Dst.size() == size &&
1368 !Builder->HasGeneratedNode)
1369 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001370 }
1371}
1372
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001373//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001374// Transfer function: Objective-C ivar references.
1375//===----------------------------------------------------------------------===//
1376
1377void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1378 NodeTy* Pred, NodeSet& Dst,
1379 bool asLValue) {
1380
1381 Expr* Base = cast<Expr>(Ex->getBase());
1382 NodeSet Tmp;
1383 Visit(Base, Pred, Tmp);
1384
1385 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1386 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001387 SVal BaseVal = GetSVal(St, Base);
1388 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001389
1390 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001391 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001392 else
1393 EvalLoad(Dst, Ex, *I, St, location);
1394 }
1395}
1396
1397//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001398// Transfer function: Objective-C fast enumeration 'for' statements.
1399//===----------------------------------------------------------------------===//
1400
1401void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1402 NodeTy* Pred, NodeSet& Dst) {
1403
1404 // ObjCForCollectionStmts are processed in two places. This method
1405 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1406 // statements within a basic block. This transfer function does two things:
1407 //
1408 // (1) binds the next container value to 'element'. This creates a new
1409 // node in the ExplodedGraph.
1410 //
1411 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1412 // whether or not the container has any more elements. This value
1413 // will be tested in ProcessBranch. We need to explicitly bind
1414 // this value because a container can contain nil elements.
1415 //
1416 // FIXME: Eventually this logic should actually do dispatches to
1417 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1418 // This will require simulating a temporary NSFastEnumerationState, either
1419 // through an SVal or through the use of MemRegions. This value can
1420 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1421 // terminates we reclaim the temporary (it goes out of scope) and we
1422 // we can test if the SVal is 0 or if the MemRegion is null (depending
1423 // on what approach we take).
1424 //
1425 // For now: simulate (1) by assigning either a symbol or nil if the
1426 // container is empty. Thus this transfer function will by default
1427 // result in state splitting.
1428
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001429 Stmt* elem = S->getElement();
1430 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001431
1432 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001433 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001434 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001435 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1436 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1437 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001438 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001439
1440 NodeSet Tmp;
1441 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001442
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001443 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1444 const GRState* state = GetState(*I);
1445 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1446 }
1447}
1448
1449void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1450 NodeTy* Pred, NodeSet& Dst,
1451 SVal ElementV) {
1452
1453
Ted Kremenekaf337412008-11-12 19:24:17 +00001454
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001455 // Get the current state. Use 'EvalLocation' to determine if it is a null
1456 // pointer, etc.
1457 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001458
Ted Kremenek8c354752008-12-16 22:02:27 +00001459 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1460 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001461 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001462
1463 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001464
Ted Kremenekaf337412008-11-12 19:24:17 +00001465 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001466 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001467 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1468 GRStateRef hasElems = state.BindExpr(S, TrueV);
1469
Ted Kremenekaf337412008-11-12 19:24:17 +00001470 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001471 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1472 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001473
1474 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1475 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1476 // FIXME: The proper thing to do is to really iterate over the
1477 // container. We will do this with dispatch logic to the store.
1478 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001479 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001480 assert (Loc::IsLocType(T));
1481 unsigned Count = Builder->getCurrentBlockCount();
1482 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1483 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001484
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001485 // Bind the location to 'nil' on the false branch.
1486 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1487 noElems = noElems.BindLoc(ElementV, nilV);
1488 }
1489
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001490 // Create the new nodes.
1491 MakeNode(Dst, S, Pred, hasElems);
1492 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001493}
1494
1495//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001496// Transfer function: Objective-C message expressions.
1497//===----------------------------------------------------------------------===//
1498
1499void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1500 NodeSet& Dst){
1501
1502 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1503 Pred, Dst);
1504}
1505
1506void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001507 ObjCMessageExpr::arg_iterator AI,
1508 ObjCMessageExpr::arg_iterator AE,
1509 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001510 if (AI == AE) {
1511
1512 // Process the receiver.
1513
1514 if (Expr* Receiver = ME->getReceiver()) {
1515 NodeSet Tmp;
1516 Visit(Receiver, Pred, Tmp);
1517
1518 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1519 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1520
1521 return;
1522 }
1523
1524 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1525 return;
1526 }
1527
1528 NodeSet Tmp;
1529 Visit(*AI, Pred, Tmp);
1530
1531 ++AI;
1532
1533 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1534 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1535}
1536
1537void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1538 NodeTy* Pred,
1539 NodeSet& Dst) {
1540
1541 // FIXME: More logic for the processing the method call.
1542
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001543 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001544 bool RaisesException = false;
1545
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001546
1547 if (Expr* Receiver = ME->getReceiver()) {
1548
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001549 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001550
1551 // Check for undefined control-flow or calls to NULL.
1552
1553 if (L.isUndef()) {
1554 NodeTy* N = Builder->generateNode(ME, St, Pred);
1555
1556 if (N) {
1557 N->markAsSink();
1558 UndefReceivers.insert(N);
1559 }
1560
1561 return;
1562 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001563
1564 // Check if the "raise" message was sent.
1565 if (ME->getSelector() == RaiseSel)
1566 RaisesException = true;
1567 }
1568 else {
1569
1570 IdentifierInfo* ClsName = ME->getClassName();
1571 Selector S = ME->getSelector();
1572
1573 // Check for special instance methods.
1574
1575 if (!NSExceptionII) {
1576 ASTContext& Ctx = getContext();
1577
1578 NSExceptionII = &Ctx.Idents.get("NSException");
1579 }
1580
1581 if (ClsName == NSExceptionII) {
1582
1583 enum { NUM_RAISE_SELECTORS = 2 };
1584
1585 // Lazily create a cache of the selectors.
1586
1587 if (!NSExceptionInstanceRaiseSelectors) {
1588
1589 ASTContext& Ctx = getContext();
1590
1591 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1592
1593 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1594 unsigned idx = 0;
1595
1596 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001597 II.push_back(&Ctx.Idents.get("raise"));
1598 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001599 NSExceptionInstanceRaiseSelectors[idx++] =
1600 Ctx.Selectors.getSelector(II.size(), &II[0]);
1601
1602 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001603 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001604 NSExceptionInstanceRaiseSelectors[idx++] =
1605 Ctx.Selectors.getSelector(II.size(), &II[0]);
1606 }
1607
1608 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1609 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1610 RaisesException = true; break;
1611 }
1612 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001613 }
1614
1615 // Check for any arguments that are uninitialized/undefined.
1616
1617 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1618 I != E; ++I) {
1619
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001620 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001621
1622 // Generate an error node for passing an uninitialized/undefined value
1623 // as an argument to a message expression. This node is a sink.
1624 NodeTy* N = Builder->generateNode(ME, St, Pred);
1625
1626 if (N) {
1627 N->markAsSink();
1628 MsgExprUndefArgs[N] = *I;
1629 }
1630
1631 return;
1632 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001633 }
1634
1635 // Check if we raise an exception. For now treat these as sinks. Eventually
1636 // we will want to handle exceptions properly.
1637
1638 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1639
1640 if (RaisesException)
1641 Builder->BuildSinks = true;
1642
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001643 // Dispatch to plug-in transfer function.
1644
1645 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001646 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001647
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001648 EvalObjCMessageExpr(Dst, ME, Pred);
1649
1650 // Handle the case where no nodes where generated. Auto-generate that
1651 // contains the updated state if we aren't generating sinks.
1652
Ted Kremenekb0533962008-04-18 20:35:30 +00001653 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001654 MakeNode(Dst, ME, Pred, St);
1655}
1656
1657//===----------------------------------------------------------------------===//
1658// Transfer functions: Miscellaneous statements.
1659//===----------------------------------------------------------------------===//
1660
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001661void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1662 QualType PtrTy,
1663 Expr* CastE, NodeTy* Pred,
1664 NodeSet& Dst) {
1665 if (!V.isUnknownOrUndef()) {
1666 // FIXME: Determine if the number of bits of the target type is
1667 // equal or exceeds the number of bits to store the pointer value.
1668 // If not, flag an error.
1669 unsigned bits = getContext().getTypeSize(PtrTy);
1670 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
1671 }
1672
1673 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
1674}
1675
1676
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001677void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001678 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001679 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001680 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001681
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001682 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001683 T = ExCast->getTypeAsWritten();
1684
Zhongxing Xued340f72008-10-22 08:02:16 +00001685 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001686 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001687 else
1688 Visit(Ex, Pred, S1);
1689
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001690 // Check for casting to "void".
1691 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001692
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001693 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001694 Dst.Add(*I1);
1695
Ted Kremenek874d63f2008-01-24 02:02:54 +00001696 return;
1697 }
1698
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001699 // FIXME: The rest of this should probably just go into EvalCall, and
1700 // let the transfer function object be responsible for constructing
1701 // nodes.
1702
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001703 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001704 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001705 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001706 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001707
1708 // Unknown?
1709
1710 if (V.isUnknown()) {
1711 Dst.Add(N);
1712 continue;
1713 }
1714
1715 // Undefined?
1716
1717 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001718 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001719 continue;
1720 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001721
1722 // For const casts, just propagate the value.
1723 ASTContext& C = getContext();
1724
1725 if (C.getCanonicalType(T).getUnqualifiedType() ==
1726 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001727 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001728 continue;
1729 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001730
1731 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001732 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001733 VisitCastPointerToInteger(V, St, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001734 continue;
1735 }
1736
1737 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001738 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1739 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001740 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001741 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001742 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001743 continue;
1744 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001745
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001746 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001747 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001748 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001749 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001750
1751 // Are we casting from an array to a pointer? If so just pass on
1752 // the decayed value.
1753 if (T->isPointerType()) {
1754 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
1755 continue;
1756 }
1757
1758 // Are we casting from an array to an integer? If so, cast the decayed
1759 // pointer value to an integer.
1760 assert(T->isIntegerType());
1761 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1762 QualType PointerTy = getContext().getPointerType(ElemTy);
1763 VisitCastPointerToInteger(V, St, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001764 continue;
1765 }
1766
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001767 // Check for casts from a region to a specific type.
1768 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001769 assert(Loc::IsLocType(T));
1770 assert(Loc::IsLocType(ExTy));
1771
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001772 const MemRegion* R = RV->getRegion();
1773 StoreManager& StoreMgr = getStoreManager();
1774
1775 // Delegate to store manager to get the result of casting a region
1776 // to a different type.
1777 const StoreManager::CastResult& Res = StoreMgr.CastRegion(St, R, T);
1778
1779 // Inspect the result. If the MemRegion* returned is NULL, this
1780 // expression evaluates to UnknownVal.
1781 R = Res.getRegion();
1782 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1783
1784 // Generate the new node in the ExplodedGraph.
1785 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001786 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001787 }
1788
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001789 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001790 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001791 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001792}
1793
Ted Kremenek4f090272008-10-27 21:54:31 +00001794void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001795 NodeTy* Pred, NodeSet& Dst,
1796 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001797 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1798 NodeSet Tmp;
1799 Visit(ILE, Pred, Tmp);
1800
1801 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001802 const GRState* St = GetState(*I);
1803 SVal ILV = GetSVal(St, ILE);
1804 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001805
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001806 if (asLValue)
1807 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1808 else
1809 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001810 }
1811}
1812
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001813void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001814
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001815 // The CFG has one DeclStmt per Decl.
1816 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001817
1818 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001819 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001820
Ted Kremenekefd59942008-12-08 22:47:34 +00001821 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001822 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001823
1824 // FIXME: static variables may have an initializer, but the second
1825 // time a function is called those values may not be current.
1826 NodeSet Tmp;
1827
Ted Kremenekaf337412008-11-12 19:24:17 +00001828 if (InitEx)
1829 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001830
1831 if (Tmp.empty())
1832 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001833
1834 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001835 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001836 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001837
1838 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001839 if (InitEx) {
1840 SVal InitVal = GetSVal(St, InitEx);
1841 QualType T = VD->getType();
1842
1843 // Recover some path-sensitivity if a scalar value evaluated to
1844 // UnknownVal.
1845 if (InitVal.isUnknown()) {
1846 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001847 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001848 InitVal = loc::SymbolVal(Sym);
1849 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001850 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001851 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001852 InitVal = nonloc::SymbolVal(Sym);
1853 }
1854 }
1855
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001856 St = StateMgr.BindDecl(St, VD, InitVal);
1857 } else
1858 St = StateMgr.BindDeclWithNoInit(St, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001859
1860 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1861 QualType T = getContext().getCanonicalType(VD->getType());
1862 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1863 // FIXME: Handle multi-dimensional VLAs.
1864
1865 Expr* SE = VLA->getSizeExpr();
1866 SVal Size = GetSVal(St, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001867
1868 if (Size.isUndef()) {
1869 if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
1870 N->markAsSink();
1871 ExplicitBadSizedVLA.insert(N);
1872 }
1873 continue;
1874 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001875
1876 bool isFeasibleZero = false;
1877 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1878
1879 bool isFeasibleNotZero = false;
1880 St = Assume(St, Size, true, isFeasibleNotZero);
1881
1882 if (isFeasibleZero) {
1883 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1884 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001885 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1886 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001887 }
1888 }
1889
1890 if (!isFeasibleNotZero)
1891 continue;
1892 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001893
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001894 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001895 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001896}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001897
Ted Kremenekf75b1862008-10-30 17:47:32 +00001898namespace {
1899 // This class is used by VisitInitListExpr as an item in a worklist
1900 // for processing the values contained in an InitListExpr.
1901class VISIBILITY_HIDDEN InitListWLItem {
1902public:
1903 llvm::ImmutableList<SVal> Vals;
1904 GRExprEngine::NodeTy* N;
1905 InitListExpr::reverse_iterator Itr;
1906
1907 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1908 InitListExpr::reverse_iterator itr)
1909 : Vals(vals), N(n), Itr(itr) {}
1910};
1911}
1912
1913
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001914void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1915 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001916
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001917 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001918 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001919 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001920
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001921 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001922
Ted Kremeneka49e3672008-10-30 23:14:36 +00001923 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001924
Ted Kremeneka49e3672008-10-30 23:14:36 +00001925 // Handle base case where the initializer has no elements.
1926 // e.g: static int* myArray[] = {};
1927 if (NumInitElements == 0) {
1928 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1929 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1930 return;
1931 }
1932
1933 // Create a worklist to process the initializers.
1934 llvm::SmallVector<InitListWLItem, 10> WorkList;
1935 WorkList.reserve(NumInitElements);
1936 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001937 InitListExpr::reverse_iterator ItrEnd = E->rend();
1938
Ted Kremeneka49e3672008-10-30 23:14:36 +00001939 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001940 while (!WorkList.empty()) {
1941 InitListWLItem X = WorkList.back();
1942 WorkList.pop_back();
1943
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001944 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001945 Visit(*X.Itr, X.N, Tmp);
1946
1947 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001948
Ted Kremenekf75b1862008-10-30 17:47:32 +00001949 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1950 // Get the last initializer value.
1951 state = GetState(*NI);
1952 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1953
1954 // Construct the new list of values by prepending the new value to
1955 // the already constructed list.
1956 llvm::ImmutableList<SVal> NewVals =
1957 getBasicVals().consVals(InitV, X.Vals);
1958
1959 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001960 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001961 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001962
Ted Kremenekf75b1862008-10-30 17:47:32 +00001963 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001964 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001965 }
1966 else {
1967 // Still some initializer values to go. Push them onto the worklist.
1968 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1969 }
1970 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001971 }
Ted Kremenek87903072008-10-30 18:34:31 +00001972
1973 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001974 }
1975
Ted Kremenek062e2f92008-11-13 06:10:40 +00001976 if (T->isUnionType() || T->isVectorType()) {
1977 // FIXME: to be implemented.
1978 // Note: That vectors can return true for T->isIntegerType()
1979 MakeNode(Dst, E, Pred, state);
1980 return;
1981 }
1982
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001983 if (Loc::IsLocType(T) || T->isIntegerType()) {
1984 assert (E->getNumInits() == 1);
1985 NodeSet Tmp;
1986 Expr* Init = E->getInit(0);
1987 Visit(Init, Pred, Tmp);
1988 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1989 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001990 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001991 }
1992 return;
1993 }
1994
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001995
1996 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1997 assert(0 && "unprocessed InitListExpr type");
1998}
Ted Kremenekf233d482008-02-05 00:26:40 +00001999
Sebastian Redl05189992008-11-11 17:56:53 +00002000/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2001void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2002 NodeTy* Pred,
2003 NodeSet& Dst) {
2004 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002005 uint64_t amt;
2006
2007 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002008 if (T == getContext().VoidTy) {
2009 // sizeof(void) == 1 byte.
2010 amt = 1;
2011 }
2012 else if (!T.getTypePtr()->isConstantSizeType()) {
2013 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002014 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002015 }
2016 else if (T->isObjCInterfaceType()) {
2017 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2018 // the compiler has laid out its representation. Just report Unknown
2019 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002020 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002021 }
2022 else {
2023 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002024 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002025 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002026 }
2027 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002028 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002029
Ted Kremenek0e561a32008-03-21 21:30:14 +00002030 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002031 BindExpr(GetState(Pred), Ex,
2032 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002033}
2034
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002035
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002036void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002037 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002038
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002039 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002040
2041 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002042 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002043
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002044 case UnaryOperator::Deref: {
2045
2046 Expr* Ex = U->getSubExpr()->IgnoreParens();
2047 NodeSet Tmp;
2048 Visit(Ex, Pred, Tmp);
2049
2050 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002051
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002052 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002053 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002054
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002055 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002056 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002057 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002058 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002059 }
2060
2061 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002062 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002063
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002064 case UnaryOperator::Real: {
2065
2066 Expr* Ex = U->getSubExpr()->IgnoreParens();
2067 NodeSet Tmp;
2068 Visit(Ex, Pred, Tmp);
2069
2070 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2071
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002072 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002073 if (Ex->getType()->isAnyComplexType()) {
2074 // Just report "Unknown."
2075 Dst.Add(*I);
2076 continue;
2077 }
2078
2079 // For all other types, UnaryOperator::Real is an identity operation.
2080 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002081 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002082 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002083 }
2084
2085 return;
2086 }
2087
2088 case UnaryOperator::Imag: {
2089
2090 Expr* Ex = U->getSubExpr()->IgnoreParens();
2091 NodeSet Tmp;
2092 Visit(Ex, Pred, Tmp);
2093
2094 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002095 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002096 if (Ex->getType()->isAnyComplexType()) {
2097 // Just report "Unknown."
2098 Dst.Add(*I);
2099 continue;
2100 }
2101
2102 // For all other types, UnaryOperator::Float returns 0.
2103 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002104 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002105 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002106 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002107 }
2108
2109 return;
2110 }
2111
2112 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002113 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002114 Dst.Add(Pred);
2115 return;
2116
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002117 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002118 case UnaryOperator::Extension: {
2119
2120 // Unary "+" is a no-op, similar to a parentheses. We still have places
2121 // where it may be a block-level expression, so we need to
2122 // generate an extra node that just propagates the value of the
2123 // subexpression.
2124
2125 Expr* Ex = U->getSubExpr()->IgnoreParens();
2126 NodeSet Tmp;
2127 Visit(Ex, Pred, Tmp);
2128
2129 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002130 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002131 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002132 }
2133
2134 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002135 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002136
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002137 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002138
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002139 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002140 Expr* Ex = U->getSubExpr()->IgnoreParens();
2141 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002142 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002143
2144 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002145 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002146 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002147 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002148 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002149 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002150
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002151 return;
2152 }
2153
2154 case UnaryOperator::LNot:
2155 case UnaryOperator::Minus:
2156 case UnaryOperator::Not: {
2157
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002158 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002159 Expr* Ex = U->getSubExpr()->IgnoreParens();
2160 NodeSet Tmp;
2161 Visit(Ex, Pred, Tmp);
2162
2163 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002164 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002165
2166 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002167 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002168
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002169 if (V.isUnknownOrUndef()) {
2170 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2171 continue;
2172 }
2173
Ted Kremenek60595da2008-11-15 04:01:56 +00002174// QualType DstT = getContext().getCanonicalType(U->getType());
2175// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2176//
2177// if (DstT != SrcT) // Perform promotions.
2178// V = EvalCast(V, DstT);
2179//
2180// if (V.isUnknownOrUndef()) {
2181// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2182// continue;
2183// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002184
2185 switch (U->getOpcode()) {
2186 default:
2187 assert(false && "Invalid Opcode.");
2188 break;
2189
2190 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002191 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002192 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002193 break;
2194
2195 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002196 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002197 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002198 break;
2199
2200 case UnaryOperator::LNot:
2201
2202 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2203 //
2204 // Note: technically we do "E == 0", but this is the same in the
2205 // transfer functions as "0 == E".
2206
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002207 if (isa<Loc>(V)) {
2208 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2209 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002210 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002211 }
2212 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002213 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002214#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002215 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2216 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002217#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002218 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002219 continue;
2220#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002221 }
2222
2223 break;
2224 }
2225
2226 MakeNode(Dst, U, *I, St);
2227 }
2228
2229 return;
2230 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002231 }
2232
2233 // Handle ++ and -- (both pre- and post-increment).
2234
2235 assert (U->isIncrementDecrementOp());
2236 NodeSet Tmp;
2237 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002238 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002239
2240 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2241
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002242 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002243 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002244
2245 // Perform a load.
2246 NodeSet Tmp2;
2247 EvalLoad(Tmp2, Ex, *I, St, V1);
2248
2249 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2250
2251 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002252 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002253
2254 // Propagate unknown and undefined values.
2255 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002256 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002257 continue;
2258 }
2259
Ted Kremenek443003b2008-02-21 19:29:23 +00002260 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002261
2262 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2263 : BinaryOperator::Sub;
2264
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002265 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002266 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002267
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002268 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002269 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002270 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002271 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002272}
2273
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002274void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2275 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2276}
2277
2278void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2279 AsmStmt::outputs_iterator I,
2280 AsmStmt::outputs_iterator E,
2281 NodeTy* Pred, NodeSet& Dst) {
2282 if (I == E) {
2283 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2284 return;
2285 }
2286
2287 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002288 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002289
2290 ++I;
2291
2292 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2293 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2294}
2295
2296void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2297 AsmStmt::inputs_iterator I,
2298 AsmStmt::inputs_iterator E,
2299 NodeTy* Pred, NodeSet& Dst) {
2300 if (I == E) {
2301
2302 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002303 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002304
2305 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2306 // which interprets the inline asm and stores proper results in the
2307 // outputs.
2308
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002309 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002310
2311 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2312 OE = A->end_outputs(); OI != OE; ++OI) {
2313
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002314 SVal X = GetSVal(St, *OI);
2315 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002316
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002317 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002318 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002319 }
2320
Ted Kremenek0e561a32008-03-21 21:30:14 +00002321 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002322 return;
2323 }
2324
2325 NodeSet Tmp;
2326 Visit(*I, Pred, Tmp);
2327
2328 ++I;
2329
2330 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2331 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2332}
2333
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002334void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2335 assert (Builder && "GRStmtNodeBuilder must be defined.");
2336
2337 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002338
Ted Kremenek186350f2008-04-23 20:12:28 +00002339 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2340 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002341
Ted Kremenek729a9a22008-07-17 23:15:45 +00002342 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002343
Ted Kremenekb0533962008-04-18 20:35:30 +00002344 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002345
Ted Kremenekb0533962008-04-18 20:35:30 +00002346 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002347 MakeNode(Dst, S, Pred, GetState(Pred));
2348}
2349
Ted Kremenek02737ed2008-03-31 15:02:58 +00002350void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2351
2352 Expr* R = S->getRetValue();
2353
2354 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002355 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002356 return;
2357 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002358
Ted Kremenek5917d782008-11-21 00:27:44 +00002359 NodeSet Tmp;
2360 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002361
Ted Kremenek5917d782008-11-21 00:27:44 +00002362 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2363 SVal X = GetSVal((*I)->getState(), R);
2364
2365 // Check if we return the address of a stack variable.
2366 if (isa<loc::MemRegionVal>(X)) {
2367 // Determine if the value is on the stack.
2368 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002369
Ted Kremenek5917d782008-11-21 00:27:44 +00002370 if (R && getStateManager().hasStackStorage(R)) {
2371 // Create a special node representing the error.
2372 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2373 N->markAsSink();
2374 RetsStackAddr.insert(N);
2375 }
2376 continue;
2377 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002378 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002379 // Check if we return an undefined value.
2380 else if (X.isUndef()) {
2381 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2382 N->markAsSink();
2383 RetsUndef.insert(N);
2384 }
2385 continue;
2386 }
2387
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002388 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002389 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002390}
Ted Kremenek55deb972008-03-25 00:34:37 +00002391
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002392//===----------------------------------------------------------------------===//
2393// Transfer functions: Binary operators.
2394//===----------------------------------------------------------------------===//
2395
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002396const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2397 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002398
2399 // Divide by undefined? (potentially zero)
2400
2401 if (Denom.isUndef()) {
2402 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2403
2404 if (DivUndef) {
2405 DivUndef->markAsSink();
2406 ExplicitBadDivides.insert(DivUndef);
2407 }
2408
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002409 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002410 }
2411
2412 // Check for divide/remainder-by-zero.
2413 // First, "assume" that the denominator is 0 or undefined.
2414
2415 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002416 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002417
2418 // Second, "assume" that the denominator cannot be 0.
2419
2420 bool isFeasibleNotZero = false;
2421 St = Assume(St, Denom, true, isFeasibleNotZero);
2422
2423 // Create the node for the divide-by-zero (if it occurred).
2424
2425 if (isFeasibleZero)
2426 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2427 DivZeroNode->markAsSink();
2428
2429 if (isFeasibleNotZero)
2430 ImplicitBadDivides.insert(DivZeroNode);
2431 else
2432 ExplicitBadDivides.insert(DivZeroNode);
2433
2434 }
2435
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002436 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002437}
2438
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002439void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002440 GRExprEngine::NodeTy* Pred,
2441 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002442
2443 NodeSet Tmp1;
2444 Expr* LHS = B->getLHS()->IgnoreParens();
2445 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002446
Ted Kremenek759623e2008-12-06 02:39:30 +00002447 // FIXME: Add proper support for ObjCKVCRefExpr.
2448 if (isa<ObjCKVCRefExpr>(LHS)) {
2449 Visit(RHS, Pred, Dst);
2450 return;
2451 }
2452
2453
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002454 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002455 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002456 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002457 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002458
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002459 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002460
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002461 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002462
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002463 // Process the RHS.
2464
2465 NodeSet Tmp2;
2466 Visit(RHS, *I1, Tmp2);
2467
2468 // With both the LHS and RHS evaluated, process the operation itself.
2469
2470 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002471
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002472 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002473 const GRState* OldSt = St;
2474
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002475 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002476 BinaryOperator::Opcode Op = B->getOpcode();
2477
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002478 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002479
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002480 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002481
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002482 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002483 // FIXME: Handle structs.
2484 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002485
Ted Kremenek062e2f92008-11-13 06:10:40 +00002486 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2487 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002488 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002489 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002490
Ted Kremenek062e2f92008-11-13 06:10:40 +00002491 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002492 ? cast<SVal>(loc::SymbolVal(Sym))
2493 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002494 }
2495
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002496 // Simulate the effects of a "store": bind the value of the RHS
2497 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002498
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002499 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002500 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002501 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002502
2503 case BinaryOperator::Div:
2504 case BinaryOperator::Rem:
2505
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002506 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002507 if (RHS->getType()->isIntegerType() &&
2508 RHS->getType()->isScalarType()) {
2509
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002510 St = CheckDivideZero(B, St, *I2, RightV);
2511 if (!St) continue;
2512 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002513
2514 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002515
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002516 default: {
2517
2518 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002519 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002520
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002521 // Process non-assignements except commas or short-circuited
2522 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002523
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002524 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002525
2526 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002527 if (OldSt != St) {
2528 // Generate a new node if we have already created a new state.
2529 MakeNode(Dst, B, *I2, St);
2530 }
2531 else
2532 Dst.Add(*I2);
2533
Ted Kremenek89063af2008-02-21 19:15:37 +00002534 continue;
2535 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002536
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002537 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002538
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002539 // The operands were *not* undefined, but the result is undefined.
2540 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002541
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002542 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002543 UndefNode->markAsSink();
2544 UndefResults.insert(UndefNode);
2545 }
2546
2547 continue;
2548 }
2549
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002550 // Otherwise, create a new node.
2551
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002552 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002553 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002554 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002555 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002556
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002557 assert (B->isCompoundAssignmentOp());
2558
Ted Kremenek934e3e92008-10-27 23:02:39 +00002559 if (Op >= BinaryOperator::AndAssign) {
2560 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2561 BinaryOperator::And));
2562 }
2563 else {
2564 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2565 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002566
2567 // Perform a load (the LHS). This performs the checks for
2568 // null dereferences, and so on.
2569 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002570 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002571 EvalLoad(Tmp3, LHS, *I2, St, location);
2572
2573 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2574
2575 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002576 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002577
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002578 // Check for divide-by-zero.
2579 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002580 && RHS->getType()->isIntegerType()
2581 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002582
2583 // CheckDivideZero returns a new state where the denominator
2584 // is assumed to be non-zero.
2585 St = CheckDivideZero(B, St, *I3, RightV);
2586
2587 if (!St)
2588 continue;
2589 }
2590
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002591 // Propagate undefined values (left-side).
2592 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002593 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002594 continue;
2595 }
2596
2597 // Propagate unknown values (left and right-side).
2598 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002599 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002600 UnknownVal());
2601 continue;
2602 }
2603
2604 // At this point:
2605 //
2606 // The LHS is not Undef/Unknown.
2607 // The RHS is not Unknown.
2608
2609 // Get the computation type.
2610 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002611 CTy = getContext().getCanonicalType(CTy);
2612
2613 QualType LTy = getContext().getCanonicalType(LHS->getType());
2614 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002615
2616 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002617 if (LTy != CTy) V = EvalCast(V, CTy);
2618 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002619
2620 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002621 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002622 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002623 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002624 continue;
2625 }
2626
Ted Kremenek60595da2008-11-15 04:01:56 +00002627 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002628 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002629
2630 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002631 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002632 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2633 UndefNode->markAsSink();
2634 UndefResults.insert(UndefNode);
2635 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002636 continue;
2637 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002638
2639 // EXPERIMENTAL: "Conjured" symbols.
2640 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002641
2642 SVal LHSVal;
2643
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002644 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2645 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002646
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002647 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002648
Ted Kremenek60595da2008-11-15 04:01:56 +00002649 // The symbolic value is actually for the type of the left-hand side
2650 // expression, not the computation type, as this is the value the
2651 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002652 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002653 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002654 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002655 : cast<SVal>(nonloc::SymbolVal(Sym));
2656
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002657 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002658 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002659 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002660 else {
2661 // The left-hand side may bind to a different value then the
2662 // computation type.
2663 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2664 }
2665
2666 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002667 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002668 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002669 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002670}
Ted Kremenekee985462008-01-16 18:18:48 +00002671
2672//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002673// Transfer-function Helpers.
2674//===----------------------------------------------------------------------===//
2675
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002676void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002677 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002678 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002679 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002680
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002681 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002682 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2683
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002684 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002685 MakeNode(Dst, Ex, Pred, *I);
2686}
2687
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002688void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002689 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002690 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002691
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002692 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002693 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002694}
2695
2696//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002697// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002698//===----------------------------------------------------------------------===//
2699
Ted Kremenekaa66a322008-01-16 21:46:15 +00002700#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002701static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002702static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002703
Ted Kremenekaa66a322008-01-16 21:46:15 +00002704namespace llvm {
2705template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002706struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002707 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002708
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002709 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2710
2711 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002712 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002713 GraphPrintCheckerState->isUndefDeref(N) ||
2714 GraphPrintCheckerState->isUndefStore(N) ||
2715 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002716 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2717 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002718 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002719 GraphPrintCheckerState->isBadCall(N) ||
2720 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002721 return "color=\"red\",style=\"filled\"";
2722
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002723 if (GraphPrintCheckerState->isNoReturnCall(N))
2724 return "color=\"blue\",style=\"filled\"";
2725
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002726 return "";
2727 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002728
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002729 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002730 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002731
2732 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002733 ProgramPoint Loc = N->getLocation();
2734
2735 switch (Loc.getKind()) {
2736 case ProgramPoint::BlockEntranceKind:
2737 Out << "Block Entrance: B"
2738 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2739 break;
2740
2741 case ProgramPoint::BlockExitKind:
2742 assert (false);
2743 break;
2744
Ted Kremenekaa66a322008-01-16 21:46:15 +00002745 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002746 if (isa<PostStmt>(Loc)) {
2747 const PostStmt& L = cast<PostStmt>(Loc);
2748 Stmt* S = L.getStmt();
2749 SourceLocation SLoc = S->getLocStart();
2750
2751 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2752 llvm::raw_os_ostream OutS(Out);
2753 S->printPretty(OutS);
2754 OutS.flush();
2755
2756 if (SLoc.isFileID()) {
2757 Out << "\\lline="
2758 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2759 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2760 }
2761
2762 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2763 Out << "\\|Implicit-Null Dereference.\\l";
2764 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2765 Out << "\\|Explicit-Null Dereference.\\l";
2766 else if (GraphPrintCheckerState->isUndefDeref(N))
2767 Out << "\\|Dereference of undefialied value.\\l";
2768 else if (GraphPrintCheckerState->isUndefStore(N))
2769 Out << "\\|Store to Undefined Loc.";
2770 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2771 Out << "\\|Explicit divide-by zero or undefined value.";
2772 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2773 Out << "\\|Implicit divide-by zero or undefined value.";
2774 else if (GraphPrintCheckerState->isUndefResult(N))
2775 Out << "\\|Result of operation is undefined.";
2776 else if (GraphPrintCheckerState->isNoReturnCall(N))
2777 Out << "\\|Call to function marked \"noreturn\".";
2778 else if (GraphPrintCheckerState->isBadCall(N))
2779 Out << "\\|Call to NULL/Undefined.";
2780 else if (GraphPrintCheckerState->isUndefArg(N))
2781 Out << "\\|Argument in call is undefined";
2782
2783 break;
2784 }
2785
Ted Kremenekaa66a322008-01-16 21:46:15 +00002786 const BlockEdge& E = cast<BlockEdge>(Loc);
2787 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2788 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002789
2790 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002791
2792 SourceLocation SLoc = T->getLocStart();
2793
Ted Kremenekb38911f2008-01-30 23:03:39 +00002794 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002795
Ted Kremeneka95d3752008-09-13 05:16:45 +00002796 llvm::raw_os_ostream OutS(Out);
2797 E.getSrc()->printTerminator(OutS);
2798 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002799
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002800 if (SLoc.isFileID()) {
2801 Out << "\\lline="
2802 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2803 << GraphPrintSourceManager->getColumnNumber(SLoc);
2804 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002805
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002806 if (isa<SwitchStmt>(T)) {
2807 Stmt* Label = E.getDst()->getLabel();
2808
2809 if (Label) {
2810 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2811 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002812 llvm::raw_os_ostream OutS(Out);
2813 C->getLHS()->printPretty(OutS);
2814 OutS.flush();
2815
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002816 if (Stmt* RHS = C->getRHS()) {
2817 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002818 RHS->printPretty(OutS);
2819 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002820 }
2821
2822 Out << ":";
2823 }
2824 else {
2825 assert (isa<DefaultStmt>(Label));
2826 Out << "\\ldefault:";
2827 }
2828 }
2829 else
2830 Out << "\\l(implicit) default:";
2831 }
2832 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002833 // FIXME
2834 }
2835 else {
2836 Out << "\\lCondition: ";
2837 if (*E.getSrc()->succ_begin() == E.getDst())
2838 Out << "true";
2839 else
2840 Out << "false";
2841 }
2842
2843 Out << "\\l";
2844 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002845
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002846 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2847 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002848 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002849 }
2850 }
2851
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002852 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002853
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002854 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2855 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002856
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002857 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002858 return Out.str();
2859 }
2860};
2861} // end llvm namespace
2862#endif
2863
Ted Kremenekffe0f432008-03-07 22:58:01 +00002864#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002865
2866template <typename ITERATOR>
2867GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2868
2869template <>
2870GRExprEngine::NodeTy*
2871GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2872 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2873 return I->first;
2874}
2875
Ted Kremenekffe0f432008-03-07 22:58:01 +00002876template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002877static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002878 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002879
Ted Kremenekd4527582008-09-16 18:44:52 +00002880 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002881
2882 for ( ; I != E; ++I ) {
2883 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002884 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002885
Ted Kremenekd4527582008-09-16 18:44:52 +00002886 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002887 continue;
2888
Ted Kremenekd4527582008-09-16 18:44:52 +00002889 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002890 Sources.push_back(N);
2891 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002892}
2893#endif
2894
2895void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002896#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002897 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002898 std::vector<NodeTy*> Src;
2899
2900 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002901 AddSources(Src, null_derefs_begin(), null_derefs_end());
2902 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2903 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2904 AddSources(Src, undef_results_begin(), undef_results_end());
2905 AddSources(Src, bad_calls_begin(), bad_calls_end());
2906 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002907 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002908
Ted Kremenekcb612922008-04-18 19:23:43 +00002909 // The new way.
2910 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2911 (*I)->GetErrorNodes(Src);
2912
2913
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002914 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002915 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002916 else {
2917 GraphPrintCheckerState = this;
2918 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002919
Ted Kremenekffe0f432008-03-07 22:58:01 +00002920 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002921
2922 GraphPrintCheckerState = NULL;
2923 GraphPrintSourceManager = NULL;
2924 }
2925#endif
2926}
2927
2928void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2929#ifndef NDEBUG
2930 GraphPrintCheckerState = this;
2931 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002932
Ted Kremenek493d7a22008-03-11 18:25:33 +00002933 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2934
2935 if (!TrimmedG)
2936 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2937 else {
2938 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2939 delete TrimmedG;
2940 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002941
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002942 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002943 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002944#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002945}