blob: 0b9ae6088b50b7fb8014faf10311b1731f6450ed [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),
Zhongxing Xuff944a82008-12-22 01:52:37 +0000124 PurgeDead(purgeDead),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000125 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000126 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000127 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000128 CurrentStmt(NULL),
129 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000130 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
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:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000350 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000351 break;
352
353 case Stmt::DeclStmtClass:
354 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
355 break;
356
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000357 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000358 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000359 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 VisitCast(C, C->getSubExpr(), Pred, Dst);
361 break;
362 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000363
364 case Stmt::InitListExprClass:
365 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
366 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000368 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000369 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
370 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000371
372 case Stmt::ObjCIvarRefExprClass:
373 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
374 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000375
376 case Stmt::ObjCForCollectionStmtClass:
377 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
378 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000379
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000380 case Stmt::ObjCMessageExprClass: {
381 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
382 break;
383 }
384
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000385 case Stmt::ObjCAtThrowStmtClass: {
386 // FIXME: This is not complete. We basically treat @throw as
387 // an abort.
388 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
389 Builder->BuildSinks = true;
390 MakeNode(Dst, S, Pred, GetState(Pred));
391 break;
392 }
393
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000394 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000395 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396 break;
397
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000398 case Stmt::ReturnStmtClass:
399 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
400 break;
401
Sebastian Redl05189992008-11-11 17:56:53 +0000402 case Stmt::SizeOfAlignOfExprClass:
403 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000404 break;
405
406 case Stmt::StmtExprClass: {
407 StmtExpr* SE = cast<StmtExpr>(S);
408
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000409 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000410
411 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
412 // probably just remove these from the CFG.
413 assert (!SE->getSubStmt()->body_empty());
414
415 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000416 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000417 else
418 Dst.Add(Pred);
419
420 break;
421 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000422
423 case Stmt::StringLiteralClass:
424 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
425 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000426
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000427 case Stmt::UnaryOperatorClass:
428 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000429 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000430 }
431}
432
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000433void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000434
435 Ex = Ex->IgnoreParens();
436
437 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
438 Dst.Add(Pred);
439 return;
440 }
441
442 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000443
444 case Stmt::ArraySubscriptExprClass:
445 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
446 return;
447
448 case Stmt::DeclRefExprClass:
449 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
450 return;
451
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000452 case Stmt::ObjCIvarRefExprClass:
453 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
454 return;
455
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000456 case Stmt::UnaryOperatorClass:
457 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
458 return;
459
460 case Stmt::MemberExprClass:
461 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
462 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000463
Ted Kremenek4f090272008-10-27 21:54:31 +0000464 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000465 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000466 return;
467
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000468 case Stmt::ObjCPropertyRefExprClass:
469 // FIXME: Property assignments are lvalues, but not really "locations".
470 // e.g.: self.x = something;
471 // Here the "self.x" really can translate to a method call (setter) when
472 // the assignment is made. Moreover, the entire assignment expression
473 // evaluate to whatever "something" is, not calling the "getter" for
474 // the property (which would make sense since it can have side effects).
475 // We'll probably treat this as a location, but not one that we can
476 // take the address of. Perhaps we need a new SVal class for cases
477 // like thsis?
478 // Note that we have a similar problem for bitfields, since they don't
479 // have "locations" in the sense that we can take their address.
480 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000481 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000482
483 case Stmt::StringLiteralClass: {
484 const GRState* St = GetState(Pred);
485 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000486 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000487 return;
488 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000489
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000490 default:
491 // Arbitrary subexpressions can return aggregate temporaries that
492 // can be used in a lvalue context. We need to enhance our support
493 // of such temporaries in both the environment and the store, so right
494 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000495 assert ((Ex->getType()->isAggregateType() ||
496 Ex->getType()->isUnionType()) &&
497 "Other kinds of expressions with non-aggregate/union types do"
498 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000499
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000500 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000501 }
502}
503
504//===----------------------------------------------------------------------===//
505// Block entrance. (Update counters).
506//===----------------------------------------------------------------------===//
507
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000508bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000509 GRBlockCounter BC) {
510
511 return BC.getNumVisited(B->getBlockID()) < 3;
512}
513
514//===----------------------------------------------------------------------===//
515// Branch processing.
516//===----------------------------------------------------------------------===//
517
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000518const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000519 Stmt* Terminator,
520 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000521
522 switch (Terminator->getStmtClass()) {
523 default:
524 return St;
525
526 case Stmt::BinaryOperatorClass: { // '&&' and '||'
527
528 BinaryOperator* B = cast<BinaryOperator>(Terminator);
529 BinaryOperator::Opcode Op = B->getOpcode();
530
531 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
532
533 // For &&, if we take the true branch, then the value of the whole
534 // expression is that of the RHS expression.
535 //
536 // For ||, if we take the false branch, then the value of the whole
537 // expression is that of the RHS expression.
538
539 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
540 (Op == BinaryOperator::LOr && !branchTaken)
541 ? B->getRHS() : B->getLHS();
542
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000543 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000544 }
545
546 case Stmt::ConditionalOperatorClass: { // ?:
547
548 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
549
550 // For ?, if branchTaken == true then the value is either the LHS or
551 // the condition itself. (GNU extension).
552
553 Expr* Ex;
554
555 if (branchTaken)
556 Ex = C->getLHS() ? C->getLHS() : C->getCond();
557 else
558 Ex = C->getRHS();
559
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000560 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000561 }
562
563 case Stmt::ChooseExprClass: { // ?:
564
565 ChooseExpr* C = cast<ChooseExpr>(Terminator);
566
567 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000568 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000569 }
570 }
571}
572
Ted Kremenekaf337412008-11-12 19:24:17 +0000573void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000574 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000575
Ted Kremeneke7d22112008-02-11 19:21:59 +0000576 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000577 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000578 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000579
Ted Kremenekb2331832008-02-15 22:29:00 +0000580 // Check for NULL conditions; e.g. "for(;;)"
581 if (!Condition) {
582 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000583 return;
584 }
585
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000586 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000587
588 switch (V.getBaseKind()) {
589 default:
590 break;
591
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000592 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000593 builder.generateNode(MarkBranch(PrevState, Term, true), true);
594 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000595 return;
596
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000597 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000598 NodeTy* N = builder.generateNode(PrevState, true);
599
600 if (N) {
601 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000602 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000603 }
604
605 builder.markInfeasible(false);
606 return;
607 }
608 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000609
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000610 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000611
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000612 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000613 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000614
615 if (isFeasible)
616 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000617 else
618 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000619
620 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000621
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000622 isFeasible = false;
623 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000624
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000625 if (isFeasible)
626 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000627 else
628 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000629}
630
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000631/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000632/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000633void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000634
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000635 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000636 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000637
638 // Three possibilities:
639 //
640 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000641 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000642 // (3) We have no clue about the label. Dispatch to all targets.
643 //
644
645 typedef IndirectGotoNodeBuilder::iterator iterator;
646
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000647 if (isa<loc::GotoLabel>(V)) {
648 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000649
650 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000651 if (I.getLabel() == L) {
652 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000653 return;
654 }
655 }
656
657 assert (false && "No block with label.");
658 return;
659 }
660
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000661 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000662 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000663 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000664 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000665 return;
666 }
667
668 // This is really a catch-all. We don't support symbolics yet.
669
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000670 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000671
672 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000673 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000674}
Ted Kremenekf233d482008-02-05 00:26:40 +0000675
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000676
677void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
678 NodeTy* Pred, NodeSet& Dst) {
679
680 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
681
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000682 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000683 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000684
685 assert (X.isUndef());
686
687 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
688
689 assert (SE);
690
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000691 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000692
693 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000694 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000695}
696
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000697/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
698/// nodes by processing the 'effects' of a switch statement.
699void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
700
701 typedef SwitchNodeBuilder::iterator iterator;
702
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 }
712
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000713 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000714
715 // While most of this can be assumed (such as the signedness), having it
716 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000717
Chris Lattner98be4942008-03-05 18:54:05 +0000718 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000719
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000720 APSInt V1(bits, false);
721 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000722 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000723
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000724 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000725
726 CaseStmt* Case = cast<CaseStmt>(I.getCase());
727
728 // Evaluate the case.
729 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
730 assert (false && "Case condition must evaluate to an integer constant.");
731 return;
732 }
733
734 // Get the RHS of the case, if it exists.
735
736 if (Expr* E = Case->getRHS()) {
737 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
738 assert (false &&
739 "Case condition (RHS) must evaluate to an integer constant.");
740 return ;
741 }
742
743 assert (V1 <= V2);
744 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000745 else
746 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000747
748 // FIXME: Eventually we should replace the logic below with a range
749 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000750 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751
Ted Kremenek14a11402008-03-17 22:17:56 +0000752 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000753 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000754
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000755 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000756
757 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000758
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000759 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000760 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000761
762 if (isFeasible) {
763 builder.generateCaseStmtNode(I, StNew);
764
765 // If CondV evaluates to a constant, then we know that this
766 // is the *only* case that we can take, so stop evaluating the
767 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000768 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000769 return;
770 }
771
772 // Now "assume" that the case doesn't match. Add this state
773 // to the default state (if it is feasible).
774
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000775 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000776 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777
Ted Kremenek5014ab12008-04-23 05:03:18 +0000778 if (isFeasible) {
779 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000780 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000781 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000782
Ted Kremenek14a11402008-03-17 22:17:56 +0000783 // Concretize the next value in the range.
784 if (V1 == V2)
785 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000786
Ted Kremenek14a11402008-03-17 22:17:56 +0000787 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000788 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000789
790 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000791 }
792
793 // If we reach here, than we know that the default branch is
794 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000795 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000796}
797
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000798//===----------------------------------------------------------------------===//
799// Transfer functions: logical operations ('&&', '||').
800//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000801
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000802void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000803 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000804
Ted Kremenek05a23782008-02-26 19:05:15 +0000805 assert (B->getOpcode() == BinaryOperator::LAnd ||
806 B->getOpcode() == BinaryOperator::LOr);
807
808 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
809
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000810 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000811 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000812
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000813 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000814
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000815 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000816
817 assert (Ex);
818
819 if (Ex == B->getRHS()) {
820
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000821 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000822
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000823 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000824
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000825 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000826 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000827 return;
828 }
829
Ted Kremenek05a23782008-02-26 19:05:15 +0000830 // We took the RHS. Because the value of the '&&' or '||' expression must
831 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
832 // or 1. Alternatively, we could take a lazy approach, and calculate this
833 // value later when necessary. We don't have the machinery in place for
834 // this right now, and since most logical expressions are used for branches,
835 // the payoff is not likely to be large. Instead, we do eager evaluation.
836
837 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000838 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000839
840 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000841 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000842 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000843
844 isFeasible = false;
845 NewState = Assume(St, X, false, isFeasible);
846
847 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000848 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000849 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000850 }
851 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000852 // We took the LHS expression. Depending on whether we are '&&' or
853 // '||' we know what the value of the expression is via properties of
854 // the short-circuiting.
855
856 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000857 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000858 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000859}
Ted Kremenek05a23782008-02-26 19:05:15 +0000860
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000861//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000862// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000863//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000864
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000865void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
866 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000867
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000868 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000869
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000870 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000871
872 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
873
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000874 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000875
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000876 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000877 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000878 else
879 EvalLoad(Dst, Ex, Pred, St, V);
880 return;
881
882 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
883 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
884
885 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000886 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000887 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000888 return;
889
890 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000891 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000892 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000893 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000894 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000895 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000896
897 assert (false &&
898 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000899}
900
Ted Kremenek540cbe22008-04-22 04:56:29 +0000901/// VisitArraySubscriptExpr - Transfer function for array accesses
902void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000903 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000904
905 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000906 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000907 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000908 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000909
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000910 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000911 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000912 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000913
914 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000915 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000916 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000917
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000918 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000919 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000920 else
921 EvalLoad(Dst, A, *I2, St, V);
922 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000923 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000924}
925
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000926/// VisitMemberExpr - Transfer function for member expressions.
927void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000928 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000929
930 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000931 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000932
933 if (M->isArrow())
934 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
935 else
936 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
937
Douglas Gregor86f19402008-12-20 23:49:58 +0000938 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
939 if (!Field) // FIXME: skipping member expressions for non-fields
940 return;
941
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000942 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000943 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000944 // FIXME: Should we insert some assumption logic in here to determine
945 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000946 // later when using FieldOffset lvals (which we no longer have).
947 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000948
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000949 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000950 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000951 else
952 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000953 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000954}
955
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000956void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000957 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000958
959 assert (Builder && "GRStmtNodeBuilder must be defined.");
960
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000961 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8c354752008-12-16 22:02:27 +0000962 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000963
Ted Kremenek8c354752008-12-16 22:02:27 +0000964 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000965 return;
966
Ted Kremenek8c354752008-12-16 22:02:27 +0000967 St = GetState(Pred);
968
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969 // Proceed with the store.
970
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000971 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000972
Ted Kremenek186350f2008-04-23 20:12:28 +0000973 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000974 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000975 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000976
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000977 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000978 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000979
Ted Kremenek729a9a22008-07-17 23:15:45 +0000980 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000981
982 // Handle the case where no nodes where generated. Auto-generate that
983 // contains the updated state if we aren't generating sinks.
984
Ted Kremenekb0533962008-04-18 20:35:30 +0000985 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000986 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000987 location, Val);
988}
989
990void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000991 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000992 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000993
Ted Kremenek8c354752008-12-16 22:02:27 +0000994 // Evaluate the location (checks for bad dereferences).
995 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000996
Ted Kremenek8c354752008-12-16 22:02:27 +0000997 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000998 return;
999
Ted Kremenek8c354752008-12-16 22:02:27 +00001000 St = GetState(Pred);
1001
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001002 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001003 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001004
1005 // FIXME: Currently symbolic analysis "generates" new symbols
1006 // for the contents of values. We need a better approach.
1007
1008 // FIXME: The "CheckOnly" option exists only because Array and Field
1009 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001010 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +00001011
Ted Kremenek8c354752008-12-16 22:02:27 +00001012 if (CheckOnly) {
1013 Dst.Add(Pred);
1014 return;
1015 }
1016
1017 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001018 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001019 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001020 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001021 else {
1022 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
1023 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1024 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001025}
1026
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001027void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001028 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001029
1030 NodeSet TmpDst;
1031 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1032
1033 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1034 MakeNode(Dst, Ex, *I, (*I)->getState());
1035}
1036
Ted Kremenek8c354752008-12-16 22:02:27 +00001037GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
1038 const GRState* St,
1039 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001040
1041 // Check for loads/stores from/to undefined values.
1042 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001043 NodeTy* N =
1044 Builder->generateNode(Ex, St, Pred,
1045 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001046
Ted Kremenek8c354752008-12-16 22:02:27 +00001047 if (N) {
1048 N->markAsSink();
1049 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001050 }
1051
Ted Kremenek8c354752008-12-16 22:02:27 +00001052 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053 }
1054
1055 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1056 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001057 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001058
1059 // During a load, one of two possible situations arise:
1060 // (1) A crash, because the location (pointer) was NULL.
1061 // (2) The location (pointer) is not NULL, and the dereference works.
1062 //
1063 // We add these assumptions.
1064
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001065 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001066
1067 // "Assume" that the pointer is not NULL.
1068
1069 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001070 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001071
1072 // "Assume" that the pointer is NULL.
1073
1074 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001075 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1076 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001077
1078 if (isFeasibleNull) {
1079
Ted Kremenek7360fda2008-09-18 23:09:54 +00001080 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001081 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001082 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1083
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001084 // We don't use "MakeNode" here because the node will be a sink
1085 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001086 NodeTy* NullNode =
1087 Builder->generateNode(Ex, StNull, Pred,
1088 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001089
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001090 if (NullNode) {
1091
1092 NullNode->markAsSink();
1093
1094 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1095 else ExplicitNullDeref.insert(NullNode);
1096 }
1097 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001098
1099 if (!isFeasibleNotNull)
1100 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001101
1102 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001103 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001104 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1105 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1106 // Get the index of the accessed element.
1107 SVal Idx = ER->getIndex();
1108 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001109 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1110 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001111
1112 bool isFeasibleInBound = false;
1113 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1114 true, isFeasibleInBound);
1115
1116 bool isFeasibleOutBound = false;
1117 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1118 false, isFeasibleOutBound);
1119
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001120 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001121 // Report warning. Make sink node manually.
1122 NodeTy* OOBNode =
1123 Builder->generateNode(Ex, StOutBound, Pred,
1124 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001125
1126 if (OOBNode) {
1127 OOBNode->markAsSink();
1128
1129 if (isFeasibleInBound)
1130 ImplicitOOBMemAccesses.insert(OOBNode);
1131 else
1132 ExplicitOOBMemAccesses.insert(OOBNode);
1133 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001134 }
1135
Ted Kremenek8c354752008-12-16 22:02:27 +00001136 if (!isFeasibleInBound)
1137 return 0;
1138
1139 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001140 }
1141 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001142
Ted Kremenek8c354752008-12-16 22:02:27 +00001143 // Generate a new node indicating the checks succeed.
1144 return Builder->generateNode(Ex, StNotNull, Pred,
1145 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001146}
1147
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001148//===----------------------------------------------------------------------===//
1149// Transfer function: Function calls.
1150//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001151void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001152 CallExpr::arg_iterator AI,
1153 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001154 NodeSet& Dst)
1155{
1156 // Determine the type of function we're calling (if available).
1157 const FunctionTypeProto *Proto = NULL;
1158 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1159 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1160 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1161
1162 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1163}
1164
1165void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1166 CallExpr::arg_iterator AI,
1167 CallExpr::arg_iterator AE,
1168 NodeSet& Dst, const FunctionTypeProto *Proto,
1169 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001170
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001171 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001172 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001173 // If the call argument is being bound to a reference parameter,
1174 // visit it as an lvalue, not an rvalue.
1175 bool VisitAsLvalue = false;
1176 if (Proto && ParamIdx < Proto->getNumArgs())
1177 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1178
1179 NodeSet DstTmp;
1180 if (VisitAsLvalue)
1181 VisitLValue(*AI, Pred, DstTmp);
1182 else
1183 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001184 ++AI;
1185
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001186 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001187 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001188
1189 return;
1190 }
1191
1192 // If we reach here we have processed all of the arguments. Evaluate
1193 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001194
Ted Kremenek994a09b2008-02-25 21:16:03 +00001195 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001196 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001197
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001198 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001199
Ted Kremenekde434242008-02-19 01:44:53 +00001200 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001201 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1202
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001203 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001204 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001205
Ted Kremeneka1354a52008-03-03 16:47:31 +00001206 // FIXME: Add support for symbolic function calls (calls involving
1207 // function pointer values that are symbolic).
1208
1209 // Check for undefined control-flow or calls to NULL.
1210
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001211 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001212 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001213
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001214 if (N) {
1215 N->markAsSink();
1216 BadCalls.insert(N);
1217 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001218
Ted Kremenekde434242008-02-19 01:44:53 +00001219 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001220 }
1221
1222 // Check for the "noreturn" attribute.
1223
1224 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1225
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001226 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001227
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001228 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001229
1230 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001231 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001232 else {
1233 // HACK: Some functions are not marked noreturn, and don't return.
1234 // Here are a few hardwired ones. If this takes too long, we can
1235 // potentially cache these results.
1236 const char* s = FD->getIdentifier()->getName();
1237 unsigned n = strlen(s);
1238
1239 switch (n) {
1240 default:
1241 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001242
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001243 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001244 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1245 break;
1246
1247 case 5:
1248 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001249 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001250 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001251 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001252 // FIXME: use Assume to inspect the possible symbolic value of
1253 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001254 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001255 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001256 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001257 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001258 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001259 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001260
1261 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001262 if (!memcmp(s, "Assert", 6)) {
1263 Builder->BuildSinks = true;
1264 break;
1265 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001266
1267 // FIXME: This is just a wrapper around throwing an exception.
1268 // Eventually inter-procedural analysis should handle this easily.
1269 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1270
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001271 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001272
1273 case 7:
1274 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1275 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001276
Ted Kremenekf47bb782008-04-30 17:54:04 +00001277 case 8:
1278 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1279 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001280
1281 case 12:
1282 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1283 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001284
Ted Kremenekf9683082008-09-19 02:30:47 +00001285 case 13:
1286 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1287 break;
1288
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001289 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001290 if (!memcmp(s, "dtrace_assfail", 14) ||
1291 !memcmp(s, "yy_fatal_error", 14))
1292 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001293 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001294
1295 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001296 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1297 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001298 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001299
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001300 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001301 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001302
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001303 }
1304 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001305
1306 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001307
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001308 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001309
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001310 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001311
Ted Kremenek186350f2008-04-23 20:12:28 +00001312 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001313 switch (id) {
1314 case Builtin::BI__builtin_expect: {
1315 // For __builtin_expect, just return the value of the subexpression.
1316 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001317 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001318 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001319 continue;
1320 }
1321
Ted Kremenekb3021332008-11-02 00:35:01 +00001322 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001323 // FIXME: Refactor into StoreManager itself?
1324 MemRegionManager& RM = getStateManager().getRegionManager();
1325 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001326 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001327
1328 // Set the extent of the region in bytes. This enables us to use the
1329 // SVal of the argument directly. If we save the extent in bits, we
1330 // cannot represent values like symbol*8.
1331 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1332 St = getStoreManager().setExtent(St, R, Extent);
1333
Ted Kremenekb3021332008-11-02 00:35:01 +00001334 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1335 continue;
1336 }
1337
Ted Kremenek55aea312008-03-05 22:59:42 +00001338 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001339 break;
1340 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001341 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001342
Ted Kremenek186350f2008-04-23 20:12:28 +00001343 // Check any arguments passed-by-value against being undefined.
1344
1345 bool badArg = false;
1346
1347 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1348 I != E; ++I) {
1349
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001350 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001351 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001352
Ted Kremenek186350f2008-04-23 20:12:28 +00001353 if (N) {
1354 N->markAsSink();
1355 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001356 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001357
Ted Kremenek186350f2008-04-23 20:12:28 +00001358 badArg = true;
1359 break;
1360 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001361 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001362
1363 if (badArg)
1364 continue;
1365
1366 // Dispatch to the plug-in transfer function.
1367
1368 unsigned size = Dst.size();
1369 SaveOr OldHasGen(Builder->HasGeneratedNode);
1370 EvalCall(Dst, CE, L, *DI);
1371
1372 // Handle the case where no nodes where generated. Auto-generate that
1373 // contains the updated state if we aren't generating sinks.
1374
1375 if (!Builder->BuildSinks && Dst.size() == size &&
1376 !Builder->HasGeneratedNode)
1377 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001378 }
1379}
1380
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001381//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001382// Transfer function: Objective-C ivar references.
1383//===----------------------------------------------------------------------===//
1384
1385void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1386 NodeTy* Pred, NodeSet& Dst,
1387 bool asLValue) {
1388
1389 Expr* Base = cast<Expr>(Ex->getBase());
1390 NodeSet Tmp;
1391 Visit(Base, Pred, Tmp);
1392
1393 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1394 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001395 SVal BaseVal = GetSVal(St, Base);
1396 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001397
1398 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001399 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001400 else
1401 EvalLoad(Dst, Ex, *I, St, location);
1402 }
1403}
1404
1405//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001406// Transfer function: Objective-C fast enumeration 'for' statements.
1407//===----------------------------------------------------------------------===//
1408
1409void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1410 NodeTy* Pred, NodeSet& Dst) {
1411
1412 // ObjCForCollectionStmts are processed in two places. This method
1413 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1414 // statements within a basic block. This transfer function does two things:
1415 //
1416 // (1) binds the next container value to 'element'. This creates a new
1417 // node in the ExplodedGraph.
1418 //
1419 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1420 // whether or not the container has any more elements. This value
1421 // will be tested in ProcessBranch. We need to explicitly bind
1422 // this value because a container can contain nil elements.
1423 //
1424 // FIXME: Eventually this logic should actually do dispatches to
1425 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1426 // This will require simulating a temporary NSFastEnumerationState, either
1427 // through an SVal or through the use of MemRegions. This value can
1428 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1429 // terminates we reclaim the temporary (it goes out of scope) and we
1430 // we can test if the SVal is 0 or if the MemRegion is null (depending
1431 // on what approach we take).
1432 //
1433 // For now: simulate (1) by assigning either a symbol or nil if the
1434 // container is empty. Thus this transfer function will by default
1435 // result in state splitting.
1436
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001437 Stmt* elem = S->getElement();
1438 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001439
1440 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001441 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001442 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001443 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1444 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1445 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001446 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001447
1448 NodeSet Tmp;
1449 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001450
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001451 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1452 const GRState* state = GetState(*I);
1453 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1454 }
1455}
1456
1457void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1458 NodeTy* Pred, NodeSet& Dst,
1459 SVal ElementV) {
1460
1461
Ted Kremenekaf337412008-11-12 19:24:17 +00001462
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001463 // Get the current state. Use 'EvalLocation' to determine if it is a null
1464 // pointer, etc.
1465 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001466
Ted Kremenek8c354752008-12-16 22:02:27 +00001467 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1468 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001469 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001470
1471 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001472
Ted Kremenekaf337412008-11-12 19:24:17 +00001473 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001474 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001475 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1476 GRStateRef hasElems = state.BindExpr(S, TrueV);
1477
Ted Kremenekaf337412008-11-12 19:24:17 +00001478 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001479 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1480 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001481
1482 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1483 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1484 // FIXME: The proper thing to do is to really iterate over the
1485 // container. We will do this with dispatch logic to the store.
1486 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001487 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001488 assert (Loc::IsLocType(T));
1489 unsigned Count = Builder->getCurrentBlockCount();
1490 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1491 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001492
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001493 // Bind the location to 'nil' on the false branch.
1494 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1495 noElems = noElems.BindLoc(ElementV, nilV);
1496 }
1497
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001498 // Create the new nodes.
1499 MakeNode(Dst, S, Pred, hasElems);
1500 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001501}
1502
1503//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001504// Transfer function: Objective-C message expressions.
1505//===----------------------------------------------------------------------===//
1506
1507void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1508 NodeSet& Dst){
1509
1510 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1511 Pred, Dst);
1512}
1513
1514void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001515 ObjCMessageExpr::arg_iterator AI,
1516 ObjCMessageExpr::arg_iterator AE,
1517 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001518 if (AI == AE) {
1519
1520 // Process the receiver.
1521
1522 if (Expr* Receiver = ME->getReceiver()) {
1523 NodeSet Tmp;
1524 Visit(Receiver, Pred, Tmp);
1525
1526 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1527 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1528
1529 return;
1530 }
1531
1532 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1533 return;
1534 }
1535
1536 NodeSet Tmp;
1537 Visit(*AI, Pred, Tmp);
1538
1539 ++AI;
1540
1541 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1542 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1543}
1544
1545void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1546 NodeTy* Pred,
1547 NodeSet& Dst) {
1548
1549 // FIXME: More logic for the processing the method call.
1550
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001551 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001552 bool RaisesException = false;
1553
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001554
1555 if (Expr* Receiver = ME->getReceiver()) {
1556
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001557 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001558
1559 // Check for undefined control-flow or calls to NULL.
1560
1561 if (L.isUndef()) {
1562 NodeTy* N = Builder->generateNode(ME, St, Pred);
1563
1564 if (N) {
1565 N->markAsSink();
1566 UndefReceivers.insert(N);
1567 }
1568
1569 return;
1570 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001571
1572 // Check if the "raise" message was sent.
1573 if (ME->getSelector() == RaiseSel)
1574 RaisesException = true;
1575 }
1576 else {
1577
1578 IdentifierInfo* ClsName = ME->getClassName();
1579 Selector S = ME->getSelector();
1580
1581 // Check for special instance methods.
1582
1583 if (!NSExceptionII) {
1584 ASTContext& Ctx = getContext();
1585
1586 NSExceptionII = &Ctx.Idents.get("NSException");
1587 }
1588
1589 if (ClsName == NSExceptionII) {
1590
1591 enum { NUM_RAISE_SELECTORS = 2 };
1592
1593 // Lazily create a cache of the selectors.
1594
1595 if (!NSExceptionInstanceRaiseSelectors) {
1596
1597 ASTContext& Ctx = getContext();
1598
1599 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1600
1601 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1602 unsigned idx = 0;
1603
1604 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001605 II.push_back(&Ctx.Idents.get("raise"));
1606 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001607 NSExceptionInstanceRaiseSelectors[idx++] =
1608 Ctx.Selectors.getSelector(II.size(), &II[0]);
1609
1610 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001611 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001612 NSExceptionInstanceRaiseSelectors[idx++] =
1613 Ctx.Selectors.getSelector(II.size(), &II[0]);
1614 }
1615
1616 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1617 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1618 RaisesException = true; break;
1619 }
1620 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001621 }
1622
1623 // Check for any arguments that are uninitialized/undefined.
1624
1625 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1626 I != E; ++I) {
1627
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001628 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001629
1630 // Generate an error node for passing an uninitialized/undefined value
1631 // as an argument to a message expression. This node is a sink.
1632 NodeTy* N = Builder->generateNode(ME, St, Pred);
1633
1634 if (N) {
1635 N->markAsSink();
1636 MsgExprUndefArgs[N] = *I;
1637 }
1638
1639 return;
1640 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001641 }
1642
1643 // Check if we raise an exception. For now treat these as sinks. Eventually
1644 // we will want to handle exceptions properly.
1645
1646 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1647
1648 if (RaisesException)
1649 Builder->BuildSinks = true;
1650
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001651 // Dispatch to plug-in transfer function.
1652
1653 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001654 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001655
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001656 EvalObjCMessageExpr(Dst, ME, Pred);
1657
1658 // Handle the case where no nodes where generated. Auto-generate that
1659 // contains the updated state if we aren't generating sinks.
1660
Ted Kremenekb0533962008-04-18 20:35:30 +00001661 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001662 MakeNode(Dst, ME, Pred, St);
1663}
1664
1665//===----------------------------------------------------------------------===//
1666// Transfer functions: Miscellaneous statements.
1667//===----------------------------------------------------------------------===//
1668
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001669void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001670 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001671 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001672 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001673
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001674 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001675 T = ExCast->getTypeAsWritten();
1676
Zhongxing Xued340f72008-10-22 08:02:16 +00001677 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001678 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001679 else
1680 Visit(Ex, Pred, S1);
1681
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001682 // Check for casting to "void".
1683 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001684
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001685 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001686 Dst.Add(*I1);
1687
Ted Kremenek874d63f2008-01-24 02:02:54 +00001688 return;
1689 }
1690
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001691 // FIXME: The rest of this should probably just go into EvalCall, and
1692 // let the transfer function object be responsible for constructing
1693 // nodes.
1694
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001695 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001696 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001697 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001698 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001699
1700 // Unknown?
1701
1702 if (V.isUnknown()) {
1703 Dst.Add(N);
1704 continue;
1705 }
1706
1707 // Undefined?
1708
1709 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001710 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001711 continue;
1712 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001713
1714 // For const casts, just propagate the value.
1715 ASTContext& C = getContext();
1716
1717 if (C.getCanonicalType(T).getUnqualifiedType() ==
1718 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001719 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001720 continue;
1721 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001722
1723 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001724 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001725 unsigned bits = getContext().getTypeSize(ExTy);
1726
1727 // FIXME: Determine if the number of bits of the target type is
1728 // equal or exceeds the number of bits to store the pointer value.
1729 // If not, flag an error.
1730
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001731 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001732 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001733 continue;
1734 }
1735
1736 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001737 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1738 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001739 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001740 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001741 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001742 continue;
1743 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001744
Zhongxing Xu37d682a2008-11-14 09:23:38 +00001745 // Check for casts from array type to pointer type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001746 if (ExTy->isArrayType()) {
Ted Kremenek0fb7c612008-11-15 05:00:27 +00001747 assert(T->isPointerType());
Zhongxing Xue1911af2008-10-23 03:10:39 +00001748 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001749 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001750 continue;
1751 }
1752
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001753 // Check for casts from a region to a specific type.
1754 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001755 assert(Loc::IsLocType(T));
1756 assert(Loc::IsLocType(ExTy));
1757
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001758 const MemRegion* R = RV->getRegion();
1759 StoreManager& StoreMgr = getStoreManager();
1760
1761 // Delegate to store manager to get the result of casting a region
1762 // to a different type.
1763 const StoreManager::CastResult& Res = StoreMgr.CastRegion(St, R, T);
1764
1765 // Inspect the result. If the MemRegion* returned is NULL, this
1766 // expression evaluates to UnknownVal.
1767 R = Res.getRegion();
1768 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1769
1770 // Generate the new node in the ExplodedGraph.
1771 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001772 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001773 }
1774
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001775 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001776 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001777 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001778}
1779
Ted Kremenek4f090272008-10-27 21:54:31 +00001780void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001781 NodeTy* Pred, NodeSet& Dst,
1782 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001783 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1784 NodeSet Tmp;
1785 Visit(ILE, Pred, Tmp);
1786
1787 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001788 const GRState* St = GetState(*I);
1789 SVal ILV = GetSVal(St, ILE);
1790 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001791
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001792 if (asLValue)
1793 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1794 else
1795 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001796 }
1797}
1798
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001799void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001800
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001801 // The CFG has one DeclStmt per Decl.
1802 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001803
1804 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001805 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001806
Ted Kremenekefd59942008-12-08 22:47:34 +00001807 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001808 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001809
1810 // FIXME: static variables may have an initializer, but the second
1811 // time a function is called those values may not be current.
1812 NodeSet Tmp;
1813
Ted Kremenekaf337412008-11-12 19:24:17 +00001814 if (InitEx)
1815 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001816
1817 if (Tmp.empty())
1818 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001819
1820 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001821 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001822 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001823
1824 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001825 if (InitEx) {
1826 SVal InitVal = GetSVal(St, InitEx);
1827 QualType T = VD->getType();
1828
1829 // Recover some path-sensitivity if a scalar value evaluated to
1830 // UnknownVal.
1831 if (InitVal.isUnknown()) {
1832 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001833 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001834 InitVal = loc::SymbolVal(Sym);
1835 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001836 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001837 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001838 InitVal = nonloc::SymbolVal(Sym);
1839 }
1840 }
1841
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001842 St = StateMgr.BindDecl(St, VD, InitVal);
1843 } else
1844 St = StateMgr.BindDeclWithNoInit(St, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001845
1846 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1847 QualType T = getContext().getCanonicalType(VD->getType());
1848 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1849 // FIXME: Handle multi-dimensional VLAs.
1850
1851 Expr* SE = VLA->getSizeExpr();
1852 SVal Size = GetSVal(St, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001853
1854 if (Size.isUndef()) {
1855 if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
1856 N->markAsSink();
1857 ExplicitBadSizedVLA.insert(N);
1858 }
1859 continue;
1860 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001861
1862 bool isFeasibleZero = false;
1863 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1864
1865 bool isFeasibleNotZero = false;
1866 St = Assume(St, Size, true, isFeasibleNotZero);
1867
1868 if (isFeasibleZero) {
1869 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1870 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001871 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1872 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001873 }
1874 }
1875
1876 if (!isFeasibleNotZero)
1877 continue;
1878 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001879
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001880 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001881 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001882}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001883
Ted Kremenekf75b1862008-10-30 17:47:32 +00001884namespace {
1885 // This class is used by VisitInitListExpr as an item in a worklist
1886 // for processing the values contained in an InitListExpr.
1887class VISIBILITY_HIDDEN InitListWLItem {
1888public:
1889 llvm::ImmutableList<SVal> Vals;
1890 GRExprEngine::NodeTy* N;
1891 InitListExpr::reverse_iterator Itr;
1892
1893 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1894 InitListExpr::reverse_iterator itr)
1895 : Vals(vals), N(n), Itr(itr) {}
1896};
1897}
1898
1899
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001900void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1901 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001902
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001903 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001904 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001905 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001906
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001907 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001908
Ted Kremeneka49e3672008-10-30 23:14:36 +00001909 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001910
Ted Kremeneka49e3672008-10-30 23:14:36 +00001911 // Handle base case where the initializer has no elements.
1912 // e.g: static int* myArray[] = {};
1913 if (NumInitElements == 0) {
1914 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1915 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1916 return;
1917 }
1918
1919 // Create a worklist to process the initializers.
1920 llvm::SmallVector<InitListWLItem, 10> WorkList;
1921 WorkList.reserve(NumInitElements);
1922 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001923 InitListExpr::reverse_iterator ItrEnd = E->rend();
1924
Ted Kremeneka49e3672008-10-30 23:14:36 +00001925 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001926 while (!WorkList.empty()) {
1927 InitListWLItem X = WorkList.back();
1928 WorkList.pop_back();
1929
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001930 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001931 Visit(*X.Itr, X.N, Tmp);
1932
1933 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001934
Ted Kremenekf75b1862008-10-30 17:47:32 +00001935 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1936 // Get the last initializer value.
1937 state = GetState(*NI);
1938 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1939
1940 // Construct the new list of values by prepending the new value to
1941 // the already constructed list.
1942 llvm::ImmutableList<SVal> NewVals =
1943 getBasicVals().consVals(InitV, X.Vals);
1944
1945 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001946 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001947 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001948
Ted Kremenekf75b1862008-10-30 17:47:32 +00001949 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001950 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001951 }
1952 else {
1953 // Still some initializer values to go. Push them onto the worklist.
1954 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1955 }
1956 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001957 }
Ted Kremenek87903072008-10-30 18:34:31 +00001958
1959 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001960 }
1961
Ted Kremenek062e2f92008-11-13 06:10:40 +00001962 if (T->isUnionType() || T->isVectorType()) {
1963 // FIXME: to be implemented.
1964 // Note: That vectors can return true for T->isIntegerType()
1965 MakeNode(Dst, E, Pred, state);
1966 return;
1967 }
1968
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001969 if (Loc::IsLocType(T) || T->isIntegerType()) {
1970 assert (E->getNumInits() == 1);
1971 NodeSet Tmp;
1972 Expr* Init = E->getInit(0);
1973 Visit(Init, Pred, Tmp);
1974 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1975 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001976 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001977 }
1978 return;
1979 }
1980
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001981
1982 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1983 assert(0 && "unprocessed InitListExpr type");
1984}
Ted Kremenekf233d482008-02-05 00:26:40 +00001985
Sebastian Redl05189992008-11-11 17:56:53 +00001986/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1987void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1988 NodeTy* Pred,
1989 NodeSet& Dst) {
1990 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001991 uint64_t amt;
1992
1993 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001994 if (T == getContext().VoidTy) {
1995 // sizeof(void) == 1 byte.
1996 amt = 1;
1997 }
1998 else if (!T.getTypePtr()->isConstantSizeType()) {
1999 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002000 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002001 }
2002 else if (T->isObjCInterfaceType()) {
2003 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2004 // the compiler has laid out its representation. Just report Unknown
2005 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002006 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002007 }
2008 else {
2009 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002010 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002011 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002012 }
2013 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002014 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002015
Ted Kremenek0e561a32008-03-21 21:30:14 +00002016 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002017 BindExpr(GetState(Pred), Ex,
2018 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002019}
2020
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002021
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002022void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002023 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002024
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002025 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002026
2027 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002028 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002029
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002030 case UnaryOperator::Deref: {
2031
2032 Expr* Ex = U->getSubExpr()->IgnoreParens();
2033 NodeSet Tmp;
2034 Visit(Ex, Pred, Tmp);
2035
2036 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002037
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002038 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002039 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002040
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002041 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002042 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002043 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002044 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002045 }
2046
2047 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002048 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002049
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002050 case UnaryOperator::Real: {
2051
2052 Expr* Ex = U->getSubExpr()->IgnoreParens();
2053 NodeSet Tmp;
2054 Visit(Ex, Pred, Tmp);
2055
2056 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2057
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002058 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002059 if (Ex->getType()->isAnyComplexType()) {
2060 // Just report "Unknown."
2061 Dst.Add(*I);
2062 continue;
2063 }
2064
2065 // For all other types, UnaryOperator::Real is an identity operation.
2066 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002067 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002068 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002069 }
2070
2071 return;
2072 }
2073
2074 case UnaryOperator::Imag: {
2075
2076 Expr* Ex = U->getSubExpr()->IgnoreParens();
2077 NodeSet Tmp;
2078 Visit(Ex, Pred, Tmp);
2079
2080 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002081 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002082 if (Ex->getType()->isAnyComplexType()) {
2083 // Just report "Unknown."
2084 Dst.Add(*I);
2085 continue;
2086 }
2087
2088 // For all other types, UnaryOperator::Float returns 0.
2089 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002090 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002091 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002092 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002093 }
2094
2095 return;
2096 }
2097
2098 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002099 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002100 Dst.Add(Pred);
2101 return;
2102
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002103 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002104 case UnaryOperator::Extension: {
2105
2106 // Unary "+" is a no-op, similar to a parentheses. We still have places
2107 // where it may be a block-level expression, so we need to
2108 // generate an extra node that just propagates the value of the
2109 // subexpression.
2110
2111 Expr* Ex = U->getSubExpr()->IgnoreParens();
2112 NodeSet Tmp;
2113 Visit(Ex, Pred, Tmp);
2114
2115 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002116 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002117 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002118 }
2119
2120 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002121 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002122
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002123 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002124
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002125 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002126 Expr* Ex = U->getSubExpr()->IgnoreParens();
2127 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002128 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002129
2130 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002131 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002132 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002133 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002134 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002135 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002136
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002137 return;
2138 }
2139
2140 case UnaryOperator::LNot:
2141 case UnaryOperator::Minus:
2142 case UnaryOperator::Not: {
2143
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002144 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002145 Expr* Ex = U->getSubExpr()->IgnoreParens();
2146 NodeSet Tmp;
2147 Visit(Ex, Pred, Tmp);
2148
2149 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002150 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002151
2152 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002153 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002154
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002155 if (V.isUnknownOrUndef()) {
2156 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2157 continue;
2158 }
2159
Ted Kremenek60595da2008-11-15 04:01:56 +00002160// QualType DstT = getContext().getCanonicalType(U->getType());
2161// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2162//
2163// if (DstT != SrcT) // Perform promotions.
2164// V = EvalCast(V, DstT);
2165//
2166// if (V.isUnknownOrUndef()) {
2167// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2168// continue;
2169// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002170
2171 switch (U->getOpcode()) {
2172 default:
2173 assert(false && "Invalid Opcode.");
2174 break;
2175
2176 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002177 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002178 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002179 break;
2180
2181 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002182 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002183 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002184 break;
2185
2186 case UnaryOperator::LNot:
2187
2188 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2189 //
2190 // Note: technically we do "E == 0", but this is the same in the
2191 // transfer functions as "0 == E".
2192
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002193 if (isa<Loc>(V)) {
2194 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2195 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002196 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002197 }
2198 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002199 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002200#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002201 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2202 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002203#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002204 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002205 continue;
2206#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002207 }
2208
2209 break;
2210 }
2211
2212 MakeNode(Dst, U, *I, St);
2213 }
2214
2215 return;
2216 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002217 }
2218
2219 // Handle ++ and -- (both pre- and post-increment).
2220
2221 assert (U->isIncrementDecrementOp());
2222 NodeSet Tmp;
2223 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002224 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002225
2226 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2227
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002228 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002229 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002230
2231 // Perform a load.
2232 NodeSet Tmp2;
2233 EvalLoad(Tmp2, Ex, *I, St, V1);
2234
2235 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2236
2237 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002238 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002239
2240 // Propagate unknown and undefined values.
2241 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002242 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002243 continue;
2244 }
2245
Ted Kremenek443003b2008-02-21 19:29:23 +00002246 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002247
2248 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2249 : BinaryOperator::Sub;
2250
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002251 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002252 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002253
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002254 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002255 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002256 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002257 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002258}
2259
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002260void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2261 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2262}
2263
2264void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2265 AsmStmt::outputs_iterator I,
2266 AsmStmt::outputs_iterator E,
2267 NodeTy* Pred, NodeSet& Dst) {
2268 if (I == E) {
2269 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2270 return;
2271 }
2272
2273 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002274 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002275
2276 ++I;
2277
2278 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2279 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2280}
2281
2282void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2283 AsmStmt::inputs_iterator I,
2284 AsmStmt::inputs_iterator E,
2285 NodeTy* Pred, NodeSet& Dst) {
2286 if (I == E) {
2287
2288 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002289 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002290
2291 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2292 // which interprets the inline asm and stores proper results in the
2293 // outputs.
2294
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002295 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002296
2297 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2298 OE = A->end_outputs(); OI != OE; ++OI) {
2299
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002300 SVal X = GetSVal(St, *OI);
2301 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002302
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002303 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002304 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002305 }
2306
Ted Kremenek0e561a32008-03-21 21:30:14 +00002307 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002308 return;
2309 }
2310
2311 NodeSet Tmp;
2312 Visit(*I, Pred, Tmp);
2313
2314 ++I;
2315
2316 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2317 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2318}
2319
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002320void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2321 assert (Builder && "GRStmtNodeBuilder must be defined.");
2322
2323 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002324
Ted Kremenek186350f2008-04-23 20:12:28 +00002325 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2326 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002327
Ted Kremenek729a9a22008-07-17 23:15:45 +00002328 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002329
Ted Kremenekb0533962008-04-18 20:35:30 +00002330 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002331
Ted Kremenekb0533962008-04-18 20:35:30 +00002332 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002333 MakeNode(Dst, S, Pred, GetState(Pred));
2334}
2335
Ted Kremenek02737ed2008-03-31 15:02:58 +00002336void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2337
2338 Expr* R = S->getRetValue();
2339
2340 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002341 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002342 return;
2343 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002344
Ted Kremenek5917d782008-11-21 00:27:44 +00002345 NodeSet Tmp;
2346 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002347
Ted Kremenek5917d782008-11-21 00:27:44 +00002348 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2349 SVal X = GetSVal((*I)->getState(), R);
2350
2351 // Check if we return the address of a stack variable.
2352 if (isa<loc::MemRegionVal>(X)) {
2353 // Determine if the value is on the stack.
2354 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002355
Ted Kremenek5917d782008-11-21 00:27:44 +00002356 if (R && getStateManager().hasStackStorage(R)) {
2357 // Create a special node representing the error.
2358 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2359 N->markAsSink();
2360 RetsStackAddr.insert(N);
2361 }
2362 continue;
2363 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002364 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002365 // Check if we return an undefined value.
2366 else if (X.isUndef()) {
2367 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2368 N->markAsSink();
2369 RetsUndef.insert(N);
2370 }
2371 continue;
2372 }
2373
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002374 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002375 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002376}
Ted Kremenek55deb972008-03-25 00:34:37 +00002377
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002378//===----------------------------------------------------------------------===//
2379// Transfer functions: Binary operators.
2380//===----------------------------------------------------------------------===//
2381
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002382const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2383 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002384
2385 // Divide by undefined? (potentially zero)
2386
2387 if (Denom.isUndef()) {
2388 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2389
2390 if (DivUndef) {
2391 DivUndef->markAsSink();
2392 ExplicitBadDivides.insert(DivUndef);
2393 }
2394
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002395 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002396 }
2397
2398 // Check for divide/remainder-by-zero.
2399 // First, "assume" that the denominator is 0 or undefined.
2400
2401 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002402 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002403
2404 // Second, "assume" that the denominator cannot be 0.
2405
2406 bool isFeasibleNotZero = false;
2407 St = Assume(St, Denom, true, isFeasibleNotZero);
2408
2409 // Create the node for the divide-by-zero (if it occurred).
2410
2411 if (isFeasibleZero)
2412 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2413 DivZeroNode->markAsSink();
2414
2415 if (isFeasibleNotZero)
2416 ImplicitBadDivides.insert(DivZeroNode);
2417 else
2418 ExplicitBadDivides.insert(DivZeroNode);
2419
2420 }
2421
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002422 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002423}
2424
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002425void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002426 GRExprEngine::NodeTy* Pred,
2427 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002428
2429 NodeSet Tmp1;
2430 Expr* LHS = B->getLHS()->IgnoreParens();
2431 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002432
Ted Kremenek759623e2008-12-06 02:39:30 +00002433 // FIXME: Add proper support for ObjCKVCRefExpr.
2434 if (isa<ObjCKVCRefExpr>(LHS)) {
2435 Visit(RHS, Pred, Dst);
2436 return;
2437 }
2438
2439
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002440 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002441 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002442 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002443 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002444
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002445 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002446
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002447 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002448
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002449 // Process the RHS.
2450
2451 NodeSet Tmp2;
2452 Visit(RHS, *I1, Tmp2);
2453
2454 // With both the LHS and RHS evaluated, process the operation itself.
2455
2456 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002457
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002458 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002459 const GRState* OldSt = St;
2460
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002461 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002462 BinaryOperator::Opcode Op = B->getOpcode();
2463
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002464 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002465
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002466 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002467
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002468 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002469 // FIXME: Handle structs.
2470 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002471
Ted Kremenek062e2f92008-11-13 06:10:40 +00002472 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2473 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002474 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002475 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002476
Ted Kremenek062e2f92008-11-13 06:10:40 +00002477 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002478 ? cast<SVal>(loc::SymbolVal(Sym))
2479 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002480 }
2481
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002482 // Simulate the effects of a "store": bind the value of the RHS
2483 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002484
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002485 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002486 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002487 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002488
2489 case BinaryOperator::Div:
2490 case BinaryOperator::Rem:
2491
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002492 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002493 if (RHS->getType()->isIntegerType() &&
2494 RHS->getType()->isScalarType()) {
2495
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002496 St = CheckDivideZero(B, St, *I2, RightV);
2497 if (!St) continue;
2498 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002499
2500 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002501
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002502 default: {
2503
2504 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002505 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002506
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002507 // Process non-assignements except commas or short-circuited
2508 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002509
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002510 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002511
2512 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002513 if (OldSt != St) {
2514 // Generate a new node if we have already created a new state.
2515 MakeNode(Dst, B, *I2, St);
2516 }
2517 else
2518 Dst.Add(*I2);
2519
Ted Kremenek89063af2008-02-21 19:15:37 +00002520 continue;
2521 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002522
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002523 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002524
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002525 // The operands were *not* undefined, but the result is undefined.
2526 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002527
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002528 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002529 UndefNode->markAsSink();
2530 UndefResults.insert(UndefNode);
2531 }
2532
2533 continue;
2534 }
2535
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002536 // Otherwise, create a new node.
2537
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002538 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002539 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002540 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002541 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002542
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002543 assert (B->isCompoundAssignmentOp());
2544
Ted Kremenek934e3e92008-10-27 23:02:39 +00002545 if (Op >= BinaryOperator::AndAssign) {
2546 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2547 BinaryOperator::And));
2548 }
2549 else {
2550 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2551 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002552
2553 // Perform a load (the LHS). This performs the checks for
2554 // null dereferences, and so on.
2555 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002556 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002557 EvalLoad(Tmp3, LHS, *I2, St, location);
2558
2559 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2560
2561 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002562 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002563
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002564 // Check for divide-by-zero.
2565 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002566 && RHS->getType()->isIntegerType()
2567 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002568
2569 // CheckDivideZero returns a new state where the denominator
2570 // is assumed to be non-zero.
2571 St = CheckDivideZero(B, St, *I3, RightV);
2572
2573 if (!St)
2574 continue;
2575 }
2576
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002577 // Propagate undefined values (left-side).
2578 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002579 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002580 continue;
2581 }
2582
2583 // Propagate unknown values (left and right-side).
2584 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002585 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002586 UnknownVal());
2587 continue;
2588 }
2589
2590 // At this point:
2591 //
2592 // The LHS is not Undef/Unknown.
2593 // The RHS is not Unknown.
2594
2595 // Get the computation type.
2596 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002597 CTy = getContext().getCanonicalType(CTy);
2598
2599 QualType LTy = getContext().getCanonicalType(LHS->getType());
2600 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002601
2602 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002603 if (LTy != CTy) V = EvalCast(V, CTy);
2604 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002605
2606 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002607 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002608 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002609 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002610 continue;
2611 }
2612
Ted Kremenek60595da2008-11-15 04:01:56 +00002613 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002614 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002615
2616 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002617 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002618 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2619 UndefNode->markAsSink();
2620 UndefResults.insert(UndefNode);
2621 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002622 continue;
2623 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002624
2625 // EXPERIMENTAL: "Conjured" symbols.
2626 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002627
2628 SVal LHSVal;
2629
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002630 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2631 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002632
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002633 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002634
Ted Kremenek60595da2008-11-15 04:01:56 +00002635 // The symbolic value is actually for the type of the left-hand side
2636 // expression, not the computation type, as this is the value the
2637 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002638 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002639 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002640 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002641 : cast<SVal>(nonloc::SymbolVal(Sym));
2642
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002643 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002644 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002645 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002646 else {
2647 // The left-hand side may bind to a different value then the
2648 // computation type.
2649 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2650 }
2651
2652 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002653 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002654 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002655 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002656}
Ted Kremenekee985462008-01-16 18:18:48 +00002657
2658//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002659// Transfer-function Helpers.
2660//===----------------------------------------------------------------------===//
2661
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002662void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002663 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002664 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002665 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002666
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002667 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002668 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2669
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002670 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002671 MakeNode(Dst, Ex, Pred, *I);
2672}
2673
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002674void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002675 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002676 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002677
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002678 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002679 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002680}
2681
2682//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002683// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002684//===----------------------------------------------------------------------===//
2685
Ted Kremenekaa66a322008-01-16 21:46:15 +00002686#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002687static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002688static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002689
Ted Kremenekaa66a322008-01-16 21:46:15 +00002690namespace llvm {
2691template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002692struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002693 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002694
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002695 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2696
2697 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002698 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002699 GraphPrintCheckerState->isUndefDeref(N) ||
2700 GraphPrintCheckerState->isUndefStore(N) ||
2701 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002702 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2703 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002704 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002705 GraphPrintCheckerState->isBadCall(N) ||
2706 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002707 return "color=\"red\",style=\"filled\"";
2708
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002709 if (GraphPrintCheckerState->isNoReturnCall(N))
2710 return "color=\"blue\",style=\"filled\"";
2711
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002712 return "";
2713 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002714
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002715 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002716 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002717
2718 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002719 ProgramPoint Loc = N->getLocation();
2720
2721 switch (Loc.getKind()) {
2722 case ProgramPoint::BlockEntranceKind:
2723 Out << "Block Entrance: B"
2724 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2725 break;
2726
2727 case ProgramPoint::BlockExitKind:
2728 assert (false);
2729 break;
2730
Ted Kremenekaa66a322008-01-16 21:46:15 +00002731 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002732 if (isa<PostStmt>(Loc)) {
2733 const PostStmt& L = cast<PostStmt>(Loc);
2734 Stmt* S = L.getStmt();
2735 SourceLocation SLoc = S->getLocStart();
2736
2737 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2738 llvm::raw_os_ostream OutS(Out);
2739 S->printPretty(OutS);
2740 OutS.flush();
2741
2742 if (SLoc.isFileID()) {
2743 Out << "\\lline="
2744 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2745 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2746 }
2747
2748 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2749 Out << "\\|Implicit-Null Dereference.\\l";
2750 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2751 Out << "\\|Explicit-Null Dereference.\\l";
2752 else if (GraphPrintCheckerState->isUndefDeref(N))
2753 Out << "\\|Dereference of undefialied value.\\l";
2754 else if (GraphPrintCheckerState->isUndefStore(N))
2755 Out << "\\|Store to Undefined Loc.";
2756 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2757 Out << "\\|Explicit divide-by zero or undefined value.";
2758 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2759 Out << "\\|Implicit divide-by zero or undefined value.";
2760 else if (GraphPrintCheckerState->isUndefResult(N))
2761 Out << "\\|Result of operation is undefined.";
2762 else if (GraphPrintCheckerState->isNoReturnCall(N))
2763 Out << "\\|Call to function marked \"noreturn\".";
2764 else if (GraphPrintCheckerState->isBadCall(N))
2765 Out << "\\|Call to NULL/Undefined.";
2766 else if (GraphPrintCheckerState->isUndefArg(N))
2767 Out << "\\|Argument in call is undefined";
2768
2769 break;
2770 }
2771
Ted Kremenekaa66a322008-01-16 21:46:15 +00002772 const BlockEdge& E = cast<BlockEdge>(Loc);
2773 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2774 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002775
2776 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002777
2778 SourceLocation SLoc = T->getLocStart();
2779
Ted Kremenekb38911f2008-01-30 23:03:39 +00002780 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002781
Ted Kremeneka95d3752008-09-13 05:16:45 +00002782 llvm::raw_os_ostream OutS(Out);
2783 E.getSrc()->printTerminator(OutS);
2784 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002785
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002786 if (SLoc.isFileID()) {
2787 Out << "\\lline="
2788 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2789 << GraphPrintSourceManager->getColumnNumber(SLoc);
2790 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002791
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002792 if (isa<SwitchStmt>(T)) {
2793 Stmt* Label = E.getDst()->getLabel();
2794
2795 if (Label) {
2796 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2797 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002798 llvm::raw_os_ostream OutS(Out);
2799 C->getLHS()->printPretty(OutS);
2800 OutS.flush();
2801
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002802 if (Stmt* RHS = C->getRHS()) {
2803 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002804 RHS->printPretty(OutS);
2805 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002806 }
2807
2808 Out << ":";
2809 }
2810 else {
2811 assert (isa<DefaultStmt>(Label));
2812 Out << "\\ldefault:";
2813 }
2814 }
2815 else
2816 Out << "\\l(implicit) default:";
2817 }
2818 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002819 // FIXME
2820 }
2821 else {
2822 Out << "\\lCondition: ";
2823 if (*E.getSrc()->succ_begin() == E.getDst())
2824 Out << "true";
2825 else
2826 Out << "false";
2827 }
2828
2829 Out << "\\l";
2830 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002831
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002832 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2833 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002834 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002835 }
2836 }
2837
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002838 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002839
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002840 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2841 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002842
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002843 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002844 return Out.str();
2845 }
2846};
2847} // end llvm namespace
2848#endif
2849
Ted Kremenekffe0f432008-03-07 22:58:01 +00002850#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002851
2852template <typename ITERATOR>
2853GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2854
2855template <>
2856GRExprEngine::NodeTy*
2857GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2858 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2859 return I->first;
2860}
2861
Ted Kremenekffe0f432008-03-07 22:58:01 +00002862template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002863static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002864 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002865
Ted Kremenekd4527582008-09-16 18:44:52 +00002866 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002867
2868 for ( ; I != E; ++I ) {
2869 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002870 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002871
Ted Kremenekd4527582008-09-16 18:44:52 +00002872 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002873 continue;
2874
Ted Kremenekd4527582008-09-16 18:44:52 +00002875 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002876 Sources.push_back(N);
2877 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002878}
2879#endif
2880
2881void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002882#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002883 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002884 std::vector<NodeTy*> Src;
2885
2886 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002887 AddSources(Src, null_derefs_begin(), null_derefs_end());
2888 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2889 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2890 AddSources(Src, undef_results_begin(), undef_results_end());
2891 AddSources(Src, bad_calls_begin(), bad_calls_end());
2892 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002893 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002894
Ted Kremenekcb612922008-04-18 19:23:43 +00002895 // The new way.
2896 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2897 (*I)->GetErrorNodes(Src);
2898
2899
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002900 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002901 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002902 else {
2903 GraphPrintCheckerState = this;
2904 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002905
Ted Kremenekffe0f432008-03-07 22:58:01 +00002906 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002907
2908 GraphPrintCheckerState = NULL;
2909 GraphPrintSourceManager = NULL;
2910 }
2911#endif
2912}
2913
2914void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2915#ifndef NDEBUG
2916 GraphPrintCheckerState = this;
2917 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002918
Ted Kremenek493d7a22008-03-11 18:25:33 +00002919 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2920
2921 if (!TrimmedG)
2922 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2923 else {
2924 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2925 delete TrimmedG;
2926 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002927
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002928 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002929 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002930#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002931}