blob: 2fa163ad230ea8f399fadeeaf08bba91d8a804c5 [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
314 case Stmt::CallExprClass: {
315 CallExpr* C = cast<CallExpr>(S);
316 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
317 break;
318 }
319
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000320 // FIXME: ChooseExpr is really a constant. We need to fix
321 // the CFG do not model them as explicit control-flow.
322
323 case Stmt::ChooseExprClass: { // __builtin_choose_expr
324 ChooseExpr* C = cast<ChooseExpr>(S);
325 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
326 break;
327 }
328
329 case Stmt::CompoundAssignOperatorClass:
330 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
331 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000332
333 case Stmt::CompoundLiteralExprClass:
334 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
335 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000336
337 case Stmt::ConditionalOperatorClass: { // '?' operator
338 ConditionalOperator* C = cast<ConditionalOperator>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::DeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000344 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000345 break;
346
347 case Stmt::DeclStmtClass:
348 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
349 break;
350
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000351 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000352 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000353 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000354 VisitCast(C, C->getSubExpr(), Pred, Dst);
355 break;
356 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000357
358 case Stmt::InitListExprClass:
359 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
360 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000361
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000362 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000363 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
364 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000365
366 case Stmt::ObjCIvarRefExprClass:
367 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
368 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000369
370 case Stmt::ObjCForCollectionStmtClass:
371 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
372 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000373
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000374 case Stmt::ObjCMessageExprClass: {
375 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
376 break;
377 }
378
379 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000380 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000381 break;
382
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000383 case Stmt::ReturnStmtClass:
384 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
385 break;
386
Sebastian Redl05189992008-11-11 17:56:53 +0000387 case Stmt::SizeOfAlignOfExprClass:
388 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000389 break;
390
391 case Stmt::StmtExprClass: {
392 StmtExpr* SE = cast<StmtExpr>(S);
393
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000394 const GRState* St = GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000395
396 // FIXME: Not certain if we can have empty StmtExprs. If so, we should
397 // probably just remove these from the CFG.
398 assert (!SE->getSubStmt()->body_empty());
399
400 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000401 MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000402 else
403 Dst.Add(Pred);
404
405 break;
406 }
407
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000408 case Stmt::UnaryOperatorClass:
409 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000410 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000411 }
412}
413
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000414void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000415
416 Ex = Ex->IgnoreParens();
417
418 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
419 Dst.Add(Pred);
420 return;
421 }
422
423 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000424
425 case Stmt::ArraySubscriptExprClass:
426 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
427 return;
428
429 case Stmt::DeclRefExprClass:
430 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
431 return;
432
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000433 case Stmt::ObjCIvarRefExprClass:
434 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
435 return;
436
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000437 case Stmt::UnaryOperatorClass:
438 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
439 return;
440
441 case Stmt::MemberExprClass:
442 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
443 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000444
Ted Kremenek4f090272008-10-27 21:54:31 +0000445 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000446 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000447 return;
448
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000449 case Stmt::ObjCPropertyRefExprClass:
450 // FIXME: Property assignments are lvalues, but not really "locations".
451 // e.g.: self.x = something;
452 // Here the "self.x" really can translate to a method call (setter) when
453 // the assignment is made. Moreover, the entire assignment expression
454 // evaluate to whatever "something" is, not calling the "getter" for
455 // the property (which would make sense since it can have side effects).
456 // We'll probably treat this as a location, but not one that we can
457 // take the address of. Perhaps we need a new SVal class for cases
458 // like thsis?
459 // Note that we have a similar problem for bitfields, since they don't
460 // have "locations" in the sense that we can take their address.
461 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000462 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000463
464 case Stmt::StringLiteralClass: {
465 const GRState* St = GetState(Pred);
466 SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000467 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000468 return;
469 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000470
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000471 default:
472 // Arbitrary subexpressions can return aggregate temporaries that
473 // can be used in a lvalue context. We need to enhance our support
474 // of such temporaries in both the environment and the store, so right
475 // now we just do a regular visit.
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000476 assert ((Ex->getType()->isAggregateType() ||
477 Ex->getType()->isUnionType()) &&
478 "Other kinds of expressions with non-aggregate/union types do"
479 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000480
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000481 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000482 }
483}
484
485//===----------------------------------------------------------------------===//
486// Block entrance. (Update counters).
487//===----------------------------------------------------------------------===//
488
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000489bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000490 GRBlockCounter BC) {
491
492 return BC.getNumVisited(B->getBlockID()) < 3;
493}
494
495//===----------------------------------------------------------------------===//
496// Branch processing.
497//===----------------------------------------------------------------------===//
498
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000499const GRState* GRExprEngine::MarkBranch(const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +0000500 Stmt* Terminator,
501 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000502
503 switch (Terminator->getStmtClass()) {
504 default:
505 return St;
506
507 case Stmt::BinaryOperatorClass: { // '&&' and '||'
508
509 BinaryOperator* B = cast<BinaryOperator>(Terminator);
510 BinaryOperator::Opcode Op = B->getOpcode();
511
512 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
513
514 // For &&, if we take the true branch, then the value of the whole
515 // expression is that of the RHS expression.
516 //
517 // For ||, if we take the false branch, then the value of the whole
518 // expression is that of the RHS expression.
519
520 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
521 (Op == BinaryOperator::LOr && !branchTaken)
522 ? B->getRHS() : B->getLHS();
523
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000524 return BindBlkExpr(St, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000525 }
526
527 case Stmt::ConditionalOperatorClass: { // ?:
528
529 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
530
531 // For ?, if branchTaken == true then the value is either the LHS or
532 // the condition itself. (GNU extension).
533
534 Expr* Ex;
535
536 if (branchTaken)
537 Ex = C->getLHS() ? C->getLHS() : C->getCond();
538 else
539 Ex = C->getRHS();
540
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000541 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000542 }
543
544 case Stmt::ChooseExprClass: { // ?:
545
546 ChooseExpr* C = cast<ChooseExpr>(Terminator);
547
548 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000549 return BindBlkExpr(St, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000550 }
551 }
552}
553
Ted Kremenekaf337412008-11-12 19:24:17 +0000554void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000555 BranchNodeBuilder& builder) {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000556
Ted Kremeneke7d22112008-02-11 19:21:59 +0000557 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000558 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000559 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000560
Ted Kremenekb2331832008-02-15 22:29:00 +0000561 // Check for NULL conditions; e.g. "for(;;)"
562 if (!Condition) {
563 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000564 return;
565 }
566
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000567 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000568
569 switch (V.getBaseKind()) {
570 default:
571 break;
572
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000573 case SVal::UnknownKind:
Ted Kremenek58b33212008-02-26 19:40:44 +0000574 builder.generateNode(MarkBranch(PrevState, Term, true), true);
575 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000576 return;
577
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000578 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000579 NodeTy* N = builder.generateNode(PrevState, true);
580
581 if (N) {
582 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000583 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000584 }
585
586 builder.markInfeasible(false);
587 return;
588 }
589 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000590
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000591 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000592
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000593 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000594 const GRState* St = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000595
596 if (isFeasible)
597 builder.generateNode(MarkBranch(St, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000598 else
599 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000600
601 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000602
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000603 isFeasible = false;
604 St = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000605
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000606 if (isFeasible)
607 builder.generateNode(MarkBranch(St, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000608 else
609 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000610}
611
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000612/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000613/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000614void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000615
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000616 const GRState* St = builder.getState();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000617 SVal V = GetSVal(St, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000618
619 // Three possibilities:
620 //
621 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000622 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000623 // (3) We have no clue about the label. Dispatch to all targets.
624 //
625
626 typedef IndirectGotoNodeBuilder::iterator iterator;
627
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000628 if (isa<loc::GotoLabel>(V)) {
629 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000630
631 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000632 if (I.getLabel() == L) {
633 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000634 return;
635 }
636 }
637
638 assert (false && "No block with label.");
639 return;
640 }
641
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000642 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000643 // Dispatch to the first target and mark it as a sink.
Ted Kremenek24f1a962008-02-13 17:27:37 +0000644 NodeTy* N = builder.generateNode(builder.begin(), St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000645 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000646 return;
647 }
648
649 // This is really a catch-all. We don't support symbolics yet.
650
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000651 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000652
653 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek24f1a962008-02-13 17:27:37 +0000654 builder.generateNode(I, St);
Ted Kremenek754607e2008-02-13 00:24:44 +0000655}
Ted Kremenekf233d482008-02-05 00:26:40 +0000656
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000657
658void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
659 NodeTy* Pred, NodeSet& Dst) {
660
661 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
662
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000663 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000664 SVal X = GetBlkExprSVal(St, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000665
666 assert (X.isUndef());
667
668 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
669
670 assert (SE);
671
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000672 X = GetBlkExprSVal(St, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000673
674 // Make sure that we invalidate the previous binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000675 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000676}
677
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000678/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
679/// nodes by processing the 'effects' of a switch statement.
680void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
681
682 typedef SwitchNodeBuilder::iterator iterator;
683
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000684 const GRState* St = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000685 Expr* CondE = builder.getCondition();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000686 SVal CondV = GetSVal(St, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000687
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000688 if (CondV.isUndef()) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000689 NodeTy* N = builder.generateDefaultCaseNode(St, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000690 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000691 return;
692 }
693
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000694 const GRState* DefaultSt = St;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000695
696 // While most of this can be assumed (such as the signedness), having it
697 // just computed makes sure everything makes the same assumptions end-to-end.
Ted Kremenek692416c2008-02-18 22:57:02 +0000698
Chris Lattner98be4942008-03-05 18:54:05 +0000699 unsigned bits = getContext().getTypeSize(CondE->getType());
Ted Kremenek692416c2008-02-18 22:57:02 +0000700
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000701 APSInt V1(bits, false);
702 APSInt V2 = V1;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000703 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000704
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000705 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000706
707 CaseStmt* Case = cast<CaseStmt>(I.getCase());
708
709 // Evaluate the case.
710 if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
711 assert (false && "Case condition must evaluate to an integer constant.");
712 return;
713 }
714
715 // Get the RHS of the case, if it exists.
716
717 if (Expr* E = Case->getRHS()) {
718 if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
719 assert (false &&
720 "Case condition (RHS) must evaluate to an integer constant.");
721 return ;
722 }
723
724 assert (V1 <= V2);
725 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000726 else
727 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000728
729 // FIXME: Eventually we should replace the logic below with a range
730 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000731 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000732
Ted Kremenek14a11402008-03-17 22:17:56 +0000733 do {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000734 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000735
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000736 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000737
738 // Now "assume" that the case matches.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000739
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000740 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000741 const GRState* StNew = Assume(St, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000742
743 if (isFeasible) {
744 builder.generateCaseStmtNode(I, StNew);
745
746 // If CondV evaluates to a constant, then we know that this
747 // is the *only* case that we can take, so stop evaluating the
748 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000749 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000750 return;
751 }
752
753 // Now "assume" that the case doesn't match. Add this state
754 // to the default state (if it is feasible).
755
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000756 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000757 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000758
Ted Kremenek5014ab12008-04-23 05:03:18 +0000759 if (isFeasible) {
760 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000761 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000762 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000763
Ted Kremenek14a11402008-03-17 22:17:56 +0000764 // Concretize the next value in the range.
765 if (V1 == V2)
766 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000767
Ted Kremenek14a11402008-03-17 22:17:56 +0000768 ++V1;
Ted Kremenek58cda6f2008-03-17 22:18:22 +0000769 assert (V1 <= V2);
Ted Kremenek14a11402008-03-17 22:17:56 +0000770
771 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000772 }
773
774 // If we reach here, than we know that the default branch is
775 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000776 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777}
778
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000779//===----------------------------------------------------------------------===//
780// Transfer functions: logical operations ('&&', '||').
781//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000782
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000783void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000784 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000785
Ted Kremenek05a23782008-02-26 19:05:15 +0000786 assert (B->getOpcode() == BinaryOperator::LAnd ||
787 B->getOpcode() == BinaryOperator::LOr);
788
789 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
790
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000791 const GRState* St = GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000792 SVal X = GetBlkExprSVal(St, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000793
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000794 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000795
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000796 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000797
798 assert (Ex);
799
800 if (Ex == B->getRHS()) {
801
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000802 X = GetBlkExprSVal(St, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000803
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000804 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000805
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000806 if (X.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000807 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000808 return;
809 }
810
Ted Kremenek05a23782008-02-26 19:05:15 +0000811 // We took the RHS. Because the value of the '&&' or '||' expression must
812 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
813 // or 1. Alternatively, we could take a lazy approach, and calculate this
814 // value later when necessary. We don't have the machinery in place for
815 // this right now, and since most logical expressions are used for branches,
816 // the payoff is not likely to be large. Instead, we do eager evaluation.
817
818 bool isFeasible = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000819 const GRState* NewState = Assume(St, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000820
821 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000822 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000823 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000824
825 isFeasible = false;
826 NewState = Assume(St, X, false, isFeasible);
827
828 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000829 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000830 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000831 }
832 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000833 // We took the LHS expression. Depending on whether we are '&&' or
834 // '||' we know what the value of the expression is via properties of
835 // the short-circuiting.
836
837 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000838 MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000839 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000840}
Ted Kremenek05a23782008-02-26 19:05:15 +0000841
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000842//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000843// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000844//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000845
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000846void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
847 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000848
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000849 const GRState* St = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000850
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000851 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000852
853 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
854
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000855 SVal V = StateMgr.GetLValue(St, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000856
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000857 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000858 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000859 else
860 EvalLoad(Dst, Ex, Pred, St, V);
861 return;
862
863 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
864 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
865
866 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000867 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000868 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000869 return;
870
871 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenekcd162cc2008-10-17 00:55:33 +0000872 // FIXME: Does this need to be revised? We were getting cases in
873 // real code that did this.
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000874
Ted Kremeneke580c1b2008-10-31 15:33:11 +0000875 // FIXME: This is not a valid assertion. Produce a test case that
876 // refutes it.
877 // assert(asLValue); // Can we assume this?
Zhongxing Xuda6b9992008-10-31 06:05:32 +0000878
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000879 SVal V = loc::FuncVal(FD);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000880 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000881 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000882 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000883
884 assert (false &&
885 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000886}
887
Ted Kremenek540cbe22008-04-22 04:56:29 +0000888/// VisitArraySubscriptExpr - Transfer function for array accesses
889void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000890 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000891
892 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000893 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000894 NodeSet Tmp;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000895 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000896
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000897 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000898 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000899 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000900
901 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000902 const GRState* St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000903 SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000904
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000905 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000906 MakeNode(Dst, A, *I2, BindExpr(St, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000907 else
908 EvalLoad(Dst, A, *I2, St, V);
909 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000910 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000911}
912
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000913/// VisitMemberExpr - Transfer function for member expressions.
914void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000915 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000916
917 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000918 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000919
920 if (M->isArrow())
921 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
922 else
923 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
924
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000925 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000926 const GRState* St = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000927 // FIXME: Should we insert some assumption logic in here to determine
928 // if "Base" is a valid piece of memory? Before we put this assumption
929 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000930 SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000931
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000932 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000933 MakeNode(Dst, M, *I, BindExpr(St, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000934 else
935 EvalLoad(Dst, M, *I, St, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000936 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000937}
938
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000939void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000940 const GRState* St, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000941
942 assert (Builder && "GRStmtNodeBuilder must be defined.");
943
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000944 // Evaluate the location (checks for bad dereferences).
945 St = EvalLocation(Ex, Pred, St, location);
946
947 if (!St)
948 return;
949
950 // Proceed with the store.
951
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000952 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +0000953
Ted Kremenek186350f2008-04-23 20:12:28 +0000954 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000955 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek186350f2008-04-23 20:12:28 +0000956 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +0000957
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000958 assert (!location.isUndef());
Ted Kremenek82bae3f2008-09-20 01:50:34 +0000959 Builder->PointKind = ProgramPoint::PostStoreKind;
Ted Kremenek13922612008-04-16 20:40:59 +0000960
Ted Kremenek729a9a22008-07-17 23:15:45 +0000961 getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000962
963 // Handle the case where no nodes where generated. Auto-generate that
964 // contains the updated state if we aren't generating sinks.
965
Ted Kremenekb0533962008-04-18 20:35:30 +0000966 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek729a9a22008-07-17 23:15:45 +0000967 getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000968 location, Val);
969}
970
971void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000972 const GRState* St, SVal location,
Ted Kremenek4323a572008-07-10 22:03:41 +0000973 bool CheckOnly) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000974
975 // Evaluate the location (checks for bad dereferences).
976
977 St = EvalLocation(Ex, Pred, St, location, true);
978
979 if (!St)
980 return;
981
982 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +0000983 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000984
985 // FIXME: Currently symbolic analysis "generates" new symbols
986 // for the contents of values. We need a better approach.
987
988 // FIXME: The "CheckOnly" option exists only because Array and Field
989 // loads aren't fully implemented. Eventually this option will go away.
Ted Kremenek982e6742008-08-28 18:43:46 +0000990
Ted Kremenek436f2b92008-04-30 04:23:07 +0000991 if (CheckOnly)
Ted Kremenek982e6742008-08-28 18:43:46 +0000992 MakeNode(Dst, Ex, Pred, St, K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000993 else if (location.isUnknown()) {
994 // This is important. We must nuke the old binding.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000995 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +0000996 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000997 else
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000998 MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, GetSVal(St, cast<Loc>(location),
999 Ex->getType())), K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001000}
1001
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001002void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001003 const GRState* St, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001004
1005 NodeSet TmpDst;
1006 EvalStore(TmpDst, StoreE, Pred, St, location, Val);
1007
1008 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1009 MakeNode(Dst, Ex, *I, (*I)->getState());
1010}
1011
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001012const GRState* GRExprEngine::EvalLocation(Expr* Ex, NodeTy* Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001013 const GRState* St,
1014 SVal location, bool isLoad) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001015
1016 // Check for loads/stores from/to undefined values.
1017 if (location.isUndef()) {
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001018 ProgramPoint::Kind K =
1019 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1020
1021 if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001022 Succ->markAsSink();
1023 UndefDeref.insert(Succ);
1024 }
1025
1026 return NULL;
1027 }
1028
1029 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1030 if (location.isUnknown())
1031 return St;
1032
1033 // During a load, one of two possible situations arise:
1034 // (1) A crash, because the location (pointer) was NULL.
1035 // (2) The location (pointer) is not NULL, and the dereference works.
1036 //
1037 // We add these assumptions.
1038
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001039 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001040
1041 // "Assume" that the pointer is not NULL.
1042
1043 bool isFeasibleNotNull = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001044 const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001045
1046 // "Assume" that the pointer is NULL.
1047
1048 bool isFeasibleNull = false;
Ted Kremenek7360fda2008-09-18 23:09:54 +00001049 GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
1050 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001051
1052 if (isFeasibleNull) {
1053
Ted Kremenek7360fda2008-09-18 23:09:54 +00001054 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001055 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001056 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1057
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001058 // We don't use "MakeNode" here because the node will be a sink
1059 // and we have no intention of processing it later.
1060
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001061 ProgramPoint::Kind K =
1062 isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
1063
1064 NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001065
1066 if (NullNode) {
1067
1068 NullNode->markAsSink();
1069
1070 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1071 else ExplicitNullDeref.insert(NullNode);
1072 }
1073 }
Zhongxing Xu60156f02008-11-08 03:45:42 +00001074
1075 // Check for out-of-bound array access.
1076 if (isFeasibleNotNull && isa<loc::MemRegionVal>(LV)) {
1077 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1078 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1079 // Get the index of the accessed element.
1080 SVal Idx = ER->getIndex();
1081 // Get the extent of the array.
1082 SVal NumElements = StateMgr.getStoreManager().getSizeInElements(StNotNull,
1083 ER->getSuperRegion());
1084
1085 bool isFeasibleInBound = false;
1086 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1087 true, isFeasibleInBound);
1088
1089 bool isFeasibleOutBound = false;
1090 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1091 false, isFeasibleOutBound);
Chris Lattner8341be72008-11-10 03:00:37 +00001092 StInBound = StOutBound = 0; // FIXME: squeltch warning.
Zhongxing Xu60156f02008-11-08 03:45:42 +00001093
1094 // Report warnings ...
1095 }
1096 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001097
1098 return isFeasibleNotNull ? StNotNull : NULL;
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001099}
1100
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001101//===----------------------------------------------------------------------===//
1102// Transfer function: Function calls.
1103//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001104void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001105 CallExpr::arg_iterator AI,
1106 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001107 NodeSet& Dst)
1108{
1109 // Determine the type of function we're calling (if available).
1110 const FunctionTypeProto *Proto = NULL;
1111 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1112 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
1113 Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
1114
1115 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1116}
1117
1118void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1119 CallExpr::arg_iterator AI,
1120 CallExpr::arg_iterator AE,
1121 NodeSet& Dst, const FunctionTypeProto *Proto,
1122 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001123
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001124 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001125 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001126 // If the call argument is being bound to a reference parameter,
1127 // visit it as an lvalue, not an rvalue.
1128 bool VisitAsLvalue = false;
1129 if (Proto && ParamIdx < Proto->getNumArgs())
1130 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1131
1132 NodeSet DstTmp;
1133 if (VisitAsLvalue)
1134 VisitLValue(*AI, Pred, DstTmp);
1135 else
1136 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001137 ++AI;
1138
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001139 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001140 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001141
1142 return;
1143 }
1144
1145 // If we reach here we have processed all of the arguments. Evaluate
1146 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001147
Ted Kremenek994a09b2008-02-25 21:16:03 +00001148 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001149 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001150
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001151 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001152
Ted Kremenekde434242008-02-19 01:44:53 +00001153 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001154 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1155
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001156 const GRState* St = GetState(*DI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001157 SVal L = GetSVal(St, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001158
Ted Kremeneka1354a52008-03-03 16:47:31 +00001159 // FIXME: Add support for symbolic function calls (calls involving
1160 // function pointer values that are symbolic).
1161
1162 // Check for undefined control-flow or calls to NULL.
1163
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001164 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremenekde434242008-02-19 01:44:53 +00001165 NodeTy* N = Builder->generateNode(CE, St, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001166
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001167 if (N) {
1168 N->markAsSink();
1169 BadCalls.insert(N);
1170 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001171
Ted Kremenekde434242008-02-19 01:44:53 +00001172 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001173 }
1174
1175 // Check for the "noreturn" attribute.
1176
1177 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1178
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001179 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001180
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001181 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001182
1183 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001184 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001185 else {
1186 // HACK: Some functions are not marked noreturn, and don't return.
1187 // Here are a few hardwired ones. If this takes too long, we can
1188 // potentially cache these results.
1189 const char* s = FD->getIdentifier()->getName();
1190 unsigned n = strlen(s);
1191
1192 switch (n) {
1193 default:
1194 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001195
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001196 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001197 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1198 break;
1199
1200 case 5:
1201 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001202 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001203 if (CE->getNumArgs() > 0) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001204 SVal X = GetSVal(St, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001205 // FIXME: use Assume to inspect the possible symbolic value of
1206 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001207 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001208 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001209 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001210 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001211 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001212 break;
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001213
1214 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001215 if (!memcmp(s, "Assert", 6)) {
1216 Builder->BuildSinks = true;
1217 break;
1218 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001219
1220 // FIXME: This is just a wrapper around throwing an exception.
1221 // Eventually inter-procedural analysis should handle this easily.
1222 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1223
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001224 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001225
1226 case 7:
1227 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1228 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001229
Ted Kremenekf47bb782008-04-30 17:54:04 +00001230 case 8:
1231 if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
1232 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001233
1234 case 12:
1235 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1236 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001237
Ted Kremenekf9683082008-09-19 02:30:47 +00001238 case 13:
1239 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1240 break;
1241
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001242 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001243 if (!memcmp(s, "dtrace_assfail", 14) ||
1244 !memcmp(s, "yy_fatal_error", 14))
1245 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001246 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001247
1248 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001249 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
1250 !memcmp(s, "_DTAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001251 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001252
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001253 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001254 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001255
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001256 }
1257 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001258
1259 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001260
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001261 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001262
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001263 IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001264
Ted Kremenek186350f2008-04-23 20:12:28 +00001265 if (unsigned id = Info->getBuiltinID())
Ted Kremenek55aea312008-03-05 22:59:42 +00001266 switch (id) {
1267 case Builtin::BI__builtin_expect: {
1268 // For __builtin_expect, just return the value of the subexpression.
1269 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001270 SVal X = GetSVal(St, *(CE->arg_begin()));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001271 MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001272 continue;
1273 }
1274
Ted Kremenekb3021332008-11-02 00:35:01 +00001275 case Builtin::BI__builtin_alloca: {
1276 // FIXME: Handle size.
1277 // FIXME: Refactor into StoreManager itself?
1278 MemRegionManager& RM = getStateManager().getRegionManager();
1279 const MemRegion* R =
1280 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
1281 MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
1282 continue;
1283 }
1284
Ted Kremenek55aea312008-03-05 22:59:42 +00001285 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001286 break;
1287 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001288 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001289
Ted Kremenek186350f2008-04-23 20:12:28 +00001290 // Check any arguments passed-by-value against being undefined.
1291
1292 bool badArg = false;
1293
1294 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1295 I != E; ++I) {
1296
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001297 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001298 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001299
Ted Kremenek186350f2008-04-23 20:12:28 +00001300 if (N) {
1301 N->markAsSink();
1302 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001303 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001304
Ted Kremenek186350f2008-04-23 20:12:28 +00001305 badArg = true;
1306 break;
1307 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001308 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001309
1310 if (badArg)
1311 continue;
1312
1313 // Dispatch to the plug-in transfer function.
1314
1315 unsigned size = Dst.size();
1316 SaveOr OldHasGen(Builder->HasGeneratedNode);
1317 EvalCall(Dst, CE, L, *DI);
1318
1319 // Handle the case where no nodes where generated. Auto-generate that
1320 // contains the updated state if we aren't generating sinks.
1321
1322 if (!Builder->BuildSinks && Dst.size() == size &&
1323 !Builder->HasGeneratedNode)
1324 MakeNode(Dst, CE, *DI, St);
Ted Kremenekde434242008-02-19 01:44:53 +00001325 }
1326}
1327
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001328//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001329// Transfer function: Objective-C ivar references.
1330//===----------------------------------------------------------------------===//
1331
1332void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1333 NodeTy* Pred, NodeSet& Dst,
1334 bool asLValue) {
1335
1336 Expr* Base = cast<Expr>(Ex->getBase());
1337 NodeSet Tmp;
1338 Visit(Base, Pred, Tmp);
1339
1340 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1341 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001342 SVal BaseVal = GetSVal(St, Base);
1343 SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001344
1345 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001346 MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001347 else
1348 EvalLoad(Dst, Ex, *I, St, location);
1349 }
1350}
1351
1352//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001353// Transfer function: Objective-C fast enumeration 'for' statements.
1354//===----------------------------------------------------------------------===//
1355
1356void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1357 NodeTy* Pred, NodeSet& Dst) {
1358
1359 // ObjCForCollectionStmts are processed in two places. This method
1360 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1361 // statements within a basic block. This transfer function does two things:
1362 //
1363 // (1) binds the next container value to 'element'. This creates a new
1364 // node in the ExplodedGraph.
1365 //
1366 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1367 // whether or not the container has any more elements. This value
1368 // will be tested in ProcessBranch. We need to explicitly bind
1369 // this value because a container can contain nil elements.
1370 //
1371 // FIXME: Eventually this logic should actually do dispatches to
1372 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1373 // This will require simulating a temporary NSFastEnumerationState, either
1374 // through an SVal or through the use of MemRegions. This value can
1375 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1376 // terminates we reclaim the temporary (it goes out of scope) and we
1377 // we can test if the SVal is 0 or if the MemRegion is null (depending
1378 // on what approach we take).
1379 //
1380 // For now: simulate (1) by assigning either a symbol or nil if the
1381 // container is empty. Thus this transfer function will by default
1382 // result in state splitting.
1383
1384 Stmt* elem = S->getElement();
1385 VarDecl* ElemD;
1386 bool bindDecl = false;
1387
1388 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
1389 ElemD = cast<VarDecl>(DS->getSolitaryDecl());
1390 assert (ElemD->getInit() == 0);
1391 bindDecl = true;
1392 }
1393 else
1394 ElemD = cast<VarDecl>(cast<DeclRefExpr>(elem)->getDecl());
1395
1396
1397 // Get the current state, as well as the QualType for 'int' and the
1398 // type of the element.
1399 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
1400 QualType IntTy = getContext().IntTy;
1401 QualType ElemTy = ElemD->getType();
1402
1403 // Handle the case where the container still has elements.
1404 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1405 GRStateRef hasElems = state.BindExpr(S, TrueV);
1406
1407 assert (Loc::IsLocType(ElemTy));
1408 unsigned Count = Builder->getCurrentBlockCount();
1409 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, ElemTy, Count));
1410
1411 if (bindDecl)
1412 hasElems = hasElems.BindDecl(ElemD, &SymV, Count);
1413 else
1414 hasElems = hasElems.BindLoc(hasElems.GetLValue(ElemD), SymV);
1415
1416
1417 // Handle the case where the container has no elements.
1418
1419
1420
1421
1422
1423}
1424
1425//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001426// Transfer function: Objective-C message expressions.
1427//===----------------------------------------------------------------------===//
1428
1429void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1430 NodeSet& Dst){
1431
1432 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1433 Pred, Dst);
1434}
1435
1436void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001437 ObjCMessageExpr::arg_iterator AI,
1438 ObjCMessageExpr::arg_iterator AE,
1439 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001440 if (AI == AE) {
1441
1442 // Process the receiver.
1443
1444 if (Expr* Receiver = ME->getReceiver()) {
1445 NodeSet Tmp;
1446 Visit(Receiver, Pred, Tmp);
1447
1448 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1449 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1450
1451 return;
1452 }
1453
1454 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1455 return;
1456 }
1457
1458 NodeSet Tmp;
1459 Visit(*AI, Pred, Tmp);
1460
1461 ++AI;
1462
1463 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1464 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1465}
1466
1467void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1468 NodeTy* Pred,
1469 NodeSet& Dst) {
1470
1471 // FIXME: More logic for the processing the method call.
1472
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001473 const GRState* St = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001474 bool RaisesException = false;
1475
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001476
1477 if (Expr* Receiver = ME->getReceiver()) {
1478
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001479 SVal L = GetSVal(St, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001480
1481 // Check for undefined control-flow or calls to NULL.
1482
1483 if (L.isUndef()) {
1484 NodeTy* N = Builder->generateNode(ME, St, Pred);
1485
1486 if (N) {
1487 N->markAsSink();
1488 UndefReceivers.insert(N);
1489 }
1490
1491 return;
1492 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001493
1494 // Check if the "raise" message was sent.
1495 if (ME->getSelector() == RaiseSel)
1496 RaisesException = true;
1497 }
1498 else {
1499
1500 IdentifierInfo* ClsName = ME->getClassName();
1501 Selector S = ME->getSelector();
1502
1503 // Check for special instance methods.
1504
1505 if (!NSExceptionII) {
1506 ASTContext& Ctx = getContext();
1507
1508 NSExceptionII = &Ctx.Idents.get("NSException");
1509 }
1510
1511 if (ClsName == NSExceptionII) {
1512
1513 enum { NUM_RAISE_SELECTORS = 2 };
1514
1515 // Lazily create a cache of the selectors.
1516
1517 if (!NSExceptionInstanceRaiseSelectors) {
1518
1519 ASTContext& Ctx = getContext();
1520
1521 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1522
1523 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1524 unsigned idx = 0;
1525
1526 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001527 II.push_back(&Ctx.Idents.get("raise"));
1528 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001529 NSExceptionInstanceRaiseSelectors[idx++] =
1530 Ctx.Selectors.getSelector(II.size(), &II[0]);
1531
1532 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001533 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001534 NSExceptionInstanceRaiseSelectors[idx++] =
1535 Ctx.Selectors.getSelector(II.size(), &II[0]);
1536 }
1537
1538 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1539 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1540 RaisesException = true; break;
1541 }
1542 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001543 }
1544
1545 // Check for any arguments that are uninitialized/undefined.
1546
1547 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1548 I != E; ++I) {
1549
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001550 if (GetSVal(St, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001551
1552 // Generate an error node for passing an uninitialized/undefined value
1553 // as an argument to a message expression. This node is a sink.
1554 NodeTy* N = Builder->generateNode(ME, St, Pred);
1555
1556 if (N) {
1557 N->markAsSink();
1558 MsgExprUndefArgs[N] = *I;
1559 }
1560
1561 return;
1562 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001563 }
1564
1565 // Check if we raise an exception. For now treat these as sinks. Eventually
1566 // we will want to handle exceptions properly.
1567
1568 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1569
1570 if (RaisesException)
1571 Builder->BuildSinks = true;
1572
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001573 // Dispatch to plug-in transfer function.
1574
1575 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001576 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001577
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001578 EvalObjCMessageExpr(Dst, ME, Pred);
1579
1580 // Handle the case where no nodes where generated. Auto-generate that
1581 // contains the updated state if we aren't generating sinks.
1582
Ted Kremenekb0533962008-04-18 20:35:30 +00001583 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001584 MakeNode(Dst, ME, Pred, St);
1585}
1586
1587//===----------------------------------------------------------------------===//
1588// Transfer functions: Miscellaneous statements.
1589//===----------------------------------------------------------------------===//
1590
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001591void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001592 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001593 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001594 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001595
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001596 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001597 T = ExCast->getTypeAsWritten();
1598
Zhongxing Xued340f72008-10-22 08:02:16 +00001599 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001600 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001601 else
1602 Visit(Ex, Pred, S1);
1603
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001604 // Check for casting to "void".
1605 if (T->isVoidType()) {
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001606
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001607 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001608 Dst.Add(*I1);
1609
Ted Kremenek874d63f2008-01-24 02:02:54 +00001610 return;
1611 }
1612
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001613 // FIXME: The rest of this should probably just go into EvalCall, and
1614 // let the transfer function object be responsible for constructing
1615 // nodes.
1616
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001617 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001618 NodeTy* N = *I1;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001619 const GRState* St = GetState(N);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001620 SVal V = GetSVal(St, Ex);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001621
1622 // Unknown?
1623
1624 if (V.isUnknown()) {
1625 Dst.Add(N);
1626 continue;
1627 }
1628
1629 // Undefined?
1630
1631 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001632 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001633 continue;
1634 }
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001635
1636 // For const casts, just propagate the value.
1637 ASTContext& C = getContext();
1638
1639 if (C.getCanonicalType(T).getUnqualifiedType() ==
1640 C.getCanonicalType(ExTy).getUnqualifiedType()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001641 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001642 continue;
1643 }
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001644
1645 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001646 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001647 unsigned bits = getContext().getTypeSize(ExTy);
1648
1649 // FIXME: Determine if the number of bits of the target type is
1650 // equal or exceeds the number of bits to store the pointer value.
1651 // If not, flag an error.
1652
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001653 V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001654 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001655 continue;
1656 }
1657
1658 // Check for casts from integers to pointers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001659 if (Loc::IsLocType(T) && ExTy->isIntegerType())
1660 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001661 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001662 V = LV->getLoc();
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 }
Zhongxing Xue1911af2008-10-23 03:10:39 +00001666
1667 // StoreManager casts array to different values.
1668 if (ExTy->isArrayType()) {
Ted Kremenek0b50f5b2008-10-24 21:10:49 +00001669 assert(T->isPointerType() || T->isReferenceType());
Zhongxing Xue564b522008-10-23 04:19:25 +00001670
Zhongxing Xue1911af2008-10-23 03:10:39 +00001671 V = StateMgr.ArrayToPointer(V);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001672 MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
Zhongxing Xue1911af2008-10-23 03:10:39 +00001673 continue;
1674 }
1675
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001676 // All other cases.
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001677 MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
Ted Kremenek874d63f2008-01-24 02:02:54 +00001678 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001679}
1680
Ted Kremenek4f090272008-10-27 21:54:31 +00001681void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001682 NodeTy* Pred, NodeSet& Dst,
1683 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001684 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1685 NodeSet Tmp;
1686 Visit(ILE, Pred, Tmp);
1687
1688 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001689 const GRState* St = GetState(*I);
1690 SVal ILV = GetSVal(St, ILE);
1691 St = StateMgr.BindCompoundLiteral(St, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001692
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001693 if (asLValue)
1694 MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
1695 else
1696 MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001697 }
1698}
1699
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001700void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001701
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001702 // The CFG has one DeclStmt per Decl.
1703 ScopedDecl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001704
1705 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001706 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001707
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001708 const VarDecl* VD = dyn_cast<VarDecl>(D);
1709
Ted Kremenekaf337412008-11-12 19:24:17 +00001710 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001711
1712 // FIXME: static variables may have an initializer, but the second
1713 // time a function is called those values may not be current.
1714 NodeSet Tmp;
1715
Ted Kremenekaf337412008-11-12 19:24:17 +00001716 if (InitEx)
1717 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001718
1719 if (Tmp.empty())
1720 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001721
1722 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001723 const GRState* St = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00001724 unsigned Count = Builder->getCurrentBlockCount();
1725
1726 if (InitEx) {
1727 SVal InitVal = GetSVal(St, InitEx);
1728 QualType T = VD->getType();
1729
1730 // Recover some path-sensitivity if a scalar value evaluated to
1731 // UnknownVal.
1732 if (InitVal.isUnknown()) {
1733 if (Loc::IsLocType(T)) {
1734 SymbolID Sym = SymMgr.getConjuredSymbol(InitEx, Count);
1735 InitVal = loc::SymbolVal(Sym);
1736 }
1737 else if (T->isIntegerType()) {
1738 SymbolID Sym = SymMgr.getConjuredSymbol(InitEx, Count);
1739 InitVal = nonloc::SymbolVal(Sym);
1740 }
1741 }
1742
1743 St = StateMgr.BindDecl(St, VD, &InitVal, Count);
1744 }
1745 else
1746 St = StateMgr.BindDecl(St, VD, 0, Count);
1747
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001748 MakeNode(Dst, DS, *I, St);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001749 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001750}
Ted Kremenek874d63f2008-01-24 02:02:54 +00001751
Ted Kremenekf75b1862008-10-30 17:47:32 +00001752namespace {
1753 // This class is used by VisitInitListExpr as an item in a worklist
1754 // for processing the values contained in an InitListExpr.
1755class VISIBILITY_HIDDEN InitListWLItem {
1756public:
1757 llvm::ImmutableList<SVal> Vals;
1758 GRExprEngine::NodeTy* N;
1759 InitListExpr::reverse_iterator Itr;
1760
1761 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
1762 InitListExpr::reverse_iterator itr)
1763 : Vals(vals), N(n), Itr(itr) {}
1764};
1765}
1766
1767
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001768void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
1769 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00001770
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001771 const GRState* state = GetState(Pred);
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001772 QualType T = E->getType();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001773 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001774
Zhongxing Xu05d1c572008-10-30 05:35:59 +00001775 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00001776
Ted Kremeneka49e3672008-10-30 23:14:36 +00001777 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00001778
Ted Kremeneka49e3672008-10-30 23:14:36 +00001779 // Handle base case where the initializer has no elements.
1780 // e.g: static int* myArray[] = {};
1781 if (NumInitElements == 0) {
1782 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
1783 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
1784 return;
1785 }
1786
1787 // Create a worklist to process the initializers.
1788 llvm::SmallVector<InitListWLItem, 10> WorkList;
1789 WorkList.reserve(NumInitElements);
1790 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001791 InitListExpr::reverse_iterator ItrEnd = E->rend();
1792
Ted Kremeneka49e3672008-10-30 23:14:36 +00001793 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001794 while (!WorkList.empty()) {
1795 InitListWLItem X = WorkList.back();
1796 WorkList.pop_back();
1797
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001798 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00001799 Visit(*X.Itr, X.N, Tmp);
1800
1801 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001802
Ted Kremenekf75b1862008-10-30 17:47:32 +00001803 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1804 // Get the last initializer value.
1805 state = GetState(*NI);
1806 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
1807
1808 // Construct the new list of values by prepending the new value to
1809 // the already constructed list.
1810 llvm::ImmutableList<SVal> NewVals =
1811 getBasicVals().consVals(InitV, X.Vals);
1812
1813 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00001814 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00001815 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001816
Ted Kremenekf75b1862008-10-30 17:47:32 +00001817 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00001818 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00001819 }
1820 else {
1821 // Still some initializer values to go. Push them onto the worklist.
1822 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
1823 }
1824 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001825 }
Ted Kremenek87903072008-10-30 18:34:31 +00001826
1827 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001828 }
1829
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001830 if (Loc::IsLocType(T) || T->isIntegerType()) {
1831 assert (E->getNumInits() == 1);
1832 NodeSet Tmp;
1833 Expr* Init = E->getInit(0);
1834 Visit(Init, Pred, Tmp);
1835 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
1836 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001837 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00001838 }
1839 return;
1840 }
1841
1842 if (T->isUnionType()) {
1843 // FIXME: to be implemented.
1844 MakeNode(Dst, E, Pred, state);
1845 return;
1846 }
1847
1848 printf("InitListExpr type = %s\n", T.getAsString().c_str());
1849 assert(0 && "unprocessed InitListExpr type");
1850}
Ted Kremenekf233d482008-02-05 00:26:40 +00001851
Sebastian Redl05189992008-11-11 17:56:53 +00001852/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
1853void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
1854 NodeTy* Pred,
1855 NodeSet& Dst) {
1856 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00001857 uint64_t amt;
1858
1859 if (Ex->isSizeOf()) {
Ted Kremenek9ef1ec92008-02-21 18:43:30 +00001860
Ted Kremenek87e80342008-03-15 03:13:20 +00001861 // FIXME: Add support for VLAs.
1862 if (!T.getTypePtr()->isConstantSizeType())
1863 return;
1864
Ted Kremenekf342d182008-04-30 21:31:12 +00001865 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
1866 // the compiler has laid out its representation. Just report Unknown
1867 // for these.
1868 if (T->isObjCInterfaceType())
1869 return;
1870
Ted Kremenek87e80342008-03-15 03:13:20 +00001871 amt = 1; // Handle sizeof(void)
1872
1873 if (T != getContext().VoidTy)
1874 amt = getContext().getTypeSize(T) / 8;
1875
1876 }
1877 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00001878 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001879
Ted Kremenek0e561a32008-03-21 21:30:14 +00001880 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001881 BindExpr(GetState(Pred), Ex,
1882 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00001883}
1884
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001885
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001886void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001887 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001888
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001889 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001890
1891 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001892 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001893
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001894 case UnaryOperator::Deref: {
1895
1896 Expr* Ex = U->getSubExpr()->IgnoreParens();
1897 NodeSet Tmp;
1898 Visit(Ex, Pred, Tmp);
1899
1900 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001901
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001902 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001903 SVal location = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001904
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001905 if (asLValue)
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001906 MakeNode(Dst, U, *I, BindExpr(St, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001907 else
Ted Kremenek5c96c272008-05-21 15:48:33 +00001908 EvalLoad(Dst, U, *I, St, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001909 }
1910
1911 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001912 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00001913
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001914 case UnaryOperator::Real: {
1915
1916 Expr* Ex = U->getSubExpr()->IgnoreParens();
1917 NodeSet Tmp;
1918 Visit(Ex, Pred, Tmp);
1919
1920 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
1921
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001922 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001923 if (Ex->getType()->isAnyComplexType()) {
1924 // Just report "Unknown."
1925 Dst.Add(*I);
1926 continue;
1927 }
1928
1929 // For all other types, UnaryOperator::Real is an identity operation.
1930 assert (U->getType() == Ex->getType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001931 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001932 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001933 }
1934
1935 return;
1936 }
1937
1938 case UnaryOperator::Imag: {
1939
1940 Expr* Ex = U->getSubExpr()->IgnoreParens();
1941 NodeSet Tmp;
1942 Visit(Ex, Pred, Tmp);
1943
1944 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001945 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001946 if (Ex->getType()->isAnyComplexType()) {
1947 // Just report "Unknown."
1948 Dst.Add(*I);
1949 continue;
1950 }
1951
1952 // For all other types, UnaryOperator::Float returns 0.
1953 assert (Ex->getType()->isIntegerType());
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001954 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001955 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001956 MakeNode(Dst, U, *I, BindExpr(St, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00001957 }
1958
1959 return;
1960 }
1961
1962 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00001963 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00001964 Dst.Add(Pred);
1965 return;
1966
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001967 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001968 case UnaryOperator::Extension: {
1969
1970 // Unary "+" is a no-op, similar to a parentheses. We still have places
1971 // where it may be a block-level expression, so we need to
1972 // generate an extra node that just propagates the value of the
1973 // subexpression.
1974
1975 Expr* Ex = U->getSubExpr()->IgnoreParens();
1976 NodeSet Tmp;
1977 Visit(Ex, Pred, Tmp);
1978
1979 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001980 const GRState* St = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001981 MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001982 }
1983
1984 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001985 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00001986
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001987 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00001988
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001989 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001990 Expr* Ex = U->getSubExpr()->IgnoreParens();
1991 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001992 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001993
1994 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001995 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001996 SVal V = GetSVal(St, Ex);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00001997 St = BindExpr(St, U, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001998 MakeNode(Dst, U, *I, St);
Ted Kremenek89063af2008-02-21 19:15:37 +00001999 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002000
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002001 return;
2002 }
2003
2004 case UnaryOperator::LNot:
2005 case UnaryOperator::Minus:
2006 case UnaryOperator::Not: {
2007
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002008 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002009 Expr* Ex = U->getSubExpr()->IgnoreParens();
2010 NodeSet Tmp;
2011 Visit(Ex, Pred, Tmp);
2012
2013 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002014 const GRState* St = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002015
2016 // Get the value of the subexpression.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002017 SVal V = GetSVal(St, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002018
2019 // Perform promotions.
Ted Kremenek5a236cb2008-09-30 05:35:42 +00002020 // FIXME: This is the right thing to do, but it currently breaks
2021 // a bunch of tests.
2022 // V = EvalCast(V, U->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002023
2024 if (V.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002025 MakeNode(Dst, U, *I, BindExpr(St, U, V));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002026 continue;
2027 }
2028
2029 switch (U->getOpcode()) {
2030 default:
2031 assert(false && "Invalid Opcode.");
2032 break;
2033
2034 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002035 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002036 St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002037 break;
2038
2039 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002040 // FIXME: Do we need to handle promotions?
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002041 St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002042 break;
2043
2044 case UnaryOperator::LNot:
2045
2046 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2047 //
2048 // Note: technically we do "E == 0", but this is the same in the
2049 // transfer functions as "0 == E".
2050
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002051 if (isa<Loc>(V)) {
2052 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2053 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002054 St = BindExpr(St, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002055 }
2056 else {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002057 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002058#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002059 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
2060 St = SetSVal(St, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002061#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002062 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002063 continue;
2064#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002065 }
2066
2067 break;
2068 }
2069
2070 MakeNode(Dst, U, *I, St);
2071 }
2072
2073 return;
2074 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002075 }
2076
2077 // Handle ++ and -- (both pre- and post-increment).
2078
2079 assert (U->isIncrementDecrementOp());
2080 NodeSet Tmp;
2081 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002082 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002083
2084 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2085
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002086 const GRState* St = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002087 SVal V1 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002088
2089 // Perform a load.
2090 NodeSet Tmp2;
2091 EvalLoad(Tmp2, Ex, *I, St, V1);
2092
2093 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2094
2095 St = GetState(*I2);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002096 SVal V2 = GetSVal(St, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002097
2098 // Propagate unknown and undefined values.
2099 if (V2.isUnknownOrUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002100 MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002101 continue;
2102 }
2103
Ted Kremenek443003b2008-02-21 19:29:23 +00002104 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002105
2106 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2107 : BinaryOperator::Sub;
2108
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002109 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002110 St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002111
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002112 // Perform the store.
Ted Kremenek436f2b92008-04-30 04:23:07 +00002113 EvalStore(Dst, U, *I2, St, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002114 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002115 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002116}
2117
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002118void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2119 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2120}
2121
2122void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2123 AsmStmt::outputs_iterator I,
2124 AsmStmt::outputs_iterator E,
2125 NodeTy* Pred, NodeSet& Dst) {
2126 if (I == E) {
2127 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2128 return;
2129 }
2130
2131 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002132 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002133
2134 ++I;
2135
2136 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2137 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2138}
2139
2140void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2141 AsmStmt::inputs_iterator I,
2142 AsmStmt::inputs_iterator E,
2143 NodeTy* Pred, NodeSet& Dst) {
2144 if (I == E) {
2145
2146 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002147 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002148
2149 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2150 // which interprets the inline asm and stores proper results in the
2151 // outputs.
2152
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002153 const GRState* St = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002154
2155 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2156 OE = A->end_outputs(); OI != OE; ++OI) {
2157
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002158 SVal X = GetSVal(St, *OI);
2159 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002160
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002161 if (isa<Loc>(X))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002162 St = BindLoc(St, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002163 }
2164
Ted Kremenek0e561a32008-03-21 21:30:14 +00002165 MakeNode(Dst, A, Pred, St);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002166 return;
2167 }
2168
2169 NodeSet Tmp;
2170 Visit(*I, Pred, Tmp);
2171
2172 ++I;
2173
2174 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2175 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2176}
2177
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002178void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2179 assert (Builder && "GRStmtNodeBuilder must be defined.");
2180
2181 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002182
Ted Kremenek186350f2008-04-23 20:12:28 +00002183 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2184 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002185
Ted Kremenek729a9a22008-07-17 23:15:45 +00002186 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002187
Ted Kremenekb0533962008-04-18 20:35:30 +00002188 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002189
Ted Kremenekb0533962008-04-18 20:35:30 +00002190 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002191 MakeNode(Dst, S, Pred, GetState(Pred));
2192}
2193
Ted Kremenek02737ed2008-03-31 15:02:58 +00002194void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2195
2196 Expr* R = S->getRetValue();
2197
2198 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002199 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002200 return;
2201 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002202
2203 NodeSet DstRet;
Ted Kremenek02737ed2008-03-31 15:02:58 +00002204 QualType T = R->getType();
2205
Chris Lattner423a3c92008-04-02 17:45:06 +00002206 if (T->isPointerLikeType()) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002207
2208 // Check if any of the return values return the address of a stack variable.
2209
2210 NodeSet Tmp;
2211 Visit(R, Pred, Tmp);
2212
2213 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002214 SVal X = GetSVal((*I)->getState(), R);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002215
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002216 if (isa<loc::MemRegionVal>(X)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002217
Ted Kremenek9e240492008-10-04 05:50:14 +00002218 // Determine if the value is on the stack.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002219 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00002220
2221 if (R && getStateManager().hasStackStorage(R)) {
Ted Kremenek02737ed2008-03-31 15:02:58 +00002222
2223 // Create a special node representing the v
2224
2225 NodeTy* RetStackNode = Builder->generateNode(S, GetState(*I), *I);
2226
2227 if (RetStackNode) {
2228 RetStackNode->markAsSink();
2229 RetsStackAddr.insert(RetStackNode);
2230 }
2231
2232 continue;
2233 }
2234 }
2235
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002236 DstRet.Add(*I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002237 }
2238 }
2239 else
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002240 Visit(R, Pred, DstRet);
2241
2242 for (NodeSet::iterator I=DstRet.begin(), E=DstRet.end(); I!=E; ++I)
2243 EvalReturn(Dst, S, *I);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002244}
Ted Kremenek55deb972008-03-25 00:34:37 +00002245
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002246//===----------------------------------------------------------------------===//
2247// Transfer functions: Binary operators.
2248//===----------------------------------------------------------------------===//
2249
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002250const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
2251 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002252
2253 // Divide by undefined? (potentially zero)
2254
2255 if (Denom.isUndef()) {
2256 NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
2257
2258 if (DivUndef) {
2259 DivUndef->markAsSink();
2260 ExplicitBadDivides.insert(DivUndef);
2261 }
2262
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002263 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002264 }
2265
2266 // Check for divide/remainder-by-zero.
2267 // First, "assume" that the denominator is 0 or undefined.
2268
2269 bool isFeasibleZero = false;
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002270 const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002271
2272 // Second, "assume" that the denominator cannot be 0.
2273
2274 bool isFeasibleNotZero = false;
2275 St = Assume(St, Denom, true, isFeasibleNotZero);
2276
2277 // Create the node for the divide-by-zero (if it occurred).
2278
2279 if (isFeasibleZero)
2280 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2281 DivZeroNode->markAsSink();
2282
2283 if (isFeasibleNotZero)
2284 ImplicitBadDivides.insert(DivZeroNode);
2285 else
2286 ExplicitBadDivides.insert(DivZeroNode);
2287
2288 }
2289
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002290 return isFeasibleNotZero ? St : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002291}
2292
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002293void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002294 GRExprEngine::NodeTy* Pred,
2295 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002296
2297 NodeSet Tmp1;
2298 Expr* LHS = B->getLHS()->IgnoreParens();
2299 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002300
2301 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002302 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002303 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002304 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002305
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002306 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002307
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002308 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002309
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002310 // Process the RHS.
2311
2312 NodeSet Tmp2;
2313 Visit(RHS, *I1, Tmp2);
2314
2315 // With both the LHS and RHS evaluated, process the operation itself.
2316
2317 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002318
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002319 const GRState* St = GetState(*I2);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002320 const GRState* OldSt = St;
2321
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002322 SVal RightV = GetSVal(St, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002323 BinaryOperator::Opcode Op = B->getOpcode();
2324
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002325 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002326
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002327 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002328
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002329 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002330 // FIXME: Handle structs.
2331 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002332
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002333 if (RightV.isUnknown() && (T->isIntegerType() || Loc::IsLocType(T))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002334 unsigned Count = Builder->getCurrentBlockCount();
2335 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2336
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002337 RightV = Loc::IsLocType(B->getRHS()->getType())
2338 ? cast<SVal>(loc::SymbolVal(Sym))
2339 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002340 }
2341
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002342 // Simulate the effects of a "store": bind the value of the RHS
2343 // to the L-Value represented by the LHS.
Ted Kremeneke38718e2008-04-16 18:21:25 +00002344
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002345 EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002346 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002347 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002348
2349 case BinaryOperator::Div:
2350 case BinaryOperator::Rem:
2351
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002352 // Special checking for integer denominators.
2353 if (RHS->getType()->isIntegerType()) {
2354 St = CheckDivideZero(B, St, *I2, RightV);
2355 if (!St) continue;
2356 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002357
2358 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002359
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002360 default: {
2361
2362 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002363 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002364
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002365 // Process non-assignements except commas or short-circuited
2366 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002367
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002368 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002369
2370 if (Result.isUnknown()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002371 if (OldSt != St) {
2372 // Generate a new node if we have already created a new state.
2373 MakeNode(Dst, B, *I2, St);
2374 }
2375 else
2376 Dst.Add(*I2);
2377
Ted Kremenek89063af2008-02-21 19:15:37 +00002378 continue;
2379 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002380
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002381 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002382
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002383 // The operands were *not* undefined, but the result is undefined.
2384 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002385
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002386 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002387 UndefNode->markAsSink();
2388 UndefResults.insert(UndefNode);
2389 }
2390
2391 continue;
2392 }
2393
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002394 // Otherwise, create a new node.
2395
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002396 MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002397 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002398 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002399 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002400
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002401 assert (B->isCompoundAssignmentOp());
2402
Ted Kremenek934e3e92008-10-27 23:02:39 +00002403 if (Op >= BinaryOperator::AndAssign) {
2404 Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
2405 BinaryOperator::And));
2406 }
2407 else {
2408 Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
2409 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002410
2411 // Perform a load (the LHS). This performs the checks for
2412 // null dereferences, and so on.
2413 NodeSet Tmp3;
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002414 SVal location = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002415 EvalLoad(Tmp3, LHS, *I2, St, location);
2416
2417 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2418
2419 St = GetState(*I3);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002420 SVal V = GetSVal(St, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002421
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002422 // Check for divide-by-zero.
2423 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
2424 && RHS->getType()->isIntegerType()) {
2425
2426 // CheckDivideZero returns a new state where the denominator
2427 // is assumed to be non-zero.
2428 St = CheckDivideZero(B, St, *I3, RightV);
2429
2430 if (!St)
2431 continue;
2432 }
2433
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002434 // Propagate undefined values (left-side).
2435 if (V.isUndef()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002436 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002437 continue;
2438 }
2439
2440 // Propagate unknown values (left and right-side).
2441 if (RightV.isUnknown() || V.isUnknown()) {
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002442 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002443 UnknownVal());
2444 continue;
2445 }
2446
2447 // At this point:
2448 //
2449 // The LHS is not Undef/Unknown.
2450 // The RHS is not Unknown.
2451
2452 // Get the computation type.
2453 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
2454
2455 // Perform promotions.
2456 V = EvalCast(V, CTy);
2457 RightV = EvalCast(RightV, CTy);
2458
2459 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002460 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002461 // Propagate undefined values (right-side).
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002462 EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002463 continue;
2464 }
2465
2466 // Compute the result of the operation.
2467
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002468 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002469
2470 if (Result.isUndef()) {
2471
2472 // The operands were not undefined, but the result is undefined.
2473
2474 if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
2475 UndefNode->markAsSink();
2476 UndefResults.insert(UndefNode);
2477 }
2478
2479 continue;
2480 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002481
2482 // EXPERIMENTAL: "Conjured" symbols.
2483 // FIXME: Handle structs.
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002484 if (Result.isUnknown() &&
2485 (CTy->isIntegerType() || Loc::IsLocType(CTy))) {
2486
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002487 unsigned Count = Builder->getCurrentBlockCount();
2488 SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
2489
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002490 Result = Loc::IsLocType(CTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002491 ? cast<SVal>(loc::SymbolVal(Sym))
2492 : cast<SVal>(nonloc::SymbolVal(Sym));
2493 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002494
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002495 EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), location, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002496 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002497 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002498 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002499}
Ted Kremenekee985462008-01-16 18:18:48 +00002500
2501//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002502// Transfer-function Helpers.
2503//===----------------------------------------------------------------------===//
2504
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002505void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002506 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002507 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002508 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002509
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002510 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002511 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2512
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002513 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002514 MakeNode(Dst, Ex, Pred, *I);
2515}
2516
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002517void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002518 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002519 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002520
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002521 GRStateSet::AutoPopulate AP(OStates, St);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002522 if (R.isValid()) getTF().EvalBinOpNN(OStates, StateMgr, St, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002523}
2524
2525//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002526// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002527//===----------------------------------------------------------------------===//
2528
Ted Kremenekaa66a322008-01-16 21:46:15 +00002529#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002530static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002531static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002532
Ted Kremenekaa66a322008-01-16 21:46:15 +00002533namespace llvm {
2534template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002535struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002536 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002537
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002538 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2539
2540 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002541 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002542 GraphPrintCheckerState->isUndefDeref(N) ||
2543 GraphPrintCheckerState->isUndefStore(N) ||
2544 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002545 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2546 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002547 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002548 GraphPrintCheckerState->isBadCall(N) ||
2549 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002550 return "color=\"red\",style=\"filled\"";
2551
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002552 if (GraphPrintCheckerState->isNoReturnCall(N))
2553 return "color=\"blue\",style=\"filled\"";
2554
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002555 return "";
2556 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002557
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002558 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002559 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002560
2561 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002562 ProgramPoint Loc = N->getLocation();
2563
2564 switch (Loc.getKind()) {
2565 case ProgramPoint::BlockEntranceKind:
2566 Out << "Block Entrance: B"
2567 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2568 break;
2569
2570 case ProgramPoint::BlockExitKind:
2571 assert (false);
2572 break;
2573
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002574 case ProgramPoint::PostLoadKind:
Ted Kremenek331b0ac2008-06-18 05:34:07 +00002575 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenekaa66a322008-01-16 21:46:15 +00002576 case ProgramPoint::PostStmtKind: {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002577 const PostStmt& L = cast<PostStmt>(Loc);
2578 Stmt* S = L.getStmt();
2579 SourceLocation SLoc = S->getLocStart();
2580
2581 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremeneka95d3752008-09-13 05:16:45 +00002582 llvm::raw_os_ostream OutS(Out);
2583 S->printPretty(OutS);
2584 OutS.flush();
Ted Kremenek9ff731d2008-01-24 22:27:20 +00002585
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002586 if (SLoc.isFileID()) {
2587 Out << "\\lline="
2588 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00002589 << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002590 }
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002591
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002592 if (GraphPrintCheckerState->isImplicitNullDeref(N))
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002593 Out << "\\|Implicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002594 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
Ted Kremenek63a4f692008-02-07 06:04:18 +00002595 Out << "\\|Explicit-Null Dereference.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002596 else if (GraphPrintCheckerState->isUndefDeref(N))
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002597 Out << "\\|Dereference of undefialied value.\\l";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002598 else if (GraphPrintCheckerState->isUndefStore(N))
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002599 Out << "\\|Store to Undefined Loc.";
Ted Kremenek4d839b42008-03-07 19:04:53 +00002600 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2601 Out << "\\|Explicit divide-by zero or undefined value.";
2602 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2603 Out << "\\|Implicit divide-by zero or undefined value.";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002604 else if (GraphPrintCheckerState->isUndefResult(N))
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002605 Out << "\\|Result of operation is undefined.";
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002606 else if (GraphPrintCheckerState->isNoReturnCall(N))
2607 Out << "\\|Call to function marked \"noreturn\".";
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002608 else if (GraphPrintCheckerState->isBadCall(N))
2609 Out << "\\|Call to NULL/Undefined.";
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002610 else if (GraphPrintCheckerState->isUndefArg(N))
2611 Out << "\\|Argument in call is undefined";
Ted Kremenekd131c4f2008-02-07 05:48:01 +00002612
Ted Kremenekaa66a322008-01-16 21:46:15 +00002613 break;
2614 }
2615
2616 default: {
2617 const BlockEdge& E = cast<BlockEdge>(Loc);
2618 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2619 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00002620
2621 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00002622
2623 SourceLocation SLoc = T->getLocStart();
2624
Ted Kremenekb38911f2008-01-30 23:03:39 +00002625 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00002626
Ted Kremeneka95d3752008-09-13 05:16:45 +00002627 llvm::raw_os_ostream OutS(Out);
2628 E.getSrc()->printTerminator(OutS);
2629 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00002630
Ted Kremenek9b5551d2008-03-09 03:30:59 +00002631 if (SLoc.isFileID()) {
2632 Out << "\\lline="
2633 << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
2634 << GraphPrintSourceManager->getColumnNumber(SLoc);
2635 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00002636
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002637 if (isa<SwitchStmt>(T)) {
2638 Stmt* Label = E.getDst()->getLabel();
2639
2640 if (Label) {
2641 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2642 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002643 llvm::raw_os_ostream OutS(Out);
2644 C->getLHS()->printPretty(OutS);
2645 OutS.flush();
2646
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002647 if (Stmt* RHS = C->getRHS()) {
2648 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00002649 RHS->printPretty(OutS);
2650 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002651 }
2652
2653 Out << ":";
2654 }
2655 else {
2656 assert (isa<DefaultStmt>(Label));
2657 Out << "\\ldefault:";
2658 }
2659 }
2660 else
2661 Out << "\\l(implicit) default:";
2662 }
2663 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00002664 // FIXME
2665 }
2666 else {
2667 Out << "\\lCondition: ";
2668 if (*E.getSrc()->succ_begin() == E.getDst())
2669 Out << "true";
2670 else
2671 Out << "false";
2672 }
2673
2674 Out << "\\l";
2675 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002676
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002677 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
2678 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002679 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00002680 }
2681 }
2682
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00002683 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00002684
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002685 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
2686 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002687
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002688 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00002689 return Out.str();
2690 }
2691};
2692} // end llvm namespace
2693#endif
2694
Ted Kremenekffe0f432008-03-07 22:58:01 +00002695#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002696
2697template <typename ITERATOR>
2698GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
2699
2700template <>
2701GRExprEngine::NodeTy*
2702GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
2703 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
2704 return I->first;
2705}
2706
Ted Kremenekffe0f432008-03-07 22:58:01 +00002707template <typename ITERATOR>
Ted Kremenekcb612922008-04-18 19:23:43 +00002708static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002709 ITERATOR I, ITERATOR E) {
Ted Kremenekffe0f432008-03-07 22:58:01 +00002710
Ted Kremenekd4527582008-09-16 18:44:52 +00002711 llvm::SmallSet<ProgramPoint,10> CachedSources;
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002712
2713 for ( ; I != E; ++I ) {
2714 GRExprEngine::NodeTy* N = GetGraphNode(I);
Ted Kremenekd4527582008-09-16 18:44:52 +00002715 ProgramPoint P = N->getLocation();
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002716
Ted Kremenekd4527582008-09-16 18:44:52 +00002717 if (CachedSources.count(P))
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002718 continue;
2719
Ted Kremenekd4527582008-09-16 18:44:52 +00002720 CachedSources.insert(P);
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002721 Sources.push_back(N);
2722 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002723}
2724#endif
2725
2726void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00002727#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00002728 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002729 std::vector<NodeTy*> Src;
2730
2731 // Fixme: Migrate over to the new way of adding nodes.
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002732 AddSources(Src, null_derefs_begin(), null_derefs_end());
2733 AddSources(Src, undef_derefs_begin(), undef_derefs_end());
2734 AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
2735 AddSources(Src, undef_results_begin(), undef_results_end());
2736 AddSources(Src, bad_calls_begin(), bad_calls_end());
2737 AddSources(Src, undef_arg_begin(), undef_arg_end());
Ted Kremenek1b9df4c2008-03-14 18:14:50 +00002738 AddSources(Src, undef_branches_begin(), undef_branches_end());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002739
Ted Kremenekcb612922008-04-18 19:23:43 +00002740 // The new way.
2741 for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
2742 (*I)->GetErrorNodes(Src);
2743
2744
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00002745 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00002746 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00002747 else {
2748 GraphPrintCheckerState = this;
2749 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00002750
Ted Kremenekffe0f432008-03-07 22:58:01 +00002751 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00002752
2753 GraphPrintCheckerState = NULL;
2754 GraphPrintSourceManager = NULL;
2755 }
2756#endif
2757}
2758
2759void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
2760#ifndef NDEBUG
2761 GraphPrintCheckerState = this;
2762 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00002763
Ted Kremenek493d7a22008-03-11 18:25:33 +00002764 GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
2765
2766 if (!TrimmedG)
2767 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
2768 else {
2769 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
2770 delete TrimmedG;
2771 }
Ted Kremenekffe0f432008-03-07 22:58:01 +00002772
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002773 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002774 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00002775#endif
Ted Kremenekee985462008-01-16 18:18:48 +00002776}