blob: 1487ed6f2ca82465b204b38be1909901a7d1ee61 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000017#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000018#include "clang/Basic/SourceManager.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000020#include "llvm/ADT/ImmutableList.h"
21#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000023
Ted Kremenek0f5f0592008-02-27 06:07:00 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#include <sstream>
27#endif
28
Ted Kremenekb387a3f2008-02-14 22:16:04 +000029using namespace clang;
30using llvm::dyn_cast;
31using llvm::cast;
32using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000033
Ted Kremeneke695e1c2008-04-15 23:06:53 +000034//===----------------------------------------------------------------------===//
35// Engine construction and deletion.
36//===----------------------------------------------------------------------===//
37
Ted Kremenekbdb435d2008-07-11 18:37:32 +000038namespace {
39
40class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
41 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
42 typedef llvm::DenseMap<void*,Checks> MapTy;
43
44 MapTy M;
45 Checks::Factory F;
46
47public:
48 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
49
50 virtual ~MappedBatchAuditor() {
51 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
52
53 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
54 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
55
56 GRSimpleAPICheck* check = *I;
57
58 if (AlreadyVisited.count(check))
59 continue;
60
61 AlreadyVisited.insert(check);
62 delete check;
63 }
64 }
65
66 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
67 assert (A && "Check cannot be null.");
68 void* key = reinterpret_cast<void*>((uintptr_t) C);
69 MapTy::iterator I = M.find(key);
70 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
71 }
72
73 virtual void EmitWarnings(BugReporter& BR) {
74 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
75
76 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
77 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
78
79 GRSimpleAPICheck* check = *I;
80
81 if (AlreadyVisited.count(check))
82 continue;
83
84 check->EmitWarnings(BR);
85 }
86 }
87
Ted Kremenek4adc81e2008-08-13 04:27:00 +000088 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000089 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
90 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
91 MapTy::iterator MI = M.find(key);
92
93 if (MI == M.end())
94 return false;
95
96 bool isSink = false;
97
98 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000099 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Zhongxing Xuff944a82008-12-22 01:52:37 +0000118 LiveVariables& L, bool purgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000119 StoreManagerCreator SMC,
120 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000121 : CoreEngine(cfg, CD, Ctx, *this),
122 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000123 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000125 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000127 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
129 RaiseSel(GetNullarySelector("raise", G.getContext())),
130 PurgeDead(purgeDead){}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000131
Ted Kremenek1a654b62008-06-20 21:45:25 +0000132GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000133 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
134 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000135
Ted Kremeneke448ab42008-05-01 18:33:28 +0000136
137 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000138}
139
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000140//===----------------------------------------------------------------------===//
141// Utility methods.
142//===----------------------------------------------------------------------===//
143
144// SaveAndRestore - A utility class that uses RIIA to save and restore
145// the value of a variable.
146template<typename T>
147struct VISIBILITY_HIDDEN SaveAndRestore {
148 SaveAndRestore(T& x) : X(x), old_value(x) {}
149 ~SaveAndRestore() { X = old_value; }
150 T get() { return old_value; }
151
152 T& X;
153 T old_value;
154};
155
Ted Kremenek186350f2008-04-23 20:12:28 +0000156// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
157// value of a variable is saved, and during the dstor the old value is
158// or'ed with the new value.
159struct VISIBILITY_HIDDEN SaveOr {
160 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
161 ~SaveOr() { X |= old_value; }
162
163 bool& X;
164 bool old_value;
165};
166
167
Ted Kremenekc0959972008-07-02 21:24:01 +0000168void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000169 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000170 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000171 (*I)->EmitWarnings(BR);
172 }
173
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000174 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000175 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000176 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000177 }
178}
179
180void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000181 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000182 tf->RegisterChecks(*this);
183 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000184}
185
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000186void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
187 if (!BatchAuditor)
188 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
189
190 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000191}
192
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000193const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000194 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000195}
196
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000197//===----------------------------------------------------------------------===//
198// Top-level transfer function logic (Dispatcher).
199//===----------------------------------------------------------------------===//
200
201void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
202
203 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000213
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000214 // Create the cleaned state.
Zhongxing Xuff944a82008-12-22 01:52:37 +0000215 if (PurgeDead)
216 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt,
218 Liveness, DeadSymbols);
219 else
220 CleanedState = EntryNode->getState();
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000221
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000223 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000224
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000225 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000226 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000227 else {
228 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000229 SaveOr OldHasGen(Builder->HasGeneratedNode);
230
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000231 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
232 Builder->PurgingDeadSymbols = true;
233
Ted Kremenek729a9a22008-07-17 23:15:45 +0000234 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000235 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000236
237 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
238 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000239 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000240
241 bool HasAutoGenerated = false;
242
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000243 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000244
245 NodeSet Dst;
246
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000247 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000248 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
249
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000250 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000251 Visit(S, *I, Dst);
252
253 // Do we need to auto-generate a node? We only need to do this to generate
254 // a node with a "cleaned" state; GRCoreEngine will actually handle
255 // auto-transitions for other cases.
256 if (Dst.size() == 1 && *Dst.begin() == EntryNode
257 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
258 HasAutoGenerated = true;
259 builder.generateNode(S, GetState(EntryNode), *I);
260 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000261 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000263 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000264 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000265 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000266
267 // FIXME: Consolidate.
268 StateMgr.CurrentStmt = 0;
269 CurrentStmt = 0;
270
Ted Kremenek846d4e92008-04-24 23:35:58 +0000271 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000272}
273
274void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
275
276 // FIXME: add metadata to the CFG so that we can disable
277 // this check when we KNOW that there is no block-level subexpression.
278 // The motivation is that this check requires a hashtable lookup.
279
280 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
281 Dst.Add(Pred);
282 return;
283 }
284
285 switch (S->getStmtClass()) {
286
287 default:
288 // Cases we intentionally have "default" handle:
289 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
290
291 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
292 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000293
294 case Stmt::ArraySubscriptExprClass:
295 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
296 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000297
298 case Stmt::AsmStmtClass:
299 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
300 break;
301
302 case Stmt::BinaryOperatorClass: {
303 BinaryOperator* B = cast<BinaryOperator>(S);
304
305 if (B->isLogicalOp()) {
306 VisitLogicalExpr(B, Pred, Dst);
307 break;
308 }
309 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000310 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000311 MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000312 break;
313 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000314
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000315 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
316 break;
317 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000318
Douglas Gregorb4609802008-11-14 16:09:21 +0000319 case Stmt::CallExprClass:
320 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000321 CallExpr* C = cast<CallExpr>(S);
322 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000323 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000325
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000326 // FIXME: ChooseExpr is really a constant. We need to fix
327 // the CFG do not model them as explicit control-flow.
328
329 case Stmt::ChooseExprClass: { // __builtin_choose_expr
330 ChooseExpr* C = cast<ChooseExpr>(S);
331 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
332 break;
333 }
334
335 case Stmt::CompoundAssignOperatorClass:
336 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
337 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000338
339 case Stmt::CompoundLiteralExprClass:
340 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
341 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000342
343 case Stmt::ConditionalOperatorClass: { // '?' operator
344 ConditionalOperator* C = cast<ConditionalOperator>(S);
345 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
346 break;
347 }
348
349 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000350 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000351 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000352 break;
353
354 case Stmt::DeclStmtClass:
355 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
356 break;
357
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000358 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000359 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000360 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000361 VisitCast(C, C->getSubExpr(), Pred, Dst);
362 break;
363 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000364
365 case Stmt::InitListExprClass:
366 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
367 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000368
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000369 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000370 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
371 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000372
373 case Stmt::ObjCIvarRefExprClass:
374 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
375 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000376
377 case Stmt::ObjCForCollectionStmtClass:
378 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
379 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000380
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000381 case Stmt::ObjCMessageExprClass: {
382 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
383 break;
384 }
385
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000386 case Stmt::ObjCAtThrowStmtClass: {
387 // FIXME: This is not complete. We basically treat @throw as
388 // an abort.
389 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
390 Builder->BuildSinks = true;
391 MakeNode(Dst, S, Pred, GetState(Pred));
392 break;
393 }
394
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000395 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000396 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000397 break;
398
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000399 case Stmt::ReturnStmtClass:
400 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
401 break;
402
Sebastian Redl05189992008-11-11 17:56:53 +0000403 case Stmt::SizeOfAlignOfExprClass:
404 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 break;
406
407 case Stmt::StmtExprClass: {
408 StmtExpr* SE = cast<StmtExpr>(S);
409
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000410 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411
412 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
413 // probably just remove these from the CFG.
414 assert (!SE->getSubStmt()->body_empty());
415
416 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000417 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000418 else
419 Dst.Add(Pred);
420
421 break;
422 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000423
424 case Stmt::StringLiteralClass:
425 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
426 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000427
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000428 case Stmt::UnaryOperatorClass:
429 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000430 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000431 }
432}
433
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000434void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000435
436 Ex = Ex->IgnoreParens();
437
438 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
439 Dst.Add(Pred);
440 return;
441 }
442
443 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000444
445 case Stmt::ArraySubscriptExprClass:
446 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
447 return;
448
449 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000450 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
452 return;
453
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000454 case Stmt::ObjCIvarRefExprClass:
455 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
456 return;
457
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000458 case Stmt::UnaryOperatorClass:
459 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
460 return;
461
462 case Stmt::MemberExprClass:
463 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
464 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000465
Ted Kremenek4f090272008-10-27 21:54:31 +0000466 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000467 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000468 return;
469
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000470 case Stmt::ObjCPropertyRefExprClass:
471 // FIXME: Property assignments are lvalues, but not really "locations".
472 // e.g.: self.x = something;
473 // Here the "self.x" really can translate to a method call (setter) when
474 // the assignment is made. Moreover, the entire assignment expression
475 // evaluate to whatever "something" is, not calling the "getter" for
476 // the property (which would make sense since it can have side effects).
477 // We'll probably treat this as a location, but not one that we can
478 // take the address of. Perhaps we need a new SVal class for cases
479 // like thsis?
480 // Note that we have a similar problem for bitfields, since they don't
481 // have "locations" in the sense that we can take their address.
482 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000483 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000484
485 case Stmt::StringLiteralClass: {
486 const GRState* St = GetState(Pred);
487 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000488 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000489 return;
490 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000491
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000492 default:
493 // Arbitrary subexpressions can return aggregate temporaries that
494 // can be used in a lvalue context. We need to enhance our support
495 // of such temporaries in both the environment and the store, so right
496 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000497 assert ((Ex->getType()->isAggregateType() ||
498 Ex->getType()->isUnionType()) &&
499 "Other kinds of expressions with non-aggregate/union types do"
500 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000501
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000502 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000503 }
504}
505
506//===----------------------------------------------------------------------===//
507// Block entrance. (Update counters).
508//===----------------------------------------------------------------------===//
509
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000510bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000511 GRBlockCounter BC) {
512
513 return BC.getNumVisited(B->getBlockID()) < 3;
514}
515
516//===----------------------------------------------------------------------===//
517// Branch processing.
518//===----------------------------------------------------------------------===//
519
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000520const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000521 Stmt* Terminator,
522 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000523
524 switch (Terminator->getStmtClass()) {
525 default:
526 return St;
527
528 case Stmt::BinaryOperatorClass: { // '&&' and '||'
529
530 BinaryOperator* B = cast<BinaryOperator>(Terminator);
531 BinaryOperator::Opcode Op = B->getOpcode();
532
533 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
534
535 // For &&, if we take the true branch, then the value of the whole
536 // expression is that of the RHS expression.
537 //
538 // For ||, if we take the false branch, then the value of the whole
539 // expression is that of the RHS expression.
540
541 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
542 (Op == BinaryOperator::LOr && !branchTaken)
543 ? B->getRHS() : B->getLHS();
544
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000545 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000546 }
547
548 case Stmt::ConditionalOperatorClass: { // ?:
549
550 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
551
552 // For ?, if branchTaken == true then the value is either the LHS or
553 // the condition itself. (GNU extension).
554
555 Expr* Ex;
556
557 if (branchTaken)
558 Ex = C->getLHS() ? C->getLHS() : C->getCond();
559 else
560 Ex = C->getRHS();
561
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000562 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000563 }
564
565 case Stmt::ChooseExprClass: { // ?:
566
567 ChooseExpr* C = cast<ChooseExpr>(Terminator);
568
569 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000570 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000571 }
572 }
573}
574
Ted Kremenekaf337412008-11-12 19:24:17 +0000575void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000576 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000577
Ted Kremeneke7d22112008-02-11 19:21:59 +0000578 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000579 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000580 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000581
Ted Kremenekb2331832008-02-15 22:29:00 +0000582 // Check for NULL conditions; e.g. "for(;;)"
583 if (!Condition) {
584 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000585 return;
586 }
587
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000588 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000589
590 switch (V.getBaseKind()) {
591 default:
592 break;
593
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000594 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000595 builder.generateNode(MarkBranch(PrevState, Term, true), true);
596 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000597 return;
598
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000599 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000600 NodeTy* N = builder.generateNode(PrevState, true);
601
602 if (N) {
603 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000604 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000605 }
606
607 builder.markInfeasible(false);
608 return;
609 }
610 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000611
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000612 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000613
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000614 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000615 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000616
617 if (isFeasible)
618 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000619 else
620 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000621
622 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000623
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000624 isFeasible = false;
625 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000626
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000627 if (isFeasible)
628 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000629 else
630 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000631}
632
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000633/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000634/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000635void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000636
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000637 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000638 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000639
640 // Three possibilities:
641 //
642 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000643 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000644 // (3) We have no clue about the label. Dispatch to all targets.
645 //
646
647 typedef IndirectGotoNodeBuilder::iterator iterator;
648
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000649 if (isa<loc::GotoLabel>(V)) {
650 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000651
652 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000653 if (I.getLabel() == L) {
654 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000655 return;
656 }
657 }
658
659 assert (false && "No block with label.");
660 return;
661 }
662
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000663 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000664 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000665 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000666 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000667 return;
668 }
669
670 // This is really a catch-all. We don't support symbolics yet.
671
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000672 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000673
674 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000675 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000676}
Ted Kremenekf233d482008-02-05 00:26:40 +0000677
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000678
679void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
680 NodeTy* Pred, NodeSet& Dst) {
681
682 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
683
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000684 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000685 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000686
687 assert (X.isUndef());
688
689 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
690
691 assert (SE);
692
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000693 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000694
695 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000696 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000697}
698
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000699/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
700/// nodes by processing the 'effects' of a switch statement.
701void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
702
703 typedef SwitchNodeBuilder::iterator iterator;
704
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000705 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000706 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000707 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000708
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000709 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000710 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000711 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000712 return;
713 }
714
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000715 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000716
717 // While most of this can be assumed (such as the signedness), having it
718 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000719
Chris Lattner98be4942008-03-05 18:54:05 +0000720 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000721
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000722 APSInt V1(bits, false);
723 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000724 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000725
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000726 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000727
728 CaseStmt* Case = cast<CaseStmt>(I.getCase());
729
730 // Evaluate the case.
731 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
732 assert (false && "Case condition must evaluate to an integer constant.");
733 return;
734 }
735
736 // Get the RHS of the case, if it exists.
737
738 if (Expr* E = Case->getRHS()) {
739 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
740 assert (false &&
741 "Case condition (RHS) must evaluate to an integer constant.");
742 return ;
743 }
744
745 assert (V1 <= V2);
746 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000747 else
748 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000749
750 // FIXME: Eventually we should replace the logic below with a range
751 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000752 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000753
Ted Kremenek14a11402008-03-17 22:17:56 +0000754 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000755 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000756
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000757 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000758
759 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000760
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000761 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000762 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000763
764 if (isFeasible) {
765 builder.generateCaseStmtNode(I, StNew);
766
767 // If CondV evaluates to a constant, then we know that this
768 // is the *only* case that we can take, so stop evaluating the
769 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000770 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000771 return;
772 }
773
774 // Now "assume" that the case doesn't match. Add this state
775 // to the default state (if it is feasible).
776
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000777 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000778 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000779
Ted Kremenek5014ab12008-04-23 05:03:18 +0000780 if (isFeasible) {
781 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000782 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000783 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000784
Ted Kremenek14a11402008-03-17 22:17:56 +0000785 // Concretize the next value in the range.
786 if (V1 == V2)
787 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000788
Ted Kremenek14a11402008-03-17 22:17:56 +0000789 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000790 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000791
792 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000793 }
794
795 // If we reach here, than we know that the default branch is
796 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000797 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000798}
799
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000800//===----------------------------------------------------------------------===//
801// Transfer functions: logical operations ('&&', '||').
802//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000803
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000804void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000805 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000806
Ted Kremenek05a23782008-02-26 19:05:15 +0000807 assert (B->getOpcode() == BinaryOperator::LAnd ||
808 B->getOpcode() == BinaryOperator::LOr);
809
810 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
811
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000812 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000813 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000814
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000815 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000816
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000817 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000818
819 assert (Ex);
820
821 if (Ex == B->getRHS()) {
822
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000823 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000824
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000825 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000826
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000827 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000828 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000829 return;
830 }
831
Ted Kremenek05a23782008-02-26 19:05:15 +0000832 // We took the RHS. Because the value of the '&&' or '||' expression must
833 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
834 // or 1. Alternatively, we could take a lazy approach, and calculate this
835 // value later when necessary. We don't have the machinery in place for
836 // this right now, and since most logical expressions are used for branches,
837 // the payoff is not likely to be large. Instead, we do eager evaluation.
838
839 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000840 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000841
842 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000843 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000844 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000845
846 isFeasible = false;
847 NewState = Assume(St, X, false, isFeasible);
848
849 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000850 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000851 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000852 }
853 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000854 // We took the LHS expression. Depending on whether we are '&&' or
855 // '||' we know what the value of the expression is via properties of
856 // the short-circuiting.
857
858 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000859 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000860 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000861}
Ted Kremenek05a23782008-02-26 19:05:15 +0000862
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000863//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000864// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000865//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000866
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000867void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
868 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000869
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000870 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000871
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000872 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000873
874 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
875
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000876 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000877
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000878 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000879 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000880 else
881 EvalLoad(Dst, Ex, Pred, St, V);
882 return;
883
884 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
885 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
886
887 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000888 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000889 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000890 return;
891
892 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000893 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000894 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000895 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000896 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000897 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000898
899 assert (false &&
900 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000901}
902
Ted Kremenek540cbe22008-04-22 04:56:29 +0000903/// VisitArraySubscriptExpr - Transfer function for array accesses
904void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000905 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000906
907 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000908 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000909 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000910 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000911
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000912 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000913 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000914 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000915
916 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000917 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000918 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000919
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000920 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000921 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000922 else
923 EvalLoad(Dst, A, *I2, St, V);
924 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000925 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000926}
927
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000928/// VisitMemberExpr - Transfer function for member expressions.
929void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000930 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000931
932 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000933 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000934
935 if (M->isArrow())
936 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
937 else
938 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
939
Douglas Gregor86f19402008-12-20 23:49:58 +0000940 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
941 if (!Field) // FIXME: skipping member expressions for non-fields
942 return;
943
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000944 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000945 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000946 // FIXME: Should we insert some assumption logic in here to determine
947 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000948 // later when using FieldOffset lvals (which we no longer have).
949 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000950
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000951 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000952 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000953 else
954 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000955 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000956}
957
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000958void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000959 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000960
961 assert (Builder && "GRStmtNodeBuilder must be defined.");
962
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000963 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8c354752008-12-16 22:02:27 +0000964 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000965
Ted Kremenek8c354752008-12-16 22:02:27 +0000966 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000967 return;
968
Ted Kremenek8c354752008-12-16 22:02:27 +0000969 St = GetState(Pred);
970
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000971 // Proceed with the store.
972
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000973 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000974
Ted Kremenek186350f2008-04-23 20:12:28 +0000975 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000976 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000977 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000978
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000979 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000980 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000981
Ted Kremenek729a9a22008-07-17 23:15:45 +0000982 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000983
984 // Handle the case where no nodes where generated. Auto-generate that
985 // contains the updated state if we aren't generating sinks.
986
Ted Kremenekb0533962008-04-18 20:35:30 +0000987 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000988 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000989 location, Val);
990}
991
992void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000993 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000994 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000995
Ted Kremenek8c354752008-12-16 22:02:27 +0000996 // Evaluate the location (checks for bad dereferences).
997 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000998
Ted Kremenek8c354752008-12-16 22:02:27 +0000999 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001000 return;
1001
Ted Kremenek8c354752008-12-16 22:02:27 +00001002 St = GetState(Pred);
1003
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001004 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001005 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001006
1007 // FIXME: Currently symbolic analysis "generates" new symbols
1008 // for the contents of values. We need a better approach.
1009
1010 // FIXME: The "CheckOnly" option exists only because Array and Field
1011 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001012 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +00001013
Ted Kremenek8c354752008-12-16 22:02:27 +00001014 if (CheckOnly) {
1015 Dst.Add(Pred);
1016 return;
1017 }
1018
1019 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001020 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001021 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001022 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001023 else {
1024 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
1025 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1026 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001027}
1028
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001029void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001030 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001031
1032 NodeSet TmpDst;
1033 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1034
1035 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1036 MakeNode(Dst, Ex, *I, (*I)->getState());
1037}
1038
Ted Kremenek8c354752008-12-16 22:02:27 +00001039GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
1040 const GRState* St,
1041 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001042
1043 // Check for loads/stores from/to undefined values.
1044 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001045 NodeTy* N =
1046 Builder->generateNode(Ex, St, Pred,
1047 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001048
Ted Kremenek8c354752008-12-16 22:02:27 +00001049 if (N) {
1050 N->markAsSink();
1051 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052 }
1053
Ted Kremenek8c354752008-12-16 22:02:27 +00001054 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001055 }
1056
1057 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1058 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001059 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001060
1061 // During a load, one of two possible situations arise:
1062 // (1) A crash, because the location (pointer) was NULL.
1063 // (2) The location (pointer) is not NULL, and the dereference works.
1064 //
1065 // We add these assumptions.
1066
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001067 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001068
1069 // "Assume" that the pointer is not NULL.
1070
1071 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001072 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001073
1074 // "Assume" that the pointer is NULL.
1075
1076 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001077 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1078 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001079
1080 if (isFeasibleNull) {
1081
Ted Kremenek7360fda2008-09-18 23:09:54 +00001082 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001083 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001084 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1085
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001086 // We don't use "MakeNode" here because the node will be a sink
1087 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001088 NodeTy* NullNode =
1089 Builder->generateNode(Ex, StNull, Pred,
1090 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001091
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001092 if (NullNode) {
1093
1094 NullNode->markAsSink();
1095
1096 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1097 else ExplicitNullDeref.insert(NullNode);
1098 }
1099 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001100
1101 if (!isFeasibleNotNull)
1102 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001103
1104 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001105 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001106 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1107 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1108 // Get the index of the accessed element.
1109 SVal Idx = ER->getIndex();
1110 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001111 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1112 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001113
1114 bool isFeasibleInBound = false;
1115 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1116 true, isFeasibleInBound);
1117
1118 bool isFeasibleOutBound = false;
1119 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1120 false, isFeasibleOutBound);
1121
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001122 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001123 // Report warning. Make sink node manually.
1124 NodeTy* OOBNode =
1125 Builder->generateNode(Ex, StOutBound, Pred,
1126 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001127
1128 if (OOBNode) {
1129 OOBNode->markAsSink();
1130
1131 if (isFeasibleInBound)
1132 ImplicitOOBMemAccesses.insert(OOBNode);
1133 else
1134 ExplicitOOBMemAccesses.insert(OOBNode);
1135 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001136 }
1137
Ted Kremenek8c354752008-12-16 22:02:27 +00001138 if (!isFeasibleInBound)
1139 return 0;
1140
1141 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001142 }
1143 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001144
Ted Kremenek8c354752008-12-16 22:02:27 +00001145 // Generate a new node indicating the checks succeed.
1146 return Builder->generateNode(Ex, StNotNull, Pred,
1147 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001148}
1149
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001150//===----------------------------------------------------------------------===//
1151// Transfer function: Function calls.
1152//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001153void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001154 CallExpr::arg_iterator AI,
1155 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001156 NodeSet& Dst)
1157{
1158 // Determine the type of function we're calling (if available).
1159 const FunctionTypeProto *Proto = NULL;
1160 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1161 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1162 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1163
1164 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1165}
1166
1167void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1168 CallExpr::arg_iterator AI,
1169 CallExpr::arg_iterator AE,
1170 NodeSet& Dst, const FunctionTypeProto *Proto,
1171 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001172
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001173 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001174 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001175 // If the call argument is being bound to a reference parameter,
1176 // visit it as an lvalue, not an rvalue.
1177 bool VisitAsLvalue = false;
1178 if (Proto && ParamIdx < Proto->getNumArgs())
1179 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1180
1181 NodeSet DstTmp;
1182 if (VisitAsLvalue)
1183 VisitLValue(*AI, Pred, DstTmp);
1184 else
1185 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001186 ++AI;
1187
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001188 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001189 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001190
1191 return;
1192 }
1193
1194 // If we reach here we have processed all of the arguments. Evaluate
1195 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001196
Ted Kremenek994a09b2008-02-25 21:16:03 +00001197 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001198 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001199
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001200 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001201
Ted Kremenekde434242008-02-19 01:44:53 +00001202 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001203 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1204
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001205 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001206 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001207
Ted Kremeneka1354a52008-03-03 16:47:31 +00001208 // FIXME: Add support for symbolic function calls (calls involving
1209 // function pointer values that are symbolic).
1210
1211 // Check for undefined control-flow or calls to NULL.
1212
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001213 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001214 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001215
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001216 if (N) {
1217 N->markAsSink();
1218 BadCalls.insert(N);
1219 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001220
Ted Kremenekde434242008-02-19 01:44:53 +00001221 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001222 }
1223
1224 // Check for the "noreturn" attribute.
1225
1226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1227
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001228 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001229
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001230 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001231
1232 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001233 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001234 else {
1235 // HACK: Some functions are not marked noreturn, and don't return.
1236 // Here are a few hardwired ones. If this takes too long, we can
1237 // potentially cache these results.
1238 const char* s = FD->getIdentifier()->getName();
1239 unsigned n = strlen(s);
1240
1241 switch (n) {
1242 default:
1243 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001244
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001245 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001246 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1247 break;
1248
1249 case 5:
1250 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001251 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001252 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001253 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001254 // FIXME: use Assume to inspect the possible symbolic value of
1255 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001256 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001257 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001258 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001259 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001260 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001261 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001262
1263 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001264 if (!memcmp(s, "Assert", 6)) {
1265 Builder->BuildSinks = true;
1266 break;
1267 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001268
1269 // FIXME: This is just a wrapper around throwing an exception.
1270 // Eventually inter-procedural analysis should handle this easily.
1271 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1272
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001273 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001274
1275 case 7:
1276 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1277 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001278
Ted Kremenekf47bb782008-04-30 17:54:04 +00001279 case 8:
1280 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1281 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001282
1283 case 12:
1284 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1285 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001286
Ted Kremenekf9683082008-09-19 02:30:47 +00001287 case 13:
1288 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1289 break;
1290
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001291 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001292 if (!memcmp(s, "dtrace_assfail", 14) ||
1293 !memcmp(s, "yy_fatal_error", 14))
1294 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001295 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001296
1297 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001298 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1299 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001300 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001301
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001302 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001303 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001304
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001305 }
1306 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001307
1308 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001309
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001310 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001311
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001312 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001313
Ted Kremenek186350f2008-04-23 20:12:28 +00001314 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001315 switch (id) {
1316 case Builtin::BI__builtin_expect: {
1317 // For __builtin_expect, just return the value of the subexpression.
1318 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001319 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001320 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001321 continue;
1322 }
1323
Ted Kremenekb3021332008-11-02 00:35:01 +00001324 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001325 // FIXME: Refactor into StoreManager itself?
1326 MemRegionManager& RM = getStateManager().getRegionManager();
1327 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001328 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001329
1330 // Set the extent of the region in bytes. This enables us to use the
1331 // SVal of the argument directly. If we save the extent in bits, we
1332 // cannot represent values like symbol*8.
1333 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1334 St = getStoreManager().setExtent(St, R, Extent);
1335
Ted Kremenekb3021332008-11-02 00:35:01 +00001336 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1337 continue;
1338 }
1339
Ted Kremenek55aea312008-03-05 22:59:42 +00001340 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001341 break;
1342 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001343 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001344
Ted Kremenek186350f2008-04-23 20:12:28 +00001345 // Check any arguments passed-by-value against being undefined.
1346
1347 bool badArg = false;
1348
1349 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1350 I != E; ++I) {
1351
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001352 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001353 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001354
Ted Kremenek186350f2008-04-23 20:12:28 +00001355 if (N) {
1356 N->markAsSink();
1357 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001358 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001359
Ted Kremenek186350f2008-04-23 20:12:28 +00001360 badArg = true;
1361 break;
1362 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001363 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001364
1365 if (badArg)
1366 continue;
1367
1368 // Dispatch to the plug-in transfer function.
1369
1370 unsigned size = Dst.size();
1371 SaveOr OldHasGen(Builder->HasGeneratedNode);
1372 EvalCall(Dst, CE, L, *DI);
1373
1374 // Handle the case where no nodes where generated. Auto-generate that
1375 // contains the updated state if we aren't generating sinks.
1376
1377 if (!Builder->BuildSinks && Dst.size() == size &&
1378 !Builder->HasGeneratedNode)
1379 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001380 }
1381}
1382
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001383//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001384// Transfer function: Objective-C ivar references.
1385//===----------------------------------------------------------------------===//
1386
1387void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1388 NodeTy* Pred, NodeSet& Dst,
1389 bool asLValue) {
1390
1391 Expr* Base = cast<Expr>(Ex->getBase());
1392 NodeSet Tmp;
1393 Visit(Base, Pred, Tmp);
1394
1395 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1396 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001397 SVal BaseVal = GetSVal(St, Base);
1398 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001399
1400 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001401 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001402 else
1403 EvalLoad(Dst, Ex, *I, St, location);
1404 }
1405}
1406
1407//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001408// Transfer function: Objective-C fast enumeration 'for' statements.
1409//===----------------------------------------------------------------------===//
1410
1411void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1412 NodeTy* Pred, NodeSet& Dst) {
1413
1414 // ObjCForCollectionStmts are processed in two places. This method
1415 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1416 // statements within a basic block. This transfer function does two things:
1417 //
1418 // (1) binds the next container value to 'element'. This creates a new
1419 // node in the ExplodedGraph.
1420 //
1421 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1422 // whether or not the container has any more elements. This value
1423 // will be tested in ProcessBranch. We need to explicitly bind
1424 // this value because a container can contain nil elements.
1425 //
1426 // FIXME: Eventually this logic should actually do dispatches to
1427 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1428 // This will require simulating a temporary NSFastEnumerationState, either
1429 // through an SVal or through the use of MemRegions. This value can
1430 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1431 // terminates we reclaim the temporary (it goes out of scope) and we
1432 // we can test if the SVal is 0 or if the MemRegion is null (depending
1433 // on what approach we take).
1434 //
1435 // For now: simulate (1) by assigning either a symbol or nil if the
1436 // container is empty. Thus this transfer function will by default
1437 // result in state splitting.
1438
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001439 Stmt* elem = S->getElement();
1440 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001441
1442 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001443 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001444 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001445 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1446 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1447 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001448 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001449
1450 NodeSet Tmp;
1451 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001452
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001453 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1454 const GRState* state = GetState(*I);
1455 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1456 }
1457}
1458
1459void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1460 NodeTy* Pred, NodeSet& Dst,
1461 SVal ElementV) {
1462
1463
Ted Kremenekaf337412008-11-12 19:24:17 +00001464
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001465 // Get the current state. Use 'EvalLocation' to determine if it is a null
1466 // pointer, etc.
1467 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001468
Ted Kremenek8c354752008-12-16 22:02:27 +00001469 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1470 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001471 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001472
1473 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001474
Ted Kremenekaf337412008-11-12 19:24:17 +00001475 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001476 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001477 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1478 GRStateRef hasElems = state.BindExpr(S, TrueV);
1479
Ted Kremenekaf337412008-11-12 19:24:17 +00001480 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001481 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1482 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001483
1484 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1485 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1486 // FIXME: The proper thing to do is to really iterate over the
1487 // container. We will do this with dispatch logic to the store.
1488 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001489 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001490 assert (Loc::IsLocType(T));
1491 unsigned Count = Builder->getCurrentBlockCount();
1492 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1493 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001494
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001495 // Bind the location to 'nil' on the false branch.
1496 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1497 noElems = noElems.BindLoc(ElementV, nilV);
1498 }
1499
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001500 // Create the new nodes.
1501 MakeNode(Dst, S, Pred, hasElems);
1502 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001503}
1504
1505//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001506// Transfer function: Objective-C message expressions.
1507//===----------------------------------------------------------------------===//
1508
1509void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1510 NodeSet& Dst){
1511
1512 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1513 Pred, Dst);
1514}
1515
1516void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001517 ObjCMessageExpr::arg_iterator AI,
1518 ObjCMessageExpr::arg_iterator AE,
1519 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001520 if (AI == AE) {
1521
1522 // Process the receiver.
1523
1524 if (Expr* Receiver = ME->getReceiver()) {
1525 NodeSet Tmp;
1526 Visit(Receiver, Pred, Tmp);
1527
1528 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1529 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1530
1531 return;
1532 }
1533
1534 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1535 return;
1536 }
1537
1538 NodeSet Tmp;
1539 Visit(*AI, Pred, Tmp);
1540
1541 ++AI;
1542
1543 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1544 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1545}
1546
1547void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1548 NodeTy* Pred,
1549 NodeSet& Dst) {
1550
1551 // FIXME: More logic for the processing the method call.
1552
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001553 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001554 bool RaisesException = false;
1555
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001556
1557 if (Expr* Receiver = ME->getReceiver()) {
1558
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001559 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001560
1561 // Check for undefined control-flow or calls to NULL.
1562
1563 if (L.isUndef()) {
1564 NodeTy* N = Builder->generateNode(ME, St, Pred);
1565
1566 if (N) {
1567 N->markAsSink();
1568 UndefReceivers.insert(N);
1569 }
1570
1571 return;
1572 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001573
1574 // Check if the "raise" message was sent.
1575 if (ME->getSelector() == RaiseSel)
1576 RaisesException = true;
1577 }
1578 else {
1579
1580 IdentifierInfo* ClsName = ME->getClassName();
1581 Selector S = ME->getSelector();
1582
1583 // Check for special instance methods.
1584
1585 if (!NSExceptionII) {
1586 ASTContext& Ctx = getContext();
1587
1588 NSExceptionII = &Ctx.Idents.get("NSException");
1589 }
1590
1591 if (ClsName == NSExceptionII) {
1592
1593 enum { NUM_RAISE_SELECTORS = 2 };
1594
1595 // Lazily create a cache of the selectors.
1596
1597 if (!NSExceptionInstanceRaiseSelectors) {
1598
1599 ASTContext& Ctx = getContext();
1600
1601 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1602
1603 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1604 unsigned idx = 0;
1605
1606 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001607 II.push_back(&Ctx.Idents.get("raise"));
1608 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001609 NSExceptionInstanceRaiseSelectors[idx++] =
1610 Ctx.Selectors.getSelector(II.size(), &II[0]);
1611
1612 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001613 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001614 NSExceptionInstanceRaiseSelectors[idx++] =
1615 Ctx.Selectors.getSelector(II.size(), &II[0]);
1616 }
1617
1618 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1619 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1620 RaisesException = true; break;
1621 }
1622 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001623 }
1624
1625 // Check for any arguments that are uninitialized/undefined.
1626
1627 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1628 I != E; ++I) {
1629
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001630 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001631
1632 // Generate an error node for passing an uninitialized/undefined value
1633 // as an argument to a message expression. This node is a sink.
1634 NodeTy* N = Builder->generateNode(ME, St, Pred);
1635
1636 if (N) {
1637 N->markAsSink();
1638 MsgExprUndefArgs[N] = *I;
1639 }
1640
1641 return;
1642 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001643 }
1644
1645 // Check if we raise an exception. For now treat these as sinks. Eventually
1646 // we will want to handle exceptions properly.
1647
1648 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1649
1650 if (RaisesException)
1651 Builder->BuildSinks = true;
1652
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001653 // Dispatch to plug-in transfer function.
1654
1655 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001656 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001657
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001658 EvalObjCMessageExpr(Dst, ME, Pred);
1659
1660 // Handle the case where no nodes where generated. Auto-generate that
1661 // contains the updated state if we aren't generating sinks.
1662
Ted Kremenekb0533962008-04-18 20:35:30 +00001663 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001664 MakeNode(Dst, ME, Pred, St);
1665}
1666
1667//===----------------------------------------------------------------------===//
1668// Transfer functions: Miscellaneous statements.
1669//===----------------------------------------------------------------------===//
1670
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001671void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001672 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001673 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001674 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001675
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001676 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001677 T = ExCast->getTypeAsWritten();
1678
Zhongxing Xued340f72008-10-22 08:02:16 +00001679 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001680 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001681 else
1682 Visit(Ex, Pred, S1);
1683
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001684 // Check for casting to "void".
1685 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001686
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001687 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001688 Dst.Add(*I1);
1689
Ted Kremenek874d63f2008-01-24 02:02:54 +00001690 return;
1691 }
1692
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001693 // FIXME: The rest of this should probably just go into EvalCall, and
1694 // let the transfer function object be responsible for constructing
1695 // nodes.
1696
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001697 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001698 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001699 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001700 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001701
1702 // Unknown?
1703
1704 if (V.isUnknown()) {
1705 Dst.Add(N);
1706 continue;
1707 }
1708
1709 // Undefined?
1710
1711 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001712 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001713 continue;
1714 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001715
1716 // For const casts, just propagate the value.
1717 ASTContext& C = getContext();
1718
1719 if (C.getCanonicalType(T).getUnqualifiedType() ==
1720 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001721 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001722 continue;
1723 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001724
1725 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001726 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001727 unsigned bits = getContext().getTypeSize(ExTy);
1728
1729 // FIXME: Determine if the number of bits of the target type is
1730 // equal or exceeds the number of bits to store the pointer value.
1731 // If not, flag an error.
1732
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001733 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001734 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001735 continue;
1736 }
1737
1738 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001739 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1740 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001741 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001742 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001743 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001744 continue;
1745 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001746
Zhongxing Xu37d682a2008-11-14 09:23:38 +00001747 // Check for casts from array type to pointer type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001748 if (ExTy->isArrayType()) {
Ted Kremenek0fb7c612008-11-15 05:00:27 +00001749 assert(T->isPointerType());
Zhongxing Xue1911af2008-10-23 03:10:39 +00001750 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001751 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001752 continue;
1753 }
1754
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001755 // Check for casts from a region to a specific type.
1756 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001757 assert(Loc::IsLocType(T));
1758 assert(Loc::IsLocType(ExTy));
1759
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001760 const MemRegion* R = RV->getRegion();
1761 StoreManager& StoreMgr = getStoreManager();
1762
1763 // Delegate to store manager to get the result of casting a region
1764 // to a different type.
1765 const StoreManager::CastResult& Res = StoreMgr.CastRegion(St, R, T);
1766
1767 // Inspect the result. If the MemRegion* returned is NULL, this
1768 // expression evaluates to UnknownVal.
1769 R = Res.getRegion();
1770 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1771
1772 // Generate the new node in the ExplodedGraph.
1773 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001774 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001775 }
1776
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001777 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001778 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001779 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001780}
1781
Ted Kremenek4f090272008-10-27 21:54:31 +00001782void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001783 NodeTy* Pred, NodeSet& Dst,
1784 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001785 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1786 NodeSet Tmp;
1787 Visit(ILE, Pred, Tmp);
1788
1789 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001790 const GRState* St = GetState(*I);
1791 SVal ILV = GetSVal(St, ILE);
1792 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001793
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001794 if (asLValue)
1795 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1796 else
1797 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001798 }
1799}
1800
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001801void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001802
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001803 // The CFG has one DeclStmt per Decl.
1804 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001805
1806 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001807 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001808
Ted Kremenekefd59942008-12-08 22:47:34 +00001809 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001810 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001811
1812 // FIXME: static variables may have an initializer, but the second
1813 // time a function is called those values may not be current.
1814 NodeSet Tmp;
1815
Ted Kremenekaf337412008-11-12 19:24:17 +00001816 if (InitEx)
1817 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001818
1819 if (Tmp.empty())
1820 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001821
1822 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001823 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001824 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001825
1826 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001827 if (InitEx) {
1828 SVal InitVal = GetSVal(St, InitEx);
1829 QualType T = VD->getType();
1830
1831 // Recover some path-sensitivity if a scalar value evaluated to
1832 // UnknownVal.
1833 if (InitVal.isUnknown()) {
1834 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001835 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001836 InitVal = loc::SymbolVal(Sym);
1837 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001838 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001839 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001840 InitVal = nonloc::SymbolVal(Sym);
1841 }
1842 }
1843
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001844 St = StateMgr.BindDecl(St, VD, InitVal);
1845 } else
1846 St = StateMgr.BindDeclWithNoInit(St, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001847
1848 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1849 QualType T = getContext().getCanonicalType(VD->getType());
1850 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1851 // FIXME: Handle multi-dimensional VLAs.
1852
1853 Expr* SE = VLA->getSizeExpr();
1854 SVal Size = GetSVal(St, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001855
1856 if (Size.isUndef()) {
1857 if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
1858 N->markAsSink();
1859 ExplicitBadSizedVLA.insert(N);
1860 }
1861 continue;
1862 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001863
1864 bool isFeasibleZero = false;
1865 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1866
1867 bool isFeasibleNotZero = false;
1868 St = Assume(St, Size, true, isFeasibleNotZero);
1869
1870 if (isFeasibleZero) {
1871 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1872 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001873 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1874 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001875 }
1876 }
1877
1878 if (!isFeasibleNotZero)
1879 continue;
1880 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001881
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001882 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001883 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001884}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001885
Ted Kremenekf75b1862008-10-30 17:47:32 +00001886namespace {
1887 // This class is used by VisitInitListExpr as an item in a worklist
1888 // for processing the values contained in an InitListExpr.
1889class VISIBILITY_HIDDEN InitListWLItem {
1890public:
1891 llvm::ImmutableList<SVal> Vals;
1892 GRExprEngine::NodeTy* N;
1893 InitListExpr::reverse_iterator Itr;
1894
1895 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1896 InitListExpr::reverse_iterator itr)
1897 : Vals(vals), N(n), Itr(itr) {}
1898};
1899}
1900
1901
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001902void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1903 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001904
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001905 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001906 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001907 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001908
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001909 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001910
Ted Kremeneka49e3672008-10-30 23:14:36 +00001911 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001912
Ted Kremeneka49e3672008-10-30 23:14:36 +00001913 // Handle base case where the initializer has no elements.
1914 // e.g: static int* myArray[] = {};
1915 if (NumInitElements == 0) {
1916 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1917 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1918 return;
1919 }
1920
1921 // Create a worklist to process the initializers.
1922 llvm::SmallVector<InitListWLItem, 10> WorkList;
1923 WorkList.reserve(NumInitElements);
1924 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001925 InitListExpr::reverse_iterator ItrEnd = E->rend();
1926
Ted Kremeneka49e3672008-10-30 23:14:36 +00001927 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001928 while (!WorkList.empty()) {
1929 InitListWLItem X = WorkList.back();
1930 WorkList.pop_back();
1931
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001932 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001933 Visit(*X.Itr, X.N, Tmp);
1934
1935 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001936
Ted Kremenekf75b1862008-10-30 17:47:32 +00001937 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1938 // Get the last initializer value.
1939 state = GetState(*NI);
1940 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1941
1942 // Construct the new list of values by prepending the new value to
1943 // the already constructed list.
1944 llvm::ImmutableList<SVal> NewVals =
1945 getBasicVals().consVals(InitV, X.Vals);
1946
1947 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001948 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001949 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001950
Ted Kremenekf75b1862008-10-30 17:47:32 +00001951 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001952 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001953 }
1954 else {
1955 // Still some initializer values to go. Push them onto the worklist.
1956 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1957 }
1958 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001959 }
Ted Kremenek87903072008-10-30 18:34:31 +00001960
1961 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001962 }
1963
Ted Kremenek062e2f92008-11-13 06:10:40 +00001964 if (T->isUnionType() || T->isVectorType()) {
1965 // FIXME: to be implemented.
1966 // Note: That vectors can return true for T->isIntegerType()
1967 MakeNode(Dst, E, Pred, state);
1968 return;
1969 }
1970
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001971 if (Loc::IsLocType(T) || T->isIntegerType()) {
1972 assert (E->getNumInits() == 1);
1973 NodeSet Tmp;
1974 Expr* Init = E->getInit(0);
1975 Visit(Init, Pred, Tmp);
1976 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1977 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001978 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001979 }
1980 return;
1981 }
1982
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001983
1984 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1985 assert(0 && "unprocessed InitListExpr type");
1986}
Ted Kremenekf233d482008-02-05 00:26:40 +00001987
Sebastian Redl05189992008-11-11 17:56:53 +00001988/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1989void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1990 NodeTy* Pred,
1991 NodeSet& Dst) {
1992 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001993 uint64_t amt;
1994
1995 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001996 if (T == getContext().VoidTy) {
1997 // sizeof(void) == 1 byte.
1998 amt = 1;
1999 }
2000 else if (!T.getTypePtr()->isConstantSizeType()) {
2001 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002002 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002003 }
2004 else if (T->isObjCInterfaceType()) {
2005 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2006 // the compiler has laid out its representation. Just report Unknown
2007 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002008 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002009 }
2010 else {
2011 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002012 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002013 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002014 }
2015 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002016 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002017
Ted Kremenek0e561a32008-03-21 21:30:14 +00002018 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002019 BindExpr(GetState(Pred), Ex,
2020 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002021}
2022
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002023
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002024void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002025 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002026
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002027 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002028
2029 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002030 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002031
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002032 case UnaryOperator::Deref: {
2033
2034 Expr* Ex = U->getSubExpr()->IgnoreParens();
2035 NodeSet Tmp;
2036 Visit(Ex, Pred, Tmp);
2037
2038 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002039
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002040 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002041 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002042
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002043 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002044 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002045 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002046 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002047 }
2048
2049 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002050 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002051
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002052 case UnaryOperator::Real: {
2053
2054 Expr* Ex = U->getSubExpr()->IgnoreParens();
2055 NodeSet Tmp;
2056 Visit(Ex, Pred, Tmp);
2057
2058 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2059
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002060 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002061 if (Ex->getType()->isAnyComplexType()) {
2062 // Just report "Unknown."
2063 Dst.Add(*I);
2064 continue;
2065 }
2066
2067 // For all other types, UnaryOperator::Real is an identity operation.
2068 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002069 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002070 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002071 }
2072
2073 return;
2074 }
2075
2076 case UnaryOperator::Imag: {
2077
2078 Expr* Ex = U->getSubExpr()->IgnoreParens();
2079 NodeSet Tmp;
2080 Visit(Ex, Pred, Tmp);
2081
2082 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002083 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002084 if (Ex->getType()->isAnyComplexType()) {
2085 // Just report "Unknown."
2086 Dst.Add(*I);
2087 continue;
2088 }
2089
2090 // For all other types, UnaryOperator::Float returns 0.
2091 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002092 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002093 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002094 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002095 }
2096
2097 return;
2098 }
2099
2100 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002101 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002102 Dst.Add(Pred);
2103 return;
2104
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002105 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002106 case UnaryOperator::Extension: {
2107
2108 // Unary "+" is a no-op, similar to a parentheses. We still have places
2109 // where it may be a block-level expression, so we need to
2110 // generate an extra node that just propagates the value of the
2111 // subexpression.
2112
2113 Expr* Ex = U->getSubExpr()->IgnoreParens();
2114 NodeSet Tmp;
2115 Visit(Ex, Pred, Tmp);
2116
2117 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002118 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002119 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002120 }
2121
2122 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002123 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002124
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002125 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002126
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002127 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002128 Expr* Ex = U->getSubExpr()->IgnoreParens();
2129 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002130 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002131
2132 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002133 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002134 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002135 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002136 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002137 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002138
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002139 return;
2140 }
2141
2142 case UnaryOperator::LNot:
2143 case UnaryOperator::Minus:
2144 case UnaryOperator::Not: {
2145
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002146 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002147 Expr* Ex = U->getSubExpr()->IgnoreParens();
2148 NodeSet Tmp;
2149 Visit(Ex, Pred, Tmp);
2150
2151 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002152 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002153
2154 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002155 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002156
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002157 if (V.isUnknownOrUndef()) {
2158 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2159 continue;
2160 }
2161
Ted Kremenek60595da2008-11-15 04:01:56 +00002162// QualType DstT = getContext().getCanonicalType(U->getType());
2163// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2164//
2165// if (DstT != SrcT) // Perform promotions.
2166// V = EvalCast(V, DstT);
2167//
2168// if (V.isUnknownOrUndef()) {
2169// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2170// continue;
2171// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002172
2173 switch (U->getOpcode()) {
2174 default:
2175 assert(false && "Invalid Opcode.");
2176 break;
2177
2178 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002179 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002180 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002181 break;
2182
2183 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002184 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002185 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002186 break;
2187
2188 case UnaryOperator::LNot:
2189
2190 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2191 //
2192 // Note: technically we do "E == 0", but this is the same in the
2193 // transfer functions as "0 == E".
2194
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002195 if (isa<Loc>(V)) {
2196 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2197 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002198 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002199 }
2200 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002201 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002202#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002203 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2204 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002205#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002206 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002207 continue;
2208#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002209 }
2210
2211 break;
2212 }
2213
2214 MakeNode(Dst, U, *I, St);
2215 }
2216
2217 return;
2218 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002219 }
2220
2221 // Handle ++ and -- (both pre- and post-increment).
2222
2223 assert (U->isIncrementDecrementOp());
2224 NodeSet Tmp;
2225 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002226 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002227
2228 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2229
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002230 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002231 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002232
2233 // Perform a load.
2234 NodeSet Tmp2;
2235 EvalLoad(Tmp2, Ex, *I, St, V1);
2236
2237 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2238
2239 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002240 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002241
2242 // Propagate unknown and undefined values.
2243 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002244 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002245 continue;
2246 }
2247
Ted Kremenek443003b2008-02-21 19:29:23 +00002248 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002249
2250 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2251 : BinaryOperator::Sub;
2252
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002253 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002254 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002255
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002256 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002257 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002258 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002259 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002260}
2261
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002262void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2263 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2264}
2265
2266void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2267 AsmStmt::outputs_iterator I,
2268 AsmStmt::outputs_iterator E,
2269 NodeTy* Pred, NodeSet& Dst) {
2270 if (I == E) {
2271 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2272 return;
2273 }
2274
2275 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002276 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002277
2278 ++I;
2279
2280 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2281 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2282}
2283
2284void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2285 AsmStmt::inputs_iterator I,
2286 AsmStmt::inputs_iterator E,
2287 NodeTy* Pred, NodeSet& Dst) {
2288 if (I == E) {
2289
2290 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002291 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002292
2293 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2294 // which interprets the inline asm and stores proper results in the
2295 // outputs.
2296
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002297 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002298
2299 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2300 OE = A->end_outputs(); OI != OE; ++OI) {
2301
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002302 SVal X = GetSVal(St, *OI);
2303 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002304
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002305 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002306 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002307 }
2308
Ted Kremenek0e561a32008-03-21 21:30:14 +00002309 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002310 return;
2311 }
2312
2313 NodeSet Tmp;
2314 Visit(*I, Pred, Tmp);
2315
2316 ++I;
2317
2318 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2319 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2320}
2321
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002322void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2323 assert (Builder && "GRStmtNodeBuilder must be defined.");
2324
2325 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002326
Ted Kremenek186350f2008-04-23 20:12:28 +00002327 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2328 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002329
Ted Kremenek729a9a22008-07-17 23:15:45 +00002330 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002331
Ted Kremenekb0533962008-04-18 20:35:30 +00002332 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002333
Ted Kremenekb0533962008-04-18 20:35:30 +00002334 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002335 MakeNode(Dst, S, Pred, GetState(Pred));
2336}
2337
Ted Kremenek02737ed2008-03-31 15:02:58 +00002338void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2339
2340 Expr* R = S->getRetValue();
2341
2342 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002343 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002344 return;
2345 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002346
Ted Kremenek5917d782008-11-21 00:27:44 +00002347 NodeSet Tmp;
2348 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002349
Ted Kremenek5917d782008-11-21 00:27:44 +00002350 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2351 SVal X = GetSVal((*I)->getState(), R);
2352
2353 // Check if we return the address of a stack variable.
2354 if (isa<loc::MemRegionVal>(X)) {
2355 // Determine if the value is on the stack.
2356 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002357
Ted Kremenek5917d782008-11-21 00:27:44 +00002358 if (R && getStateManager().hasStackStorage(R)) {
2359 // Create a special node representing the error.
2360 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2361 N->markAsSink();
2362 RetsStackAddr.insert(N);
2363 }
2364 continue;
2365 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002366 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002367 // Check if we return an undefined value.
2368 else if (X.isUndef()) {
2369 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2370 N->markAsSink();
2371 RetsUndef.insert(N);
2372 }
2373 continue;
2374 }
2375
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002376 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002377 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002378}
Ted Kremenek55deb972008-03-25 00:34:37 +00002379
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002380//===----------------------------------------------------------------------===//
2381// Transfer functions: Binary operators.
2382//===----------------------------------------------------------------------===//
2383
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002384const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2385 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002386
2387 // Divide by undefined? (potentially zero)
2388
2389 if (Denom.isUndef()) {
2390 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2391
2392 if (DivUndef) {
2393 DivUndef->markAsSink();
2394 ExplicitBadDivides.insert(DivUndef);
2395 }
2396
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002397 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002398 }
2399
2400 // Check for divide/remainder-by-zero.
2401 // First, "assume" that the denominator is 0 or undefined.
2402
2403 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002404 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002405
2406 // Second, "assume" that the denominator cannot be 0.
2407
2408 bool isFeasibleNotZero = false;
2409 St = Assume(St, Denom, true, isFeasibleNotZero);
2410
2411 // Create the node for the divide-by-zero (if it occurred).
2412
2413 if (isFeasibleZero)
2414 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2415 DivZeroNode->markAsSink();
2416
2417 if (isFeasibleNotZero)
2418 ImplicitBadDivides.insert(DivZeroNode);
2419 else
2420 ExplicitBadDivides.insert(DivZeroNode);
2421
2422 }
2423
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002424 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002425}
2426
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002427void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002428 GRExprEngine::NodeTy* Pred,
2429 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002430
2431 NodeSet Tmp1;
2432 Expr* LHS = B->getLHS()->IgnoreParens();
2433 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002434
Ted Kremenek759623e2008-12-06 02:39:30 +00002435 // FIXME: Add proper support for ObjCKVCRefExpr.
2436 if (isa<ObjCKVCRefExpr>(LHS)) {
2437 Visit(RHS, Pred, Dst);
2438 return;
2439 }
2440
2441
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002442 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002443 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002444 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002445 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002446
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002447 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002448
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002449 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002450
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002451 // Process the RHS.
2452
2453 NodeSet Tmp2;
2454 Visit(RHS, *I1, Tmp2);
2455
2456 // With both the LHS and RHS evaluated, process the operation itself.
2457
2458 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002459
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002460 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002461 const GRState* OldSt = St;
2462
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002463 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002464 BinaryOperator::Opcode Op = B->getOpcode();
2465
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002466 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002467
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002468 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002469
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002470 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002471 // FIXME: Handle structs.
2472 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002473
Ted Kremenek062e2f92008-11-13 06:10:40 +00002474 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2475 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002476 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002477 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002478
Ted Kremenek062e2f92008-11-13 06:10:40 +00002479 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002480 ? cast<SVal>(loc::SymbolVal(Sym))
2481 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002482 }
2483
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002484 // Simulate the effects of a "store": bind the value of the RHS
2485 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002486
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002487 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002488 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002489 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002490
2491 case BinaryOperator::Div:
2492 case BinaryOperator::Rem:
2493
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002494 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002495 if (RHS->getType()->isIntegerType() &&
2496 RHS->getType()->isScalarType()) {
2497
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002498 St = CheckDivideZero(B, St, *I2, RightV);
2499 if (!St) continue;
2500 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002501
2502 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002503
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002504 default: {
2505
2506 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002507 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002508
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002509 // Process non-assignements except commas or short-circuited
2510 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002511
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002512 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002513
2514 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002515 if (OldSt != St) {
2516 // Generate a new node if we have already created a new state.
2517 MakeNode(Dst, B, *I2, St);
2518 }
2519 else
2520 Dst.Add(*I2);
2521
Ted Kremenek89063af2008-02-21 19:15:37 +00002522 continue;
2523 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002524
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002525 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002526
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002527 // The operands were *not* undefined, but the result is undefined.
2528 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002529
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002530 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002531 UndefNode->markAsSink();
2532 UndefResults.insert(UndefNode);
2533 }
2534
2535 continue;
2536 }
2537
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002538 // Otherwise, create a new node.
2539
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002540 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002541 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002542 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002543 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002544
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002545 assert (B->isCompoundAssignmentOp());
2546
Ted Kremenek934e3e92008-10-27 23:02:39 +00002547 if (Op >= BinaryOperator::AndAssign) {
2548 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2549 BinaryOperator::And));
2550 }
2551 else {
2552 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2553 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002554
2555 // Perform a load (the LHS). This performs the checks for
2556 // null dereferences, and so on.
2557 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002558 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002559 EvalLoad(Tmp3, LHS, *I2, St, location);
2560
2561 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2562
2563 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002564 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002565
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002566 // Check for divide-by-zero.
2567 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002568 && RHS->getType()->isIntegerType()
2569 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002570
2571 // CheckDivideZero returns a new state where the denominator
2572 // is assumed to be non-zero.
2573 St = CheckDivideZero(B, St, *I3, RightV);
2574
2575 if (!St)
2576 continue;
2577 }
2578
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002579 // Propagate undefined values (left-side).
2580 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002581 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002582 continue;
2583 }
2584
2585 // Propagate unknown values (left and right-side).
2586 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002587 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002588 UnknownVal());
2589 continue;
2590 }
2591
2592 // At this point:
2593 //
2594 // The LHS is not Undef/Unknown.
2595 // The RHS is not Unknown.
2596
2597 // Get the computation type.
2598 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002599 CTy = getContext().getCanonicalType(CTy);
2600
2601 QualType LTy = getContext().getCanonicalType(LHS->getType());
2602 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002603
2604 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002605 if (LTy != CTy) V = EvalCast(V, CTy);
2606 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002607
2608 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002609 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002610 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002611 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002612 continue;
2613 }
2614
Ted Kremenek60595da2008-11-15 04:01:56 +00002615 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002616 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002617
2618 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002619 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002620 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2621 UndefNode->markAsSink();
2622 UndefResults.insert(UndefNode);
2623 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002624 continue;
2625 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002626
2627 // EXPERIMENTAL: "Conjured" symbols.
2628 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002629
2630 SVal LHSVal;
2631
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002632 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2633 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002634
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002635 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002636
Ted Kremenek60595da2008-11-15 04:01:56 +00002637 // The symbolic value is actually for the type of the left-hand side
2638 // expression, not the computation type, as this is the value the
2639 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002640 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002641 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002642 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002643 : cast<SVal>(nonloc::SymbolVal(Sym));
2644
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002645 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002646 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002647 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002648 else {
2649 // The left-hand side may bind to a different value then the
2650 // computation type.
2651 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2652 }
2653
2654 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002655 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002656 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002657 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002658}
Ted Kremenekee985462008-01-16 18:18:48 +00002659
2660//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002661// Transfer-function Helpers.
2662//===----------------------------------------------------------------------===//
2663
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002664void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002665 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002666 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002667 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002668
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002669 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002670 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2671
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002672 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002673 MakeNode(Dst, Ex, Pred, *I);
2674}
2675
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002676void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002677 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002678 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002679
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002680 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002681 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002682}
2683
2684//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002685// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002686//===----------------------------------------------------------------------===//
2687
Ted Kremenekaa66a322008-01-16 21:46:15 +00002688#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002689static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002690static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002691
Ted Kremenekaa66a322008-01-16 21:46:15 +00002692namespace llvm {
2693template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002694struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002695 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002696
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002697 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2698
2699 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002700 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002701 GraphPrintCheckerState->isUndefDeref(N) ||
2702 GraphPrintCheckerState->isUndefStore(N) ||
2703 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002704 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2705 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002706 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002707 GraphPrintCheckerState->isBadCall(N) ||
2708 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002709 return "color=\"red\",style=\"filled\"";
2710
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002711 if (GraphPrintCheckerState->isNoReturnCall(N))
2712 return "color=\"blue\",style=\"filled\"";
2713
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002714 return "";
2715 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002716
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002717 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002718 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002719
2720 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002721 ProgramPoint Loc = N->getLocation();
2722
2723 switch (Loc.getKind()) {
2724 case ProgramPoint::BlockEntranceKind:
2725 Out << "Block Entrance: B"
2726 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2727 break;
2728
2729 case ProgramPoint::BlockExitKind:
2730 assert (false);
2731 break;
2732
Ted Kremenekaa66a322008-01-16 21:46:15 +00002733 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002734 if (isa<PostStmt>(Loc)) {
2735 const PostStmt& L = cast<PostStmt>(Loc);
2736 Stmt* S = L.getStmt();
2737 SourceLocation SLoc = S->getLocStart();
2738
2739 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2740 llvm::raw_os_ostream OutS(Out);
2741 S->printPretty(OutS);
2742 OutS.flush();
2743
2744 if (SLoc.isFileID()) {
2745 Out << "\\lline="
2746 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2747 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2748 }
2749
2750 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2751 Out << "\\|Implicit-Null Dereference.\\l";
2752 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2753 Out << "\\|Explicit-Null Dereference.\\l";
2754 else if (GraphPrintCheckerState->isUndefDeref(N))
2755 Out << "\\|Dereference of undefialied value.\\l";
2756 else if (GraphPrintCheckerState->isUndefStore(N))
2757 Out << "\\|Store to Undefined Loc.";
2758 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2759 Out << "\\|Explicit divide-by zero or undefined value.";
2760 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2761 Out << "\\|Implicit divide-by zero or undefined value.";
2762 else if (GraphPrintCheckerState->isUndefResult(N))
2763 Out << "\\|Result of operation is undefined.";
2764 else if (GraphPrintCheckerState->isNoReturnCall(N))
2765 Out << "\\|Call to function marked \"noreturn\".";
2766 else if (GraphPrintCheckerState->isBadCall(N))
2767 Out << "\\|Call to NULL/Undefined.";
2768 else if (GraphPrintCheckerState->isUndefArg(N))
2769 Out << "\\|Argument in call is undefined";
2770
2771 break;
2772 }
2773
Ted Kremenekaa66a322008-01-16 21:46:15 +00002774 const BlockEdge& E = cast<BlockEdge>(Loc);
2775 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2776 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002777
2778 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002779
2780 SourceLocation SLoc = T->getLocStart();
2781
Ted Kremenekb38911f2008-01-30 23:03:39 +00002782 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002783
Ted Kremeneka95d3752008-09-13 05:16:45 +00002784 llvm::raw_os_ostream OutS(Out);
2785 E.getSrc()->printTerminator(OutS);
2786 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002787
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002788 if (SLoc.isFileID()) {
2789 Out << "\\lline="
2790 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2791 << GraphPrintSourceManager->getColumnNumber(SLoc);
2792 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002793
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002794 if (isa<SwitchStmt>(T)) {
2795 Stmt* Label = E.getDst()->getLabel();
2796
2797 if (Label) {
2798 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2799 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002800 llvm::raw_os_ostream OutS(Out);
2801 C->getLHS()->printPretty(OutS);
2802 OutS.flush();
2803
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002804 if (Stmt* RHS = C->getRHS()) {
2805 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002806 RHS->printPretty(OutS);
2807 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002808 }
2809
2810 Out << ":";
2811 }
2812 else {
2813 assert (isa<DefaultStmt>(Label));
2814 Out << "\\ldefault:";
2815 }
2816 }
2817 else
2818 Out << "\\l(implicit) default:";
2819 }
2820 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002821 // FIXME
2822 }
2823 else {
2824 Out << "\\lCondition: ";
2825 if (*E.getSrc()->succ_begin() == E.getDst())
2826 Out << "true";
2827 else
2828 Out << "false";
2829 }
2830
2831 Out << "\\l";
2832 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002833
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002834 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2835 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002836 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002837 }
2838 }
2839
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002840 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002841
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002842 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2843 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002844
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002845 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002846 return Out.str();
2847 }
2848};
2849} // end llvm namespace
2850#endif
2851
Ted Kremenekffe0f432008-03-07 22:58:01 +00002852#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002853
2854template <typename ITERATOR>
2855GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2856
2857template <>
2858GRExprEngine::NodeTy*
2859GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2860 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2861 return I->first;
2862}
2863
Ted Kremenekffe0f432008-03-07 22:58:01 +00002864template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002865static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002866 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002867
Ted Kremenekd4527582008-09-16 18:44:52 +00002868 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002869
2870 for ( ; I != E; ++I ) {
2871 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002872 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002873
Ted Kremenekd4527582008-09-16 18:44:52 +00002874 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002875 continue;
2876
Ted Kremenekd4527582008-09-16 18:44:52 +00002877 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002878 Sources.push_back(N);
2879 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002880}
2881#endif
2882
2883void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002884#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002885 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002886 std::vector<NodeTy*> Src;
2887
2888 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002889 AddSources(Src, null_derefs_begin(), null_derefs_end());
2890 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2891 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2892 AddSources(Src, undef_results_begin(), undef_results_end());
2893 AddSources(Src, bad_calls_begin(), bad_calls_end());
2894 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002895 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002896
Ted Kremenekcb612922008-04-18 19:23:43 +00002897 // The new way.
2898 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2899 (*I)->GetErrorNodes(Src);
2900
2901
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002902 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002903 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002904 else {
2905 GraphPrintCheckerState = this;
2906 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002907
Ted Kremenekffe0f432008-03-07 22:58:01 +00002908 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002909
2910 GraphPrintCheckerState = NULL;
2911 GraphPrintSourceManager = NULL;
2912 }
2913#endif
2914}
2915
2916void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2917#ifndef NDEBUG
2918 GraphPrintCheckerState = this;
2919 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002920
Ted Kremenek493d7a22008-03-11 18:25:33 +00002921 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2922
2923 if (!TrimmedG)
2924 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2925 else {
2926 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2927 delete TrimmedG;
2928 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002929
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002930 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002931 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002932#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002933}