blob: 545d869379dfed2fe802d25c5b96d94f97c465a2 [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,
119 GRStateManager::StoreManagerCreator SMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000120 : CoreEngine(cfg, CD, Ctx, *this),
121 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000122 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000123 Builder(NULL),
Ted Kremenek95c7b002008-10-24 01:04:59 +0000124 StateMgr(G.getContext(), SMC,
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000125 CreateBasicConstraintManager, 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 }
309
310 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
311 break;
312 }
313
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);
318 break;
319 }
320
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
380 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000381 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000382 break;
383
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000384 case Stmt::ReturnStmtClass:
385 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
386 break;
387
Sebastian Redl05189992008-11-11 17:56:53 +0000388 case Stmt::SizeOfAlignOfExprClass:
389 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000390 break;
391
392 case Stmt::StmtExprClass: {
393 StmtExpr* SE = cast<StmtExpr>(S);
394
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000395 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396
397 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
398 // probably just remove these from the CFG.
399 assert (!SE->getSubStmt()->body_empty());
400
401 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000402 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 else
404 Dst.Add(Pred);
405
406 break;
407 }
408
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000409 case Stmt::UnaryOperatorClass:
410 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000412 }
413}
414
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000415void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000416
417 Ex = Ex->IgnoreParens();
418
419 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
420 Dst.Add(Pred);
421 return;
422 }
423
424 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000425
426 case Stmt::ArraySubscriptExprClass:
427 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
428 return;
429
430 case Stmt::DeclRefExprClass:
431 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
432 return;
433
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000434 case Stmt::ObjCIvarRefExprClass:
435 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
436 return;
437
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000438 case Stmt::UnaryOperatorClass:
439 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
440 return;
441
442 case Stmt::MemberExprClass:
443 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
444 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000445
Ted Kremenek4f090272008-10-27 21:54:31 +0000446 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000447 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000448 return;
449
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000450 case Stmt::ObjCPropertyRefExprClass:
451 // FIXME: Property assignments are lvalues, but not really "locations".
452 // e.g.: self.x = something;
453 // Here the "self.x" really can translate to a method call (setter) when
454 // the assignment is made. Moreover, the entire assignment expression
455 // evaluate to whatever "something" is, not calling the "getter" for
456 // the property (which would make sense since it can have side effects).
457 // We'll probably treat this as a location, but not one that we can
458 // take the address of. Perhaps we need a new SVal class for cases
459 // like thsis?
460 // Note that we have a similar problem for bitfields, since they don't
461 // have "locations" in the sense that we can take their address.
462 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000463 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000464
465 case Stmt::StringLiteralClass: {
466 const GRState* St = GetState(Pred);
467 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000468 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000469 return;
470 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000471
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000472 default:
473 // Arbitrary subexpressions can return aggregate temporaries that
474 // can be used in a lvalue context. We need to enhance our support
475 // of such temporaries in both the environment and the store, so right
476 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000477 assert ((Ex->getType()->isAggregateType() ||
478 Ex->getType()->isUnionType()) &&
479 "Other kinds of expressions with non-aggregate/union types do"
480 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000481
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000482 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000483 }
484}
485
486//===----------------------------------------------------------------------===//
487// Block entrance. (Update counters).
488//===----------------------------------------------------------------------===//
489
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000490bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000491 GRBlockCounter BC) {
492
493 return BC.getNumVisited(B->getBlockID()) < 3;
494}
495
496//===----------------------------------------------------------------------===//
497// Branch processing.
498//===----------------------------------------------------------------------===//
499
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000500const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000501 Stmt* Terminator,
502 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000503
504 switch (Terminator->getStmtClass()) {
505 default:
506 return St;
507
508 case Stmt::BinaryOperatorClass: { // '&&' and '||'
509
510 BinaryOperator* B = cast<BinaryOperator>(Terminator);
511 BinaryOperator::Opcode Op = B->getOpcode();
512
513 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
514
515 // For &&, if we take the true branch, then the value of the whole
516 // expression is that of the RHS expression.
517 //
518 // For ||, if we take the false branch, then the value of the whole
519 // expression is that of the RHS expression.
520
521 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
522 (Op == BinaryOperator::LOr && !branchTaken)
523 ? B->getRHS() : B->getLHS();
524
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000525 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000526 }
527
528 case Stmt::ConditionalOperatorClass: { // ?:
529
530 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
531
532 // For ?, if branchTaken == true then the value is either the LHS or
533 // the condition itself. (GNU extension).
534
535 Expr* Ex;
536
537 if (branchTaken)
538 Ex = C->getLHS() ? C->getLHS() : C->getCond();
539 else
540 Ex = C->getRHS();
541
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000542 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000543 }
544
545 case Stmt::ChooseExprClass: { // ?:
546
547 ChooseExpr* C = cast<ChooseExpr>(Terminator);
548
549 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000550 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000551 }
552 }
553}
554
Ted Kremenekaf337412008-11-12 19:24:17 +0000555void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000556 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000557
Ted Kremeneke7d22112008-02-11 19:21:59 +0000558 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000559 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000560 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000561
Ted Kremenekb2331832008-02-15 22:29:00 +0000562 // Check for NULL conditions; e.g. "for(;;)"
563 if (!Condition) {
564 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000565 return;
566 }
567
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000568 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000569
570 switch (V.getBaseKind()) {
571 default:
572 break;
573
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000574 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000575 builder.generateNode(MarkBranch(PrevState, Term, true), true);
576 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000577 return;
578
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000579 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000580 NodeTy* N = builder.generateNode(PrevState, true);
581
582 if (N) {
583 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000584 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000585 }
586
587 builder.markInfeasible(false);
588 return;
589 }
590 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000591
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000592 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000593
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000594 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000595 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000596
597 if (isFeasible)
598 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000599 else
600 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000601
602 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000603
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000604 isFeasible = false;
605 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000606
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000607 if (isFeasible)
608 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000609 else
610 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000611}
612
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000613/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000614/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000615void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000616
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000617 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000618 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000619
620 // Three possibilities:
621 //
622 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000623 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000624 // (3) We have no clue about the label. Dispatch to all targets.
625 //
626
627 typedef IndirectGotoNodeBuilder::iterator iterator;
628
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000629 if (isa<loc::GotoLabel>(V)) {
630 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000631
632 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000633 if (I.getLabel() == L) {
634 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000635 return;
636 }
637 }
638
639 assert (false && "No block with label.");
640 return;
641 }
642
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000643 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000644 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000645 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000646 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000647 return;
648 }
649
650 // This is really a catch-all. We don't support symbolics yet.
651
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000652 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000653
654 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000655 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000656}
Ted Kremenekf233d482008-02-05 00:26:40 +0000657
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000658
659void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
660 NodeTy* Pred, NodeSet& Dst) {
661
662 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
663
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000664 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000665 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000666
667 assert (X.isUndef());
668
669 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
670
671 assert (SE);
672
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000673 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000674
675 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000676 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000677}
678
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000679/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
680/// nodes by processing the 'effects' of a switch statement.
681void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
682
683 typedef SwitchNodeBuilder::iterator iterator;
684
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000685 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000686 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000687 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000688
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000689 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000690 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000691 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000692 return;
693 }
694
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000695 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000696
697 // While most of this can be assumed (such as the signedness), having it
698 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000699
Chris Lattner98be4942008-03-05 18:54:05 +0000700 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000701
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000702 APSInt V1(bits, false);
703 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000704 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000705
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000706 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000707
708 CaseStmt* Case = cast<CaseStmt>(I.getCase());
709
710 // Evaluate the case.
711 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
712 assert (false && "Case condition must evaluate to an integer constant.");
713 return;
714 }
715
716 // Get the RHS of the case, if it exists.
717
718 if (Expr* E = Case->getRHS()) {
719 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
720 assert (false &&
721 "Case condition (RHS) must evaluate to an integer constant.");
722 return ;
723 }
724
725 assert (V1 <= V2);
726 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000727 else
728 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000729
730 // FIXME: Eventually we should replace the logic below with a range
731 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000732 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000733
Ted Kremenek14a11402008-03-17 22:17:56 +0000734 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000735 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000736
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000737 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000738
739 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000740
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000741 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000742 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000743
744 if (isFeasible) {
745 builder.generateCaseStmtNode(I, StNew);
746
747 // If CondV evaluates to a constant, then we know that this
748 // is the *only* case that we can take, so stop evaluating the
749 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000750 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751 return;
752 }
753
754 // Now "assume" that the case doesn't match. Add this state
755 // to the default state (if it is feasible).
756
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000757 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000758 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759
Ted Kremenek5014ab12008-04-23 05:03:18 +0000760 if (isFeasible) {
761 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000762 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000763 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000764
Ted Kremenek14a11402008-03-17 22:17:56 +0000765 // Concretize the next value in the range.
766 if (V1 == V2)
767 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000768
Ted Kremenek14a11402008-03-17 22:17:56 +0000769 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000770 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000771
772 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000773 }
774
775 // If we reach here, than we know that the default branch is
776 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000777 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000778}
779
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000780//===----------------------------------------------------------------------===//
781// Transfer functions: logical operations ('&&', '||').
782//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000783
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000784void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000785 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000786
Ted Kremenek05a23782008-02-26 19:05:15 +0000787 assert (B->getOpcode() == BinaryOperator::LAnd ||
788 B->getOpcode() == BinaryOperator::LOr);
789
790 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
791
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000792 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000793 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000794
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000795 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000796
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000797 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000798
799 assert (Ex);
800
801 if (Ex == B->getRHS()) {
802
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000803 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000804
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000805 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000806
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000807 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000808 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000809 return;
810 }
811
Ted Kremenek05a23782008-02-26 19:05:15 +0000812 // We took the RHS. Because the value of the '&&' or '||' expression must
813 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
814 // or 1. Alternatively, we could take a lazy approach, and calculate this
815 // value later when necessary. We don't have the machinery in place for
816 // this right now, and since most logical expressions are used for branches,
817 // the payoff is not likely to be large. Instead, we do eager evaluation.
818
819 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000820 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000821
822 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000823 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000824 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000825
826 isFeasible = false;
827 NewState = Assume(St, X, false, isFeasible);
828
829 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000830 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000831 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000832 }
833 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000834 // We took the LHS expression. Depending on whether we are '&&' or
835 // '||' we know what the value of the expression is via properties of
836 // the short-circuiting.
837
838 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000839 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000840 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000841}
Ted Kremenek05a23782008-02-26 19:05:15 +0000842
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000843//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000844// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000845//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000846
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000847void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
848 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000849
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000850 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000851
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000852 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000853
854 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
855
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000856 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000857
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000858 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000859 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000860 else
861 EvalLoad(Dst, Ex, Pred, St, V);
862 return;
863
864 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
865 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
866
867 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000868 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000869 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000870 return;
871
872 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekcd162cc2008-10-17 00:55:33 +0000873 // FIXME: Does this need to be revised? We were getting cases in
874 // real code that did this.
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000875
Ted Kremeneke580c1b2008-10-31 15:33:11 +0000876 // FIXME: This is not a valid assertion. Produce a test case that
877 // refutes it.
878 // assert(asLValue); // Can we assume this?
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000879
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000880 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000881 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000882 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000883 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000884
885 assert (false &&
886 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000887}
888
Ted Kremenek540cbe22008-04-22 04:56:29 +0000889/// VisitArraySubscriptExpr - Transfer function for array accesses
890void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000891 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000892
893 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000894 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000895 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000896 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000897
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000898 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000899 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000900 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000901
902 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000903 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000904 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000905
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000906 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000907 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000908 else
909 EvalLoad(Dst, A, *I2, St, V);
910 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000911 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000912}
913
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000914/// VisitMemberExpr - Transfer function for member expressions.
915void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000916 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000917
918 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000919 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000920
921 if (M->isArrow())
922 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
923 else
924 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
925
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000926 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000927 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000928 // FIXME: Should we insert some assumption logic in here to determine
929 // if "Base" is a valid piece of memory? Before we put this assumption
930 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000931 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000932
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000933 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000934 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000935 else
936 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000937 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000938}
939
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000940void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000941 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000942
943 assert (Builder && "GRStmtNodeBuilder must be defined.");
944
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000945 // Evaluate the location (checks for bad dereferences).
946 St = EvalLocation(Ex, Pred, St, location);
947
948 if (!St)
949 return;
950
951 // Proceed with the store.
952
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000953 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000954
Ted Kremenek186350f2008-04-23 20:12:28 +0000955 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000956 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000957 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000958
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000959 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000960 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000961
Ted Kremenek729a9a22008-07-17 23:15:45 +0000962 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000963
964 // Handle the case where no nodes where generated. Auto-generate that
965 // contains the updated state if we aren't generating sinks.
966
Ted Kremenekb0533962008-04-18 20:35:30 +0000967 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000968 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000969 location, Val);
970}
971
972void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000973 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000974 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000975
976 // Evaluate the location (checks for bad dereferences).
977
978 St = EvalLocation(Ex, Pred, St, location, true);
979
980 if (!St)
981 return;
982
983 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000984 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000985
986 // FIXME: Currently symbolic analysis "generates" new symbols
987 // for the contents of values. We need a better approach.
988
989 // FIXME: The "CheckOnly" option exists only because Array and Field
990 // loads aren't fully implemented. Eventually this option will go away.
Ted Kremenek982e6742008-08-28 18:43:46 +0000991
Ted Kremenek436f2b92008-04-30 04:23:07 +0000992 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000993 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000994 else if (location.isUnknown()) {
995 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000996 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000997 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000998 else
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000999 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, GetSVal(St, cast<Loc>(location),
1000 Ex->getType())), K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001001}
1002
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001003void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001004 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001005
1006 NodeSet TmpDst;
1007 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1008
1009 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1010 MakeNode(Dst, Ex, *I, (*I)->getState());
1011}
1012
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001013const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001014 const GRState* St,
1015 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001016
1017 // Check for loads/stores from/to undefined values.
1018 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001019 ProgramPoint::Kind K =
1020 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1021
1022 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001023 Succ->markAsSink();
1024 UndefDeref.insert(Succ);
1025 }
1026
1027 return NULL;
1028 }
1029
1030 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1031 if (location.isUnknown())
1032 return St;
1033
1034 // During a load, one of two possible situations arise:
1035 // (1) A crash, because the location (pointer) was NULL.
1036 // (2) The location (pointer) is not NULL, and the dereference works.
1037 //
1038 // We add these assumptions.
1039
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001040 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001041
1042 // "Assume" that the pointer is not NULL.
1043
1044 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001045 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046
1047 // "Assume" that the pointer is NULL.
1048
1049 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001050 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1051 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052
1053 if (isFeasibleNull) {
1054
Ted Kremenek7360fda2008-09-18 23:09:54 +00001055 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001056 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001057 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1058
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001059 // We don't use "MakeNode" here because the node will be a sink
1060 // and we have no intention of processing it later.
1061
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001062 ProgramPoint::Kind K =
1063 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1064
1065 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001066
1067 if (NullNode) {
1068
1069 NullNode->markAsSink();
1070
1071 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1072 else ExplicitNullDeref.insert(NullNode);
1073 }
1074 }
Zhongxing Xu60156f02008-11-08 03:45:42 +00001075
1076 // Check for out-of-bound array access.
1077 if (isFeasibleNotNull && isa<loc::MemRegionVal>(LV)) {
1078 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1079 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1080 // Get the index of the accessed element.
1081 SVal Idx = ER->getIndex();
1082 // Get the extent of the array.
1083 SVal NumElements = StateMgr.getStoreManager().getSizeInElements(StNotNull,
1084 ER->getSuperRegion());
1085
1086 bool isFeasibleInBound = false;
1087 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1088 true, isFeasibleInBound);
1089
1090 bool isFeasibleOutBound = false;
1091 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1092 false, isFeasibleOutBound);
Chris Lattner8341be72008-11-10 03:00:37 +00001093 StInBound = StOutBound = 0; // FIXME: squeltch warning.
Zhongxing Xu60156f02008-11-08 03:45:42 +00001094
1095 // Report warnings ...
1096 }
1097 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001098
1099 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001100}
1101
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001102//===----------------------------------------------------------------------===//
1103// Transfer function: Function calls.
1104//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001105void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001106 CallExpr::arg_iterator AI,
1107 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001108 NodeSet& Dst)
1109{
1110 // Determine the type of function we're calling (if available).
1111 const FunctionTypeProto *Proto = NULL;
1112 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1113 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1114 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1115
1116 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1117}
1118
1119void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1120 CallExpr::arg_iterator AI,
1121 CallExpr::arg_iterator AE,
1122 NodeSet& Dst, const FunctionTypeProto *Proto,
1123 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001124
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001125 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001126 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001127 // If the call argument is being bound to a reference parameter,
1128 // visit it as an lvalue, not an rvalue.
1129 bool VisitAsLvalue = false;
1130 if (Proto && ParamIdx < Proto->getNumArgs())
1131 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1132
1133 NodeSet DstTmp;
1134 if (VisitAsLvalue)
1135 VisitLValue(*AI, Pred, DstTmp);
1136 else
1137 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001138 ++AI;
1139
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001140 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001141 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001142
1143 return;
1144 }
1145
1146 // If we reach here we have processed all of the arguments. Evaluate
1147 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001148
Ted Kremenek994a09b2008-02-25 21:16:03 +00001149 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001150 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001151
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001152 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001153
Ted Kremenekde434242008-02-19 01:44:53 +00001154 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001155 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1156
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001157 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001158 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001159
Ted Kremeneka1354a52008-03-03 16:47:31 +00001160 // FIXME: Add support for symbolic function calls (calls involving
1161 // function pointer values that are symbolic).
1162
1163 // Check for undefined control-flow or calls to NULL.
1164
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001165 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001166 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001167
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001168 if (N) {
1169 N->markAsSink();
1170 BadCalls.insert(N);
1171 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001172
Ted Kremenekde434242008-02-19 01:44:53 +00001173 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001174 }
1175
1176 // Check for the "noreturn" attribute.
1177
1178 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1179
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001180 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001181
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001182 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001183
1184 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001185 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001186 else {
1187 // HACK: Some functions are not marked noreturn, and don't return.
1188 // Here are a few hardwired ones. If this takes too long, we can
1189 // potentially cache these results.
1190 const char* s = FD->getIdentifier()->getName();
1191 unsigned n = strlen(s);
1192
1193 switch (n) {
1194 default:
1195 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001196
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001197 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001198 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1199 break;
1200
1201 case 5:
1202 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001203 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001204 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001205 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001206 // FIXME: use Assume to inspect the possible symbolic value of
1207 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001208 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001209 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001210 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001211 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001212 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001213 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001214
1215 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001216 if (!memcmp(s, "Assert", 6)) {
1217 Builder->BuildSinks = true;
1218 break;
1219 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001220
1221 // FIXME: This is just a wrapper around throwing an exception.
1222 // Eventually inter-procedural analysis should handle this easily.
1223 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1224
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001225 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001226
1227 case 7:
1228 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1229 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001230
Ted Kremenekf47bb782008-04-30 17:54:04 +00001231 case 8:
1232 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1233 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001234
1235 case 12:
1236 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1237 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001238
Ted Kremenekf9683082008-09-19 02:30:47 +00001239 case 13:
1240 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1241 break;
1242
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001243 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001244 if (!memcmp(s, "dtrace_assfail", 14) ||
1245 !memcmp(s, "yy_fatal_error", 14))
1246 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001247 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001248
1249 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001250 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1251 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001252 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001253
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001254 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001255 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001256
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001257 }
1258 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001259
1260 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001261
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001262 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001263
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001264 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001265
Ted Kremenek186350f2008-04-23 20:12:28 +00001266 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001267 switch (id) {
1268 case Builtin::BI__builtin_expect: {
1269 // For __builtin_expect, just return the value of the subexpression.
1270 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001271 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001272 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001273 continue;
1274 }
1275
Ted Kremenekb3021332008-11-02 00:35:01 +00001276 case Builtin::BI__builtin_alloca: {
1277 // FIXME: Handle size.
1278 // FIXME: Refactor into StoreManager itself?
1279 MemRegionManager& RM = getStateManager().getRegionManager();
1280 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001281 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Ted Kremenekb3021332008-11-02 00:35:01 +00001282 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1283 continue;
1284 }
1285
Ted Kremenek55aea312008-03-05 22:59:42 +00001286 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001287 break;
1288 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001289 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001290
Ted Kremenek186350f2008-04-23 20:12:28 +00001291 // Check any arguments passed-by-value against being undefined.
1292
1293 bool badArg = false;
1294
1295 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1296 I != E; ++I) {
1297
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001298 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001299 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001300
Ted Kremenek186350f2008-04-23 20:12:28 +00001301 if (N) {
1302 N->markAsSink();
1303 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001304 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001305
Ted Kremenek186350f2008-04-23 20:12:28 +00001306 badArg = true;
1307 break;
1308 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001309 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001310
1311 if (badArg)
1312 continue;
1313
1314 // Dispatch to the plug-in transfer function.
1315
1316 unsigned size = Dst.size();
1317 SaveOr OldHasGen(Builder->HasGeneratedNode);
1318 EvalCall(Dst, CE, L, *DI);
1319
1320 // Handle the case where no nodes where generated. Auto-generate that
1321 // contains the updated state if we aren't generating sinks.
1322
1323 if (!Builder->BuildSinks && Dst.size() == size &&
1324 !Builder->HasGeneratedNode)
1325 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001326 }
1327}
1328
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001329//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001330// Transfer function: Objective-C ivar references.
1331//===----------------------------------------------------------------------===//
1332
1333void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1334 NodeTy* Pred, NodeSet& Dst,
1335 bool asLValue) {
1336
1337 Expr* Base = cast<Expr>(Ex->getBase());
1338 NodeSet Tmp;
1339 Visit(Base, Pred, Tmp);
1340
1341 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1342 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001343 SVal BaseVal = GetSVal(St, Base);
1344 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001345
1346 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001347 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001348 else
1349 EvalLoad(Dst, Ex, *I, St, location);
1350 }
1351}
1352
1353//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001354// Transfer function: Objective-C fast enumeration 'for' statements.
1355//===----------------------------------------------------------------------===//
1356
1357void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1358 NodeTy* Pred, NodeSet& Dst) {
1359
1360 // ObjCForCollectionStmts are processed in two places. This method
1361 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1362 // statements within a basic block. This transfer function does two things:
1363 //
1364 // (1) binds the next container value to 'element'. This creates a new
1365 // node in the ExplodedGraph.
1366 //
1367 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1368 // whether or not the container has any more elements. This value
1369 // will be tested in ProcessBranch. We need to explicitly bind
1370 // this value because a container can contain nil elements.
1371 //
1372 // FIXME: Eventually this logic should actually do dispatches to
1373 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1374 // This will require simulating a temporary NSFastEnumerationState, either
1375 // through an SVal or through the use of MemRegions. This value can
1376 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1377 // terminates we reclaim the temporary (it goes out of scope) and we
1378 // we can test if the SVal is 0 or if the MemRegion is null (depending
1379 // on what approach we take).
1380 //
1381 // For now: simulate (1) by assigning either a symbol or nil if the
1382 // container is empty. Thus this transfer function will by default
1383 // result in state splitting.
1384
1385 Stmt* elem = S->getElement();
1386 VarDecl* ElemD;
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001387 bool newDecl = false;
Ted Kremenekaf337412008-11-12 19:24:17 +00001388
1389 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
1390 ElemD = cast<VarDecl>(DS->getSolitaryDecl());
1391 assert (ElemD->getInit() == 0);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001392 newDecl = true;
Ted Kremenekaf337412008-11-12 19:24:17 +00001393 }
1394 else
1395 ElemD = cast<VarDecl>(cast<DeclRefExpr>(elem)->getDecl());
1396
1397
1398 // Get the current state, as well as the QualType for 'int' and the
1399 // type of the element.
1400 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
1401 QualType IntTy = getContext().IntTy;
1402 QualType ElemTy = ElemD->getType();
1403
1404 // Handle the case where the container still has elements.
1405 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1406 GRStateRef hasElems = state.BindExpr(S, TrueV);
1407
1408 assert (Loc::IsLocType(ElemTy));
1409 unsigned Count = Builder->getCurrentBlockCount();
1410 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, ElemTy, Count));
1411
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001412 if (newDecl)
Ted Kremenekaf337412008-11-12 19:24:17 +00001413 hasElems = hasElems.BindDecl(ElemD, &SymV, Count);
1414 else
1415 hasElems = hasElems.BindLoc(hasElems.GetLValue(ElemD), SymV);
1416
Ted Kremenekaf337412008-11-12 19:24:17 +00001417 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001418 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1419 GRStateRef noElems = state.BindExpr(S, FalseV);
1420
1421 if (!newDecl) {
1422 // We only need to bind nil to an existing variable, since out of the
1423 // loop the VarDecl won't exist and there will be no chance to get it's
1424 // address via the '&' operator.
1425 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, ElemTy));
1426 noElems.BindLoc(noElems.GetLValue(ElemD), nilV);
1427 }
1428
1429 // Create the new nodes.
1430 MakeNode(Dst, S, Pred, hasElems);
1431 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001432}
1433
1434//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001435// Transfer function: Objective-C message expressions.
1436//===----------------------------------------------------------------------===//
1437
1438void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1439 NodeSet& Dst){
1440
1441 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1442 Pred, Dst);
1443}
1444
1445void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001446 ObjCMessageExpr::arg_iterator AI,
1447 ObjCMessageExpr::arg_iterator AE,
1448 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001449 if (AI == AE) {
1450
1451 // Process the receiver.
1452
1453 if (Expr* Receiver = ME->getReceiver()) {
1454 NodeSet Tmp;
1455 Visit(Receiver, Pred, Tmp);
1456
1457 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1458 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1459
1460 return;
1461 }
1462
1463 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1464 return;
1465 }
1466
1467 NodeSet Tmp;
1468 Visit(*AI, Pred, Tmp);
1469
1470 ++AI;
1471
1472 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1473 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1474}
1475
1476void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1477 NodeTy* Pred,
1478 NodeSet& Dst) {
1479
1480 // FIXME: More logic for the processing the method call.
1481
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001482 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001483 bool RaisesException = false;
1484
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001485
1486 if (Expr* Receiver = ME->getReceiver()) {
1487
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001488 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001489
1490 // Check for undefined control-flow or calls to NULL.
1491
1492 if (L.isUndef()) {
1493 NodeTy* N = Builder->generateNode(ME, St, Pred);
1494
1495 if (N) {
1496 N->markAsSink();
1497 UndefReceivers.insert(N);
1498 }
1499
1500 return;
1501 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001502
1503 // Check if the "raise" message was sent.
1504 if (ME->getSelector() == RaiseSel)
1505 RaisesException = true;
1506 }
1507 else {
1508
1509 IdentifierInfo* ClsName = ME->getClassName();
1510 Selector S = ME->getSelector();
1511
1512 // Check for special instance methods.
1513
1514 if (!NSExceptionII) {
1515 ASTContext& Ctx = getContext();
1516
1517 NSExceptionII = &Ctx.Idents.get("NSException");
1518 }
1519
1520 if (ClsName == NSExceptionII) {
1521
1522 enum { NUM_RAISE_SELECTORS = 2 };
1523
1524 // Lazily create a cache of the selectors.
1525
1526 if (!NSExceptionInstanceRaiseSelectors) {
1527
1528 ASTContext& Ctx = getContext();
1529
1530 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1531
1532 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1533 unsigned idx = 0;
1534
1535 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001536 II.push_back(&Ctx.Idents.get("raise"));
1537 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001538 NSExceptionInstanceRaiseSelectors[idx++] =
1539 Ctx.Selectors.getSelector(II.size(), &II[0]);
1540
1541 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001542 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001543 NSExceptionInstanceRaiseSelectors[idx++] =
1544 Ctx.Selectors.getSelector(II.size(), &II[0]);
1545 }
1546
1547 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1548 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1549 RaisesException = true; break;
1550 }
1551 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001552 }
1553
1554 // Check for any arguments that are uninitialized/undefined.
1555
1556 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1557 I != E; ++I) {
1558
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001559 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001560
1561 // Generate an error node for passing an uninitialized/undefined value
1562 // as an argument to a message expression. This node is a sink.
1563 NodeTy* N = Builder->generateNode(ME, St, Pred);
1564
1565 if (N) {
1566 N->markAsSink();
1567 MsgExprUndefArgs[N] = *I;
1568 }
1569
1570 return;
1571 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001572 }
1573
1574 // Check if we raise an exception. For now treat these as sinks. Eventually
1575 // we will want to handle exceptions properly.
1576
1577 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1578
1579 if (RaisesException)
1580 Builder->BuildSinks = true;
1581
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001582 // Dispatch to plug-in transfer function.
1583
1584 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001585 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001586
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001587 EvalObjCMessageExpr(Dst, ME, Pred);
1588
1589 // Handle the case where no nodes where generated. Auto-generate that
1590 // contains the updated state if we aren't generating sinks.
1591
Ted Kremenekb0533962008-04-18 20:35:30 +00001592 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001593 MakeNode(Dst, ME, Pred, St);
1594}
1595
1596//===----------------------------------------------------------------------===//
1597// Transfer functions: Miscellaneous statements.
1598//===----------------------------------------------------------------------===//
1599
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001600void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001601 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001602 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001603 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001604
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001605 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001606 T = ExCast->getTypeAsWritten();
1607
Zhongxing Xued340f72008-10-22 08:02:16 +00001608 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001609 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001610 else
1611 Visit(Ex, Pred, S1);
1612
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001613 // Check for casting to "void".
1614 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001615
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001616 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001617 Dst.Add(*I1);
1618
Ted Kremenek874d63f2008-01-24 02:02:54 +00001619 return;
1620 }
1621
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001622 // FIXME: The rest of this should probably just go into EvalCall, and
1623 // let the transfer function object be responsible for constructing
1624 // nodes.
1625
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001626 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001627 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001628 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001629 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001630
1631 // Unknown?
1632
1633 if (V.isUnknown()) {
1634 Dst.Add(N);
1635 continue;
1636 }
1637
1638 // Undefined?
1639
1640 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001641 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001642 continue;
1643 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001644
1645 // For const casts, just propagate the value.
1646 ASTContext& C = getContext();
1647
1648 if (C.getCanonicalType(T).getUnqualifiedType() ==
1649 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001650 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001651 continue;
1652 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001653
1654 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001655 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001656 unsigned bits = getContext().getTypeSize(ExTy);
1657
1658 // FIXME: Determine if the number of bits of the target type is
1659 // equal or exceeds the number of bits to store the pointer value.
1660 // If not, flag an error.
1661
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001662 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001663 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001664 continue;
1665 }
1666
1667 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001668 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1669 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001670 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001671 V = LV->getLoc();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001672 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001673 continue;
1674 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001675
Zhongxing Xu37d682a2008-11-14 09:23:38 +00001676 // Check for casts from array type to pointer type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001677 if (ExTy->isArrayType()) {
Ted Kremenek0b50f5b2008-10-24 21:10:49 +00001678 assert(T->isPointerType() || T->isReferenceType());
Zhongxing Xue564b522008-10-23 04:19:25 +00001679
Zhongxing Xue1911af2008-10-23 03:10:39 +00001680 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001681 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001682 continue;
1683 }
1684
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001685 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001686 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001687 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001688}
1689
Ted Kremenek4f090272008-10-27 21:54:31 +00001690void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001691 NodeTy* Pred, NodeSet& Dst,
1692 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001693 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1694 NodeSet Tmp;
1695 Visit(ILE, Pred, Tmp);
1696
1697 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001698 const GRState* St = GetState(*I);
1699 SVal ILV = GetSVal(St, ILE);
1700 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001701
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001702 if (asLValue)
1703 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1704 else
1705 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001706 }
1707}
1708
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001709void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001710
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001711 // The CFG has one DeclStmt per Decl.
1712 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001713
1714 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001715 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001716
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001717 const VarDecl* VD = dyn_cast<VarDecl>(D);
1718
Ted Kremenekaf337412008-11-12 19:24:17 +00001719 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001720
1721 // FIXME: static variables may have an initializer, but the second
1722 // time a function is called those values may not be current.
1723 NodeSet Tmp;
1724
Ted Kremenekaf337412008-11-12 19:24:17 +00001725 if (InitEx)
1726 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001727
1728 if (Tmp.empty())
1729 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001730
1731 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001732 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001733 unsigned Count = Builder->getCurrentBlockCount();
1734
1735 if (InitEx) {
1736 SVal InitVal = GetSVal(St, InitEx);
1737 QualType T = VD->getType();
1738
1739 // Recover some path-sensitivity if a scalar value evaluated to
1740 // UnknownVal.
1741 if (InitVal.isUnknown()) {
1742 if (Loc::IsLocType(T)) {
1743 SymbolID Sym = SymMgr.getConjuredSymbol(InitEx, Count);
1744 InitVal = loc::SymbolVal(Sym);
1745 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00001746 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenekaf337412008-11-12 19:24:17 +00001747 SymbolID Sym = SymMgr.getConjuredSymbol(InitEx, Count);
1748 InitVal = nonloc::SymbolVal(Sym);
1749 }
1750 }
1751
1752 St = StateMgr.BindDecl(St, VD, &InitVal, Count);
1753 }
1754 else
1755 St = StateMgr.BindDecl(St, VD, 0, Count);
1756
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001757 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001758 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001759}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001760
Ted Kremenekf75b1862008-10-30 17:47:32 +00001761namespace {
1762 // This class is used by VisitInitListExpr as an item in a worklist
1763 // for processing the values contained in an InitListExpr.
1764class VISIBILITY_HIDDEN InitListWLItem {
1765public:
1766 llvm::ImmutableList<SVal> Vals;
1767 GRExprEngine::NodeTy* N;
1768 InitListExpr::reverse_iterator Itr;
1769
1770 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1771 InitListExpr::reverse_iterator itr)
1772 : Vals(vals), N(n), Itr(itr) {}
1773};
1774}
1775
1776
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001777void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1778 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001779
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001780 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00001781 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00001782 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001783
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001784 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001785
Ted Kremeneka49e3672008-10-30 23:14:36 +00001786 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001787
Ted Kremeneka49e3672008-10-30 23:14:36 +00001788 // Handle base case where the initializer has no elements.
1789 // e.g: static int* myArray[] = {};
1790 if (NumInitElements == 0) {
1791 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1792 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1793 return;
1794 }
1795
1796 // Create a worklist to process the initializers.
1797 llvm::SmallVector<InitListWLItem, 10> WorkList;
1798 WorkList.reserve(NumInitElements);
1799 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001800 InitListExpr::reverse_iterator ItrEnd = E->rend();
1801
Ted Kremeneka49e3672008-10-30 23:14:36 +00001802 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001803 while (!WorkList.empty()) {
1804 InitListWLItem X = WorkList.back();
1805 WorkList.pop_back();
1806
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001807 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001808 Visit(*X.Itr, X.N, Tmp);
1809
1810 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001811
Ted Kremenekf75b1862008-10-30 17:47:32 +00001812 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1813 // Get the last initializer value.
1814 state = GetState(*NI);
1815 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1816
1817 // Construct the new list of values by prepending the new value to
1818 // the already constructed list.
1819 llvm::ImmutableList<SVal> NewVals =
1820 getBasicVals().consVals(InitV, X.Vals);
1821
1822 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001823 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001824 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001825
Ted Kremenekf75b1862008-10-30 17:47:32 +00001826 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001827 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001828 }
1829 else {
1830 // Still some initializer values to go. Push them onto the worklist.
1831 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1832 }
1833 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001834 }
Ted Kremenek87903072008-10-30 18:34:31 +00001835
1836 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001837 }
1838
Ted Kremenek062e2f92008-11-13 06:10:40 +00001839 if (T->isUnionType() || T->isVectorType()) {
1840 // FIXME: to be implemented.
1841 // Note: That vectors can return true for T->isIntegerType()
1842 MakeNode(Dst, E, Pred, state);
1843 return;
1844 }
1845
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001846 if (Loc::IsLocType(T) || T->isIntegerType()) {
1847 assert (E->getNumInits() == 1);
1848 NodeSet Tmp;
1849 Expr* Init = E->getInit(0);
1850 Visit(Init, Pred, Tmp);
1851 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1852 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001853 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001854 }
1855 return;
1856 }
1857
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001858
1859 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1860 assert(0 && "unprocessed InitListExpr type");
1861}
Ted Kremenekf233d482008-02-05 00:26:40 +00001862
Sebastian Redl05189992008-11-11 17:56:53 +00001863/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1864void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1865 NodeTy* Pred,
1866 NodeSet& Dst) {
1867 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001868 uint64_t amt;
1869
1870 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001871
Ted Kremenek87e80342008-03-15 03:13:20 +00001872 // FIXME: Add support for VLAs.
1873 if (!T.getTypePtr()->isConstantSizeType())
1874 return;
1875
Ted Kremenekf342d182008-04-30 21:31:12 +00001876 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1877 // the compiler has laid out its representation. Just report Unknown
1878 // for these.
1879 if (T->isObjCInterfaceType())
1880 return;
1881
Ted Kremenek87e80342008-03-15 03:13:20 +00001882 amt = 1; // Handle sizeof(void)
1883
1884 if (T != getContext().VoidTy)
1885 amt = getContext().getTypeSize(T) / 8;
1886
1887 }
1888 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001889 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001890
Ted Kremenek0e561a32008-03-21 21:30:14 +00001891 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001892 BindExpr(GetState(Pred), Ex,
1893 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001894}
1895
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001896
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001897void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001898 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001899
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001900 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001901
1902 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001903 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001904
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001905 case UnaryOperator::Deref: {
1906
1907 Expr* Ex = U->getSubExpr()->IgnoreParens();
1908 NodeSet Tmp;
1909 Visit(Ex, Pred, Tmp);
1910
1911 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001912
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001913 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001914 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001915
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001916 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001917 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001918 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001919 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001920 }
1921
1922 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001923 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001924
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001925 case UnaryOperator::Real: {
1926
1927 Expr* Ex = U->getSubExpr()->IgnoreParens();
1928 NodeSet Tmp;
1929 Visit(Ex, Pred, Tmp);
1930
1931 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1932
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001933 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001934 if (Ex->getType()->isAnyComplexType()) {
1935 // Just report "Unknown."
1936 Dst.Add(*I);
1937 continue;
1938 }
1939
1940 // For all other types, UnaryOperator::Real is an identity operation.
1941 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001942 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001943 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001944 }
1945
1946 return;
1947 }
1948
1949 case UnaryOperator::Imag: {
1950
1951 Expr* Ex = U->getSubExpr()->IgnoreParens();
1952 NodeSet Tmp;
1953 Visit(Ex, Pred, Tmp);
1954
1955 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001956 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001957 if (Ex->getType()->isAnyComplexType()) {
1958 // Just report "Unknown."
1959 Dst.Add(*I);
1960 continue;
1961 }
1962
1963 // For all other types, UnaryOperator::Float returns 0.
1964 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001965 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001966 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001967 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001968 }
1969
1970 return;
1971 }
1972
1973 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001974 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001975 Dst.Add(Pred);
1976 return;
1977
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001978 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001979 case UnaryOperator::Extension: {
1980
1981 // Unary "+" is a no-op, similar to a parentheses. We still have places
1982 // where it may be a block-level expression, so we need to
1983 // generate an extra node that just propagates the value of the
1984 // subexpression.
1985
1986 Expr* Ex = U->getSubExpr()->IgnoreParens();
1987 NodeSet Tmp;
1988 Visit(Ex, Pred, Tmp);
1989
1990 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001991 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001992 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001993 }
1994
1995 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001996 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001997
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001998 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001999
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002000 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002001 Expr* Ex = U->getSubExpr()->IgnoreParens();
2002 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002003 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002004
2005 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002006 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002007 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002008 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002009 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00002010 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002011
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002012 return;
2013 }
2014
2015 case UnaryOperator::LNot:
2016 case UnaryOperator::Minus:
2017 case UnaryOperator::Not: {
2018
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002019 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002020 Expr* Ex = U->getSubExpr()->IgnoreParens();
2021 NodeSet Tmp;
2022 Visit(Ex, Pred, Tmp);
2023
2024 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002025 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002026
2027 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002028 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002029
2030 // Perform promotions.
Ted Kremenek5a236cb2008-09-30 05:35:42 +00002031 // FIXME: This is the right thing to do, but it currently breaks
2032 // a bunch of tests.
2033 // V = EvalCast(V, U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002034
2035 if (V.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002036 MakeNode(Dst, U, *I, BindExpr(St, U, V));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002037 continue;
2038 }
2039
2040 switch (U->getOpcode()) {
2041 default:
2042 assert(false && "Invalid Opcode.");
2043 break;
2044
2045 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002046 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002047 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002048 break;
2049
2050 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002051 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002052 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002053 break;
2054
2055 case UnaryOperator::LNot:
2056
2057 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2058 //
2059 // Note: technically we do "E == 0", but this is the same in the
2060 // transfer functions as "0 == E".
2061
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002062 if (isa<Loc>(V)) {
2063 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2064 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002065 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002066 }
2067 else {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002068 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002069#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002070 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2071 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002072#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002073 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002074 continue;
2075#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002076 }
2077
2078 break;
2079 }
2080
2081 MakeNode(Dst, U, *I, St);
2082 }
2083
2084 return;
2085 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002086 }
2087
2088 // Handle ++ and -- (both pre- and post-increment).
2089
2090 assert (U->isIncrementDecrementOp());
2091 NodeSet Tmp;
2092 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002093 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002094
2095 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2096
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002097 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002098 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002099
2100 // Perform a load.
2101 NodeSet Tmp2;
2102 EvalLoad(Tmp2, Ex, *I, St, V1);
2103
2104 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2105
2106 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002107 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002108
2109 // Propagate unknown and undefined values.
2110 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002111 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002112 continue;
2113 }
2114
Ted Kremenek443003b2008-02-21 19:29:23 +00002115 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002116
2117 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2118 : BinaryOperator::Sub;
2119
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002120 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002121 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002122
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002123 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002124 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002125 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002126 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002127}
2128
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002129void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2130 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2131}
2132
2133void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2134 AsmStmt::outputs_iterator I,
2135 AsmStmt::outputs_iterator E,
2136 NodeTy* Pred, NodeSet& Dst) {
2137 if (I == E) {
2138 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2139 return;
2140 }
2141
2142 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002143 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002144
2145 ++I;
2146
2147 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2148 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2149}
2150
2151void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2152 AsmStmt::inputs_iterator I,
2153 AsmStmt::inputs_iterator E,
2154 NodeTy* Pred, NodeSet& Dst) {
2155 if (I == E) {
2156
2157 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002158 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002159
2160 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2161 // which interprets the inline asm and stores proper results in the
2162 // outputs.
2163
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002164 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002165
2166 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2167 OE = A->end_outputs(); OI != OE; ++OI) {
2168
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002169 SVal X = GetSVal(St, *OI);
2170 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002171
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002172 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002173 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002174 }
2175
Ted Kremenek0e561a32008-03-21 21:30:14 +00002176 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002177 return;
2178 }
2179
2180 NodeSet Tmp;
2181 Visit(*I, Pred, Tmp);
2182
2183 ++I;
2184
2185 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2186 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2187}
2188
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002189void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2190 assert (Builder && "GRStmtNodeBuilder must be defined.");
2191
2192 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002193
Ted Kremenek186350f2008-04-23 20:12:28 +00002194 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2195 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002196
Ted Kremenek729a9a22008-07-17 23:15:45 +00002197 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002198
Ted Kremenekb0533962008-04-18 20:35:30 +00002199 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002200
Ted Kremenekb0533962008-04-18 20:35:30 +00002201 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002202 MakeNode(Dst, S, Pred, GetState(Pred));
2203}
2204
Ted Kremenek02737ed2008-03-31 15:02:58 +00002205void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2206
2207 Expr* R = S->getRetValue();
2208
2209 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002210 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002211 return;
2212 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002213
2214 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00002215 QualType T = R->getType();
2216
Chris Lattner423a3c92008-04-02 17:45:06 +00002217 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002218
2219 // Check if any of the return values return the address of a stack variable.
2220
2221 NodeSet Tmp;
2222 Visit(R, Pred, Tmp);
2223
2224 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002225 SVal X = GetSVal((*I)->getState(), R);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002226
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002227 if (isa<loc::MemRegionVal>(X)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002228
Ted Kremenek9e240492008-10-04 05:50:14 +00002229 // Determine if the value is on the stack.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002230 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00002231
2232 if (R && getStateManager().hasStackStorage(R)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002233
2234 // Create a special node representing the v
2235
2236 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
2237
2238 if (RetStackNode) {
2239 RetStackNode->markAsSink();
2240 RetsStackAddr.insert(RetStackNode);
2241 }
2242
2243 continue;
2244 }
2245 }
2246
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002247 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002248 }
2249 }
2250 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002251 Visit(R, Pred, DstRet);
2252
2253 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2254 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002255}
Ted Kremenek55deb972008-03-25 00:34:37 +00002256
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002257//===----------------------------------------------------------------------===//
2258// Transfer functions: Binary operators.
2259//===----------------------------------------------------------------------===//
2260
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002261const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2262 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002263
2264 // Divide by undefined? (potentially zero)
2265
2266 if (Denom.isUndef()) {
2267 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2268
2269 if (DivUndef) {
2270 DivUndef->markAsSink();
2271 ExplicitBadDivides.insert(DivUndef);
2272 }
2273
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002274 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002275 }
2276
2277 // Check for divide/remainder-by-zero.
2278 // First, "assume" that the denominator is 0 or undefined.
2279
2280 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002281 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002282
2283 // Second, "assume" that the denominator cannot be 0.
2284
2285 bool isFeasibleNotZero = false;
2286 St = Assume(St, Denom, true, isFeasibleNotZero);
2287
2288 // Create the node for the divide-by-zero (if it occurred).
2289
2290 if (isFeasibleZero)
2291 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2292 DivZeroNode->markAsSink();
2293
2294 if (isFeasibleNotZero)
2295 ImplicitBadDivides.insert(DivZeroNode);
2296 else
2297 ExplicitBadDivides.insert(DivZeroNode);
2298
2299 }
2300
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002301 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002302}
2303
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002304void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002305 GRExprEngine::NodeTy* Pred,
2306 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002307
2308 NodeSet Tmp1;
2309 Expr* LHS = B->getLHS()->IgnoreParens();
2310 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002311
2312 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002313 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002314 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002315 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002316
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002317 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002318
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002319 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002320
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002321 // Process the RHS.
2322
2323 NodeSet Tmp2;
2324 Visit(RHS, *I1, Tmp2);
2325
2326 // With both the LHS and RHS evaluated, process the operation itself.
2327
2328 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002329
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002330 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002331 const GRState* OldSt = St;
2332
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002333 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002334 BinaryOperator::Opcode Op = B->getOpcode();
2335
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002336 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002337
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002338 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002339
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002340 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002341 // FIXME: Handle structs.
2342 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002343
Ted Kremenek062e2f92008-11-13 06:10:40 +00002344 if (RightV.isUnknown() && (Loc::IsLocType(T) ||
2345 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002346 unsigned Count = Builder->getCurrentBlockCount();
2347 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2348
Ted Kremenek062e2f92008-11-13 06:10:40 +00002349 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002350 ? cast<SVal>(loc::SymbolVal(Sym))
2351 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002352 }
2353
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002354 // Simulate the effects of a "store": bind the value of the RHS
2355 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002356
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002357 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002358 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002359 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002360
2361 case BinaryOperator::Div:
2362 case BinaryOperator::Rem:
2363
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002364 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002365 if (RHS->getType()->isIntegerType() &&
2366 RHS->getType()->isScalarType()) {
2367
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002368 St = CheckDivideZero(B, St, *I2, RightV);
2369 if (!St) continue;
2370 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002371
2372 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002373
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002374 default: {
2375
2376 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002377 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002378
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002379 // Process non-assignements except commas or short-circuited
2380 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002381
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002382 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002383
2384 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002385 if (OldSt != St) {
2386 // Generate a new node if we have already created a new state.
2387 MakeNode(Dst, B, *I2, St);
2388 }
2389 else
2390 Dst.Add(*I2);
2391
Ted Kremenek89063af2008-02-21 19:15:37 +00002392 continue;
2393 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002394
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002395 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002396
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002397 // The operands were *not* undefined, but the result is undefined.
2398 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002399
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002400 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002401 UndefNode->markAsSink();
2402 UndefResults.insert(UndefNode);
2403 }
2404
2405 continue;
2406 }
2407
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002408 // Otherwise, create a new node.
2409
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002410 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002411 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002412 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002413 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002414
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002415 assert (B->isCompoundAssignmentOp());
2416
Ted Kremenek934e3e92008-10-27 23:02:39 +00002417 if (Op >= BinaryOperator::AndAssign) {
2418 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2419 BinaryOperator::And));
2420 }
2421 else {
2422 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2423 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002424
2425 // Perform a load (the LHS). This performs the checks for
2426 // null dereferences, and so on.
2427 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002428 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002429 EvalLoad(Tmp3, LHS, *I2, St, location);
2430
2431 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2432
2433 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002434 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002435
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002436 // Check for divide-by-zero.
2437 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002438 && RHS->getType()->isIntegerType()
2439 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002440
2441 // CheckDivideZero returns a new state where the denominator
2442 // is assumed to be non-zero.
2443 St = CheckDivideZero(B, St, *I3, RightV);
2444
2445 if (!St)
2446 continue;
2447 }
2448
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002449 // Propagate undefined values (left-side).
2450 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002451 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002452 continue;
2453 }
2454
2455 // Propagate unknown values (left and right-side).
2456 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002457 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002458 UnknownVal());
2459 continue;
2460 }
2461
2462 // At this point:
2463 //
2464 // The LHS is not Undef/Unknown.
2465 // The RHS is not Unknown.
2466
2467 // Get the computation type.
2468 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2469
2470 // Perform promotions.
2471 V = EvalCast(V, CTy);
2472 RightV = EvalCast(RightV, CTy);
2473
2474 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002475 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002476 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002477 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002478 continue;
2479 }
2480
2481 // Compute the result of the operation.
2482
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002483 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002484
2485 if (Result.isUndef()) {
2486
2487 // The operands were not undefined, but the result is undefined.
2488
2489 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2490 UndefNode->markAsSink();
2491 UndefResults.insert(UndefNode);
2492 }
2493
2494 continue;
2495 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002496
2497 // EXPERIMENTAL: "Conjured" symbols.
2498 // FIXME: Handle structs.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002499 if (Result.isUnknown() && (Loc::IsLocType(CTy) ||
2500 (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002501
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002502 unsigned Count = Builder->getCurrentBlockCount();
2503 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2504
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002505 Result = Loc::IsLocType(CTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002506 ? cast<SVal>(loc::SymbolVal(Sym))
2507 : cast<SVal>(nonloc::SymbolVal(Sym));
2508 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002509
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002510 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002511 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002512 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002513 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002514}
Ted Kremenekee985462008-01-16 18:18:48 +00002515
2516//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002517// Transfer-function Helpers.
2518//===----------------------------------------------------------------------===//
2519
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002520void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002521 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002522 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002523 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002524
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002525 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002526 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2527
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002528 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002529 MakeNode(Dst, Ex, Pred, *I);
2530}
2531
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002532void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002533 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002534 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002535
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002536 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002537 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002538}
2539
2540//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002541// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002542//===----------------------------------------------------------------------===//
2543
Ted Kremenekaa66a322008-01-16 21:46:15 +00002544#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002545static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002546static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002547
Ted Kremenekaa66a322008-01-16 21:46:15 +00002548namespace llvm {
2549template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002550struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002551 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002552
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002553 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2554
2555 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002556 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002557 GraphPrintCheckerState->isUndefDeref(N) ||
2558 GraphPrintCheckerState->isUndefStore(N) ||
2559 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002560 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2561 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002562 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002563 GraphPrintCheckerState->isBadCall(N) ||
2564 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002565 return "color=\"red\",style=\"filled\"";
2566
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002567 if (GraphPrintCheckerState->isNoReturnCall(N))
2568 return "color=\"blue\",style=\"filled\"";
2569
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002570 return "";
2571 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002572
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002573 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002574 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002575
2576 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002577 ProgramPoint Loc = N->getLocation();
2578
2579 switch (Loc.getKind()) {
2580 case ProgramPoint::BlockEntranceKind:
2581 Out << "Block Entrance: B"
2582 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2583 break;
2584
2585 case ProgramPoint::BlockExitKind:
2586 assert (false);
2587 break;
2588
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002589 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002590 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002591 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002592 const PostStmt& L = cast<PostStmt>(Loc);
2593 Stmt* S = L.getStmt();
2594 SourceLocation SLoc = S->getLocStart();
2595
2596 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002597 llvm::raw_os_ostream OutS(Out);
2598 S->printPretty(OutS);
2599 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002600
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002601 if (SLoc.isFileID()) {
2602 Out << "\\lline="
2603 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002604 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002605 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002606
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002607 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002608 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002609 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002610 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002611 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002612 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002613 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002614 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002615 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2616 Out << "\\|Explicit divide-by zero or undefined value.";
2617 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2618 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002619 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002620 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002621 else if (GraphPrintCheckerState->isNoReturnCall(N))
2622 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002623 else if (GraphPrintCheckerState->isBadCall(N))
2624 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002625 else if (GraphPrintCheckerState->isUndefArg(N))
2626 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002627
Ted Kremenekaa66a322008-01-16 21:46:15 +00002628 break;
2629 }
2630
2631 default: {
2632 const BlockEdge& E = cast<BlockEdge>(Loc);
2633 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2634 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002635
2636 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002637
2638 SourceLocation SLoc = T->getLocStart();
2639
Ted Kremenekb38911f2008-01-30 23:03:39 +00002640 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002641
Ted Kremeneka95d3752008-09-13 05:16:45 +00002642 llvm::raw_os_ostream OutS(Out);
2643 E.getSrc()->printTerminator(OutS);
2644 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002645
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002646 if (SLoc.isFileID()) {
2647 Out << "\\lline="
2648 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2649 << GraphPrintSourceManager->getColumnNumber(SLoc);
2650 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002651
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002652 if (isa<SwitchStmt>(T)) {
2653 Stmt* Label = E.getDst()->getLabel();
2654
2655 if (Label) {
2656 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2657 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002658 llvm::raw_os_ostream OutS(Out);
2659 C->getLHS()->printPretty(OutS);
2660 OutS.flush();
2661
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002662 if (Stmt* RHS = C->getRHS()) {
2663 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002664 RHS->printPretty(OutS);
2665 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002666 }
2667
2668 Out << ":";
2669 }
2670 else {
2671 assert (isa<DefaultStmt>(Label));
2672 Out << "\\ldefault:";
2673 }
2674 }
2675 else
2676 Out << "\\l(implicit) default:";
2677 }
2678 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002679 // FIXME
2680 }
2681 else {
2682 Out << "\\lCondition: ";
2683 if (*E.getSrc()->succ_begin() == E.getDst())
2684 Out << "true";
2685 else
2686 Out << "false";
2687 }
2688
2689 Out << "\\l";
2690 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002691
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002692 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2693 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002694 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002695 }
2696 }
2697
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002698 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002699
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002700 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2701 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002702
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002703 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002704 return Out.str();
2705 }
2706};
2707} // end llvm namespace
2708#endif
2709
Ted Kremenekffe0f432008-03-07 22:58:01 +00002710#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002711
2712template <typename ITERATOR>
2713GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2714
2715template <>
2716GRExprEngine::NodeTy*
2717GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2718 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2719 return I->first;
2720}
2721
Ted Kremenekffe0f432008-03-07 22:58:01 +00002722template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002723static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002724 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002725
Ted Kremenekd4527582008-09-16 18:44:52 +00002726 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002727
2728 for ( ; I != E; ++I ) {
2729 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002730 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002731
Ted Kremenekd4527582008-09-16 18:44:52 +00002732 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002733 continue;
2734
Ted Kremenekd4527582008-09-16 18:44:52 +00002735 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002736 Sources.push_back(N);
2737 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002738}
2739#endif
2740
2741void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002742#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002743 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002744 std::vector<NodeTy*> Src;
2745
2746 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002747 AddSources(Src, null_derefs_begin(), null_derefs_end());
2748 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2749 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2750 AddSources(Src, undef_results_begin(), undef_results_end());
2751 AddSources(Src, bad_calls_begin(), bad_calls_end());
2752 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002753 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002754
Ted Kremenekcb612922008-04-18 19:23:43 +00002755 // The new way.
2756 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2757 (*I)->GetErrorNodes(Src);
2758
2759
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002760 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002761 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002762 else {
2763 GraphPrintCheckerState = this;
2764 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002765
Ted Kremenekffe0f432008-03-07 22:58:01 +00002766 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002767
2768 GraphPrintCheckerState = NULL;
2769 GraphPrintSourceManager = NULL;
2770 }
2771#endif
2772}
2773
2774void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2775#ifndef NDEBUG
2776 GraphPrintCheckerState = this;
2777 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002778
Ted Kremenek493d7a22008-03-11 18:25:33 +00002779 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2780
2781 if (!TrimmedG)
2782 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2783 else {
2784 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2785 delete TrimmedG;
2786 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002787
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002788 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002789 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002790#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002791}