blob: 487aac06c5823634962c70050092628c0daa216c [file] [log] [blame]
Ted Kremenek50df4f42008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenekc48b8e42008-01-31 02:35:41 +00002//
Ted Kremenek2e160602008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenek68d70a82008-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 Kremenek50df4f42008-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 Kremenek68d70a82008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek75732212009-04-01 06:52:48 +000016#include "clang/AST/ParentMap.h"
Ted Kremenek50df4f42008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek0e80dea2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek8b41e8c2008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek820c73b2009-03-11 02:41:36 +000021#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek3862eb12008-02-14 22:36:46 +000022#include "llvm/Support/Streams.h"
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000023#include "llvm/ADT/ImmutableList.h"
24#include "llvm/Support/Compiler.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenekf22f8682008-07-10 22:03:41 +000026
Ted Kremenek9f6b1612008-02-27 06:07:00 +000027#ifndef NDEBUG
28#include "llvm/Support/GraphWriter.h"
29#include <sstream>
30#endif
31
Ted Kremenekd4467432008-02-14 22:16:04 +000032using namespace clang;
33using llvm::dyn_cast;
34using llvm::cast;
35using llvm::APSInt;
Ted Kremenekf031b872008-01-23 19:59:44 +000036
Ted Kremenekca5f6202008-04-15 23:06:53 +000037//===----------------------------------------------------------------------===//
38// Engine construction and deletion.
39//===----------------------------------------------------------------------===//
40
Ted Kremenek7d4d9f32008-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;
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000049 Checks AllStmts;
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000050
51public:
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000052 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
53 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000054
55 virtual ~MappedBatchAuditor() {
56 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
57
58 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
59 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
60
61 GRSimpleAPICheck* check = *I;
62
63 if (AlreadyVisited.count(check))
64 continue;
65
66 AlreadyVisited.insert(check);
67 delete check;
68 }
69 }
70
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000071 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000072 assert (A && "Check cannot be null.");
73 void* key = reinterpret_cast<void*>((uintptr_t) C);
74 MapTy::iterator I = M.find(key);
75 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
76 }
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000077
78 void AddCheck(GRSimpleAPICheck *A) {
79 assert (A && "Check cannot be null.");
80 AllStmts = F.Concat(A, AllStmts);
81 }
Ted Kremenekbf6babf2009-02-04 23:49:09 +000082
Ted Kremenekabd89ac2008-08-13 04:27:00 +000083 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000084 // First handle the auditors that accept all statements.
85 bool isSink = false;
86 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
87 isSink |= (*I)->Audit(N, VMgr);
88
89 // Next handle the auditors that accept only specific statements.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000090 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
91 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
92 MapTy::iterator MI = M.find(key);
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +000093 if (MI != M.end()) {
94 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
95 isSink |= (*I)->Audit(N, VMgr);
96 }
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000097
Ted Kremenek7d4d9f32008-07-11 18:37:32 +000098 return isSink;
99 }
100};
101
102} // end anonymous namespace
103
104//===----------------------------------------------------------------------===//
105// Engine construction and deletion.
106//===----------------------------------------------------------------------===//
107
Ted Kremenek5f20a632008-05-01 18:33:28 +0000108static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
109 IdentifierInfo* II = &Ctx.Idents.get(name);
110 return Ctx.Selectors.getSelector(0, &II);
111}
112
Ted Kremenekf973eb02008-03-09 18:05:48 +0000113
Ted Kremenek1607f512008-07-02 20:13:38 +0000114GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000115 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek8f520972009-02-25 22:32:02 +0000116 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000117 StoreManagerCreator SMC,
118 ConstraintManagerCreator CMC)
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek1607f512008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000122 Builder(NULL),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000123 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000124 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek5f20a632008-05-01 18:33:28 +0000125 CurrentStmt(NULL),
Zhongxing Xu8833aa92008-12-22 08:30:52 +0000126 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
127 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000128 PurgeDead(purgeDead),
Ted Kremenek8f520972009-02-25 22:32:02 +0000129 BR(BRD, *this),
130 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000131
Ted Kremenek72f52c02008-06-20 21:45:25 +0000132GRExprEngine::~GRExprEngine() {
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000133 BR.FlushReports();
Ted Kremenek5f20a632008-05-01 18:33:28 +0000134 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000135}
136
Ted Kremenekca5f6202008-04-15 23:06:53 +0000137//===----------------------------------------------------------------------===//
138// Utility methods.
139//===----------------------------------------------------------------------===//
140
Ted Kremenek0a6a80b2008-04-23 20:12:28 +0000141
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000142void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenekc7469542008-07-17 23:15:45 +0000143 StateMgr.TF = tf;
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000144 tf->RegisterChecks(getBugReporter());
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000145 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000146}
147
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000148void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
149 if (!BatchAuditor)
150 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
151
152 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek0e80dea2008-04-09 21:41:14 +0000153}
154
Ted Kremenek9fb9a4b2009-03-30 17:53:05 +0000155void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
156 if (!BatchAuditor)
157 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
158
159 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
160}
161
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000162const GRState* GRExprEngine::getInitialState() {
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000163 return StateMgr.getInitialState();
Ted Kremenek7f5ebc72008-02-04 21:59:01 +0000164}
165
Ted Kremenekca5f6202008-04-15 23:06:53 +0000166//===----------------------------------------------------------------------===//
167// Top-level transfer function logic (Dispatcher).
168//===----------------------------------------------------------------------===//
169
170void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
171
Ted Kremenek820c73b2009-03-11 02:41:36 +0000172 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
173 S->getLocStart(),
174 "Error evaluating statement");
175
Ted Kremenekca5f6202008-04-15 23:06:53 +0000176 Builder = &builder;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000177 EntryNode = builder.getLastNode();
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000178
179 // FIXME: Consolidate.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000180 CurrentStmt = S;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000181 StateMgr.CurrentStmt = S;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000182
183 // Set up our simple checks.
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000184 if (BatchAuditor)
185 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000186
Ted Kremenek7d4d9f32008-07-11 18:37:32 +0000187 // Create the cleaned state.
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000188 SymbolReaper SymReaper(Liveness, SymMgr);
189 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
190 CurrentStmt, SymReaper)
191 : EntryNode->getState();
192
Ted Kremenek7487f942008-04-24 18:31:42 +0000193 // Process any special transfer function for dead symbols.
Ted Kremenek7487f942008-04-24 18:31:42 +0000194 NodeSet Tmp;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000195
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000196 if (!SymReaper.hasDeadSymbols())
Ted Kremenekfa7be362008-04-24 23:35:58 +0000197 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000198 else {
199 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000200 SaveOr OldHasGen(Builder->HasGeneratedNode);
201
Ted Kremenekf05eec42008-06-18 05:34:07 +0000202 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
203 Builder->PurgingDeadSymbols = true;
204
Ted Kremenekc7469542008-07-17 23:15:45 +0000205 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek5c0729b2009-01-21 22:26:05 +0000206 CleanedState, SymReaper);
Ted Kremenekfa7be362008-04-24 23:35:58 +0000207
208 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
209 Tmp.Add(EntryNode);
Ted Kremenek7487f942008-04-24 18:31:42 +0000210 }
Ted Kremenekfa7be362008-04-24 23:35:58 +0000211
212 bool HasAutoGenerated = false;
213
Ted Kremenek7487f942008-04-24 18:31:42 +0000214 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekfa7be362008-04-24 23:35:58 +0000215
216 NodeSet Dst;
217
Ted Kremenek7487f942008-04-24 18:31:42 +0000218 // Set the cleaned state.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000219 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
220
Ted Kremenek7487f942008-04-24 18:31:42 +0000221 // Visit the statement.
Ted Kremenekfa7be362008-04-24 23:35:58 +0000222 Visit(S, *I, Dst);
223
224 // Do we need to auto-generate a node? We only need to do this to generate
225 // a node with a "cleaned" state; GRCoreEngine will actually handle
226 // auto-transitions for other cases.
227 if (Dst.size() == 1 && *Dst.begin() == EntryNode
228 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
229 HasAutoGenerated = true;
230 builder.generateNode(S, GetState(EntryNode), *I);
231 }
Ted Kremenek7487f942008-04-24 18:31:42 +0000232 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000233
Ted Kremenekca5f6202008-04-15 23:06:53 +0000234 // NULL out these variables to cleanup.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000235 CleanedState = NULL;
Ted Kremenekfa7be362008-04-24 23:35:58 +0000236 EntryNode = NULL;
Ted Kremenekfa81dff2008-07-17 21:27:31 +0000237
238 // FIXME: Consolidate.
239 StateMgr.CurrentStmt = 0;
240 CurrentStmt = 0;
241
Ted Kremenekfa7be362008-04-24 23:35:58 +0000242 Builder = NULL;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000243}
244
Ted Kremenek820c73b2009-03-11 02:41:36 +0000245void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
246 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
247 S->getLocStart(),
248 "Error evaluating statement");
249
Ted Kremenekca5f6202008-04-15 23:06:53 +0000250 // FIXME: add metadata to the CFG so that we can disable
251 // this check when we KNOW that there is no block-level subexpression.
252 // The motivation is that this check requires a hashtable lookup.
253
254 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
255 Dst.Add(Pred);
256 return;
257 }
258
259 switch (S->getStmtClass()) {
260
261 default:
262 // Cases we intentionally have "default" handle:
263 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
264
265 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
266 break;
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000267
268 case Stmt::ArraySubscriptExprClass:
269 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
270 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000271
272 case Stmt::AsmStmtClass:
273 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
274 break;
275
276 case Stmt::BinaryOperatorClass: {
277 BinaryOperator* B = cast<BinaryOperator>(S);
278
279 if (B->isLogicalOp()) {
280 VisitLogicalExpr(B, Pred, Dst);
281 break;
282 }
283 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000284 const GRState* state = GetState(Pred);
285 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000286 break;
287 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000288
Ted Kremenek8f520972009-02-25 22:32:02 +0000289 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
290 NodeSet Tmp;
291 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenek34a611b2009-02-25 23:32:10 +0000292 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek8f520972009-02-25 22:32:02 +0000293 }
294 else
295 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
296
Ted Kremenekca5f6202008-04-15 23:06:53 +0000297 break;
298 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000299
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000300 case Stmt::CallExprClass:
301 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000302 CallExpr* C = cast<CallExpr>(S);
303 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek034a9472008-11-14 19:47:18 +0000304 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000305 }
Ted Kremenek034a9472008-11-14 19:47:18 +0000306
Ted Kremenekca5f6202008-04-15 23:06:53 +0000307 // FIXME: ChooseExpr is really a constant. We need to fix
308 // the CFG do not model them as explicit control-flow.
309
310 case Stmt::ChooseExprClass: { // __builtin_choose_expr
311 ChooseExpr* C = cast<ChooseExpr>(S);
312 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
313 break;
314 }
315
316 case Stmt::CompoundAssignOperatorClass:
317 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
318 break;
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000319
320 case Stmt::CompoundLiteralExprClass:
321 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
322 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000323
324 case Stmt::ConditionalOperatorClass: { // '?' operator
325 ConditionalOperator* C = cast<ConditionalOperator>(S);
326 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
327 break;
328 }
329
330 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000331 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000332 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000333 break;
334
335 case Stmt::DeclStmtClass:
336 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
337 break;
338
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000339 case Stmt::ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +0000340 case Stmt::CStyleCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000341 CastExpr* C = cast<CastExpr>(S);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000342 VisitCast(C, C->getSubExpr(), Pred, Dst);
343 break;
344 }
Zhongxing Xuebcad732008-10-30 05:02:23 +0000345
346 case Stmt::InitListExprClass:
347 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
348 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000349
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000350 case Stmt::MemberExprClass:
Ted Kremenekd0d86202008-04-21 23:43:38 +0000351 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
352 break;
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000353
354 case Stmt::ObjCIvarRefExprClass:
355 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
356 break;
Ted Kremenek13e167f2008-11-12 19:24:17 +0000357
358 case Stmt::ObjCForCollectionStmtClass:
359 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
360 break;
Ted Kremenekd0d86202008-04-21 23:43:38 +0000361
Ted Kremenekca5f6202008-04-15 23:06:53 +0000362 case Stmt::ObjCMessageExprClass: {
363 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
364 break;
365 }
366
Ted Kremenek3c186252008-12-09 20:18:58 +0000367 case Stmt::ObjCAtThrowStmtClass: {
368 // FIXME: This is not complete. We basically treat @throw as
369 // an abort.
370 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
371 Builder->BuildSinks = true;
372 MakeNode(Dst, S, Pred, GetState(Pred));
373 break;
374 }
375
Ted Kremenekca5f6202008-04-15 23:06:53 +0000376 case Stmt::ParenExprClass:
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000377 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000378 break;
379
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000380 case Stmt::ReturnStmtClass:
381 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
382 break;
383
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000384 case Stmt::SizeOfAlignOfExprClass:
385 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000386 break;
387
388 case Stmt::StmtExprClass: {
389 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000390
391 if (SE->getSubStmt()->body_empty()) {
392 // Empty statement expression.
393 assert(SE->getType() == getContext().VoidTy
394 && "Empty statement expression must have void type.");
395 Dst.Add(Pred);
396 break;
397 }
398
399 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
400 const GRState* state = GetState(Pred);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000401 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremenekfbc09f52009-02-14 05:55:08 +0000402 }
Ted Kremenekca5f6202008-04-15 23:06:53 +0000403 else
404 Dst.Add(Pred);
405
406 break;
407 }
Zhongxing Xu9faabb12008-11-30 05:49:49 +0000408
409 case Stmt::StringLiteralClass:
410 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
411 break;
Ted Kremenekca5f6202008-04-15 23:06:53 +0000412
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000413 case Stmt::UnaryOperatorClass: {
414 UnaryOperator *U = cast<UnaryOperator>(S);
415 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
416 NodeSet Tmp;
417 VisitUnaryOperator(U, Pred, Tmp, false);
418 EvalEagerlyAssume(Dst, Tmp, U);
419 }
420 else
421 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000422 break;
Ted Kremenek8ecca5e2009-03-18 23:49:26 +0000423 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000424 }
425}
426
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000427void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000428
429 Ex = Ex->IgnoreParens();
430
431 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
432 Dst.Add(Pred);
433 return;
434 }
435
436 switch (Ex->getStmtClass()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000437
438 case Stmt::ArraySubscriptExprClass:
439 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
440 return;
441
442 case Stmt::DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000443 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000444 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
445 return;
446
Ted Kremeneke7b0b272008-10-17 00:03:18 +0000447 case Stmt::ObjCIvarRefExprClass:
448 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
449 return;
450
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000451 case Stmt::UnaryOperatorClass:
452 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
453 return;
454
455 case Stmt::MemberExprClass:
456 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
457 return;
Ted Kremenek71c707b2008-10-17 17:24:14 +0000458
Ted Kremenekd83daa52008-10-27 21:54:31 +0000459 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000460 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekd83daa52008-10-27 21:54:31 +0000461 return;
462
Ted Kremenek71c707b2008-10-17 17:24:14 +0000463 case Stmt::ObjCPropertyRefExprClass:
464 // FIXME: Property assignments are lvalues, but not really "locations".
465 // e.g.: self.x = something;
466 // Here the "self.x" really can translate to a method call (setter) when
467 // the assignment is made. Moreover, the entire assignment expression
468 // evaluate to whatever "something" is, not calling the "getter" for
469 // the property (which would make sense since it can have side effects).
470 // We'll probably treat this as a location, but not one that we can
471 // take the address of. Perhaps we need a new SVal class for cases
472 // like thsis?
473 // Note that we have a similar problem for bitfields, since they don't
474 // have "locations" in the sense that we can take their address.
475 Dst.Add(Pred);
Ted Kremenek2aefa732008-10-18 04:08:49 +0000476 return;
Zhongxing Xu2abba442008-10-25 14:18:57 +0000477
478 case Stmt::StringLiteralClass: {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000479 const GRState* state = GetState(Pred);
480 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
481 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu2abba442008-10-25 14:18:57 +0000482 return;
483 }
Ted Kremenek2aefa732008-10-18 04:08:49 +0000484
Ted Kremenek2c829a32008-10-18 04:15:35 +0000485 default:
486 // Arbitrary subexpressions can return aggregate temporaries that
487 // can be used in a lvalue context. We need to enhance our support
488 // of such temporaries in both the environment and the store, so right
489 // now we just do a regular visit.
Douglas Gregore7ef5002009-01-30 17:31:00 +0000490 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek6c833892008-10-25 20:09:21 +0000491 "Other kinds of expressions with non-aggregate/union types do"
492 " not have lvalues.");
Ted Kremenek2aefa732008-10-18 04:08:49 +0000493
Ted Kremenek2c829a32008-10-18 04:15:35 +0000494 Visit(Ex, Pred, Dst);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000495 }
496}
497
498//===----------------------------------------------------------------------===//
499// Block entrance. (Update counters).
500//===----------------------------------------------------------------------===//
501
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000502bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000503 GRBlockCounter BC) {
504
505 return BC.getNumVisited(B->getBlockID()) < 3;
506}
507
508//===----------------------------------------------------------------------===//
509// Branch processing.
510//===----------------------------------------------------------------------===//
511
Ted Kremeneke66ba682009-02-13 01:45:31 +0000512const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenekf22f8682008-07-10 22:03:41 +0000513 Stmt* Terminator,
514 bool branchTaken) {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000515
516 switch (Terminator->getStmtClass()) {
517 default:
Ted Kremeneke66ba682009-02-13 01:45:31 +0000518 return state;
Ted Kremenek99ecce72008-02-26 19:05:15 +0000519
520 case Stmt::BinaryOperatorClass: { // '&&' and '||'
521
522 BinaryOperator* B = cast<BinaryOperator>(Terminator);
523 BinaryOperator::Opcode Op = B->getOpcode();
524
525 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
526
527 // For &&, if we take the true branch, then the value of the whole
528 // expression is that of the RHS expression.
529 //
530 // For ||, if we take the false branch, then the value of the whole
531 // expression is that of the RHS expression.
532
533 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
534 (Op == BinaryOperator::LOr && !branchTaken)
535 ? B->getRHS() : B->getLHS();
536
Ted Kremeneke66ba682009-02-13 01:45:31 +0000537 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000538 }
539
540 case Stmt::ConditionalOperatorClass: { // ?:
541
542 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
543
544 // For ?, if branchTaken == true then the value is either the LHS or
545 // the condition itself. (GNU extension).
546
547 Expr* Ex;
548
549 if (branchTaken)
550 Ex = C->getLHS() ? C->getLHS() : C->getCond();
551 else
552 Ex = C->getRHS();
553
Ted Kremeneke66ba682009-02-13 01:45:31 +0000554 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000555 }
556
557 case Stmt::ChooseExprClass: { // ?:
558
559 ChooseExpr* C = cast<ChooseExpr>(Terminator);
560
561 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneke66ba682009-02-13 01:45:31 +0000562 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000563 }
564 }
565}
566
Ted Kremenekc39c2172009-03-13 16:32:54 +0000567/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
568/// to try to recover some path-sensitivity for casts of symbolic
569/// integers that promote their values (which are currently not tracked well).
570/// This function returns the SVal bound to Condition->IgnoreCasts if all the
571// cast(s) did was sign-extend the original value.
572static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
573 Stmt* Condition, ASTContext& Ctx) {
574
575 Expr *Ex = dyn_cast<Expr>(Condition);
576 if (!Ex)
577 return UnknownVal();
578
579 uint64_t bits = 0;
580 bool bitsInit = false;
581
582 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
583 QualType T = CE->getType();
584
585 if (!T->isIntegerType())
586 return UnknownVal();
587
588 uint64_t newBits = Ctx.getTypeSize(T);
589 if (!bitsInit || newBits < bits) {
590 bitsInit = true;
591 bits = newBits;
592 }
593
594 Ex = CE->getSubExpr();
595 }
596
597 // We reached a non-cast. Is it a symbolic value?
598 QualType T = Ex->getType();
599
600 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
601 return UnknownVal();
602
603 return StateMgr.GetSVal(state, Ex);
604}
605
Ted Kremenek13e167f2008-11-12 19:24:17 +0000606void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenek07baa252008-02-21 18:02:17 +0000607 BranchNodeBuilder& builder) {
Ted Kremenek820c73b2009-03-11 02:41:36 +0000608
Ted Kremenek17c5f112008-02-11 19:21:59 +0000609 // Remove old bindings for subexpressions.
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000610 const GRState* PrevState =
Ted Kremenekf22f8682008-07-10 22:03:41 +0000611 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000612
Ted Kremenek022b6052008-02-15 22:29:00 +0000613 // Check for NULL conditions; e.g. "for(;;)"
614 if (!Condition) {
615 builder.markInfeasible(false);
Ted Kremenek022b6052008-02-15 22:29:00 +0000616 return;
617 }
618
Ted Kremeneke43de222009-03-11 03:54:24 +0000619 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
620 Condition->getLocStart(),
621 "Error evaluating branch");
622
Zhongxing Xu097fc982008-10-17 05:57:07 +0000623 SVal V = GetSVal(PrevState, Condition);
Ted Kremenek90960972008-01-30 23:03:39 +0000624
625 switch (V.getBaseKind()) {
626 default:
627 break;
628
Ted Kremenekc39c2172009-03-13 16:32:54 +0000629 case SVal::UnknownKind: {
630 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
631 if (Ex->getType()->isIntegerType()) {
632 // Try to recover some path-sensitivity. Right now casts of symbolic
633 // integers that promote their values are currently not tracked well.
634 // If 'Condition' is such an expression, try and recover the
635 // underlying value and use that instead.
636 SVal recovered = RecoverCastedSymbol(getStateManager(),
637 builder.getState(), Condition,
638 getContext());
639
640 if (!recovered.isUnknown()) {
641 V = recovered;
642 break;
643 }
644 }
645 }
646
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000647 builder.generateNode(MarkBranch(PrevState, Term, true), true);
648 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenek90960972008-01-30 23:03:39 +0000649 return;
Ted Kremenekc39c2172009-03-13 16:32:54 +0000650 }
Ted Kremenek90960972008-01-30 23:03:39 +0000651
Zhongxing Xu097fc982008-10-17 05:57:07 +0000652 case SVal::UndefinedKind: {
Ted Kremenek90960972008-01-30 23:03:39 +0000653 NodeTy* N = builder.generateNode(PrevState, true);
654
655 if (N) {
656 N->markAsSink();
Ted Kremenekb31af242008-02-28 09:25:22 +0000657 UndefBranches.insert(N);
Ted Kremenek90960972008-01-30 23:03:39 +0000658 }
659
660 builder.markInfeasible(false);
661 return;
662 }
663 }
Ted Kremenek4b170e52008-02-12 18:08:17 +0000664
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000665 // Process the true branch.
Ted Kremenek4b170e52008-02-12 18:08:17 +0000666
Ted Kremenekd4676512008-03-12 21:45:47 +0000667 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000668 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000669
670 if (isFeasible)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000671 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek4b170e52008-02-12 18:08:17 +0000672 else
673 builder.markInfeasible(true);
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000674
675 // Process the false branch.
Ted Kremenek90960972008-01-30 23:03:39 +0000676
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000677 isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000678 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenek90960972008-01-30 23:03:39 +0000679
Ted Kremenek5c6eeb12008-02-29 20:27:50 +0000680 if (isFeasible)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000681 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000682 else
683 builder.markInfeasible(false);
Ted Kremenek6ff3cea2008-01-29 23:32:35 +0000684}
685
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000686/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000687/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000688void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000689
Ted Kremeneke66ba682009-02-13 01:45:31 +0000690 const GRState* state = builder.getState();
691 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000692
693 // Three possibilities:
694 //
695 // (1) We know the computed label.
Ted Kremenekb31af242008-02-28 09:25:22 +0000696 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000697 // (3) We have no clue about the label. Dispatch to all targets.
698 //
699
700 typedef IndirectGotoNodeBuilder::iterator iterator;
701
Zhongxing Xu097fc982008-10-17 05:57:07 +0000702 if (isa<loc::GotoLabel>(V)) {
703 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000704
705 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek79f63f52008-02-13 17:27:37 +0000706 if (I.getLabel() == L) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000707 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000708 return;
709 }
710 }
711
712 assert (false && "No block with label.");
713 return;
714 }
715
Zhongxing Xu097fc982008-10-17 05:57:07 +0000716 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000717 // Dispatch to the first target and mark it as a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000718 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000719 UndefBranches.insert(N);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000720 return;
721 }
722
723 // This is really a catch-all. We don't support symbolics yet.
724
Ted Kremenek07baa252008-02-21 18:02:17 +0000725 assert (V.isUnknown());
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000726
727 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000728 builder.generateNode(I, state);
Ted Kremenek677f4ef2008-02-13 00:24:44 +0000729}
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000730
Ted Kremenekca5f6202008-04-15 23:06:53 +0000731
732void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
733 NodeTy* Pred, NodeSet& Dst) {
734
735 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
736
Ted Kremeneke66ba682009-02-13 01:45:31 +0000737 const GRState* state = GetState(Pred);
738 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000739
740 assert (X.isUndef());
741
742 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
743
744 assert (SE);
745
Ted Kremeneke66ba682009-02-13 01:45:31 +0000746 X = GetBlkExprSVal(state, SE);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000747
748 // Make sure that we invalidate the previous binding.
Ted Kremeneke66ba682009-02-13 01:45:31 +0000749 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremenekca5f6202008-04-15 23:06:53 +0000750}
751
Ted Kremenekaee121c2008-02-13 23:08:21 +0000752/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
753/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000754void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
755 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000756 const GRState* state = builder.getState();
Ted Kremenekbc965a62008-02-18 22:57:02 +0000757 Expr* CondE = builder.getCondition();
Ted Kremeneke66ba682009-02-13 01:45:31 +0000758 SVal CondV = GetSVal(state, CondE);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000759
Ted Kremenekb31af242008-02-28 09:25:22 +0000760 if (CondV.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000761 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenekb31af242008-02-28 09:25:22 +0000762 UndefBranches.insert(N);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000763 return;
764 }
Ted Kremenekbc965a62008-02-18 22:57:02 +0000765
Ted Kremeneke66ba682009-02-13 01:45:31 +0000766 const GRState* DefaultSt = state;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000767 bool DefaultFeasible = false;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000768
Ted Kremenek07baa252008-02-21 18:02:17 +0000769 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekaee121c2008-02-13 23:08:21 +0000770 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000771
772 // Evaluate the LHS of the case value.
773 Expr::EvalResult V1;
774 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekaee121c2008-02-13 23:08:21 +0000775
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000776 // Sanity checks. These go away in Release builds.
777 assert(b && V1.Val.isInt() && !V1.HasSideEffects
778 && "Case condition must evaluate to an integer constant.");
779 b = b; // silence unused variable warning
780 assert(V1.Val.getInt().getBitWidth() ==
781 getContext().getTypeSize(CondE->getType()));
782
Ted Kremenekaee121c2008-02-13 23:08:21 +0000783 // Get the RHS of the case, if it exists.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000784 Expr::EvalResult V2;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000785
786 if (Expr* E = Case->getRHS()) {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000787 b = E->Evaluate(V2, getContext());
788 assert(b && V2.Val.isInt() && !V2.HasSideEffects
789 && "Case condition must evaluate to an integer constant.");
790 b = b; // silence unused variable warning
Ted Kremenekaee121c2008-02-13 23:08:21 +0000791 }
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000792 else
793 V2 = V1;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000794
795 // FIXME: Eventually we should replace the logic below with a range
796 // comparison, rather than concretize the values within the range.
Ted Kremenek07baa252008-02-21 18:02:17 +0000797 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekaee121c2008-02-13 23:08:21 +0000798
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000799 do {
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000800 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek74556a12009-03-26 03:35:11 +0000801 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
802 getContext().IntTy);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000803
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000804 // Now "assume" that the case matches.
Ted Kremenekd4676512008-03-12 21:45:47 +0000805 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000806 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000807
808 if (isFeasible) {
809 builder.generateCaseStmtNode(I, StNew);
810
811 // If CondV evaluates to a constant, then we know that this
812 // is the *only* case that we can take, so stop evaluating the
813 // others.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000814 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekaee121c2008-02-13 23:08:21 +0000815 return;
816 }
817
818 // Now "assume" that the case doesn't match. Add this state
819 // to the default state (if it is feasible).
820
Ted Kremenekd4676512008-03-12 21:45:47 +0000821 isFeasible = false;
Ted Kremenekb1934132008-02-14 19:37:24 +0000822 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000823
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000824 if (isFeasible) {
825 DefaultFeasible = true;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000826 DefaultSt = StNew;
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000827 }
Ted Kremenekaee121c2008-02-13 23:08:21 +0000828
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000829 // Concretize the next value in the range.
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000830 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000831 break;
Ted Kremenekaee121c2008-02-13 23:08:21 +0000832
Ted Kremenek7f6c3a22009-01-17 01:54:16 +0000833 ++V1.Val.getInt();
834 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenekf1d623e2008-03-17 22:17:56 +0000835
836 } while (true);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000837 }
838
839 // If we reach here, than we know that the default branch is
840 // possible.
Ted Kremenekdf3aaa12008-04-23 05:03:18 +0000841 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekaee121c2008-02-13 23:08:21 +0000842}
843
Ted Kremenekca5f6202008-04-15 23:06:53 +0000844//===----------------------------------------------------------------------===//
845// Transfer functions: logical operations ('&&', '||').
846//===----------------------------------------------------------------------===//
Ted Kremenekaee121c2008-02-13 23:08:21 +0000847
Ted Kremenek30fa28b2008-02-13 17:41:41 +0000848void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +0000849 NodeSet& Dst) {
Ted Kremenekbf988d02008-02-19 00:22:37 +0000850
Ted Kremenek99ecce72008-02-26 19:05:15 +0000851 assert (B->getOpcode() == BinaryOperator::LAnd ||
852 B->getOpcode() == BinaryOperator::LOr);
853
854 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
855
Ted Kremeneke66ba682009-02-13 01:45:31 +0000856 const GRState* state = GetState(Pred);
857 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000858
Ted Kremenekb31af242008-02-28 09:25:22 +0000859 assert (X.isUndef());
Ted Kremenek99ecce72008-02-26 19:05:15 +0000860
Ted Kremenekb31af242008-02-28 09:25:22 +0000861 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek99ecce72008-02-26 19:05:15 +0000862
863 assert (Ex);
864
865 if (Ex == B->getRHS()) {
866
Ted Kremeneke66ba682009-02-13 01:45:31 +0000867 X = GetBlkExprSVal(state, Ex);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000868
Ted Kremenekb31af242008-02-28 09:25:22 +0000869 // Handle undefined values.
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000870
Ted Kremenekb31af242008-02-28 09:25:22 +0000871 if (X.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000872 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek5f2eb192008-02-26 19:40:44 +0000873 return;
874 }
875
Ted Kremenek99ecce72008-02-26 19:05:15 +0000876 // We took the RHS. Because the value of the '&&' or '||' expression must
877 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
878 // or 1. Alternatively, we could take a lazy approach, and calculate this
879 // value later when necessary. We don't have the machinery in place for
880 // this right now, and since most logical expressions are used for branches,
881 // the payoff is not likely to be large. Instead, we do eager evaluation.
882
883 bool isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000884 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000885
886 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000887 MakeNode(Dst, B, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000888 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek99ecce72008-02-26 19:05:15 +0000889
890 isFeasible = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +0000891 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek99ecce72008-02-26 19:05:15 +0000892
893 if (isFeasible)
Ted Kremenekf10f2882008-03-21 21:30:14 +0000894 MakeNode(Dst, B, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000895 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000896 }
897 else {
Ted Kremenek99ecce72008-02-26 19:05:15 +0000898 // We took the LHS expression. Depending on whether we are '&&' or
899 // '||' we know what the value of the expression is via properties of
900 // the short-circuiting.
901
902 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000903 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000904 }
Ted Kremenek1f0eb992008-02-05 00:26:40 +0000905}
Ted Kremenek99ecce72008-02-26 19:05:15 +0000906
Ted Kremenekca5f6202008-04-15 23:06:53 +0000907//===----------------------------------------------------------------------===//
Ted Kremenek4d22f0e2008-04-16 18:39:06 +0000908// Transfer functions: Loads and stores.
Ted Kremenekca5f6202008-04-15 23:06:53 +0000909//===----------------------------------------------------------------------===//
Ted Kremenek68d70a82008-01-15 23:55:06 +0000910
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000911void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
912 bool asLValue) {
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000913
Ted Kremeneke66ba682009-02-13 01:45:31 +0000914 const GRState* state = GetState(Pred);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000915
Douglas Gregord2baafd2008-10-21 16:13:35 +0000916 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000917
918 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
919
Ted Kremeneke66ba682009-02-13 01:45:31 +0000920 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xude186ae2008-10-17 02:20:14 +0000921
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000922 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000923 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000924 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000925 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000926 return;
927
928 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
929 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
930
931 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000932 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneke66ba682009-02-13 01:45:31 +0000933 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000934 return;
935
936 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek44a40142008-11-15 02:35:08 +0000937 assert(asLValue);
Zhongxing Xu097fc982008-10-17 05:57:07 +0000938 SVal V = loc::FuncVal(FD);
Ted Kremeneke66ba682009-02-13 01:45:31 +0000939 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000940 return;
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000941 }
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000942
943 assert (false &&
944 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek9b32cd02008-02-07 04:16:04 +0000945}
946
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000947/// VisitArraySubscriptExpr - Transfer function for array accesses
948void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000949 NodeSet& Dst, bool asLValue) {
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000950
951 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenekc4385b42008-04-29 23:24:44 +0000952 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000953 NodeSet Tmp;
Ted Kremenekbe9fe042009-02-24 02:23:11 +0000954
955 if (Base->getType()->isVectorType()) {
956 // For vector types get its lvalue.
957 // FIXME: This may not be correct. Is the rvalue of a vector its location?
958 // In fact, I think this is just a hack. We need to get the right
959 // semantics.
960 VisitLValue(Base, Pred, Tmp);
961 }
962 else
963 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000964
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000965 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenekc4385b42008-04-29 23:24:44 +0000966 NodeSet Tmp2;
Ted Kremenek6eaf0e32008-10-17 00:51:01 +0000967 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenekc4385b42008-04-29 23:24:44 +0000968
969 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000970 const GRState* state = GetState(*I2);
971 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
972 GetSVal(state, Idx));
Ted Kremenekc4385b42008-04-29 23:24:44 +0000973
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000974 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +0000975 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenekc4385b42008-04-29 23:24:44 +0000976 else
Ted Kremeneke66ba682009-02-13 01:45:31 +0000977 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenekc4385b42008-04-29 23:24:44 +0000978 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +0000979 }
Ted Kremenekbb7c1562008-04-22 04:56:29 +0000980}
981
Ted Kremenekd0d86202008-04-21 23:43:38 +0000982/// VisitMemberExpr - Transfer function for member expressions.
983void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000984 NodeSet& Dst, bool asLValue) {
Ted Kremenekd0d86202008-04-21 23:43:38 +0000985
986 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenekd0d86202008-04-21 23:43:38 +0000987 NodeSet Tmp;
Ted Kremenek66f07b12008-10-18 03:28:48 +0000988
989 if (M->isArrow())
990 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
991 else
992 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
993
Douglas Gregor82d44772008-12-20 23:49:58 +0000994 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
995 if (!Field) // FIXME: skipping member expressions for non-fields
996 return;
997
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000998 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +0000999 const GRState* state = GetState(*I);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001000 // FIXME: Should we insert some assumption logic in here to determine
1001 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor82d44772008-12-20 23:49:58 +00001002 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneke66ba682009-02-13 01:45:31 +00001003 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenek6eaf0e32008-10-17 00:51:01 +00001004
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001005 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001006 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001007 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001008 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001009 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00001010}
1011
Ted Kremeneke66ba682009-02-13 01:45:31 +00001012/// EvalBind - Handle the semantics of binding a value to a specific location.
1013/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1014void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1015 const GRState* state, SVal location, SVal Val) {
1016
Ted Kremeneka42be302009-02-14 01:43:44 +00001017 const GRState* newState = 0;
1018
1019 if (location.isUnknown()) {
1020 // We know that the new state will be the same as the old state since
1021 // the location of the binding is "unknown". Consequently, there
1022 // is no reason to just create a new node.
1023 newState = state;
1024 }
1025 else {
1026 // We are binding to a value other than 'unknown'. Perform the binding
1027 // using the StoreManager.
1028 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1029 }
Ted Kremeneke66ba682009-02-13 01:45:31 +00001030
Ted Kremeneka42be302009-02-14 01:43:44 +00001031 // The next thing to do is check if the GRTransferFuncs object wants to
1032 // update the state based on the new binding. If the GRTransferFunc object
1033 // doesn't do anything, just auto-propagate the current state.
1034 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1035 newState != state);
1036
1037 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001038}
1039
1040/// EvalStore - Handle the semantics of a store via an assignment.
1041/// @param Dst The node set to store generated state nodes
1042/// @param Ex The expression representing the location of the store
1043/// @param state The current simulation state
1044/// @param location The location to store the value
1045/// @param Val The value to be stored
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001046void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001047 const GRState* state, SVal location, SVal Val) {
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001048
1049 assert (Builder && "GRStmtNodeBuilder must be defined.");
1050
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001051 // Evaluate the location (checks for bad dereferences).
Ted Kremeneke66ba682009-02-13 01:45:31 +00001052 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001053
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001054 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001055 return;
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001056
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001057 assert (!location.isUndef());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001058 state = GetState(Pred);
1059
1060 // Proceed with the store.
1061 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1062 Builder->PointKind = ProgramPoint::PostStoreKind;
1063 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001064}
1065
1066void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001067 const GRState* state, SVal location) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001068
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001069 // Evaluate the location (checks for bad dereferences).
Ted Kremeneke66ba682009-02-13 01:45:31 +00001070 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001071
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001072 if (!Pred)
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001073 return;
1074
Ted Kremeneke66ba682009-02-13 01:45:31 +00001075 state = GetState(Pred);
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001076
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001077 // Proceed with the load.
Ted Kremenekc8ce08a2008-08-28 18:43:46 +00001078 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001079
1080 // FIXME: Currently symbolic analysis "generates" new symbols
1081 // for the contents of values. We need a better approach.
1082
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001083 if (location.isUnknown()) {
Ted Kremenekbf573852008-04-30 04:23:07 +00001084 // This is important. We must nuke the old binding.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001085 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenekbf573852008-04-30 04:23:07 +00001086 }
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001087 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001088 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
1089 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xu72a05eb2008-11-28 08:34:30 +00001090 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001091}
1092
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001093void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001094 const GRState* state, SVal location, SVal Val) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001095
1096 NodeSet TmpDst;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001097 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00001098
1099 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1100 MakeNode(Dst, Ex, *I, (*I)->getState());
1101}
1102
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001103GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneke66ba682009-02-13 01:45:31 +00001104 const GRState* state,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001105 SVal location) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001106
1107 // Check for loads/stores from/to undefined values.
1108 if (location.isUndef()) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001109 NodeTy* N =
Ted Kremeneke66ba682009-02-13 01:45:31 +00001110 Builder->generateNode(Ex, state, Pred,
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001111 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001112
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001113 if (N) {
1114 N->markAsSink();
1115 UndefDeref.insert(N);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001116 }
1117
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001118 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001119 }
1120
1121 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1122 if (location.isUnknown())
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001123 return Pred;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001124
1125 // During a load, one of two possible situations arise:
1126 // (1) A crash, because the location (pointer) was NULL.
1127 // (2) The location (pointer) is not NULL, and the dereference works.
1128 //
1129 // We add these assumptions.
1130
Zhongxing Xu097fc982008-10-17 05:57:07 +00001131 Loc LV = cast<Loc>(location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001132
1133 // "Assume" that the pointer is not NULL.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001134 bool isFeasibleNotNull = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001135 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001136
1137 // "Assume" that the pointer is NULL.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001138 bool isFeasibleNull = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001139 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001140 getStateManager());
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001141
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001142 if (isFeasibleNull) {
1143
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001144 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001145 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekbb7a3d92008-09-18 23:09:54 +00001146 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1147
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001148 // We don't use "MakeNode" here because the node will be a sink
1149 // and we have no intention of processing it later.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001150 NodeTy* NullNode =
1151 Builder->generateNode(Ex, StNull, Pred,
1152 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenekf05eec42008-06-18 05:34:07 +00001153
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001154 if (NullNode) {
1155
1156 NullNode->markAsSink();
1157
1158 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1159 else ExplicitNullDeref.insert(NullNode);
1160 }
1161 }
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001162
1163 if (!isFeasibleNotNull)
1164 return 0;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001165
1166 // Check for out-of-bound array access.
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001167 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001168 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1169 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1170 // Get the index of the accessed element.
1171 SVal Idx = ER->getIndex();
1172 // Get the extent of the array.
Zhongxing Xu3625e542008-11-24 07:02:06 +00001173 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1174 ER->getSuperRegion());
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001175
1176 bool isFeasibleInBound = false;
1177 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1178 true, isFeasibleInBound);
1179
1180 bool isFeasibleOutBound = false;
1181 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1182 false, isFeasibleOutBound);
1183
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001184 if (isFeasibleOutBound) {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001185 // Report warning. Make sink node manually.
1186 NodeTy* OOBNode =
1187 Builder->generateNode(Ex, StOutBound, Pred,
1188 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu5c70c772008-11-23 05:52:28 +00001189
1190 if (OOBNode) {
1191 OOBNode->markAsSink();
1192
1193 if (isFeasibleInBound)
1194 ImplicitOOBMemAccesses.insert(OOBNode);
1195 else
1196 ExplicitOOBMemAccesses.insert(OOBNode);
1197 }
Zhongxing Xud52b8cf2008-11-22 13:21:46 +00001198 }
1199
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001200 if (!isFeasibleInBound)
1201 return 0;
1202
1203 StNotNull = StInBound;
Zhongxing Xu7b5c5b52008-11-08 03:45:42 +00001204 }
1205 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00001206
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001207 // Generate a new node indicating the checks succeed.
1208 return Builder->generateNode(Ex, StNotNull, Pred,
1209 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenek4d22f0e2008-04-16 18:39:06 +00001210}
1211
Ted Kremenekca5f6202008-04-15 23:06:53 +00001212//===----------------------------------------------------------------------===//
1213// Transfer function: Function calls.
1214//===----------------------------------------------------------------------===//
Ted Kremenekd9268e32008-02-19 01:44:53 +00001215void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenek07baa252008-02-21 18:02:17 +00001216 CallExpr::arg_iterator AI,
1217 CallExpr::arg_iterator AE,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001218 NodeSet& Dst)
1219{
1220 // Determine the type of function we're calling (if available).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001221 const FunctionProtoType *Proto = NULL;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001222 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1223 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor4fa58902009-02-26 23:50:07 +00001224 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001225
1226 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1227}
1228
1229void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1230 CallExpr::arg_iterator AI,
1231 CallExpr::arg_iterator AE,
Douglas Gregor4fa58902009-02-26 23:50:07 +00001232 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001233 unsigned ParamIdx) {
Ted Kremenekd9268e32008-02-19 01:44:53 +00001234
Ted Kremenek07baa252008-02-21 18:02:17 +00001235 // Process the arguments.
Ted Kremenek07baa252008-02-21 18:02:17 +00001236 if (AI != AE) {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001237 // If the call argument is being bound to a reference parameter,
1238 // visit it as an lvalue, not an rvalue.
1239 bool VisitAsLvalue = false;
1240 if (Proto && ParamIdx < Proto->getNumArgs())
1241 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1242
1243 NodeSet DstTmp;
1244 if (VisitAsLvalue)
1245 VisitLValue(*AI, Pred, DstTmp);
1246 else
1247 Visit(*AI, Pred, DstTmp);
Ted Kremenek07baa252008-02-21 18:02:17 +00001248 ++AI;
1249
Ted Kremenek769f3482008-03-04 22:01:56 +00001250 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor0d5d89d2008-10-28 00:22:11 +00001251 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001252
1253 return;
1254 }
1255
1256 // If we reach here we have processed all of the arguments. Evaluate
1257 // the callee expression.
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001258
Ted Kremenekc71901d2008-02-25 21:16:03 +00001259 NodeSet DstTmp;
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001260 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001261
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001262 Visit(Callee, Pred, DstTmp);
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001263
Ted Kremenekd9268e32008-02-19 01:44:53 +00001264 // Finally, evaluate the function call.
Ted Kremenek07baa252008-02-21 18:02:17 +00001265 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1266
Ted Kremeneke66ba682009-02-13 01:45:31 +00001267 const GRState* state = GetState(*DI);
1268 SVal L = GetSVal(state, Callee);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001269
Ted Kremenekcda2efd2008-03-03 16:47:31 +00001270 // FIXME: Add support for symbolic function calls (calls involving
1271 // function pointer values that are symbolic).
1272
1273 // Check for undefined control-flow or calls to NULL.
1274
Zhongxing Xu097fc982008-10-17 05:57:07 +00001275 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001276 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenek769f3482008-03-04 22:01:56 +00001277
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00001278 if (N) {
1279 N->markAsSink();
1280 BadCalls.insert(N);
1281 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001282
Ted Kremenekd9268e32008-02-19 01:44:53 +00001283 continue;
Ted Kremenekb451dd32008-03-05 21:15:02 +00001284 }
1285
1286 // Check for the "noreturn" attribute.
1287
1288 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1289
Zhongxing Xu097fc982008-10-17 05:57:07 +00001290 if (isa<loc::FuncVal>(L)) {
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001291
Zhongxing Xu097fc982008-10-17 05:57:07 +00001292 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001293
1294 if (FD->getAttr<NoReturnAttr>())
Ted Kremenekb451dd32008-03-05 21:15:02 +00001295 Builder->BuildSinks = true;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001296 else {
1297 // HACK: Some functions are not marked noreturn, and don't return.
1298 // Here are a few hardwired ones. If this takes too long, we can
1299 // potentially cache these results.
1300 const char* s = FD->getIdentifier()->getName();
1301 unsigned n = strlen(s);
1302
1303 switch (n) {
1304 default:
1305 break;
Ted Kremenek550025b2008-03-14 23:25:49 +00001306
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001307 case 4:
Ted Kremenek550025b2008-03-14 23:25:49 +00001308 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1309 break;
1310
1311 case 5:
1312 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xu9857e742008-10-07 10:06:03 +00001313 else if (!memcmp(s, "error", 5)) {
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001314 if (CE->getNumArgs() > 0) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001315 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001316 // FIXME: use Assume to inspect the possible symbolic value of
1317 // X. Also check the specific signature of error().
Zhongxing Xu097fc982008-10-17 05:57:07 +00001318 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001319 if (CI && CI->getValue() != 0)
Zhongxing Xu9857e742008-10-07 10:06:03 +00001320 Builder->BuildSinks = true;
Zhongxing Xu21ec5fd2008-10-09 03:19:06 +00001321 }
Zhongxing Xu9857e742008-10-07 10:06:03 +00001322 }
Ted Kremenek550025b2008-03-14 23:25:49 +00001323 break;
Ted Kremenek9086f592009-02-17 17:48:52 +00001324
Ted Kremenek23271be2008-04-22 05:37:33 +00001325 case 6:
Ted Kremenek0aa9a282008-05-17 00:42:01 +00001326 if (!memcmp(s, "Assert", 6)) {
1327 Builder->BuildSinks = true;
1328 break;
1329 }
Ted Kremenek6b008c62008-05-01 15:55:59 +00001330
1331 // FIXME: This is just a wrapper around throwing an exception.
1332 // Eventually inter-procedural analysis should handle this easily.
1333 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1334
Ted Kremenek23271be2008-04-22 05:37:33 +00001335 break;
Ted Kremenekcbdc0ed2008-04-23 00:41:25 +00001336
1337 case 7:
1338 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1339 break;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001340
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001341 case 8:
Ted Kremenek9086f592009-02-17 17:48:52 +00001342 if (!memcmp(s ,"db_error", 8) ||
1343 !memcmp(s, "__assert", 8))
1344 Builder->BuildSinks = true;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001345 break;
Ted Kremenek0f84f662008-05-01 17:52:49 +00001346
1347 case 12:
1348 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1349 break;
Ted Kremenekc37d49e2008-04-30 17:54:04 +00001350
Ted Kremenek19903a22008-09-19 02:30:47 +00001351 case 13:
1352 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1353 break;
1354
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001355 case 14:
Ted Kremenekd32c0852008-10-30 00:00:57 +00001356 if (!memcmp(s, "dtrace_assfail", 14) ||
1357 !memcmp(s, "yy_fatal_error", 14))
1358 Builder->BuildSinks = true;
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001359 break;
Ted Kremeneka46fea72008-05-17 00:33:23 +00001360
1361 case 26:
Ted Kremenekd2774212008-07-18 16:28:33 +00001362 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek51b11012009-02-17 23:27:17 +00001363 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1364 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenekc3888a62008-05-17 00:40:45 +00001365 Builder->BuildSinks = true;
Ted Kremenekd2774212008-07-18 16:28:33 +00001366
Ted Kremeneka46fea72008-05-17 00:33:23 +00001367 break;
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001368 }
Ted Kremenek0d9ff342008-04-22 06:09:33 +00001369
Ted Kremenek02b1ff72008-03-14 21:58:42 +00001370 }
1371 }
Ted Kremenekb451dd32008-03-05 21:15:02 +00001372
1373 // Evaluate the call.
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001374
Zhongxing Xu097fc982008-10-17 05:57:07 +00001375 if (isa<loc::FuncVal>(L)) {
Ted Kremenek769f3482008-03-04 22:01:56 +00001376
Douglas Gregorb5af7382009-02-14 18:57:46 +00001377 if (unsigned id
1378 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek21581c62008-03-05 22:59:42 +00001379 switch (id) {
1380 case Builtin::BI__builtin_expect: {
1381 // For __builtin_expect, just return the value of the subexpression.
1382 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneke66ba682009-02-13 01:45:31 +00001383 SVal X = GetSVal(state, *(CE->arg_begin()));
1384 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek21581c62008-03-05 22:59:42 +00001385 continue;
1386 }
1387
Ted Kremenek19891fa2008-11-02 00:35:01 +00001388 case Builtin::BI__builtin_alloca: {
Ted Kremenek19891fa2008-11-02 00:35:01 +00001389 // FIXME: Refactor into StoreManager itself?
1390 MemRegionManager& RM = getStateManager().getRegionManager();
1391 const MemRegion* R =
Zhongxing Xu42b6ff22008-11-13 07:58:20 +00001392 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001393
1394 // Set the extent of the region in bytes. This enables us to use the
1395 // SVal of the argument directly. If we save the extent in bits, we
1396 // cannot represent values like symbol*8.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001397 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1398 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xu2ca0d6e2008-11-24 09:44:56 +00001399
Ted Kremeneke66ba682009-02-13 01:45:31 +00001400 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenek19891fa2008-11-02 00:35:01 +00001401 continue;
1402 }
1403
Ted Kremenek21581c62008-03-05 22:59:42 +00001404 default:
Ted Kremenek21581c62008-03-05 22:59:42 +00001405 break;
1406 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001407 }
Ted Kremenek07baa252008-02-21 18:02:17 +00001408
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001409 // Check any arguments passed-by-value against being undefined.
1410
1411 bool badArg = false;
1412
1413 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1414 I != E; ++I) {
1415
Zhongxing Xu097fc982008-10-17 05:57:07 +00001416 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001417 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenekb451dd32008-03-05 21:15:02 +00001418
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001419 if (N) {
1420 N->markAsSink();
1421 UndefArgs[N] = *I;
Ted Kremenek769f3482008-03-04 22:01:56 +00001422 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001423
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001424 badArg = true;
1425 break;
1426 }
Ted Kremenek769f3482008-03-04 22:01:56 +00001427 }
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001428
1429 if (badArg)
1430 continue;
1431
1432 // Dispatch to the plug-in transfer function.
1433
1434 unsigned size = Dst.size();
1435 SaveOr OldHasGen(Builder->HasGeneratedNode);
1436 EvalCall(Dst, CE, L, *DI);
1437
1438 // Handle the case where no nodes where generated. Auto-generate that
1439 // contains the updated state if we aren't generating sinks.
1440
1441 if (!Builder->BuildSinks && Dst.size() == size &&
1442 !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001443 MakeNode(Dst, CE, *DI, state);
Ted Kremenekd9268e32008-02-19 01:44:53 +00001444 }
1445}
1446
Ted Kremenekca5f6202008-04-15 23:06:53 +00001447//===----------------------------------------------------------------------===//
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001448// Transfer function: Objective-C ivar references.
1449//===----------------------------------------------------------------------===//
1450
Ted Kremenek9a48d862009-02-28 20:50:43 +00001451static std::pair<const void*,const void*> EagerlyAssumeTag
1452 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1453
Ted Kremenek34a611b2009-02-25 23:32:10 +00001454void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001455 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1456 NodeTy *Pred = *I;
Ted Kremenek34a611b2009-02-25 23:32:10 +00001457
1458 // Test if the previous node was as the same expression. This can happen
1459 // when the expression fails to evaluate to anything meaningful and
1460 // (as an optimization) we don't generate a node.
1461 ProgramPoint P = Pred->getLocation();
1462 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1463 Dst.Add(Pred);
1464 continue;
1465 }
1466
Ted Kremenek8f520972009-02-25 22:32:02 +00001467 const GRState* state = Pred->getState();
Ted Kremenek34a611b2009-02-25 23:32:10 +00001468 SVal V = GetSVal(state, Ex);
Ted Kremenek74556a12009-03-26 03:35:11 +00001469 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek8f520972009-02-25 22:32:02 +00001470 // First assume that the condition is true.
1471 bool isFeasible = false;
1472 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1473 if (isFeasible) {
Ted Kremenek34a611b2009-02-25 23:32:10 +00001474 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1475 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001476 stateTrue, Pred));
1477 }
1478
1479 // Next, assume that the condition is false.
1480 isFeasible = false;
1481 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1482 if (isFeasible) {
Ted Kremenek34a611b2009-02-25 23:32:10 +00001483 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1484 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek8f520972009-02-25 22:32:02 +00001485 stateFalse, Pred));
1486 }
1487 }
1488 else
1489 Dst.Add(Pred);
1490 }
1491}
1492
1493//===----------------------------------------------------------------------===//
1494// Transfer function: Objective-C ivar references.
1495//===----------------------------------------------------------------------===//
1496
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001497void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1498 NodeTy* Pred, NodeSet& Dst,
1499 bool asLValue) {
1500
1501 Expr* Base = cast<Expr>(Ex->getBase());
1502 NodeSet Tmp;
1503 Visit(Base, Pred, Tmp);
1504
1505 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001506 const GRState* state = GetState(*I);
1507 SVal BaseVal = GetSVal(state, Base);
1508 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001509
1510 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001511 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001512 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00001513 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremeneke7b0b272008-10-17 00:03:18 +00001514 }
1515}
1516
1517//===----------------------------------------------------------------------===//
Ted Kremenek13e167f2008-11-12 19:24:17 +00001518// Transfer function: Objective-C fast enumeration 'for' statements.
1519//===----------------------------------------------------------------------===//
1520
1521void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1522 NodeTy* Pred, NodeSet& Dst) {
1523
1524 // ObjCForCollectionStmts are processed in two places. This method
1525 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1526 // statements within a basic block. This transfer function does two things:
1527 //
1528 // (1) binds the next container value to 'element'. This creates a new
1529 // node in the ExplodedGraph.
1530 //
1531 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1532 // whether or not the container has any more elements. This value
1533 // will be tested in ProcessBranch. We need to explicitly bind
1534 // this value because a container can contain nil elements.
1535 //
1536 // FIXME: Eventually this logic should actually do dispatches to
1537 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1538 // This will require simulating a temporary NSFastEnumerationState, either
1539 // through an SVal or through the use of MemRegions. This value can
1540 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1541 // terminates we reclaim the temporary (it goes out of scope) and we
1542 // we can test if the SVal is 0 or if the MemRegion is null (depending
1543 // on what approach we take).
1544 //
1545 // For now: simulate (1) by assigning either a symbol or nil if the
1546 // container is empty. Thus this transfer function will by default
1547 // result in state splitting.
1548
Ted Kremenek034a9472008-11-14 19:47:18 +00001549 Stmt* elem = S->getElement();
1550 SVal ElementV;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001551
1552 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner4a9a85e2009-03-28 06:33:19 +00001553 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek13e167f2008-11-12 19:24:17 +00001554 assert (ElemD->getInit() == 0);
Ted Kremenek034a9472008-11-14 19:47:18 +00001555 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1556 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1557 return;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001558 }
Ted Kremenek034a9472008-11-14 19:47:18 +00001559
1560 NodeSet Tmp;
1561 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001562
Ted Kremenek034a9472008-11-14 19:47:18 +00001563 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1564 const GRState* state = GetState(*I);
1565 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1566 }
1567}
1568
1569void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1570 NodeTy* Pred, NodeSet& Dst,
1571 SVal ElementV) {
1572
1573
Ted Kremenek13e167f2008-11-12 19:24:17 +00001574
Ted Kremenek034a9472008-11-14 19:47:18 +00001575 // Get the current state. Use 'EvalLocation' to determine if it is a null
1576 // pointer, etc.
1577 Stmt* elem = S->getElement();
Ted Kremenek13e167f2008-11-12 19:24:17 +00001578
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001579 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1580 if (!Pred)
Ted Kremenek034a9472008-11-14 19:47:18 +00001581 return;
Ted Kremeneke27c37a2008-12-16 22:02:27 +00001582
1583 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek034a9472008-11-14 19:47:18 +00001584
Ted Kremenek13e167f2008-11-12 19:24:17 +00001585 // Handle the case where the container still has elements.
Ted Kremenek034a9472008-11-14 19:47:18 +00001586 QualType IntTy = getContext().IntTy;
Ted Kremenek13e167f2008-11-12 19:24:17 +00001587 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1588 GRStateRef hasElems = state.BindExpr(S, TrueV);
1589
Ted Kremenek13e167f2008-11-12 19:24:17 +00001590 // Handle the case where the container has no elements.
Ted Kremenekd3789d72008-11-12 21:12:46 +00001591 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1592 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek034a9472008-11-14 19:47:18 +00001593
1594 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1595 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1596 // FIXME: The proper thing to do is to really iterate over the
1597 // container. We will do this with dispatch logic to the store.
1598 // For now, just 'conjure' up a symbolic value.
Ted Kremenekf5da3252008-12-13 21:49:13 +00001599 QualType T = R->getRValueType(getContext());
Ted Kremenek034a9472008-11-14 19:47:18 +00001600 assert (Loc::IsLocType(T));
1601 unsigned Count = Builder->getCurrentBlockCount();
1602 loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
1603 hasElems = hasElems.BindLoc(ElementV, SymV);
Ted Kremenekd3789d72008-11-12 21:12:46 +00001604
Ted Kremenek034a9472008-11-14 19:47:18 +00001605 // Bind the location to 'nil' on the false branch.
1606 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1607 noElems = noElems.BindLoc(ElementV, nilV);
1608 }
1609
Ted Kremenekd3789d72008-11-12 21:12:46 +00001610 // Create the new nodes.
1611 MakeNode(Dst, S, Pred, hasElems);
1612 MakeNode(Dst, S, Pred, noElems);
Ted Kremenek13e167f2008-11-12 19:24:17 +00001613}
1614
1615//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +00001616// Transfer function: Objective-C message expressions.
1617//===----------------------------------------------------------------------===//
1618
1619void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1620 NodeSet& Dst){
1621
1622 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1623 Pred, Dst);
1624}
1625
1626void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001627 ObjCMessageExpr::arg_iterator AI,
1628 ObjCMessageExpr::arg_iterator AE,
1629 NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001630 if (AI == AE) {
1631
1632 // Process the receiver.
1633
1634 if (Expr* Receiver = ME->getReceiver()) {
1635 NodeSet Tmp;
1636 Visit(Receiver, Pred, Tmp);
1637
1638 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1639 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1640
1641 return;
1642 }
1643
1644 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1645 return;
1646 }
1647
1648 NodeSet Tmp;
1649 Visit(*AI, Pred, Tmp);
1650
1651 ++AI;
1652
1653 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1654 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1655}
1656
1657void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1658 NodeTy* Pred,
1659 NodeSet& Dst) {
1660
1661 // FIXME: More logic for the processing the method call.
1662
Ted Kremeneke66ba682009-02-13 01:45:31 +00001663 const GRState* state = GetState(Pred);
Ted Kremenek5f20a632008-05-01 18:33:28 +00001664 bool RaisesException = false;
1665
Ted Kremenekca5f6202008-04-15 23:06:53 +00001666
1667 if (Expr* Receiver = ME->getReceiver()) {
1668
Ted Kremeneke66ba682009-02-13 01:45:31 +00001669 SVal L = GetSVal(state, Receiver);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001670
Ted Kremenek95a98252009-02-19 04:06:22 +00001671 // Check for undefined control-flow.
Ted Kremenekca5f6202008-04-15 23:06:53 +00001672 if (L.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001673 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001674
1675 if (N) {
1676 N->markAsSink();
1677 UndefReceivers.insert(N);
1678 }
1679
1680 return;
1681 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001682
Ted Kremenek95a98252009-02-19 04:06:22 +00001683 // "Assume" that the receiver is not NULL.
1684 bool isFeasibleNotNull = false;
1685 Assume(state, L, true, isFeasibleNotNull);
1686
1687 // "Assume" that the receiver is NULL.
1688 bool isFeasibleNull = false;
1689 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1690
1691 if (isFeasibleNull) {
1692 // Check if the receiver was nil and the return value a struct.
Ted Kremenek75732212009-04-01 06:52:48 +00001693 if (ME->getType()->isRecordType() &&
1694 BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek95a98252009-02-19 04:06:22 +00001695 // The [0 ...] expressions will return garbage. Flag either an
1696 // explicit or implicit error. Because of the structure of this
1697 // function we currently do not bifurfacte the state graph at
1698 // this point.
1699 // FIXME: We should bifurcate and fill the returned struct with
1700 // garbage.
1701 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1702 N->markAsSink();
1703 if (isFeasibleNotNull)
1704 NilReceiverStructRetImplicit.insert(N);
1705 else
1706 NilReceiverStructRetExplicit.insert(N);
1707 }
1708 }
1709 }
1710
Ted Kremenek5f20a632008-05-01 18:33:28 +00001711 // Check if the "raise" message was sent.
1712 if (ME->getSelector() == RaiseSel)
1713 RaisesException = true;
1714 }
1715 else {
1716
1717 IdentifierInfo* ClsName = ME->getClassName();
1718 Selector S = ME->getSelector();
1719
1720 // Check for special instance methods.
1721
1722 if (!NSExceptionII) {
1723 ASTContext& Ctx = getContext();
1724
1725 NSExceptionII = &Ctx.Idents.get("NSException");
1726 }
1727
1728 if (ClsName == NSExceptionII) {
1729
1730 enum { NUM_RAISE_SELECTORS = 2 };
1731
1732 // Lazily create a cache of the selectors.
1733
1734 if (!NSExceptionInstanceRaiseSelectors) {
1735
1736 ASTContext& Ctx = getContext();
1737
1738 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1739
1740 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1741 unsigned idx = 0;
1742
1743 // raise:format:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001744 II.push_back(&Ctx.Idents.get("raise"));
1745 II.push_back(&Ctx.Idents.get("format"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001746 NSExceptionInstanceRaiseSelectors[idx++] =
1747 Ctx.Selectors.getSelector(II.size(), &II[0]);
1748
1749 // raise:format::arguments:
Ted Kremenek2227bdf2008-05-02 17:12:56 +00001750 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremenek5f20a632008-05-01 18:33:28 +00001751 NSExceptionInstanceRaiseSelectors[idx++] =
1752 Ctx.Selectors.getSelector(II.size(), &II[0]);
1753 }
1754
1755 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1756 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1757 RaisesException = true; break;
1758 }
1759 }
Ted Kremenekca5f6202008-04-15 23:06:53 +00001760 }
1761
1762 // Check for any arguments that are uninitialized/undefined.
1763
1764 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1765 I != E; ++I) {
1766
Ted Kremeneke66ba682009-02-13 01:45:31 +00001767 if (GetSVal(state, *I).isUndef()) {
Ted Kremenekca5f6202008-04-15 23:06:53 +00001768
1769 // Generate an error node for passing an uninitialized/undefined value
1770 // as an argument to a message expression. This node is a sink.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001771 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001772
1773 if (N) {
1774 N->markAsSink();
1775 MsgExprUndefArgs[N] = *I;
1776 }
1777
1778 return;
1779 }
Ted Kremenek5f20a632008-05-01 18:33:28 +00001780 }
1781
1782 // Check if we raise an exception. For now treat these as sinks. Eventually
1783 // we will want to handle exceptions properly.
1784
1785 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1786
1787 if (RaisesException)
1788 Builder->BuildSinks = true;
1789
Ted Kremenekca5f6202008-04-15 23:06:53 +00001790 // Dispatch to plug-in transfer function.
1791
1792 unsigned size = Dst.size();
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00001793 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001794
Ted Kremenekca5f6202008-04-15 23:06:53 +00001795 EvalObjCMessageExpr(Dst, ME, Pred);
1796
1797 // Handle the case where no nodes where generated. Auto-generate that
1798 // contains the updated state if we aren't generating sinks.
1799
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00001800 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneke66ba682009-02-13 01:45:31 +00001801 MakeNode(Dst, ME, Pred, state);
Ted Kremenekca5f6202008-04-15 23:06:53 +00001802}
1803
1804//===----------------------------------------------------------------------===//
1805// Transfer functions: Miscellaneous statements.
1806//===----------------------------------------------------------------------===//
1807
Ted Kremenek16354a42009-01-13 01:04:21 +00001808void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1809 QualType PtrTy,
1810 Expr* CastE, NodeTy* Pred,
1811 NodeSet& Dst) {
1812 if (!V.isUnknownOrUndef()) {
1813 // FIXME: Determine if the number of bits of the target type is
1814 // equal or exceeds the number of bits to store the pointer value.
Ted Kremenek3f755632009-03-05 03:42:31 +00001815 // If not, flag an error.
Ted Kremenek52978eb2009-03-05 03:44:53 +00001816 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
1817 CastE->getType())));
Ted Kremenek16354a42009-01-13 01:04:21 +00001818 }
Ted Kremenek3f755632009-03-05 03:42:31 +00001819 else
1820 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremenek16354a42009-01-13 01:04:21 +00001821}
1822
1823
Ted Kremenek07baa252008-02-21 18:02:17 +00001824void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5f585b02008-02-19 18:52:54 +00001825 NodeSet S1;
Ted Kremenek5f585b02008-02-19 18:52:54 +00001826 QualType T = CastE->getType();
Zhongxing Xu3739b0b2008-10-21 06:54:23 +00001827 QualType ExTy = Ex->getType();
Zhongxing Xu943909c2008-10-22 08:02:16 +00001828
Zhongxing Xu8f8ab962008-10-31 07:26:14 +00001829 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor21a04f32008-10-27 19:41:14 +00001830 T = ExCast->getTypeAsWritten();
1831
Zhongxing Xu943909c2008-10-22 08:02:16 +00001832 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00001833 VisitLValue(Ex, Pred, S1);
Ted Kremenek1d1b6c92008-03-04 22:16:08 +00001834 else
1835 Visit(Ex, Pred, S1);
1836
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001837 // Check for casting to "void".
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00001838 if (T->isVoidType()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00001839 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5f585b02008-02-19 18:52:54 +00001840 Dst.Add(*I1);
1841
Ted Kremenek54eddae2008-01-24 02:02:54 +00001842 return;
1843 }
1844
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001845 // FIXME: The rest of this should probably just go into EvalCall, and
1846 // let the transfer function object be responsible for constructing
1847 // nodes.
1848
Ted Kremenek07baa252008-02-21 18:02:17 +00001849 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek54eddae2008-01-24 02:02:54 +00001850 NodeTy* N = *I1;
Ted Kremeneke66ba682009-02-13 01:45:31 +00001851 const GRState* state = GetState(N);
1852 SVal V = GetSVal(state, Ex);
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001853 ASTContext& C = getContext();
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001854
1855 // Unknown?
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001856 if (V.isUnknown()) {
1857 Dst.Add(N);
1858 continue;
1859 }
1860
1861 // Undefined?
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001862 if (V.isUndef())
1863 goto PassThrough;
Ted Kremenek98fc4092008-09-19 20:51:22 +00001864
1865 // For const casts, just propagate the value.
Ted Kremenek98fc4092008-09-19 20:51:22 +00001866 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001867 C.getCanonicalType(ExTy).getUnqualifiedType())
1868 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00001869
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001870 // Check for casts from pointers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001871 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00001872 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001873 continue;
1874 }
1875
1876 // Check for casts from integers to pointers.
Ted Kremenek040d5bc2009-03-05 02:33:55 +00001877 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00001878 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001879 // Just unpackage the lval and return it.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001880 V = LV->getLoc();
Ted Kremeneke66ba682009-02-13 01:45:31 +00001881 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001882 continue;
Ted Kremenekfe1a0b12008-04-22 21:10:18 +00001883 }
Ted Kremenek3f755632009-03-05 03:42:31 +00001884
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001885 goto DispatchCast;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00001886 }
1887
1888 // Just pass through function and block pointers.
1889 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
1890 assert(Loc::IsLocType(T));
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001891 goto PassThrough;
Ted Kremenek040d5bc2009-03-05 02:33:55 +00001892 }
1893
Ted Kremenek16354a42009-01-13 01:04:21 +00001894 // Check for casts from array type to another type.
Zhongxing Xua9e8e082008-10-23 03:10:39 +00001895 if (ExTy->isArrayType()) {
Ted Kremenek16354a42009-01-13 01:04:21 +00001896 // We will always decay to a pointer.
Zhongxing Xu9ddfd192009-03-30 05:55:46 +00001897 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremenek16354a42009-01-13 01:04:21 +00001898
1899 // Are we casting from an array to a pointer? If so just pass on
1900 // the decayed value.
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001901 if (T->isPointerType())
1902 goto PassThrough;
Ted Kremenek16354a42009-01-13 01:04:21 +00001903
1904 // Are we casting from an array to an integer? If so, cast the decayed
1905 // pointer value to an integer.
1906 assert(T->isIntegerType());
1907 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1908 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneke66ba682009-02-13 01:45:31 +00001909 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xua9e8e082008-10-23 03:10:39 +00001910 continue;
1911 }
1912
Ted Kremenekf5da3252008-12-13 21:49:13 +00001913 // Check for casts from a region to a specific type.
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00001914 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
1915 // FIXME: For TypedViewRegions, we should handle the case where the
1916 // underlying symbolic pointer is a function pointer or
1917 // block pointer.
1918
1919 // FIXME: We should handle the case where we strip off view layers to get
1920 // to a desugared type.
1921
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00001922 assert(Loc::IsLocType(T));
Zhongxing Xu1f48e432009-04-03 07:33:13 +00001923 // We get a symbolic function pointer for a dereference of a function
1924 // pointer, but it is of function type. Example:
1925
1926 // struct FPRec {
1927 // void (*my_func)(int * x);
1928 // };
1929 //
1930 // int bar(int x);
1931 //
1932 // int f1_a(struct FPRec* foo) {
1933 // int x;
1934 // (*foo->my_func)(&x);
1935 // return bar(x)+1; // no-warning
1936 // }
1937
1938 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00001939
Ted Kremenekf5da3252008-12-13 21:49:13 +00001940 const MemRegion* R = RV->getRegion();
1941 StoreManager& StoreMgr = getStoreManager();
1942
1943 // Delegate to store manager to get the result of casting a region
1944 // to a different type.
Ted Kremeneke66ba682009-02-13 01:45:31 +00001945 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenekf5da3252008-12-13 21:49:13 +00001946
1947 // Inspect the result. If the MemRegion* returned is NULL, this
1948 // expression evaluates to UnknownVal.
1949 R = Res.getRegion();
1950 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1951
1952 // Generate the new node in the ExplodedGraph.
1953 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenek2c0de352008-12-13 19:24:37 +00001954 continue;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +00001955 }
1956
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00001957 // If we are casting a symbolic value, make a symbolic region and a
1958 // TypedViewRegion subregion.
1959 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) {
1960 SymbolRef Sym = SV->getSymbol();
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001961 QualType SymTy = getSymbolManager().getType(Sym);
1962
1963 // Just pass through symbols that are function or block pointers.
1964 if (SymTy->isFunctionPointerType() || SymTy->isBlockPointerType())
1965 goto PassThrough;
Ted Kremenekc0bfc3d2009-03-05 22:47:06 +00001966
1967 // Are we casting to a function or block pointer?
1968 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
1969 // FIXME: We should verify that the underlying type of the symbolic
1970 // pointer is a void* (or maybe char*). Other things are an abuse
1971 // of the type system.
1972 goto PassThrough;
1973 }
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001974
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00001975 StoreManager& StoreMgr = getStoreManager();
Ted Kremenek74556a12009-03-26 03:35:11 +00001976 const MemRegion* R = StoreMgr.getRegionManager().getSymbolicRegion(Sym);
Ted Kremenek5a64fcc2009-03-04 00:14:35 +00001977
1978 // Delegate to store manager to get the result of casting a region
1979 // to a different type.
1980 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
1981
1982 // Inspect the result. If the MemRegion* returned is NULL, this
1983 // expression evaluates to UnknownVal.
1984 R = Res.getRegion();
1985 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
1986
1987 // Generate the new node in the ExplodedGraph.
1988 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
1989 continue;
1990 }
1991
Ted Kremenek311ff9b2009-03-05 20:22:13 +00001992 // All other cases.
1993 DispatchCast: {
1994 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
1995 EvalCast(V, CastE->getType())));
1996 continue;
1997 }
1998
1999 PassThrough: {
2000 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2001 }
Ted Kremenek54eddae2008-01-24 02:02:54 +00002002 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002003}
2004
Ted Kremenekd83daa52008-10-27 21:54:31 +00002005void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002006 NodeTy* Pred, NodeSet& Dst,
2007 bool asLValue) {
Ted Kremenekd83daa52008-10-27 21:54:31 +00002008 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2009 NodeSet Tmp;
2010 Visit(ILE, Pred, Tmp);
2011
2012 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002013 const GRState* state = GetState(*I);
2014 SVal ILV = GetSVal(state, ILE);
2015 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenekd83daa52008-10-27 21:54:31 +00002016
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002017 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002018 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +00002019 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002020 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenekd83daa52008-10-27 21:54:31 +00002021 }
2022}
2023
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002024void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002025
Ted Kremenek811af062008-10-06 18:43:53 +00002026 // The CFG has one DeclStmt per Decl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002027 Decl* D = *DS->decl_begin();
Ted Kremenek448ab622008-08-28 18:34:26 +00002028
2029 if (!D || !isa<VarDecl>(D))
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002030 return;
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002031
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002032 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002033 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002034
2035 // FIXME: static variables may have an initializer, but the second
2036 // time a function is called those values may not be current.
2037 NodeSet Tmp;
2038
Ted Kremenek13e167f2008-11-12 19:24:17 +00002039 if (InitEx)
2040 Visit(InitEx, Pred, Tmp);
Ted Kremenek448ab622008-08-28 18:34:26 +00002041
2042 if (Tmp.empty())
2043 Tmp.Add(Pred);
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002044
2045 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002046 const GRState* state = GetState(*I);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002047 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002048
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002049 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2050 QualType T = getContext().getCanonicalType(VD->getType());
2051 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2052 // FIXME: Handle multi-dimensional VLAs.
2053
2054 Expr* SE = VLA->getSizeExpr();
2055 SVal Size = GetSVal(state, SE);
2056
2057 if (Size.isUndef()) {
2058 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2059 N->markAsSink();
2060 ExplicitBadSizedVLA.insert(N);
2061 }
2062 continue;
2063 }
2064
2065 bool isFeasibleZero = false;
2066 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2067
2068 bool isFeasibleNotZero = false;
2069 state = Assume(state, Size, true, isFeasibleNotZero);
2070
2071 if (isFeasibleZero) {
2072 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2073 N->markAsSink();
2074 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2075 else ExplicitBadSizedVLA.insert(N);
2076 }
2077 }
2078
2079 if (!isFeasibleNotZero)
2080 continue;
2081 }
2082
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +00002083 // Decls without InitExpr are not initialized explicitly.
Ted Kremenek13e167f2008-11-12 19:24:17 +00002084 if (InitEx) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002085 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002086 QualType T = VD->getType();
2087
2088 // Recover some path-sensitivity if a scalar value evaluated to
2089 // UnknownVal.
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002090 if (InitVal.isUnknown() ||
2091 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek13e167f2008-11-12 19:24:17 +00002092 if (Loc::IsLocType(T)) {
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002093 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002094 InitVal = loc::SymbolVal(Sym);
2095 }
Ted Kremenek79413a52008-11-13 06:10:40 +00002096 else if (T->isIntegerType() && T->isScalarType()) {
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002097 SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
Ted Kremenek13e167f2008-11-12 19:24:17 +00002098 InitVal = nonloc::SymbolVal(Sym);
2099 }
2100 }
2101
Ted Kremeneke66ba682009-02-13 01:45:31 +00002102 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002103
2104 // The next thing to do is check if the GRTransferFuncs object wants to
2105 // update the state based on the new binding. If the GRTransferFunc
2106 // object doesn't do anything, just auto-propagate the current state.
2107 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2108 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2109 InitVal);
2110 }
2111 else {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002112 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenekcdd523e2009-02-14 01:54:57 +00002113 MakeNode(Dst, DS, *I, state);
Ted Kremenekf8f0d3c2008-12-08 22:47:34 +00002114 }
Ted Kremenekcfbc56a2008-04-22 22:25:27 +00002115 }
Ted Kremenekb9c30e32008-01-24 20:55:43 +00002116}
Ted Kremenek54eddae2008-01-24 02:02:54 +00002117
Ted Kremeneke56ece22008-10-30 17:47:32 +00002118namespace {
2119 // This class is used by VisitInitListExpr as an item in a worklist
2120 // for processing the values contained in an InitListExpr.
2121class VISIBILITY_HIDDEN InitListWLItem {
2122public:
2123 llvm::ImmutableList<SVal> Vals;
2124 GRExprEngine::NodeTy* N;
2125 InitListExpr::reverse_iterator Itr;
2126
2127 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2128 InitListExpr::reverse_iterator itr)
2129 : Vals(vals), N(n), Itr(itr) {}
2130};
2131}
2132
2133
Zhongxing Xuebcad732008-10-30 05:02:23 +00002134void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2135 NodeSet& Dst) {
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002136
Zhongxing Xuebcad732008-10-30 05:02:23 +00002137 const GRState* state = GetState(Pred);
Ted Kremenek3d221152008-11-13 05:05:34 +00002138 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremeneke56ece22008-10-30 17:47:32 +00002139 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuebcad732008-10-30 05:02:23 +00002140
Zhongxing Xuf5cbb762008-10-30 05:35:59 +00002141 if (T->isArrayType() || T->isStructureType()) {
Ted Kremeneke56ece22008-10-30 17:47:32 +00002142
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002143 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremeneke56ece22008-10-30 17:47:32 +00002144
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002145 // Handle base case where the initializer has no elements.
2146 // e.g: static int* myArray[] = {};
2147 if (NumInitElements == 0) {
2148 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2149 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2150 return;
2151 }
2152
2153 // Create a worklist to process the initializers.
2154 llvm::SmallVector<InitListWLItem, 10> WorkList;
2155 WorkList.reserve(NumInitElements);
2156 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002157 InitListExpr::reverse_iterator ItrEnd = E->rend();
2158
Ted Kremeneka4b7f692008-10-30 23:14:36 +00002159 // Process the worklist until it is empty.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002160 while (!WorkList.empty()) {
2161 InitListWLItem X = WorkList.back();
2162 WorkList.pop_back();
2163
Zhongxing Xuebcad732008-10-30 05:02:23 +00002164 NodeSet Tmp;
Ted Kremeneke56ece22008-10-30 17:47:32 +00002165 Visit(*X.Itr, X.N, Tmp);
2166
2167 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002168
Ted Kremeneke56ece22008-10-30 17:47:32 +00002169 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2170 // Get the last initializer value.
2171 state = GetState(*NI);
2172 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2173
2174 // Construct the new list of values by prepending the new value to
2175 // the already constructed list.
2176 llvm::ImmutableList<SVal> NewVals =
2177 getBasicVals().consVals(InitV, X.Vals);
2178
2179 if (NewItr == ItrEnd) {
Zhongxing Xua852b312008-10-31 03:01:26 +00002180 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremeneke56ece22008-10-30 17:47:32 +00002181 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuebcad732008-10-30 05:02:23 +00002182
Ted Kremeneke56ece22008-10-30 17:47:32 +00002183 // Make final state and node.
Ted Kremenek78c06532008-10-30 18:37:08 +00002184 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremeneke56ece22008-10-30 17:47:32 +00002185 }
2186 else {
2187 // Still some initializer values to go. Push them onto the worklist.
2188 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2189 }
2190 }
Zhongxing Xuebcad732008-10-30 05:02:23 +00002191 }
Ted Kremenek9c5058d2008-10-30 18:34:31 +00002192
2193 return;
Zhongxing Xuebcad732008-10-30 05:02:23 +00002194 }
2195
Ted Kremenek79413a52008-11-13 06:10:40 +00002196 if (T->isUnionType() || T->isVectorType()) {
2197 // FIXME: to be implemented.
2198 // Note: That vectors can return true for T->isIntegerType()
2199 MakeNode(Dst, E, Pred, state);
2200 return;
2201 }
2202
Zhongxing Xuebcad732008-10-30 05:02:23 +00002203 if (Loc::IsLocType(T) || T->isIntegerType()) {
2204 assert (E->getNumInits() == 1);
2205 NodeSet Tmp;
2206 Expr* Init = E->getInit(0);
2207 Visit(Init, Pred, Tmp);
2208 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2209 state = GetState(*I);
Zhongxing Xu696b3a82008-10-30 05:33:54 +00002210 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuebcad732008-10-30 05:02:23 +00002211 }
2212 return;
2213 }
2214
Zhongxing Xuebcad732008-10-30 05:02:23 +00002215
2216 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2217 assert(0 && "unprocessed InitListExpr type");
2218}
Ted Kremenek1f0eb992008-02-05 00:26:40 +00002219
Sebastian Redl0cb7c872008-11-11 17:56:53 +00002220/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2221void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2222 NodeTy* Pred,
2223 NodeSet& Dst) {
2224 QualType T = Ex->getTypeOfArgument();
Ted Kremenekc3b12832008-03-15 03:13:20 +00002225 uint64_t amt;
2226
2227 if (Ex->isSizeOf()) {
Ted Kremenek41cf0152008-12-15 18:51:00 +00002228 if (T == getContext().VoidTy) {
2229 // sizeof(void) == 1 byte.
2230 amt = 1;
2231 }
2232 else if (!T.getTypePtr()->isConstantSizeType()) {
2233 // FIXME: Add support for VLAs.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002234 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002235 }
2236 else if (T->isObjCInterfaceType()) {
2237 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2238 // the compiler has laid out its representation. Just report Unknown
2239 // for these.
Ted Kremeneka9223262008-04-30 21:31:12 +00002240 return;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002241 }
2242 else {
2243 // All other cases.
Ted Kremenekc3b12832008-03-15 03:13:20 +00002244 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek41cf0152008-12-15 18:51:00 +00002245 }
Ted Kremenekc3b12832008-03-15 03:13:20 +00002246 }
2247 else // Get alignment of the type.
Ted Kremenek8eac9c02008-03-15 03:13:55 +00002248 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekfd85f292008-02-12 19:49:57 +00002249
Ted Kremenekf10f2882008-03-21 21:30:14 +00002250 MakeNode(Dst, Ex, Pred,
Zhongxing Xu696b3a82008-10-30 05:33:54 +00002251 BindExpr(GetState(Pred), Ex,
2252 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekfd85f292008-02-12 19:49:57 +00002253}
2254
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002255
Ted Kremenek07baa252008-02-21 18:02:17 +00002256void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002257 NodeSet& Dst, bool asLValue) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002258
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002259 switch (U->getOpcode()) {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002260
2261 default:
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002262 break;
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002263
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002264 case UnaryOperator::Deref: {
2265
2266 Expr* Ex = U->getSubExpr()->IgnoreParens();
2267 NodeSet Tmp;
2268 Visit(Ex, Pred, Tmp);
2269
2270 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002271
Ted Kremeneke66ba682009-02-13 01:45:31 +00002272 const GRState* state = GetState(*I);
2273 SVal location = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002274
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002275 if (asLValue)
Ted Kremeneke66ba682009-02-13 01:45:31 +00002276 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002277 else
Ted Kremeneke66ba682009-02-13 01:45:31 +00002278 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002279 }
2280
2281 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002282 }
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002283
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002284 case UnaryOperator::Real: {
2285
2286 Expr* Ex = U->getSubExpr()->IgnoreParens();
2287 NodeSet Tmp;
2288 Visit(Ex, Pred, Tmp);
2289
2290 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2291
Zhongxing Xu097fc982008-10-17 05:57:07 +00002292 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002293 if (Ex->getType()->isAnyComplexType()) {
2294 // Just report "Unknown."
2295 Dst.Add(*I);
2296 continue;
2297 }
2298
2299 // For all other types, UnaryOperator::Real is an identity operation.
2300 assert (U->getType() == Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002301 const GRState* state = GetState(*I);
2302 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002303 }
2304
2305 return;
2306 }
2307
2308 case UnaryOperator::Imag: {
2309
2310 Expr* Ex = U->getSubExpr()->IgnoreParens();
2311 NodeSet Tmp;
2312 Visit(Ex, Pred, Tmp);
2313
2314 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu097fc982008-10-17 05:57:07 +00002315 // FIXME: We don't have complex SValues yet.
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002316 if (Ex->getType()->isAnyComplexType()) {
2317 // Just report "Unknown."
2318 Dst.Add(*I);
2319 continue;
2320 }
2321
2322 // For all other types, UnaryOperator::Float returns 0.
2323 assert (Ex->getType()->isIntegerType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002324 const GRState* state = GetState(*I);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002325 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002326 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekfe952cb2008-06-19 17:55:38 +00002327 }
2328
2329 return;
2330 }
2331
2332 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002333 case UnaryOperator::OffsetOf:
Ted Kremenek5c4d4092008-04-30 21:45:55 +00002334 Dst.Add(Pred);
2335 return;
2336
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002337 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002338 case UnaryOperator::Extension: {
2339
2340 // Unary "+" is a no-op, similar to a parentheses. We still have places
2341 // where it may be a block-level expression, so we need to
2342 // generate an extra node that just propagates the value of the
2343 // subexpression.
2344
2345 Expr* Ex = U->getSubExpr()->IgnoreParens();
2346 NodeSet Tmp;
2347 Visit(Ex, Pred, Tmp);
2348
2349 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002350 const GRState* state = GetState(*I);
2351 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002352 }
2353
2354 return;
Ted Kremenek07baa252008-02-21 18:02:17 +00002355 }
Ted Kremenek1be5eb92008-01-24 02:28:56 +00002356
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002357 case UnaryOperator::AddrOf: {
Ted Kremenekb996ebc2008-02-20 04:02:35 +00002358
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002359 assert(!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002360 Expr* Ex = U->getSubExpr()->IgnoreParens();
2361 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002362 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002363
2364 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002365 const GRState* state = GetState(*I);
2366 SVal V = GetSVal(state, Ex);
2367 state = BindExpr(state, U, V);
2368 MakeNode(Dst, U, *I, state);
Ted Kremenekb8782e12008-02-21 19:15:37 +00002369 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002370
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002371 return;
2372 }
2373
2374 case UnaryOperator::LNot:
2375 case UnaryOperator::Minus:
2376 case UnaryOperator::Not: {
2377
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002378 assert (!asLValue);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002379 Expr* Ex = U->getSubExpr()->IgnoreParens();
2380 NodeSet Tmp;
2381 Visit(Ex, Pred, Tmp);
2382
2383 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002384 const GRState* state = GetState(*I);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002385
2386 // Get the value of the subexpression.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002387 SVal V = GetSVal(state, Ex);
Ted Kremenekcf807ad2008-09-30 05:32:44 +00002388
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002389 if (V.isUnknownOrUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002390 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremenek61b89eb2008-11-15 00:20:05 +00002391 continue;
2392 }
2393
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002394// QualType DstT = getContext().getCanonicalType(U->getType());
2395// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2396//
2397// if (DstT != SrcT) // Perform promotions.
2398// V = EvalCast(V, DstT);
2399//
2400// if (V.isUnknownOrUndef()) {
2401// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2402// continue;
2403// }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002404
2405 switch (U->getOpcode()) {
2406 default:
2407 assert(false && "Invalid Opcode.");
2408 break;
2409
2410 case UnaryOperator::Not:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002411 // FIXME: Do we need to handle promotions?
Ted Kremeneke66ba682009-02-13 01:45:31 +00002412 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002413 break;
2414
2415 case UnaryOperator::Minus:
Ted Kremenek8cbffa32008-10-01 00:21:14 +00002416 // FIXME: Do we need to handle promotions?
Ted Kremeneke66ba682009-02-13 01:45:31 +00002417 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002418 break;
2419
2420 case UnaryOperator::LNot:
2421
2422 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2423 //
2424 // Note: technically we do "E == 0", but this is the same in the
2425 // transfer functions as "0 == E".
2426
Zhongxing Xu097fc982008-10-17 05:57:07 +00002427 if (isa<Loc>(V)) {
2428 loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
Ted Kremenek74556a12009-03-26 03:35:11 +00002429 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2430 U->getType());
Ted Kremeneke66ba682009-02-13 01:45:31 +00002431 state = BindExpr(state, U, Result);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002432 }
2433 else {
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002434 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002435#if 0
Zhongxing Xu097fc982008-10-17 05:57:07 +00002436 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneke66ba682009-02-13 01:45:31 +00002437 state = SetSVal(state, U, Result);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002438#else
Ted Kremenek74556a12009-03-26 03:35:11 +00002439 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2440 U->getType());
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002441 continue;
2442#endif
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002443 }
2444
2445 break;
2446 }
2447
Ted Kremeneke66ba682009-02-13 01:45:31 +00002448 MakeNode(Dst, U, *I, state);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002449 }
2450
2451 return;
2452 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002453 }
2454
2455 // Handle ++ and -- (both pre- and post-increment).
2456
2457 assert (U->isIncrementDecrementOp());
2458 NodeSet Tmp;
2459 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002460 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002461
2462 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2463
Ted Kremeneke66ba682009-02-13 01:45:31 +00002464 const GRState* state = GetState(*I);
2465 SVal V1 = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002466
2467 // Perform a load.
2468 NodeSet Tmp2;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002469 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002470
2471 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2472
Ted Kremeneke66ba682009-02-13 01:45:31 +00002473 state = GetState(*I2);
2474 SVal V2 = GetSVal(state, Ex);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002475
2476 // Propagate unknown and undefined values.
2477 if (V2.isUnknownOrUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002478 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenek07baa252008-02-21 18:02:17 +00002479 continue;
2480 }
2481
Ted Kremeneke43de222009-03-11 03:54:24 +00002482 // Handle all other values.
Ted Kremenek22640ce2008-02-15 22:09:30 +00002483 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2484 : BinaryOperator::Sub;
Ted Kremeneke43de222009-03-11 03:54:24 +00002485
Ted Kremenek74556a12009-03-26 03:35:11 +00002486 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenek607415e2009-03-20 20:10:45 +00002487
2488 // Conjure a new symbol if necessary to recover precision.
2489 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result))
2490 Result = SVal::GetConjuredSymbolVal(SymMgr, Ex,
2491 Builder->getCurrentBlockCount());
2492
Ted Kremeneke66ba682009-02-13 01:45:31 +00002493 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002494
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002495 // Perform the store.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002496 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002497 }
Ted Kremenekd0d86202008-04-21 23:43:38 +00002498 }
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002499}
2500
Ted Kremenek31803c32008-03-17 21:11:24 +00002501void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2502 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2503}
2504
2505void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2506 AsmStmt::outputs_iterator I,
2507 AsmStmt::outputs_iterator E,
2508 NodeTy* Pred, NodeSet& Dst) {
2509 if (I == E) {
2510 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2511 return;
2512 }
2513
2514 NodeSet Tmp;
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002515 VisitLValue(*I, Pred, Tmp);
Ted Kremenek31803c32008-03-17 21:11:24 +00002516
2517 ++I;
2518
2519 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2520 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2521}
2522
2523void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2524 AsmStmt::inputs_iterator I,
2525 AsmStmt::inputs_iterator E,
2526 NodeTy* Pred, NodeSet& Dst) {
2527 if (I == E) {
2528
2529 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu097fc982008-10-17 05:57:07 +00002530 // should evaluate to Locs. Nuke all of their values.
Ted Kremenek31803c32008-03-17 21:11:24 +00002531
2532 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2533 // which interprets the inline asm and stores proper results in the
2534 // outputs.
2535
Ted Kremeneke66ba682009-02-13 01:45:31 +00002536 const GRState* state = GetState(Pred);
Ted Kremenek31803c32008-03-17 21:11:24 +00002537
2538 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2539 OE = A->end_outputs(); OI != OE; ++OI) {
2540
Ted Kremeneke66ba682009-02-13 01:45:31 +00002541 SVal X = GetSVal(state, *OI);
Zhongxing Xu097fc982008-10-17 05:57:07 +00002542 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenek31803c32008-03-17 21:11:24 +00002543
Zhongxing Xu097fc982008-10-17 05:57:07 +00002544 if (isa<Loc>(X))
Ted Kremeneke66ba682009-02-13 01:45:31 +00002545 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenek31803c32008-03-17 21:11:24 +00002546 }
2547
Ted Kremeneke66ba682009-02-13 01:45:31 +00002548 MakeNode(Dst, A, Pred, state);
Ted Kremenek31803c32008-03-17 21:11:24 +00002549 return;
2550 }
2551
2552 NodeSet Tmp;
2553 Visit(*I, Pred, Tmp);
2554
2555 ++I;
2556
2557 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2558 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2559}
2560
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002561void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2562 assert (Builder && "GRStmtNodeBuilder must be defined.");
2563
2564 unsigned size = Dst.size();
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002565
Ted Kremenek0a6a80b2008-04-23 20:12:28 +00002566 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2567 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002568
Ted Kremenekc7469542008-07-17 23:15:45 +00002569 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002570
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002571 // Handle the case where no nodes where generated.
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002572
Ted Kremenek0b03c6e2008-04-18 20:35:30 +00002573 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002574 MakeNode(Dst, S, Pred, GetState(Pred));
2575}
2576
Ted Kremenek108048c2008-03-31 15:02:58 +00002577void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2578
2579 Expr* R = S->getRetValue();
2580
2581 if (!R) {
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002582 EvalReturn(Dst, S, Pred);
Ted Kremenek108048c2008-03-31 15:02:58 +00002583 return;
2584 }
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002585
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002586 NodeSet Tmp;
2587 Visit(R, Pred, Tmp);
Ted Kremenek108048c2008-03-31 15:02:58 +00002588
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002589 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2590 SVal X = GetSVal((*I)->getState(), R);
2591
2592 // Check if we return the address of a stack variable.
2593 if (isa<loc::MemRegionVal>(X)) {
2594 // Determine if the value is on the stack.
2595 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek108048c2008-03-31 15:02:58 +00002596
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002597 if (R && getStateManager().hasStackStorage(R)) {
2598 // Create a special node representing the error.
2599 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2600 N->markAsSink();
2601 RetsStackAddr.insert(N);
2602 }
2603 continue;
2604 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002605 }
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002606 // Check if we return an undefined value.
2607 else if (X.isUndef()) {
2608 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2609 N->markAsSink();
2610 RetsUndef.insert(N);
2611 }
2612 continue;
2613 }
2614
Ted Kremenekc208f4e2008-04-16 23:05:51 +00002615 EvalReturn(Dst, S, *I);
Ted Kremenek28d40dc2008-11-21 00:27:44 +00002616 }
Ted Kremenek108048c2008-03-31 15:02:58 +00002617}
Ted Kremenekc6b7a1e2008-03-25 00:34:37 +00002618
Ted Kremenekca5f6202008-04-15 23:06:53 +00002619//===----------------------------------------------------------------------===//
2620// Transfer functions: Binary operators.
2621//===----------------------------------------------------------------------===//
2622
Ted Kremeneke66ba682009-02-13 01:45:31 +00002623const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenek6c438f82008-10-20 23:40:25 +00002624 NodeTy* Pred, SVal Denom) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002625
2626 // Divide by undefined? (potentially zero)
2627
2628 if (Denom.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002629 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002630
2631 if (DivUndef) {
2632 DivUndef->markAsSink();
2633 ExplicitBadDivides.insert(DivUndef);
2634 }
2635
Ted Kremenek6c438f82008-10-20 23:40:25 +00002636 return 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002637 }
2638
2639 // Check for divide/remainder-by-zero.
2640 // First, "assume" that the denominator is 0 or undefined.
2641
2642 bool isFeasibleZero = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002643 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002644
2645 // Second, "assume" that the denominator cannot be 0.
2646
2647 bool isFeasibleNotZero = false;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002648 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002649
2650 // Create the node for the divide-by-zero (if it occurred).
2651
2652 if (isFeasibleZero)
2653 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2654 DivZeroNode->markAsSink();
2655
2656 if (isFeasibleNotZero)
2657 ImplicitBadDivides.insert(DivZeroNode);
2658 else
2659 ExplicitBadDivides.insert(DivZeroNode);
2660
2661 }
2662
Ted Kremeneke66ba682009-02-13 01:45:31 +00002663 return isFeasibleNotZero ? state : 0;
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002664}
2665
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002666void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekaee121c2008-02-13 23:08:21 +00002667 GRExprEngine::NodeTy* Pred,
2668 GRExprEngine::NodeSet& Dst) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002669
2670 NodeSet Tmp1;
2671 Expr* LHS = B->getLHS()->IgnoreParens();
2672 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002673
Ted Kremenek52510d82008-12-06 02:39:30 +00002674 // FIXME: Add proper support for ObjCKVCRefExpr.
2675 if (isa<ObjCKVCRefExpr>(LHS)) {
2676 Visit(RHS, Pred, Dst);
2677 return;
2678 }
2679
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002680 if (B->isAssignmentOp())
Zhongxing Xu44e00b02008-10-16 06:09:51 +00002681 VisitLValue(LHS, Pred, Tmp1);
Ted Kremeneke1f38b62008-02-07 01:08:27 +00002682 else
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002683 Visit(LHS, Pred, Tmp1);
Ted Kremenekafba4b22008-01-16 00:53:15 +00002684
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002685 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002686
Zhongxing Xu097fc982008-10-17 05:57:07 +00002687 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke860db82008-01-17 00:52:48 +00002688
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002689 // Process the RHS.
2690
2691 NodeSet Tmp2;
2692 Visit(RHS, *I1, Tmp2);
2693
2694 // With both the LHS and RHS evaluated, process the operation itself.
2695
2696 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002697
Ted Kremeneke66ba682009-02-13 01:45:31 +00002698 const GRState* state = GetState(*I2);
2699 const GRState* OldSt = state;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002700
Ted Kremeneke66ba682009-02-13 01:45:31 +00002701 SVal RightV = GetSVal(state, RHS);
Ted Kremenek15cb0782008-02-06 22:50:25 +00002702 BinaryOperator::Opcode Op = B->getOpcode();
2703
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002704 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002705
Ted Kremenekf031b872008-01-23 19:59:44 +00002706 case BinaryOperator::Assign: {
Ted Kremenek07baa252008-02-21 18:02:17 +00002707
Ted Kremenekd4676512008-03-12 21:45:47 +00002708 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenek8f90e712008-10-17 22:23:12 +00002709 // FIXME: Handle structs.
2710 QualType T = RHS->getType();
Ted Kremenekd4676512008-03-12 21:45:47 +00002711
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002712 if ((RightV.isUnknown() ||
2713 !getConstraintManager().canReasonAbout(RightV))
2714 && (Loc::IsLocType(T) ||
2715 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenekd4676512008-03-12 21:45:47 +00002716 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002717 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
Ted Kremenekd4676512008-03-12 21:45:47 +00002718
Ted Kremenek79413a52008-11-13 06:10:40 +00002719 RightV = Loc::IsLocType(T)
Zhongxing Xu097fc982008-10-17 05:57:07 +00002720 ? cast<SVal>(loc::SymbolVal(Sym))
2721 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekd4676512008-03-12 21:45:47 +00002722 }
2723
Ted Kremenekd4676512008-03-12 21:45:47 +00002724 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002725 // to the L-Value represented by the LHS.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002726 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2727 RightV);
Ted Kremenekf5069582008-04-16 18:21:25 +00002728 continue;
Ted Kremenekf031b872008-01-23 19:59:44 +00002729 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002730
2731 case BinaryOperator::Div:
2732 case BinaryOperator::Rem:
2733
Ted Kremenek6c438f82008-10-20 23:40:25 +00002734 // Special checking for integer denominators.
Ted Kremenek79413a52008-11-13 06:10:40 +00002735 if (RHS->getType()->isIntegerType() &&
2736 RHS->getType()->isScalarType()) {
2737
Ted Kremeneke66ba682009-02-13 01:45:31 +00002738 state = CheckDivideZero(B, state, *I2, RightV);
2739 if (!state) continue;
Ted Kremenek6c438f82008-10-20 23:40:25 +00002740 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002741
2742 // FALL-THROUGH.
Ted Kremenekf031b872008-01-23 19:59:44 +00002743
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002744 default: {
2745
2746 if (B->isAssignmentOp())
Ted Kremenek07baa252008-02-21 18:02:17 +00002747 break;
Ted Kremenek07baa252008-02-21 18:02:17 +00002748
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002749 // Process non-assignements except commas or short-circuited
2750 // logical expressions (LAnd and LOr).
Ted Kremenek07baa252008-02-21 18:02:17 +00002751
Ted Kremenek74556a12009-03-26 03:35:11 +00002752 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002753
2754 if (Result.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002755 if (OldSt != state) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002756 // Generate a new node if we have already created a new state.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002757 MakeNode(Dst, B, *I2, state);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002758 }
2759 else
2760 Dst.Add(*I2);
2761
Ted Kremenekb8782e12008-02-21 19:15:37 +00002762 continue;
2763 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002764
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002765 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenek07baa252008-02-21 18:02:17 +00002766
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002767 // The operands were *not* undefined, but the result is undefined.
2768 // This is a special node that should be flagged as an error.
Ted Kremenek2c369792008-02-25 18:42:54 +00002769
Ted Kremeneke66ba682009-02-13 01:45:31 +00002770 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenekc2d07202008-02-28 20:32:03 +00002771 UndefNode->markAsSink();
2772 UndefResults.insert(UndefNode);
2773 }
2774
2775 continue;
2776 }
2777
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002778 // Otherwise, create a new node.
2779
Ted Kremeneke66ba682009-02-13 01:45:31 +00002780 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremenekf5069582008-04-16 18:21:25 +00002781 continue;
Ted Kremenek15cb0782008-02-06 22:50:25 +00002782 }
Ted Kremenekf031b872008-01-23 19:59:44 +00002783 }
Ted Kremenek07baa252008-02-21 18:02:17 +00002784
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002785 assert (B->isCompoundAssignmentOp());
2786
Ted Kremenek570882a2009-02-07 00:52:24 +00002787 switch (Op) {
2788 default:
2789 assert(0 && "Invalid opcode for compound assignment.");
2790 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2791 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2792 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2793 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2794 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2795 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2796 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2797 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2798 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2799 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek59fcaa02008-10-27 23:02:39 +00002800 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002801
2802 // Perform a load (the LHS). This performs the checks for
2803 // null dereferences, and so on.
2804 NodeSet Tmp3;
Ted Kremeneke66ba682009-02-13 01:45:31 +00002805 SVal location = GetSVal(state, LHS);
2806 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002807
2808 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2809
Ted Kremeneke66ba682009-02-13 01:45:31 +00002810 state = GetState(*I3);
2811 SVal V = GetSVal(state, LHS);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002812
Ted Kremenek6c438f82008-10-20 23:40:25 +00002813 // Check for divide-by-zero.
2814 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek79413a52008-11-13 06:10:40 +00002815 && RHS->getType()->isIntegerType()
2816 && RHS->getType()->isScalarType()) {
Ted Kremenek6c438f82008-10-20 23:40:25 +00002817
2818 // CheckDivideZero returns a new state where the denominator
2819 // is assumed to be non-zero.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002820 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenek6c438f82008-10-20 23:40:25 +00002821
Ted Kremeneke66ba682009-02-13 01:45:31 +00002822 if (!state)
Ted Kremenek6c438f82008-10-20 23:40:25 +00002823 continue;
2824 }
2825
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002826 // Propagate undefined values (left-side).
2827 if (V.isUndef()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002828 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002829 continue;
2830 }
2831
2832 // Propagate unknown values (left and right-side).
2833 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneke66ba682009-02-13 01:45:31 +00002834 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2835 location, UnknownVal());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002836 continue;
2837 }
2838
2839 // At this point:
2840 //
2841 // The LHS is not Undef/Unknown.
2842 // The RHS is not Unknown.
2843
2844 // Get the computation type.
Eli Friedman3cd92882009-03-28 01:22:36 +00002845 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002846 CTy = getContext().getCanonicalType(CTy);
Eli Friedman3cd92882009-03-28 01:22:36 +00002847
2848 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
2849 CLHSTy = getContext().getCanonicalType(CTy);
2850
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002851 QualType LTy = getContext().getCanonicalType(LHS->getType());
2852 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman3cd92882009-03-28 01:22:36 +00002853
2854 // Promote LHS.
2855 V = EvalCast(V, CLHSTy);
2856
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002857 // Evaluate operands and promote to result type.
Ted Kremenek6c438f82008-10-20 23:40:25 +00002858 if (RightV.isUndef()) {
Ted Kremenekb2de2ef2008-09-20 01:50:34 +00002859 // Propagate undefined values (right-side).
Ted Kremenek3f755632009-03-05 03:42:31 +00002860 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneke66ba682009-02-13 01:45:31 +00002861 RightV);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002862 continue;
2863 }
2864
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002865 // Compute the result of the operation.
Ted Kremenek74556a12009-03-26 03:35:11 +00002866 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002867
2868 if (Result.isUndef()) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002869 // The operands were not undefined, but the result is undefined.
Ted Kremeneke66ba682009-02-13 01:45:31 +00002870 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002871 UndefNode->markAsSink();
2872 UndefResults.insert(UndefNode);
2873 }
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002874 continue;
2875 }
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002876
2877 // EXPERIMENTAL: "Conjured" symbols.
2878 // FIXME: Handle structs.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002879
2880 SVal LHSVal;
2881
Ted Kremenekd6a5a422009-03-11 02:24:48 +00002882 if ((Result.isUnknown() ||
2883 !getConstraintManager().canReasonAbout(Result))
2884 && (Loc::IsLocType(CTy)
2885 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek943ed4b2008-10-21 19:49:01 +00002886
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002887 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002888
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002889 // The symbolic value is actually for the type of the left-hand side
2890 // expression, not the computation type, as this is the value the
2891 // LValue on the LHS will bind to.
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002892 SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002893 LHSVal = Loc::IsLocType(LTy)
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002894 ? cast<SVal>(loc::SymbolVal(Sym))
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002895 : cast<SVal>(nonloc::SymbolVal(Sym));
2896
Zhongxing Xu5c70c772008-11-23 05:52:28 +00002897 // However, we need to convert the symbol to the computation type.
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002898 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenekfa50a3e2008-10-20 23:13:25 +00002899 }
Ted Kremenek59bbf8e2008-11-15 04:01:56 +00002900 else {
2901 // The left-hand side may bind to a different value then the
2902 // computation type.
2903 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2904 }
2905
Ted Kremeneke66ba682009-02-13 01:45:31 +00002906 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2907 LHSVal);
Ted Kremenek5f6b4422008-04-29 21:04:26 +00002908 }
Ted Kremenekafba4b22008-01-16 00:53:15 +00002909 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00002910 }
Ted Kremenek68d70a82008-01-15 23:55:06 +00002911}
Ted Kremenekd2500ab2008-01-16 18:18:48 +00002912
2913//===----------------------------------------------------------------------===//
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002914// Transfer-function Helpers.
2915//===----------------------------------------------------------------------===//
2916
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002917void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002918 BinaryOperator::Opcode Op,
Zhongxing Xu097fc982008-10-17 05:57:07 +00002919 NonLoc L, NonLoc R,
Ted Kremenek74556a12009-03-26 03:35:11 +00002920 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek9c4ce602008-07-18 05:53:58 +00002921
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002922 GRStateSet OStates;
Ted Kremenek74556a12009-03-26 03:35:11 +00002923 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek9c4ce602008-07-18 05:53:58 +00002924
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002925 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek9c4ce602008-07-18 05:53:58 +00002926 MakeNode(Dst, Ex, Pred, *I);
2927}
2928
Ted Kremeneke66ba682009-02-13 01:45:31 +00002929void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek9c4ce602008-07-18 05:53:58 +00002930 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremenek74556a12009-03-26 03:35:11 +00002931 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002932
Ted Kremeneke66ba682009-02-13 01:45:31 +00002933 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremenek74556a12009-03-26 03:35:11 +00002934 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002935}
2936
Ted Kremenek74556a12009-03-26 03:35:11 +00002937SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
2938 QualType T) {
Ted Kremenek4281e622009-01-30 19:27:39 +00002939
2940 if (L.isUndef() || R.isUndef())
2941 return UndefinedVal();
2942
2943 if (L.isUnknown() || R.isUnknown())
2944 return UnknownVal();
2945
2946 if (isa<Loc>(L)) {
2947 if (isa<Loc>(R))
2948 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2949 else
2950 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2951 }
2952
2953 if (isa<Loc>(R)) {
2954 // Support pointer arithmetic where the increment/decrement operand
2955 // is on the left and the pointer on the right.
2956
2957 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2958
2959 // Commute the operands.
2960 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2961 cast<NonLoc>(L));
2962 }
2963 else
2964 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremenek74556a12009-03-26 03:35:11 +00002965 cast<NonLoc>(R), T);
Ted Kremenek4281e622009-01-30 19:27:39 +00002966}
2967
Ted Kremenekfa81dff2008-07-17 21:27:31 +00002968//===----------------------------------------------------------------------===//
Ted Kremenek3862eb12008-02-14 22:36:46 +00002969// Visualization.
Ted Kremenekd2500ab2008-01-16 18:18:48 +00002970//===----------------------------------------------------------------------===//
2971
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002972#ifndef NDEBUG
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002973static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00002974static SourceManager* GraphPrintSourceManager;
Ted Kremenek428d39e2008-01-30 23:24:39 +00002975
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002976namespace llvm {
2977template<>
Ted Kremenek30fa28b2008-02-13 17:41:41 +00002978struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00002979 public DefaultDOTGraphTraits {
Ted Kremenek08cfd832008-02-08 21:10:02 +00002980
Ted Kremeneka853de62008-02-14 22:54:53 +00002981 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
2982
2983 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenekbf988d02008-02-19 00:22:37 +00002984 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenekb31af242008-02-28 09:25:22 +00002985 GraphPrintCheckerState->isUndefDeref(N) ||
2986 GraphPrintCheckerState->isUndefStore(N) ||
2987 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek75f32c62008-03-07 19:04:53 +00002988 GraphPrintCheckerState->isExplicitBadDivide(N) ||
2989 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek43863eb2008-02-29 23:14:48 +00002990 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek9b31f5b2008-02-29 23:53:11 +00002991 GraphPrintCheckerState->isBadCall(N) ||
2992 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka853de62008-02-14 22:54:53 +00002993 return "color=\"red\",style=\"filled\"";
2994
Ted Kremenekc2d07202008-02-28 20:32:03 +00002995 if (GraphPrintCheckerState->isNoReturnCall(N))
2996 return "color=\"blue\",style=\"filled\"";
2997
Ted Kremeneka853de62008-02-14 22:54:53 +00002998 return "";
2999 }
Ted Kremeneke6536692008-02-06 03:56:15 +00003000
Ted Kremenek30fa28b2008-02-13 17:41:41 +00003001 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003002 std::ostringstream Out;
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003003
3004 // Program Location.
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003005 ProgramPoint Loc = N->getLocation();
3006
3007 switch (Loc.getKind()) {
3008 case ProgramPoint::BlockEntranceKind:
3009 Out << "Block Entrance: B"
3010 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3011 break;
3012
3013 case ProgramPoint::BlockExitKind:
3014 assert (false);
3015 break;
3016
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003017 default: {
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003018 if (isa<PostStmt>(Loc)) {
3019 const PostStmt& L = cast<PostStmt>(Loc);
3020 Stmt* S = L.getStmt();
3021 SourceLocation SLoc = S->getLocStart();
3022
3023 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3024 llvm::raw_os_ostream OutS(Out);
3025 S->printPretty(OutS);
3026 OutS.flush();
3027
3028 if (SLoc.isFileID()) {
3029 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003030 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3031 << " col="
3032 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3033 << "\\l";
Ted Kremeneke27c37a2008-12-16 22:02:27 +00003034 }
3035
3036 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3037 Out << "\\|Implicit-Null Dereference.\\l";
3038 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3039 Out << "\\|Explicit-Null Dereference.\\l";
3040 else if (GraphPrintCheckerState->isUndefDeref(N))
3041 Out << "\\|Dereference of undefialied value.\\l";
3042 else if (GraphPrintCheckerState->isUndefStore(N))
3043 Out << "\\|Store to Undefined Loc.";
3044 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3045 Out << "\\|Explicit divide-by zero or undefined value.";
3046 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3047 Out << "\\|Implicit divide-by zero or undefined value.";
3048 else if (GraphPrintCheckerState->isUndefResult(N))
3049 Out << "\\|Result of operation is undefined.";
3050 else if (GraphPrintCheckerState->isNoReturnCall(N))
3051 Out << "\\|Call to function marked \"noreturn\".";
3052 else if (GraphPrintCheckerState->isBadCall(N))
3053 Out << "\\|Call to NULL/Undefined.";
3054 else if (GraphPrintCheckerState->isUndefArg(N))
3055 Out << "\\|Argument in call is undefined";
3056
3057 break;
3058 }
3059
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003060 const BlockEdge& E = cast<BlockEdge>(Loc);
3061 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3062 << E.getDst()->getBlockID() << ')';
Ted Kremenek90960972008-01-30 23:03:39 +00003063
3064 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003065
3066 SourceLocation SLoc = T->getLocStart();
3067
Ted Kremenek90960972008-01-30 23:03:39 +00003068 Out << "\\|Terminator: ";
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003069
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003070 llvm::raw_os_ostream OutS(Out);
3071 E.getSrc()->printTerminator(OutS);
3072 OutS.flush();
Ted Kremenek90960972008-01-30 23:03:39 +00003073
Ted Kremenekf97c6682008-03-09 03:30:59 +00003074 if (SLoc.isFileID()) {
3075 Out << "\\lline="
Chris Lattnere79fc852009-02-04 00:55:58 +00003076 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3077 << " col="
3078 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenekf97c6682008-03-09 03:30:59 +00003079 }
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003080
Ted Kremenekaee121c2008-02-13 23:08:21 +00003081 if (isa<SwitchStmt>(T)) {
3082 Stmt* Label = E.getDst()->getLabel();
3083
3084 if (Label) {
3085 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3086 Out << "\\lcase ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003087 llvm::raw_os_ostream OutS(Out);
3088 C->getLHS()->printPretty(OutS);
3089 OutS.flush();
3090
Ted Kremenekaee121c2008-02-13 23:08:21 +00003091 if (Stmt* RHS = C->getRHS()) {
3092 Out << " .. ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00003093 RHS->printPretty(OutS);
3094 OutS.flush();
Ted Kremenekaee121c2008-02-13 23:08:21 +00003095 }
3096
3097 Out << ":";
3098 }
3099 else {
3100 assert (isa<DefaultStmt>(Label));
3101 Out << "\\ldefault:";
3102 }
3103 }
3104 else
3105 Out << "\\l(implicit) default:";
3106 }
3107 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenek90960972008-01-30 23:03:39 +00003108 // FIXME
3109 }
3110 else {
3111 Out << "\\lCondition: ";
3112 if (*E.getSrc()->succ_begin() == E.getDst())
3113 Out << "true";
3114 else
3115 Out << "false";
3116 }
3117
3118 Out << "\\l";
3119 }
Ted Kremenek428d39e2008-01-30 23:24:39 +00003120
Ted Kremenekb31af242008-02-28 09:25:22 +00003121 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3122 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek428d39e2008-01-30 23:24:39 +00003123 }
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003124 }
3125 }
3126
Ted Kremenekf4b49df2008-02-28 10:21:43 +00003127 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek08cfd832008-02-08 21:10:02 +00003128
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003129 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3130 state.printDOT(Out);
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003131
Ted Kremenekbacd6cd2008-01-23 22:30:44 +00003132 Out << "\\l";
Ted Kremenekdd9e97d2008-01-16 21:46:15 +00003133 return Out.str();
3134 }
3135};
3136} // end llvm namespace
3137#endif
3138
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003139#ifndef NDEBUG
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003140template <typename ITERATOR>
3141GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3142
3143template <>
3144GRExprEngine::NodeTy*
3145GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3146 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3147 return I->first;
3148}
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003149#endif
3150
3151void GRExprEngine::ViewGraph(bool trim) {
Ted Kremeneke44a8302008-03-11 18:25:33 +00003152#ifndef NDEBUG
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003153 if (trim) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003154 std::vector<NodeTy*> Src;
Ted Kremenekf00d09b2009-03-11 01:41:22 +00003155
3156 // Flush any outstanding reports to make sure we cover all the nodes.
3157 // This does not cause them to get displayed.
3158 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3159 const_cast<BugType*>(*I)->FlushReports(BR);
3160
3161 // Iterate through the reports and get their nodes.
3162 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3163 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3164 const BugReportEquivClass& EQ = *I2;
3165 const BugReport &R = **EQ.begin();
3166 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3167 if (N) Src.push_back(N);
3168 }
3169 }
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00003170
Ted Kremenek83f04aa2008-03-12 17:18:20 +00003171 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003172 }
Ted Kremeneke44a8302008-03-11 18:25:33 +00003173 else {
3174 GraphPrintCheckerState = this;
3175 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00003176
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003177 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremeneke44a8302008-03-11 18:25:33 +00003178
3179 GraphPrintCheckerState = NULL;
3180 GraphPrintSourceManager = NULL;
3181 }
3182#endif
3183}
3184
3185void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3186#ifndef NDEBUG
3187 GraphPrintCheckerState = this;
3188 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00003189
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003190 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneke44a8302008-03-11 18:25:33 +00003191
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003192 if (!TrimmedG.get())
Ted Kremeneke44a8302008-03-11 18:25:33 +00003193 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekbf6babf2009-02-04 23:49:09 +00003194 else
Ted Kremeneke44a8302008-03-11 18:25:33 +00003195 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenek5e1e05c2008-03-07 22:58:01 +00003196
Ted Kremenek428d39e2008-01-30 23:24:39 +00003197 GraphPrintCheckerState = NULL;
Ted Kremenek8b41e8c2008-03-07 20:57:30 +00003198 GraphPrintSourceManager = NULL;
Ted Kremenek3862eb12008-02-14 22:36:46 +00003199#endif
Ted Kremenekd2500ab2008-01-16 18:18:48 +00003200}