blob: 46d1f798cf0f436a4d6009465f6bfed479881f3a [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenekb930d7a2009-04-01 06:52:48 +000016#include "clang/AST/ParentMap.h"
Ted Kremenek77349cb2008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000021#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000022#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000023#include "llvm/ADT/ImmutableList.h"
24#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000026
Ted Kremenek0f5f0592008-02-27 06:07:00 +000027#ifndef NDEBUG
28#include "llvm/Support/GraphWriter.h"
29#include <sstream>
30#endif
31
Ted Kremenekb387a3f2008-02-14 22:16:04 +000032using namespace clang;
33using llvm::dyn_cast;
34using llvm::cast;
35using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036
Ted Kremeneke695e1c2008-04-15 23:06:53 +000037//===----------------------------------------------------------------------===//
38// Engine construction and deletion.
39//===----------------------------------------------------------------------===//
40
Ted Kremenekbdb435d2008-07-11 18:37:32 +000041namespace {
42
43class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
44 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
45 typedef llvm::DenseMap<void*,Checks> MapTy;
46
47 MapTy M;
48 Checks::Factory F;
Ted Kremenek536aa022009-03-30 17:53:05 +000049 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000050
51public:
Ted Kremenek536aa022009-03-30 17:53:05 +000052 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
53 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenekbdb435d2008-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 Kremenek536aa022009-03-30 17:53:05 +000071 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-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 Kremenek536aa022009-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 Kremenekcf118d42009-02-04 23:49:09 +000082
Ted Kremenek4adc81e2008-08-13 04:27:00 +000083 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-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 Kremenekbdb435d2008-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 Kremenek536aa022009-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 Kremenekbdb435d2008-07-11 18:37:32 +000097
Ted Kremenekbdb435d2008-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 Kremeneke448ab42008-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 Kremenekdaa497e2008-03-09 18:05:48 +0000113
Ted Kremenek8b233612008-07-02 20:13:38 +0000114GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000115 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000116 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000117 StoreManagerCreator SMC,
118 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000123 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000125 ValMgr(StateMgr.getValueManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000126 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000127 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
128 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000129 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000130 BR(BRD, *this),
131 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132
Ted Kremenek1a654b62008-06-20 21:45:25 +0000133GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000134 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000136}
137
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000138//===----------------------------------------------------------------------===//
139// Utility methods.
140//===----------------------------------------------------------------------===//
141
Ted Kremenek186350f2008-04-23 20:12:28 +0000142
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000143void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000144 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000146 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000147}
148
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000149void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
150 if (!BatchAuditor)
151 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
152
153 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000154}
155
Ted Kremenek536aa022009-03-30 17:53:05 +0000156void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
157 if (!BatchAuditor)
158 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
159
160 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
161}
162
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000163const GRState* GRExprEngine::getInitialState() {
Ted Kremenek52e56022009-04-10 00:59:50 +0000164 const GRState *state = StateMgr.getInitialState();
165
166 // Precondition: the first argument of 'main' is an integer guaranteed
167 // to be > 0.
168 // FIXME: It would be nice if we had a more general mechanism to add
169 // such preconditions. Some day.
170 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
171 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
172 FD->getNumParams() > 0) {
173 const ParmVarDecl *PD = FD->getParamDecl(0);
174 QualType T = PD->getType();
175 if (T->isIntegerType())
176 if (const MemRegion *R = StateMgr.getRegion(PD)) {
177 SVal V = GetSVal(state, loc::MemRegionVal(R));
178 SVal Constraint = EvalBinOp(BinaryOperator::GT, V,
179 ValMgr.makeZeroVal(T),
180 getContext().IntTy);
181 bool isFeasible = false;
182 const GRState *newState = Assume(state, Constraint, true,
183 isFeasible);
184 if (newState) state = newState;
185 }
186 }
187
188 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000189}
190
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000191//===----------------------------------------------------------------------===//
192// Top-level transfer function logic (Dispatcher).
193//===----------------------------------------------------------------------===//
194
195void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
196
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000197 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
198 S->getLocStart(),
199 "Error evaluating statement");
200
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000201 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000202 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000203
204 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000205 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000206 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207
208 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000209 if (BatchAuditor)
210 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000211
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000212 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000213 SymbolReaper SymReaper(Liveness, SymMgr);
214 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
215 CurrentStmt, SymReaper)
216 : EntryNode->getState();
217
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000219 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000220
Ted Kremenek241677a2009-01-21 22:26:05 +0000221 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000222 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000223 else {
224 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000225 SaveOr OldHasGen(Builder->HasGeneratedNode);
226
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000227 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
228 Builder->PurgingDeadSymbols = true;
229
Ted Kremenek729a9a22008-07-17 23:15:45 +0000230 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000231 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000232
233 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
234 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000235 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000236
237 bool HasAutoGenerated = false;
238
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000239 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000240
241 NodeSet Dst;
242
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000243 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000244 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
245
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000246 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000247 Visit(S, *I, Dst);
248
249 // Do we need to auto-generate a node? We only need to do this to generate
250 // a node with a "cleaned" state; GRCoreEngine will actually handle
251 // auto-transitions for other cases.
252 if (Dst.size() == 1 && *Dst.begin() == EntryNode
253 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
254 HasAutoGenerated = true;
255 builder.generateNode(S, GetState(EntryNode), *I);
256 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000257 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000260 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000261 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000262
263 // FIXME: Consolidate.
264 StateMgr.CurrentStmt = 0;
265 CurrentStmt = 0;
266
Ted Kremenek846d4e92008-04-24 23:35:58 +0000267 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000268}
269
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000270void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
271 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
272 S->getLocStart(),
273 "Error evaluating statement");
274
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000275 // FIXME: add metadata to the CFG so that we can disable
276 // this check when we KNOW that there is no block-level subexpression.
277 // The motivation is that this check requires a hashtable lookup.
278
279 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
280 Dst.Add(Pred);
281 return;
282 }
283
284 switch (S->getStmtClass()) {
285
286 default:
287 // Cases we intentionally have "default" handle:
288 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
289
290 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
291 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000292
293 case Stmt::ArraySubscriptExprClass:
294 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
295 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000296
297 case Stmt::AsmStmtClass:
298 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
299 break;
300
301 case Stmt::BinaryOperatorClass: {
302 BinaryOperator* B = cast<BinaryOperator>(S);
303
304 if (B->isLogicalOp()) {
305 VisitLogicalExpr(B, Pred, Dst);
306 break;
307 }
308 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000309 const GRState* state = GetState(Pred);
310 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000311 break;
312 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000313
Ted Kremenek48af2a92009-02-25 22:32:02 +0000314 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
315 NodeSet Tmp;
316 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000317 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000318 }
319 else
320 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
321
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000322 break;
323 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000324
Douglas Gregorb4609802008-11-14 16:09:21 +0000325 case Stmt::CallExprClass:
326 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000327 CallExpr* C = cast<CallExpr>(S);
328 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000329 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000330 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000331
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000332 // FIXME: ChooseExpr is really a constant. We need to fix
333 // the CFG do not model them as explicit control-flow.
334
335 case Stmt::ChooseExprClass: { // __builtin_choose_expr
336 ChooseExpr* C = cast<ChooseExpr>(S);
337 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
338 break;
339 }
340
341 case Stmt::CompoundAssignOperatorClass:
342 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
343 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000344
345 case Stmt::CompoundLiteralExprClass:
346 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
347 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000348
349 case Stmt::ConditionalOperatorClass: { // '?' operator
350 ConditionalOperator* C = cast<ConditionalOperator>(S);
351 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
352 break;
353 }
354
355 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000356 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000357 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000358 break;
359
360 case Stmt::DeclStmtClass:
361 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
362 break;
363
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000364 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000365 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000366 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367 VisitCast(C, C->getSubExpr(), Pred, Dst);
368 break;
369 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000370
371 case Stmt::InitListExprClass:
372 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
373 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000374
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000375 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000376 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
377 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000378
379 case Stmt::ObjCIvarRefExprClass:
380 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
381 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000382
383 case Stmt::ObjCForCollectionStmtClass:
384 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
385 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000386
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000387 case Stmt::ObjCMessageExprClass: {
388 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
389 break;
390 }
391
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000392 case Stmt::ObjCAtThrowStmtClass: {
393 // FIXME: This is not complete. We basically treat @throw as
394 // an abort.
395 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
396 Builder->BuildSinks = true;
397 MakeNode(Dst, S, Pred, GetState(Pred));
398 break;
399 }
400
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000401 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000402 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 break;
404
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000405 case Stmt::ReturnStmtClass:
406 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
407 break;
408
Sebastian Redl05189992008-11-11 17:56:53 +0000409 case Stmt::SizeOfAlignOfExprClass:
410 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 break;
412
413 case Stmt::StmtExprClass: {
414 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000415
416 if (SE->getSubStmt()->body_empty()) {
417 // Empty statement expression.
418 assert(SE->getType() == getContext().VoidTy
419 && "Empty statement expression must have void type.");
420 Dst.Add(Pred);
421 break;
422 }
423
424 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
425 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000426 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000427 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000428 else
429 Dst.Add(Pred);
430
431 break;
432 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000433
434 case Stmt::StringLiteralClass:
435 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
436 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000437
Ted Kremenek72374592009-03-18 23:49:26 +0000438 case Stmt::UnaryOperatorClass: {
439 UnaryOperator *U = cast<UnaryOperator>(S);
440 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
441 NodeSet Tmp;
442 VisitUnaryOperator(U, Pred, Tmp, false);
443 EvalEagerlyAssume(Dst, Tmp, U);
444 }
445 else
446 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000447 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000448 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000449 }
450}
451
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000452void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000453
454 Ex = Ex->IgnoreParens();
455
456 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
457 Dst.Add(Pred);
458 return;
459 }
460
461 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000462
463 case Stmt::ArraySubscriptExprClass:
464 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
465 return;
466
467 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000468 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000469 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
470 return;
471
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000472 case Stmt::ObjCIvarRefExprClass:
473 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
474 return;
475
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000476 case Stmt::UnaryOperatorClass:
477 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
478 return;
479
480 case Stmt::MemberExprClass:
481 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
482 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000483
Ted Kremenek4f090272008-10-27 21:54:31 +0000484 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000485 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000486 return;
487
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000488 case Stmt::ObjCPropertyRefExprClass:
489 // FIXME: Property assignments are lvalues, but not really "locations".
490 // e.g.: self.x = something;
491 // Here the "self.x" really can translate to a method call (setter) when
492 // the assignment is made. Moreover, the entire assignment expression
493 // evaluate to whatever "something" is, not calling the "getter" for
494 // the property (which would make sense since it can have side effects).
495 // We'll probably treat this as a location, but not one that we can
496 // take the address of. Perhaps we need a new SVal class for cases
497 // like thsis?
498 // Note that we have a similar problem for bitfields, since they don't
499 // have "locations" in the sense that we can take their address.
500 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000501 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000502
503 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000504 const GRState* state = GetState(Pred);
505 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
506 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000507 return;
508 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000509
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000510 default:
511 // Arbitrary subexpressions can return aggregate temporaries that
512 // can be used in a lvalue context. We need to enhance our support
513 // of such temporaries in both the environment and the store, so right
514 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000515 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000516 "Other kinds of expressions with non-aggregate/union types do"
517 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000518
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000519 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000520 }
521}
522
523//===----------------------------------------------------------------------===//
524// Block entrance. (Update counters).
525//===----------------------------------------------------------------------===//
526
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000527bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000528 GRBlockCounter BC) {
529
530 return BC.getNumVisited(B->getBlockID()) < 3;
531}
532
533//===----------------------------------------------------------------------===//
534// Branch processing.
535//===----------------------------------------------------------------------===//
536
Ted Kremeneka8538d92009-02-13 01:45:31 +0000537const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000538 Stmt* Terminator,
539 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000540
541 switch (Terminator->getStmtClass()) {
542 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000543 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000544
545 case Stmt::BinaryOperatorClass: { // '&&' and '||'
546
547 BinaryOperator* B = cast<BinaryOperator>(Terminator);
548 BinaryOperator::Opcode Op = B->getOpcode();
549
550 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
551
552 // For &&, if we take the true branch, then the value of the whole
553 // expression is that of the RHS expression.
554 //
555 // For ||, if we take the false branch, then the value of the whole
556 // expression is that of the RHS expression.
557
558 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
559 (Op == BinaryOperator::LOr && !branchTaken)
560 ? B->getRHS() : B->getLHS();
561
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000563 }
564
565 case Stmt::ConditionalOperatorClass: { // ?:
566
567 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
568
569 // For ?, if branchTaken == true then the value is either the LHS or
570 // the condition itself. (GNU extension).
571
572 Expr* Ex;
573
574 if (branchTaken)
575 Ex = C->getLHS() ? C->getLHS() : C->getCond();
576 else
577 Ex = C->getRHS();
578
Ted Kremeneka8538d92009-02-13 01:45:31 +0000579 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000580 }
581
582 case Stmt::ChooseExprClass: { // ?:
583
584 ChooseExpr* C = cast<ChooseExpr>(Terminator);
585
586 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000587 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000588 }
589 }
590}
591
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000592/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
593/// to try to recover some path-sensitivity for casts of symbolic
594/// integers that promote their values (which are currently not tracked well).
595/// This function returns the SVal bound to Condition->IgnoreCasts if all the
596// cast(s) did was sign-extend the original value.
597static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
598 Stmt* Condition, ASTContext& Ctx) {
599
600 Expr *Ex = dyn_cast<Expr>(Condition);
601 if (!Ex)
602 return UnknownVal();
603
604 uint64_t bits = 0;
605 bool bitsInit = false;
606
607 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
608 QualType T = CE->getType();
609
610 if (!T->isIntegerType())
611 return UnknownVal();
612
613 uint64_t newBits = Ctx.getTypeSize(T);
614 if (!bitsInit || newBits < bits) {
615 bitsInit = true;
616 bits = newBits;
617 }
618
619 Ex = CE->getSubExpr();
620 }
621
622 // We reached a non-cast. Is it a symbolic value?
623 QualType T = Ex->getType();
624
625 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
626 return UnknownVal();
627
628 return StateMgr.GetSVal(state, Ex);
629}
630
Ted Kremenekaf337412008-11-12 19:24:17 +0000631void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000632 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000633
Ted Kremeneke7d22112008-02-11 19:21:59 +0000634 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000635 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000636 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000637
Ted Kremenekb2331832008-02-15 22:29:00 +0000638 // Check for NULL conditions; e.g. "for(;;)"
639 if (!Condition) {
640 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000641 return;
642 }
643
Ted Kremenek21028dd2009-03-11 03:54:24 +0000644 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
645 Condition->getLocStart(),
646 "Error evaluating branch");
647
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000648 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000649
650 switch (V.getBaseKind()) {
651 default:
652 break;
653
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000654 case SVal::UnknownKind: {
655 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
656 if (Ex->getType()->isIntegerType()) {
657 // Try to recover some path-sensitivity. Right now casts of symbolic
658 // integers that promote their values are currently not tracked well.
659 // If 'Condition' is such an expression, try and recover the
660 // underlying value and use that instead.
661 SVal recovered = RecoverCastedSymbol(getStateManager(),
662 builder.getState(), Condition,
663 getContext());
664
665 if (!recovered.isUnknown()) {
666 V = recovered;
667 break;
668 }
669 }
670 }
671
Ted Kremenek58b33212008-02-26 19:40:44 +0000672 builder.generateNode(MarkBranch(PrevState, Term, true), true);
673 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000674 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000675 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000676
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000677 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000678 NodeTy* N = builder.generateNode(PrevState, true);
679
680 if (N) {
681 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000682 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000683 }
684
685 builder.markInfeasible(false);
686 return;
687 }
688 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000689
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000690 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000691
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000692 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000693 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000694
695 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000696 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000697 else
698 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000699
700 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000701
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000702 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000703 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000704
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000705 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000706 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000707 else
708 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000709}
710
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000711/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000712/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000713void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000714
Ted Kremeneka8538d92009-02-13 01:45:31 +0000715 const GRState* state = builder.getState();
716 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000717
718 // Three possibilities:
719 //
720 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000721 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000722 // (3) We have no clue about the label. Dispatch to all targets.
723 //
724
725 typedef IndirectGotoNodeBuilder::iterator iterator;
726
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000727 if (isa<loc::GotoLabel>(V)) {
728 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000729
730 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000731 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000732 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000733 return;
734 }
735 }
736
737 assert (false && "No block with label.");
738 return;
739 }
740
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000741 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000742 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000743 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000744 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000745 return;
746 }
747
748 // This is really a catch-all. We don't support symbolics yet.
749
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000750 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000751
752 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000753 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000754}
Ted Kremenekf233d482008-02-05 00:26:40 +0000755
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000756
757void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
758 NodeTy* Pred, NodeSet& Dst) {
759
760 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
761
Ted Kremeneka8538d92009-02-13 01:45:31 +0000762 const GRState* state = GetState(Pred);
763 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000764
765 assert (X.isUndef());
766
767 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
768
769 assert (SE);
770
Ted Kremeneka8538d92009-02-13 01:45:31 +0000771 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000772
773 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000774 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000775}
776
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
778/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000779void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
780 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000781 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000782 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000783 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000784
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000785 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000786 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000787 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000788 return;
789 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000790
Ted Kremeneka8538d92009-02-13 01:45:31 +0000791 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000792 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000793
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000794 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000795 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000796
797 // Evaluate the LHS of the case value.
798 Expr::EvalResult V1;
799 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000800
Ted Kremenek72afb372009-01-17 01:54:16 +0000801 // Sanity checks. These go away in Release builds.
802 assert(b && V1.Val.isInt() && !V1.HasSideEffects
803 && "Case condition must evaluate to an integer constant.");
804 b = b; // silence unused variable warning
805 assert(V1.Val.getInt().getBitWidth() ==
806 getContext().getTypeSize(CondE->getType()));
807
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000808 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000809 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000810
811 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000812 b = E->Evaluate(V2, getContext());
813 assert(b && V2.Val.isInt() && !V2.HasSideEffects
814 && "Case condition must evaluate to an integer constant.");
815 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000816 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000817 else
818 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000819
820 // FIXME: Eventually we should replace the logic below with a range
821 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000822 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000823
Ted Kremenek14a11402008-03-17 22:17:56 +0000824 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000825 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000826 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
827 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000828
Ted Kremenek72afb372009-01-17 01:54:16 +0000829 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000830 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000831 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000832
833 if (isFeasible) {
834 builder.generateCaseStmtNode(I, StNew);
835
836 // If CondV evaluates to a constant, then we know that this
837 // is the *only* case that we can take, so stop evaluating the
838 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000839 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000840 return;
841 }
842
843 // Now "assume" that the case doesn't match. Add this state
844 // to the default state (if it is feasible).
845
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000846 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000847 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000848
Ted Kremenek5014ab12008-04-23 05:03:18 +0000849 if (isFeasible) {
850 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000851 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000852 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000853
Ted Kremenek14a11402008-03-17 22:17:56 +0000854 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000855 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000856 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000857
Ted Kremenek72afb372009-01-17 01:54:16 +0000858 ++V1.Val.getInt();
859 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000860
861 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000862 }
863
864 // If we reach here, than we know that the default branch is
865 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000866 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000867}
868
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000869//===----------------------------------------------------------------------===//
870// Transfer functions: logical operations ('&&', '||').
871//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000872
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000873void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000874 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000875
Ted Kremenek05a23782008-02-26 19:05:15 +0000876 assert (B->getOpcode() == BinaryOperator::LAnd ||
877 B->getOpcode() == BinaryOperator::LOr);
878
879 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
880
Ted Kremeneka8538d92009-02-13 01:45:31 +0000881 const GRState* state = GetState(Pred);
882 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000883
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000884 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000885
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000886 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000887
888 assert (Ex);
889
890 if (Ex == B->getRHS()) {
891
Ted Kremeneka8538d92009-02-13 01:45:31 +0000892 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000893
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000894 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000895
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000896 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000897 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000898 return;
899 }
900
Ted Kremenek05a23782008-02-26 19:05:15 +0000901 // We took the RHS. Because the value of the '&&' or '||' expression must
902 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
903 // or 1. Alternatively, we could take a lazy approach, and calculate this
904 // value later when necessary. We don't have the machinery in place for
905 // this right now, and since most logical expressions are used for branches,
906 // the payoff is not likely to be large. Instead, we do eager evaluation.
907
908 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000909 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000910
911 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000912 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000913 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000914
915 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000916 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000917
918 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000919 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000920 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000921 }
922 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000923 // We took the LHS expression. Depending on whether we are '&&' or
924 // '||' we know what the value of the expression is via properties of
925 // the short-circuiting.
926
927 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000928 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000929 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000930}
Ted Kremenek05a23782008-02-26 19:05:15 +0000931
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000932//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000933// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000934//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000935
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000936void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
937 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000938
Ted Kremeneka8538d92009-02-13 01:45:31 +0000939 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000940
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000941 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000942
943 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
944
Ted Kremeneka8538d92009-02-13 01:45:31 +0000945 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000946
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000947 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000948 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000949 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000950 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000951 return;
952
953 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
954 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
955
956 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000957 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000958 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000959 return;
960
961 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000962 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000963 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000964 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000965 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000966 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000967
968 assert (false &&
969 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000970}
971
Ted Kremenek540cbe22008-04-22 04:56:29 +0000972/// VisitArraySubscriptExpr - Transfer function for array accesses
973void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000974 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000975
976 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000977 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000979
980 if (Base->getType()->isVectorType()) {
981 // For vector types get its lvalue.
982 // FIXME: This may not be correct. Is the rvalue of a vector its location?
983 // In fact, I think this is just a hack. We need to get the right
984 // semantics.
985 VisitLValue(Base, Pred, Tmp);
986 }
987 else
988 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000989
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000990 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000991 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000992 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000993
994 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000995 const GRState* state = GetState(*I2);
996 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
997 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000998
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000999 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001000 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001001 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001002 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001003 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001004 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001005}
1006
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001007/// VisitMemberExpr - Transfer function for member expressions.
1008void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001009 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001010
1011 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001012 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001013
1014 if (M->isArrow())
1015 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1016 else
1017 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1018
Douglas Gregor86f19402008-12-20 23:49:58 +00001019 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1020 if (!Field) // FIXME: skipping member expressions for non-fields
1021 return;
1022
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001023 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001024 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001025 // FIXME: Should we insert some assumption logic in here to determine
1026 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001027 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001028 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001029
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001030 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001031 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001032 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001033 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001034 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001035}
1036
Ted Kremeneka8538d92009-02-13 01:45:31 +00001037/// EvalBind - Handle the semantics of binding a value to a specific location.
1038/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1039void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1040 const GRState* state, SVal location, SVal Val) {
1041
Ted Kremenek41573eb2009-02-14 01:43:44 +00001042 const GRState* newState = 0;
1043
1044 if (location.isUnknown()) {
1045 // We know that the new state will be the same as the old state since
1046 // the location of the binding is "unknown". Consequently, there
1047 // is no reason to just create a new node.
1048 newState = state;
1049 }
1050 else {
1051 // We are binding to a value other than 'unknown'. Perform the binding
1052 // using the StoreManager.
1053 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1054 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001055
Ted Kremenek41573eb2009-02-14 01:43:44 +00001056 // The next thing to do is check if the GRTransferFuncs object wants to
1057 // update the state based on the new binding. If the GRTransferFunc object
1058 // doesn't do anything, just auto-propagate the current state.
1059 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1060 newState != state);
1061
1062 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001063}
1064
1065/// EvalStore - Handle the semantics of a store via an assignment.
1066/// @param Dst The node set to store generated state nodes
1067/// @param Ex The expression representing the location of the store
1068/// @param state The current simulation state
1069/// @param location The location to store the value
1070/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001071void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001072 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001073
1074 assert (Builder && "GRStmtNodeBuilder must be defined.");
1075
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001076 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001077 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001078
Ted Kremenek8c354752008-12-16 22:02:27 +00001079 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001080 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001081
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001082 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001083 state = GetState(Pred);
1084
1085 // Proceed with the store.
1086 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1087 Builder->PointKind = ProgramPoint::PostStoreKind;
1088 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001089}
1090
1091void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001092 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001093
Ted Kremenek8c354752008-12-16 22:02:27 +00001094 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001095 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001096
Ted Kremenek8c354752008-12-16 22:02:27 +00001097 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001098 return;
1099
Ted Kremeneka8538d92009-02-13 01:45:31 +00001100 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001101
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001102 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001103 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001104
1105 // FIXME: Currently symbolic analysis "generates" new symbols
1106 // for the contents of values. We need a better approach.
1107
Ted Kremenek8c354752008-12-16 22:02:27 +00001108 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001109 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001110 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001111 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001112 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001113 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
1114 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001115 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116}
1117
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001118void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001119 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001120
1121 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001122 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001123
1124 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1125 MakeNode(Dst, Ex, *I, (*I)->getState());
1126}
1127
Ted Kremenek8c354752008-12-16 22:02:27 +00001128GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001129 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001130 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001131
1132 // Check for loads/stores from/to undefined values.
1133 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001134 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001135 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001136 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001137
Ted Kremenek8c354752008-12-16 22:02:27 +00001138 if (N) {
1139 N->markAsSink();
1140 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001141 }
1142
Ted Kremenek8c354752008-12-16 22:02:27 +00001143 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001144 }
1145
1146 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1147 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001148 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001149
1150 // During a load, one of two possible situations arise:
1151 // (1) A crash, because the location (pointer) was NULL.
1152 // (2) The location (pointer) is not NULL, and the dereference works.
1153 //
1154 // We add these assumptions.
1155
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001156 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001157
1158 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001159 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001160 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001161
1162 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001163 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001164 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001165 getStateManager());
Zhongxing Xua1718c72009-04-03 07:33:13 +00001166
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001167 if (isFeasibleNull) {
1168
Ted Kremenek7360fda2008-09-18 23:09:54 +00001169 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001170 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001171 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1172
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001173 // We don't use "MakeNode" here because the node will be a sink
1174 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001175 NodeTy* NullNode =
1176 Builder->generateNode(Ex, StNull, Pred,
1177 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001178
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001179 if (NullNode) {
1180
1181 NullNode->markAsSink();
1182
1183 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1184 else ExplicitNullDeref.insert(NullNode);
1185 }
1186 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001187
1188 if (!isFeasibleNotNull)
1189 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001190
1191 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001192 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001193 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1194 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1195 // Get the index of the accessed element.
1196 SVal Idx = ER->getIndex();
1197 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001198 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1199 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001200
1201 bool isFeasibleInBound = false;
1202 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1203 true, isFeasibleInBound);
1204
1205 bool isFeasibleOutBound = false;
1206 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1207 false, isFeasibleOutBound);
1208
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001209 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001210 // Report warning. Make sink node manually.
1211 NodeTy* OOBNode =
1212 Builder->generateNode(Ex, StOutBound, Pred,
1213 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001214
1215 if (OOBNode) {
1216 OOBNode->markAsSink();
1217
1218 if (isFeasibleInBound)
1219 ImplicitOOBMemAccesses.insert(OOBNode);
1220 else
1221 ExplicitOOBMemAccesses.insert(OOBNode);
1222 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001223 }
1224
Ted Kremenek8c354752008-12-16 22:02:27 +00001225 if (!isFeasibleInBound)
1226 return 0;
1227
1228 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001229 }
1230 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001231
Ted Kremenek8c354752008-12-16 22:02:27 +00001232 // Generate a new node indicating the checks succeed.
1233 return Builder->generateNode(Ex, StNotNull, Pred,
1234 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001235}
1236
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001237//===----------------------------------------------------------------------===//
1238// Transfer function: Function calls.
1239//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001240void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001241 CallExpr::arg_iterator AI,
1242 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001243 NodeSet& Dst)
1244{
1245 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001246 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001247 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1248 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001249 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001250
1251 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1252}
1253
1254void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1255 CallExpr::arg_iterator AI,
1256 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001258 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001259
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001260 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001261 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001262 // If the call argument is being bound to a reference parameter,
1263 // visit it as an lvalue, not an rvalue.
1264 bool VisitAsLvalue = false;
1265 if (Proto && ParamIdx < Proto->getNumArgs())
1266 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1267
1268 NodeSet DstTmp;
1269 if (VisitAsLvalue)
1270 VisitLValue(*AI, Pred, DstTmp);
1271 else
1272 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001273 ++AI;
1274
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001275 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001276 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001277
1278 return;
1279 }
1280
1281 // If we reach here we have processed all of the arguments. Evaluate
1282 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001283
Ted Kremenek994a09b2008-02-25 21:16:03 +00001284 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001285 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001286
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001287 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001288
Ted Kremenekde434242008-02-19 01:44:53 +00001289 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001290 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1291
Ted Kremeneka8538d92009-02-13 01:45:31 +00001292 const GRState* state = GetState(*DI);
1293 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001294
Ted Kremeneka1354a52008-03-03 16:47:31 +00001295 // FIXME: Add support for symbolic function calls (calls involving
1296 // function pointer values that are symbolic).
1297
1298 // Check for undefined control-flow or calls to NULL.
1299
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001300 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001301 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001302
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001303 if (N) {
1304 N->markAsSink();
1305 BadCalls.insert(N);
1306 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001307
Ted Kremenekde434242008-02-19 01:44:53 +00001308 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001309 }
1310
1311 // Check for the "noreturn" attribute.
1312
1313 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1314
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001315 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001316
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001317 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001318
Ted Kremenekb7252322009-04-10 00:01:14 +00001319 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001320 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001321 else {
1322 // HACK: Some functions are not marked noreturn, and don't return.
1323 // Here are a few hardwired ones. If this takes too long, we can
1324 // potentially cache these results.
1325 const char* s = FD->getIdentifier()->getName();
1326 unsigned n = strlen(s);
1327
1328 switch (n) {
1329 default:
1330 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001331
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001332 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001333 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1334 break;
1335
1336 case 5:
1337 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001338 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001339 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001340 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001341 // FIXME: use Assume to inspect the possible symbolic value of
1342 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001343 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001344 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001345 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001346 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001347 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001348 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001349
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001350 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001351 if (!memcmp(s, "Assert", 6)) {
1352 Builder->BuildSinks = true;
1353 break;
1354 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001355
1356 // FIXME: This is just a wrapper around throwing an exception.
1357 // Eventually inter-procedural analysis should handle this easily.
1358 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1359
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001360 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001361
1362 case 7:
1363 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1364 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001365
Ted Kremenekf47bb782008-04-30 17:54:04 +00001366 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001367 if (!memcmp(s ,"db_error", 8) ||
1368 !memcmp(s, "__assert", 8))
1369 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001370 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001371
1372 case 12:
1373 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1374 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001375
Ted Kremenekf9683082008-09-19 02:30:47 +00001376 case 13:
1377 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1378 break;
1379
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001380 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001381 if (!memcmp(s, "dtrace_assfail", 14) ||
1382 !memcmp(s, "yy_fatal_error", 14))
1383 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001384 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001385
1386 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001387 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001388 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1389 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001390 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001391
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001392 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001393 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001394
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001395 }
1396 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001397
1398 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001399
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001400 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001401
Douglas Gregor3c385e52009-02-14 18:57:46 +00001402 if (unsigned id
1403 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001404 switch (id) {
1405 case Builtin::BI__builtin_expect: {
1406 // For __builtin_expect, just return the value of the subexpression.
1407 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001408 SVal X = GetSVal(state, *(CE->arg_begin()));
1409 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001410 continue;
1411 }
1412
Ted Kremenekb3021332008-11-02 00:35:01 +00001413 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001414 // FIXME: Refactor into StoreManager itself?
1415 MemRegionManager& RM = getStateManager().getRegionManager();
1416 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001417 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001418
1419 // Set the extent of the region in bytes. This enables us to use the
1420 // SVal of the argument directly. If we save the extent in bits, we
1421 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001422 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1423 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001424
Ted Kremeneka8538d92009-02-13 01:45:31 +00001425 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001426 continue;
1427 }
1428
Ted Kremenek55aea312008-03-05 22:59:42 +00001429 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001430 break;
1431 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001432 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001433
Ted Kremenek186350f2008-04-23 20:12:28 +00001434 // Check any arguments passed-by-value against being undefined.
1435
1436 bool badArg = false;
1437
1438 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1439 I != E; ++I) {
1440
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001441 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001442 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001443
Ted Kremenek186350f2008-04-23 20:12:28 +00001444 if (N) {
1445 N->markAsSink();
1446 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001447 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001448
Ted Kremenek186350f2008-04-23 20:12:28 +00001449 badArg = true;
1450 break;
1451 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001452 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001453
1454 if (badArg)
1455 continue;
1456
1457 // Dispatch to the plug-in transfer function.
1458
1459 unsigned size = Dst.size();
1460 SaveOr OldHasGen(Builder->HasGeneratedNode);
1461 EvalCall(Dst, CE, L, *DI);
1462
1463 // Handle the case where no nodes where generated. Auto-generate that
1464 // contains the updated state if we aren't generating sinks.
1465
1466 if (!Builder->BuildSinks && Dst.size() == size &&
1467 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001468 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001469 }
1470}
1471
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001472//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001473// Transfer function: Objective-C ivar references.
1474//===----------------------------------------------------------------------===//
1475
Ted Kremenekf5cae632009-02-28 20:50:43 +00001476static std::pair<const void*,const void*> EagerlyAssumeTag
1477 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1478
Ted Kremenekb2939022009-02-25 23:32:10 +00001479void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001480 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1481 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001482
1483 // Test if the previous node was as the same expression. This can happen
1484 // when the expression fails to evaluate to anything meaningful and
1485 // (as an optimization) we don't generate a node.
1486 ProgramPoint P = Pred->getLocation();
1487 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1488 Dst.Add(Pred);
1489 continue;
1490 }
1491
Ted Kremenek48af2a92009-02-25 22:32:02 +00001492 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001493 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001494 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001495 // First assume that the condition is true.
1496 bool isFeasible = false;
1497 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1498 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001499 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1500 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001501 stateTrue, Pred));
1502 }
1503
1504 // Next, assume that the condition is false.
1505 isFeasible = false;
1506 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1507 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001508 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1509 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001510 stateFalse, Pred));
1511 }
1512 }
1513 else
1514 Dst.Add(Pred);
1515 }
1516}
1517
1518//===----------------------------------------------------------------------===//
1519// Transfer function: Objective-C ivar references.
1520//===----------------------------------------------------------------------===//
1521
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001522void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1523 NodeTy* Pred, NodeSet& Dst,
1524 bool asLValue) {
1525
1526 Expr* Base = cast<Expr>(Ex->getBase());
1527 NodeSet Tmp;
1528 Visit(Base, Pred, Tmp);
1529
1530 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001531 const GRState* state = GetState(*I);
1532 SVal BaseVal = GetSVal(state, Base);
1533 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001534
1535 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001536 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001537 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001538 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001539 }
1540}
1541
1542//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001543// Transfer function: Objective-C fast enumeration 'for' statements.
1544//===----------------------------------------------------------------------===//
1545
1546void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1547 NodeTy* Pred, NodeSet& Dst) {
1548
1549 // ObjCForCollectionStmts are processed in two places. This method
1550 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1551 // statements within a basic block. This transfer function does two things:
1552 //
1553 // (1) binds the next container value to 'element'. This creates a new
1554 // node in the ExplodedGraph.
1555 //
1556 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1557 // whether or not the container has any more elements. This value
1558 // will be tested in ProcessBranch. We need to explicitly bind
1559 // this value because a container can contain nil elements.
1560 //
1561 // FIXME: Eventually this logic should actually do dispatches to
1562 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1563 // This will require simulating a temporary NSFastEnumerationState, either
1564 // through an SVal or through the use of MemRegions. This value can
1565 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1566 // terminates we reclaim the temporary (it goes out of scope) and we
1567 // we can test if the SVal is 0 or if the MemRegion is null (depending
1568 // on what approach we take).
1569 //
1570 // For now: simulate (1) by assigning either a symbol or nil if the
1571 // container is empty. Thus this transfer function will by default
1572 // result in state splitting.
1573
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001574 Stmt* elem = S->getElement();
1575 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001576
1577 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001578 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001579 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001580 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1581 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1582 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001583 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001584
1585 NodeSet Tmp;
1586 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001587
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001588 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1589 const GRState* state = GetState(*I);
1590 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1591 }
1592}
1593
1594void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1595 NodeTy* Pred, NodeSet& Dst,
1596 SVal ElementV) {
1597
1598
Ted Kremenekaf337412008-11-12 19:24:17 +00001599
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001600 // Get the current state. Use 'EvalLocation' to determine if it is a null
1601 // pointer, etc.
1602 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001603
Ted Kremenek8c354752008-12-16 22:02:27 +00001604 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1605 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001606 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001607
1608 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001609
Ted Kremenekaf337412008-11-12 19:24:17 +00001610 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001611 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001612 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1613 GRStateRef hasElems = state.BindExpr(S, TrueV);
1614
Ted Kremenekaf337412008-11-12 19:24:17 +00001615 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001616 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1617 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001618
1619 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1620 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1621 // FIXME: The proper thing to do is to really iterate over the
1622 // container. We will do this with dispatch logic to the store.
1623 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001624 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001625 assert (Loc::IsLocType(T));
1626 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001627 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1628 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1629 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001630
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001631 // Bind the location to 'nil' on the false branch.
1632 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1633 noElems = noElems.BindLoc(ElementV, nilV);
1634 }
1635
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001636 // Create the new nodes.
1637 MakeNode(Dst, S, Pred, hasElems);
1638 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001639}
1640
1641//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001642// Transfer function: Objective-C message expressions.
1643//===----------------------------------------------------------------------===//
1644
1645void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1646 NodeSet& Dst){
1647
1648 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1649 Pred, Dst);
1650}
1651
1652void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001653 ObjCMessageExpr::arg_iterator AI,
1654 ObjCMessageExpr::arg_iterator AE,
1655 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001656 if (AI == AE) {
1657
1658 // Process the receiver.
1659
1660 if (Expr* Receiver = ME->getReceiver()) {
1661 NodeSet Tmp;
1662 Visit(Receiver, Pred, Tmp);
1663
1664 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1665 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1666
1667 return;
1668 }
1669
1670 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1671 return;
1672 }
1673
1674 NodeSet Tmp;
1675 Visit(*AI, Pred, Tmp);
1676
1677 ++AI;
1678
1679 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1680 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1681}
1682
1683void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1684 NodeTy* Pred,
1685 NodeSet& Dst) {
1686
1687 // FIXME: More logic for the processing the method call.
1688
Ted Kremeneka8538d92009-02-13 01:45:31 +00001689 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001690 bool RaisesException = false;
1691
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001692
1693 if (Expr* Receiver = ME->getReceiver()) {
1694
Ted Kremeneka8538d92009-02-13 01:45:31 +00001695 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001696
Ted Kremenek21fe8372009-02-19 04:06:22 +00001697 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001698 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001699 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001700
1701 if (N) {
1702 N->markAsSink();
1703 UndefReceivers.insert(N);
1704 }
1705
1706 return;
1707 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001708
Ted Kremenek21fe8372009-02-19 04:06:22 +00001709 // "Assume" that the receiver is not NULL.
1710 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001711 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001712
1713 // "Assume" that the receiver is NULL.
1714 bool isFeasibleNull = false;
1715 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1716
1717 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001718 QualType RetTy = ME->getType();
1719
Ted Kremenek21fe8372009-02-19 04:06:22 +00001720 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001721 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001722 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001723 // The [0 ...] expressions will return garbage. Flag either an
1724 // explicit or implicit error. Because of the structure of this
1725 // function we currently do not bifurfacte the state graph at
1726 // this point.
1727 // FIXME: We should bifurcate and fill the returned struct with
1728 // garbage.
1729 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1730 N->markAsSink();
1731 if (isFeasibleNotNull)
1732 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001733 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001734 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001735 }
1736 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001737 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001738 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001739 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001740 if (RetTy != Ctx.VoidTy) {
1741 if (BR.getParentMap().isConsumedExpr(ME)) {
1742 // sizeof(void *)
1743 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1744 // sizeof(return type)
1745 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001746
Ted Kremenekfe630b92009-04-09 05:45:56 +00001747 if(voidPtrSize < returnTypeSize) {
1748 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1749 N->markAsSink();
1750 if(isFeasibleNotNull)
1751 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001752 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001753 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001754 }
1755 }
1756 else if (!isFeasibleNotNull) {
1757 // Handle the safe cases where the return value is 0 if the
1758 // receiver is nil.
1759 //
1760 // FIXME: For now take the conservative approach that we only
1761 // return null values if we *know* that the receiver is nil.
1762 // This is because we can have surprises like:
1763 //
1764 // ... = [[NSScreens screens] objectAtIndex:0];
1765 //
1766 // What can happen is that [... screens] could return nil, but
1767 // it most likely isn't nil. We should assume the semantics
1768 // of this case unless we have *a lot* more knowledge.
1769 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001770 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001771 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001772 return;
1773 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001774 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001775 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001776 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001777 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001778 // of this method should assume that the receiver is not nil.
1779 if (!StNotNull)
1780 return;
1781
Ted Kremenekda9ae602009-04-08 18:51:08 +00001782 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001783 }
1784
Ted Kremeneke448ab42008-05-01 18:33:28 +00001785 // Check if the "raise" message was sent.
1786 if (ME->getSelector() == RaiseSel)
1787 RaisesException = true;
1788 }
1789 else {
1790
1791 IdentifierInfo* ClsName = ME->getClassName();
1792 Selector S = ME->getSelector();
1793
1794 // Check for special instance methods.
1795
1796 if (!NSExceptionII) {
1797 ASTContext& Ctx = getContext();
1798
1799 NSExceptionII = &Ctx.Idents.get("NSException");
1800 }
1801
1802 if (ClsName == NSExceptionII) {
1803
1804 enum { NUM_RAISE_SELECTORS = 2 };
1805
1806 // Lazily create a cache of the selectors.
1807
1808 if (!NSExceptionInstanceRaiseSelectors) {
1809
1810 ASTContext& Ctx = getContext();
1811
1812 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1813
1814 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1815 unsigned idx = 0;
1816
1817 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001818 II.push_back(&Ctx.Idents.get("raise"));
1819 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001820 NSExceptionInstanceRaiseSelectors[idx++] =
1821 Ctx.Selectors.getSelector(II.size(), &II[0]);
1822
1823 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001824 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001825 NSExceptionInstanceRaiseSelectors[idx++] =
1826 Ctx.Selectors.getSelector(II.size(), &II[0]);
1827 }
1828
1829 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1830 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1831 RaisesException = true; break;
1832 }
1833 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001834 }
1835
1836 // Check for any arguments that are uninitialized/undefined.
1837
1838 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1839 I != E; ++I) {
1840
Ted Kremeneka8538d92009-02-13 01:45:31 +00001841 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001842
1843 // Generate an error node for passing an uninitialized/undefined value
1844 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001845 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001846
1847 if (N) {
1848 N->markAsSink();
1849 MsgExprUndefArgs[N] = *I;
1850 }
1851
1852 return;
1853 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001854 }
1855
1856 // Check if we raise an exception. For now treat these as sinks. Eventually
1857 // we will want to handle exceptions properly.
1858
1859 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1860
1861 if (RaisesException)
1862 Builder->BuildSinks = true;
1863
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001864 // Dispatch to plug-in transfer function.
1865
1866 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001867 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001868
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001869 EvalObjCMessageExpr(Dst, ME, Pred);
1870
1871 // Handle the case where no nodes where generated. Auto-generate that
1872 // contains the updated state if we aren't generating sinks.
1873
Ted Kremenekb0533962008-04-18 20:35:30 +00001874 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001875 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001876}
1877
1878//===----------------------------------------------------------------------===//
1879// Transfer functions: Miscellaneous statements.
1880//===----------------------------------------------------------------------===//
1881
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001882void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1883 QualType PtrTy,
1884 Expr* CastE, NodeTy* Pred,
1885 NodeSet& Dst) {
1886 if (!V.isUnknownOrUndef()) {
1887 // FIXME: Determine if the number of bits of the target type is
1888 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00001889 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00001890 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
1891 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001892 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001893 else
1894 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001895}
1896
1897
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001898void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001899 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001900 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001901 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001902
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001903 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001904 T = ExCast->getTypeAsWritten();
1905
Zhongxing Xued340f72008-10-22 08:02:16 +00001906 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001907 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001908 else
1909 Visit(Ex, Pred, S1);
1910
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001911 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00001912 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001913 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001914 Dst.Add(*I1);
1915
Ted Kremenek874d63f2008-01-24 02:02:54 +00001916 return;
1917 }
1918
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001919 // FIXME: The rest of this should probably just go into EvalCall, and
1920 // let the transfer function object be responsible for constructing
1921 // nodes.
1922
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001923 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001924 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001925 const GRState* state = GetState(N);
1926 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00001927 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001928
1929 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001930 if (V.isUnknown()) {
1931 Dst.Add(N);
1932 continue;
1933 }
1934
1935 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00001936 if (V.isUndef())
1937 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001938
1939 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001940 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00001941 C.getCanonicalType(ExTy).getUnqualifiedType())
1942 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001943
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001944 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001945 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001946 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001947 continue;
1948 }
1949
1950 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00001951 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001952 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001953 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001954 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001955 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00001956 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001957 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001958
Ted Kremenekc5302912009-03-05 20:22:13 +00001959 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00001960 }
1961
1962 // Just pass through function and block pointers.
1963 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
1964 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00001965 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001966 }
1967
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001968 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001969 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001970 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00001971 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001972
1973 // Are we casting from an array to a pointer? If so just pass on
1974 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00001975 if (T->isPointerType())
1976 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001977
1978 // Are we casting from an array to an integer? If so, cast the decayed
1979 // pointer value to an integer.
1980 assert(T->isIntegerType());
1981 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1982 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001983 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001984 continue;
1985 }
1986
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001987 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001988 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
1989 // FIXME: For TypedViewRegions, we should handle the case where the
1990 // underlying symbolic pointer is a function pointer or
1991 // block pointer.
1992
1993 // FIXME: We should handle the case where we strip off view layers to get
1994 // to a desugared type.
1995
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001996 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00001997 // We get a symbolic function pointer for a dereference of a function
1998 // pointer, but it is of function type. Example:
1999
2000 // struct FPRec {
2001 // void (*my_func)(int * x);
2002 // };
2003 //
2004 // int bar(int x);
2005 //
2006 // int f1_a(struct FPRec* foo) {
2007 // int x;
2008 // (*foo->my_func)(&x);
2009 // return bar(x)+1; // no-warning
2010 // }
2011
2012 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002013
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002014 const MemRegion* R = RV->getRegion();
2015 StoreManager& StoreMgr = getStoreManager();
2016
2017 // Delegate to store manager to get the result of casting a region
2018 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002019 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002020
2021 // Inspect the result. If the MemRegion* returned is NULL, this
2022 // expression evaluates to UnknownVal.
2023 R = Res.getRegion();
2024 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2025
2026 // Generate the new node in the ExplodedGraph.
2027 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002028 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002029 }
2030
Ted Kremenekdc402902009-03-04 00:14:35 +00002031 // If we are casting a symbolic value, make a symbolic region and a
2032 // TypedViewRegion subregion.
2033 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) {
2034 SymbolRef Sym = SV->getSymbol();
Ted Kremenekc5302912009-03-05 20:22:13 +00002035 QualType SymTy = getSymbolManager().getType(Sym);
2036
2037 // Just pass through symbols that are function or block pointers.
2038 if (SymTy->isFunctionPointerType() || SymTy->isBlockPointerType())
2039 goto PassThrough;
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002040
2041 // Are we casting to a function or block pointer?
2042 if (T->isFunctionPointerType() || T->isBlockPointerType()) {
2043 // FIXME: We should verify that the underlying type of the symbolic
2044 // pointer is a void* (or maybe char*). Other things are an abuse
2045 // of the type system.
2046 goto PassThrough;
2047 }
Ted Kremenekc5302912009-03-05 20:22:13 +00002048
Ted Kremenekdc402902009-03-04 00:14:35 +00002049 StoreManager& StoreMgr = getStoreManager();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002050 const MemRegion* R = StoreMgr.getRegionManager().getSymbolicRegion(Sym);
Ted Kremenekdc402902009-03-04 00:14:35 +00002051
2052 // Delegate to store manager to get the result of casting a region
2053 // to a different type.
2054 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
2055
2056 // Inspect the result. If the MemRegion* returned is NULL, this
2057 // expression evaluates to UnknownVal.
2058 R = Res.getRegion();
2059 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2060
2061 // Generate the new node in the ExplodedGraph.
2062 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
2063 continue;
2064 }
2065
Ted Kremenekc5302912009-03-05 20:22:13 +00002066 // All other cases.
2067 DispatchCast: {
2068 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2069 EvalCast(V, CastE->getType())));
2070 continue;
2071 }
2072
2073 PassThrough: {
2074 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2075 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002076 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002077}
2078
Ted Kremenek4f090272008-10-27 21:54:31 +00002079void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002080 NodeTy* Pred, NodeSet& Dst,
2081 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002082 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2083 NodeSet Tmp;
2084 Visit(ILE, Pred, Tmp);
2085
2086 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002087 const GRState* state = GetState(*I);
2088 SVal ILV = GetSVal(state, ILE);
2089 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002090
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002091 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002092 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002093 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002094 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002095 }
2096}
2097
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002098void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002099
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002100 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002101 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002102
2103 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002104 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002105
Ted Kremenekefd59942008-12-08 22:47:34 +00002106 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002107 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002108
2109 // FIXME: static variables may have an initializer, but the second
2110 // time a function is called those values may not be current.
2111 NodeSet Tmp;
2112
Ted Kremenekaf337412008-11-12 19:24:17 +00002113 if (InitEx)
2114 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002115
2116 if (Tmp.empty())
2117 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002118
2119 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002120 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002121 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002122
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002123 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2124 QualType T = getContext().getCanonicalType(VD->getType());
2125 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2126 // FIXME: Handle multi-dimensional VLAs.
2127
2128 Expr* SE = VLA->getSizeExpr();
2129 SVal Size = GetSVal(state, SE);
2130
2131 if (Size.isUndef()) {
2132 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2133 N->markAsSink();
2134 ExplicitBadSizedVLA.insert(N);
2135 }
2136 continue;
2137 }
2138
2139 bool isFeasibleZero = false;
2140 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2141
2142 bool isFeasibleNotZero = false;
2143 state = Assume(state, Size, true, isFeasibleNotZero);
2144
2145 if (isFeasibleZero) {
2146 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2147 N->markAsSink();
2148 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2149 else ExplicitBadSizedVLA.insert(N);
2150 }
2151 }
2152
2153 if (!isFeasibleNotZero)
2154 continue;
2155 }
2156
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002157 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002158 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002159 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002160 QualType T = VD->getType();
2161
2162 // Recover some path-sensitivity if a scalar value evaluated to
2163 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002164 if (InitVal.isUnknown() ||
2165 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002166 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002167 }
2168
Ted Kremeneka8538d92009-02-13 01:45:31 +00002169 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002170
2171 // The next thing to do is check if the GRTransferFuncs object wants to
2172 // update the state based on the new binding. If the GRTransferFunc
2173 // object doesn't do anything, just auto-propagate the current state.
2174 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2175 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2176 InitVal);
2177 }
2178 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002179 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002180 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002181 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002182 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002183}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002184
Ted Kremenekf75b1862008-10-30 17:47:32 +00002185namespace {
2186 // This class is used by VisitInitListExpr as an item in a worklist
2187 // for processing the values contained in an InitListExpr.
2188class VISIBILITY_HIDDEN InitListWLItem {
2189public:
2190 llvm::ImmutableList<SVal> Vals;
2191 GRExprEngine::NodeTy* N;
2192 InitListExpr::reverse_iterator Itr;
2193
2194 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2195 InitListExpr::reverse_iterator itr)
2196 : Vals(vals), N(n), Itr(itr) {}
2197};
2198}
2199
2200
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002201void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2202 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002203
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002204 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002205 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002206 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002207
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002208 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002209
Ted Kremeneka49e3672008-10-30 23:14:36 +00002210 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002211
Ted Kremeneka49e3672008-10-30 23:14:36 +00002212 // Handle base case where the initializer has no elements.
2213 // e.g: static int* myArray[] = {};
2214 if (NumInitElements == 0) {
2215 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2216 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2217 return;
2218 }
2219
2220 // Create a worklist to process the initializers.
2221 llvm::SmallVector<InitListWLItem, 10> WorkList;
2222 WorkList.reserve(NumInitElements);
2223 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002224 InitListExpr::reverse_iterator ItrEnd = E->rend();
2225
Ted Kremeneka49e3672008-10-30 23:14:36 +00002226 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002227 while (!WorkList.empty()) {
2228 InitListWLItem X = WorkList.back();
2229 WorkList.pop_back();
2230
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002231 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002232 Visit(*X.Itr, X.N, Tmp);
2233
2234 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002235
Ted Kremenekf75b1862008-10-30 17:47:32 +00002236 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2237 // Get the last initializer value.
2238 state = GetState(*NI);
2239 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2240
2241 // Construct the new list of values by prepending the new value to
2242 // the already constructed list.
2243 llvm::ImmutableList<SVal> NewVals =
2244 getBasicVals().consVals(InitV, X.Vals);
2245
2246 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002247 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002248 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002249
Ted Kremenekf75b1862008-10-30 17:47:32 +00002250 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002251 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002252 }
2253 else {
2254 // Still some initializer values to go. Push them onto the worklist.
2255 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2256 }
2257 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002258 }
Ted Kremenek87903072008-10-30 18:34:31 +00002259
2260 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002261 }
2262
Ted Kremenek062e2f92008-11-13 06:10:40 +00002263 if (T->isUnionType() || T->isVectorType()) {
2264 // FIXME: to be implemented.
2265 // Note: That vectors can return true for T->isIntegerType()
2266 MakeNode(Dst, E, Pred, state);
2267 return;
2268 }
2269
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002270 if (Loc::IsLocType(T) || T->isIntegerType()) {
2271 assert (E->getNumInits() == 1);
2272 NodeSet Tmp;
2273 Expr* Init = E->getInit(0);
2274 Visit(Init, Pred, Tmp);
2275 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2276 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002277 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002278 }
2279 return;
2280 }
2281
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002282
2283 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2284 assert(0 && "unprocessed InitListExpr type");
2285}
Ted Kremenekf233d482008-02-05 00:26:40 +00002286
Sebastian Redl05189992008-11-11 17:56:53 +00002287/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2288void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2289 NodeTy* Pred,
2290 NodeSet& Dst) {
2291 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002292 uint64_t amt;
2293
2294 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002295 if (T == getContext().VoidTy) {
2296 // sizeof(void) == 1 byte.
2297 amt = 1;
2298 }
2299 else if (!T.getTypePtr()->isConstantSizeType()) {
2300 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002301 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002302 }
2303 else if (T->isObjCInterfaceType()) {
2304 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2305 // the compiler has laid out its representation. Just report Unknown
2306 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002307 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002308 }
2309 else {
2310 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002311 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002312 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002313 }
2314 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002315 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002316
Ted Kremenek0e561a32008-03-21 21:30:14 +00002317 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002318 BindExpr(GetState(Pred), Ex,
2319 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002320}
2321
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002322
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002323void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002324 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002325
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002326 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002327
2328 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002329 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002330
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002331 case UnaryOperator::Deref: {
2332
2333 Expr* Ex = U->getSubExpr()->IgnoreParens();
2334 NodeSet Tmp;
2335 Visit(Ex, Pred, Tmp);
2336
2337 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002338
Ted Kremeneka8538d92009-02-13 01:45:31 +00002339 const GRState* state = GetState(*I);
2340 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002341
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002342 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002343 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002344 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002345 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002346 }
2347
2348 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002349 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002350
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002351 case UnaryOperator::Real: {
2352
2353 Expr* Ex = U->getSubExpr()->IgnoreParens();
2354 NodeSet Tmp;
2355 Visit(Ex, Pred, Tmp);
2356
2357 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2358
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002359 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002360 if (Ex->getType()->isAnyComplexType()) {
2361 // Just report "Unknown."
2362 Dst.Add(*I);
2363 continue;
2364 }
2365
2366 // For all other types, UnaryOperator::Real is an identity operation.
2367 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002368 const GRState* state = GetState(*I);
2369 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002370 }
2371
2372 return;
2373 }
2374
2375 case UnaryOperator::Imag: {
2376
2377 Expr* Ex = U->getSubExpr()->IgnoreParens();
2378 NodeSet Tmp;
2379 Visit(Ex, Pred, Tmp);
2380
2381 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002382 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002383 if (Ex->getType()->isAnyComplexType()) {
2384 // Just report "Unknown."
2385 Dst.Add(*I);
2386 continue;
2387 }
2388
2389 // For all other types, UnaryOperator::Float returns 0.
2390 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002391 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002392 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002393 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002394 }
2395
2396 return;
2397 }
2398
2399 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002400 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002401 Dst.Add(Pred);
2402 return;
2403
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002404 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002405 case UnaryOperator::Extension: {
2406
2407 // Unary "+" is a no-op, similar to a parentheses. We still have places
2408 // where it may be a block-level expression, so we need to
2409 // generate an extra node that just propagates the value of the
2410 // subexpression.
2411
2412 Expr* Ex = U->getSubExpr()->IgnoreParens();
2413 NodeSet Tmp;
2414 Visit(Ex, Pred, Tmp);
2415
2416 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002417 const GRState* state = GetState(*I);
2418 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002419 }
2420
2421 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002422 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002423
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002424 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002425
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002426 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002427 Expr* Ex = U->getSubExpr()->IgnoreParens();
2428 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002429 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002430
2431 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002432 const GRState* state = GetState(*I);
2433 SVal V = GetSVal(state, Ex);
2434 state = BindExpr(state, U, V);
2435 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002436 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002437
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002438 return;
2439 }
2440
2441 case UnaryOperator::LNot:
2442 case UnaryOperator::Minus:
2443 case UnaryOperator::Not: {
2444
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002445 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002446 Expr* Ex = U->getSubExpr()->IgnoreParens();
2447 NodeSet Tmp;
2448 Visit(Ex, Pred, Tmp);
2449
2450 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002451 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002452
2453 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002454 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002455
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002456 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002457 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002458 continue;
2459 }
2460
Ted Kremenek60595da2008-11-15 04:01:56 +00002461// QualType DstT = getContext().getCanonicalType(U->getType());
2462// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2463//
2464// if (DstT != SrcT) // Perform promotions.
2465// V = EvalCast(V, DstT);
2466//
2467// if (V.isUnknownOrUndef()) {
2468// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2469// continue;
2470// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002471
2472 switch (U->getOpcode()) {
2473 default:
2474 assert(false && "Invalid Opcode.");
2475 break;
2476
2477 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002478 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002479 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002480 break;
2481
2482 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002483 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002484 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002485 break;
2486
2487 case UnaryOperator::LNot:
2488
2489 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2490 //
2491 // Note: technically we do "E == 0", but this is the same in the
2492 // transfer functions as "0 == E".
2493
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002494 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002495 Loc X = Loc::MakeNull(getBasicVals());
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002496 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2497 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002498 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002499 }
2500 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002501 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002502#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002503 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002504 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002505#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002506 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2507 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002508 continue;
2509#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002510 }
2511
2512 break;
2513 }
2514
Ted Kremeneka8538d92009-02-13 01:45:31 +00002515 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002516 }
2517
2518 return;
2519 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002520 }
2521
2522 // Handle ++ and -- (both pre- and post-increment).
2523
2524 assert (U->isIncrementDecrementOp());
2525 NodeSet Tmp;
2526 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002527 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002528
2529 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2530
Ted Kremeneka8538d92009-02-13 01:45:31 +00002531 const GRState* state = GetState(*I);
2532 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002533
2534 // Perform a load.
2535 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002536 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002537
2538 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2539
Ted Kremeneka8538d92009-02-13 01:45:31 +00002540 state = GetState(*I2);
2541 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002542
2543 // Propagate unknown and undefined values.
2544 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002545 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002546 continue;
2547 }
2548
Ted Kremenek21028dd2009-03-11 03:54:24 +00002549 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002550 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2551 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002552
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002553 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002554
2555 // Conjure a new symbol if necessary to recover precision.
2556 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result))
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002557 Result = ValMgr.getConjuredSymbolVal(Ex,
2558 Builder->getCurrentBlockCount());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002559
Ted Kremeneka8538d92009-02-13 01:45:31 +00002560 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002561
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002562 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002563 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002564 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002565 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002566}
2567
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002568void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2569 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2570}
2571
2572void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2573 AsmStmt::outputs_iterator I,
2574 AsmStmt::outputs_iterator E,
2575 NodeTy* Pred, NodeSet& Dst) {
2576 if (I == E) {
2577 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2578 return;
2579 }
2580
2581 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002582 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002583
2584 ++I;
2585
2586 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2587 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2588}
2589
2590void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2591 AsmStmt::inputs_iterator I,
2592 AsmStmt::inputs_iterator E,
2593 NodeTy* Pred, NodeSet& Dst) {
2594 if (I == E) {
2595
2596 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002597 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002598
2599 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2600 // which interprets the inline asm and stores proper results in the
2601 // outputs.
2602
Ted Kremeneka8538d92009-02-13 01:45:31 +00002603 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002604
2605 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2606 OE = A->end_outputs(); OI != OE; ++OI) {
2607
Ted Kremeneka8538d92009-02-13 01:45:31 +00002608 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002609 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002610
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002611 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002612 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002613 }
2614
Ted Kremeneka8538d92009-02-13 01:45:31 +00002615 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002616 return;
2617 }
2618
2619 NodeSet Tmp;
2620 Visit(*I, Pred, Tmp);
2621
2622 ++I;
2623
2624 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2625 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2626}
2627
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002628void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2629 assert (Builder && "GRStmtNodeBuilder must be defined.");
2630
2631 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002632
Ted Kremenek186350f2008-04-23 20:12:28 +00002633 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2634 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002635
Ted Kremenek729a9a22008-07-17 23:15:45 +00002636 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002637
Ted Kremenekb0533962008-04-18 20:35:30 +00002638 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002639
Ted Kremenekb0533962008-04-18 20:35:30 +00002640 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002641 MakeNode(Dst, S, Pred, GetState(Pred));
2642}
2643
Ted Kremenek02737ed2008-03-31 15:02:58 +00002644void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2645
2646 Expr* R = S->getRetValue();
2647
2648 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002649 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002650 return;
2651 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002652
Ted Kremenek5917d782008-11-21 00:27:44 +00002653 NodeSet Tmp;
2654 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002655
Ted Kremenek5917d782008-11-21 00:27:44 +00002656 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2657 SVal X = GetSVal((*I)->getState(), R);
2658
2659 // Check if we return the address of a stack variable.
2660 if (isa<loc::MemRegionVal>(X)) {
2661 // Determine if the value is on the stack.
2662 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002663
Ted Kremenek5917d782008-11-21 00:27:44 +00002664 if (R && getStateManager().hasStackStorage(R)) {
2665 // Create a special node representing the error.
2666 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2667 N->markAsSink();
2668 RetsStackAddr.insert(N);
2669 }
2670 continue;
2671 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002672 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002673 // Check if we return an undefined value.
2674 else if (X.isUndef()) {
2675 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2676 N->markAsSink();
2677 RetsUndef.insert(N);
2678 }
2679 continue;
2680 }
2681
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002682 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002683 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002684}
Ted Kremenek55deb972008-03-25 00:34:37 +00002685
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002686//===----------------------------------------------------------------------===//
2687// Transfer functions: Binary operators.
2688//===----------------------------------------------------------------------===//
2689
Ted Kremeneka8538d92009-02-13 01:45:31 +00002690const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002691 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002692
2693 // Divide by undefined? (potentially zero)
2694
2695 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002696 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002697
2698 if (DivUndef) {
2699 DivUndef->markAsSink();
2700 ExplicitBadDivides.insert(DivUndef);
2701 }
2702
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002703 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002704 }
2705
2706 // Check for divide/remainder-by-zero.
2707 // First, "assume" that the denominator is 0 or undefined.
2708
2709 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002710 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002711
2712 // Second, "assume" that the denominator cannot be 0.
2713
2714 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002715 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002716
2717 // Create the node for the divide-by-zero (if it occurred).
2718
2719 if (isFeasibleZero)
2720 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2721 DivZeroNode->markAsSink();
2722
2723 if (isFeasibleNotZero)
2724 ImplicitBadDivides.insert(DivZeroNode);
2725 else
2726 ExplicitBadDivides.insert(DivZeroNode);
2727
2728 }
2729
Ted Kremeneka8538d92009-02-13 01:45:31 +00002730 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002731}
2732
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002733void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002734 GRExprEngine::NodeTy* Pred,
2735 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002736
2737 NodeSet Tmp1;
2738 Expr* LHS = B->getLHS()->IgnoreParens();
2739 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002740
Ted Kremenek759623e2008-12-06 02:39:30 +00002741 // FIXME: Add proper support for ObjCKVCRefExpr.
2742 if (isa<ObjCKVCRefExpr>(LHS)) {
2743 Visit(RHS, Pred, Dst);
2744 return;
2745 }
2746
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002747 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002748 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002749 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002750 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002751
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002752 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002753
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002754 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002755
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002756 // Process the RHS.
2757
2758 NodeSet Tmp2;
2759 Visit(RHS, *I1, Tmp2);
2760
2761 // With both the LHS and RHS evaluated, process the operation itself.
2762
2763 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002764
Ted Kremeneka8538d92009-02-13 01:45:31 +00002765 const GRState* state = GetState(*I2);
2766 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002767
Ted Kremeneka8538d92009-02-13 01:45:31 +00002768 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002769 BinaryOperator::Opcode Op = B->getOpcode();
2770
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002771 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002772
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002773 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002774
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002775 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002776 // FIXME: Handle structs.
2777 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002778
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002779 if ((RightV.isUnknown() ||
2780 !getConstraintManager().canReasonAbout(RightV))
2781 && (Loc::IsLocType(T) ||
2782 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002783 unsigned Count = Builder->getCurrentBlockCount();
2784 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002785 }
2786
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002787 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002788 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002789 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2790 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002791 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002792 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002793
2794 case BinaryOperator::Div:
2795 case BinaryOperator::Rem:
2796
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002797 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002798 if (RHS->getType()->isIntegerType() &&
2799 RHS->getType()->isScalarType()) {
2800
Ted Kremeneka8538d92009-02-13 01:45:31 +00002801 state = CheckDivideZero(B, state, *I2, RightV);
2802 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002803 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002804
2805 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002806
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002807 default: {
2808
2809 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002810 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002811
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002812 // Process non-assignements except commas or short-circuited
2813 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002814
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002815 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002816
2817 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002818 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002819 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002820 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002821 }
2822 else
2823 Dst.Add(*I2);
2824
Ted Kremenek89063af2008-02-21 19:15:37 +00002825 continue;
2826 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002827
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002828 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002829
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002830 // The operands were *not* undefined, but the result is undefined.
2831 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002832
Ted Kremeneka8538d92009-02-13 01:45:31 +00002833 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002834 UndefNode->markAsSink();
2835 UndefResults.insert(UndefNode);
2836 }
2837
2838 continue;
2839 }
2840
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002841 // Otherwise, create a new node.
2842
Ted Kremeneka8538d92009-02-13 01:45:31 +00002843 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002844 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002845 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002846 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002847
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002848 assert (B->isCompoundAssignmentOp());
2849
Ted Kremenekcad29962009-02-07 00:52:24 +00002850 switch (Op) {
2851 default:
2852 assert(0 && "Invalid opcode for compound assignment.");
2853 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2854 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2855 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2856 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2857 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2858 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2859 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2860 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2861 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2862 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002863 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002864
2865 // Perform a load (the LHS). This performs the checks for
2866 // null dereferences, and so on.
2867 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002868 SVal location = GetSVal(state, LHS);
2869 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002870
2871 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2872
Ted Kremeneka8538d92009-02-13 01:45:31 +00002873 state = GetState(*I3);
2874 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002875
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002876 // Check for divide-by-zero.
2877 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002878 && RHS->getType()->isIntegerType()
2879 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002880
2881 // CheckDivideZero returns a new state where the denominator
2882 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002883 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002884
Ted Kremeneka8538d92009-02-13 01:45:31 +00002885 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002886 continue;
2887 }
2888
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002889 // Propagate undefined values (left-side).
2890 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002891 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002892 continue;
2893 }
2894
2895 // Propagate unknown values (left and right-side).
2896 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002897 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2898 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002899 continue;
2900 }
2901
2902 // At this point:
2903 //
2904 // The LHS is not Undef/Unknown.
2905 // The RHS is not Unknown.
2906
2907 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00002908 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002909 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002910
2911 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
2912 CLHSTy = getContext().getCanonicalType(CTy);
2913
Ted Kremenek60595da2008-11-15 04:01:56 +00002914 QualType LTy = getContext().getCanonicalType(LHS->getType());
2915 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00002916
2917 // Promote LHS.
2918 V = EvalCast(V, CLHSTy);
2919
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002920 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002921 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002922 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00002923 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00002924 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002925 continue;
2926 }
2927
Ted Kremenek60595da2008-11-15 04:01:56 +00002928 // Compute the result of the operation.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002929 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002930
2931 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002932 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002933 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002934 UndefNode->markAsSink();
2935 UndefResults.insert(UndefNode);
2936 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002937 continue;
2938 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002939
2940 // EXPERIMENTAL: "Conjured" symbols.
2941 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002942
2943 SVal LHSVal;
2944
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002945 if ((Result.isUnknown() ||
2946 !getConstraintManager().canReasonAbout(Result))
2947 && (Loc::IsLocType(CTy)
2948 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002949
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002950 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002951
Ted Kremenek60595da2008-11-15 04:01:56 +00002952 // The symbolic value is actually for the type of the left-hand side
2953 // expression, not the computation type, as this is the value the
2954 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002955 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002956
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002957 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002958 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002959 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002960 else {
2961 // The left-hand side may bind to a different value then the
2962 // computation type.
2963 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2964 }
2965
Ted Kremeneka8538d92009-02-13 01:45:31 +00002966 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2967 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002968 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002969 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002970 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002971}
Ted Kremenekee985462008-01-16 18:18:48 +00002972
2973//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002974// Transfer-function Helpers.
2975//===----------------------------------------------------------------------===//
2976
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002977void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002978 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002979 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002980 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002981
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002982 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002983 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002984
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002985 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002986 MakeNode(Dst, Ex, Pred, *I);
2987}
2988
Ted Kremeneka8538d92009-02-13 01:45:31 +00002989void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002990 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002991 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002992
Ted Kremeneka8538d92009-02-13 01:45:31 +00002993 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002994 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002995}
2996
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002997SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
2998 QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002999
3000 if (L.isUndef() || R.isUndef())
3001 return UndefinedVal();
3002
3003 if (L.isUnknown() || R.isUnknown())
3004 return UnknownVal();
3005
3006 if (isa<Loc>(L)) {
3007 if (isa<Loc>(R))
3008 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3009 else
3010 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
3011 }
3012
3013 if (isa<Loc>(R)) {
3014 // Support pointer arithmetic where the increment/decrement operand
3015 // is on the left and the pointer on the right.
3016
3017 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3018
3019 // Commute the operands.
3020 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
3021 cast<NonLoc>(L));
3022 }
3023 else
3024 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003025 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003026}
3027
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003028//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003029// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003030//===----------------------------------------------------------------------===//
3031
Ted Kremenekaa66a322008-01-16 21:46:15 +00003032#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003033static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003034static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003035
Ted Kremenekaa66a322008-01-16 21:46:15 +00003036namespace llvm {
3037template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003038struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003039 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003040
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003041 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3042
3043 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003044 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003045 GraphPrintCheckerState->isUndefDeref(N) ||
3046 GraphPrintCheckerState->isUndefStore(N) ||
3047 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003048 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3049 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003050 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003051 GraphPrintCheckerState->isBadCall(N) ||
3052 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003053 return "color=\"red\",style=\"filled\"";
3054
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003055 if (GraphPrintCheckerState->isNoReturnCall(N))
3056 return "color=\"blue\",style=\"filled\"";
3057
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003058 return "";
3059 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003060
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003061 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003062 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003063
3064 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003065 ProgramPoint Loc = N->getLocation();
3066
3067 switch (Loc.getKind()) {
3068 case ProgramPoint::BlockEntranceKind:
3069 Out << "Block Entrance: B"
3070 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3071 break;
3072
3073 case ProgramPoint::BlockExitKind:
3074 assert (false);
3075 break;
3076
Ted Kremenekaa66a322008-01-16 21:46:15 +00003077 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003078 if (isa<PostStmt>(Loc)) {
3079 const PostStmt& L = cast<PostStmt>(Loc);
3080 Stmt* S = L.getStmt();
3081 SourceLocation SLoc = S->getLocStart();
3082
3083 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3084 llvm::raw_os_ostream OutS(Out);
3085 S->printPretty(OutS);
3086 OutS.flush();
3087
3088 if (SLoc.isFileID()) {
3089 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003090 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3091 << " col="
3092 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3093 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003094 }
3095
3096 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3097 Out << "\\|Implicit-Null Dereference.\\l";
3098 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3099 Out << "\\|Explicit-Null Dereference.\\l";
3100 else if (GraphPrintCheckerState->isUndefDeref(N))
3101 Out << "\\|Dereference of undefialied value.\\l";
3102 else if (GraphPrintCheckerState->isUndefStore(N))
3103 Out << "\\|Store to Undefined Loc.";
3104 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3105 Out << "\\|Explicit divide-by zero or undefined value.";
3106 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3107 Out << "\\|Implicit divide-by zero or undefined value.";
3108 else if (GraphPrintCheckerState->isUndefResult(N))
3109 Out << "\\|Result of operation is undefined.";
3110 else if (GraphPrintCheckerState->isNoReturnCall(N))
3111 Out << "\\|Call to function marked \"noreturn\".";
3112 else if (GraphPrintCheckerState->isBadCall(N))
3113 Out << "\\|Call to NULL/Undefined.";
3114 else if (GraphPrintCheckerState->isUndefArg(N))
3115 Out << "\\|Argument in call is undefined";
3116
3117 break;
3118 }
3119
Ted Kremenekaa66a322008-01-16 21:46:15 +00003120 const BlockEdge& E = cast<BlockEdge>(Loc);
3121 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3122 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003123
3124 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003125
3126 SourceLocation SLoc = T->getLocStart();
3127
Ted Kremenekb38911f2008-01-30 23:03:39 +00003128 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003129
Ted Kremeneka95d3752008-09-13 05:16:45 +00003130 llvm::raw_os_ostream OutS(Out);
3131 E.getSrc()->printTerminator(OutS);
3132 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003133
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003134 if (SLoc.isFileID()) {
3135 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003136 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3137 << " col="
3138 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003139 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003140
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003141 if (isa<SwitchStmt>(T)) {
3142 Stmt* Label = E.getDst()->getLabel();
3143
3144 if (Label) {
3145 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3146 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003147 llvm::raw_os_ostream OutS(Out);
3148 C->getLHS()->printPretty(OutS);
3149 OutS.flush();
3150
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003151 if (Stmt* RHS = C->getRHS()) {
3152 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003153 RHS->printPretty(OutS);
3154 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003155 }
3156
3157 Out << ":";
3158 }
3159 else {
3160 assert (isa<DefaultStmt>(Label));
3161 Out << "\\ldefault:";
3162 }
3163 }
3164 else
3165 Out << "\\l(implicit) default:";
3166 }
3167 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003168 // FIXME
3169 }
3170 else {
3171 Out << "\\lCondition: ";
3172 if (*E.getSrc()->succ_begin() == E.getDst())
3173 Out << "true";
3174 else
3175 Out << "false";
3176 }
3177
3178 Out << "\\l";
3179 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003180
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003181 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3182 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003183 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003184 }
3185 }
3186
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003187 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003188
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003189 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3190 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003191
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003192 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003193 return Out.str();
3194 }
3195};
3196} // end llvm namespace
3197#endif
3198
Ted Kremenekffe0f432008-03-07 22:58:01 +00003199#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003200template <typename ITERATOR>
3201GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3202
3203template <>
3204GRExprEngine::NodeTy*
3205GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3206 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3207 return I->first;
3208}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003209#endif
3210
3211void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003212#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003213 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003214 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003215
3216 // Flush any outstanding reports to make sure we cover all the nodes.
3217 // This does not cause them to get displayed.
3218 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3219 const_cast<BugType*>(*I)->FlushReports(BR);
3220
3221 // Iterate through the reports and get their nodes.
3222 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3223 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3224 const BugReportEquivClass& EQ = *I2;
3225 const BugReport &R = **EQ.begin();
3226 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3227 if (N) Src.push_back(N);
3228 }
3229 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003230
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003231 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003232 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003233 else {
3234 GraphPrintCheckerState = this;
3235 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003236
Ted Kremenekffe0f432008-03-07 22:58:01 +00003237 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003238
3239 GraphPrintCheckerState = NULL;
3240 GraphPrintSourceManager = NULL;
3241 }
3242#endif
3243}
3244
3245void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3246#ifndef NDEBUG
3247 GraphPrintCheckerState = this;
3248 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003249
Ted Kremenekcf118d42009-02-04 23:49:09 +00003250 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003251
Ted Kremenekcf118d42009-02-04 23:49:09 +00003252 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003253 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003254 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003255 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003256
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003257 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003258 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003259#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003260}