blob: 424229019ec282d0b95a072385d6de32772fae5d [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,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000118 LiveVariables& L,
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),
128 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Ted Kremenek8b233612008-07-02 20:13:38 +0000129 RaiseSel(GetNullarySelector("raise", G.getContext())) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000130
Ted Kremenek1a654b62008-06-20 21:45:25 +0000131GRExprEngine::~GRExprEngine() {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132 for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
133 delete *I;
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000134
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135
136 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000137}
138
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000139//===----------------------------------------------------------------------===//
140// Utility methods.
141//===----------------------------------------------------------------------===//
142
143// SaveAndRestore - A utility class that uses RIIA to save and restore
144// the value of a variable.
145template<typename T>
146struct VISIBILITY_HIDDEN SaveAndRestore {
147 SaveAndRestore(T& x) : X(x), old_value(x) {}
148 ~SaveAndRestore() { X = old_value; }
149 T get() { return old_value; }
150
151 T& X;
152 T old_value;
153};
154
Ted Kremenek186350f2008-04-23 20:12:28 +0000155// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
156// value of a variable is saved, and during the dstor the old value is
157// or'ed with the new value.
158struct VISIBILITY_HIDDEN SaveOr {
159 SaveOr(bool& x) : X(x), old_value(x) { x = false; }
160 ~SaveOr() { X |= old_value; }
161
162 bool& X;
163 bool old_value;
164};
165
166
Ted Kremenekc0959972008-07-02 21:24:01 +0000167void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000168 for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
Ted Kremenekc0959972008-07-02 21:24:01 +0000169 GRBugReporter BR(BRData, *this);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000170 (*I)->EmitWarnings(BR);
171 }
172
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000173 if (BatchAuditor) {
Ted Kremenekc0959972008-07-02 21:24:01 +0000174 GRBugReporter BR(BRData, *this);
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000175 BatchAuditor->EmitWarnings(BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000176 }
177}
178
179void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000180 StateMgr.TF = tf;
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181 tf->RegisterChecks(*this);
182 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000183}
184
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000185void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
186 if (!BatchAuditor)
187 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
188
189 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000190}
191
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000193 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000194}
195
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000196//===----------------------------------------------------------------------===//
197// Top-level transfer function logic (Dispatcher).
198//===----------------------------------------------------------------------===//
199
200void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
201
202 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000203 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000204
205 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000206 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000207 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000208
209 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000210 if (BatchAuditor)
211 Builder->setAuditor(BatchAuditor.get());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000212
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000213 // Create the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000214 CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
215 Liveness, DeadSymbols);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000216
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000217 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 if (DeadSymbols.empty())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000221 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000222 else {
223 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 SaveOr OldHasGen(Builder->HasGeneratedNode);
225
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000226 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
227 Builder->PurgingDeadSymbols = true;
228
Ted Kremenek729a9a22008-07-17 23:15:45 +0000229 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek910e9992008-04-25 01:25:15 +0000230 CleanedState, DeadSymbols);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000231
232 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
233 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000234 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000235
236 bool HasAutoGenerated = false;
237
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000238 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000239
240 NodeSet Dst;
241
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000242 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000243 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Visit(S, *I, Dst);
247
248 // Do we need to auto-generate a node? We only need to do this to generate
249 // a node with a "cleaned" state; GRCoreEngine will actually handle
250 // auto-transitions for other cases.
251 if (Dst.size() == 1 && *Dst.begin() == EntryNode
252 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
253 HasAutoGenerated = true;
254 builder.generateNode(S, GetState(EntryNode), *I);
255 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000256 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000257
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000260 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000261
262 // FIXME: Consolidate.
263 StateMgr.CurrentStmt = 0;
264 CurrentStmt = 0;
265
Ted Kremenek846d4e92008-04-24 23:35:58 +0000266 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000267}
268
269void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
270
271 // FIXME: add metadata to the CFG so that we can disable
272 // this check when we KNOW that there is no block-level subexpression.
273 // The motivation is that this check requires a hashtable lookup.
274
275 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
276 Dst.Add(Pred);
277 return;
278 }
279
280 switch (S->getStmtClass()) {
281
282 default:
283 // Cases we intentionally have "default" handle:
284 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
285
286 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
287 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000288
289 case Stmt::ArraySubscriptExprClass:
290 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
291 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000292
293 case Stmt::AsmStmtClass:
294 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
295 break;
296
297 case Stmt::BinaryOperatorClass: {
298 BinaryOperator* B = cast<BinaryOperator>(S);
299
300 if (B->isLogicalOp()) {
301 VisitLogicalExpr(B, Pred, Dst);
302 break;
303 }
304 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000305 const GRState* St = GetState(Pred);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000306 MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000307 break;
308 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000309
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000310 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
311 break;
312 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000313
Douglas Gregorb4609802008-11-14 16:09:21 +0000314 case Stmt::CallExprClass:
315 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000316 CallExpr* C = cast<CallExpr>(S);
317 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000318 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000319 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000320
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000321 // FIXME: ChooseExpr is really a constant. We need to fix
322 // the CFG do not model them as explicit control-flow.
323
324 case Stmt::ChooseExprClass: { // __builtin_choose_expr
325 ChooseExpr* C = cast<ChooseExpr>(S);
326 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
327 break;
328 }
329
330 case Stmt::CompoundAssignOperatorClass:
331 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
332 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000333
334 case Stmt::CompoundLiteralExprClass:
335 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
336 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000337
338 case Stmt::ConditionalOperatorClass: { // '?' operator
339 ConditionalOperator* C = cast<ConditionalOperator>(S);
340 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
341 break;
342 }
343
344 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000345 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000346 break;
347
348 case Stmt::DeclStmtClass:
349 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
350 break;
351
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000352 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000353 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000354 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000355 VisitCast(C, C->getSubExpr(), Pred, Dst);
356 break;
357 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000358
359 case Stmt::InitListExprClass:
360 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
361 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000362
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000363 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000364 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
365 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000366
367 case Stmt::ObjCIvarRefExprClass:
368 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
369 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000370
371 case Stmt::ObjCForCollectionStmtClass:
372 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
373 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000374
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000375 case Stmt::ObjCMessageExprClass: {
376 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
377 break;
378 }
379
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000380 case Stmt::ObjCAtThrowStmtClass: {
381 // FIXME: This is not complete. We basically treat @throw as
382 // an abort.
383 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
384 Builder->BuildSinks = true;
385 MakeNode(Dst, S, Pred, GetState(Pred));
386 break;
387 }
388
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000389 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000390 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000391 break;
392
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000393 case Stmt::ReturnStmtClass:
394 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
395 break;
396
Sebastian Redl05189992008-11-11 17:56:53 +0000397 case Stmt::SizeOfAlignOfExprClass:
398 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000399 break;
400
401 case Stmt::StmtExprClass: {
402 StmtExpr* SE = cast<StmtExpr>(S);
403
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000404 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405
406 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
407 // probably just remove these from the CFG.
408 assert (!SE->getSubStmt()->body_empty());
409
410 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000411 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000412 else
413 Dst.Add(Pred);
414
415 break;
416 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000417
418 case Stmt::StringLiteralClass:
419 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
420 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000421
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000422 case Stmt::UnaryOperatorClass:
423 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000424 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000425 }
426}
427
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000428void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000429
430 Ex = Ex->IgnoreParens();
431
432 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
433 Dst.Add(Pred);
434 return;
435 }
436
437 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000438
439 case Stmt::ArraySubscriptExprClass:
440 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
441 return;
442
443 case Stmt::DeclRefExprClass:
444 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
445 return;
446
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000447 case Stmt::ObjCIvarRefExprClass:
448 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
449 return;
450
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 case Stmt::UnaryOperatorClass:
452 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
453 return;
454
455 case Stmt::MemberExprClass:
456 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
457 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000458
Ted Kremenek4f090272008-10-27 21:54:31 +0000459 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000460 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000461 return;
462
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000463 case Stmt::ObjCPropertyRefExprClass:
464 // FIXME: Property assignments are lvalues, but not really "locations".
465 // e.g.: self.x = something;
466 // Here the "self.x" really can translate to a method call (setter) when
467 // the assignment is made. Moreover, the entire assignment expression
468 // evaluate to whatever "something" is, not calling the "getter" for
469 // the property (which would make sense since it can have side effects).
470 // We'll probably treat this as a location, but not one that we can
471 // take the address of. Perhaps we need a new SVal class for cases
472 // like thsis?
473 // Note that we have a similar problem for bitfields, since they don't
474 // have "locations" in the sense that we can take their address.
475 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000476 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000477
478 case Stmt::StringLiteralClass: {
479 const GRState* St = GetState(Pred);
480 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000481 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000482 return;
483 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000484
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000485 default:
486 // Arbitrary subexpressions can return aggregate temporaries that
487 // can be used in a lvalue context. We need to enhance our support
488 // of such temporaries in both the environment and the store, so right
489 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000490 assert ((Ex->getType()->isAggregateType() ||
491 Ex->getType()->isUnionType()) &&
492 "Other kinds of expressions with non-aggregate/union types do"
493 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000494
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000495 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000496 }
497}
498
499//===----------------------------------------------------------------------===//
500// Block entrance. (Update counters).
501//===----------------------------------------------------------------------===//
502
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000503bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000504 GRBlockCounter BC) {
505
506 return BC.getNumVisited(B->getBlockID()) < 3;
507}
508
509//===----------------------------------------------------------------------===//
510// Branch processing.
511//===----------------------------------------------------------------------===//
512
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000513const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000514 Stmt* Terminator,
515 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000516
517 switch (Terminator->getStmtClass()) {
518 default:
519 return St;
520
521 case Stmt::BinaryOperatorClass: { // '&&' and '||'
522
523 BinaryOperator* B = cast<BinaryOperator>(Terminator);
524 BinaryOperator::Opcode Op = B->getOpcode();
525
526 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
527
528 // For &&, if we take the true branch, then the value of the whole
529 // expression is that of the RHS expression.
530 //
531 // For ||, if we take the false branch, then the value of the whole
532 // expression is that of the RHS expression.
533
534 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
535 (Op == BinaryOperator::LOr && !branchTaken)
536 ? B->getRHS() : B->getLHS();
537
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000538 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000539 }
540
541 case Stmt::ConditionalOperatorClass: { // ?:
542
543 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
544
545 // For ?, if branchTaken == true then the value is either the LHS or
546 // the condition itself. (GNU extension).
547
548 Expr* Ex;
549
550 if (branchTaken)
551 Ex = C->getLHS() ? C->getLHS() : C->getCond();
552 else
553 Ex = C->getRHS();
554
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000555 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000556 }
557
558 case Stmt::ChooseExprClass: { // ?:
559
560 ChooseExpr* C = cast<ChooseExpr>(Terminator);
561
562 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000563 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000564 }
565 }
566}
567
Ted Kremenekaf337412008-11-12 19:24:17 +0000568void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000569 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000570
Ted Kremeneke7d22112008-02-11 19:21:59 +0000571 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000572 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000573 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000574
Ted Kremenekb2331832008-02-15 22:29:00 +0000575 // Check for NULL conditions; e.g. "for(;;)"
576 if (!Condition) {
577 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000578 return;
579 }
580
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000581 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000582
583 switch (V.getBaseKind()) {
584 default:
585 break;
586
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000587 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000588 builder.generateNode(MarkBranch(PrevState, Term, true), true);
589 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000590 return;
591
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000592 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000593 NodeTy* N = builder.generateNode(PrevState, true);
594
595 if (N) {
596 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000597 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000598 }
599
600 builder.markInfeasible(false);
601 return;
602 }
603 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000604
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000605 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000606
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000607 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000608 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000609
610 if (isFeasible)
611 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000612 else
613 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000614
615 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000616
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000617 isFeasible = false;
618 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000619
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000620 if (isFeasible)
621 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000622 else
623 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000624}
625
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000626/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000627/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000628void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000629
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000630 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000631 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000632
633 // Three possibilities:
634 //
635 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000636 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000637 // (3) We have no clue about the label. Dispatch to all targets.
638 //
639
640 typedef IndirectGotoNodeBuilder::iterator iterator;
641
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000642 if (isa<loc::GotoLabel>(V)) {
643 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000644
645 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000646 if (I.getLabel() == L) {
647 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000648 return;
649 }
650 }
651
652 assert (false && "No block with label.");
653 return;
654 }
655
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000656 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000657 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000658 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000659 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000660 return;
661 }
662
663 // This is really a catch-all. We don't support symbolics yet.
664
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000665 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000666
667 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000668 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000669}
Ted Kremenekf233d482008-02-05 00:26:40 +0000670
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000671
672void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
673 NodeTy* Pred, NodeSet& Dst) {
674
675 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
676
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000677 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000678 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000679
680 assert (X.isUndef());
681
682 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
683
684 assert (SE);
685
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000686 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000687
688 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000689 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000690}
691
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000692/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
693/// nodes by processing the 'effects' of a switch statement.
694void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
695
696 typedef SwitchNodeBuilder::iterator iterator;
697
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000698 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000699 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000700 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000701
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000702 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000703 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000704 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000705 return;
706 }
707
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000708 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000709
710 // While most of this can be assumed (such as the signedness), having it
711 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000712
Chris Lattner98be4942008-03-05 18:54:05 +0000713 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000714
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000715 APSInt V1(bits, false);
716 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000717 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000718
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000719 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000720
721 CaseStmt* Case = cast<CaseStmt>(I.getCase());
722
723 // Evaluate the case.
724 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
725 assert (false && "Case condition must evaluate to an integer constant.");
726 return;
727 }
728
729 // Get the RHS of the case, if it exists.
730
731 if (Expr* E = Case->getRHS()) {
732 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
733 assert (false &&
734 "Case condition (RHS) must evaluate to an integer constant.");
735 return ;
736 }
737
738 assert (V1 <= V2);
739 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000740 else
741 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000742
743 // FIXME: Eventually we should replace the logic below with a range
744 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000745 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000746
Ted Kremenek14a11402008-03-17 22:17:56 +0000747 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000748 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000749
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000750 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751
752 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000753
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000754 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000755 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000756
757 if (isFeasible) {
758 builder.generateCaseStmtNode(I, StNew);
759
760 // If CondV evaluates to a constant, then we know that this
761 // is the *only* case that we can take, so stop evaluating the
762 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000763 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000764 return;
765 }
766
767 // Now "assume" that the case doesn't match. Add this state
768 // to the default state (if it is feasible).
769
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000770 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000771 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000772
Ted Kremenek5014ab12008-04-23 05:03:18 +0000773 if (isFeasible) {
774 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000775 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000776 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777
Ted Kremenek14a11402008-03-17 22:17:56 +0000778 // Concretize the next value in the range.
779 if (V1 == V2)
780 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000781
Ted Kremenek14a11402008-03-17 22:17:56 +0000782 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000783 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000784
785 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000786 }
787
788 // If we reach here, than we know that the default branch is
789 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000790 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000791}
792
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000793//===----------------------------------------------------------------------===//
794// Transfer functions: logical operations ('&&', '||').
795//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000796
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000797void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000798 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000799
Ted Kremenek05a23782008-02-26 19:05:15 +0000800 assert (B->getOpcode() == BinaryOperator::LAnd ||
801 B->getOpcode() == BinaryOperator::LOr);
802
803 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
804
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000805 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000806 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000807
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000808 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000809
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000810 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000811
812 assert (Ex);
813
814 if (Ex == B->getRHS()) {
815
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000816 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000817
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000818 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000819
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000820 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000821 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000822 return;
823 }
824
Ted Kremenek05a23782008-02-26 19:05:15 +0000825 // We took the RHS. Because the value of the '&&' or '||' expression must
826 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
827 // or 1. Alternatively, we could take a lazy approach, and calculate this
828 // value later when necessary. We don't have the machinery in place for
829 // this right now, and since most logical expressions are used for branches,
830 // the payoff is not likely to be large. Instead, we do eager evaluation.
831
832 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000833 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000834
835 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000836 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000837 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000838
839 isFeasible = false;
840 NewState = Assume(St, X, false, isFeasible);
841
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(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000845 }
846 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000847 // We took the LHS expression. Depending on whether we are '&&' or
848 // '||' we know what the value of the expression is via properties of
849 // the short-circuiting.
850
851 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000852 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000853 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000854}
Ted Kremenek05a23782008-02-26 19:05:15 +0000855
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000856//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000857// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000858//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000859
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000860void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
861 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000862
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000863 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000864
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000865 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000866
867 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
868
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000869 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000870
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000871 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000872 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000873 else
874 EvalLoad(Dst, Ex, Pred, St, V);
875 return;
876
877 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
878 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
879
880 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000881 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000882 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000883 return;
884
885 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000886 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000887 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000888 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000889 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000890 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000891
892 assert (false &&
893 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000894}
895
Ted Kremenek540cbe22008-04-22 04:56:29 +0000896/// VisitArraySubscriptExpr - Transfer function for array accesses
897void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000898 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000899
900 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000901 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000902 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000903 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000904
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000905 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000906 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000907 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000908
909 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000910 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000911 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000912
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000913 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000914 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000915 else
916 EvalLoad(Dst, A, *I2, St, V);
917 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000918 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000919}
920
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000921/// VisitMemberExpr - Transfer function for member expressions.
922void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000923 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000924
925 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000926 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000927
928 if (M->isArrow())
929 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
930 else
931 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
932
Douglas Gregor86f19402008-12-20 23:49:58 +0000933 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
934 if (!Field) // FIXME: skipping member expressions for non-fields
935 return;
936
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000937 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000938 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000939 // FIXME: Should we insert some assumption logic in here to determine
940 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000941 // later when using FieldOffset lvals (which we no longer have).
942 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000943
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000944 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000945 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000946 else
947 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000948 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000949}
950
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000951void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000952 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000953
954 assert (Builder && "GRStmtNodeBuilder must be defined.");
955
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000956 // Evaluate the location (checks for bad dereferences).
Ted Kremenek8c354752008-12-16 22:02:27 +0000957 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000958
Ted Kremenek8c354752008-12-16 22:02:27 +0000959 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960 return;
961
Ted Kremenek8c354752008-12-16 22:02:27 +0000962 St = GetState(Pred);
963
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000964 // Proceed with the store.
965
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000966 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000967
Ted Kremenek186350f2008-04-23 20:12:28 +0000968 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000969 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000970 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000971
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000972 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000973 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000974
Ted Kremenek729a9a22008-07-17 23:15:45 +0000975 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000976
977 // Handle the case where no nodes where generated. Auto-generate that
978 // contains the updated state if we aren't generating sinks.
979
Ted Kremenekb0533962008-04-18 20:35:30 +0000980 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000981 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000982 location, Val);
983}
984
985void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000986 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000987 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000988
Ted Kremenek8c354752008-12-16 22:02:27 +0000989 // Evaluate the location (checks for bad dereferences).
990 Pred = EvalLocation(Ex, Pred, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000991
Ted Kremenek8c354752008-12-16 22:02:27 +0000992 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000993 return;
994
Ted Kremenek8c354752008-12-16 22:02:27 +0000995 St = GetState(Pred);
996
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000997 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000998 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000999
1000 // FIXME: Currently symbolic analysis "generates" new symbols
1001 // for the contents of values. We need a better approach.
1002
1003 // FIXME: The "CheckOnly" option exists only because Array and Field
1004 // loads aren't fully implemented. Eventually this option will go away.
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001005 assert(!CheckOnly);
Ted Kremenek982e6742008-08-28 18:43:46 +00001006
Ted Kremenek8c354752008-12-16 22:02:27 +00001007 if (CheckOnly) {
1008 Dst.Add(Pred);
1009 return;
1010 }
1011
1012 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001013 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001014 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001015 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001016 else {
1017 SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
1018 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
1019 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001020}
1021
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001022void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001023 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001024
1025 NodeSet TmpDst;
1026 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1027
1028 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1029 MakeNode(Dst, Ex, *I, (*I)->getState());
1030}
1031
Ted Kremenek8c354752008-12-16 22:02:27 +00001032GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
1033 const GRState* St,
1034 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001035
1036 // Check for loads/stores from/to undefined values.
1037 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001038 NodeTy* N =
1039 Builder->generateNode(Ex, St, Pred,
1040 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001041
Ted Kremenek8c354752008-12-16 22:02:27 +00001042 if (N) {
1043 N->markAsSink();
1044 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001045 }
1046
Ted Kremenek8c354752008-12-16 22:02:27 +00001047 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001048 }
1049
1050 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1051 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001052 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001053
1054 // During a load, one of two possible situations arise:
1055 // (1) A crash, because the location (pointer) was NULL.
1056 // (2) The location (pointer) is not NULL, and the dereference works.
1057 //
1058 // We add these assumptions.
1059
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001060 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001061
1062 // "Assume" that the pointer is not NULL.
1063
1064 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001065 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001066
1067 // "Assume" that the pointer is NULL.
1068
1069 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001070 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1071 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001072
1073 if (isFeasibleNull) {
1074
Ted Kremenek7360fda2008-09-18 23:09:54 +00001075 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001076 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001077 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1078
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001079 // We don't use "MakeNode" here because the node will be a sink
1080 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001081 NodeTy* NullNode =
1082 Builder->generateNode(Ex, StNull, Pred,
1083 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001084
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001085 if (NullNode) {
1086
1087 NullNode->markAsSink();
1088
1089 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1090 else ExplicitNullDeref.insert(NullNode);
1091 }
1092 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001093
1094 if (!isFeasibleNotNull)
1095 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001096
1097 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001098 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001099 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1100 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1101 // Get the index of the accessed element.
1102 SVal Idx = ER->getIndex();
1103 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001104 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1105 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001106
1107 bool isFeasibleInBound = false;
1108 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1109 true, isFeasibleInBound);
1110
1111 bool isFeasibleOutBound = false;
1112 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1113 false, isFeasibleOutBound);
1114
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001115 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001116 // Report warning. Make sink node manually.
1117 NodeTy* OOBNode =
1118 Builder->generateNode(Ex, StOutBound, Pred,
1119 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001120
1121 if (OOBNode) {
1122 OOBNode->markAsSink();
1123
1124 if (isFeasibleInBound)
1125 ImplicitOOBMemAccesses.insert(OOBNode);
1126 else
1127 ExplicitOOBMemAccesses.insert(OOBNode);
1128 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001129 }
1130
Ted Kremenek8c354752008-12-16 22:02:27 +00001131 if (!isFeasibleInBound)
1132 return 0;
1133
1134 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001135 }
1136 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001137
Ted Kremenek8c354752008-12-16 22:02:27 +00001138 // Generate a new node indicating the checks succeed.
1139 return Builder->generateNode(Ex, StNotNull, Pred,
1140 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001141}
1142
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001143//===----------------------------------------------------------------------===//
1144// Transfer function: Function calls.
1145//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001146void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001147 CallExpr::arg_iterator AI,
1148 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001149 NodeSet& Dst)
1150{
1151 // Determine the type of function we're calling (if available).
1152 const FunctionTypeProto *Proto = NULL;
1153 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1154 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1155 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1156
1157 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1158}
1159
1160void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1161 CallExpr::arg_iterator AI,
1162 CallExpr::arg_iterator AE,
1163 NodeSet& Dst, const FunctionTypeProto *Proto,
1164 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001165
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001166 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001167 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001168 // If the call argument is being bound to a reference parameter,
1169 // visit it as an lvalue, not an rvalue.
1170 bool VisitAsLvalue = false;
1171 if (Proto && ParamIdx < Proto->getNumArgs())
1172 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1173
1174 NodeSet DstTmp;
1175 if (VisitAsLvalue)
1176 VisitLValue(*AI, Pred, DstTmp);
1177 else
1178 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001179 ++AI;
1180
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001181 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001182 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001183
1184 return;
1185 }
1186
1187 // If we reach here we have processed all of the arguments. Evaluate
1188 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001189
Ted Kremenek994a09b2008-02-25 21:16:03 +00001190 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001191 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001192
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001193 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001194
Ted Kremenekde434242008-02-19 01:44:53 +00001195 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001196 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1197
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001198 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001199 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001200
Ted Kremeneka1354a52008-03-03 16:47:31 +00001201 // FIXME: Add support for symbolic function calls (calls involving
1202 // function pointer values that are symbolic).
1203
1204 // Check for undefined control-flow or calls to NULL.
1205
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001206 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001207 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001208
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001209 if (N) {
1210 N->markAsSink();
1211 BadCalls.insert(N);
1212 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001213
Ted Kremenekde434242008-02-19 01:44:53 +00001214 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001215 }
1216
1217 // Check for the "noreturn" attribute.
1218
1219 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1220
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001221 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001222
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001223 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001224
1225 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001226 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001227 else {
1228 // HACK: Some functions are not marked noreturn, and don't return.
1229 // Here are a few hardwired ones. If this takes too long, we can
1230 // potentially cache these results.
1231 const char* s = FD->getIdentifier()->getName();
1232 unsigned n = strlen(s);
1233
1234 switch (n) {
1235 default:
1236 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001237
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001238 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001239 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1240 break;
1241
1242 case 5:
1243 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001244 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001245 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001246 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001247 // FIXME: use Assume to inspect the possible symbolic value of
1248 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001249 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001250 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001251 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001252 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001253 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001254 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001255
1256 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001257 if (!memcmp(s, "Assert", 6)) {
1258 Builder->BuildSinks = true;
1259 break;
1260 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001261
1262 // FIXME: This is just a wrapper around throwing an exception.
1263 // Eventually inter-procedural analysis should handle this easily.
1264 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1265
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001266 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001267
1268 case 7:
1269 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1270 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001271
Ted Kremenekf47bb782008-04-30 17:54:04 +00001272 case 8:
1273 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1274 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001275
1276 case 12:
1277 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1278 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001279
Ted Kremenekf9683082008-09-19 02:30:47 +00001280 case 13:
1281 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1282 break;
1283
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001284 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001285 if (!memcmp(s, "dtrace_assfail", 14) ||
1286 !memcmp(s, "yy_fatal_error", 14))
1287 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001288 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001289
1290 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001291 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1292 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001293 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001294
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001295 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001296 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001297
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001298 }
1299 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001300
1301 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001302
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001303 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001304
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001305 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001306
Ted Kremenek186350f2008-04-23 20:12:28 +00001307 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001308 switch (id) {
1309 case Builtin::BI__builtin_expect: {
1310 // For __builtin_expect, just return the value of the subexpression.
1311 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001312 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001313 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001314 continue;
1315 }
1316
Ted Kremenekb3021332008-11-02 00:35:01 +00001317 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001318 // FIXME: Refactor into StoreManager itself?
1319 MemRegionManager& RM = getStateManager().getRegionManager();
1320 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001321 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001322
1323 // Set the extent of the region in bytes. This enables us to use the
1324 // SVal of the argument directly. If we save the extent in bits, we
1325 // cannot represent values like symbol*8.
1326 SVal Extent = GetSVal(St, *(CE->arg_begin()));
1327 St = getStoreManager().setExtent(St, R, Extent);
1328
Ted Kremenekb3021332008-11-02 00:35:01 +00001329 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1330 continue;
1331 }
1332
Ted Kremenek55aea312008-03-05 22:59:42 +00001333 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001334 break;
1335 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001336 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001337
Ted Kremenek186350f2008-04-23 20:12:28 +00001338 // Check any arguments passed-by-value against being undefined.
1339
1340 bool badArg = false;
1341
1342 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1343 I != E; ++I) {
1344
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001345 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001346 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001347
Ted Kremenek186350f2008-04-23 20:12:28 +00001348 if (N) {
1349 N->markAsSink();
1350 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001351 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001352
Ted Kremenek186350f2008-04-23 20:12:28 +00001353 badArg = true;
1354 break;
1355 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001356 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001357
1358 if (badArg)
1359 continue;
1360
1361 // Dispatch to the plug-in transfer function.
1362
1363 unsigned size = Dst.size();
1364 SaveOr OldHasGen(Builder->HasGeneratedNode);
1365 EvalCall(Dst, CE, L, *DI);
1366
1367 // Handle the case where no nodes where generated. Auto-generate that
1368 // contains the updated state if we aren't generating sinks.
1369
1370 if (!Builder->BuildSinks && Dst.size() == size &&
1371 !Builder->HasGeneratedNode)
1372 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001373 }
1374}
1375
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001376//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001377// Transfer function: Objective-C ivar references.
1378//===----------------------------------------------------------------------===//
1379
1380void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1381 NodeTy* Pred, NodeSet& Dst,
1382 bool asLValue) {
1383
1384 Expr* Base = cast<Expr>(Ex->getBase());
1385 NodeSet Tmp;
1386 Visit(Base, Pred, Tmp);
1387
1388 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1389 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001390 SVal BaseVal = GetSVal(St, Base);
1391 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001392
1393 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001394 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001395 else
1396 EvalLoad(Dst, Ex, *I, St, location);
1397 }
1398}
1399
1400//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001401// Transfer function: Objective-C fast enumeration 'for' statements.
1402//===----------------------------------------------------------------------===//
1403
1404void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1405 NodeTy* Pred, NodeSet& Dst) {
1406
1407 // ObjCForCollectionStmts are processed in two places. This method
1408 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1409 // statements within a basic block. This transfer function does two things:
1410 //
1411 // (1) binds the next container value to 'element'. This creates a new
1412 // node in the ExplodedGraph.
1413 //
1414 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1415 // whether or not the container has any more elements. This value
1416 // will be tested in ProcessBranch. We need to explicitly bind
1417 // this value because a container can contain nil elements.
1418 //
1419 // FIXME: Eventually this logic should actually do dispatches to
1420 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1421 // This will require simulating a temporary NSFastEnumerationState, either
1422 // through an SVal or through the use of MemRegions. This value can
1423 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1424 // terminates we reclaim the temporary (it goes out of scope) and we
1425 // we can test if the SVal is 0 or if the MemRegion is null (depending
1426 // on what approach we take).
1427 //
1428 // For now: simulate (1) by assigning either a symbol or nil if the
1429 // container is empty. Thus this transfer function will by default
1430 // result in state splitting.
1431
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001432 Stmt* elem = S->getElement();
1433 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001434
1435 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001436 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001437 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001438 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1439 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1440 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001441 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001442
1443 NodeSet Tmp;
1444 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001445
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001446 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1447 const GRState* state = GetState(*I);
1448 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1449 }
1450}
1451
1452void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1453 NodeTy* Pred, NodeSet& Dst,
1454 SVal ElementV) {
1455
1456
Ted Kremenekaf337412008-11-12 19:24:17 +00001457
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001458 // Get the current state. Use 'EvalLocation' to determine if it is a null
1459 // pointer, etc.
1460 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001461
Ted Kremenek8c354752008-12-16 22:02:27 +00001462 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1463 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001464 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001465
1466 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001467
Ted Kremenekaf337412008-11-12 19:24:17 +00001468 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001469 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001470 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1471 GRStateRef hasElems = state.BindExpr(S, TrueV);
1472
Ted Kremenekaf337412008-11-12 19:24:17 +00001473 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001474 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1475 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001476
1477 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1478 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1479 // FIXME: The proper thing to do is to really iterate over the
1480 // container. We will do this with dispatch logic to the store.
1481 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001482 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001483 assert (Loc::IsLocType(T));
1484 unsigned Count = Builder->getCurrentBlockCount();
1485 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1486 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001487
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001488 // Bind the location to 'nil' on the false branch.
1489 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1490 noElems = noElems.BindLoc(ElementV, nilV);
1491 }
1492
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001493 // Create the new nodes.
1494 MakeNode(Dst, S, Pred, hasElems);
1495 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001496}
1497
1498//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001499// Transfer function: Objective-C message expressions.
1500//===----------------------------------------------------------------------===//
1501
1502void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1503 NodeSet& Dst){
1504
1505 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1506 Pred, Dst);
1507}
1508
1509void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001510 ObjCMessageExpr::arg_iterator AI,
1511 ObjCMessageExpr::arg_iterator AE,
1512 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001513 if (AI == AE) {
1514
1515 // Process the receiver.
1516
1517 if (Expr* Receiver = ME->getReceiver()) {
1518 NodeSet Tmp;
1519 Visit(Receiver, Pred, Tmp);
1520
1521 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1522 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1523
1524 return;
1525 }
1526
1527 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1528 return;
1529 }
1530
1531 NodeSet Tmp;
1532 Visit(*AI, Pred, Tmp);
1533
1534 ++AI;
1535
1536 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1537 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1538}
1539
1540void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1541 NodeTy* Pred,
1542 NodeSet& Dst) {
1543
1544 // FIXME: More logic for the processing the method call.
1545
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001546 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001547 bool RaisesException = false;
1548
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001549
1550 if (Expr* Receiver = ME->getReceiver()) {
1551
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001552 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001553
1554 // Check for undefined control-flow or calls to NULL.
1555
1556 if (L.isUndef()) {
1557 NodeTy* N = Builder->generateNode(ME, St, Pred);
1558
1559 if (N) {
1560 N->markAsSink();
1561 UndefReceivers.insert(N);
1562 }
1563
1564 return;
1565 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001566
1567 // Check if the "raise" message was sent.
1568 if (ME->getSelector() == RaiseSel)
1569 RaisesException = true;
1570 }
1571 else {
1572
1573 IdentifierInfo* ClsName = ME->getClassName();
1574 Selector S = ME->getSelector();
1575
1576 // Check for special instance methods.
1577
1578 if (!NSExceptionII) {
1579 ASTContext& Ctx = getContext();
1580
1581 NSExceptionII = &Ctx.Idents.get("NSException");
1582 }
1583
1584 if (ClsName == NSExceptionII) {
1585
1586 enum { NUM_RAISE_SELECTORS = 2 };
1587
1588 // Lazily create a cache of the selectors.
1589
1590 if (!NSExceptionInstanceRaiseSelectors) {
1591
1592 ASTContext& Ctx = getContext();
1593
1594 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1595
1596 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1597 unsigned idx = 0;
1598
1599 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001600 II.push_back(&Ctx.Idents.get("raise"));
1601 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001602 NSExceptionInstanceRaiseSelectors[idx++] =
1603 Ctx.Selectors.getSelector(II.size(), &II[0]);
1604
1605 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001606 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001607 NSExceptionInstanceRaiseSelectors[idx++] =
1608 Ctx.Selectors.getSelector(II.size(), &II[0]);
1609 }
1610
1611 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1612 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1613 RaisesException = true; break;
1614 }
1615 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001616 }
1617
1618 // Check for any arguments that are uninitialized/undefined.
1619
1620 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1621 I != E; ++I) {
1622
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001623 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001624
1625 // Generate an error node for passing an uninitialized/undefined value
1626 // as an argument to a message expression. This node is a sink.
1627 NodeTy* N = Builder->generateNode(ME, St, Pred);
1628
1629 if (N) {
1630 N->markAsSink();
1631 MsgExprUndefArgs[N] = *I;
1632 }
1633
1634 return;
1635 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001636 }
1637
1638 // Check if we raise an exception. For now treat these as sinks. Eventually
1639 // we will want to handle exceptions properly.
1640
1641 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1642
1643 if (RaisesException)
1644 Builder->BuildSinks = true;
1645
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001646 // Dispatch to plug-in transfer function.
1647
1648 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001649 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001650
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001651 EvalObjCMessageExpr(Dst, ME, Pred);
1652
1653 // Handle the case where no nodes where generated. Auto-generate that
1654 // contains the updated state if we aren't generating sinks.
1655
Ted Kremenekb0533962008-04-18 20:35:30 +00001656 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001657 MakeNode(Dst, ME, Pred, St);
1658}
1659
1660//===----------------------------------------------------------------------===//
1661// Transfer functions: Miscellaneous statements.
1662//===----------------------------------------------------------------------===//
1663
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001664void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001665 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001666 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001667 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001668
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001669 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001670 T = ExCast->getTypeAsWritten();
1671
Zhongxing Xued340f72008-10-22 08:02:16 +00001672 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001673 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001674 else
1675 Visit(Ex, Pred, S1);
1676
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001677 // Check for casting to "void".
1678 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001679
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001680 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001681 Dst.Add(*I1);
1682
Ted Kremenek874d63f2008-01-24 02:02:54 +00001683 return;
1684 }
1685
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001686 // FIXME: The rest of this should probably just go into EvalCall, and
1687 // let the transfer function object be responsible for constructing
1688 // nodes.
1689
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001690 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001691 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001692 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001693 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001694
1695 // Unknown?
1696
1697 if (V.isUnknown()) {
1698 Dst.Add(N);
1699 continue;
1700 }
1701
1702 // Undefined?
1703
1704 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001705 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001706 continue;
1707 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001708
1709 // For const casts, just propagate the value.
1710 ASTContext& C = getContext();
1711
1712 if (C.getCanonicalType(T).getUnqualifiedType() ==
1713 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001714 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001715 continue;
1716 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001717
1718 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001719 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001720 unsigned bits = getContext().getTypeSize(ExTy);
1721
1722 // FIXME: Determine if the number of bits of the target type is
1723 // equal or exceeds the number of bits to store the pointer value.
1724 // If not, flag an error.
1725
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001726 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001727 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001728 continue;
1729 }
1730
1731 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001732 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1733 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001734 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001735 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001736 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001737 continue;
1738 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001739
Zhongxing Xu37d682a2008-11-14 09:23:38 +00001740 // Check for casts from array type to pointer type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001741 if (ExTy->isArrayType()) {
Ted Kremenek0fb7c612008-11-15 05:00:27 +00001742 assert(T->isPointerType());
Zhongxing Xue1911af2008-10-23 03:10:39 +00001743 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001744 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001745 continue;
1746 }
1747
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001748 // Check for casts from a region to a specific type.
1749 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001750 assert(Loc::IsLocType(T));
1751 assert(Loc::IsLocType(ExTy));
1752
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001753 const MemRegion* R = RV->getRegion();
1754 StoreManager& StoreMgr = getStoreManager();
1755
1756 // Delegate to store manager to get the result of casting a region
1757 // to a different type.
1758 const StoreManager::CastResult& Res = StoreMgr.CastRegion(St, R, T);
1759
1760 // Inspect the result. If the MemRegion* returned is NULL, this
1761 // expression evaluates to UnknownVal.
1762 R = Res.getRegion();
1763 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1764
1765 // Generate the new node in the ExplodedGraph.
1766 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001767 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001768 }
1769
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001770 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001771 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001772 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001773}
1774
Ted Kremenek4f090272008-10-27 21:54:31 +00001775void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001776 NodeTy* Pred, NodeSet& Dst,
1777 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001778 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1779 NodeSet Tmp;
1780 Visit(ILE, Pred, Tmp);
1781
1782 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001783 const GRState* St = GetState(*I);
1784 SVal ILV = GetSVal(St, ILE);
1785 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001786
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001787 if (asLValue)
1788 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1789 else
1790 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001791 }
1792}
1793
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001794void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001795
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001796 // The CFG has one DeclStmt per Decl.
1797 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001798
1799 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001800 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001801
Ted Kremenekefd59942008-12-08 22:47:34 +00001802 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001803 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001804
1805 // FIXME: static variables may have an initializer, but the second
1806 // time a function is called those values may not be current.
1807 NodeSet Tmp;
1808
Ted Kremenekaf337412008-11-12 19:24:17 +00001809 if (InitEx)
1810 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001811
1812 if (Tmp.empty())
1813 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001814
1815 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001816 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001817 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001818
1819 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00001820 if (InitEx) {
1821 SVal InitVal = GetSVal(St, InitEx);
1822 QualType T = VD->getType();
1823
1824 // Recover some path-sensitivity if a scalar value evaluated to
1825 // UnknownVal.
1826 if (InitVal.isUnknown()) {
1827 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001828 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001829 InitVal = loc::SymbolVal(Sym);
1830 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001831 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001832 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00001833 InitVal = nonloc::SymbolVal(Sym);
1834 }
1835 }
1836
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001837 St = StateMgr.BindDecl(St, VD, InitVal);
1838 } else
1839 St = StateMgr.BindDeclWithNoInit(St, VD);
Ted Kremenekefd59942008-12-08 22:47:34 +00001840
1841 // Check if 'VD' is a VLA and if so check if has a non-zero size.
1842 QualType T = getContext().getCanonicalType(VD->getType());
1843 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
1844 // FIXME: Handle multi-dimensional VLAs.
1845
1846 Expr* SE = VLA->getSizeExpr();
1847 SVal Size = GetSVal(St, SE);
Ted Kremenek159d2482008-12-09 00:44:16 +00001848
1849 if (Size.isUndef()) {
1850 if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
1851 N->markAsSink();
1852 ExplicitBadSizedVLA.insert(N);
1853 }
1854 continue;
1855 }
Ted Kremenekefd59942008-12-08 22:47:34 +00001856
1857 bool isFeasibleZero = false;
1858 const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
1859
1860 bool isFeasibleNotZero = false;
1861 St = Assume(St, Size, true, isFeasibleNotZero);
1862
1863 if (isFeasibleZero) {
1864 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
1865 N->markAsSink();
Ted Kremenek159d2482008-12-09 00:44:16 +00001866 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
1867 else ExplicitBadSizedVLA.insert(N);
Ted Kremenekefd59942008-12-08 22:47:34 +00001868 }
1869 }
1870
1871 if (!isFeasibleNotZero)
1872 continue;
1873 }
Ted Kremenekaf337412008-11-12 19:24:17 +00001874
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001875 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001876 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001877}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001878
Ted Kremenekf75b1862008-10-30 17:47:32 +00001879namespace {
1880 // This class is used by VisitInitListExpr as an item in a worklist
1881 // for processing the values contained in an InitListExpr.
1882class VISIBILITY_HIDDEN InitListWLItem {
1883public:
1884 llvm::ImmutableList<SVal> Vals;
1885 GRExprEngine::NodeTy* N;
1886 InitListExpr::reverse_iterator Itr;
1887
1888 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1889 InitListExpr::reverse_iterator itr)
1890 : Vals(vals), N(n), Itr(itr) {}
1891};
1892}
1893
1894
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001895void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1896 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001897
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001898 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001899 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001900 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001901
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001902 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001903
Ted Kremeneka49e3672008-10-30 23:14:36 +00001904 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001905
Ted Kremeneka49e3672008-10-30 23:14:36 +00001906 // Handle base case where the initializer has no elements.
1907 // e.g: static int* myArray[] = {};
1908 if (NumInitElements == 0) {
1909 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1910 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1911 return;
1912 }
1913
1914 // Create a worklist to process the initializers.
1915 llvm::SmallVector<InitListWLItem, 10> WorkList;
1916 WorkList.reserve(NumInitElements);
1917 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001918 InitListExpr::reverse_iterator ItrEnd = E->rend();
1919
Ted Kremeneka49e3672008-10-30 23:14:36 +00001920 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001921 while (!WorkList.empty()) {
1922 InitListWLItem X = WorkList.back();
1923 WorkList.pop_back();
1924
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001925 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001926 Visit(*X.Itr, X.N, Tmp);
1927
1928 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001929
Ted Kremenekf75b1862008-10-30 17:47:32 +00001930 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1931 // Get the last initializer value.
1932 state = GetState(*NI);
1933 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1934
1935 // Construct the new list of values by prepending the new value to
1936 // the already constructed list.
1937 llvm::ImmutableList<SVal> NewVals =
1938 getBasicVals().consVals(InitV, X.Vals);
1939
1940 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001941 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001942 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001943
Ted Kremenekf75b1862008-10-30 17:47:32 +00001944 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001945 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001946 }
1947 else {
1948 // Still some initializer values to go. Push them onto the worklist.
1949 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1950 }
1951 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001952 }
Ted Kremenek87903072008-10-30 18:34:31 +00001953
1954 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001955 }
1956
Ted Kremenek062e2f92008-11-13 06:10:40 +00001957 if (T->isUnionType() || T->isVectorType()) {
1958 // FIXME: to be implemented.
1959 // Note: That vectors can return true for T->isIntegerType()
1960 MakeNode(Dst, E, Pred, state);
1961 return;
1962 }
1963
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001964 if (Loc::IsLocType(T) || T->isIntegerType()) {
1965 assert (E->getNumInits() == 1);
1966 NodeSet Tmp;
1967 Expr* Init = E->getInit(0);
1968 Visit(Init, Pred, Tmp);
1969 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1970 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001971 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001972 }
1973 return;
1974 }
1975
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001976
1977 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1978 assert(0 && "unprocessed InitListExpr type");
1979}
Ted Kremenekf233d482008-02-05 00:26:40 +00001980
Sebastian Redl05189992008-11-11 17:56:53 +00001981/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1982void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1983 NodeTy* Pred,
1984 NodeSet& Dst) {
1985 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001986 uint64_t amt;
1987
1988 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001989 if (T == getContext().VoidTy) {
1990 // sizeof(void) == 1 byte.
1991 amt = 1;
1992 }
1993 else if (!T.getTypePtr()->isConstantSizeType()) {
1994 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00001995 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00001996 }
1997 else if (T->isObjCInterfaceType()) {
1998 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1999 // the compiler has laid out its representation. Just report Unknown
2000 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002001 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002002 }
2003 else {
2004 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002005 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002006 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002007 }
2008 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002009 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002010
Ted Kremenek0e561a32008-03-21 21:30:14 +00002011 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002012 BindExpr(GetState(Pred), Ex,
2013 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002014}
2015
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002016
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002017void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002018 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002019
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002020 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002021
2022 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002023 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002024
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002025 case UnaryOperator::Deref: {
2026
2027 Expr* Ex = U->getSubExpr()->IgnoreParens();
2028 NodeSet Tmp;
2029 Visit(Ex, Pred, Tmp);
2030
2031 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002032
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002033 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002034 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002035
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002036 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002037 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002038 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00002039 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002040 }
2041
2042 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002043 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002044
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002045 case UnaryOperator::Real: {
2046
2047 Expr* Ex = U->getSubExpr()->IgnoreParens();
2048 NodeSet Tmp;
2049 Visit(Ex, Pred, Tmp);
2050
2051 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2052
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002053 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002054 if (Ex->getType()->isAnyComplexType()) {
2055 // Just report "Unknown."
2056 Dst.Add(*I);
2057 continue;
2058 }
2059
2060 // For all other types, UnaryOperator::Real is an identity operation.
2061 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002062 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002063 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002064 }
2065
2066 return;
2067 }
2068
2069 case UnaryOperator::Imag: {
2070
2071 Expr* Ex = U->getSubExpr()->IgnoreParens();
2072 NodeSet Tmp;
2073 Visit(Ex, Pred, Tmp);
2074
2075 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002076 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002077 if (Ex->getType()->isAnyComplexType()) {
2078 // Just report "Unknown."
2079 Dst.Add(*I);
2080 continue;
2081 }
2082
2083 // For all other types, UnaryOperator::Float returns 0.
2084 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002085 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002086 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002087 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002088 }
2089
2090 return;
2091 }
2092
2093 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002094 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002095 Dst.Add(Pred);
2096 return;
2097
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002098 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002099 case UnaryOperator::Extension: {
2100
2101 // Unary "+" is a no-op, similar to a parentheses. We still have places
2102 // where it may be a block-level expression, so we need to
2103 // generate an extra node that just propagates the value of the
2104 // subexpression.
2105
2106 Expr* Ex = U->getSubExpr()->IgnoreParens();
2107 NodeSet Tmp;
2108 Visit(Ex, Pred, Tmp);
2109
2110 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002111 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002112 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002113 }
2114
2115 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002116 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002117
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002118 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002119
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002120 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002121 Expr* Ex = U->getSubExpr()->IgnoreParens();
2122 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002123 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002124
2125 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002126 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002127 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002128 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002129 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002130 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002131
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002132 return;
2133 }
2134
2135 case UnaryOperator::LNot:
2136 case UnaryOperator::Minus:
2137 case UnaryOperator::Not: {
2138
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002139 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002140 Expr* Ex = U->getSubExpr()->IgnoreParens();
2141 NodeSet Tmp;
2142 Visit(Ex, Pred, Tmp);
2143
2144 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002145 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002146
2147 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002148 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002149
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002150 if (V.isUnknownOrUndef()) {
2151 MakeNode(Dst, U, *I, BindExpr(St, U, V));
2152 continue;
2153 }
2154
Ted Kremenek60595da2008-11-15 04:01:56 +00002155// QualType DstT = getContext().getCanonicalType(U->getType());
2156// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2157//
2158// if (DstT != SrcT) // Perform promotions.
2159// V = EvalCast(V, DstT);
2160//
2161// if (V.isUnknownOrUndef()) {
2162// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2163// continue;
2164// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002165
2166 switch (U->getOpcode()) {
2167 default:
2168 assert(false && "Invalid Opcode.");
2169 break;
2170
2171 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002172 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002173 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002174 break;
2175
2176 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002177 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002178 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002179 break;
2180
2181 case UnaryOperator::LNot:
2182
2183 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2184 //
2185 // Note: technically we do "E == 0", but this is the same in the
2186 // transfer functions as "0 == E".
2187
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002188 if (isa<Loc>(V)) {
2189 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2190 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002191 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002192 }
2193 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002194 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002195#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002196 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2197 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002198#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002199 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002200 continue;
2201#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002202 }
2203
2204 break;
2205 }
2206
2207 MakeNode(Dst, U, *I, St);
2208 }
2209
2210 return;
2211 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002212 }
2213
2214 // Handle ++ and -- (both pre- and post-increment).
2215
2216 assert (U->isIncrementDecrementOp());
2217 NodeSet Tmp;
2218 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002219 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002220
2221 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2222
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002223 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002224 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002225
2226 // Perform a load.
2227 NodeSet Tmp2;
2228 EvalLoad(Tmp2, Ex, *I, St, V1);
2229
2230 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2231
2232 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002233 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002234
2235 // Propagate unknown and undefined values.
2236 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002237 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002238 continue;
2239 }
2240
Ted Kremenek443003b2008-02-21 19:29:23 +00002241 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002242
2243 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2244 : BinaryOperator::Sub;
2245
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002246 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002247 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002248
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002249 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002250 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002251 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002252 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002253}
2254
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002255void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2256 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2257}
2258
2259void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2260 AsmStmt::outputs_iterator I,
2261 AsmStmt::outputs_iterator E,
2262 NodeTy* Pred, NodeSet& Dst) {
2263 if (I == E) {
2264 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2265 return;
2266 }
2267
2268 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002269 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002270
2271 ++I;
2272
2273 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2274 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2275}
2276
2277void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2278 AsmStmt::inputs_iterator I,
2279 AsmStmt::inputs_iterator E,
2280 NodeTy* Pred, NodeSet& Dst) {
2281 if (I == E) {
2282
2283 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002284 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002285
2286 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2287 // which interprets the inline asm and stores proper results in the
2288 // outputs.
2289
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002290 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002291
2292 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2293 OE = A->end_outputs(); OI != OE; ++OI) {
2294
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002295 SVal X = GetSVal(St, *OI);
2296 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002297
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002298 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002299 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002300 }
2301
Ted Kremenek0e561a32008-03-21 21:30:14 +00002302 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002303 return;
2304 }
2305
2306 NodeSet Tmp;
2307 Visit(*I, Pred, Tmp);
2308
2309 ++I;
2310
2311 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2312 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2313}
2314
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002315void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2316 assert (Builder && "GRStmtNodeBuilder must be defined.");
2317
2318 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002319
Ted Kremenek186350f2008-04-23 20:12:28 +00002320 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2321 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002322
Ted Kremenek729a9a22008-07-17 23:15:45 +00002323 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002324
Ted Kremenekb0533962008-04-18 20:35:30 +00002325 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002326
Ted Kremenekb0533962008-04-18 20:35:30 +00002327 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002328 MakeNode(Dst, S, Pred, GetState(Pred));
2329}
2330
Ted Kremenek02737ed2008-03-31 15:02:58 +00002331void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2332
2333 Expr* R = S->getRetValue();
2334
2335 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002336 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002337 return;
2338 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002339
Ted Kremenek5917d782008-11-21 00:27:44 +00002340 NodeSet Tmp;
2341 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002342
Ted Kremenek5917d782008-11-21 00:27:44 +00002343 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2344 SVal X = GetSVal((*I)->getState(), R);
2345
2346 // Check if we return the address of a stack variable.
2347 if (isa<loc::MemRegionVal>(X)) {
2348 // Determine if the value is on the stack.
2349 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002350
Ted Kremenek5917d782008-11-21 00:27:44 +00002351 if (R && getStateManager().hasStackStorage(R)) {
2352 // Create a special node representing the error.
2353 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2354 N->markAsSink();
2355 RetsStackAddr.insert(N);
2356 }
2357 continue;
2358 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002359 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002360 // Check if we return an undefined value.
2361 else if (X.isUndef()) {
2362 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2363 N->markAsSink();
2364 RetsUndef.insert(N);
2365 }
2366 continue;
2367 }
2368
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002369 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002370 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002371}
Ted Kremenek55deb972008-03-25 00:34:37 +00002372
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002373//===----------------------------------------------------------------------===//
2374// Transfer functions: Binary operators.
2375//===----------------------------------------------------------------------===//
2376
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002377const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2378 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002379
2380 // Divide by undefined? (potentially zero)
2381
2382 if (Denom.isUndef()) {
2383 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2384
2385 if (DivUndef) {
2386 DivUndef->markAsSink();
2387 ExplicitBadDivides.insert(DivUndef);
2388 }
2389
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002390 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002391 }
2392
2393 // Check for divide/remainder-by-zero.
2394 // First, "assume" that the denominator is 0 or undefined.
2395
2396 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002397 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002398
2399 // Second, "assume" that the denominator cannot be 0.
2400
2401 bool isFeasibleNotZero = false;
2402 St = Assume(St, Denom, true, isFeasibleNotZero);
2403
2404 // Create the node for the divide-by-zero (if it occurred).
2405
2406 if (isFeasibleZero)
2407 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2408 DivZeroNode->markAsSink();
2409
2410 if (isFeasibleNotZero)
2411 ImplicitBadDivides.insert(DivZeroNode);
2412 else
2413 ExplicitBadDivides.insert(DivZeroNode);
2414
2415 }
2416
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002417 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002418}
2419
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002420void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002421 GRExprEngine::NodeTy* Pred,
2422 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002423
2424 NodeSet Tmp1;
2425 Expr* LHS = B->getLHS()->IgnoreParens();
2426 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002427
Ted Kremenek759623e2008-12-06 02:39:30 +00002428 // FIXME: Add proper support for ObjCKVCRefExpr.
2429 if (isa<ObjCKVCRefExpr>(LHS)) {
2430 Visit(RHS, Pred, Dst);
2431 return;
2432 }
2433
2434
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002435 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002436 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002437 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002438 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002439
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002440 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002441
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002442 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002443
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002444 // Process the RHS.
2445
2446 NodeSet Tmp2;
2447 Visit(RHS, *I1, Tmp2);
2448
2449 // With both the LHS and RHS evaluated, process the operation itself.
2450
2451 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002452
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002453 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002454 const GRState* OldSt = St;
2455
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002456 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002457 BinaryOperator::Opcode Op = B->getOpcode();
2458
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002459 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002460
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002461 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002462
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002463 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002464 // FIXME: Handle structs.
2465 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002466
Ted Kremenek062e2f92008-11-13 06:10:40 +00002467 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2468 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002469 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002470 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002471
Ted Kremenek062e2f92008-11-13 06:10:40 +00002472 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002473 ? cast<SVal>(loc::SymbolVal(Sym))
2474 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002475 }
2476
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002477 // Simulate the effects of a "store": bind the value of the RHS
2478 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002479
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002480 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002481 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002482 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002483
2484 case BinaryOperator::Div:
2485 case BinaryOperator::Rem:
2486
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002487 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002488 if (RHS->getType()->isIntegerType() &&
2489 RHS->getType()->isScalarType()) {
2490
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002491 St = CheckDivideZero(B, St, *I2, RightV);
2492 if (!St) continue;
2493 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002494
2495 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002496
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002497 default: {
2498
2499 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002500 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002501
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002502 // Process non-assignements except commas or short-circuited
2503 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002504
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002505 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002506
2507 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002508 if (OldSt != St) {
2509 // Generate a new node if we have already created a new state.
2510 MakeNode(Dst, B, *I2, St);
2511 }
2512 else
2513 Dst.Add(*I2);
2514
Ted Kremenek89063af2008-02-21 19:15:37 +00002515 continue;
2516 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002517
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002518 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002519
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002520 // The operands were *not* undefined, but the result is undefined.
2521 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002522
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002523 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002524 UndefNode->markAsSink();
2525 UndefResults.insert(UndefNode);
2526 }
2527
2528 continue;
2529 }
2530
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002531 // Otherwise, create a new node.
2532
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002533 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002534 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002535 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002536 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002537
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002538 assert (B->isCompoundAssignmentOp());
2539
Ted Kremenek934e3e92008-10-27 23:02:39 +00002540 if (Op >= BinaryOperator::AndAssign) {
2541 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2542 BinaryOperator::And));
2543 }
2544 else {
2545 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2546 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002547
2548 // Perform a load (the LHS). This performs the checks for
2549 // null dereferences, and so on.
2550 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002551 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002552 EvalLoad(Tmp3, LHS, *I2, St, location);
2553
2554 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2555
2556 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002557 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002558
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002559 // Check for divide-by-zero.
2560 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002561 && RHS->getType()->isIntegerType()
2562 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002563
2564 // CheckDivideZero returns a new state where the denominator
2565 // is assumed to be non-zero.
2566 St = CheckDivideZero(B, St, *I3, RightV);
2567
2568 if (!St)
2569 continue;
2570 }
2571
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002572 // Propagate undefined values (left-side).
2573 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002574 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002575 continue;
2576 }
2577
2578 // Propagate unknown values (left and right-side).
2579 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002580 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002581 UnknownVal());
2582 continue;
2583 }
2584
2585 // At this point:
2586 //
2587 // The LHS is not Undef/Unknown.
2588 // The RHS is not Unknown.
2589
2590 // Get the computation type.
2591 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002592 CTy = getContext().getCanonicalType(CTy);
2593
2594 QualType LTy = getContext().getCanonicalType(LHS->getType());
2595 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002596
2597 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002598 if (LTy != CTy) V = EvalCast(V, CTy);
2599 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002600
2601 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002602 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002603 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002604 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002605 continue;
2606 }
2607
Ted Kremenek60595da2008-11-15 04:01:56 +00002608 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002609 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002610
2611 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002612 // The operands were not undefined, but the result is undefined.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002613 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2614 UndefNode->markAsSink();
2615 UndefResults.insert(UndefNode);
2616 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002617 continue;
2618 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002619
2620 // EXPERIMENTAL: "Conjured" symbols.
2621 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002622
2623 SVal LHSVal;
2624
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002625 if (Result.isUnknown() && (Loc::IsLocType(CTy)
2626 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002627
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002628 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002629
Ted Kremenek60595da2008-11-15 04:01:56 +00002630 // The symbolic value is actually for the type of the left-hand side
2631 // expression, not the computation type, as this is the value the
2632 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002633 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002634 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002635 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002636 : cast<SVal>(nonloc::SymbolVal(Sym));
2637
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002638 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002639 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002640 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002641 else {
2642 // The left-hand side may bind to a different value then the
2643 // computation type.
2644 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2645 }
2646
2647 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002648 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002649 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002650 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002651}
Ted Kremenekee985462008-01-16 18:18:48 +00002652
2653//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002654// Transfer-function Helpers.
2655//===----------------------------------------------------------------------===//
2656
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002657void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002658 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002659 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002660 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002661
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002662 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002663 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2664
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002665 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002666 MakeNode(Dst, Ex, Pred, *I);
2667}
2668
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002669void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002670 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002671 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002672
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002673 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002674 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002675}
2676
2677//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002678// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002679//===----------------------------------------------------------------------===//
2680
Ted Kremenekaa66a322008-01-16 21:46:15 +00002681#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002682static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002683static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002684
Ted Kremenekaa66a322008-01-16 21:46:15 +00002685namespace llvm {
2686template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002687struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002688 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002689
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002690 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2691
2692 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002693 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002694 GraphPrintCheckerState->isUndefDeref(N) ||
2695 GraphPrintCheckerState->isUndefStore(N) ||
2696 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002697 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2698 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002699 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002700 GraphPrintCheckerState->isBadCall(N) ||
2701 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002702 return "color=\"red\",style=\"filled\"";
2703
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002704 if (GraphPrintCheckerState->isNoReturnCall(N))
2705 return "color=\"blue\",style=\"filled\"";
2706
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002707 return "";
2708 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002709
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002710 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002711 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002712
2713 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002714 ProgramPoint Loc = N->getLocation();
2715
2716 switch (Loc.getKind()) {
2717 case ProgramPoint::BlockEntranceKind:
2718 Out << "Block Entrance: B"
2719 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2720 break;
2721
2722 case ProgramPoint::BlockExitKind:
2723 assert (false);
2724 break;
2725
Ted Kremenekaa66a322008-01-16 21:46:15 +00002726 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002727 if (isa<PostStmt>(Loc)) {
2728 const PostStmt& L = cast<PostStmt>(Loc);
2729 Stmt* S = L.getStmt();
2730 SourceLocation SLoc = S->getLocStart();
2731
2732 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2733 llvm::raw_os_ostream OutS(Out);
2734 S->printPretty(OutS);
2735 OutS.flush();
2736
2737 if (SLoc.isFileID()) {
2738 Out << "\\lline="
2739 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2740 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
2741 }
2742
2743 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2744 Out << "\\|Implicit-Null Dereference.\\l";
2745 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2746 Out << "\\|Explicit-Null Dereference.\\l";
2747 else if (GraphPrintCheckerState->isUndefDeref(N))
2748 Out << "\\|Dereference of undefialied value.\\l";
2749 else if (GraphPrintCheckerState->isUndefStore(N))
2750 Out << "\\|Store to Undefined Loc.";
2751 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2752 Out << "\\|Explicit divide-by zero or undefined value.";
2753 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2754 Out << "\\|Implicit divide-by zero or undefined value.";
2755 else if (GraphPrintCheckerState->isUndefResult(N))
2756 Out << "\\|Result of operation is undefined.";
2757 else if (GraphPrintCheckerState->isNoReturnCall(N))
2758 Out << "\\|Call to function marked \"noreturn\".";
2759 else if (GraphPrintCheckerState->isBadCall(N))
2760 Out << "\\|Call to NULL/Undefined.";
2761 else if (GraphPrintCheckerState->isUndefArg(N))
2762 Out << "\\|Argument in call is undefined";
2763
2764 break;
2765 }
2766
Ted Kremenekaa66a322008-01-16 21:46:15 +00002767 const BlockEdge& E = cast<BlockEdge>(Loc);
2768 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2769 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002770
2771 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002772
2773 SourceLocation SLoc = T->getLocStart();
2774
Ted Kremenekb38911f2008-01-30 23:03:39 +00002775 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002776
Ted Kremeneka95d3752008-09-13 05:16:45 +00002777 llvm::raw_os_ostream OutS(Out);
2778 E.getSrc()->printTerminator(OutS);
2779 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002780
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002781 if (SLoc.isFileID()) {
2782 Out << "\\lline="
2783 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2784 << GraphPrintSourceManager->getColumnNumber(SLoc);
2785 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002786
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002787 if (isa<SwitchStmt>(T)) {
2788 Stmt* Label = E.getDst()->getLabel();
2789
2790 if (Label) {
2791 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2792 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002793 llvm::raw_os_ostream OutS(Out);
2794 C->getLHS()->printPretty(OutS);
2795 OutS.flush();
2796
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002797 if (Stmt* RHS = C->getRHS()) {
2798 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002799 RHS->printPretty(OutS);
2800 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002801 }
2802
2803 Out << ":";
2804 }
2805 else {
2806 assert (isa<DefaultStmt>(Label));
2807 Out << "\\ldefault:";
2808 }
2809 }
2810 else
2811 Out << "\\l(implicit) default:";
2812 }
2813 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002814 // FIXME
2815 }
2816 else {
2817 Out << "\\lCondition: ";
2818 if (*E.getSrc()->succ_begin() == E.getDst())
2819 Out << "true";
2820 else
2821 Out << "false";
2822 }
2823
2824 Out << "\\l";
2825 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002826
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002827 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2828 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002829 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002830 }
2831 }
2832
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002833 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002834
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002835 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2836 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002837
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002838 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002839 return Out.str();
2840 }
2841};
2842} // end llvm namespace
2843#endif
2844
Ted Kremenekffe0f432008-03-07 22:58:01 +00002845#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002846
2847template <typename ITERATOR>
2848GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2849
2850template <>
2851GRExprEngine::NodeTy*
2852GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2853 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2854 return I->first;
2855}
2856
Ted Kremenekffe0f432008-03-07 22:58:01 +00002857template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002858static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002859 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002860
Ted Kremenekd4527582008-09-16 18:44:52 +00002861 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002862
2863 for ( ; I != E; ++I ) {
2864 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002865 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002866
Ted Kremenekd4527582008-09-16 18:44:52 +00002867 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002868 continue;
2869
Ted Kremenekd4527582008-09-16 18:44:52 +00002870 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002871 Sources.push_back(N);
2872 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002873}
2874#endif
2875
2876void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002877#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002878 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002879 std::vector<NodeTy*> Src;
2880
2881 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002882 AddSources(Src, null_derefs_begin(), null_derefs_end());
2883 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2884 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2885 AddSources(Src, undef_results_begin(), undef_results_end());
2886 AddSources(Src, bad_calls_begin(), bad_calls_end());
2887 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002888 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002889
Ted Kremenekcb612922008-04-18 19:23:43 +00002890 // The new way.
2891 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2892 (*I)->GetErrorNodes(Src);
2893
2894
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002895 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002896 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002897 else {
2898 GraphPrintCheckerState = this;
2899 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002900
Ted Kremenekffe0f432008-03-07 22:58:01 +00002901 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002902
2903 GraphPrintCheckerState = NULL;
2904 GraphPrintSourceManager = NULL;
2905 }
2906#endif
2907}
2908
2909void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2910#ifndef NDEBUG
2911 GraphPrintCheckerState = this;
2912 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002913
Ted Kremenek493d7a22008-03-11 18:25:33 +00002914 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2915
2916 if (!TrimmedG)
2917 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2918 else {
2919 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2920 delete TrimmedG;
2921 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002922
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002923 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002924 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002925#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002926}