blob: 6d360ed5033c726f8c1d0bdb0bff71cad7058790 [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 Kremenek41573eb2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
18
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000021#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000022#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000023#include "llvm/ADT/ImmutableList.h"
24#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000026
Ted Kremenek0f5f0592008-02-27 06:07:00 +000027#ifndef NDEBUG
28#include "llvm/Support/GraphWriter.h"
29#include <sstream>
30#endif
31
Ted Kremenekb387a3f2008-02-14 22:16:04 +000032using namespace clang;
33using llvm::dyn_cast;
34using llvm::cast;
35using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036
Ted Kremeneke695e1c2008-04-15 23:06:53 +000037//===----------------------------------------------------------------------===//
38// Engine construction and deletion.
39//===----------------------------------------------------------------------===//
40
Ted Kremenekbdb435d2008-07-11 18:37:32 +000041namespace {
42
43class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
44 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
45 typedef llvm::DenseMap<void*,Checks> MapTy;
46
47 MapTy M;
48 Checks::Factory F;
49
50public:
51 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
52
53 virtual ~MappedBatchAuditor() {
54 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
55
56 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
57 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
58
59 GRSimpleAPICheck* check = *I;
60
61 if (AlreadyVisited.count(check))
62 continue;
63
64 AlreadyVisited.insert(check);
65 delete check;
66 }
67 }
68
69 void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
70 assert (A && "Check cannot be null.");
71 void* key = reinterpret_cast<void*>((uintptr_t) C);
72 MapTy::iterator I = M.find(key);
73 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
74 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000075
Ted Kremenek4adc81e2008-08-13 04:27:00 +000076 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000077 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
78 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
79 MapTy::iterator MI = M.find(key);
80
81 if (MI == M.end())
82 return false;
83
84 bool isSink = false;
85
86 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
Ted Kremenek584def72008-07-22 00:46:16 +000087 isSink |= (*I)->Audit(N, VMgr);
Ted Kremenekbdb435d2008-07-11 18:37:32 +000088
89 return isSink;
90 }
91};
92
93} // end anonymous namespace
94
95//===----------------------------------------------------------------------===//
96// Engine construction and deletion.
97//===----------------------------------------------------------------------===//
98
Ted Kremeneke448ab42008-05-01 18:33:28 +000099static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
100 IdentifierInfo* II = &Ctx.Idents.get(name);
101 return Ctx.Selectors.getSelector(0, &II);
102}
103
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000104
Ted Kremenek8b233612008-07-02 20:13:38 +0000105GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000106 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000107 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000108 StoreManagerCreator SMC,
109 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000110 : CoreEngine(cfg, CD, Ctx, *this),
111 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000112 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000113 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000114 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000115 SymMgr(StateMgr.getSymbolManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000116 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000117 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
118 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000119 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000120 BR(BRD, *this),
121 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122
Ted Kremenek1a654b62008-06-20 21:45:25 +0000123GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000124 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000125 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000126}
127
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000128//===----------------------------------------------------------------------===//
129// Utility methods.
130//===----------------------------------------------------------------------===//
131
Ted Kremenek186350f2008-04-23 20:12:28 +0000132
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000133void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000134 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000135 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000136 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000137}
138
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000139void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
140 if (!BatchAuditor)
141 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
142
143 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000144}
145
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000146const GRState* GRExprEngine::getInitialState() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000147 return StateMgr.getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000148}
149
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000150//===----------------------------------------------------------------------===//
151// Top-level transfer function logic (Dispatcher).
152//===----------------------------------------------------------------------===//
153
154void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
155
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000156 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
157 S->getLocStart(),
158 "Error evaluating statement");
159
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000160 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000161 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000162
163 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000164 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000165 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000166
167 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000168 if (BatchAuditor)
169 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000170
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000171 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000172 SymbolReaper SymReaper(Liveness, SymMgr);
173 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
174 CurrentStmt, SymReaper)
175 : EntryNode->getState();
176
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000177 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000178 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000179
Ted Kremenek241677a2009-01-21 22:26:05 +0000180 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000181 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000182 else {
183 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000184 SaveOr OldHasGen(Builder->HasGeneratedNode);
185
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000186 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
187 Builder->PurgingDeadSymbols = true;
188
Ted Kremenek729a9a22008-07-17 23:15:45 +0000189 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000190 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000191
192 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
193 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000194 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000195
196 bool HasAutoGenerated = false;
197
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000198 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000199
200 NodeSet Dst;
201
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000202 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000203 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
204
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000205 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000206 Visit(S, *I, Dst);
207
208 // Do we need to auto-generate a node? We only need to do this to generate
209 // a node with a "cleaned" state; GRCoreEngine will actually handle
210 // auto-transitions for other cases.
211 if (Dst.size() == 1 && *Dst.begin() == EntryNode
212 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
213 HasAutoGenerated = true;
214 builder.generateNode(S, GetState(EntryNode), *I);
215 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000216 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000217
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000218 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000219 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000220 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000221
222 // FIXME: Consolidate.
223 StateMgr.CurrentStmt = 0;
224 CurrentStmt = 0;
225
Ted Kremenek846d4e92008-04-24 23:35:58 +0000226 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000227}
228
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000229void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
230 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
231 S->getLocStart(),
232 "Error evaluating statement");
233
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000234 // FIXME: add metadata to the CFG so that we can disable
235 // this check when we KNOW that there is no block-level subexpression.
236 // The motivation is that this check requires a hashtable lookup.
237
238 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
239 Dst.Add(Pred);
240 return;
241 }
242
243 switch (S->getStmtClass()) {
244
245 default:
246 // Cases we intentionally have "default" handle:
247 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
248
249 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
250 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000251
252 case Stmt::ArraySubscriptExprClass:
253 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
254 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000255
256 case Stmt::AsmStmtClass:
257 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
258 break;
259
260 case Stmt::BinaryOperatorClass: {
261 BinaryOperator* B = cast<BinaryOperator>(S);
262
263 if (B->isLogicalOp()) {
264 VisitLogicalExpr(B, Pred, Dst);
265 break;
266 }
267 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000268 const GRState* state = GetState(Pred);
269 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000270 break;
271 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000272
Ted Kremenek48af2a92009-02-25 22:32:02 +0000273 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
274 NodeSet Tmp;
275 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000276 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000277 }
278 else
279 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
280
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000281 break;
282 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000283
Douglas Gregorb4609802008-11-14 16:09:21 +0000284 case Stmt::CallExprClass:
285 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000286 CallExpr* C = cast<CallExpr>(S);
287 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000288 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000289 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000290
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000291 // FIXME: ChooseExpr is really a constant. We need to fix
292 // the CFG do not model them as explicit control-flow.
293
294 case Stmt::ChooseExprClass: { // __builtin_choose_expr
295 ChooseExpr* C = cast<ChooseExpr>(S);
296 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
297 break;
298 }
299
300 case Stmt::CompoundAssignOperatorClass:
301 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
302 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000303
304 case Stmt::CompoundLiteralExprClass:
305 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
306 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000307
308 case Stmt::ConditionalOperatorClass: { // '?' operator
309 ConditionalOperator* C = cast<ConditionalOperator>(S);
310 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
311 break;
312 }
313
314 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000315 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000316 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000317 break;
318
319 case Stmt::DeclStmtClass:
320 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
321 break;
322
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000323 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000324 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000325 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000326 VisitCast(C, C->getSubExpr(), Pred, Dst);
327 break;
328 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000329
330 case Stmt::InitListExprClass:
331 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
332 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000333
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000334 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000335 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
336 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000337
338 case Stmt::ObjCIvarRefExprClass:
339 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
340 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000341
342 case Stmt::ObjCForCollectionStmtClass:
343 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
344 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000345
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000346 case Stmt::ObjCMessageExprClass: {
347 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
348 break;
349 }
350
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000351 case Stmt::ObjCAtThrowStmtClass: {
352 // FIXME: This is not complete. We basically treat @throw as
353 // an abort.
354 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
355 Builder->BuildSinks = true;
356 MakeNode(Dst, S, Pred, GetState(Pred));
357 break;
358 }
359
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000361 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000362 break;
363
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000364 case Stmt::ReturnStmtClass:
365 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
366 break;
367
Sebastian Redl05189992008-11-11 17:56:53 +0000368 case Stmt::SizeOfAlignOfExprClass:
369 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000370 break;
371
372 case Stmt::StmtExprClass: {
373 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000374
375 if (SE->getSubStmt()->body_empty()) {
376 // Empty statement expression.
377 assert(SE->getType() == getContext().VoidTy
378 && "Empty statement expression must have void type.");
379 Dst.Add(Pred);
380 break;
381 }
382
383 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
384 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000385 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000386 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000387 else
388 Dst.Add(Pred);
389
390 break;
391 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000392
393 case Stmt::StringLiteralClass:
394 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
395 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000396
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000397 case Stmt::UnaryOperatorClass:
398 VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000399 break;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000400 }
401}
402
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000403void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000404
405 Ex = Ex->IgnoreParens();
406
407 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
408 Dst.Add(Pred);
409 return;
410 }
411
412 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000413
414 case Stmt::ArraySubscriptExprClass:
415 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
416 return;
417
418 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000419 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000420 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
421 return;
422
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000423 case Stmt::ObjCIvarRefExprClass:
424 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
425 return;
426
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000427 case Stmt::UnaryOperatorClass:
428 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
429 return;
430
431 case Stmt::MemberExprClass:
432 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
433 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000434
Ted Kremenek4f090272008-10-27 21:54:31 +0000435 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000436 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000437 return;
438
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000439 case Stmt::ObjCPropertyRefExprClass:
440 // FIXME: Property assignments are lvalues, but not really "locations".
441 // e.g.: self.x = something;
442 // Here the "self.x" really can translate to a method call (setter) when
443 // the assignment is made. Moreover, the entire assignment expression
444 // evaluate to whatever "something" is, not calling the "getter" for
445 // the property (which would make sense since it can have side effects).
446 // We'll probably treat this as a location, but not one that we can
447 // take the address of. Perhaps we need a new SVal class for cases
448 // like thsis?
449 // Note that we have a similar problem for bitfields, since they don't
450 // have "locations" in the sense that we can take their address.
451 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000452 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000453
454 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000455 const GRState* state = GetState(Pred);
456 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
457 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000458 return;
459 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000460
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000461 default:
462 // Arbitrary subexpressions can return aggregate temporaries that
463 // can be used in a lvalue context. We need to enhance our support
464 // of such temporaries in both the environment and the store, so right
465 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000466 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000467 "Other kinds of expressions with non-aggregate/union types do"
468 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000469
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000470 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000471 }
472}
473
474//===----------------------------------------------------------------------===//
475// Block entrance. (Update counters).
476//===----------------------------------------------------------------------===//
477
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000478bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000479 GRBlockCounter BC) {
480
481 return BC.getNumVisited(B->getBlockID()) < 3;
482}
483
484//===----------------------------------------------------------------------===//
485// Branch processing.
486//===----------------------------------------------------------------------===//
487
Ted Kremeneka8538d92009-02-13 01:45:31 +0000488const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000489 Stmt* Terminator,
490 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000491
492 switch (Terminator->getStmtClass()) {
493 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000494 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000495
496 case Stmt::BinaryOperatorClass: { // '&&' and '||'
497
498 BinaryOperator* B = cast<BinaryOperator>(Terminator);
499 BinaryOperator::Opcode Op = B->getOpcode();
500
501 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
502
503 // For &&, if we take the true branch, then the value of the whole
504 // expression is that of the RHS expression.
505 //
506 // For ||, if we take the false branch, then the value of the whole
507 // expression is that of the RHS expression.
508
509 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
510 (Op == BinaryOperator::LOr && !branchTaken)
511 ? B->getRHS() : B->getLHS();
512
Ted Kremeneka8538d92009-02-13 01:45:31 +0000513 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000514 }
515
516 case Stmt::ConditionalOperatorClass: { // ?:
517
518 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
519
520 // For ?, if branchTaken == true then the value is either the LHS or
521 // the condition itself. (GNU extension).
522
523 Expr* Ex;
524
525 if (branchTaken)
526 Ex = C->getLHS() ? C->getLHS() : C->getCond();
527 else
528 Ex = C->getRHS();
529
Ted Kremeneka8538d92009-02-13 01:45:31 +0000530 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000531 }
532
533 case Stmt::ChooseExprClass: { // ?:
534
535 ChooseExpr* C = cast<ChooseExpr>(Terminator);
536
537 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000538 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000539 }
540 }
541}
542
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000543/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
544/// to try to recover some path-sensitivity for casts of symbolic
545/// integers that promote their values (which are currently not tracked well).
546/// This function returns the SVal bound to Condition->IgnoreCasts if all the
547// cast(s) did was sign-extend the original value.
548static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
549 Stmt* Condition, ASTContext& Ctx) {
550
551 Expr *Ex = dyn_cast<Expr>(Condition);
552 if (!Ex)
553 return UnknownVal();
554
555 uint64_t bits = 0;
556 bool bitsInit = false;
557
558 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
559 QualType T = CE->getType();
560
561 if (!T->isIntegerType())
562 return UnknownVal();
563
564 uint64_t newBits = Ctx.getTypeSize(T);
565 if (!bitsInit || newBits < bits) {
566 bitsInit = true;
567 bits = newBits;
568 }
569
570 Ex = CE->getSubExpr();
571 }
572
573 // We reached a non-cast. Is it a symbolic value?
574 QualType T = Ex->getType();
575
576 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
577 return UnknownVal();
578
579 return StateMgr.GetSVal(state, Ex);
580}
581
Ted Kremenekaf337412008-11-12 19:24:17 +0000582void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000583 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000584
Ted Kremeneke7d22112008-02-11 19:21:59 +0000585 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000586 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000587 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000588
Ted Kremenekb2331832008-02-15 22:29:00 +0000589 // Check for NULL conditions; e.g. "for(;;)"
590 if (!Condition) {
591 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000592 return;
593 }
594
Ted Kremenek21028dd2009-03-11 03:54:24 +0000595 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
596 Condition->getLocStart(),
597 "Error evaluating branch");
598
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000599 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000600
601 switch (V.getBaseKind()) {
602 default:
603 break;
604
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000605 case SVal::UnknownKind: {
606 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
607 if (Ex->getType()->isIntegerType()) {
608 // Try to recover some path-sensitivity. Right now casts of symbolic
609 // integers that promote their values are currently not tracked well.
610 // If 'Condition' is such an expression, try and recover the
611 // underlying value and use that instead.
612 SVal recovered = RecoverCastedSymbol(getStateManager(),
613 builder.getState(), Condition,
614 getContext());
615
616 if (!recovered.isUnknown()) {
617 V = recovered;
618 break;
619 }
620 }
621 }
622
Ted Kremenek58b33212008-02-26 19:40:44 +0000623 builder.generateNode(MarkBranch(PrevState, Term, true), true);
624 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000625 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000626 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000627
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000628 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000629 NodeTy* N = builder.generateNode(PrevState, true);
630
631 if (N) {
632 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000633 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000634 }
635
636 builder.markInfeasible(false);
637 return;
638 }
639 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000640
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000641 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000642
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000643 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000644 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000645
646 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000647 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000648 else
649 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000650
651 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000652
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000653 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000654 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000655
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000656 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000657 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000658 else
659 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000660}
661
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000662/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000663/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000664void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000665
Ted Kremeneka8538d92009-02-13 01:45:31 +0000666 const GRState* state = builder.getState();
667 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000668
669 // Three possibilities:
670 //
671 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000672 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000673 // (3) We have no clue about the label. Dispatch to all targets.
674 //
675
676 typedef IndirectGotoNodeBuilder::iterator iterator;
677
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000678 if (isa<loc::GotoLabel>(V)) {
679 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000680
681 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000682 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000683 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000684 return;
685 }
686 }
687
688 assert (false && "No block with label.");
689 return;
690 }
691
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000692 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000693 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000694 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000695 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000696 return;
697 }
698
699 // This is really a catch-all. We don't support symbolics yet.
700
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000701 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000702
703 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000704 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000705}
Ted Kremenekf233d482008-02-05 00:26:40 +0000706
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000707
708void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
709 NodeTy* Pred, NodeSet& Dst) {
710
711 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
712
Ted Kremeneka8538d92009-02-13 01:45:31 +0000713 const GRState* state = GetState(Pred);
714 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000715
716 assert (X.isUndef());
717
718 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
719
720 assert (SE);
721
Ted Kremeneka8538d92009-02-13 01:45:31 +0000722 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000723
724 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000725 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000726}
727
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000728/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
729/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000730void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
731 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000732 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000733 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000734 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000735
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000736 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000737 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000738 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000739 return;
740 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000741
Ted Kremeneka8538d92009-02-13 01:45:31 +0000742 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000743 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000744
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000745 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000746 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000747
748 // Evaluate the LHS of the case value.
749 Expr::EvalResult V1;
750 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000751
Ted Kremenek72afb372009-01-17 01:54:16 +0000752 // Sanity checks. These go away in Release builds.
753 assert(b && V1.Val.isInt() && !V1.HasSideEffects
754 && "Case condition must evaluate to an integer constant.");
755 b = b; // silence unused variable warning
756 assert(V1.Val.getInt().getBitWidth() ==
757 getContext().getTypeSize(CondE->getType()));
758
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000759 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000760 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000761
762 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000763 b = E->Evaluate(V2, getContext());
764 assert(b && V2.Val.isInt() && !V2.HasSideEffects
765 && "Case condition must evaluate to an integer constant.");
766 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000767 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000768 else
769 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000770
771 // FIXME: Eventually we should replace the logic below with a range
772 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000773 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000774
Ted Kremenek14a11402008-03-17 22:17:56 +0000775 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000776 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000777 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000778
Ted Kremenek72afb372009-01-17 01:54:16 +0000779 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000780 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000781 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000782
783 if (isFeasible) {
784 builder.generateCaseStmtNode(I, StNew);
785
786 // If CondV evaluates to a constant, then we know that this
787 // is the *only* case that we can take, so stop evaluating the
788 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000789 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000790 return;
791 }
792
793 // Now "assume" that the case doesn't match. Add this state
794 // to the default state (if it is feasible).
795
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000796 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000797 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000798
Ted Kremenek5014ab12008-04-23 05:03:18 +0000799 if (isFeasible) {
800 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000801 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000802 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000803
Ted Kremenek14a11402008-03-17 22:17:56 +0000804 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000805 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000806 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000807
Ted Kremenek72afb372009-01-17 01:54:16 +0000808 ++V1.Val.getInt();
809 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000810
811 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000812 }
813
814 // If we reach here, than we know that the default branch is
815 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000816 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000817}
818
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000819//===----------------------------------------------------------------------===//
820// Transfer functions: logical operations ('&&', '||').
821//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000822
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000823void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000824 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000825
Ted Kremenek05a23782008-02-26 19:05:15 +0000826 assert (B->getOpcode() == BinaryOperator::LAnd ||
827 B->getOpcode() == BinaryOperator::LOr);
828
829 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
830
Ted Kremeneka8538d92009-02-13 01:45:31 +0000831 const GRState* state = GetState(Pred);
832 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000833
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000834 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000835
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000836 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000837
838 assert (Ex);
839
840 if (Ex == B->getRHS()) {
841
Ted Kremeneka8538d92009-02-13 01:45:31 +0000842 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000843
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000844 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000845
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000846 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000847 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000848 return;
849 }
850
Ted Kremenek05a23782008-02-26 19:05:15 +0000851 // We took the RHS. Because the value of the '&&' or '||' expression must
852 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
853 // or 1. Alternatively, we could take a lazy approach, and calculate this
854 // value later when necessary. We don't have the machinery in place for
855 // this right now, and since most logical expressions are used for branches,
856 // the payoff is not likely to be large. Instead, we do eager evaluation.
857
858 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000859 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000860
861 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000862 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000863 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000864
865 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000866 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000867
868 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000869 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000870 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000871 }
872 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000873 // We took the LHS expression. Depending on whether we are '&&' or
874 // '||' we know what the value of the expression is via properties of
875 // the short-circuiting.
876
877 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000878 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000879 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000880}
Ted Kremenek05a23782008-02-26 19:05:15 +0000881
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000882//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000883// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000884//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000885
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000886void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
887 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000888
Ted Kremeneka8538d92009-02-13 01:45:31 +0000889 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000890
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000891 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000892
893 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
894
Ted Kremeneka8538d92009-02-13 01:45:31 +0000895 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000896
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000897 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000898 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000899 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000900 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000901 return;
902
903 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
904 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
905
906 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000907 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000908 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000909 return;
910
911 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000912 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000913 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000914 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000915 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000916 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000917
918 assert (false &&
919 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000920}
921
Ted Kremenek540cbe22008-04-22 04:56:29 +0000922/// VisitArraySubscriptExpr - Transfer function for array accesses
923void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000924 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000925
926 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000927 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000928 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000929
930 if (Base->getType()->isVectorType()) {
931 // For vector types get its lvalue.
932 // FIXME: This may not be correct. Is the rvalue of a vector its location?
933 // In fact, I think this is just a hack. We need to get the right
934 // semantics.
935 VisitLValue(Base, Pred, Tmp);
936 }
937 else
938 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000939
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000940 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000941 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000942 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000943
944 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000945 const GRState* state = GetState(*I2);
946 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
947 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000948
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000949 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000950 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000951 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000952 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000953 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000954 }
Ted Kremenek540cbe22008-04-22 04:56:29 +0000955}
956
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000957/// VisitMemberExpr - Transfer function for member expressions.
958void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000959 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000960
961 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000962 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +0000963
964 if (M->isArrow())
965 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
966 else
967 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
968
Douglas Gregor86f19402008-12-20 23:49:58 +0000969 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
970 if (!Field) // FIXME: skipping member expressions for non-fields
971 return;
972
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000973 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000974 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000975 // FIXME: Should we insert some assumption logic in here to determine
976 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +0000977 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +0000978 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000979
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000980 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000981 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000982 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000983 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000984 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000985}
986
Ted Kremeneka8538d92009-02-13 01:45:31 +0000987/// EvalBind - Handle the semantics of binding a value to a specific location.
988/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
989void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
990 const GRState* state, SVal location, SVal Val) {
991
Ted Kremenek41573eb2009-02-14 01:43:44 +0000992 const GRState* newState = 0;
993
994 if (location.isUnknown()) {
995 // We know that the new state will be the same as the old state since
996 // the location of the binding is "unknown". Consequently, there
997 // is no reason to just create a new node.
998 newState = state;
999 }
1000 else {
1001 // We are binding to a value other than 'unknown'. Perform the binding
1002 // using the StoreManager.
1003 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1004 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001005
Ted Kremenek41573eb2009-02-14 01:43:44 +00001006 // The next thing to do is check if the GRTransferFuncs object wants to
1007 // update the state based on the new binding. If the GRTransferFunc object
1008 // doesn't do anything, just auto-propagate the current state.
1009 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1010 newState != state);
1011
1012 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001013}
1014
1015/// EvalStore - Handle the semantics of a store via an assignment.
1016/// @param Dst The node set to store generated state nodes
1017/// @param Ex The expression representing the location of the store
1018/// @param state The current simulation state
1019/// @param location The location to store the value
1020/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001021void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001022 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001023
1024 assert (Builder && "GRStmtNodeBuilder must be defined.");
1025
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001026 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001027 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001028
Ted Kremenek8c354752008-12-16 22:02:27 +00001029 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001030 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001031
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001032 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001033 state = GetState(Pred);
1034
1035 // Proceed with the store.
1036 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1037 Builder->PointKind = ProgramPoint::PostStoreKind;
1038 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001039}
1040
1041void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001042 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001043
Ted Kremenek8c354752008-12-16 22:02:27 +00001044 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001045 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001046
Ted Kremenek8c354752008-12-16 22:02:27 +00001047 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001048 return;
1049
Ted Kremeneka8538d92009-02-13 01:45:31 +00001050 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001051
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001052 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001053 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001054
1055 // FIXME: Currently symbolic analysis "generates" new symbols
1056 // for the contents of values. We need a better approach.
1057
Ted Kremenek8c354752008-12-16 22:02:27 +00001058 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001059 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001060 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001061 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001062 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001063 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
1064 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001065 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001066}
1067
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001068void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001069 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001070
1071 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001072 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001073
1074 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1075 MakeNode(Dst, Ex, *I, (*I)->getState());
1076}
1077
Ted Kremenek8c354752008-12-16 22:02:27 +00001078GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001079 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001080 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001081
1082 // Check for loads/stores from/to undefined values.
1083 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001084 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001085 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001086 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001087
Ted Kremenek8c354752008-12-16 22:02:27 +00001088 if (N) {
1089 N->markAsSink();
1090 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001091 }
1092
Ted Kremenek8c354752008-12-16 22:02:27 +00001093 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001094 }
1095
1096 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1097 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001098 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001099
1100 // During a load, one of two possible situations arise:
1101 // (1) A crash, because the location (pointer) was NULL.
1102 // (2) The location (pointer) is not NULL, and the dereference works.
1103 //
1104 // We add these assumptions.
1105
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001106 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001107
1108 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001109 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001110 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001111
1112 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001113 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001114 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001115 getStateManager());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116
1117 if (isFeasibleNull) {
1118
Ted Kremenek7360fda2008-09-18 23:09:54 +00001119 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001120 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001121 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1122
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001123 // We don't use "MakeNode" here because the node will be a sink
1124 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001125 NodeTy* NullNode =
1126 Builder->generateNode(Ex, StNull, Pred,
1127 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001128
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001129 if (NullNode) {
1130
1131 NullNode->markAsSink();
1132
1133 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1134 else ExplicitNullDeref.insert(NullNode);
1135 }
1136 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001137
1138 if (!isFeasibleNotNull)
1139 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001140
1141 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001142 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001143 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1144 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1145 // Get the index of the accessed element.
1146 SVal Idx = ER->getIndex();
1147 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001148 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1149 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001150
1151 bool isFeasibleInBound = false;
1152 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1153 true, isFeasibleInBound);
1154
1155 bool isFeasibleOutBound = false;
1156 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1157 false, isFeasibleOutBound);
1158
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001159 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001160 // Report warning. Make sink node manually.
1161 NodeTy* OOBNode =
1162 Builder->generateNode(Ex, StOutBound, Pred,
1163 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001164
1165 if (OOBNode) {
1166 OOBNode->markAsSink();
1167
1168 if (isFeasibleInBound)
1169 ImplicitOOBMemAccesses.insert(OOBNode);
1170 else
1171 ExplicitOOBMemAccesses.insert(OOBNode);
1172 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001173 }
1174
Ted Kremenek8c354752008-12-16 22:02:27 +00001175 if (!isFeasibleInBound)
1176 return 0;
1177
1178 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001179 }
1180 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001181
Ted Kremenek8c354752008-12-16 22:02:27 +00001182 // Generate a new node indicating the checks succeed.
1183 return Builder->generateNode(Ex, StNotNull, Pred,
1184 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001185}
1186
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001187//===----------------------------------------------------------------------===//
1188// Transfer function: Function calls.
1189//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001190void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001191 CallExpr::arg_iterator AI,
1192 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001193 NodeSet& Dst)
1194{
1195 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001196 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001197 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1198 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001199 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001200
1201 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1202}
1203
1204void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1205 CallExpr::arg_iterator AI,
1206 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001207 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001208 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001209
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001210 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001211 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001212 // If the call argument is being bound to a reference parameter,
1213 // visit it as an lvalue, not an rvalue.
1214 bool VisitAsLvalue = false;
1215 if (Proto && ParamIdx < Proto->getNumArgs())
1216 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1217
1218 NodeSet DstTmp;
1219 if (VisitAsLvalue)
1220 VisitLValue(*AI, Pred, DstTmp);
1221 else
1222 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001223 ++AI;
1224
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001225 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001226 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001227
1228 return;
1229 }
1230
1231 // If we reach here we have processed all of the arguments. Evaluate
1232 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001233
Ted Kremenek994a09b2008-02-25 21:16:03 +00001234 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001235 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001236
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001237 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001238
Ted Kremenekde434242008-02-19 01:44:53 +00001239 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001240 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1241
Ted Kremeneka8538d92009-02-13 01:45:31 +00001242 const GRState* state = GetState(*DI);
1243 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001244
Ted Kremeneka1354a52008-03-03 16:47:31 +00001245 // FIXME: Add support for symbolic function calls (calls involving
1246 // function pointer values that are symbolic).
1247
1248 // Check for undefined control-flow or calls to NULL.
1249
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001250 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001251 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001252
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001253 if (N) {
1254 N->markAsSink();
1255 BadCalls.insert(N);
1256 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001257
Ted Kremenekde434242008-02-19 01:44:53 +00001258 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001259 }
1260
1261 // Check for the "noreturn" attribute.
1262
1263 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1264
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001265 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001266
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001267 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001268
1269 if (FD->getAttr<NoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001270 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001271 else {
1272 // HACK: Some functions are not marked noreturn, and don't return.
1273 // Here are a few hardwired ones. If this takes too long, we can
1274 // potentially cache these results.
1275 const char* s = FD->getIdentifier()->getName();
1276 unsigned n = strlen(s);
1277
1278 switch (n) {
1279 default:
1280 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001281
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001282 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001283 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1284 break;
1285
1286 case 5:
1287 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001288 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001289 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001290 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001291 // FIXME: use Assume to inspect the possible symbolic value of
1292 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001293 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001294 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001295 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001296 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001297 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001298 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001299
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001300 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001301 if (!memcmp(s, "Assert", 6)) {
1302 Builder->BuildSinks = true;
1303 break;
1304 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001305
1306 // FIXME: This is just a wrapper around throwing an exception.
1307 // Eventually inter-procedural analysis should handle this easily.
1308 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1309
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001310 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001311
1312 case 7:
1313 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1314 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001315
Ted Kremenekf47bb782008-04-30 17:54:04 +00001316 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001317 if (!memcmp(s ,"db_error", 8) ||
1318 !memcmp(s, "__assert", 8))
1319 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001320 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001321
1322 case 12:
1323 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1324 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001325
Ted Kremenekf9683082008-09-19 02:30:47 +00001326 case 13:
1327 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1328 break;
1329
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001330 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001331 if (!memcmp(s, "dtrace_assfail", 14) ||
1332 !memcmp(s, "yy_fatal_error", 14))
1333 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001334 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001335
1336 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001337 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001338 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1339 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001340 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001341
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001342 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001343 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001344
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001345 }
1346 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001347
1348 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001349
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001350 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001351
Douglas Gregor3c385e52009-02-14 18:57:46 +00001352 if (unsigned id
1353 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001354 switch (id) {
1355 case Builtin::BI__builtin_expect: {
1356 // For __builtin_expect, just return the value of the subexpression.
1357 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001358 SVal X = GetSVal(state, *(CE->arg_begin()));
1359 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001360 continue;
1361 }
1362
Ted Kremenekb3021332008-11-02 00:35:01 +00001363 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001364 // FIXME: Refactor into StoreManager itself?
1365 MemRegionManager& RM = getStateManager().getRegionManager();
1366 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001367 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001368
1369 // Set the extent of the region in bytes. This enables us to use the
1370 // SVal of the argument directly. If we save the extent in bits, we
1371 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001372 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1373 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001374
Ted Kremeneka8538d92009-02-13 01:45:31 +00001375 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001376 continue;
1377 }
1378
Ted Kremenek55aea312008-03-05 22:59:42 +00001379 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001380 break;
1381 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001382 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001383
Ted Kremenek186350f2008-04-23 20:12:28 +00001384 // Check any arguments passed-by-value against being undefined.
1385
1386 bool badArg = false;
1387
1388 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1389 I != E; ++I) {
1390
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001391 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001392 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001393
Ted Kremenek186350f2008-04-23 20:12:28 +00001394 if (N) {
1395 N->markAsSink();
1396 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001397 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001398
Ted Kremenek186350f2008-04-23 20:12:28 +00001399 badArg = true;
1400 break;
1401 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001402 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001403
1404 if (badArg)
1405 continue;
1406
1407 // Dispatch to the plug-in transfer function.
1408
1409 unsigned size = Dst.size();
1410 SaveOr OldHasGen(Builder->HasGeneratedNode);
1411 EvalCall(Dst, CE, L, *DI);
1412
1413 // Handle the case where no nodes where generated. Auto-generate that
1414 // contains the updated state if we aren't generating sinks.
1415
1416 if (!Builder->BuildSinks && Dst.size() == size &&
1417 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001418 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001419 }
1420}
1421
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001422//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001423// Transfer function: Objective-C ivar references.
1424//===----------------------------------------------------------------------===//
1425
Ted Kremenekf5cae632009-02-28 20:50:43 +00001426static std::pair<const void*,const void*> EagerlyAssumeTag
1427 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1428
Ted Kremenekb2939022009-02-25 23:32:10 +00001429void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001430 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1431 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001432
1433 // Test if the previous node was as the same expression. This can happen
1434 // when the expression fails to evaluate to anything meaningful and
1435 // (as an optimization) we don't generate a node.
1436 ProgramPoint P = Pred->getLocation();
1437 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1438 Dst.Add(Pred);
1439 continue;
1440 }
1441
Ted Kremenek48af2a92009-02-25 22:32:02 +00001442 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001443 SVal V = GetSVal(state, Ex);
Ted Kremenek48af2a92009-02-25 22:32:02 +00001444 if (isa<nonloc::SymIntConstraintVal>(V)) {
1445 // First assume that the condition is true.
1446 bool isFeasible = false;
1447 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1448 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001449 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1450 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001451 stateTrue, Pred));
1452 }
1453
1454 // Next, assume that the condition is false.
1455 isFeasible = false;
1456 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1457 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001458 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1459 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001460 stateFalse, Pred));
1461 }
1462 }
1463 else
1464 Dst.Add(Pred);
1465 }
1466}
1467
1468//===----------------------------------------------------------------------===//
1469// Transfer function: Objective-C ivar references.
1470//===----------------------------------------------------------------------===//
1471
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001472void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1473 NodeTy* Pred, NodeSet& Dst,
1474 bool asLValue) {
1475
1476 Expr* Base = cast<Expr>(Ex->getBase());
1477 NodeSet Tmp;
1478 Visit(Base, Pred, Tmp);
1479
1480 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001481 const GRState* state = GetState(*I);
1482 SVal BaseVal = GetSVal(state, Base);
1483 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001484
1485 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001486 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001487 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001488 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001489 }
1490}
1491
1492//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001493// Transfer function: Objective-C fast enumeration 'for' statements.
1494//===----------------------------------------------------------------------===//
1495
1496void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1497 NodeTy* Pred, NodeSet& Dst) {
1498
1499 // ObjCForCollectionStmts are processed in two places. This method
1500 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1501 // statements within a basic block. This transfer function does two things:
1502 //
1503 // (1) binds the next container value to 'element'. This creates a new
1504 // node in the ExplodedGraph.
1505 //
1506 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1507 // whether or not the container has any more elements. This value
1508 // will be tested in ProcessBranch. We need to explicitly bind
1509 // this value because a container can contain nil elements.
1510 //
1511 // FIXME: Eventually this logic should actually do dispatches to
1512 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1513 // This will require simulating a temporary NSFastEnumerationState, either
1514 // through an SVal or through the use of MemRegions. This value can
1515 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1516 // terminates we reclaim the temporary (it goes out of scope) and we
1517 // we can test if the SVal is 0 or if the MemRegion is null (depending
1518 // on what approach we take).
1519 //
1520 // For now: simulate (1) by assigning either a symbol or nil if the
1521 // container is empty. Thus this transfer function will by default
1522 // result in state splitting.
1523
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001524 Stmt* elem = S->getElement();
1525 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001526
1527 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001528 VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001529 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001530 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1531 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1532 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001533 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001534
1535 NodeSet Tmp;
1536 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001537
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001538 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1539 const GRState* state = GetState(*I);
1540 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1541 }
1542}
1543
1544void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1545 NodeTy* Pred, NodeSet& Dst,
1546 SVal ElementV) {
1547
1548
Ted Kremenekaf337412008-11-12 19:24:17 +00001549
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001550 // Get the current state. Use 'EvalLocation' to determine if it is a null
1551 // pointer, etc.
1552 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001553
Ted Kremenek8c354752008-12-16 22:02:27 +00001554 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1555 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001556 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001557
1558 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001559
Ted Kremenekaf337412008-11-12 19:24:17 +00001560 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001561 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001562 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1563 GRStateRef hasElems = state.BindExpr(S, TrueV);
1564
Ted Kremenekaf337412008-11-12 19:24:17 +00001565 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001566 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1567 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001568
1569 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1570 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1571 // FIXME: The proper thing to do is to really iterate over the
1572 // container. We will do this with dispatch logic to the store.
1573 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001574 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001575 assert (Loc::IsLocType(T));
1576 unsigned Count = Builder->getCurrentBlockCount();
1577 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1578 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001579
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001580 // Bind the location to 'nil' on the false branch.
1581 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1582 noElems = noElems.BindLoc(ElementV, nilV);
1583 }
1584
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001585 // Create the new nodes.
1586 MakeNode(Dst, S, Pred, hasElems);
1587 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001588}
1589
1590//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001591// Transfer function: Objective-C message expressions.
1592//===----------------------------------------------------------------------===//
1593
1594void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1595 NodeSet& Dst){
1596
1597 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1598 Pred, Dst);
1599}
1600
1601void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001602 ObjCMessageExpr::arg_iterator AI,
1603 ObjCMessageExpr::arg_iterator AE,
1604 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001605 if (AI == AE) {
1606
1607 // Process the receiver.
1608
1609 if (Expr* Receiver = ME->getReceiver()) {
1610 NodeSet Tmp;
1611 Visit(Receiver, Pred, Tmp);
1612
1613 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1614 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1615
1616 return;
1617 }
1618
1619 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1620 return;
1621 }
1622
1623 NodeSet Tmp;
1624 Visit(*AI, Pred, Tmp);
1625
1626 ++AI;
1627
1628 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1629 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1630}
1631
1632void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1633 NodeTy* Pred,
1634 NodeSet& Dst) {
1635
1636 // FIXME: More logic for the processing the method call.
1637
Ted Kremeneka8538d92009-02-13 01:45:31 +00001638 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001639 bool RaisesException = false;
1640
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001641
1642 if (Expr* Receiver = ME->getReceiver()) {
1643
Ted Kremeneka8538d92009-02-13 01:45:31 +00001644 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001645
Ted Kremenek21fe8372009-02-19 04:06:22 +00001646 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001647 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001648 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001649
1650 if (N) {
1651 N->markAsSink();
1652 UndefReceivers.insert(N);
1653 }
1654
1655 return;
1656 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001657
Ted Kremenek21fe8372009-02-19 04:06:22 +00001658 // "Assume" that the receiver is not NULL.
1659 bool isFeasibleNotNull = false;
1660 Assume(state, L, true, isFeasibleNotNull);
1661
1662 // "Assume" that the receiver is NULL.
1663 bool isFeasibleNull = false;
1664 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1665
1666 if (isFeasibleNull) {
1667 // Check if the receiver was nil and the return value a struct.
1668 if (ME->getType()->isRecordType()) {
1669 // The [0 ...] expressions will return garbage. Flag either an
1670 // explicit or implicit error. Because of the structure of this
1671 // function we currently do not bifurfacte the state graph at
1672 // this point.
1673 // FIXME: We should bifurcate and fill the returned struct with
1674 // garbage.
1675 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1676 N->markAsSink();
1677 if (isFeasibleNotNull)
1678 NilReceiverStructRetImplicit.insert(N);
1679 else
1680 NilReceiverStructRetExplicit.insert(N);
1681 }
1682 }
1683 }
1684
Ted Kremeneke448ab42008-05-01 18:33:28 +00001685 // Check if the "raise" message was sent.
1686 if (ME->getSelector() == RaiseSel)
1687 RaisesException = true;
1688 }
1689 else {
1690
1691 IdentifierInfo* ClsName = ME->getClassName();
1692 Selector S = ME->getSelector();
1693
1694 // Check for special instance methods.
1695
1696 if (!NSExceptionII) {
1697 ASTContext& Ctx = getContext();
1698
1699 NSExceptionII = &Ctx.Idents.get("NSException");
1700 }
1701
1702 if (ClsName == NSExceptionII) {
1703
1704 enum { NUM_RAISE_SELECTORS = 2 };
1705
1706 // Lazily create a cache of the selectors.
1707
1708 if (!NSExceptionInstanceRaiseSelectors) {
1709
1710 ASTContext& Ctx = getContext();
1711
1712 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1713
1714 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1715 unsigned idx = 0;
1716
1717 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001718 II.push_back(&Ctx.Idents.get("raise"));
1719 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001720 NSExceptionInstanceRaiseSelectors[idx++] =
1721 Ctx.Selectors.getSelector(II.size(), &II[0]);
1722
1723 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001724 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001725 NSExceptionInstanceRaiseSelectors[idx++] =
1726 Ctx.Selectors.getSelector(II.size(), &II[0]);
1727 }
1728
1729 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1730 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1731 RaisesException = true; break;
1732 }
1733 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001734 }
1735
1736 // Check for any arguments that are uninitialized/undefined.
1737
1738 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1739 I != E; ++I) {
1740
Ted Kremeneka8538d92009-02-13 01:45:31 +00001741 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001742
1743 // Generate an error node for passing an uninitialized/undefined value
1744 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001745 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001746
1747 if (N) {
1748 N->markAsSink();
1749 MsgExprUndefArgs[N] = *I;
1750 }
1751
1752 return;
1753 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001754 }
1755
1756 // Check if we raise an exception. For now treat these as sinks. Eventually
1757 // we will want to handle exceptions properly.
1758
1759 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1760
1761 if (RaisesException)
1762 Builder->BuildSinks = true;
1763
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001764 // Dispatch to plug-in transfer function.
1765
1766 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001767 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001768
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001769 EvalObjCMessageExpr(Dst, ME, Pred);
1770
1771 // Handle the case where no nodes where generated. Auto-generate that
1772 // contains the updated state if we aren't generating sinks.
1773
Ted Kremenekb0533962008-04-18 20:35:30 +00001774 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001775 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001776}
1777
1778//===----------------------------------------------------------------------===//
1779// Transfer functions: Miscellaneous statements.
1780//===----------------------------------------------------------------------===//
1781
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001782void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1783 QualType PtrTy,
1784 Expr* CastE, NodeTy* Pred,
1785 NodeSet& Dst) {
1786 if (!V.isUnknownOrUndef()) {
1787 // FIXME: Determine if the number of bits of the target type is
1788 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00001789 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00001790 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
1791 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001792 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001793 else
1794 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001795}
1796
1797
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001798void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001799 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001800 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001801 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001802
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001803 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001804 T = ExCast->getTypeAsWritten();
1805
Zhongxing Xued340f72008-10-22 08:02:16 +00001806 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001807 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001808 else
1809 Visit(Ex, Pred, S1);
1810
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001811 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00001812 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001813 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001814 Dst.Add(*I1);
1815
Ted Kremenek874d63f2008-01-24 02:02:54 +00001816 return;
1817 }
1818
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001819 // FIXME: The rest of this should probably just go into EvalCall, and
1820 // let the transfer function object be responsible for constructing
1821 // nodes.
1822
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001823 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001824 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001825 const GRState* state = GetState(N);
1826 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00001827 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001828
1829 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001830 if (V.isUnknown()) {
1831 Dst.Add(N);
1832 continue;
1833 }
1834
1835 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00001836 if (V.isUndef())
1837 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001838
1839 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001840 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00001841 C.getCanonicalType(ExTy).getUnqualifiedType())
1842 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001843
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001844 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001845 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001846 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001847 continue;
1848 }
1849
1850 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00001851 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001852 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001853 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001854 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001855 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00001856 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001857 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001858
Ted Kremenekc5302912009-03-05 20:22:13 +00001859 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00001860 }
1861
1862 // Just pass through function and block pointers.
1863 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
1864 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00001865 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001866 }
1867
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001868 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001869 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001870 // We will always decay to a pointer.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001871 V = StateMgr.ArrayToPointer(V);
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001872
1873 // Are we casting from an array to a pointer? If so just pass on
1874 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00001875 if (T->isPointerType())
1876 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001877
1878 // Are we casting from an array to an integer? If so, cast the decayed
1879 // pointer value to an integer.
1880 assert(T->isIntegerType());
1881 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1882 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001883 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001884 continue;
1885 }
1886
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001887 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001888 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
1889 // FIXME: For TypedViewRegions, we should handle the case where the
1890 // underlying symbolic pointer is a function pointer or
1891 // block pointer.
1892
1893 // FIXME: We should handle the case where we strip off view layers to get
1894 // to a desugared type.
1895
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001896 assert(Loc::IsLocType(T));
1897 assert(Loc::IsLocType(ExTy));
1898
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001899 const MemRegion* R = RV->getRegion();
1900 StoreManager& StoreMgr = getStoreManager();
1901
1902 // Delegate to store manager to get the result of casting a region
1903 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001904 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001905
1906 // Inspect the result. If the MemRegion* returned is NULL, this
1907 // expression evaluates to UnknownVal.
1908 R = Res.getRegion();
1909 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1910
1911 // Generate the new node in the ExplodedGraph.
1912 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00001913 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001914 }
1915
Ted Kremenekdc402902009-03-04 00:14:35 +00001916 // If we are casting a symbolic value, make a symbolic region and a
1917 // TypedViewRegion subregion.
1918 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) {
1919 SymbolRef Sym = SV->getSymbol();
Ted Kremenekc5302912009-03-05 20:22:13 +00001920 QualType SymTy = getSymbolManager().getType(Sym);
1921
1922 // Just pass through symbols that are function or block pointers.
1923 if (SymTy->isFunctionPointerType() || SymTy->isBlockPointerType())
1924 goto PassThrough;
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001925
1926 // Are we casting to a function or block pointer?
1927 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
1928 // FIXME: We should verify that the underlying type of the symbolic
1929 // pointer is a void* (or maybe char*). Other things are an abuse
1930 // of the type system.
1931 goto PassThrough;
1932 }
Ted Kremenekc5302912009-03-05 20:22:13 +00001933
Ted Kremenekdc402902009-03-04 00:14:35 +00001934 StoreManager& StoreMgr = getStoreManager();
1935 const MemRegion* R =
1936 StoreMgr.getRegionManager().getSymbolicRegion(Sym, getSymbolManager());
1937
1938 // Delegate to store manager to get the result of casting a region
1939 // to a different type.
1940 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
1941
1942 // Inspect the result. If the MemRegion* returned is NULL, this
1943 // expression evaluates to UnknownVal.
1944 R = Res.getRegion();
1945 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1946
1947 // Generate the new node in the ExplodedGraph.
1948 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
1949 continue;
1950 }
1951
Ted Kremenekc5302912009-03-05 20:22:13 +00001952 // All other cases.
1953 DispatchCast: {
1954 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
1955 EvalCast(V, CastE->getType())));
1956 continue;
1957 }
1958
1959 PassThrough: {
1960 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
1961 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00001962 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00001963}
1964
Ted Kremenek4f090272008-10-27 21:54:31 +00001965void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001966 NodeTy* Pred, NodeSet& Dst,
1967 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00001968 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
1969 NodeSet Tmp;
1970 Visit(ILE, Pred, Tmp);
1971
1972 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001973 const GRState* state = GetState(*I);
1974 SVal ILV = GetSVal(state, ILE);
1975 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00001976
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001977 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001978 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001979 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001980 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00001981 }
1982}
1983
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001984void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001985
Ted Kremenek8369a8b2008-10-06 18:43:53 +00001986 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001987 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00001988
1989 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001990 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00001991
Ted Kremenekefd59942008-12-08 22:47:34 +00001992 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00001993 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00001994
1995 // FIXME: static variables may have an initializer, but the second
1996 // time a function is called those values may not be current.
1997 NodeSet Tmp;
1998
Ted Kremenekaf337412008-11-12 19:24:17 +00001999 if (InitEx)
2000 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002001
2002 if (Tmp.empty())
2003 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002004
2005 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002006 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002007 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002008
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002009 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2010 QualType T = getContext().getCanonicalType(VD->getType());
2011 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2012 // FIXME: Handle multi-dimensional VLAs.
2013
2014 Expr* SE = VLA->getSizeExpr();
2015 SVal Size = GetSVal(state, SE);
2016
2017 if (Size.isUndef()) {
2018 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2019 N->markAsSink();
2020 ExplicitBadSizedVLA.insert(N);
2021 }
2022 continue;
2023 }
2024
2025 bool isFeasibleZero = false;
2026 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2027
2028 bool isFeasibleNotZero = false;
2029 state = Assume(state, Size, true, isFeasibleNotZero);
2030
2031 if (isFeasibleZero) {
2032 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2033 N->markAsSink();
2034 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2035 else ExplicitBadSizedVLA.insert(N);
2036 }
2037 }
2038
2039 if (!isFeasibleNotZero)
2040 continue;
2041 }
2042
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002043 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002044 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002045 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002046 QualType T = VD->getType();
2047
2048 // Recover some path-sensitivity if a scalar value evaluated to
2049 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002050 if (InitVal.isUnknown() ||
2051 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenekaf337412008-11-12 19:24:17 +00002052 if (Loc::IsLocType(T)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002053 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002054 InitVal = loc::SymbolVal(Sym);
2055 }
Ted Kremenek062e2f92008-11-13 06:10:40 +00002056 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002057 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002058 InitVal = nonloc::SymbolVal(Sym);
2059 }
2060 }
2061
Ted Kremeneka8538d92009-02-13 01:45:31 +00002062 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002063
2064 // The next thing to do is check if the GRTransferFuncs object wants to
2065 // update the state based on the new binding. If the GRTransferFunc
2066 // object doesn't do anything, just auto-propagate the current state.
2067 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2068 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2069 InitVal);
2070 }
2071 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002072 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002073 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002074 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002075 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002076}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002077
Ted Kremenekf75b1862008-10-30 17:47:32 +00002078namespace {
2079 // This class is used by VisitInitListExpr as an item in a worklist
2080 // for processing the values contained in an InitListExpr.
2081class VISIBILITY_HIDDEN InitListWLItem {
2082public:
2083 llvm::ImmutableList<SVal> Vals;
2084 GRExprEngine::NodeTy* N;
2085 InitListExpr::reverse_iterator Itr;
2086
2087 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2088 InitListExpr::reverse_iterator itr)
2089 : Vals(vals), N(n), Itr(itr) {}
2090};
2091}
2092
2093
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002094void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2095 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002096
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002097 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002098 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002099 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002100
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002101 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002102
Ted Kremeneka49e3672008-10-30 23:14:36 +00002103 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002104
Ted Kremeneka49e3672008-10-30 23:14:36 +00002105 // Handle base case where the initializer has no elements.
2106 // e.g: static int* myArray[] = {};
2107 if (NumInitElements == 0) {
2108 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2109 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2110 return;
2111 }
2112
2113 // Create a worklist to process the initializers.
2114 llvm::SmallVector<InitListWLItem, 10> WorkList;
2115 WorkList.reserve(NumInitElements);
2116 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002117 InitListExpr::reverse_iterator ItrEnd = E->rend();
2118
Ted Kremeneka49e3672008-10-30 23:14:36 +00002119 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002120 while (!WorkList.empty()) {
2121 InitListWLItem X = WorkList.back();
2122 WorkList.pop_back();
2123
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002124 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002125 Visit(*X.Itr, X.N, Tmp);
2126
2127 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002128
Ted Kremenekf75b1862008-10-30 17:47:32 +00002129 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2130 // Get the last initializer value.
2131 state = GetState(*NI);
2132 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2133
2134 // Construct the new list of values by prepending the new value to
2135 // the already constructed list.
2136 llvm::ImmutableList<SVal> NewVals =
2137 getBasicVals().consVals(InitV, X.Vals);
2138
2139 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002140 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002141 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002142
Ted Kremenekf75b1862008-10-30 17:47:32 +00002143 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002144 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002145 }
2146 else {
2147 // Still some initializer values to go. Push them onto the worklist.
2148 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2149 }
2150 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002151 }
Ted Kremenek87903072008-10-30 18:34:31 +00002152
2153 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002154 }
2155
Ted Kremenek062e2f92008-11-13 06:10:40 +00002156 if (T->isUnionType() || T->isVectorType()) {
2157 // FIXME: to be implemented.
2158 // Note: That vectors can return true for T->isIntegerType()
2159 MakeNode(Dst, E, Pred, state);
2160 return;
2161 }
2162
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002163 if (Loc::IsLocType(T) || T->isIntegerType()) {
2164 assert (E->getNumInits() == 1);
2165 NodeSet Tmp;
2166 Expr* Init = E->getInit(0);
2167 Visit(Init, Pred, Tmp);
2168 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2169 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002170 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002171 }
2172 return;
2173 }
2174
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002175
2176 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2177 assert(0 && "unprocessed InitListExpr type");
2178}
Ted Kremenekf233d482008-02-05 00:26:40 +00002179
Sebastian Redl05189992008-11-11 17:56:53 +00002180/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2181void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2182 NodeTy* Pred,
2183 NodeSet& Dst) {
2184 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002185 uint64_t amt;
2186
2187 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002188 if (T == getContext().VoidTy) {
2189 // sizeof(void) == 1 byte.
2190 amt = 1;
2191 }
2192 else if (!T.getTypePtr()->isConstantSizeType()) {
2193 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002194 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002195 }
2196 else if (T->isObjCInterfaceType()) {
2197 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2198 // the compiler has laid out its representation. Just report Unknown
2199 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002200 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002201 }
2202 else {
2203 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002204 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002205 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002206 }
2207 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002208 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002209
Ted Kremenek0e561a32008-03-21 21:30:14 +00002210 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002211 BindExpr(GetState(Pred), Ex,
2212 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002213}
2214
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002215
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002216void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002217 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002218
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002219 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002220
2221 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002222 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002223
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002224 case UnaryOperator::Deref: {
2225
2226 Expr* Ex = U->getSubExpr()->IgnoreParens();
2227 NodeSet Tmp;
2228 Visit(Ex, Pred, Tmp);
2229
2230 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002231
Ted Kremeneka8538d92009-02-13 01:45:31 +00002232 const GRState* state = GetState(*I);
2233 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002234
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002235 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002236 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002237 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002238 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002239 }
2240
2241 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002242 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002243
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002244 case UnaryOperator::Real: {
2245
2246 Expr* Ex = U->getSubExpr()->IgnoreParens();
2247 NodeSet Tmp;
2248 Visit(Ex, Pred, Tmp);
2249
2250 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2251
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002252 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002253 if (Ex->getType()->isAnyComplexType()) {
2254 // Just report "Unknown."
2255 Dst.Add(*I);
2256 continue;
2257 }
2258
2259 // For all other types, UnaryOperator::Real is an identity operation.
2260 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002261 const GRState* state = GetState(*I);
2262 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002263 }
2264
2265 return;
2266 }
2267
2268 case UnaryOperator::Imag: {
2269
2270 Expr* Ex = U->getSubExpr()->IgnoreParens();
2271 NodeSet Tmp;
2272 Visit(Ex, Pred, Tmp);
2273
2274 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002275 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002276 if (Ex->getType()->isAnyComplexType()) {
2277 // Just report "Unknown."
2278 Dst.Add(*I);
2279 continue;
2280 }
2281
2282 // For all other types, UnaryOperator::Float returns 0.
2283 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002284 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002285 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002286 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002287 }
2288
2289 return;
2290 }
2291
2292 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002293 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002294 Dst.Add(Pred);
2295 return;
2296
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002297 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002298 case UnaryOperator::Extension: {
2299
2300 // Unary "+" is a no-op, similar to a parentheses. We still have places
2301 // where it may be a block-level expression, so we need to
2302 // generate an extra node that just propagates the value of the
2303 // subexpression.
2304
2305 Expr* Ex = U->getSubExpr()->IgnoreParens();
2306 NodeSet Tmp;
2307 Visit(Ex, Pred, Tmp);
2308
2309 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002310 const GRState* state = GetState(*I);
2311 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002312 }
2313
2314 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002315 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002316
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002317 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002318
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002319 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002320 Expr* Ex = U->getSubExpr()->IgnoreParens();
2321 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002322 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002323
2324 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002325 const GRState* state = GetState(*I);
2326 SVal V = GetSVal(state, Ex);
2327 state = BindExpr(state, U, V);
2328 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002329 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002330
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002331 return;
2332 }
2333
2334 case UnaryOperator::LNot:
2335 case UnaryOperator::Minus:
2336 case UnaryOperator::Not: {
2337
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002338 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002339 Expr* Ex = U->getSubExpr()->IgnoreParens();
2340 NodeSet Tmp;
2341 Visit(Ex, Pred, Tmp);
2342
2343 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002344 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002345
2346 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002347 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002348
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002349 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002350 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002351 continue;
2352 }
2353
Ted Kremenek60595da2008-11-15 04:01:56 +00002354// QualType DstT = getContext().getCanonicalType(U->getType());
2355// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2356//
2357// if (DstT != SrcT) // Perform promotions.
2358// V = EvalCast(V, DstT);
2359//
2360// if (V.isUnknownOrUndef()) {
2361// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2362// continue;
2363// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002364
2365 switch (U->getOpcode()) {
2366 default:
2367 assert(false && "Invalid Opcode.");
2368 break;
2369
2370 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002371 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002372 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002373 break;
2374
2375 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002376 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002377 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002378 break;
2379
2380 case UnaryOperator::LNot:
2381
2382 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2383 //
2384 // Note: technically we do "E == 0", but this is the same in the
2385 // transfer functions as "0 == E".
2386
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002387 if (isa<Loc>(V)) {
2388 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
2389 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002390 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002391 }
2392 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002393 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002394#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002395 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002396 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002397#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002398 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002399 continue;
2400#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002401 }
2402
2403 break;
2404 }
2405
Ted Kremeneka8538d92009-02-13 01:45:31 +00002406 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002407 }
2408
2409 return;
2410 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002411 }
2412
2413 // Handle ++ and -- (both pre- and post-increment).
2414
2415 assert (U->isIncrementDecrementOp());
2416 NodeSet Tmp;
2417 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002418 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002419
2420 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2421
Ted Kremeneka8538d92009-02-13 01:45:31 +00002422 const GRState* state = GetState(*I);
2423 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002424
2425 // Perform a load.
2426 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002427 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002428
2429 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2430
Ted Kremeneka8538d92009-02-13 01:45:31 +00002431 state = GetState(*I2);
2432 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002433
2434 // Propagate unknown and undefined values.
2435 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002436 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002437 continue;
2438 }
2439
Ted Kremenek21028dd2009-03-11 03:54:24 +00002440 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002441 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2442 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002443
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002444 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
Ted Kremeneka8538d92009-02-13 01:45:31 +00002445 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002446
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002447 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002448 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002449 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002450 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002451}
2452
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002453void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2454 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2455}
2456
2457void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2458 AsmStmt::outputs_iterator I,
2459 AsmStmt::outputs_iterator E,
2460 NodeTy* Pred, NodeSet& Dst) {
2461 if (I == E) {
2462 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2463 return;
2464 }
2465
2466 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002467 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002468
2469 ++I;
2470
2471 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2472 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2473}
2474
2475void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2476 AsmStmt::inputs_iterator I,
2477 AsmStmt::inputs_iterator E,
2478 NodeTy* Pred, NodeSet& Dst) {
2479 if (I == E) {
2480
2481 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002482 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002483
2484 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2485 // which interprets the inline asm and stores proper results in the
2486 // outputs.
2487
Ted Kremeneka8538d92009-02-13 01:45:31 +00002488 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002489
2490 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2491 OE = A->end_outputs(); OI != OE; ++OI) {
2492
Ted Kremeneka8538d92009-02-13 01:45:31 +00002493 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002494 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002495
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002496 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002497 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002498 }
2499
Ted Kremeneka8538d92009-02-13 01:45:31 +00002500 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002501 return;
2502 }
2503
2504 NodeSet Tmp;
2505 Visit(*I, Pred, Tmp);
2506
2507 ++I;
2508
2509 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2510 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2511}
2512
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002513void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2514 assert (Builder && "GRStmtNodeBuilder must be defined.");
2515
2516 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002517
Ted Kremenek186350f2008-04-23 20:12:28 +00002518 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2519 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002520
Ted Kremenek729a9a22008-07-17 23:15:45 +00002521 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002522
Ted Kremenekb0533962008-04-18 20:35:30 +00002523 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002524
Ted Kremenekb0533962008-04-18 20:35:30 +00002525 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002526 MakeNode(Dst, S, Pred, GetState(Pred));
2527}
2528
Ted Kremenek02737ed2008-03-31 15:02:58 +00002529void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2530
2531 Expr* R = S->getRetValue();
2532
2533 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002534 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002535 return;
2536 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002537
Ted Kremenek5917d782008-11-21 00:27:44 +00002538 NodeSet Tmp;
2539 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002540
Ted Kremenek5917d782008-11-21 00:27:44 +00002541 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2542 SVal X = GetSVal((*I)->getState(), R);
2543
2544 // Check if we return the address of a stack variable.
2545 if (isa<loc::MemRegionVal>(X)) {
2546 // Determine if the value is on the stack.
2547 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002548
Ted Kremenek5917d782008-11-21 00:27:44 +00002549 if (R && getStateManager().hasStackStorage(R)) {
2550 // Create a special node representing the error.
2551 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2552 N->markAsSink();
2553 RetsStackAddr.insert(N);
2554 }
2555 continue;
2556 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002557 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002558 // Check if we return an undefined value.
2559 else if (X.isUndef()) {
2560 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2561 N->markAsSink();
2562 RetsUndef.insert(N);
2563 }
2564 continue;
2565 }
2566
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002567 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002568 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002569}
Ted Kremenek55deb972008-03-25 00:34:37 +00002570
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002571//===----------------------------------------------------------------------===//
2572// Transfer functions: Binary operators.
2573//===----------------------------------------------------------------------===//
2574
Ted Kremeneka8538d92009-02-13 01:45:31 +00002575const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002576 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002577
2578 // Divide by undefined? (potentially zero)
2579
2580 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002581 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002582
2583 if (DivUndef) {
2584 DivUndef->markAsSink();
2585 ExplicitBadDivides.insert(DivUndef);
2586 }
2587
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002588 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002589 }
2590
2591 // Check for divide/remainder-by-zero.
2592 // First, "assume" that the denominator is 0 or undefined.
2593
2594 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002595 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002596
2597 // Second, "assume" that the denominator cannot be 0.
2598
2599 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002600 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002601
2602 // Create the node for the divide-by-zero (if it occurred).
2603
2604 if (isFeasibleZero)
2605 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2606 DivZeroNode->markAsSink();
2607
2608 if (isFeasibleNotZero)
2609 ImplicitBadDivides.insert(DivZeroNode);
2610 else
2611 ExplicitBadDivides.insert(DivZeroNode);
2612
2613 }
2614
Ted Kremeneka8538d92009-02-13 01:45:31 +00002615 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002616}
2617
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002618void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002619 GRExprEngine::NodeTy* Pred,
2620 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002621
2622 NodeSet Tmp1;
2623 Expr* LHS = B->getLHS()->IgnoreParens();
2624 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002625
Ted Kremenek759623e2008-12-06 02:39:30 +00002626 // FIXME: Add proper support for ObjCKVCRefExpr.
2627 if (isa<ObjCKVCRefExpr>(LHS)) {
2628 Visit(RHS, Pred, Dst);
2629 return;
2630 }
2631
2632
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002633 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002634 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002635 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002636 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002637
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002638 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002639
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002640 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002641
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002642 // Process the RHS.
2643
2644 NodeSet Tmp2;
2645 Visit(RHS, *I1, Tmp2);
2646
2647 // With both the LHS and RHS evaluated, process the operation itself.
2648
2649 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002650
Ted Kremeneka8538d92009-02-13 01:45:31 +00002651 const GRState* state = GetState(*I2);
2652 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002653
Ted Kremeneka8538d92009-02-13 01:45:31 +00002654 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002655 BinaryOperator::Opcode Op = B->getOpcode();
2656
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002657 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002658
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002659 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002660
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002661 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002662 // FIXME: Handle structs.
2663 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002664
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002665 if ((RightV.isUnknown() ||
2666 !getConstraintManager().canReasonAbout(RightV))
2667 && (Loc::IsLocType(T) ||
2668 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002669 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00002670 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002671
Ted Kremenek062e2f92008-11-13 06:10:40 +00002672 RightV = Loc::IsLocType(T)
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002673 ? cast<SVal>(loc::SymbolVal(Sym))
2674 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002675 }
2676
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002677 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002678 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002679 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2680 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002681 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002682 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002683
2684 case BinaryOperator::Div:
2685 case BinaryOperator::Rem:
2686
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002687 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002688 if (RHS->getType()->isIntegerType() &&
2689 RHS->getType()->isScalarType()) {
2690
Ted Kremeneka8538d92009-02-13 01:45:31 +00002691 state = CheckDivideZero(B, state, *I2, RightV);
2692 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002693 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002694
2695 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002696
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002697 default: {
2698
2699 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002700 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002701
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002702 // Process non-assignements except commas or short-circuited
2703 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002704
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002705 SVal Result = EvalBinOp(Op, LeftV, RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002706
2707 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002708 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002709 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002710 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002711 }
2712 else
2713 Dst.Add(*I2);
2714
Ted Kremenek89063af2008-02-21 19:15:37 +00002715 continue;
2716 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002717
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002718 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002719
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002720 // The operands were *not* undefined, but the result is undefined.
2721 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002722
Ted Kremeneka8538d92009-02-13 01:45:31 +00002723 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002724 UndefNode->markAsSink();
2725 UndefResults.insert(UndefNode);
2726 }
2727
2728 continue;
2729 }
2730
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002731 // Otherwise, create a new node.
2732
Ted Kremeneka8538d92009-02-13 01:45:31 +00002733 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002734 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002735 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002736 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002737
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002738 assert (B->isCompoundAssignmentOp());
2739
Ted Kremenekcad29962009-02-07 00:52:24 +00002740 switch (Op) {
2741 default:
2742 assert(0 && "Invalid opcode for compound assignment.");
2743 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2744 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2745 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2746 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2747 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2748 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2749 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2750 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2751 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2752 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002753 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002754
2755 // Perform a load (the LHS). This performs the checks for
2756 // null dereferences, and so on.
2757 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002758 SVal location = GetSVal(state, LHS);
2759 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002760
2761 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2762
Ted Kremeneka8538d92009-02-13 01:45:31 +00002763 state = GetState(*I3);
2764 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002765
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002766 // Check for divide-by-zero.
2767 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002768 && RHS->getType()->isIntegerType()
2769 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002770
2771 // CheckDivideZero returns a new state where the denominator
2772 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002773 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002774
Ted Kremeneka8538d92009-02-13 01:45:31 +00002775 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002776 continue;
2777 }
2778
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002779 // Propagate undefined values (left-side).
2780 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002781 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002782 continue;
2783 }
2784
2785 // Propagate unknown values (left and right-side).
2786 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002787 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2788 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002789 continue;
2790 }
2791
2792 // At this point:
2793 //
2794 // The LHS is not Undef/Unknown.
2795 // The RHS is not Unknown.
2796
2797 // Get the computation type.
2798 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002799 CTy = getContext().getCanonicalType(CTy);
2800
2801 QualType LTy = getContext().getCanonicalType(LHS->getType());
2802 QualType RTy = getContext().getCanonicalType(RHS->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002803
2804 // Perform promotions.
Ted Kremenek60595da2008-11-15 04:01:56 +00002805 if (LTy != CTy) V = EvalCast(V, CTy);
2806 if (RTy != CTy) RightV = EvalCast(RightV, CTy);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002807
2808 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002809 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002810 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00002811 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00002812 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002813 continue;
2814 }
2815
Ted Kremenek60595da2008-11-15 04:01:56 +00002816 // Compute the result of the operation.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002817 SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002818
2819 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002820 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002821 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002822 UndefNode->markAsSink();
2823 UndefResults.insert(UndefNode);
2824 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002825 continue;
2826 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002827
2828 // EXPERIMENTAL: "Conjured" symbols.
2829 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002830
2831 SVal LHSVal;
2832
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002833 if ((Result.isUnknown() ||
2834 !getConstraintManager().canReasonAbout(Result))
2835 && (Loc::IsLocType(CTy)
2836 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002837
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002838 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002839
Ted Kremenek60595da2008-11-15 04:01:56 +00002840 // The symbolic value is actually for the type of the left-hand side
2841 // expression, not the computation type, as this is the value the
2842 // LValue on the LHS will bind to.
Ted Kremenek2dabd432008-12-05 02:27:51 +00002843 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002844 LHSVal = Loc::IsLocType(LTy)
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002845 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek60595da2008-11-15 04:01:56 +00002846 : cast<SVal>(nonloc::SymbolVal(Sym));
2847
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002848 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002849 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002850 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002851 else {
2852 // The left-hand side may bind to a different value then the
2853 // computation type.
2854 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2855 }
2856
Ted Kremeneka8538d92009-02-13 01:45:31 +00002857 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2858 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002859 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002860 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002861 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002862}
Ted Kremenekee985462008-01-16 18:18:48 +00002863
2864//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002865// Transfer-function Helpers.
2866//===----------------------------------------------------------------------===//
2867
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002868void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002869 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002870 NonLoc L, NonLoc R,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002871 ExplodedNode<GRState>* Pred) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002872
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002873 GRStateSet OStates;
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002874 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
2875
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002876 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002877 MakeNode(Dst, Ex, Pred, *I);
2878}
2879
Ted Kremeneka8538d92009-02-13 01:45:31 +00002880void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002881 Expr* Ex, BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002882 NonLoc L, NonLoc R) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002883
Ted Kremeneka8538d92009-02-13 01:45:31 +00002884 GRStateSet::AutoPopulate AP(OStates, state);
2885 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002886}
2887
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002888SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R) {
2889
2890 if (L.isUndef() || R.isUndef())
2891 return UndefinedVal();
2892
2893 if (L.isUnknown() || R.isUnknown())
2894 return UnknownVal();
2895
2896 if (isa<Loc>(L)) {
2897 if (isa<Loc>(R))
2898 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2899 else
2900 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2901 }
2902
2903 if (isa<Loc>(R)) {
2904 // Support pointer arithmetic where the increment/decrement operand
2905 // is on the left and the pointer on the right.
2906
2907 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2908
2909 // Commute the operands.
2910 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2911 cast<NonLoc>(L));
2912 }
2913 else
2914 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
2915 cast<NonLoc>(R));
2916}
2917
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002918//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002919// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002920//===----------------------------------------------------------------------===//
2921
Ted Kremenekaa66a322008-01-16 21:46:15 +00002922#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002923static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002924static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002925
Ted Kremenekaa66a322008-01-16 21:46:15 +00002926namespace llvm {
2927template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002928struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00002929 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00002930
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002931 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2932
2933 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00002934 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00002935 GraphPrintCheckerState->isUndefDeref(N) ||
2936 GraphPrintCheckerState->isUndefStore(N) ||
2937 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00002938 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2939 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00002940 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00002941 GraphPrintCheckerState->isBadCall(N) ||
2942 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002943 return "color=\"red\",style=\"filled\"";
2944
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002945 if (GraphPrintCheckerState->isNoReturnCall(N))
2946 return "color=\"blue\",style=\"filled\"";
2947
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00002948 return "";
2949 }
Ted Kremeneked4de312008-02-06 03:56:15 +00002950
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002951 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00002952 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00002953
2954 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00002955 ProgramPoint Loc = N->getLocation();
2956
2957 switch (Loc.getKind()) {
2958 case ProgramPoint::BlockEntranceKind:
2959 Out << "Block Entrance: B"
2960 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2961 break;
2962
2963 case ProgramPoint::BlockExitKind:
2964 assert (false);
2965 break;
2966
Ted Kremenekaa66a322008-01-16 21:46:15 +00002967 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00002968 if (isa<PostStmt>(Loc)) {
2969 const PostStmt& L = cast<PostStmt>(Loc);
2970 Stmt* S = L.getStmt();
2971 SourceLocation SLoc = S->getLocStart();
2972
2973 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
2974 llvm::raw_os_ostream OutS(Out);
2975 S->printPretty(OutS);
2976 OutS.flush();
2977
2978 if (SLoc.isFileID()) {
2979 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00002980 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2981 << " col="
2982 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2983 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00002984 }
2985
2986 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2987 Out << "\\|Implicit-Null Dereference.\\l";
2988 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2989 Out << "\\|Explicit-Null Dereference.\\l";
2990 else if (GraphPrintCheckerState->isUndefDeref(N))
2991 Out << "\\|Dereference of undefialied value.\\l";
2992 else if (GraphPrintCheckerState->isUndefStore(N))
2993 Out << "\\|Store to Undefined Loc.";
2994 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
2995 Out << "\\|Explicit divide-by zero or undefined value.";
2996 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
2997 Out << "\\|Implicit divide-by zero or undefined value.";
2998 else if (GraphPrintCheckerState->isUndefResult(N))
2999 Out << "\\|Result of operation is undefined.";
3000 else if (GraphPrintCheckerState->isNoReturnCall(N))
3001 Out << "\\|Call to function marked \"noreturn\".";
3002 else if (GraphPrintCheckerState->isBadCall(N))
3003 Out << "\\|Call to NULL/Undefined.";
3004 else if (GraphPrintCheckerState->isUndefArg(N))
3005 Out << "\\|Argument in call is undefined";
3006
3007 break;
3008 }
3009
Ted Kremenekaa66a322008-01-16 21:46:15 +00003010 const BlockEdge& E = cast<BlockEdge>(Loc);
3011 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3012 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003013
3014 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003015
3016 SourceLocation SLoc = T->getLocStart();
3017
Ted Kremenekb38911f2008-01-30 23:03:39 +00003018 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003019
Ted Kremeneka95d3752008-09-13 05:16:45 +00003020 llvm::raw_os_ostream OutS(Out);
3021 E.getSrc()->printTerminator(OutS);
3022 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003023
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003024 if (SLoc.isFileID()) {
3025 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003026 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3027 << " col="
3028 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003029 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003030
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003031 if (isa<SwitchStmt>(T)) {
3032 Stmt* Label = E.getDst()->getLabel();
3033
3034 if (Label) {
3035 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3036 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003037 llvm::raw_os_ostream OutS(Out);
3038 C->getLHS()->printPretty(OutS);
3039 OutS.flush();
3040
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003041 if (Stmt* RHS = C->getRHS()) {
3042 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003043 RHS->printPretty(OutS);
3044 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003045 }
3046
3047 Out << ":";
3048 }
3049 else {
3050 assert (isa<DefaultStmt>(Label));
3051 Out << "\\ldefault:";
3052 }
3053 }
3054 else
3055 Out << "\\l(implicit) default:";
3056 }
3057 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003058 // FIXME
3059 }
3060 else {
3061 Out << "\\lCondition: ";
3062 if (*E.getSrc()->succ_begin() == E.getDst())
3063 Out << "true";
3064 else
3065 Out << "false";
3066 }
3067
3068 Out << "\\l";
3069 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003070
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003071 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3072 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003073 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003074 }
3075 }
3076
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003077 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003078
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003079 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3080 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003081
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003082 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003083 return Out.str();
3084 }
3085};
3086} // end llvm namespace
3087#endif
3088
Ted Kremenekffe0f432008-03-07 22:58:01 +00003089#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003090template <typename ITERATOR>
3091GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3092
3093template <>
3094GRExprEngine::NodeTy*
3095GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3096 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3097 return I->first;
3098}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003099#endif
3100
3101void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003102#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003103 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003104 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003105
3106 // Flush any outstanding reports to make sure we cover all the nodes.
3107 // This does not cause them to get displayed.
3108 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3109 const_cast<BugType*>(*I)->FlushReports(BR);
3110
3111 // Iterate through the reports and get their nodes.
3112 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3113 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3114 const BugReportEquivClass& EQ = *I2;
3115 const BugReport &R = **EQ.begin();
3116 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3117 if (N) Src.push_back(N);
3118 }
3119 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003120
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003121 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003122 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003123 else {
3124 GraphPrintCheckerState = this;
3125 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003126
Ted Kremenekffe0f432008-03-07 22:58:01 +00003127 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003128
3129 GraphPrintCheckerState = NULL;
3130 GraphPrintSourceManager = NULL;
3131 }
3132#endif
3133}
3134
3135void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3136#ifndef NDEBUG
3137 GraphPrintCheckerState = this;
3138 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003139
Ted Kremenekcf118d42009-02-04 23:49:09 +00003140 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003141
Ted Kremenekcf118d42009-02-04 23:49:09 +00003142 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003143 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003144 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003145 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003146
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003147 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003148 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003149#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003150}