blob: d3df92df34f2ee5d394f53bfcebbac3c343daa3c [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek77349cb2008-02-14 22:13:12 +000016#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000018#include "clang/Analysis/PathSensitive/BugReporter.h"
Chris Lattner16f00492009-04-26 01:32:48 +000019#include "clang/AST/ParentMap.h"
20#include "clang/AST/StmtObjC.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/Basic/SourceManager.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000024#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000025#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000026#include "llvm/ADT/ImmutableList.h"
27#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000028#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000029
Ted Kremenek0f5f0592008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
32#include <sstream>
33#endif
34
Ted Kremenekb387a3f2008-02-14 22:16:04 +000035using namespace clang;
36using llvm::dyn_cast;
37using llvm::cast;
38using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000039
Ted Kremeneke695e1c2008-04-15 23:06:53 +000040//===----------------------------------------------------------------------===//
41// Engine construction and deletion.
42//===----------------------------------------------------------------------===//
43
Ted Kremenekbdb435d2008-07-11 18:37:32 +000044namespace {
45
46class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
47 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
48 typedef llvm::DenseMap<void*,Checks> MapTy;
49
50 MapTy M;
51 Checks::Factory F;
Ted Kremenek536aa022009-03-30 17:53:05 +000052 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000053
54public:
Ted Kremenek536aa022009-03-30 17:53:05 +000055 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
56 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenekbdb435d2008-07-11 18:37:32 +000057
58 virtual ~MappedBatchAuditor() {
59 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
60
61 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
62 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
63
64 GRSimpleAPICheck* check = *I;
65
66 if (AlreadyVisited.count(check))
67 continue;
68
69 AlreadyVisited.insert(check);
70 delete check;
71 }
72 }
73
Ted Kremenek536aa022009-03-30 17:53:05 +000074 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000075 assert (A && "Check cannot be null.");
76 void* key = reinterpret_cast<void*>((uintptr_t) C);
77 MapTy::iterator I = M.find(key);
78 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
79 }
Ted Kremenek536aa022009-03-30 17:53:05 +000080
81 void AddCheck(GRSimpleAPICheck *A) {
82 assert (A && "Check cannot be null.");
83 AllStmts = F.Concat(A, AllStmts);
84 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000085
Ted Kremenek4adc81e2008-08-13 04:27:00 +000086 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-03-30 17:53:05 +000087 // First handle the auditors that accept all statements.
88 bool isSink = false;
89 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
90 isSink |= (*I)->Audit(N, VMgr);
91
92 // Next handle the auditors that accept only specific statements.
Ted Kremenekbdb435d2008-07-11 18:37:32 +000093 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
94 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
95 MapTy::iterator MI = M.find(key);
Ted Kremenek536aa022009-03-30 17:53:05 +000096 if (MI != M.end()) {
97 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
98 isSink |= (*I)->Audit(N, VMgr);
99 }
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000100
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000101 return isSink;
102 }
103};
104
105} // end anonymous namespace
106
107//===----------------------------------------------------------------------===//
108// Engine construction and deletion.
109//===----------------------------------------------------------------------===//
110
Ted Kremeneke448ab42008-05-01 18:33:28 +0000111static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
112 IdentifierInfo* II = &Ctx.Idents.get(name);
113 return Ctx.Selectors.getSelector(0, &II);
114}
115
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000116
Ted Kremenek8b233612008-07-02 20:13:38 +0000117GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000118 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000119 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000120 StoreManagerCreator SMC,
121 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 : CoreEngine(cfg, CD, Ctx, *this),
123 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000124 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000125 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000126 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000127 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000128 ValMgr(StateMgr.getValueManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000129 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000130 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
131 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000132 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000133 BR(BRD, *this),
134 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000135
Ted Kremenek1a654b62008-06-20 21:45:25 +0000136GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000137 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000138 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000139}
140
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000141//===----------------------------------------------------------------------===//
142// Utility methods.
143//===----------------------------------------------------------------------===//
144
Ted Kremenek186350f2008-04-23 20:12:28 +0000145
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000146void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000147 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000148 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000149 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000150}
151
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000152void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
153 if (!BatchAuditor)
154 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
155
156 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000157}
158
Ted Kremenek536aa022009-03-30 17:53:05 +0000159void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
160 if (!BatchAuditor)
161 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
162
163 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
164}
165
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000166const GRState* GRExprEngine::getInitialState() {
Ted Kremenek52e56022009-04-10 00:59:50 +0000167 const GRState *state = StateMgr.getInitialState();
168
169 // Precondition: the first argument of 'main' is an integer guaranteed
170 // to be > 0.
171 // FIXME: It would be nice if we had a more general mechanism to add
172 // such preconditions. Some day.
173 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
174 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
175 FD->getNumParams() > 0) {
176 const ParmVarDecl *PD = FD->getParamDecl(0);
177 QualType T = PD->getType();
178 if (T->isIntegerType())
Ted Kremenek25e751a2009-06-23 21:37:46 +0000179 if (const MemRegion *R = state->getRegion(PD)) {
Ted Kremenek3ff12592009-06-19 17:10:32 +0000180 SVal V = state->getSVal(loc::MemRegionVal(R));
Zhongxing Xu262fd032009-05-20 09:00:16 +0000181 SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
Ted Kremenek52e56022009-04-10 00:59:50 +0000182 ValMgr.makeZeroVal(T),
183 getContext().IntTy);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000184
185 if (const GRState *newState = state->assume(Constraint, true))
186 state = newState;
Ted Kremenek52e56022009-04-10 00:59:50 +0000187 }
188 }
189
190 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000191}
192
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000193//===----------------------------------------------------------------------===//
194// Top-level transfer function logic (Dispatcher).
195//===----------------------------------------------------------------------===//
196
197void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
198
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000199 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
200 S->getLocStart(),
201 "Error evaluating statement");
202
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000203 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000204 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000205
206 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000208 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000209
210 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000211 if (BatchAuditor)
212 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000213
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000214 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000215 SymbolReaper SymReaper(Liveness, SymMgr);
216 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
217 CurrentStmt, SymReaper)
218 : EntryNode->getState();
219
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000220 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000221 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000222
Ted Kremenek241677a2009-01-21 22:26:05 +0000223 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000224 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000225 else {
226 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000227 SaveOr OldHasGen(Builder->HasGeneratedNode);
228
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000229 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
230 Builder->PurgingDeadSymbols = true;
231
Ted Kremenek729a9a22008-07-17 23:15:45 +0000232 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000233 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000234
235 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
236 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000237 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000238
239 bool HasAutoGenerated = false;
240
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000241 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000242
243 NodeSet Dst;
244
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000245 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000246 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
247
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000248 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000249 Visit(S, *I, Dst);
250
251 // Do we need to auto-generate a node? We only need to do this to generate
252 // a node with a "cleaned" state; GRCoreEngine will actually handle
253 // auto-transitions for other cases.
254 if (Dst.size() == 1 && *Dst.begin() == EntryNode
255 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
256 HasAutoGenerated = true;
257 builder.generateNode(S, GetState(EntryNode), *I);
258 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000259 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000260
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000261 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000262 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000263 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000264
265 // FIXME: Consolidate.
266 StateMgr.CurrentStmt = 0;
267 CurrentStmt = 0;
268
Ted Kremenek846d4e92008-04-24 23:35:58 +0000269 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000270}
271
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000272void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
273 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
274 S->getLocStart(),
275 "Error evaluating statement");
276
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000277 // FIXME: add metadata to the CFG so that we can disable
278 // this check when we KNOW that there is no block-level subexpression.
279 // The motivation is that this check requires a hashtable lookup.
280
281 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
282 Dst.Add(Pred);
283 return;
284 }
285
286 switch (S->getStmtClass()) {
287
288 default:
289 // Cases we intentionally have "default" handle:
290 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
291
292 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
293 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000294
295 case Stmt::ArraySubscriptExprClass:
296 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
297 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000298
299 case Stmt::AsmStmtClass:
300 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
301 break;
302
303 case Stmt::BinaryOperatorClass: {
304 BinaryOperator* B = cast<BinaryOperator>(S);
305
306 if (B->isLogicalOp()) {
307 VisitLogicalExpr(B, Pred, Dst);
308 break;
309 }
310 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000311 const GRState* state = GetState(Pred);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000312 MakeNode(Dst, B, Pred, state->bindExpr(B, state->getSVal(B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000313 break;
314 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000315
Ted Kremenek48af2a92009-02-25 22:32:02 +0000316 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
317 NodeSet Tmp;
318 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000319 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000320 }
321 else
322 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
323
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000324 break;
325 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000326
Douglas Gregorb4609802008-11-14 16:09:21 +0000327 case Stmt::CallExprClass:
328 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000329 CallExpr* C = cast<CallExpr>(S);
330 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000331 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000332 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000333
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000334 // FIXME: ChooseExpr is really a constant. We need to fix
335 // the CFG do not model them as explicit control-flow.
336
337 case Stmt::ChooseExprClass: { // __builtin_choose_expr
338 ChooseExpr* C = cast<ChooseExpr>(S);
339 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
340 break;
341 }
342
343 case Stmt::CompoundAssignOperatorClass:
344 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
345 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000346
347 case Stmt::CompoundLiteralExprClass:
348 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
349 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000350
351 case Stmt::ConditionalOperatorClass: { // '?' operator
352 ConditionalOperator* C = cast<ConditionalOperator>(S);
353 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
354 break;
355 }
356
357 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000358 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000359 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000360 break;
361
362 case Stmt::DeclStmtClass:
363 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
364 break;
365
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000366 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000367 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000368 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000369 VisitCast(C, C->getSubExpr(), Pred, Dst);
370 break;
371 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000372
373 case Stmt::InitListExprClass:
374 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
375 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000376
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000377 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000378 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
379 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000380
381 case Stmt::ObjCIvarRefExprClass:
382 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
383 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000384
385 case Stmt::ObjCForCollectionStmtClass:
386 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
387 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000388
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000389 case Stmt::ObjCMessageExprClass: {
390 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
391 break;
392 }
393
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000394 case Stmt::ObjCAtThrowStmtClass: {
395 // FIXME: This is not complete. We basically treat @throw as
396 // an abort.
397 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
398 Builder->BuildSinks = true;
399 MakeNode(Dst, S, Pred, GetState(Pred));
400 break;
401 }
402
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000404 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000405 break;
406
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000407 case Stmt::ReturnStmtClass:
408 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
409 break;
410
Sebastian Redl05189992008-11-11 17:56:53 +0000411 case Stmt::SizeOfAlignOfExprClass:
412 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000413 break;
414
415 case Stmt::StmtExprClass: {
416 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000417
418 if (SE->getSubStmt()->body_empty()) {
419 // Empty statement expression.
420 assert(SE->getType() == getContext().VoidTy
421 && "Empty statement expression must have void type.");
422 Dst.Add(Pred);
423 break;
424 }
425
426 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
427 const GRState* state = GetState(Pred);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000428 MakeNode(Dst, SE, Pred, state->bindExpr(SE, state->getSVal(LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000429 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000430 else
431 Dst.Add(Pred);
432
433 break;
434 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000435
436 case Stmt::StringLiteralClass:
437 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
438 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000439
Ted Kremenek72374592009-03-18 23:49:26 +0000440 case Stmt::UnaryOperatorClass: {
441 UnaryOperator *U = cast<UnaryOperator>(S);
442 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
443 NodeSet Tmp;
444 VisitUnaryOperator(U, Pred, Tmp, false);
445 EvalEagerlyAssume(Dst, Tmp, U);
446 }
447 else
448 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000449 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000450 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000451 }
452}
453
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000454void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000455
456 Ex = Ex->IgnoreParens();
457
458 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
459 Dst.Add(Pred);
460 return;
461 }
462
463 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000464
465 case Stmt::ArraySubscriptExprClass:
466 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
467 return;
468
469 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000470 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000471 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
472 return;
473
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000474 case Stmt::ObjCIvarRefExprClass:
475 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
476 return;
477
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000478 case Stmt::UnaryOperatorClass:
479 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
480 return;
481
482 case Stmt::MemberExprClass:
483 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
484 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000485
Ted Kremenek4f090272008-10-27 21:54:31 +0000486 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000487 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000488 return;
489
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000490 case Stmt::ObjCPropertyRefExprClass:
Ted Kremenek868210e2009-04-21 23:53:32 +0000491 case Stmt::ObjCKVCRefExprClass:
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000492 // FIXME: Property assignments are lvalues, but not really "locations".
493 // e.g.: self.x = something;
494 // Here the "self.x" really can translate to a method call (setter) when
495 // the assignment is made. Moreover, the entire assignment expression
496 // evaluate to whatever "something" is, not calling the "getter" for
497 // the property (which would make sense since it can have side effects).
498 // We'll probably treat this as a location, but not one that we can
499 // take the address of. Perhaps we need a new SVal class for cases
500 // like thsis?
501 // Note that we have a similar problem for bitfields, since they don't
502 // have "locations" in the sense that we can take their address.
503 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000504 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000505
506 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000507 const GRState* state = GetState(Pred);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000508 SVal V = state->getLValue(cast<StringLiteral>(Ex));
509 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000510 return;
511 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000512
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000513 default:
514 // Arbitrary subexpressions can return aggregate temporaries that
515 // can be used in a lvalue context. We need to enhance our support
516 // of such temporaries in both the environment and the store, so right
517 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000518 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000519 "Other kinds of expressions with non-aggregate/union types do"
520 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000521
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000522 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000523 }
524}
525
526//===----------------------------------------------------------------------===//
527// Block entrance. (Update counters).
528//===----------------------------------------------------------------------===//
529
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000530bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000531 GRBlockCounter BC) {
532
533 return BC.getNumVisited(B->getBlockID()) < 3;
534}
535
536//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +0000537// Generic node creation.
538//===----------------------------------------------------------------------===//
539
540GRExprEngine::NodeTy* GRExprEngine::MakeNode(NodeSet& Dst, Stmt* S,
541 NodeTy* Pred,
542 const GRState* St,
543 ProgramPoint::Kind K,
544 const void *tag) {
545
546 assert (Builder && "GRStmtNodeBuilder not present.");
547 SaveAndRestore<const void*> OldTag(Builder->Tag);
548 Builder->Tag = tag;
549 return Builder->MakeNode(Dst, S, Pred, St, K);
550}
551
552//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000553// Branch processing.
554//===----------------------------------------------------------------------===//
555
Ted Kremeneka8538d92009-02-13 01:45:31 +0000556const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000557 Stmt* Terminator,
558 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000559
560 switch (Terminator->getStmtClass()) {
561 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000563
564 case Stmt::BinaryOperatorClass: { // '&&' and '||'
565
566 BinaryOperator* B = cast<BinaryOperator>(Terminator);
567 BinaryOperator::Opcode Op = B->getOpcode();
568
569 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
570
571 // For &&, if we take the true branch, then the value of the whole
572 // expression is that of the RHS expression.
573 //
574 // For ||, if we take the false branch, then the value of the whole
575 // expression is that of the RHS expression.
576
577 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
578 (Op == BinaryOperator::LOr && !branchTaken)
579 ? B->getRHS() : B->getLHS();
580
Ted Kremenek3ff12592009-06-19 17:10:32 +0000581 return state->bindBlkExpr(B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000582 }
583
584 case Stmt::ConditionalOperatorClass: { // ?:
585
586 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
587
588 // For ?, if branchTaken == true then the value is either the LHS or
589 // the condition itself. (GNU extension).
590
591 Expr* Ex;
592
593 if (branchTaken)
594 Ex = C->getLHS() ? C->getLHS() : C->getCond();
595 else
596 Ex = C->getRHS();
597
Ted Kremenek3ff12592009-06-19 17:10:32 +0000598 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000599 }
600
601 case Stmt::ChooseExprClass: { // ?:
602
603 ChooseExpr* C = cast<ChooseExpr>(Terminator);
604
605 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek3ff12592009-06-19 17:10:32 +0000606 return state->bindBlkExpr(C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000607 }
608 }
609}
610
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000611/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
612/// to try to recover some path-sensitivity for casts of symbolic
613/// integers that promote their values (which are currently not tracked well).
614/// This function returns the SVal bound to Condition->IgnoreCasts if all the
615// cast(s) did was sign-extend the original value.
616static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
617 Stmt* Condition, ASTContext& Ctx) {
618
619 Expr *Ex = dyn_cast<Expr>(Condition);
620 if (!Ex)
621 return UnknownVal();
622
623 uint64_t bits = 0;
624 bool bitsInit = false;
625
626 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
627 QualType T = CE->getType();
628
629 if (!T->isIntegerType())
630 return UnknownVal();
631
632 uint64_t newBits = Ctx.getTypeSize(T);
633 if (!bitsInit || newBits < bits) {
634 bitsInit = true;
635 bits = newBits;
636 }
637
638 Ex = CE->getSubExpr();
639 }
640
641 // We reached a non-cast. Is it a symbolic value?
642 QualType T = Ex->getType();
643
644 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
645 return UnknownVal();
646
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000647 return state->getSVal(Ex);
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000648}
649
Ted Kremenekaf337412008-11-12 19:24:17 +0000650void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000651 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000652
Ted Kremeneke7d22112008-02-11 19:21:59 +0000653 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000654 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000655 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000656
Ted Kremenekb2331832008-02-15 22:29:00 +0000657 // Check for NULL conditions; e.g. "for(;;)"
658 if (!Condition) {
659 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000660 return;
661 }
662
Ted Kremenek21028dd2009-03-11 03:54:24 +0000663 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
664 Condition->getLocStart(),
665 "Error evaluating branch");
666
Ted Kremenek3ff12592009-06-19 17:10:32 +0000667 SVal V = PrevState->getSVal(Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000668
669 switch (V.getBaseKind()) {
670 default:
671 break;
672
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000673 case SVal::UnknownKind: {
674 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
675 if (Ex->getType()->isIntegerType()) {
676 // Try to recover some path-sensitivity. Right now casts of symbolic
677 // integers that promote their values are currently not tracked well.
678 // If 'Condition' is such an expression, try and recover the
679 // underlying value and use that instead.
680 SVal recovered = RecoverCastedSymbol(getStateManager(),
681 builder.getState(), Condition,
682 getContext());
683
684 if (!recovered.isUnknown()) {
685 V = recovered;
686 break;
687 }
688 }
689 }
690
Ted Kremenek58b33212008-02-26 19:40:44 +0000691 builder.generateNode(MarkBranch(PrevState, Term, true), true);
692 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000693 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000694 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000695
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000696 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000697 NodeTy* N = builder.generateNode(PrevState, true);
698
699 if (N) {
700 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000701 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000702 }
703
704 builder.markInfeasible(false);
705 return;
706 }
707 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000708
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000709 // Process the true branch.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000710 if (const GRState *state = PrevState->assume(V, true))
Ted Kremeneka8538d92009-02-13 01:45:31 +0000711 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000712 else
713 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000714
715 // Process the false branch.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000716 if (const GRState *state = PrevState->assume(V, false))
Ted Kremeneka8538d92009-02-13 01:45:31 +0000717 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000718 else
719 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000720}
721
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000722/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000723/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000724void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000725
Ted Kremenek3ff12592009-06-19 17:10:32 +0000726 const GRState *state = builder.getState();
727 SVal V = state->getSVal(builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000728
729 // Three possibilities:
730 //
731 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000732 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000733 // (3) We have no clue about the label. Dispatch to all targets.
734 //
735
736 typedef IndirectGotoNodeBuilder::iterator iterator;
737
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000738 if (isa<loc::GotoLabel>(V)) {
739 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000740
741 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000742 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000743 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000744 return;
745 }
746 }
747
748 assert (false && "No block with label.");
749 return;
750 }
751
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000752 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000753 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000754 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000755 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000756 return;
757 }
758
759 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenekb3cfd582009-04-23 17:49:43 +0000760 // FIXME: Implement dispatch for symbolic pointers.
Ted Kremenek754607e2008-02-13 00:24:44 +0000761
762 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000763 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000764}
Ted Kremenekf233d482008-02-05 00:26:40 +0000765
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000766
767void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
768 NodeTy* Pred, NodeSet& Dst) {
769
770 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
771
Ted Kremeneka8538d92009-02-13 01:45:31 +0000772 const GRState* state = GetState(Pred);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000773 SVal X = state->getBlkExprSVal(Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000774
775 assert (X.isUndef());
776
Ted Kremenek3ff12592009-06-19 17:10:32 +0000777 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
778 assert(SE);
779 X = state->getBlkExprSVal(SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000780
781 // Make sure that we invalidate the previous binding.
Ted Kremenek3ff12592009-06-19 17:10:32 +0000782 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000783}
784
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000785/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
786/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000787void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
788 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000789 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000790 Expr* CondE = builder.getCondition();
Ted Kremenek3ff12592009-06-19 17:10:32 +0000791 SVal CondV = state->getSVal(CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000792
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000793 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000794 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000795 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000796 return;
797 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000798
Ted Kremeneka8538d92009-02-13 01:45:31 +0000799 const GRState* DefaultSt = state;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000800 bool defaultIsFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000801
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000802 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000803 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000804
805 // Evaluate the LHS of the case value.
806 Expr::EvalResult V1;
807 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000808
Ted Kremenek72afb372009-01-17 01:54:16 +0000809 // Sanity checks. These go away in Release builds.
810 assert(b && V1.Val.isInt() && !V1.HasSideEffects
811 && "Case condition must evaluate to an integer constant.");
812 b = b; // silence unused variable warning
813 assert(V1.Val.getInt().getBitWidth() ==
814 getContext().getTypeSize(CondE->getType()));
815
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000816 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000817 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000818
819 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000820 b = E->Evaluate(V2, getContext());
821 assert(b && V2.Val.isInt() && !V2.HasSideEffects
822 && "Case condition must evaluate to an integer constant.");
823 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000824 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000825 else
826 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000827
828 // FIXME: Eventually we should replace the logic below with a range
829 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000830 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000831
Ted Kremenek14a11402008-03-17 22:17:56 +0000832 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000833 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Zhongxing Xu262fd032009-05-20 09:00:16 +0000834 SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000835 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000836
Ted Kremenek72afb372009-01-17 01:54:16 +0000837 // Now "assume" that the case matches.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000838 if (const GRState* stateNew = state->assume(Res, true)) {
839 builder.generateCaseStmtNode(I, stateNew);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000840
841 // If CondV evaluates to a constant, then we know that this
842 // is the *only* case that we can take, so stop evaluating the
843 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000844 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000845 return;
846 }
847
848 // Now "assume" that the case doesn't match. Add this state
849 // to the default state (if it is feasible).
Ted Kremeneka591bc02009-06-18 22:57:13 +0000850 if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
851 defaultIsFeasible = true;
852 DefaultSt = stateNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000853 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000854
Ted Kremenek14a11402008-03-17 22:17:56 +0000855 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000856 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000857 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000858
Ted Kremenek72afb372009-01-17 01:54:16 +0000859 ++V1.Val.getInt();
860 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000861
862 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000863 }
864
865 // If we reach here, than we know that the default branch is
866 // possible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000867 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000868}
869
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000870//===----------------------------------------------------------------------===//
871// Transfer functions: logical operations ('&&', '||').
872//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000873
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000874void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000875 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000876
Ted Kremenek3ff12592009-06-19 17:10:32 +0000877 assert(B->getOpcode() == BinaryOperator::LAnd ||
878 B->getOpcode() == BinaryOperator::LOr);
Ted Kremenek05a23782008-02-26 19:05:15 +0000879
Ted Kremenek3ff12592009-06-19 17:10:32 +0000880 assert(B == CurrentStmt && getCFG().isBlkExpr(B));
Ted Kremenek05a23782008-02-26 19:05:15 +0000881
Ted Kremeneka8538d92009-02-13 01:45:31 +0000882 const GRState* state = GetState(Pred);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000883 SVal X = state->getBlkExprSVal(B);
884 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
Ted Kremenek3ff12592009-06-19 17:10:32 +0000888 assert(Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000889
890 if (Ex == B->getRHS()) {
891
Ted Kremenek3ff12592009-06-19 17:10:32 +0000892 X = state->getBlkExprSVal(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 Kremenek3ff12592009-06-19 17:10:32 +0000897 MakeNode(Dst, B, Pred, state->bindBlkExpr(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,
Ted Kremeneka591bc02009-06-18 22:57:13 +0000906 // the payoff is not likely to be large. Instead, we do eager evaluation.
907 if (const GRState *newState = state->assume(X, true))
Zhongxing Xud91ee272009-06-23 09:02:15 +0000908 MakeNode(Dst, B, Pred,
909 newState->bindBlkExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Ted Kremenek05a23782008-02-26 19:05:15 +0000910
Ted Kremeneka591bc02009-06-18 22:57:13 +0000911 if (const GRState *newState = state->assume(X, false))
Zhongxing Xud91ee272009-06-23 09:02:15 +0000912 MakeNode(Dst, B, Pred,
913 newState->bindBlkExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenekf233d482008-02-05 00:26:40 +0000914 }
915 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000916 // We took the LHS expression. Depending on whether we are '&&' or
917 // '||' we know what the value of the expression is via properties of
918 // the short-circuiting.
Zhongxing Xud91ee272009-06-23 09:02:15 +0000919 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
920 B->getType());
Ted Kremenek3ff12592009-06-19 17:10:32 +0000921 MakeNode(Dst, B, Pred, state->bindBlkExpr(B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000922 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000923}
Ted Kremenek05a23782008-02-26 19:05:15 +0000924
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000925//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000926// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000927//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000928
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000929void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
930 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000931
Ted Kremeneka8538d92009-02-13 01:45:31 +0000932 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000933
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000934 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000935
936 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
937
Ted Kremenek3ff12592009-06-19 17:10:32 +0000938 SVal V = state->getLValue(VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000939
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000940 if (asLValue)
Ted Kremenek3ff12592009-06-19 17:10:32 +0000941 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek7090d542009-05-07 18:27:16 +0000942 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000943 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000944 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000945 return;
946
947 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
948 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
949
Zhongxing Xu3038c5a2009-06-23 06:13:19 +0000950 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek3ff12592009-06-19 17:10:32 +0000951 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000952 return;
953
954 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000955 assert(asLValue);
Zhongxing Xu369f4472009-04-20 05:24:46 +0000956 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000957 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V),
Ted Kremenek7090d542009-05-07 18:27:16 +0000958 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000959 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000960 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000961
962 assert (false &&
963 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000964}
965
Ted Kremenek540cbe22008-04-22 04:56:29 +0000966/// VisitArraySubscriptExpr - Transfer function for array accesses
967void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000968 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000969
970 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000971 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000972 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000973
974 if (Base->getType()->isVectorType()) {
975 // For vector types get its lvalue.
976 // FIXME: This may not be correct. Is the rvalue of a vector its location?
977 // In fact, I think this is just a hack. We need to get the right
978 // semantics.
979 VisitLValue(Base, Pred, Tmp);
980 }
981 else
982 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000983
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000984 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000985 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000986 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000987
988 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000989 const GRState* state = GetState(*I2);
Ted Kremenek3ff12592009-06-19 17:10:32 +0000990 SVal V = state->getLValue(A->getType(), state->getSVal(Base),
991 state->getSVal(Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000992
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000993 if (asLValue)
Ted Kremenek3ff12592009-06-19 17:10:32 +0000994 MakeNode(Dst, A, *I2, state->bindExpr(A, V),
Ted Kremenek7090d542009-05-07 18:27:16 +0000995 ProgramPoint::PostLValueKind);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000996 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000997 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000998 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000999 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001000}
1001
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001002/// VisitMemberExpr - Transfer function for member expressions.
1003void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001004 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001005
1006 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001007 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001008
1009 if (M->isArrow())
1010 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1011 else
1012 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1013
Douglas Gregor86f19402008-12-20 23:49:58 +00001014 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1015 if (!Field) // FIXME: skipping member expressions for non-fields
1016 return;
1017
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001018 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001019 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001020 // FIXME: Should we insert some assumption logic in here to determine
1021 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001022 // later when using FieldOffset lvals (which we no longer have).
Ted Kremenek3ff12592009-06-19 17:10:32 +00001023 SVal L = state->getLValue(state->getSVal(Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001024
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001025 if (asLValue)
Ted Kremenek3ff12592009-06-19 17:10:32 +00001026 MakeNode(Dst, M, *I, state->bindExpr(M, L),
Ted Kremenek7090d542009-05-07 18:27:16 +00001027 ProgramPoint::PostLValueKind);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001028 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001029 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001030 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001031}
1032
Ted Kremeneka8538d92009-02-13 01:45:31 +00001033/// EvalBind - Handle the semantics of binding a value to a specific location.
1034/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1035void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1036 const GRState* state, SVal location, SVal Val) {
1037
Ted Kremenek41573eb2009-02-14 01:43:44 +00001038 const GRState* newState = 0;
1039
1040 if (location.isUnknown()) {
1041 // We know that the new state will be the same as the old state since
1042 // the location of the binding is "unknown". Consequently, there
1043 // is no reason to just create a new node.
1044 newState = state;
1045 }
1046 else {
1047 // We are binding to a value other than 'unknown'. Perform the binding
1048 // using the StoreManager.
Ted Kremenek76500d02009-06-23 20:38:51 +00001049 newState = state->bindLoc(cast<Loc>(location), Val);
Ted Kremenek41573eb2009-02-14 01:43:44 +00001050 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001051
Ted Kremenek41573eb2009-02-14 01:43:44 +00001052 // The next thing to do is check if the GRTransferFuncs object wants to
1053 // update the state based on the new binding. If the GRTransferFunc object
1054 // doesn't do anything, just auto-propagate the current state.
1055 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1056 newState != state);
1057
1058 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001059}
1060
1061/// EvalStore - Handle the semantics of a store via an assignment.
1062/// @param Dst The node set to store generated state nodes
1063/// @param Ex The expression representing the location of the store
1064/// @param state The current simulation state
1065/// @param location The location to store the value
1066/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001067void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001068 const GRState* state, SVal location, SVal Val,
1069 const void *tag) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001070
1071 assert (Builder && "GRStmtNodeBuilder must be defined.");
1072
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001073 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001074 Pred = EvalLocation(Ex, Pred, state, location, tag);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001075
Ted Kremenek8c354752008-12-16 22:02:27 +00001076 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001077 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001078
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001079 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001080 state = GetState(Pred);
1081
1082 // Proceed with the store.
1083 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
Ted Kremenek1670e402009-04-11 00:11:10 +00001084 SaveAndRestore<const void*> OldTag(Builder->Tag);
1085 Builder->PointKind = ProgramPoint::PostStoreKind;
1086 Builder->Tag = tag;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001087 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001088}
1089
1090void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremenek1670e402009-04-11 00:11:10 +00001091 const GRState* state, SVal location,
1092 const void *tag) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001093
Ted Kremenek8c354752008-12-16 22:02:27 +00001094 // Evaluate the location (checks for bad dereferences).
Ted Kremenek1670e402009-04-11 00:11:10 +00001095 Pred = EvalLocation(Ex, Pred, state, location, tag);
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 Kremenek3ff12592009-06-19 17:10:32 +00001110 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, UnknownVal()), K, tag);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001111 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001112 else {
Ted Kremenek3ff12592009-06-19 17:10:32 +00001113 SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
1114 MakeNode(Dst, Ex, Pred, state->bindExpr(Ex, V), K, tag);
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 Kremenek1670e402009-04-11 00:11:10 +00001119 const GRState* state, SVal location, SVal Val,
1120 const void *tag) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001121
1122 NodeSet TmpDst;
Ted Kremenek1670e402009-04-11 00:11:10 +00001123 EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001124
1125 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
Ted Kremenek1670e402009-04-11 00:11:10 +00001126 MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001127}
1128
Ted Kremenek8c354752008-12-16 22:02:27 +00001129GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001130 const GRState* state,
Ted Kremenek1670e402009-04-11 00:11:10 +00001131 SVal location,
1132 const void *tag) {
1133
1134 SaveAndRestore<const void*> OldTag(Builder->Tag);
1135 Builder->Tag = tag;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001136
1137 // Check for loads/stores from/to undefined values.
1138 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001139 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001140 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001141 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001142
Ted Kremenek8c354752008-12-16 22:02:27 +00001143 if (N) {
1144 N->markAsSink();
1145 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001146 }
1147
Ted Kremenek8c354752008-12-16 22:02:27 +00001148 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001149 }
1150
1151 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1152 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001153 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001154
1155 // During a load, one of two possible situations arise:
1156 // (1) A crash, because the location (pointer) was NULL.
1157 // (2) The location (pointer) is not NULL, and the dereference works.
1158 //
1159 // We add these assumptions.
1160
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001161 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001162
1163 // "Assume" that the pointer is not NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001164 const GRState *StNotNull = state->assume(LV, true);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001165
1166 // "Assume" that the pointer is NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001167 const GRState *StNull = state->assume(LV, false);
Zhongxing Xua1718c72009-04-03 07:33:13 +00001168
Ted Kremeneka591bc02009-06-18 22:57:13 +00001169 if (StNull) {
Ted Kremenek7360fda2008-09-18 23:09:54 +00001170 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001171 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenekb65be702009-06-18 01:23:53 +00001172 StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001173
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001174 // We don't use "MakeNode" here because the node will be a sink
1175 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001176 NodeTy* NullNode =
1177 Builder->generateNode(Ex, StNull, Pred,
1178 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001179
Ted Kremeneka591bc02009-06-18 22:57:13 +00001180 if (NullNode) {
1181 NullNode->markAsSink();
1182 if (StNotNull) ImplicitNullDeref.insert(NullNode);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001183 else ExplicitNullDeref.insert(NullNode);
1184 }
1185 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001186
Ted Kremeneka591bc02009-06-18 22:57:13 +00001187 if (!StNotNull)
1188 return NULL;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001189
1190 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001191 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001192 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1193 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1194 // Get the index of the accessed element.
1195 SVal Idx = ER->getIndex();
1196 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001197 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1198 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001199
Ted Kremeneka591bc02009-06-18 22:57:13 +00001200 const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
1201 true);
1202 const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
1203 false);
Zhongxing Xu60156f02008-11-08 03:45:42 +00001204
Ted Kremeneka591bc02009-06-18 22:57:13 +00001205 if (StOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001206 // Report warning. Make sink node manually.
1207 NodeTy* OOBNode =
1208 Builder->generateNode(Ex, StOutBound, Pred,
1209 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001210
1211 if (OOBNode) {
1212 OOBNode->markAsSink();
1213
Ted Kremeneka591bc02009-06-18 22:57:13 +00001214 if (StInBound)
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001215 ImplicitOOBMemAccesses.insert(OOBNode);
1216 else
1217 ExplicitOOBMemAccesses.insert(OOBNode);
1218 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001219 }
1220
Ted Kremeneka591bc02009-06-18 22:57:13 +00001221 if (!StInBound)
1222 return NULL;
1223
Ted Kremenek8c354752008-12-16 22:02:27 +00001224 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001225 }
1226 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001227
Ted Kremenek8c354752008-12-16 22:02:27 +00001228 // Generate a new node indicating the checks succeed.
1229 return Builder->generateNode(Ex, StNotNull, Pred,
1230 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001231}
1232
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001233//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001234// Transfer function: OSAtomics.
1235//
1236// FIXME: Eventually refactor into a more "plugin" infrastructure.
1237//===----------------------------------------------------------------------===//
1238
1239// Mac OS X:
1240// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1241// atomic.3.html
1242//
1243static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet<GRState>& Dst,
1244 GRExprEngine& Engine,
1245 GRStmtNodeBuilder<GRState>& Builder,
1246 CallExpr* CE, SVal L,
1247 ExplodedNode<GRState>* Pred) {
1248
1249 // Not enough arguments to match OSAtomicCompareAndSwap?
1250 if (CE->getNumArgs() != 3)
1251 return false;
1252
1253 ASTContext &C = Engine.getContext();
1254 Expr *oldValueExpr = CE->getArg(0);
1255 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1256
1257 Expr *newValueExpr = CE->getArg(1);
1258 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
1259
1260 // Do the types of 'oldValue' and 'newValue' match?
1261 if (oldValueType != newValueType)
1262 return false;
1263
1264 Expr *theValueExpr = CE->getArg(2);
1265 const PointerType *theValueType = theValueExpr->getType()->getAsPointerType();
1266
1267 // theValueType not a pointer?
1268 if (!theValueType)
1269 return false;
1270
1271 QualType theValueTypePointee =
1272 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
1273
1274 // The pointee must match newValueType and oldValueType.
1275 if (theValueTypePointee != newValueType)
1276 return false;
1277
1278 static unsigned magic_load = 0;
1279 static unsigned magic_store = 0;
1280
1281 const void *OSAtomicLoadTag = &magic_load;
1282 const void *OSAtomicStoreTag = &magic_store;
1283
1284 // Load 'theValue'.
Ted Kremenek1670e402009-04-11 00:11:10 +00001285 const GRState *state = Pred->getState();
1286 ExplodedNodeSet<GRState> Tmp;
Ted Kremenek23ec48c2009-06-18 23:58:37 +00001287 SVal location = state->getSVal(theValueExpr);
Ted Kremenek1670e402009-04-11 00:11:10 +00001288 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
1289
1290 for (ExplodedNodeSet<GRState>::iterator I = Tmp.begin(), E = Tmp.end();
1291 I != E; ++I) {
1292
1293 ExplodedNode<GRState> *N = *I;
1294 const GRState *stateLoad = N->getState();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001295 SVal theValueVal = stateLoad->getSVal(theValueExpr);
1296 SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
Ted Kremenek1670e402009-04-11 00:11:10 +00001297
1298 // Perform the comparison.
Zhongxing Xu262fd032009-05-20 09:00:16 +00001299 SVal Cmp = Engine.EvalBinOp(stateLoad,
1300 BinaryOperator::EQ, theValueVal, oldValueVal,
Ted Kremenek1670e402009-04-11 00:11:10 +00001301 Engine.getContext().IntTy);
Ted Kremeneka591bc02009-06-18 22:57:13 +00001302
1303 const GRState *stateEqual = stateLoad->assume(Cmp, true);
Ted Kremenek1670e402009-04-11 00:11:10 +00001304
1305 // Were they equal?
Ted Kremeneka591bc02009-06-18 22:57:13 +00001306 if (stateEqual) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001307 // Perform the store.
1308 ExplodedNodeSet<GRState> TmpStore;
1309 Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
Ted Kremenek23ec48c2009-06-18 23:58:37 +00001310 stateEqual->getSVal(newValueExpr), OSAtomicStoreTag);
Ted Kremenek1670e402009-04-11 00:11:10 +00001311
1312 // Now bind the result of the comparison.
1313 for (ExplodedNodeSet<GRState>::iterator I2 = TmpStore.begin(),
1314 E2 = TmpStore.end(); I2 != E2; ++I2) {
1315 ExplodedNode<GRState> *predNew = *I2;
1316 const GRState *stateNew = predNew->getState();
1317 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek3ff12592009-06-19 17:10:32 +00001318 Engine.MakeNode(Dst, CE, predNew, stateNew->bindExpr(CE, Res));
Ted Kremenek1670e402009-04-11 00:11:10 +00001319 }
1320 }
1321
1322 // Were they not equal?
Ted Kremeneka591bc02009-06-18 22:57:13 +00001323 if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
Ted Kremenek1670e402009-04-11 00:11:10 +00001324 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek3ff12592009-06-19 17:10:32 +00001325 Engine.MakeNode(Dst, CE, N, stateNotEqual->bindExpr(CE, Res));
Ted Kremenek1670e402009-04-11 00:11:10 +00001326 }
1327 }
1328
1329 return true;
1330}
1331
1332static bool EvalOSAtomic(ExplodedNodeSet<GRState>& Dst,
1333 GRExprEngine& Engine,
1334 GRStmtNodeBuilder<GRState>& Builder,
1335 CallExpr* CE, SVal L,
1336 ExplodedNode<GRState>* Pred) {
Zhongxing Xu369f4472009-04-20 05:24:46 +00001337 const FunctionDecl* FD = L.getAsFunctionDecl();
1338 if (!FD)
Ted Kremenek1670e402009-04-11 00:11:10 +00001339 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +00001340
Ted Kremenek1670e402009-04-11 00:11:10 +00001341 const char *FName = FD->getNameAsCString();
1342
1343 // Check for compare and swap.
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001344 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1345 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Ted Kremenek1670e402009-04-11 00:11:10 +00001346 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
Ted Kremenekb3bf76f2009-04-11 00:54:13 +00001347
Ted Kremenek1670e402009-04-11 00:11:10 +00001348 // FIXME: Other atomics.
1349 return false;
1350}
1351
1352//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001353// Transfer function: Function calls.
1354//===----------------------------------------------------------------------===//
Ted Kremenek1670e402009-04-11 00:11:10 +00001355
1356void GRExprEngine::EvalCall(NodeSet& Dst, CallExpr* CE, SVal L, NodeTy* Pred) {
1357 assert (Builder && "GRStmtNodeBuilder must be defined.");
1358
1359 // FIXME: Allow us to chain together transfer functions.
1360 if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
1361 return;
1362
1363 getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
1364}
1365
Ted Kremenekde434242008-02-19 01:44:53 +00001366void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001367 CallExpr::arg_iterator AI,
1368 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001369 NodeSet& Dst)
1370{
1371 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001372 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001373 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1374 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001375 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001376
1377 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1378}
1379
1380void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1381 CallExpr::arg_iterator AI,
1382 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001383 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001384 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001385
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001386 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001387 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001388 // If the call argument is being bound to a reference parameter,
1389 // visit it as an lvalue, not an rvalue.
1390 bool VisitAsLvalue = false;
1391 if (Proto && ParamIdx < Proto->getNumArgs())
1392 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1393
1394 NodeSet DstTmp;
1395 if (VisitAsLvalue)
1396 VisitLValue(*AI, Pred, DstTmp);
1397 else
1398 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001399 ++AI;
1400
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001401 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001402 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001403
1404 return;
1405 }
1406
1407 // If we reach here we have processed all of the arguments. Evaluate
1408 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001409
Ted Kremenek994a09b2008-02-25 21:16:03 +00001410 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001411 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001412
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001413 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001414
Ted Kremenekde434242008-02-19 01:44:53 +00001415 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001416 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1417
Ted Kremeneka8538d92009-02-13 01:45:31 +00001418 const GRState* state = GetState(*DI);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001419 SVal L = state->getSVal(Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001420
Ted Kremeneka1354a52008-03-03 16:47:31 +00001421 // FIXME: Add support for symbolic function calls (calls involving
1422 // function pointer values that are symbolic).
1423
1424 // Check for undefined control-flow or calls to NULL.
1425
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001426 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001427 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001428
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001429 if (N) {
1430 N->markAsSink();
1431 BadCalls.insert(N);
1432 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001433
Ted Kremenekde434242008-02-19 01:44:53 +00001434 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001435 }
1436
1437 // Check for the "noreturn" attribute.
1438
1439 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Zhongxing Xu369f4472009-04-20 05:24:46 +00001440 const FunctionDecl* FD = L.getAsFunctionDecl();
1441 if (FD) {
Douglas Gregor68584ed2009-06-18 16:11:24 +00001442 if (FD->getAttr<NoReturnAttr>(getContext()) ||
1443 FD->getAttr<AnalyzerNoReturnAttr>(getContext()))
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001444 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001445 else {
1446 // HACK: Some functions are not marked noreturn, and don't return.
1447 // Here are a few hardwired ones. If this takes too long, we can
1448 // potentially cache these results.
1449 const char* s = FD->getIdentifier()->getName();
1450 unsigned n = strlen(s);
1451
1452 switch (n) {
1453 default:
1454 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001455
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001456 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001457 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1458 break;
1459
1460 case 5:
1461 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001462 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001463 if (CE->getNumArgs() > 0) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00001464 SVal X = state->getSVal(*CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001465 // FIXME: use Assume to inspect the possible symbolic value of
1466 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001467 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001468 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001469 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001470 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001471 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001472 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001473
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001474 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001475 if (!memcmp(s, "Assert", 6)) {
1476 Builder->BuildSinks = true;
1477 break;
1478 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001479
1480 // FIXME: This is just a wrapper around throwing an exception.
1481 // Eventually inter-procedural analysis should handle this easily.
1482 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1483
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001484 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001485
1486 case 7:
1487 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1488 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001489
Ted Kremenekf47bb782008-04-30 17:54:04 +00001490 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001491 if (!memcmp(s ,"db_error", 8) ||
1492 !memcmp(s, "__assert", 8))
1493 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001494 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001495
1496 case 12:
1497 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1498 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001499
Ted Kremenekf9683082008-09-19 02:30:47 +00001500 case 13:
1501 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1502 break;
1503
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001504 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001505 if (!memcmp(s, "dtrace_assfail", 14) ||
1506 !memcmp(s, "yy_fatal_error", 14))
1507 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001508 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001509
1510 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001511 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001512 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1513 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001514 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001515
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001516 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001517 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001518
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001519 }
1520 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001521
1522 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001523
Zhongxing Xu369f4472009-04-20 05:24:46 +00001524 if (FD) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001525
Zhongxing Xu369f4472009-04-20 05:24:46 +00001526 if (unsigned id = FD->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001527 switch (id) {
1528 case Builtin::BI__builtin_expect: {
1529 // For __builtin_expect, just return the value of the subexpression.
1530 assert (CE->arg_begin() != CE->arg_end());
Ted Kremenek3ff12592009-06-19 17:10:32 +00001531 SVal X = state->getSVal(*(CE->arg_begin()));
1532 MakeNode(Dst, CE, *DI, state->bindExpr(CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001533 continue;
1534 }
1535
Ted Kremenekb3021332008-11-02 00:35:01 +00001536 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001537 // FIXME: Refactor into StoreManager itself?
1538 MemRegionManager& RM = getStateManager().getRegionManager();
1539 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001540 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001541
1542 // Set the extent of the region in bytes. This enables us to use the
1543 // SVal of the argument directly. If we save the extent in bits, we
1544 // cannot represent values like symbol*8.
Ted Kremenek3ff12592009-06-19 17:10:32 +00001545 SVal Extent = state->getSVal(*(CE->arg_begin()));
Ted Kremeneka8538d92009-02-13 01:45:31 +00001546 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001547
Ted Kremenek3ff12592009-06-19 17:10:32 +00001548 MakeNode(Dst, CE, *DI, state->bindExpr(CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001549 continue;
1550 }
1551
Ted Kremenek55aea312008-03-05 22:59:42 +00001552 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001553 break;
1554 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001555 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001556
Ted Kremenek186350f2008-04-23 20:12:28 +00001557 // Check any arguments passed-by-value against being undefined.
1558
1559 bool badArg = false;
1560
1561 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1562 I != E; ++I) {
1563
Ted Kremenek3ff12592009-06-19 17:10:32 +00001564 if (GetState(*DI)->getSVal(*I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001565 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001566
Ted Kremenek186350f2008-04-23 20:12:28 +00001567 if (N) {
1568 N->markAsSink();
1569 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001570 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001571
Ted Kremenek186350f2008-04-23 20:12:28 +00001572 badArg = true;
1573 break;
1574 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001575 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001576
1577 if (badArg)
1578 continue;
1579
1580 // Dispatch to the plug-in transfer function.
1581
1582 unsigned size = Dst.size();
1583 SaveOr OldHasGen(Builder->HasGeneratedNode);
1584 EvalCall(Dst, CE, L, *DI);
1585
1586 // Handle the case where no nodes where generated. Auto-generate that
1587 // contains the updated state if we aren't generating sinks.
1588
1589 if (!Builder->BuildSinks && Dst.size() == size &&
1590 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001591 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001592 }
1593}
1594
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001595//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001596// Transfer function: Objective-C ivar references.
1597//===----------------------------------------------------------------------===//
1598
Ted Kremenekf5cae632009-02-28 20:50:43 +00001599static std::pair<const void*,const void*> EagerlyAssumeTag
1600 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1601
Ted Kremenekb2939022009-02-25 23:32:10 +00001602void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001603 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1604 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001605
1606 // Test if the previous node was as the same expression. This can happen
1607 // when the expression fails to evaluate to anything meaningful and
1608 // (as an optimization) we don't generate a node.
1609 ProgramPoint P = Pred->getLocation();
1610 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1611 Dst.Add(Pred);
1612 continue;
1613 }
1614
Ted Kremenek48af2a92009-02-25 22:32:02 +00001615 const GRState* state = Pred->getState();
Ted Kremenek3ff12592009-06-19 17:10:32 +00001616 SVal V = state->getSVal(Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001617 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001618 // First assume that the condition is true.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001619 if (const GRState *stateTrue = state->assume(V, true)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00001620 stateTrue = stateTrue->bindExpr(Ex,
1621 ValMgr.makeIntVal(1U, Ex->getType()));
Ted Kremenekb2939022009-02-25 23:32:10 +00001622 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001623 stateTrue, Pred));
1624 }
1625
1626 // Next, assume that the condition is false.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001627 if (const GRState *stateFalse = state->assume(V, false)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00001628 stateFalse = stateFalse->bindExpr(Ex,
1629 ValMgr.makeIntVal(0U, Ex->getType()));
Ted Kremenekb2939022009-02-25 23:32:10 +00001630 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001631 stateFalse, Pred));
1632 }
1633 }
1634 else
1635 Dst.Add(Pred);
1636 }
1637}
1638
1639//===----------------------------------------------------------------------===//
1640// Transfer function: Objective-C ivar references.
1641//===----------------------------------------------------------------------===//
1642
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001643void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1644 NodeTy* Pred, NodeSet& Dst,
1645 bool asLValue) {
1646
1647 Expr* Base = cast<Expr>(Ex->getBase());
1648 NodeSet Tmp;
1649 Visit(Base, Pred, Tmp);
1650
1651 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001652 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001653 SVal BaseVal = state->getSVal(Base);
1654 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001655
1656 if (asLValue)
Ted Kremenek3ff12592009-06-19 17:10:32 +00001657 MakeNode(Dst, Ex, *I, state->bindExpr(Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001658 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001659 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001660 }
1661}
1662
1663//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001664// Transfer function: Objective-C fast enumeration 'for' statements.
1665//===----------------------------------------------------------------------===//
1666
1667void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1668 NodeTy* Pred, NodeSet& Dst) {
1669
1670 // ObjCForCollectionStmts are processed in two places. This method
1671 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1672 // statements within a basic block. This transfer function does two things:
1673 //
1674 // (1) binds the next container value to 'element'. This creates a new
1675 // node in the ExplodedGraph.
1676 //
1677 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1678 // whether or not the container has any more elements. This value
1679 // will be tested in ProcessBranch. We need to explicitly bind
1680 // this value because a container can contain nil elements.
1681 //
1682 // FIXME: Eventually this logic should actually do dispatches to
1683 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1684 // This will require simulating a temporary NSFastEnumerationState, either
1685 // through an SVal or through the use of MemRegions. This value can
1686 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1687 // terminates we reclaim the temporary (it goes out of scope) and we
1688 // we can test if the SVal is 0 or if the MemRegion is null (depending
1689 // on what approach we take).
1690 //
1691 // For now: simulate (1) by assigning either a symbol or nil if the
1692 // container is empty. Thus this transfer function will by default
1693 // result in state splitting.
1694
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001695 Stmt* elem = S->getElement();
1696 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001697
1698 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001699 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001700 assert (ElemD->getInit() == 0);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001701 ElementV = GetState(Pred)->getLValue(ElemD);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001702 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1703 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001704 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001705
1706 NodeSet Tmp;
1707 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001708
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001709 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1710 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00001711 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001712 }
1713}
1714
1715void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1716 NodeTy* Pred, NodeSet& Dst,
1717 SVal ElementV) {
1718
1719
Ted Kremenekaf337412008-11-12 19:24:17 +00001720
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001721 // Get the current state. Use 'EvalLocation' to determine if it is a null
1722 // pointer, etc.
1723 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001724
Ted Kremenek8c354752008-12-16 22:02:27 +00001725 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1726 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001727 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001728
Ted Kremenekb65be702009-06-18 01:23:53 +00001729 const GRState *state = GetState(Pred);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001730
Ted Kremenekaf337412008-11-12 19:24:17 +00001731 // Handle the case where the container still has elements.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001732 SVal TrueV = ValMgr.makeTruthVal(1);
Ted Kremenekb65be702009-06-18 01:23:53 +00001733 const GRState *hasElems = state->bindExpr(S, TrueV);
Ted Kremenekaf337412008-11-12 19:24:17 +00001734
Ted Kremenekaf337412008-11-12 19:24:17 +00001735 // Handle the case where the container has no elements.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001736 SVal FalseV = ValMgr.makeTruthVal(0);
Ted Kremenekb65be702009-06-18 01:23:53 +00001737 const GRState *noElems = state->bindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001738
1739 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1740 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1741 // FIXME: The proper thing to do is to really iterate over the
1742 // container. We will do this with dispatch logic to the store.
1743 // For now, just 'conjure' up a symbolic value.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001744 QualType T = R->getValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001745 assert (Loc::IsLocType(T));
1746 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001747 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
Zhongxing Xud91ee272009-06-23 09:02:15 +00001748 SVal V = ValMgr.makeLoc(Sym);
Ted Kremenekb65be702009-06-18 01:23:53 +00001749 hasElems = hasElems->bindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001750
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001751 // Bind the location to 'nil' on the false branch.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001752 SVal nilV = ValMgr.makeIntVal(0, T);
Ted Kremenekb65be702009-06-18 01:23:53 +00001753 noElems = noElems->bindLoc(ElementV, nilV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001754 }
1755
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001756 // Create the new nodes.
1757 MakeNode(Dst, S, Pred, hasElems);
1758 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001759}
1760
1761//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001762// Transfer function: Objective-C message expressions.
1763//===----------------------------------------------------------------------===//
1764
1765void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1766 NodeSet& Dst){
1767
1768 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1769 Pred, Dst);
1770}
1771
1772void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001773 ObjCMessageExpr::arg_iterator AI,
1774 ObjCMessageExpr::arg_iterator AE,
1775 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001776 if (AI == AE) {
1777
1778 // Process the receiver.
1779
1780 if (Expr* Receiver = ME->getReceiver()) {
1781 NodeSet Tmp;
1782 Visit(Receiver, Pred, Tmp);
1783
1784 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1785 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1786
1787 return;
1788 }
1789
1790 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1791 return;
1792 }
1793
1794 NodeSet Tmp;
1795 Visit(*AI, Pred, Tmp);
1796
1797 ++AI;
1798
1799 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1800 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1801}
1802
1803void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1804 NodeTy* Pred,
1805 NodeSet& Dst) {
1806
1807 // FIXME: More logic for the processing the method call.
1808
Ted Kremeneka8538d92009-02-13 01:45:31 +00001809 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001810 bool RaisesException = false;
1811
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001812
1813 if (Expr* Receiver = ME->getReceiver()) {
1814
Ted Kremenek3ff12592009-06-19 17:10:32 +00001815 SVal L = state->getSVal(Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001816
Ted Kremenek21fe8372009-02-19 04:06:22 +00001817 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001818 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001819 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001820
1821 if (N) {
1822 N->markAsSink();
1823 UndefReceivers.insert(N);
1824 }
1825
1826 return;
1827 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001828
Ted Kremenek21fe8372009-02-19 04:06:22 +00001829 // "Assume" that the receiver is not NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001830 const GRState *StNotNull = state->assume(L, true);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001831
1832 // "Assume" that the receiver is NULL.
Ted Kremeneka591bc02009-06-18 22:57:13 +00001833 const GRState *StNull = state->assume(L, false);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001834
Ted Kremeneka591bc02009-06-18 22:57:13 +00001835 if (StNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001836 QualType RetTy = ME->getType();
1837
Ted Kremenek21fe8372009-02-19 04:06:22 +00001838 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001839 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001840 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001841 // The [0 ...] expressions will return garbage. Flag either an
1842 // explicit or implicit error. Because of the structure of this
1843 // function we currently do not bifurfacte the state graph at
1844 // this point.
1845 // FIXME: We should bifurcate and fill the returned struct with
1846 // garbage.
1847 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1848 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001849 if (StNotNull)
Ted Kremenek899b3de2009-04-08 03:07:17 +00001850 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001851 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001852 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001853 }
1854 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001855 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001856 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001857 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001858 if (RetTy != Ctx.VoidTy) {
1859 if (BR.getParentMap().isConsumedExpr(ME)) {
1860 // sizeof(void *)
1861 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1862 // sizeof(return type)
1863 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001864
Ted Kremenekfe630b92009-04-09 05:45:56 +00001865 if(voidPtrSize < returnTypeSize) {
1866 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1867 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00001868 if(StNotNull)
Ted Kremenekfe630b92009-04-09 05:45:56 +00001869 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001870 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001871 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001872 }
1873 }
Ted Kremeneka591bc02009-06-18 22:57:13 +00001874 else if (!StNotNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001875 // Handle the safe cases where the return value is 0 if the
1876 // receiver is nil.
1877 //
1878 // FIXME: For now take the conservative approach that we only
1879 // return null values if we *know* that the receiver is nil.
1880 // This is because we can have surprises like:
1881 //
1882 // ... = [[NSScreens screens] objectAtIndex:0];
1883 //
1884 // What can happen is that [... screens] could return nil, but
1885 // it most likely isn't nil. We should assume the semantics
1886 // of this case unless we have *a lot* more knowledge.
1887 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001888 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenek3ff12592009-06-19 17:10:32 +00001889 MakeNode(Dst, ME, Pred, StNull->bindExpr(ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001890 return;
1891 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001892 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001893 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001894 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001895 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001896 // of this method should assume that the receiver is not nil.
1897 if (!StNotNull)
1898 return;
1899
Ted Kremenekda9ae602009-04-08 18:51:08 +00001900 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001901 }
1902
Ted Kremeneke448ab42008-05-01 18:33:28 +00001903 // Check if the "raise" message was sent.
1904 if (ME->getSelector() == RaiseSel)
1905 RaisesException = true;
1906 }
1907 else {
1908
1909 IdentifierInfo* ClsName = ME->getClassName();
1910 Selector S = ME->getSelector();
1911
1912 // Check for special instance methods.
1913
1914 if (!NSExceptionII) {
1915 ASTContext& Ctx = getContext();
1916
1917 NSExceptionII = &Ctx.Idents.get("NSException");
1918 }
1919
1920 if (ClsName == NSExceptionII) {
1921
1922 enum { NUM_RAISE_SELECTORS = 2 };
1923
1924 // Lazily create a cache of the selectors.
1925
1926 if (!NSExceptionInstanceRaiseSelectors) {
1927
1928 ASTContext& Ctx = getContext();
1929
1930 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1931
1932 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1933 unsigned idx = 0;
1934
1935 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001936 II.push_back(&Ctx.Idents.get("raise"));
1937 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001938 NSExceptionInstanceRaiseSelectors[idx++] =
1939 Ctx.Selectors.getSelector(II.size(), &II[0]);
1940
1941 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001942 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001943 NSExceptionInstanceRaiseSelectors[idx++] =
1944 Ctx.Selectors.getSelector(II.size(), &II[0]);
1945 }
1946
1947 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1948 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1949 RaisesException = true; break;
1950 }
1951 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001952 }
1953
1954 // Check for any arguments that are uninitialized/undefined.
1955
1956 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1957 I != E; ++I) {
1958
Ted Kremenek3ff12592009-06-19 17:10:32 +00001959 if (state->getSVal(*I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001960
1961 // Generate an error node for passing an uninitialized/undefined value
1962 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001963 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001964
1965 if (N) {
1966 N->markAsSink();
1967 MsgExprUndefArgs[N] = *I;
1968 }
1969
1970 return;
1971 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001972 }
1973
1974 // Check if we raise an exception. For now treat these as sinks. Eventually
1975 // we will want to handle exceptions properly.
1976
1977 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1978
1979 if (RaisesException)
1980 Builder->BuildSinks = true;
1981
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001982 // Dispatch to plug-in transfer function.
1983
1984 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001985 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001986
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001987 EvalObjCMessageExpr(Dst, ME, Pred);
1988
1989 // Handle the case where no nodes where generated. Auto-generate that
1990 // contains the updated state if we aren't generating sinks.
1991
Ted Kremenekb0533962008-04-18 20:35:30 +00001992 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001993 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001994}
1995
1996//===----------------------------------------------------------------------===//
1997// Transfer functions: Miscellaneous statements.
1998//===----------------------------------------------------------------------===//
1999
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002000void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
2001 QualType PtrTy,
2002 Expr* CastE, NodeTy* Pred,
2003 NodeSet& Dst) {
2004 if (!V.isUnknownOrUndef()) {
2005 // FIXME: Determine if the number of bits of the target type is
2006 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00002007 // If not, flag an error.
Ted Kremenek3ff12592009-06-19 17:10:32 +00002008 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, EvalCast(cast<Loc>(V),
Ted Kremenekac78d6b2009-03-05 03:44:53 +00002009 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002010 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002011 else
Ted Kremenek3ff12592009-06-19 17:10:32 +00002012 MakeNode(Dst, CastE, Pred, state->bindExpr(CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002013}
2014
2015
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002016void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002017 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002018 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00002019 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00002020
Zhongxing Xud3118bd2008-10-31 07:26:14 +00002021 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00002022 T = ExCast->getTypeAsWritten();
2023
Zhongxing Xued340f72008-10-22 08:02:16 +00002024 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002025 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00002026 else
2027 Visit(Ex, Pred, S1);
2028
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002029 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00002030 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002031 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00002032 Dst.Add(*I1);
2033
Ted Kremenek874d63f2008-01-24 02:02:54 +00002034 return;
2035 }
2036
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002037 // FIXME: The rest of this should probably just go into EvalCall, and
2038 // let the transfer function object be responsible for constructing
2039 // nodes.
2040
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002041 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00002042 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002043 const GRState* state = GetState(N);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002044 SVal V = state->getSVal(Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00002045 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002046
2047 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002048 if (V.isUnknown()) {
2049 Dst.Add(N);
2050 continue;
2051 }
2052
2053 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00002054 if (V.isUndef())
2055 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002056
2057 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00002058 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00002059 C.getCanonicalType(ExTy).getUnqualifiedType())
2060 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002061
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002062 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002063 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002064 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002065 continue;
2066 }
2067
2068 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00002069 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002070 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002071 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002072 V = LV->getLoc();
Ted Kremenek3ff12592009-06-19 17:10:32 +00002073 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002074 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00002075 }
Ted Kremeneke121da42009-03-05 03:42:31 +00002076
Ted Kremenekc5302912009-03-05 20:22:13 +00002077 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00002078 }
2079
2080 // Just pass through function and block pointers.
2081 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
2082 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00002083 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00002084 }
2085
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002086 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00002087 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002088 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00002089 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002090
2091 // Are we casting from an array to a pointer? If so just pass on
2092 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00002093 if (T->isPointerType())
2094 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00002095
2096 // Are we casting from an array to an integer? If so, cast the decayed
2097 // pointer value to an integer.
2098 assert(T->isIntegerType());
2099 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
2100 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002101 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00002102 continue;
2103 }
2104
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002105 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00002106 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
2107 // FIXME: For TypedViewRegions, we should handle the case where the
2108 // underlying symbolic pointer is a function pointer or
2109 // block pointer.
2110
2111 // FIXME: We should handle the case where we strip off view layers to get
2112 // to a desugared type.
2113
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002114 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00002115 // We get a symbolic function pointer for a dereference of a function
2116 // pointer, but it is of function type. Example:
2117
2118 // struct FPRec {
2119 // void (*my_func)(int * x);
2120 // };
2121 //
2122 // int bar(int x);
2123 //
2124 // int f1_a(struct FPRec* foo) {
2125 // int x;
2126 // (*foo->my_func)(&x);
2127 // return bar(x)+1; // no-warning
2128 // }
2129
2130 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002131
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002132 const MemRegion* R = RV->getRegion();
2133 StoreManager& StoreMgr = getStoreManager();
2134
2135 // Delegate to store manager to get the result of casting a region
2136 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002137 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002138
2139 // Inspect the result. If the MemRegion* returned is NULL, this
2140 // expression evaluates to UnknownVal.
2141 R = Res.getRegion();
2142 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2143
2144 // Generate the new node in the ExplodedGraph.
Ted Kremenek3ff12592009-06-19 17:10:32 +00002145 MakeNode(Dst, CastE, N, Res.getState()->bindExpr(CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002146 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002147 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002148 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002149 DispatchCast: {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002150 MakeNode(Dst, CastE, N, state->bindExpr(CastE,
2151 EvalCast(V, CastE->getType())));
Ted Kremenekc5302912009-03-05 20:22:13 +00002152 continue;
2153 }
2154
2155 PassThrough: {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002156 MakeNode(Dst, CastE, N, state->bindExpr(CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00002157 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002158 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002159}
2160
Ted Kremenek4f090272008-10-27 21:54:31 +00002161void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002162 NodeTy* Pred, NodeSet& Dst,
2163 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002164 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2165 NodeSet Tmp;
2166 Visit(ILE, Pred, Tmp);
2167
2168 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002169 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002170 SVal ILV = state->getSVal(ILE);
2171 state = state->bindCompoundLiteral(CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002172
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002173 if (asLValue)
Ted Kremenek3ff12592009-06-19 17:10:32 +00002174 MakeNode(Dst, CL, *I, state->bindExpr(CL, state->getLValue(CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002175 else
Ted Kremenek3ff12592009-06-19 17:10:32 +00002176 MakeNode(Dst, CL, *I, state->bindExpr(CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002177 }
2178}
2179
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002180void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002181
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002182 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002183 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002184
2185 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002186 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002187
Ted Kremenekefd59942008-12-08 22:47:34 +00002188 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002189 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002190
2191 // FIXME: static variables may have an initializer, but the second
2192 // time a function is called those values may not be current.
2193 NodeSet Tmp;
2194
Ted Kremenekaf337412008-11-12 19:24:17 +00002195 if (InitEx)
2196 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002197
2198 if (Tmp.empty())
2199 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002200
2201 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002202 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002203 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002204
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002205 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2206 QualType T = getContext().getCanonicalType(VD->getType());
2207 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2208 // FIXME: Handle multi-dimensional VLAs.
2209
2210 Expr* SE = VLA->getSizeExpr();
Ted Kremenek3ff12592009-06-19 17:10:32 +00002211 SVal Size = state->getSVal(SE);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002212
2213 if (Size.isUndef()) {
2214 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2215 N->markAsSink();
2216 ExplicitBadSizedVLA.insert(N);
2217 }
2218 continue;
2219 }
2220
Ted Kremeneka591bc02009-06-18 22:57:13 +00002221 const GRState* zeroState = state->assume(Size, false);
2222 state = state->assume(Size, true);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002223
Ted Kremeneka591bc02009-06-18 22:57:13 +00002224 if (zeroState) {
2225 if (NodeTy* N = Builder->generateNode(DS, zeroState, Pred)) {
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002226 N->markAsSink();
Ted Kremeneka591bc02009-06-18 22:57:13 +00002227 if (state)
2228 ImplicitBadSizedVLA.insert(N);
2229 else
2230 ExplicitBadSizedVLA.insert(N);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002231 }
2232 }
2233
Ted Kremeneka591bc02009-06-18 22:57:13 +00002234 if (!state)
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002235 continue;
2236 }
2237
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002238 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002239 if (InitEx) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002240 SVal InitVal = state->getSVal(InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002241 QualType T = VD->getType();
2242
2243 // Recover some path-sensitivity if a scalar value evaluated to
2244 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002245 if (InitVal.isUnknown() ||
2246 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002247 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002248 }
2249
Ted Kremenekf3b09752009-06-23 20:27:53 +00002250 state = state->bindDecl(VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002251
2252 // The next thing to do is check if the GRTransferFuncs object wants to
2253 // update the state based on the new binding. If the GRTransferFunc
2254 // object doesn't do anything, just auto-propagate the current state.
2255 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
Ted Kremenek25e751a2009-06-23 21:37:46 +00002256 getTF().EvalBind(BuilderRef, loc::MemRegionVal(state->getRegion(VD)),
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002257 InitVal);
2258 }
2259 else {
Ted Kremenekf3b09752009-06-23 20:27:53 +00002260 state = state->bindDeclWithNoInit(VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002261 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002262 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002263 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002264}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002265
Ted Kremenekf75b1862008-10-30 17:47:32 +00002266namespace {
2267 // This class is used by VisitInitListExpr as an item in a worklist
2268 // for processing the values contained in an InitListExpr.
2269class VISIBILITY_HIDDEN InitListWLItem {
2270public:
2271 llvm::ImmutableList<SVal> Vals;
2272 GRExprEngine::NodeTy* N;
2273 InitListExpr::reverse_iterator Itr;
2274
2275 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2276 InitListExpr::reverse_iterator itr)
2277 : Vals(vals), N(n), Itr(itr) {}
2278};
2279}
2280
2281
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002282void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2283 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002284
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002285 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002286 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002287 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002288
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002289 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002290
Ted Kremeneka49e3672008-10-30 23:14:36 +00002291 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002292
Ted Kremeneka49e3672008-10-30 23:14:36 +00002293 // Handle base case where the initializer has no elements.
2294 // e.g: static int* myArray[] = {};
2295 if (NumInitElements == 0) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00002296 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002297 MakeNode(Dst, E, Pred, state->bindExpr(E, V));
Ted Kremeneka49e3672008-10-30 23:14:36 +00002298 return;
2299 }
2300
2301 // Create a worklist to process the initializers.
2302 llvm::SmallVector<InitListWLItem, 10> WorkList;
2303 WorkList.reserve(NumInitElements);
2304 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002305 InitListExpr::reverse_iterator ItrEnd = E->rend();
2306
Ted Kremeneka49e3672008-10-30 23:14:36 +00002307 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002308 while (!WorkList.empty()) {
2309 InitListWLItem X = WorkList.back();
2310 WorkList.pop_back();
2311
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002312 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002313 Visit(*X.Itr, X.N, Tmp);
2314
2315 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002316
Ted Kremenekf75b1862008-10-30 17:47:32 +00002317 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2318 // Get the last initializer value.
2319 state = GetState(*NI);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002320 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002321
2322 // Construct the new list of values by prepending the new value to
2323 // the already constructed list.
2324 llvm::ImmutableList<SVal> NewVals =
2325 getBasicVals().consVals(InitV, X.Vals);
2326
2327 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002328 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xud91ee272009-06-23 09:02:15 +00002329 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002330
Ted Kremenekf75b1862008-10-30 17:47:32 +00002331 // Make final state and node.
Ted Kremenek3ff12592009-06-19 17:10:32 +00002332 MakeNode(Dst, E, *NI, state->bindExpr(E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002333 }
2334 else {
2335 // Still some initializer values to go. Push them onto the worklist.
2336 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2337 }
2338 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002339 }
Ted Kremenek87903072008-10-30 18:34:31 +00002340
2341 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002342 }
2343
Ted Kremenek062e2f92008-11-13 06:10:40 +00002344 if (T->isUnionType() || T->isVectorType()) {
2345 // FIXME: to be implemented.
2346 // Note: That vectors can return true for T->isIntegerType()
2347 MakeNode(Dst, E, Pred, state);
2348 return;
2349 }
2350
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002351 if (Loc::IsLocType(T) || T->isIntegerType()) {
2352 assert (E->getNumInits() == 1);
2353 NodeSet Tmp;
2354 Expr* Init = E->getInit(0);
2355 Visit(Init, Pred, Tmp);
2356 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2357 state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002358 MakeNode(Dst, E, *I, state->bindExpr(E, state->getSVal(Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002359 }
2360 return;
2361 }
2362
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002363
2364 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2365 assert(0 && "unprocessed InitListExpr type");
2366}
Ted Kremenekf233d482008-02-05 00:26:40 +00002367
Sebastian Redl05189992008-11-11 17:56:53 +00002368/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2369void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2370 NodeTy* Pred,
2371 NodeSet& Dst) {
2372 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002373 uint64_t amt;
2374
2375 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002376 if (T == getContext().VoidTy) {
2377 // sizeof(void) == 1 byte.
2378 amt = 1;
2379 }
2380 else if (!T.getTypePtr()->isConstantSizeType()) {
2381 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002382 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002383 }
2384 else if (T->isObjCInterfaceType()) {
2385 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2386 // the compiler has laid out its representation. Just report Unknown
2387 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002388 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002389 }
2390 else {
2391 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002392 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002393 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002394 }
2395 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002396 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002397
Ted Kremenek0e561a32008-03-21 21:30:14 +00002398 MakeNode(Dst, Ex, Pred,
Zhongxing Xud91ee272009-06-23 09:02:15 +00002399 GetState(Pred)->bindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002400}
2401
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002402
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002403void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002404 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002405
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002406 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002407
2408 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002409 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002410
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002411 case UnaryOperator::Deref: {
2412
2413 Expr* Ex = U->getSubExpr()->IgnoreParens();
2414 NodeSet Tmp;
2415 Visit(Ex, Pred, Tmp);
2416
2417 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002418
Ted Kremeneka8538d92009-02-13 01:45:31 +00002419 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002420 SVal location = state->getSVal(Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002421
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002422 if (asLValue)
Ted Kremenek3ff12592009-06-19 17:10:32 +00002423 MakeNode(Dst, U, *I, state->bindExpr(U, location),
Ted Kremenek7090d542009-05-07 18:27:16 +00002424 ProgramPoint::PostLValueKind);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002425 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002426 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002427 }
2428
2429 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002430 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002431
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002432 case UnaryOperator::Real: {
2433
2434 Expr* Ex = U->getSubExpr()->IgnoreParens();
2435 NodeSet Tmp;
2436 Visit(Ex, Pred, Tmp);
2437
2438 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2439
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002440 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002441 if (Ex->getType()->isAnyComplexType()) {
2442 // Just report "Unknown."
2443 Dst.Add(*I);
2444 continue;
2445 }
2446
2447 // For all other types, UnaryOperator::Real is an identity operation.
2448 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002449 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002450 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002451 }
2452
2453 return;
2454 }
2455
2456 case UnaryOperator::Imag: {
2457
2458 Expr* Ex = U->getSubExpr()->IgnoreParens();
2459 NodeSet Tmp;
2460 Visit(Ex, Pred, Tmp);
2461
2462 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002463 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002464 if (Ex->getType()->isAnyComplexType()) {
2465 // Just report "Unknown."
2466 Dst.Add(*I);
2467 continue;
2468 }
2469
2470 // For all other types, UnaryOperator::Float returns 0.
2471 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002472 const GRState* state = GetState(*I);
Zhongxing Xud91ee272009-06-23 09:02:15 +00002473 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek3ff12592009-06-19 17:10:32 +00002474 MakeNode(Dst, U, *I, state->bindExpr(U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002475 }
2476
2477 return;
2478 }
2479
2480 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002481 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002482 Dst.Add(Pred);
2483 return;
2484
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002485 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002486 case UnaryOperator::Extension: {
2487
2488 // Unary "+" is a no-op, similar to a parentheses. We still have places
2489 // where it may be a block-level expression, so we need to
2490 // generate an extra node that just propagates the value of the
2491 // subexpression.
2492
2493 Expr* Ex = U->getSubExpr()->IgnoreParens();
2494 NodeSet Tmp;
2495 Visit(Ex, Pred, Tmp);
2496
2497 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002498 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002499 MakeNode(Dst, U, *I, state->bindExpr(U, state->getSVal(Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002500 }
2501
2502 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002503 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002504
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002505 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002506
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002507 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002508 Expr* Ex = U->getSubExpr()->IgnoreParens();
2509 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002510 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002511
2512 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002513 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002514 SVal V = state->getSVal(Ex);
2515 state = state->bindExpr(U, V);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002516 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002517 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002518
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002519 return;
2520 }
2521
2522 case UnaryOperator::LNot:
2523 case UnaryOperator::Minus:
2524 case UnaryOperator::Not: {
2525
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002526 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002527 Expr* Ex = U->getSubExpr()->IgnoreParens();
2528 NodeSet Tmp;
2529 Visit(Ex, Pred, Tmp);
2530
2531 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002532 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002533
2534 // Get the value of the subexpression.
Ted Kremenek3ff12592009-06-19 17:10:32 +00002535 SVal V = state->getSVal(Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002536
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002537 if (V.isUnknownOrUndef()) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002538 MakeNode(Dst, U, *I, state->bindExpr(U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002539 continue;
2540 }
2541
Ted Kremenek60595da2008-11-15 04:01:56 +00002542// QualType DstT = getContext().getCanonicalType(U->getType());
2543// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2544//
2545// if (DstT != SrcT) // Perform promotions.
2546// V = EvalCast(V, DstT);
2547//
2548// if (V.isUnknownOrUndef()) {
2549// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2550// continue;
2551// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002552
2553 switch (U->getOpcode()) {
2554 default:
2555 assert(false && "Invalid Opcode.");
2556 break;
2557
2558 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002559 // FIXME: Do we need to handle promotions?
Ted Kremenek3ff12592009-06-19 17:10:32 +00002560 state = state->bindExpr(U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002561 break;
2562
2563 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002564 // FIXME: Do we need to handle promotions?
Ted Kremenek3ff12592009-06-19 17:10:32 +00002565 state = state->bindExpr(U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002566 break;
2567
2568 case UnaryOperator::LNot:
2569
2570 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2571 //
2572 // Note: technically we do "E == 0", but this is the same in the
2573 // transfer functions as "0 == E".
2574
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002575 if (isa<Loc>(V)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +00002576 Loc X = ValMgr.makeNull();
Zhongxing Xu262fd032009-05-20 09:00:16 +00002577 SVal Result = EvalBinOp(state,BinaryOperator::EQ, cast<Loc>(V), X,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002578 U->getType());
Ted Kremenek3ff12592009-06-19 17:10:32 +00002579 state = state->bindExpr(U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002580 }
2581 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002582 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002583#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002584 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002585 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002586#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002587 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2588 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002589 continue;
2590#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002591 }
2592
2593 break;
2594 }
2595
Ted Kremeneka8538d92009-02-13 01:45:31 +00002596 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002597 }
2598
2599 return;
2600 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002601 }
2602
2603 // Handle ++ and -- (both pre- and post-increment).
2604
2605 assert (U->isIncrementDecrementOp());
2606 NodeSet Tmp;
2607 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002608 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002609
2610 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2611
Ted Kremeneka8538d92009-02-13 01:45:31 +00002612 const GRState* state = GetState(*I);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002613 SVal V1 = state->getSVal(Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002614
2615 // Perform a load.
2616 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002617 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002618
2619 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2620
Ted Kremeneka8538d92009-02-13 01:45:31 +00002621 state = GetState(*I2);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002622 SVal V2 = state->getSVal(Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002623
2624 // Propagate unknown and undefined values.
2625 if (V2.isUnknownOrUndef()) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002626 MakeNode(Dst, U, *I2, state->bindExpr(U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002627 continue;
2628 }
2629
Ted Kremenek21028dd2009-03-11 03:54:24 +00002630 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002631 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2632 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002633
Zhongxing Xud91ee272009-06-23 09:02:15 +00002634 SVal Result = EvalBinOp(state, Op, V2, ValMgr.makeIntVal(1U,U->getType()),
Zhongxing Xu262fd032009-05-20 09:00:16 +00002635 U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002636
2637 // Conjure a new symbol if necessary to recover precision.
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002638 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002639 Result = ValMgr.getConjuredSymbolVal(Ex,
2640 Builder->getCurrentBlockCount());
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002641
2642 // If the value is a location, ++/-- should always preserve
2643 // non-nullness. Check if the original value was non-null, and if so propagate
2644 // that constraint.
2645 if (Loc::IsLocType(U->getType())) {
Zhongxing Xu262fd032009-05-20 09:00:16 +00002646 SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002647 ValMgr.makeZeroVal(U->getType()),
2648 getContext().IntTy);
2649
Ted Kremeneka591bc02009-06-18 22:57:13 +00002650 if (!state->assume(Constraint, true)) {
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002651 // It isn't feasible for the original value to be null.
2652 // Propagate this constraint.
Zhongxing Xu262fd032009-05-20 09:00:16 +00002653 Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002654 ValMgr.makeZeroVal(U->getType()),
2655 getContext().IntTy);
2656
Ted Kremeneka591bc02009-06-18 22:57:13 +00002657 state = state->assume(Constraint, false);
2658 assert(state);
Ted Kremenekaf48fdd2009-04-21 22:38:05 +00002659 }
2660 }
2661 }
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002662
Ted Kremeneka591bc02009-06-18 22:57:13 +00002663 state = state->bindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002664
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002665 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002666 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002667 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002668 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002669}
2670
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002671void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2672 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2673}
2674
2675void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2676 AsmStmt::outputs_iterator I,
2677 AsmStmt::outputs_iterator E,
2678 NodeTy* Pred, NodeSet& Dst) {
2679 if (I == E) {
2680 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2681 return;
2682 }
2683
2684 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002685 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002686
2687 ++I;
2688
2689 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2690 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2691}
2692
2693void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2694 AsmStmt::inputs_iterator I,
2695 AsmStmt::inputs_iterator E,
2696 NodeTy* Pred, NodeSet& Dst) {
2697 if (I == E) {
2698
2699 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002700 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002701
2702 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2703 // which interprets the inline asm and stores proper results in the
2704 // outputs.
2705
Ted Kremeneka8538d92009-02-13 01:45:31 +00002706 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002707
2708 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2709 OE = A->end_outputs(); OI != OE; ++OI) {
2710
Ted Kremenek3ff12592009-06-19 17:10:32 +00002711 SVal X = state->getSVal(*OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002712 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002713
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002714 if (isa<Loc>(X))
Ted Kremenek3ff12592009-06-19 17:10:32 +00002715 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002716 }
2717
Ted Kremeneka8538d92009-02-13 01:45:31 +00002718 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002719 return;
2720 }
2721
2722 NodeSet Tmp;
2723 Visit(*I, Pred, Tmp);
2724
2725 ++I;
2726
2727 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2728 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2729}
2730
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002731void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2732 assert (Builder && "GRStmtNodeBuilder must be defined.");
2733
2734 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002735
Ted Kremenek186350f2008-04-23 20:12:28 +00002736 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2737 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002738
Ted Kremenek729a9a22008-07-17 23:15:45 +00002739 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002740
Ted Kremenekb0533962008-04-18 20:35:30 +00002741 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002742
Ted Kremenekb0533962008-04-18 20:35:30 +00002743 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002744 MakeNode(Dst, S, Pred, GetState(Pred));
2745}
2746
Ted Kremenek02737ed2008-03-31 15:02:58 +00002747void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2748
2749 Expr* R = S->getRetValue();
2750
2751 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002752 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002753 return;
2754 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002755
Ted Kremenek5917d782008-11-21 00:27:44 +00002756 NodeSet Tmp;
2757 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002758
Ted Kremenek5917d782008-11-21 00:27:44 +00002759 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002760 SVal X = (*I)->getState()->getSVal(R);
Ted Kremenek5917d782008-11-21 00:27:44 +00002761
2762 // Check if we return the address of a stack variable.
2763 if (isa<loc::MemRegionVal>(X)) {
2764 // Determine if the value is on the stack.
2765 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002766
Ted Kremenekea20cd72009-06-23 18:05:21 +00002767 if (R && R->hasStackStorage()) {
Ted Kremenek5917d782008-11-21 00:27:44 +00002768 // Create a special node representing the error.
2769 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2770 N->markAsSink();
2771 RetsStackAddr.insert(N);
2772 }
2773 continue;
2774 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002775 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002776 // Check if we return an undefined value.
2777 else if (X.isUndef()) {
2778 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2779 N->markAsSink();
2780 RetsUndef.insert(N);
2781 }
2782 continue;
2783 }
2784
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002785 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002786 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002787}
Ted Kremenek55deb972008-03-25 00:34:37 +00002788
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002789//===----------------------------------------------------------------------===//
2790// Transfer functions: Binary operators.
2791//===----------------------------------------------------------------------===//
2792
Ted Kremeneka8538d92009-02-13 01:45:31 +00002793const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002794 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002795
2796 // Divide by undefined? (potentially zero)
2797
2798 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002799 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002800
2801 if (DivUndef) {
2802 DivUndef->markAsSink();
2803 ExplicitBadDivides.insert(DivUndef);
2804 }
2805
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002806 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002807 }
2808
2809 // Check for divide/remainder-by-zero.
2810 // First, "assume" that the denominator is 0 or undefined.
Ted Kremeneka591bc02009-06-18 22:57:13 +00002811 const GRState* zeroState = state->assume(Denom, false);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002812
2813 // Second, "assume" that the denominator cannot be 0.
Ted Kremeneka591bc02009-06-18 22:57:13 +00002814 state = state->assume(Denom, true);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002815
Ted Kremeneka591bc02009-06-18 22:57:13 +00002816 // Create the node for the divide-by-zero (if it occurred).
2817 if (zeroState)
2818 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, zeroState, Pred)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002819 DivZeroNode->markAsSink();
2820
Ted Kremeneka591bc02009-06-18 22:57:13 +00002821 if (state)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002822 ImplicitBadDivides.insert(DivZeroNode);
2823 else
2824 ExplicitBadDivides.insert(DivZeroNode);
2825
2826 }
2827
Ted Kremeneka591bc02009-06-18 22:57:13 +00002828 return state;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002829}
2830
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002831void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002832 GRExprEngine::NodeTy* Pred,
2833 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002834
2835 NodeSet Tmp1;
2836 Expr* LHS = B->getLHS()->IgnoreParens();
2837 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002838
Ted Kremenek759623e2008-12-06 02:39:30 +00002839 // FIXME: Add proper support for ObjCKVCRefExpr.
2840 if (isa<ObjCKVCRefExpr>(LHS)) {
2841 Visit(RHS, Pred, Dst);
2842 return;
2843 }
2844
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002845 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002846 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002847 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002848 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002849
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002850 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002851
Ted Kremenek3ff12592009-06-19 17:10:32 +00002852 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002853
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002854 // Process the RHS.
2855
2856 NodeSet Tmp2;
2857 Visit(RHS, *I1, Tmp2);
2858
2859 // With both the LHS and RHS evaluated, process the operation itself.
2860
2861 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002862
Ted Kremeneka8538d92009-02-13 01:45:31 +00002863 const GRState* state = GetState(*I2);
2864 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002865
Ted Kremenek3ff12592009-06-19 17:10:32 +00002866 SVal RightV = state->getSVal(RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002867 BinaryOperator::Opcode Op = B->getOpcode();
2868
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002869 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002870
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002871 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002872
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002873 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002874 // FIXME: Handle structs.
2875 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002876
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002877 if ((RightV.isUnknown() ||
2878 !getConstraintManager().canReasonAbout(RightV))
2879 && (Loc::IsLocType(T) ||
2880 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002881 unsigned Count = Builder->getCurrentBlockCount();
2882 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002883 }
2884
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002885 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002886 // to the L-Value represented by the LHS.
Ted Kremenek3ff12592009-06-19 17:10:32 +00002887 EvalStore(Dst, B, LHS, *I2, state->bindExpr(B, RightV), LeftV,
Ted Kremeneka8538d92009-02-13 01:45:31 +00002888 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002889 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002890 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002891
2892 case BinaryOperator::Div:
2893 case BinaryOperator::Rem:
2894
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002895 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002896 if (RHS->getType()->isIntegerType() &&
2897 RHS->getType()->isScalarType()) {
2898
Ted Kremeneka8538d92009-02-13 01:45:31 +00002899 state = CheckDivideZero(B, state, *I2, RightV);
2900 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002901 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002902
2903 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002904
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002905 default: {
2906
2907 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002908 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002909
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002910 // Process non-assignements except commas or short-circuited
2911 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002912
Zhongxing Xu262fd032009-05-20 09:00:16 +00002913 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002914
2915 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002916 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002917 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002918 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002919 }
2920 else
2921 Dst.Add(*I2);
2922
Ted Kremenek89063af2008-02-21 19:15:37 +00002923 continue;
2924 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002925
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002926 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002927
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002928 // The operands were *not* undefined, but the result is undefined.
2929 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002930
Ted Kremeneka8538d92009-02-13 01:45:31 +00002931 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002932 UndefNode->markAsSink();
2933 UndefResults.insert(UndefNode);
2934 }
2935
2936 continue;
2937 }
2938
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002939 // Otherwise, create a new node.
2940
Ted Kremenek3ff12592009-06-19 17:10:32 +00002941 MakeNode(Dst, B, *I2, state->bindExpr(B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002942 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002943 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002944 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002945
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002946 assert (B->isCompoundAssignmentOp());
2947
Ted Kremenekcad29962009-02-07 00:52:24 +00002948 switch (Op) {
2949 default:
2950 assert(0 && "Invalid opcode for compound assignment.");
2951 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2952 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2953 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2954 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2955 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2956 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2957 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2958 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2959 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2960 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002961 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002962
2963 // Perform a load (the LHS). This performs the checks for
2964 // null dereferences, and so on.
2965 NodeSet Tmp3;
Ted Kremenek3ff12592009-06-19 17:10:32 +00002966 SVal location = state->getSVal(LHS);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002967 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002968
2969 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2970
Ted Kremeneka8538d92009-02-13 01:45:31 +00002971 state = GetState(*I3);
Ted Kremenek3ff12592009-06-19 17:10:32 +00002972 SVal V = state->getSVal(LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002973
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002974 // Check for divide-by-zero.
2975 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002976 && RHS->getType()->isIntegerType()
2977 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002978
2979 // CheckDivideZero returns a new state where the denominator
2980 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002981 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002982
Ted Kremeneka8538d92009-02-13 01:45:31 +00002983 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002984 continue;
2985 }
2986
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002987 // Propagate undefined values (left-side).
2988 if (V.isUndef()) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002989 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002990 continue;
2991 }
2992
2993 // Propagate unknown values (left and right-side).
2994 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremenek3ff12592009-06-19 17:10:32 +00002995 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, UnknownVal()),
Ted Kremeneka8538d92009-02-13 01:45:31 +00002996 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002997 continue;
2998 }
2999
3000 // At this point:
3001 //
3002 // The LHS is not Undef/Unknown.
3003 // The RHS is not Unknown.
3004
3005 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00003006 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00003007 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00003008
3009 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
3010 CLHSTy = getContext().getCanonicalType(CTy);
3011
Ted Kremenek60595da2008-11-15 04:01:56 +00003012 QualType LTy = getContext().getCanonicalType(LHS->getType());
3013 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00003014
3015 // Promote LHS.
3016 V = EvalCast(V, CLHSTy);
3017
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003018 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00003019 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00003020 // Propagate undefined values (right-side).
Ted Kremenek3ff12592009-06-19 17:10:32 +00003021 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003022 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003023 continue;
3024 }
3025
Ted Kremenek60595da2008-11-15 04:01:56 +00003026 // Compute the result of the operation.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003027 SVal Result = EvalCast(EvalBinOp(state, Op, V, RightV, CTy),
3028 B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003029
3030 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003031 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00003032 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003033 UndefNode->markAsSink();
3034 UndefResults.insert(UndefNode);
3035 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003036 continue;
3037 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003038
3039 // EXPERIMENTAL: "Conjured" symbols.
3040 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00003041
3042 SVal LHSVal;
3043
Ted Kremenek276c6ac2009-03-11 02:24:48 +00003044 if ((Result.isUnknown() ||
3045 !getConstraintManager().canReasonAbout(Result))
3046 && (Loc::IsLocType(CTy)
3047 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00003048
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003049 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003050
Ted Kremenek60595da2008-11-15 04:01:56 +00003051 // The symbolic value is actually for the type of the left-hand side
3052 // expression, not the computation type, as this is the value the
3053 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00003054 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00003055
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00003056 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00003057 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00003058 }
Ted Kremenek60595da2008-11-15 04:01:56 +00003059 else {
3060 // The left-hand side may bind to a different value then the
3061 // computation type.
3062 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
3063 }
3064
Ted Kremenek3ff12592009-06-19 17:10:32 +00003065 EvalStore(Dst, B, LHS, *I3, state->bindExpr(B, Result), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00003066 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00003067 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00003068 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003069 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00003070}
Ted Kremenekee985462008-01-16 18:18:48 +00003071
3072//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003073// Transfer-function Helpers.
3074//===----------------------------------------------------------------------===//
3075
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003076void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003077 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00003078 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003079 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003080
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003081 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003082 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003083
Ted Kremenek4adc81e2008-08-13 04:27:00 +00003084 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003085 MakeNode(Dst, Ex, Pred, *I);
3086}
3087
Ted Kremeneka8538d92009-02-13 01:45:31 +00003088void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00003089 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003090 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003091
Ted Kremeneka8538d92009-02-13 01:45:31 +00003092 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003093 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003094}
3095
Zhongxing Xu262fd032009-05-20 09:00:16 +00003096SVal GRExprEngine::EvalBinOp(const GRState* state, BinaryOperator::Opcode Op,
3097 SVal L, SVal R, QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003098
3099 if (L.isUndef() || R.isUndef())
3100 return UndefinedVal();
3101
3102 if (L.isUnknown() || R.isUnknown())
3103 return UnknownVal();
3104
3105 if (isa<Loc>(L)) {
3106 if (isa<Loc>(R))
3107 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
3108 else
Zhongxing Xu262fd032009-05-20 09:00:16 +00003109 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(L), cast<NonLoc>(R));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003110 }
3111
3112 if (isa<Loc>(R)) {
3113 // Support pointer arithmetic where the increment/decrement operand
3114 // is on the left and the pointer on the right.
3115
3116 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
3117
3118 // Commute the operands.
Zhongxing Xu262fd032009-05-20 09:00:16 +00003119 return getTF().EvalBinOp(*this, state, Op, cast<Loc>(R), cast<NonLoc>(L));
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003120 }
3121 else
3122 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00003123 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00003124}
3125
Ted Kremenekdf7533b2008-07-17 21:27:31 +00003126//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00003127// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00003128//===----------------------------------------------------------------------===//
3129
Ted Kremenekaa66a322008-01-16 21:46:15 +00003130#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003131static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003132static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003133
Ted Kremenekaa66a322008-01-16 21:46:15 +00003134namespace llvm {
3135template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003136struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003137 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003138
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003139 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3140
3141 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003142 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003143 GraphPrintCheckerState->isUndefDeref(N) ||
3144 GraphPrintCheckerState->isUndefStore(N) ||
3145 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003146 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3147 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003148 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003149 GraphPrintCheckerState->isBadCall(N) ||
3150 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003151 return "color=\"red\",style=\"filled\"";
3152
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003153 if (GraphPrintCheckerState->isNoReturnCall(N))
3154 return "color=\"blue\",style=\"filled\"";
3155
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003156 return "";
3157 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003158
Owen Anderson02995ce2009-06-24 17:37:55 +00003159 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*,
3160 bool ShortNames) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +00003161
3162 std::string sbuf;
3163 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003164
3165 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003166 ProgramPoint Loc = N->getLocation();
3167
3168 switch (Loc.getKind()) {
3169 case ProgramPoint::BlockEntranceKind:
3170 Out << "Block Entrance: B"
3171 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3172 break;
3173
3174 case ProgramPoint::BlockExitKind:
3175 assert (false);
3176 break;
3177
Ted Kremenekaa66a322008-01-16 21:46:15 +00003178 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003179 if (isa<PostStmt>(Loc)) {
3180 const PostStmt& L = cast<PostStmt>(Loc);
3181 Stmt* S = L.getStmt();
3182 SourceLocation SLoc = S->getLocStart();
3183
3184 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Ted Kremenek53ba0b62009-06-24 23:06:47 +00003185 S->printPretty(Out);
Ted Kremenek8c354752008-12-16 22:02:27 +00003186
3187 if (SLoc.isFileID()) {
3188 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003189 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3190 << " col="
3191 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3192 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003193 }
3194
Ted Kremenek7090d542009-05-07 18:27:16 +00003195 if (isa<PostLoad>(Loc))
3196 Out << "\\lPostLoad\\l;";
3197 else if (isa<PostStore>(Loc))
3198 Out << "\\lPostStore\\l";
3199 else if (isa<PostLValue>(Loc))
3200 Out << "\\lPostLValue\\l";
3201 else if (isa<PostLocationChecksSucceed>(Loc))
3202 Out << "\\lPostLocationChecksSucceed\\l";
3203 else if (isa<PostNullCheckFailed>(Loc))
3204 Out << "\\lPostNullCheckFailed\\l";
3205
Ted Kremenek8c354752008-12-16 22:02:27 +00003206 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3207 Out << "\\|Implicit-Null Dereference.\\l";
3208 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3209 Out << "\\|Explicit-Null Dereference.\\l";
3210 else if (GraphPrintCheckerState->isUndefDeref(N))
3211 Out << "\\|Dereference of undefialied value.\\l";
3212 else if (GraphPrintCheckerState->isUndefStore(N))
3213 Out << "\\|Store to Undefined Loc.";
3214 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3215 Out << "\\|Explicit divide-by zero or undefined value.";
3216 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3217 Out << "\\|Implicit divide-by zero or undefined value.";
3218 else if (GraphPrintCheckerState->isUndefResult(N))
3219 Out << "\\|Result of operation is undefined.";
3220 else if (GraphPrintCheckerState->isNoReturnCall(N))
3221 Out << "\\|Call to function marked \"noreturn\".";
3222 else if (GraphPrintCheckerState->isBadCall(N))
3223 Out << "\\|Call to NULL/Undefined.";
3224 else if (GraphPrintCheckerState->isUndefArg(N))
3225 Out << "\\|Argument in call is undefined";
3226
3227 break;
3228 }
3229
Ted Kremenekaa66a322008-01-16 21:46:15 +00003230 const BlockEdge& E = cast<BlockEdge>(Loc);
3231 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3232 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003233
3234 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003235
3236 SourceLocation SLoc = T->getLocStart();
3237
Ted Kremenekb38911f2008-01-30 23:03:39 +00003238 Out << "\\|Terminator: ";
Ted Kremenek53ba0b62009-06-24 23:06:47 +00003239 E.getSrc()->printTerminator(Out);
Ted Kremenekb38911f2008-01-30 23:03:39 +00003240
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003241 if (SLoc.isFileID()) {
3242 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003243 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3244 << " col="
3245 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003246 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003247
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003248 if (isa<SwitchStmt>(T)) {
3249 Stmt* Label = E.getDst()->getLabel();
3250
3251 if (Label) {
3252 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3253 Out << "\\lcase ";
Ted Kremenek53ba0b62009-06-24 23:06:47 +00003254 C->getLHS()->printPretty(Out);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003255
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003256 if (Stmt* RHS = C->getRHS()) {
3257 Out << " .. ";
Ted Kremenek53ba0b62009-06-24 23:06:47 +00003258 RHS->printPretty(Out);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003259 }
3260
3261 Out << ":";
3262 }
3263 else {
3264 assert (isa<DefaultStmt>(Label));
3265 Out << "\\ldefault:";
3266 }
3267 }
3268 else
3269 Out << "\\l(implicit) default:";
3270 }
3271 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003272 // FIXME
3273 }
3274 else {
3275 Out << "\\lCondition: ";
3276 if (*E.getSrc()->succ_begin() == E.getDst())
3277 Out << "true";
3278 else
3279 Out << "false";
3280 }
3281
3282 Out << "\\l";
3283 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003284
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003285 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3286 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003287 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003288 }
3289 }
3290
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003291 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003292
Ted Kremenekb65be702009-06-18 01:23:53 +00003293 const GRState *state = N->getState();
3294 state->printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003295
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003296 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003297 return Out.str();
3298 }
3299};
3300} // end llvm namespace
3301#endif
3302
Ted Kremenekffe0f432008-03-07 22:58:01 +00003303#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003304template <typename ITERATOR>
3305GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3306
3307template <>
3308GRExprEngine::NodeTy*
3309GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3310 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3311 return I->first;
3312}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003313#endif
3314
3315void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003316#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003317 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003318 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003319
3320 // Flush any outstanding reports to make sure we cover all the nodes.
3321 // This does not cause them to get displayed.
3322 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3323 const_cast<BugType*>(*I)->FlushReports(BR);
3324
3325 // Iterate through the reports and get their nodes.
3326 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3327 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3328 const BugReportEquivClass& EQ = *I2;
3329 const BugReport &R = **EQ.begin();
3330 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3331 if (N) Src.push_back(N);
3332 }
3333 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003334
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003335 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003336 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003337 else {
3338 GraphPrintCheckerState = this;
3339 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003340
Ted Kremenekffe0f432008-03-07 22:58:01 +00003341 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003342
3343 GraphPrintCheckerState = NULL;
3344 GraphPrintSourceManager = NULL;
3345 }
3346#endif
3347}
3348
3349void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3350#ifndef NDEBUG
3351 GraphPrintCheckerState = this;
3352 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003353
Ted Kremenekcf118d42009-02-04 23:49:09 +00003354 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003355
Ted Kremenekcf118d42009-02-04 23:49:09 +00003356 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003357 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003358 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003359 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003360
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003361 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003362 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003363#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003364}