blob: 4fe6fd6b90ff093dc77f32aa5d2250a96ddc2921 [file] [log] [blame]
Ted Kremenek77349cb2008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremenek64924852008-01-31 02:35:41 +00002//
Ted Kremenek4af84312008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekd27f8162008-01-15 23:55:06 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek77349cb2008-02-14 22:13:12 +000010// This file defines a meta-engine for path-sensitive dataflow analysis that
11// is built on GREngine, but provides the boilerplate to execute transfer
12// functions and build the ExplodedGraph at the expression level.
Ted Kremenekd27f8162008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenekb930d7a2009-04-01 06:52:48 +000016#include "clang/AST/ParentMap.h"
Ted Kremenek77349cb2008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek41573eb2009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000019#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneke97ca062008-03-07 20:57:30 +000020#include "clang/Basic/SourceManager.h"
Ted Kremenek0bed8a12009-03-11 02:41:36 +000021#include "clang/Basic/PrettyStackTrace.h"
Ted Kremeneke01c9872008-02-14 22:36:46 +000022#include "llvm/Support/Streams.h"
Ted Kremenekbdb435d2008-07-11 18:37:32 +000023#include "llvm/ADT/ImmutableList.h"
24#include "llvm/Support/Compiler.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000026
Ted Kremenek0f5f0592008-02-27 06:07:00 +000027#ifndef NDEBUG
28#include "llvm/Support/GraphWriter.h"
29#include <sstream>
30#endif
31
Ted Kremenekb387a3f2008-02-14 22:16:04 +000032using namespace clang;
33using llvm::dyn_cast;
34using llvm::cast;
35using llvm::APSInt;
Ted Kremenekab2b8c52008-01-23 19:59:44 +000036
Ted Kremeneke695e1c2008-04-15 23:06:53 +000037//===----------------------------------------------------------------------===//
38// Engine construction and deletion.
39//===----------------------------------------------------------------------===//
40
Ted Kremenekbdb435d2008-07-11 18:37:32 +000041namespace {
42
43class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
44 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
45 typedef llvm::DenseMap<void*,Checks> MapTy;
46
47 MapTy M;
48 Checks::Factory F;
Ted Kremenek536aa022009-03-30 17:53:05 +000049 Checks AllStmts;
Ted Kremenekbdb435d2008-07-11 18:37:32 +000050
51public:
Ted Kremenek536aa022009-03-30 17:53:05 +000052 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
53 F(Alloc), AllStmts(F.GetEmptyList()) {}
Ted Kremenekbdb435d2008-07-11 18:37:32 +000054
55 virtual ~MappedBatchAuditor() {
56 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
57
58 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
59 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
60
61 GRSimpleAPICheck* check = *I;
62
63 if (AlreadyVisited.count(check))
64 continue;
65
66 AlreadyVisited.insert(check);
67 delete check;
68 }
69 }
70
Ted Kremenek536aa022009-03-30 17:53:05 +000071 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekbdb435d2008-07-11 18:37:32 +000072 assert (A && "Check cannot be null.");
73 void* key = reinterpret_cast<void*>((uintptr_t) C);
74 MapTy::iterator I = M.find(key);
75 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
76 }
Ted Kremenek536aa022009-03-30 17:53:05 +000077
78 void AddCheck(GRSimpleAPICheck *A) {
79 assert (A && "Check cannot be null.");
80 AllStmts = F.Concat(A, AllStmts);
81 }
Ted Kremenekcf118d42009-02-04 23:49:09 +000082
Ted Kremenek4adc81e2008-08-13 04:27:00 +000083 virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
Ted Kremenek536aa022009-03-30 17:53:05 +000084 // First handle the auditors that accept all statements.
85 bool isSink = false;
86 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
87 isSink |= (*I)->Audit(N, VMgr);
88
89 // Next handle the auditors that accept only specific statements.
Ted Kremenekbdb435d2008-07-11 18:37:32 +000090 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
91 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
92 MapTy::iterator MI = M.find(key);
Ted Kremenek536aa022009-03-30 17:53:05 +000093 if (MI != M.end()) {
94 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
95 isSink |= (*I)->Audit(N, VMgr);
96 }
Ted Kremenekbdb435d2008-07-11 18:37:32 +000097
Ted Kremenekbdb435d2008-07-11 18:37:32 +000098 return isSink;
99 }
100};
101
102} // end anonymous namespace
103
104//===----------------------------------------------------------------------===//
105// Engine construction and deletion.
106//===----------------------------------------------------------------------===//
107
Ted Kremeneke448ab42008-05-01 18:33:28 +0000108static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
109 IdentifierInfo* II = &Ctx.Idents.get(name);
110 return Ctx.Selectors.getSelector(0, &II);
111}
112
Ted Kremenekdaa497e2008-03-09 18:05:48 +0000113
Ted Kremenek8b233612008-07-02 20:13:38 +0000114GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
Ted Kremenekcf118d42009-02-04 23:49:09 +0000115 LiveVariables& L, BugReporterData& BRD,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000116 bool purgeDead, bool eagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000117 StoreManagerCreator SMC,
118 ConstraintManagerCreator CMC)
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000119 : CoreEngine(cfg, CD, Ctx, *this),
120 G(CoreEngine.getGraph()),
Ted Kremenek8b233612008-07-02 20:13:38 +0000121 Liveness(L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000122 Builder(NULL),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000123 StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000124 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenek8e5fb282009-04-09 16:46:55 +0000125 ValMgr(StateMgr.getValueManager()),
Ted Kremeneke448ab42008-05-01 18:33:28 +0000126 CurrentStmt(NULL),
Zhongxing Xua5a41662008-12-22 08:30:52 +0000127 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
128 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000129 PurgeDead(purgeDead),
Ted Kremenek48af2a92009-02-25 22:32:02 +0000130 BR(BRD, *this),
131 EagerlyAssume(eagerlyAssume) {}
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000132
Ted Kremenek1a654b62008-06-20 21:45:25 +0000133GRExprEngine::~GRExprEngine() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000134 BR.FlushReports();
Ted Kremeneke448ab42008-05-01 18:33:28 +0000135 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000136}
137
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000138//===----------------------------------------------------------------------===//
139// Utility methods.
140//===----------------------------------------------------------------------===//
141
Ted Kremenek186350f2008-04-23 20:12:28 +0000142
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000143void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek729a9a22008-07-17 23:15:45 +0000144 StateMgr.TF = tf;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145 tf->RegisterChecks(getBugReporter());
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000146 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000147}
148
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000149void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
150 if (!BatchAuditor)
151 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
152
153 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000154}
155
Ted Kremenek536aa022009-03-30 17:53:05 +0000156void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
157 if (!BatchAuditor)
158 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
159
160 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
161}
162
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000163const GRState* GRExprEngine::getInitialState() {
Ted Kremenek52e56022009-04-10 00:59:50 +0000164 const GRState *state = StateMgr.getInitialState();
165
166 // Precondition: the first argument of 'main' is an integer guaranteed
167 // to be > 0.
168 // FIXME: It would be nice if we had a more general mechanism to add
169 // such preconditions. Some day.
170 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(&StateMgr.getCodeDecl()))
171 if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
172 FD->getNumParams() > 0) {
173 const ParmVarDecl *PD = FD->getParamDecl(0);
174 QualType T = PD->getType();
175 if (T->isIntegerType())
176 if (const MemRegion *R = StateMgr.getRegion(PD)) {
177 SVal V = GetSVal(state, loc::MemRegionVal(R));
178 SVal Constraint = EvalBinOp(BinaryOperator::GT, V,
179 ValMgr.makeZeroVal(T),
180 getContext().IntTy);
181 bool isFeasible = false;
182 const GRState *newState = Assume(state, Constraint, true,
183 isFeasible);
184 if (newState) state = newState;
185 }
186 }
187
188 return state;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000189}
190
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000191//===----------------------------------------------------------------------===//
192// Top-level transfer function logic (Dispatcher).
193//===----------------------------------------------------------------------===//
194
195void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
196
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000197 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
198 S->getLocStart(),
199 "Error evaluating statement");
200
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000201 Builder = &builder;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000202 EntryNode = builder.getLastNode();
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000203
204 // FIXME: Consolidate.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000205 CurrentStmt = S;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000206 StateMgr.CurrentStmt = S;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000207
208 // Set up our simple checks.
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000209 if (BatchAuditor)
210 Builder->setAuditor(BatchAuditor.get());
Ted Kremenek241677a2009-01-21 22:26:05 +0000211
Ted Kremenekbdb435d2008-07-11 18:37:32 +0000212 // Create the cleaned state.
Ted Kremenek241677a2009-01-21 22:26:05 +0000213 SymbolReaper SymReaper(Liveness, SymMgr);
214 CleanedState = PurgeDead ? StateMgr.RemoveDeadBindings(EntryNode->getState(),
215 CurrentStmt, SymReaper)
216 : EntryNode->getState();
217
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000218 // Process any special transfer function for dead symbols.
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000219 NodeSet Tmp;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000220
Ted Kremenek241677a2009-01-21 22:26:05 +0000221 if (!SymReaper.hasDeadSymbols())
Ted Kremenek846d4e92008-04-24 23:35:58 +0000222 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000223 else {
224 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000225 SaveOr OldHasGen(Builder->HasGeneratedNode);
226
Ted Kremenek331b0ac2008-06-18 05:34:07 +0000227 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
228 Builder->PurgingDeadSymbols = true;
229
Ted Kremenek729a9a22008-07-17 23:15:45 +0000230 getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
Ted Kremenek241677a2009-01-21 22:26:05 +0000231 CleanedState, SymReaper);
Ted Kremenek846d4e92008-04-24 23:35:58 +0000232
233 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
234 Tmp.Add(EntryNode);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000235 }
Ted Kremenek846d4e92008-04-24 23:35:58 +0000236
237 bool HasAutoGenerated = false;
238
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000239 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek846d4e92008-04-24 23:35:58 +0000240
241 NodeSet Dst;
242
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000243 // Set the cleaned state.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000244 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
245
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000246 // Visit the statement.
Ted Kremenek846d4e92008-04-24 23:35:58 +0000247 Visit(S, *I, Dst);
248
249 // Do we need to auto-generate a node? We only need to do this to generate
250 // a node with a "cleaned" state; GRCoreEngine will actually handle
251 // auto-transitions for other cases.
252 if (Dst.size() == 1 && *Dst.begin() == EntryNode
253 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
254 HasAutoGenerated = true;
255 builder.generateNode(S, GetState(EntryNode), *I);
256 }
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000257 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000258
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000259 // NULL out these variables to cleanup.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000260 CleanedState = NULL;
Ted Kremenek846d4e92008-04-24 23:35:58 +0000261 EntryNode = NULL;
Ted Kremenekdf7533b2008-07-17 21:27:31 +0000262
263 // FIXME: Consolidate.
264 StateMgr.CurrentStmt = 0;
265 CurrentStmt = 0;
266
Ted Kremenek846d4e92008-04-24 23:35:58 +0000267 Builder = NULL;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000268}
269
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000270void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
271 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
272 S->getLocStart(),
273 "Error evaluating statement");
274
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000275 // FIXME: add metadata to the CFG so that we can disable
276 // this check when we KNOW that there is no block-level subexpression.
277 // The motivation is that this check requires a hashtable lookup.
278
279 if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
280 Dst.Add(Pred);
281 return;
282 }
283
284 switch (S->getStmtClass()) {
285
286 default:
287 // Cases we intentionally have "default" handle:
288 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
289
290 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
291 break;
Ted Kremenek540cbe22008-04-22 04:56:29 +0000292
293 case Stmt::ArraySubscriptExprClass:
294 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
295 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000296
297 case Stmt::AsmStmtClass:
298 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
299 break;
300
301 case Stmt::BinaryOperatorClass: {
302 BinaryOperator* B = cast<BinaryOperator>(S);
303
304 if (B->isLogicalOp()) {
305 VisitLogicalExpr(B, Pred, Dst);
306 break;
307 }
308 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000309 const GRState* state = GetState(Pred);
310 MakeNode(Dst, B, Pred, BindExpr(state, B, GetSVal(state, B->getRHS())));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000311 break;
312 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000313
Ted Kremenek48af2a92009-02-25 22:32:02 +0000314 if (EagerlyAssume && (B->isRelationalOp() || B->isEqualityOp())) {
315 NodeSet Tmp;
316 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
Ted Kremenekb2939022009-02-25 23:32:10 +0000317 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenek48af2a92009-02-25 22:32:02 +0000318 }
319 else
320 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
321
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000322 break;
323 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000324
Douglas Gregorb4609802008-11-14 16:09:21 +0000325 case Stmt::CallExprClass:
326 case Stmt::CXXOperatorCallExprClass: {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000327 CallExpr* C = cast<CallExpr>(S);
328 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000329 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000330 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +0000331
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000332 // FIXME: ChooseExpr is really a constant. We need to fix
333 // the CFG do not model them as explicit control-flow.
334
335 case Stmt::ChooseExprClass: { // __builtin_choose_expr
336 ChooseExpr* C = cast<ChooseExpr>(S);
337 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
338 break;
339 }
340
341 case Stmt::CompoundAssignOperatorClass:
342 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
343 break;
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000344
345 case Stmt::CompoundLiteralExprClass:
346 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
347 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000348
349 case Stmt::ConditionalOperatorClass: { // '?' operator
350 ConditionalOperator* C = cast<ConditionalOperator>(S);
351 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
352 break;
353 }
354
355 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000356 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000357 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000358 break;
359
360 case Stmt::DeclStmtClass:
361 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
362 break;
363
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000364 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000365 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000366 CastExpr* C = cast<CastExpr>(S);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000367 VisitCast(C, C->getSubExpr(), Pred, Dst);
368 break;
369 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +0000370
371 case Stmt::InitListExprClass:
372 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
373 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000374
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000375 case Stmt::MemberExprClass:
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000376 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
377 break;
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000378
379 case Stmt::ObjCIvarRefExprClass:
380 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
381 break;
Ted Kremenekaf337412008-11-12 19:24:17 +0000382
383 case Stmt::ObjCForCollectionStmtClass:
384 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
385 break;
Ted Kremenek469ecbd2008-04-21 23:43:38 +0000386
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000387 case Stmt::ObjCMessageExprClass: {
388 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
389 break;
390 }
391
Ted Kremenekbbfd07a2008-12-09 20:18:58 +0000392 case Stmt::ObjCAtThrowStmtClass: {
393 // FIXME: This is not complete. We basically treat @throw as
394 // an abort.
395 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
396 Builder->BuildSinks = true;
397 MakeNode(Dst, S, Pred, GetState(Pred));
398 break;
399 }
400
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000401 case Stmt::ParenExprClass:
Ted Kremenek540cbe22008-04-22 04:56:29 +0000402 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000403 break;
404
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000405 case Stmt::ReturnStmtClass:
406 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
407 break;
408
Sebastian Redl05189992008-11-11 17:56:53 +0000409 case Stmt::SizeOfAlignOfExprClass:
410 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000411 break;
412
413 case Stmt::StmtExprClass: {
414 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000415
416 if (SE->getSubStmt()->body_empty()) {
417 // Empty statement expression.
418 assert(SE->getType() == getContext().VoidTy
419 && "Empty statement expression must have void type.");
420 Dst.Add(Pred);
421 break;
422 }
423
424 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
425 const GRState* state = GetState(Pred);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000426 MakeNode(Dst, SE, Pred, BindExpr(state, SE, GetSVal(state, LastExpr)));
Ted Kremeneka3d1eb82009-02-14 05:55:08 +0000427 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000428 else
429 Dst.Add(Pred);
430
431 break;
432 }
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000433
434 case Stmt::StringLiteralClass:
435 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
436 break;
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000437
Ted Kremenek72374592009-03-18 23:49:26 +0000438 case Stmt::UnaryOperatorClass: {
439 UnaryOperator *U = cast<UnaryOperator>(S);
440 if (EagerlyAssume && (U->getOpcode() == UnaryOperator::LNot)) {
441 NodeSet Tmp;
442 VisitUnaryOperator(U, Pred, Tmp, false);
443 EvalEagerlyAssume(Dst, Tmp, U);
444 }
445 else
446 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000447 break;
Ted Kremenek72374592009-03-18 23:49:26 +0000448 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000449 }
450}
451
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000452void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000453
454 Ex = Ex->IgnoreParens();
455
456 if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
457 Dst.Add(Pred);
458 return;
459 }
460
461 switch (Ex->getStmtClass()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000462
463 case Stmt::ArraySubscriptExprClass:
464 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
465 return;
466
467 case Stmt::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000468 case Stmt::QualifiedDeclRefExprClass:
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000469 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
470 return;
471
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000472 case Stmt::ObjCIvarRefExprClass:
473 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
474 return;
475
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000476 case Stmt::UnaryOperatorClass:
477 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
478 return;
479
480 case Stmt::MemberExprClass:
481 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
482 return;
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000483
Ted Kremenek4f090272008-10-27 21:54:31 +0000484 case Stmt::CompoundLiteralExprClass:
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000485 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenek4f090272008-10-27 21:54:31 +0000486 return;
487
Ted Kremenekb6b81d12008-10-17 17:24:14 +0000488 case Stmt::ObjCPropertyRefExprClass:
489 // FIXME: Property assignments are lvalues, but not really "locations".
490 // e.g.: self.x = something;
491 // Here the "self.x" really can translate to a method call (setter) when
492 // the assignment is made. Moreover, the entire assignment expression
493 // evaluate to whatever "something" is, not calling the "getter" for
494 // the property (which would make sense since it can have side effects).
495 // We'll probably treat this as a location, but not one that we can
496 // take the address of. Perhaps we need a new SVal class for cases
497 // like thsis?
498 // Note that we have a similar problem for bitfields, since they don't
499 // have "locations" in the sense that we can take their address.
500 Dst.Add(Pred);
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000501 return;
Zhongxing Xu143bf822008-10-25 14:18:57 +0000502
503 case Stmt::StringLiteralClass: {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000504 const GRState* state = GetState(Pred);
505 SVal V = StateMgr.GetLValue(state, cast<StringLiteral>(Ex));
506 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000507 return;
508 }
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000509
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000510 default:
511 // Arbitrary subexpressions can return aggregate temporaries that
512 // can be used in a lvalue context. We need to enhance our support
513 // of such temporaries in both the environment and the store, so right
514 // now we just do a regular visit.
Douglas Gregord7eb8462009-01-30 17:31:00 +0000515 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremenek5b2316a2008-10-25 20:09:21 +0000516 "Other kinds of expressions with non-aggregate/union types do"
517 " not have lvalues.");
Ted Kremenekc7df6d22008-10-18 04:08:49 +0000518
Ted Kremenekf8cd1b22008-10-18 04:15:35 +0000519 Visit(Ex, Pred, Dst);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000520 }
521}
522
523//===----------------------------------------------------------------------===//
524// Block entrance. (Update counters).
525//===----------------------------------------------------------------------===//
526
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000527bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000528 GRBlockCounter BC) {
529
530 return BC.getNumVisited(B->getBlockID()) < 3;
531}
532
533//===----------------------------------------------------------------------===//
534// Branch processing.
535//===----------------------------------------------------------------------===//
536
Ted Kremeneka8538d92009-02-13 01:45:31 +0000537const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremenek4323a572008-07-10 22:03:41 +0000538 Stmt* Terminator,
539 bool branchTaken) {
Ted Kremenek05a23782008-02-26 19:05:15 +0000540
541 switch (Terminator->getStmtClass()) {
542 default:
Ted Kremeneka8538d92009-02-13 01:45:31 +0000543 return state;
Ted Kremenek05a23782008-02-26 19:05:15 +0000544
545 case Stmt::BinaryOperatorClass: { // '&&' and '||'
546
547 BinaryOperator* B = cast<BinaryOperator>(Terminator);
548 BinaryOperator::Opcode Op = B->getOpcode();
549
550 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
551
552 // For &&, if we take the true branch, then the value of the whole
553 // expression is that of the RHS expression.
554 //
555 // For ||, if we take the false branch, then the value of the whole
556 // expression is that of the RHS expression.
557
558 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
559 (Op == BinaryOperator::LOr && !branchTaken)
560 ? B->getRHS() : B->getLHS();
561
Ted Kremeneka8538d92009-02-13 01:45:31 +0000562 return BindBlkExpr(state, B, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000563 }
564
565 case Stmt::ConditionalOperatorClass: { // ?:
566
567 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
568
569 // For ?, if branchTaken == true then the value is either the LHS or
570 // the condition itself. (GNU extension).
571
572 Expr* Ex;
573
574 if (branchTaken)
575 Ex = C->getLHS() ? C->getLHS() : C->getCond();
576 else
577 Ex = C->getRHS();
578
Ted Kremeneka8538d92009-02-13 01:45:31 +0000579 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000580 }
581
582 case Stmt::ChooseExprClass: { // ?:
583
584 ChooseExpr* C = cast<ChooseExpr>(Terminator);
585
586 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000587 return BindBlkExpr(state, C, UndefinedVal(Ex));
Ted Kremenek05a23782008-02-26 19:05:15 +0000588 }
589 }
590}
591
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000592/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
593/// to try to recover some path-sensitivity for casts of symbolic
594/// integers that promote their values (which are currently not tracked well).
595/// This function returns the SVal bound to Condition->IgnoreCasts if all the
596// cast(s) did was sign-extend the original value.
597static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
598 Stmt* Condition, ASTContext& Ctx) {
599
600 Expr *Ex = dyn_cast<Expr>(Condition);
601 if (!Ex)
602 return UnknownVal();
603
604 uint64_t bits = 0;
605 bool bitsInit = false;
606
607 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
608 QualType T = CE->getType();
609
610 if (!T->isIntegerType())
611 return UnknownVal();
612
613 uint64_t newBits = Ctx.getTypeSize(T);
614 if (!bitsInit || newBits < bits) {
615 bitsInit = true;
616 bits = newBits;
617 }
618
619 Ex = CE->getSubExpr();
620 }
621
622 // We reached a non-cast. Is it a symbolic value?
623 QualType T = Ex->getType();
624
625 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
626 return UnknownVal();
627
628 return StateMgr.GetSVal(state, Ex);
629}
630
Ted Kremenekaf337412008-11-12 19:24:17 +0000631void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000632 BranchNodeBuilder& builder) {
Ted Kremenek0bed8a12009-03-11 02:41:36 +0000633
Ted Kremeneke7d22112008-02-11 19:21:59 +0000634 // Remove old bindings for subexpressions.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000635 const GRState* PrevState =
Ted Kremenek4323a572008-07-10 22:03:41 +0000636 StateMgr.RemoveSubExprBindings(builder.getState());
Ted Kremenekf233d482008-02-05 00:26:40 +0000637
Ted Kremenekb2331832008-02-15 22:29:00 +0000638 // Check for NULL conditions; e.g. "for(;;)"
639 if (!Condition) {
640 builder.markInfeasible(false);
Ted Kremenekb2331832008-02-15 22:29:00 +0000641 return;
642 }
643
Ted Kremenek21028dd2009-03-11 03:54:24 +0000644 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
645 Condition->getLocStart(),
646 "Error evaluating branch");
647
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000648 SVal V = GetSVal(PrevState, Condition);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000649
650 switch (V.getBaseKind()) {
651 default:
652 break;
653
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000654 case SVal::UnknownKind: {
655 if (Expr *Ex = dyn_cast<Expr>(Condition)) {
656 if (Ex->getType()->isIntegerType()) {
657 // Try to recover some path-sensitivity. Right now casts of symbolic
658 // integers that promote their values are currently not tracked well.
659 // If 'Condition' is such an expression, try and recover the
660 // underlying value and use that instead.
661 SVal recovered = RecoverCastedSymbol(getStateManager(),
662 builder.getState(), Condition,
663 getContext());
664
665 if (!recovered.isUnknown()) {
666 V = recovered;
667 break;
668 }
669 }
670 }
671
Ted Kremenek58b33212008-02-26 19:40:44 +0000672 builder.generateNode(MarkBranch(PrevState, Term, true), true);
673 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000674 return;
Ted Kremenek6ae8a362009-03-13 16:32:54 +0000675 }
Ted Kremenekb38911f2008-01-30 23:03:39 +0000676
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000677 case SVal::UndefinedKind: {
Ted Kremenekb38911f2008-01-30 23:03:39 +0000678 NodeTy* N = builder.generateNode(PrevState, true);
679
680 if (N) {
681 N->markAsSink();
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000682 UndefBranches.insert(N);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000683 }
684
685 builder.markInfeasible(false);
686 return;
687 }
688 }
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000689
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000690 // Process the true branch.
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000691
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000692 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000693 const GRState* state = Assume(PrevState, V, true, isFeasible);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000694
695 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000696 builder.generateNode(MarkBranch(state, Term, true), true);
Ted Kremenek8e49dd62008-02-12 18:08:17 +0000697 else
698 builder.markInfeasible(true);
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000699
700 // Process the false branch.
Ted Kremenekb38911f2008-01-30 23:03:39 +0000701
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000702 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000703 state = Assume(PrevState, V, false, isFeasible);
Ted Kremenekb38911f2008-01-30 23:03:39 +0000704
Ted Kremenek6a6719a2008-02-29 20:27:50 +0000705 if (isFeasible)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000706 builder.generateNode(MarkBranch(state, Term, false), false);
Ted Kremenekf233d482008-02-05 00:26:40 +0000707 else
708 builder.markInfeasible(false);
Ted Kremenek71c29bd2008-01-29 23:32:35 +0000709}
710
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000711/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek754607e2008-02-13 00:24:44 +0000712/// nodes by processing the 'effects' of a computed goto jump.
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000713void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000714
Ted Kremeneka8538d92009-02-13 01:45:31 +0000715 const GRState* state = builder.getState();
716 SVal V = GetSVal(state, builder.getTarget());
Ted Kremenek754607e2008-02-13 00:24:44 +0000717
718 // Three possibilities:
719 //
720 // (1) We know the computed label.
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000721 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek754607e2008-02-13 00:24:44 +0000722 // (3) We have no clue about the label. Dispatch to all targets.
723 //
724
725 typedef IndirectGotoNodeBuilder::iterator iterator;
726
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000727 if (isa<loc::GotoLabel>(V)) {
728 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Ted Kremenek754607e2008-02-13 00:24:44 +0000729
730 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek24f1a962008-02-13 17:27:37 +0000731 if (I.getLabel() == L) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000732 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000733 return;
734 }
735 }
736
737 assert (false && "No block with label.");
738 return;
739 }
740
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000741 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek754607e2008-02-13 00:24:44 +0000742 // Dispatch to the first target and mark it as a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000743 NodeTy* N = builder.generateNode(builder.begin(), state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000744 UndefBranches.insert(N);
Ted Kremenek754607e2008-02-13 00:24:44 +0000745 return;
746 }
747
748 // This is really a catch-all. We don't support symbolics yet.
749
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000750 assert (V.isUnknown());
Ted Kremenek754607e2008-02-13 00:24:44 +0000751
752 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000753 builder.generateNode(I, state);
Ted Kremenek754607e2008-02-13 00:24:44 +0000754}
Ted Kremenekf233d482008-02-05 00:26:40 +0000755
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000756
757void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
758 NodeTy* Pred, NodeSet& Dst) {
759
760 assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
761
Ted Kremeneka8538d92009-02-13 01:45:31 +0000762 const GRState* state = GetState(Pred);
763 SVal X = GetBlkExprSVal(state, Ex);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000764
765 assert (X.isUndef());
766
767 Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
768
769 assert (SE);
770
Ted Kremeneka8538d92009-02-13 01:45:31 +0000771 X = GetBlkExprSVal(state, SE);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000772
773 // Make sure that we invalidate the previous binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +0000774 MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(state, Ex, X, true, true));
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000775}
776
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000777/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
778/// nodes by processing the 'effects' of a switch statement.
Ted Kremenek72afb372009-01-17 01:54:16 +0000779void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
780 typedef SwitchNodeBuilder::iterator iterator;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000781 const GRState* state = builder.getState();
Ted Kremenek692416c2008-02-18 22:57:02 +0000782 Expr* CondE = builder.getCondition();
Ted Kremeneka8538d92009-02-13 01:45:31 +0000783 SVal CondV = GetSVal(state, CondE);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000784
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000785 if (CondV.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000786 NodeTy* N = builder.generateDefaultCaseNode(state, true);
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000787 UndefBranches.insert(N);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000788 return;
789 }
Ted Kremenek692416c2008-02-18 22:57:02 +0000790
Ted Kremeneka8538d92009-02-13 01:45:31 +0000791 const GRState* DefaultSt = state;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000792 bool DefaultFeasible = false;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000793
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000794 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000795 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek72afb372009-01-17 01:54:16 +0000796
797 // Evaluate the LHS of the case value.
798 Expr::EvalResult V1;
799 bool b = Case->getLHS()->Evaluate(V1, getContext());
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000800
Ted Kremenek72afb372009-01-17 01:54:16 +0000801 // Sanity checks. These go away in Release builds.
802 assert(b && V1.Val.isInt() && !V1.HasSideEffects
803 && "Case condition must evaluate to an integer constant.");
804 b = b; // silence unused variable warning
805 assert(V1.Val.getInt().getBitWidth() ==
806 getContext().getTypeSize(CondE->getType()));
807
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000808 // Get the RHS of the case, if it exists.
Ted Kremenek72afb372009-01-17 01:54:16 +0000809 Expr::EvalResult V2;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000810
811 if (Expr* E = Case->getRHS()) {
Ted Kremenek72afb372009-01-17 01:54:16 +0000812 b = E->Evaluate(V2, getContext());
813 assert(b && V2.Val.isInt() && !V2.HasSideEffects
814 && "Case condition must evaluate to an integer constant.");
815 b = b; // silence unused variable warning
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000816 }
Ted Kremenek14a11402008-03-17 22:17:56 +0000817 else
818 V2 = V1;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000819
820 // FIXME: Eventually we should replace the logic below with a range
821 // comparison, rather than concretize the values within the range.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000822 // This should be easy once we have "ranges" for NonLVals.
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000823
Ted Kremenek14a11402008-03-17 22:17:56 +0000824 do {
Ted Kremenek72afb372009-01-17 01:54:16 +0000825 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000826 SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal,
827 getContext().IntTy);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000828
Ted Kremenek72afb372009-01-17 01:54:16 +0000829 // Now "assume" that the case matches.
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000830 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000831 const GRState* StNew = Assume(state, Res, true, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000832
833 if (isFeasible) {
834 builder.generateCaseStmtNode(I, StNew);
835
836 // If CondV evaluates to a constant, then we know that this
837 // is the *only* case that we can take, so stop evaluating the
838 // others.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000839 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000840 return;
841 }
842
843 // Now "assume" that the case doesn't match. Add this state
844 // to the default state (if it is feasible).
845
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000846 isFeasible = false;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000847 StNew = Assume(DefaultSt, Res, false, isFeasible);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000848
Ted Kremenek5014ab12008-04-23 05:03:18 +0000849 if (isFeasible) {
850 DefaultFeasible = true;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000851 DefaultSt = StNew;
Ted Kremenek5014ab12008-04-23 05:03:18 +0000852 }
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000853
Ted Kremenek14a11402008-03-17 22:17:56 +0000854 // Concretize the next value in the range.
Ted Kremenek72afb372009-01-17 01:54:16 +0000855 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek14a11402008-03-17 22:17:56 +0000856 break;
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000857
Ted Kremenek72afb372009-01-17 01:54:16 +0000858 ++V1.Val.getInt();
859 assert (V1.Val.getInt() <= V2.Val.getInt());
Ted Kremenek14a11402008-03-17 22:17:56 +0000860
861 } while (true);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000862 }
863
864 // If we reach here, than we know that the default branch is
865 // possible.
Ted Kremenek5014ab12008-04-23 05:03:18 +0000866 if (DefaultFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000867}
868
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000869//===----------------------------------------------------------------------===//
870// Transfer functions: logical operations ('&&', '||').
871//===----------------------------------------------------------------------===//
Ted Kremenekdaeb9a72008-02-13 23:08:21 +0000872
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000873void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000874 NodeSet& Dst) {
Ted Kremenek9dca0622008-02-19 00:22:37 +0000875
Ted Kremenek05a23782008-02-26 19:05:15 +0000876 assert (B->getOpcode() == BinaryOperator::LAnd ||
877 B->getOpcode() == BinaryOperator::LOr);
878
879 assert (B == CurrentStmt && getCFG().isBlkExpr(B));
880
Ted Kremeneka8538d92009-02-13 01:45:31 +0000881 const GRState* state = GetState(Pred);
882 SVal X = GetBlkExprSVal(state, B);
Ted Kremenek05a23782008-02-26 19:05:15 +0000883
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000884 assert (X.isUndef());
Ted Kremenek05a23782008-02-26 19:05:15 +0000885
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000886 Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenek05a23782008-02-26 19:05:15 +0000887
888 assert (Ex);
889
890 if (Ex == B->getRHS()) {
891
Ted Kremeneka8538d92009-02-13 01:45:31 +0000892 X = GetBlkExprSVal(state, Ex);
Ted Kremenek05a23782008-02-26 19:05:15 +0000893
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000894 // Handle undefined values.
Ted Kremenek58b33212008-02-26 19:40:44 +0000895
Ted Kremenek4a4e5242008-02-28 09:25:22 +0000896 if (X.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000897 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenek58b33212008-02-26 19:40:44 +0000898 return;
899 }
900
Ted Kremenek05a23782008-02-26 19:05:15 +0000901 // We took the RHS. Because the value of the '&&' or '||' expression must
902 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
903 // or 1. Alternatively, we could take a lazy approach, and calculate this
904 // value later when necessary. We don't have the machinery in place for
905 // this right now, and since most logical expressions are used for branches,
906 // the payoff is not likely to be large. Instead, we do eager evaluation.
907
908 bool isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000909 const GRState* NewState = Assume(state, X, true, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000910
911 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000912 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000913 BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
Ted Kremenek05a23782008-02-26 19:05:15 +0000914
915 isFeasible = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +0000916 NewState = Assume(state, X, false, isFeasible);
Ted Kremenek05a23782008-02-26 19:05:15 +0000917
918 if (isFeasible)
Ted Kremenek0e561a32008-03-21 21:30:14 +0000919 MakeNode(Dst, B, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000920 BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
Ted Kremenekf233d482008-02-05 00:26:40 +0000921 }
922 else {
Ted Kremenek05a23782008-02-26 19:05:15 +0000923 // We took the LHS expression. Depending on whether we are '&&' or
924 // '||' we know what the value of the expression is via properties of
925 // the short-circuiting.
926
927 X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000928 MakeNode(Dst, B, Pred, BindBlkExpr(state, B, X));
Ted Kremenekf233d482008-02-05 00:26:40 +0000929 }
Ted Kremenekf233d482008-02-05 00:26:40 +0000930}
Ted Kremenek05a23782008-02-26 19:05:15 +0000931
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000932//===----------------------------------------------------------------------===//
Ted Kremenekec96a2d2008-04-16 18:39:06 +0000933// Transfer functions: Loads and stores.
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000934//===----------------------------------------------------------------------===//
Ted Kremenekd27f8162008-01-15 23:55:06 +0000935
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000936void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
937 bool asLValue) {
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000938
Ted Kremeneka8538d92009-02-13 01:45:31 +0000939 const GRState* state = GetState(Pred);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000940
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000941 const NamedDecl* D = Ex->getDecl();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000942
943 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
944
Ted Kremeneka8538d92009-02-13 01:45:31 +0000945 SVal V = StateMgr.GetLValue(state, VD);
Zhongxing Xua7581732008-10-17 02:20:14 +0000946
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000947 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +0000948 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000949 else
Ted Kremeneka8538d92009-02-13 01:45:31 +0000950 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000951 return;
952
953 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
954 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
955
956 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000957 SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
Ted Kremeneka8538d92009-02-13 01:45:31 +0000958 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000959 return;
960
961 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek5631a732008-11-15 02:35:08 +0000962 assert(asLValue);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000963 SVal V = loc::FuncVal(FD);
Ted Kremeneka8538d92009-02-13 01:45:31 +0000964 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000965 return;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000966 }
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000967
968 assert (false &&
969 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000970}
971
Ted Kremenek540cbe22008-04-22 04:56:29 +0000972/// VisitArraySubscriptExpr - Transfer function for array accesses
973void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000974 NodeSet& Dst, bool asLValue) {
Ted Kremenek540cbe22008-04-22 04:56:29 +0000975
976 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000977 Expr* Idx = A->getIdx()->IgnoreParens();
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000978 NodeSet Tmp;
Ted Kremenek265a3052009-02-24 02:23:11 +0000979
980 if (Base->getType()->isVectorType()) {
981 // For vector types get its lvalue.
982 // FIXME: This may not be correct. Is the rvalue of a vector its location?
983 // In fact, I think this is just a hack. We need to get the right
984 // semantics.
985 VisitLValue(Base, Pred, Tmp);
986 }
987 else
988 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +0000989
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000990 for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000991 NodeSet Tmp2;
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000992 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000993
994 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
Ted Kremeneka8538d92009-02-13 01:45:31 +0000995 const GRState* state = GetState(*I2);
996 SVal V = StateMgr.GetLValue(state, GetSVal(state, Base),
997 GetSVal(state, Idx));
Ted Kremenek4d0348b2008-04-29 23:24:44 +0000998
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000999 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001000 MakeNode(Dst, A, *I2, BindExpr(state, A, V));
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001001 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001002 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek4d0348b2008-04-29 23:24:44 +00001003 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001004 }
Ted Kremenek540cbe22008-04-22 04:56:29 +00001005}
1006
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001007/// VisitMemberExpr - Transfer function for member expressions.
1008void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001009 NodeSet& Dst, bool asLValue) {
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001010
1011 Expr* Base = M->getBase()->IgnoreParens();
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001012 NodeSet Tmp;
Ted Kremenek5c456fe2008-10-18 03:28:48 +00001013
1014 if (M->isArrow())
1015 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1016 else
1017 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
1018
Douglas Gregor86f19402008-12-20 23:49:58 +00001019 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1020 if (!Field) // FIXME: skipping member expressions for non-fields
1021 return;
1022
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001023 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001024 const GRState* state = GetState(*I);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001025 // FIXME: Should we insert some assumption logic in here to determine
1026 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor86f19402008-12-20 23:49:58 +00001027 // later when using FieldOffset lvals (which we no longer have).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001028 SVal L = StateMgr.GetLValue(state, GetSVal(state, Base), Field);
Ted Kremenekd9bc33e2008-10-17 00:51:01 +00001029
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001030 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001031 MakeNode(Dst, M, *I, BindExpr(state, M, L));
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001032 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001033 EvalLoad(Dst, M, *I, state, L);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001034 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00001035}
1036
Ted Kremeneka8538d92009-02-13 01:45:31 +00001037/// EvalBind - Handle the semantics of binding a value to a specific location.
1038/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
1039void GRExprEngine::EvalBind(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
1040 const GRState* state, SVal location, SVal Val) {
1041
Ted Kremenek41573eb2009-02-14 01:43:44 +00001042 const GRState* newState = 0;
1043
1044 if (location.isUnknown()) {
1045 // We know that the new state will be the same as the old state since
1046 // the location of the binding is "unknown". Consequently, there
1047 // is no reason to just create a new node.
1048 newState = state;
1049 }
1050 else {
1051 // We are binding to a value other than 'unknown'. Perform the binding
1052 // using the StoreManager.
1053 newState = StateMgr.BindLoc(state, cast<Loc>(location), Val);
1054 }
Ted Kremeneka8538d92009-02-13 01:45:31 +00001055
Ted Kremenek41573eb2009-02-14 01:43:44 +00001056 // The next thing to do is check if the GRTransferFuncs object wants to
1057 // update the state based on the new binding. If the GRTransferFunc object
1058 // doesn't do anything, just auto-propagate the current state.
1059 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
1060 newState != state);
1061
1062 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001063}
1064
1065/// EvalStore - Handle the semantics of a store via an assignment.
1066/// @param Dst The node set to store generated state nodes
1067/// @param Ex The expression representing the location of the store
1068/// @param state The current simulation state
1069/// @param location The location to store the value
1070/// @param Val The value to be stored
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001071void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001072 const GRState* state, SVal location, SVal Val) {
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001073
1074 assert (Builder && "GRStmtNodeBuilder must be defined.");
1075
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001076 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001077 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001078
Ted Kremenek8c354752008-12-16 22:02:27 +00001079 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001080 return;
Ted Kremenekb0533962008-04-18 20:35:30 +00001081
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001082 assert (!location.isUndef());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001083 state = GetState(Pred);
1084
1085 // Proceed with the store.
1086 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1087 Builder->PointKind = ProgramPoint::PostStoreKind;
1088 EvalBind(Dst, Ex, Pred, state, location, Val);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001089}
1090
1091void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001092 const GRState* state, SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001093
Ted Kremenek8c354752008-12-16 22:02:27 +00001094 // Evaluate the location (checks for bad dereferences).
Ted Kremeneka8538d92009-02-13 01:45:31 +00001095 Pred = EvalLocation(Ex, Pred, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001096
Ted Kremenek8c354752008-12-16 22:02:27 +00001097 if (!Pred)
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001098 return;
1099
Ted Kremeneka8538d92009-02-13 01:45:31 +00001100 state = GetState(Pred);
Ted Kremenek8c354752008-12-16 22:02:27 +00001101
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001102 // Proceed with the load.
Ted Kremenek982e6742008-08-28 18:43:46 +00001103 ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001104
1105 // FIXME: Currently symbolic analysis "generates" new symbols
1106 // for the contents of values. We need a better approach.
1107
Ted Kremenek8c354752008-12-16 22:02:27 +00001108 if (location.isUnknown()) {
Ted Kremenek436f2b92008-04-30 04:23:07 +00001109 // This is important. We must nuke the old binding.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001110 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, UnknownVal()), K);
Ted Kremenek436f2b92008-04-30 04:23:07 +00001111 }
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001112 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001113 SVal V = GetSVal(state, cast<Loc>(location), Ex->getType());
1114 MakeNode(Dst, Ex, Pred, BindExpr(state, Ex, V), K);
Zhongxing Xud5b499d2008-11-28 08:34:30 +00001115 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001116}
1117
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001118void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001119 const GRState* state, SVal location, SVal Val) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001120
1121 NodeSet TmpDst;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001122 EvalStore(TmpDst, StoreE, Pred, state, location, Val);
Ted Kremenek82bae3f2008-09-20 01:50:34 +00001123
1124 for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
1125 MakeNode(Dst, Ex, *I, (*I)->getState());
1126}
1127
Ted Kremenek8c354752008-12-16 22:02:27 +00001128GRExprEngine::NodeTy* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
Ted Kremeneka8538d92009-02-13 01:45:31 +00001129 const GRState* state,
Ted Kremenek8c354752008-12-16 22:02:27 +00001130 SVal location) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001131
1132 // Check for loads/stores from/to undefined values.
1133 if (location.isUndef()) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001134 NodeTy* N =
Ted Kremeneka8538d92009-02-13 01:45:31 +00001135 Builder->generateNode(Ex, state, Pred,
Ted Kremenek8c354752008-12-16 22:02:27 +00001136 ProgramPoint::PostUndefLocationCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001137
Ted Kremenek8c354752008-12-16 22:02:27 +00001138 if (N) {
1139 N->markAsSink();
1140 UndefDeref.insert(N);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001141 }
1142
Ted Kremenek8c354752008-12-16 22:02:27 +00001143 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001144 }
1145
1146 // Check for loads/stores from/to unknown locations. Treat as No-Ops.
1147 if (location.isUnknown())
Ted Kremenek8c354752008-12-16 22:02:27 +00001148 return Pred;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001149
1150 // During a load, one of two possible situations arise:
1151 // (1) A crash, because the location (pointer) was NULL.
1152 // (2) The location (pointer) is not NULL, and the dereference works.
1153 //
1154 // We add these assumptions.
1155
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001156 Loc LV = cast<Loc>(location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001157
1158 // "Assume" that the pointer is not NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001159 bool isFeasibleNotNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001160 const GRState* StNotNull = Assume(state, LV, true, isFeasibleNotNull);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001161
1162 // "Assume" that the pointer is NULL.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001163 bool isFeasibleNull = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001164 GRStateRef StNull = GRStateRef(Assume(state, LV, false, isFeasibleNull),
Ted Kremenek7360fda2008-09-18 23:09:54 +00001165 getStateManager());
Zhongxing Xua1718c72009-04-03 07:33:13 +00001166
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001167 if (isFeasibleNull) {
1168
Ted Kremenek7360fda2008-09-18 23:09:54 +00001169 // Use the Generic Data Map to mark in the state what lval was null.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001170 const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
Ted Kremenek7360fda2008-09-18 23:09:54 +00001171 StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
1172
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001173 // We don't use "MakeNode" here because the node will be a sink
1174 // and we have no intention of processing it later.
Ted Kremenek8c354752008-12-16 22:02:27 +00001175 NodeTy* NullNode =
1176 Builder->generateNode(Ex, StNull, Pred,
1177 ProgramPoint::PostNullCheckFailedKind);
Ted Kremenek331b0ac2008-06-18 05:34:07 +00001178
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001179 if (NullNode) {
1180
1181 NullNode->markAsSink();
1182
1183 if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
1184 else ExplicitNullDeref.insert(NullNode);
1185 }
1186 }
Ted Kremenek8c354752008-12-16 22:02:27 +00001187
1188 if (!isFeasibleNotNull)
1189 return 0;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001190
1191 // Check for out-of-bound array access.
Ted Kremenek8c354752008-12-16 22:02:27 +00001192 if (isa<loc::MemRegionVal>(LV)) {
Zhongxing Xu60156f02008-11-08 03:45:42 +00001193 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
1194 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1195 // Get the index of the accessed element.
1196 SVal Idx = ER->getIndex();
1197 // Get the extent of the array.
Zhongxing Xu1ed8d4b2008-11-24 07:02:06 +00001198 SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
1199 ER->getSuperRegion());
Zhongxing Xu60156f02008-11-08 03:45:42 +00001200
1201 bool isFeasibleInBound = false;
1202 const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
1203 true, isFeasibleInBound);
1204
1205 bool isFeasibleOutBound = false;
1206 const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
1207 false, isFeasibleOutBound);
1208
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001209 if (isFeasibleOutBound) {
Ted Kremenek8c354752008-12-16 22:02:27 +00001210 // Report warning. Make sink node manually.
1211 NodeTy* OOBNode =
1212 Builder->generateNode(Ex, StOutBound, Pred,
1213 ProgramPoint::PostOutOfBoundsCheckFailedKind);
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00001214
1215 if (OOBNode) {
1216 OOBNode->markAsSink();
1217
1218 if (isFeasibleInBound)
1219 ImplicitOOBMemAccesses.insert(OOBNode);
1220 else
1221 ExplicitOOBMemAccesses.insert(OOBNode);
1222 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +00001223 }
1224
Ted Kremenek8c354752008-12-16 22:02:27 +00001225 if (!isFeasibleInBound)
1226 return 0;
1227
1228 StNotNull = StInBound;
Zhongxing Xu60156f02008-11-08 03:45:42 +00001229 }
1230 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00001231
Ted Kremenek8c354752008-12-16 22:02:27 +00001232 // Generate a new node indicating the checks succeed.
1233 return Builder->generateNode(Ex, StNotNull, Pred,
1234 ProgramPoint::PostLocationChecksSucceedKind);
Ted Kremenekec96a2d2008-04-16 18:39:06 +00001235}
1236
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001237//===----------------------------------------------------------------------===//
1238// Transfer function: Function calls.
1239//===----------------------------------------------------------------------===//
Ted Kremenekde434242008-02-19 01:44:53 +00001240void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001241 CallExpr::arg_iterator AI,
1242 CallExpr::arg_iterator AE,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001243 NodeSet& Dst)
1244{
1245 // Determine the type of function we're calling (if available).
Douglas Gregor72564e72009-02-26 23:50:07 +00001246 const FunctionProtoType *Proto = NULL;
Douglas Gregor9d293df2008-10-28 00:22:11 +00001247 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
1248 if (const PointerType *FnTypePtr = FnType->getAsPointerType())
Douglas Gregor72564e72009-02-26 23:50:07 +00001249 Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
Douglas Gregor9d293df2008-10-28 00:22:11 +00001250
1251 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1252}
1253
1254void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
1255 CallExpr::arg_iterator AI,
1256 CallExpr::arg_iterator AE,
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 NodeSet& Dst, const FunctionProtoType *Proto,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001258 unsigned ParamIdx) {
Ted Kremenekde434242008-02-19 01:44:53 +00001259
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001260 // Process the arguments.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001261 if (AI != AE) {
Douglas Gregor9d293df2008-10-28 00:22:11 +00001262 // If the call argument is being bound to a reference parameter,
1263 // visit it as an lvalue, not an rvalue.
1264 bool VisitAsLvalue = false;
1265 if (Proto && ParamIdx < Proto->getNumArgs())
1266 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1267
1268 NodeSet DstTmp;
1269 if (VisitAsLvalue)
1270 VisitLValue(*AI, Pred, DstTmp);
1271 else
1272 Visit(*AI, Pred, DstTmp);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001273 ++AI;
1274
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001275 for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
Douglas Gregor9d293df2008-10-28 00:22:11 +00001276 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Ted Kremenekde434242008-02-19 01:44:53 +00001277
1278 return;
1279 }
1280
1281 // If we reach here we have processed all of the arguments. Evaluate
1282 // the callee expression.
Ted Kremeneka1354a52008-03-03 16:47:31 +00001283
Ted Kremenek994a09b2008-02-25 21:16:03 +00001284 NodeSet DstTmp;
Ted Kremenek186350f2008-04-23 20:12:28 +00001285 Expr* Callee = CE->getCallee()->IgnoreParens();
Ted Kremeneka1354a52008-03-03 16:47:31 +00001286
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001287 Visit(Callee, Pred, DstTmp);
Ted Kremeneka1354a52008-03-03 16:47:31 +00001288
Ted Kremenekde434242008-02-19 01:44:53 +00001289 // Finally, evaluate the function call.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001290 for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
1291
Ted Kremeneka8538d92009-02-13 01:45:31 +00001292 const GRState* state = GetState(*DI);
1293 SVal L = GetSVal(state, Callee);
Ted Kremenekde434242008-02-19 01:44:53 +00001294
Ted Kremeneka1354a52008-03-03 16:47:31 +00001295 // FIXME: Add support for symbolic function calls (calls involving
1296 // function pointer values that are symbolic).
1297
1298 // Check for undefined control-flow or calls to NULL.
1299
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001300 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001301 NodeTy* N = Builder->generateNode(CE, state, *DI);
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001302
Ted Kremenek2ded35a2008-02-29 23:53:11 +00001303 if (N) {
1304 N->markAsSink();
1305 BadCalls.insert(N);
1306 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001307
Ted Kremenekde434242008-02-19 01:44:53 +00001308 continue;
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001309 }
1310
1311 // Check for the "noreturn" attribute.
1312
1313 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1314
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001315 if (isa<loc::FuncVal>(L)) {
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001316
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001317 FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001318
Ted Kremenekb7252322009-04-10 00:01:14 +00001319 if (FD->getAttr<NoReturnAttr>() || FD->getAttr<AnalyzerNoReturnAttr>())
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001320 Builder->BuildSinks = true;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001321 else {
1322 // HACK: Some functions are not marked noreturn, and don't return.
1323 // Here are a few hardwired ones. If this takes too long, we can
1324 // potentially cache these results.
1325 const char* s = FD->getIdentifier()->getName();
1326 unsigned n = strlen(s);
1327
1328 switch (n) {
1329 default:
1330 break;
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001331
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001332 case 4:
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001333 if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
1334 break;
1335
1336 case 5:
1337 if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
Zhongxing Xubb316c52008-10-07 10:06:03 +00001338 else if (!memcmp(s, "error", 5)) {
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001339 if (CE->getNumArgs() > 0) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001340 SVal X = GetSVal(state, *CE->arg_begin());
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001341 // FIXME: use Assume to inspect the possible symbolic value of
1342 // X. Also check the specific signature of error().
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001343 nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001344 if (CI && CI->getValue() != 0)
Zhongxing Xubb316c52008-10-07 10:06:03 +00001345 Builder->BuildSinks = true;
Zhongxing Xua90d56e2008-10-09 03:19:06 +00001346 }
Zhongxing Xubb316c52008-10-07 10:06:03 +00001347 }
Ted Kremenek76fdbde2008-03-14 23:25:49 +00001348 break;
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001349
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001350 case 6:
Ted Kremenek489ecd52008-05-17 00:42:01 +00001351 if (!memcmp(s, "Assert", 6)) {
1352 Builder->BuildSinks = true;
1353 break;
1354 }
Ted Kremenekc7122d52008-05-01 15:55:59 +00001355
1356 // FIXME: This is just a wrapper around throwing an exception.
1357 // Eventually inter-procedural analysis should handle this easily.
1358 if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
1359
Ted Kremenek9a094cb2008-04-22 05:37:33 +00001360 break;
Ted Kremenek688738f2008-04-23 00:41:25 +00001361
1362 case 7:
1363 if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
1364 break;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001365
Ted Kremenekf47bb782008-04-30 17:54:04 +00001366 case 8:
Ted Kremenek29ba6b42009-02-17 17:48:52 +00001367 if (!memcmp(s ,"db_error", 8) ||
1368 !memcmp(s, "__assert", 8))
1369 Builder->BuildSinks = true;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001370 break;
Ted Kremenek24cb8a22008-05-01 17:52:49 +00001371
1372 case 12:
1373 if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
1374 break;
Ted Kremenekf47bb782008-04-30 17:54:04 +00001375
Ted Kremenekf9683082008-09-19 02:30:47 +00001376 case 13:
1377 if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
1378 break;
1379
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001380 case 14:
Ted Kremenek2598b572008-10-30 00:00:57 +00001381 if (!memcmp(s, "dtrace_assfail", 14) ||
1382 !memcmp(s, "yy_fatal_error", 14))
1383 Builder->BuildSinks = true;
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001384 break;
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001385
1386 case 26:
Ted Kremenek7386d772008-07-18 16:28:33 +00001387 if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
Ted Kremenek40bbff02009-02-17 23:27:17 +00001388 !memcmp(s, "_DTAssertionFailureHandler", 26) ||
1389 !memcmp(s, "_TSAssertionFailureHandler", 26))
Ted Kremenek05a91122008-05-17 00:40:45 +00001390 Builder->BuildSinks = true;
Ted Kremenek7386d772008-07-18 16:28:33 +00001391
Ted Kremenekec8a1cb2008-05-17 00:33:23 +00001392 break;
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001393 }
Ted Kremenek9a108ae2008-04-22 06:09:33 +00001394
Ted Kremenek636e6ba2008-03-14 21:58:42 +00001395 }
1396 }
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001397
1398 // Evaluate the call.
Ted Kremenek186350f2008-04-23 20:12:28 +00001399
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001400 if (isa<loc::FuncVal>(L)) {
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001401
Douglas Gregor3c385e52009-02-14 18:57:46 +00001402 if (unsigned id
1403 = cast<loc::FuncVal>(L).getDecl()->getBuiltinID(getContext()))
Ted Kremenek55aea312008-03-05 22:59:42 +00001404 switch (id) {
1405 case Builtin::BI__builtin_expect: {
1406 // For __builtin_expect, just return the value of the subexpression.
1407 assert (CE->arg_begin() != CE->arg_end());
Ted Kremeneka8538d92009-02-13 01:45:31 +00001408 SVal X = GetSVal(state, *(CE->arg_begin()));
1409 MakeNode(Dst, CE, *DI, BindExpr(state, CE, X));
Ted Kremenek55aea312008-03-05 22:59:42 +00001410 continue;
1411 }
1412
Ted Kremenekb3021332008-11-02 00:35:01 +00001413 case Builtin::BI__builtin_alloca: {
Ted Kremenekb3021332008-11-02 00:35:01 +00001414 // FIXME: Refactor into StoreManager itself?
1415 MemRegionManager& RM = getStateManager().getRegionManager();
1416 const MemRegion* R =
Zhongxing Xu6d82f9d2008-11-13 07:58:20 +00001417 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001418
1419 // Set the extent of the region in bytes. This enables us to use the
1420 // SVal of the argument directly. If we save the extent in bits, we
1421 // cannot represent values like symbol*8.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001422 SVal Extent = GetSVal(state, *(CE->arg_begin()));
1423 state = getStoreManager().setExtent(state, R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001424
Ted Kremeneka8538d92009-02-13 01:45:31 +00001425 MakeNode(Dst, CE, *DI, BindExpr(state, CE, loc::MemRegionVal(R)));
Ted Kremenekb3021332008-11-02 00:35:01 +00001426 continue;
1427 }
1428
Ted Kremenek55aea312008-03-05 22:59:42 +00001429 default:
Ted Kremenek55aea312008-03-05 22:59:42 +00001430 break;
1431 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001432 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001433
Ted Kremenek186350f2008-04-23 20:12:28 +00001434 // Check any arguments passed-by-value against being undefined.
1435
1436 bool badArg = false;
1437
1438 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
1439 I != E; ++I) {
1440
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001441 if (GetSVal(GetState(*DI), *I).isUndef()) {
Ted Kremenek186350f2008-04-23 20:12:28 +00001442 NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
Ted Kremenek4bf38da2008-03-05 21:15:02 +00001443
Ted Kremenek186350f2008-04-23 20:12:28 +00001444 if (N) {
1445 N->markAsSink();
1446 UndefArgs[N] = *I;
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001447 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001448
Ted Kremenek186350f2008-04-23 20:12:28 +00001449 badArg = true;
1450 break;
1451 }
Ted Kremenekd753f3c2008-03-04 22:01:56 +00001452 }
Ted Kremenek186350f2008-04-23 20:12:28 +00001453
1454 if (badArg)
1455 continue;
1456
1457 // Dispatch to the plug-in transfer function.
1458
1459 unsigned size = Dst.size();
1460 SaveOr OldHasGen(Builder->HasGeneratedNode);
1461 EvalCall(Dst, CE, L, *DI);
1462
1463 // Handle the case where no nodes where generated. Auto-generate that
1464 // contains the updated state if we aren't generating sinks.
1465
1466 if (!Builder->BuildSinks && Dst.size() == size &&
1467 !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001468 MakeNode(Dst, CE, *DI, state);
Ted Kremenekde434242008-02-19 01:44:53 +00001469 }
1470}
1471
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001472//===----------------------------------------------------------------------===//
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001473// Transfer function: Objective-C ivar references.
1474//===----------------------------------------------------------------------===//
1475
Ted Kremenekf5cae632009-02-28 20:50:43 +00001476static std::pair<const void*,const void*> EagerlyAssumeTag
1477 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1478
Ted Kremenekb2939022009-02-25 23:32:10 +00001479void GRExprEngine::EvalEagerlyAssume(NodeSet &Dst, NodeSet &Src, Expr *Ex) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001480 for (NodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1481 NodeTy *Pred = *I;
Ted Kremenekb2939022009-02-25 23:32:10 +00001482
1483 // Test if the previous node was as the same expression. This can happen
1484 // when the expression fails to evaluate to anything meaningful and
1485 // (as an optimization) we don't generate a node.
1486 ProgramPoint P = Pred->getLocation();
1487 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
1488 Dst.Add(Pred);
1489 continue;
1490 }
1491
Ted Kremenek48af2a92009-02-25 22:32:02 +00001492 const GRState* state = Pred->getState();
Ted Kremenekb2939022009-02-25 23:32:10 +00001493 SVal V = GetSVal(state, Ex);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001494 if (isa<nonloc::SymExprVal>(V)) {
Ted Kremenek48af2a92009-02-25 22:32:02 +00001495 // First assume that the condition is true.
1496 bool isFeasible = false;
1497 const GRState *stateTrue = Assume(state, V, true, isFeasible);
1498 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001499 stateTrue = BindExpr(stateTrue, Ex, MakeConstantVal(1U, Ex));
1500 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001501 stateTrue, Pred));
1502 }
1503
1504 // Next, assume that the condition is false.
1505 isFeasible = false;
1506 const GRState *stateFalse = Assume(state, V, false, isFeasible);
1507 if (isFeasible) {
Ted Kremenekb2939022009-02-25 23:32:10 +00001508 stateFalse = BindExpr(stateFalse, Ex, MakeConstantVal(0U, Ex));
1509 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag),
Ted Kremenek48af2a92009-02-25 22:32:02 +00001510 stateFalse, Pred));
1511 }
1512 }
1513 else
1514 Dst.Add(Pred);
1515 }
1516}
1517
1518//===----------------------------------------------------------------------===//
1519// Transfer function: Objective-C ivar references.
1520//===----------------------------------------------------------------------===//
1521
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001522void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
1523 NodeTy* Pred, NodeSet& Dst,
1524 bool asLValue) {
1525
1526 Expr* Base = cast<Expr>(Ex->getBase());
1527 NodeSet Tmp;
1528 Visit(Base, Pred, Tmp);
1529
1530 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001531 const GRState* state = GetState(*I);
1532 SVal BaseVal = GetSVal(state, Base);
1533 SVal location = StateMgr.GetLValue(state, Ex->getDecl(), BaseVal);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001534
1535 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001536 MakeNode(Dst, Ex, *I, BindExpr(state, Ex, location));
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001537 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00001538 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek97ed4f62008-10-17 00:03:18 +00001539 }
1540}
1541
1542//===----------------------------------------------------------------------===//
Ted Kremenekaf337412008-11-12 19:24:17 +00001543// Transfer function: Objective-C fast enumeration 'for' statements.
1544//===----------------------------------------------------------------------===//
1545
1546void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
1547 NodeTy* Pred, NodeSet& Dst) {
1548
1549 // ObjCForCollectionStmts are processed in two places. This method
1550 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1551 // statements within a basic block. This transfer function does two things:
1552 //
1553 // (1) binds the next container value to 'element'. This creates a new
1554 // node in the ExplodedGraph.
1555 //
1556 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1557 // whether or not the container has any more elements. This value
1558 // will be tested in ProcessBranch. We need to explicitly bind
1559 // this value because a container can contain nil elements.
1560 //
1561 // FIXME: Eventually this logic should actually do dispatches to
1562 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1563 // This will require simulating a temporary NSFastEnumerationState, either
1564 // through an SVal or through the use of MemRegions. This value can
1565 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1566 // terminates we reclaim the temporary (it goes out of scope) and we
1567 // we can test if the SVal is 0 or if the MemRegion is null (depending
1568 // on what approach we take).
1569 //
1570 // For now: simulate (1) by assigning either a symbol or nil if the
1571 // container is empty. Thus this transfer function will by default
1572 // result in state splitting.
1573
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001574 Stmt* elem = S->getElement();
1575 SVal ElementV;
Ted Kremenekaf337412008-11-12 19:24:17 +00001576
1577 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner7e24e822009-03-28 06:33:19 +00001578 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenekaf337412008-11-12 19:24:17 +00001579 assert (ElemD->getInit() == 0);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001580 ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
1581 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1582 return;
Ted Kremenekaf337412008-11-12 19:24:17 +00001583 }
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001584
1585 NodeSet Tmp;
1586 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Ted Kremenekaf337412008-11-12 19:24:17 +00001587
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001588 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
1589 const GRState* state = GetState(*I);
1590 VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
1591 }
1592}
1593
1594void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
1595 NodeTy* Pred, NodeSet& Dst,
1596 SVal ElementV) {
1597
1598
Ted Kremenekaf337412008-11-12 19:24:17 +00001599
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001600 // Get the current state. Use 'EvalLocation' to determine if it is a null
1601 // pointer, etc.
1602 Stmt* elem = S->getElement();
Ted Kremenekaf337412008-11-12 19:24:17 +00001603
Ted Kremenek8c354752008-12-16 22:02:27 +00001604 Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
1605 if (!Pred)
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001606 return;
Ted Kremenek8c354752008-12-16 22:02:27 +00001607
1608 GRStateRef state = GRStateRef(GetState(Pred), getStateManager());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001609
Ted Kremenekaf337412008-11-12 19:24:17 +00001610 // Handle the case where the container still has elements.
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001611 QualType IntTy = getContext().IntTy;
Ted Kremenekaf337412008-11-12 19:24:17 +00001612 SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
1613 GRStateRef hasElems = state.BindExpr(S, TrueV);
1614
Ted Kremenekaf337412008-11-12 19:24:17 +00001615 // Handle the case where the container has no elements.
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001616 SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
1617 GRStateRef noElems = state.BindExpr(S, FalseV);
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001618
1619 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1620 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1621 // FIXME: The proper thing to do is to really iterate over the
1622 // container. We will do this with dispatch logic to the store.
1623 // For now, just 'conjure' up a symbolic value.
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001624 QualType T = R->getRValueType(getContext());
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001625 assert (Loc::IsLocType(T));
1626 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xuea7c5ce2009-04-09 06:49:52 +00001627 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1628 SVal V = Loc::MakeVal(getStoreManager().getRegionManager().getSymbolicRegion(Sym));
1629 hasElems = hasElems.BindLoc(ElementV, V);
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001630
Ted Kremenek06fb99f2008-11-14 19:47:18 +00001631 // Bind the location to 'nil' on the false branch.
1632 SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
1633 noElems = noElems.BindLoc(ElementV, nilV);
1634 }
1635
Ted Kremenek116ed0a2008-11-12 21:12:46 +00001636 // Create the new nodes.
1637 MakeNode(Dst, S, Pred, hasElems);
1638 MakeNode(Dst, S, Pred, noElems);
Ted Kremenekaf337412008-11-12 19:24:17 +00001639}
1640
1641//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001642// Transfer function: Objective-C message expressions.
1643//===----------------------------------------------------------------------===//
1644
1645void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
1646 NodeSet& Dst){
1647
1648 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1649 Pred, Dst);
1650}
1651
1652void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001653 ObjCMessageExpr::arg_iterator AI,
1654 ObjCMessageExpr::arg_iterator AE,
1655 NodeTy* Pred, NodeSet& Dst) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001656 if (AI == AE) {
1657
1658 // Process the receiver.
1659
1660 if (Expr* Receiver = ME->getReceiver()) {
1661 NodeSet Tmp;
1662 Visit(Receiver, Pred, Tmp);
1663
1664 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1665 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
1666
1667 return;
1668 }
1669
1670 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
1671 return;
1672 }
1673
1674 NodeSet Tmp;
1675 Visit(*AI, Pred, Tmp);
1676
1677 ++AI;
1678
1679 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
1680 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1681}
1682
1683void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
1684 NodeTy* Pred,
1685 NodeSet& Dst) {
1686
1687 // FIXME: More logic for the processing the method call.
1688
Ted Kremeneka8538d92009-02-13 01:45:31 +00001689 const GRState* state = GetState(Pred);
Ted Kremeneke448ab42008-05-01 18:33:28 +00001690 bool RaisesException = false;
1691
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001692
1693 if (Expr* Receiver = ME->getReceiver()) {
1694
Ted Kremeneka8538d92009-02-13 01:45:31 +00001695 SVal L = GetSVal(state, Receiver);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001696
Ted Kremenek21fe8372009-02-19 04:06:22 +00001697 // Check for undefined control-flow.
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001698 if (L.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001699 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001700
1701 if (N) {
1702 N->markAsSink();
1703 UndefReceivers.insert(N);
1704 }
1705
1706 return;
1707 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001708
Ted Kremenek21fe8372009-02-19 04:06:22 +00001709 // "Assume" that the receiver is not NULL.
1710 bool isFeasibleNotNull = false;
Ted Kremenekda9ae602009-04-08 18:51:08 +00001711 const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
Ted Kremenek21fe8372009-02-19 04:06:22 +00001712
1713 // "Assume" that the receiver is NULL.
1714 bool isFeasibleNull = false;
1715 const GRState *StNull = Assume(state, L, false, isFeasibleNull);
1716
1717 if (isFeasibleNull) {
Ted Kremenekfe630b92009-04-09 05:45:56 +00001718 QualType RetTy = ME->getType();
1719
Ted Kremenek21fe8372009-02-19 04:06:22 +00001720 // Check if the receiver was nil and the return value a struct.
Ted Kremenekfe630b92009-04-09 05:45:56 +00001721 if(RetTy->isRecordType()) {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001722 if (BR.getParentMap().isConsumedExpr(ME)) {
Ted Kremenek899b3de2009-04-08 03:07:17 +00001723 // The [0 ...] expressions will return garbage. Flag either an
1724 // explicit or implicit error. Because of the structure of this
1725 // function we currently do not bifurfacte the state graph at
1726 // this point.
1727 // FIXME: We should bifurcate and fill the returned struct with
1728 // garbage.
1729 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1730 N->markAsSink();
1731 if (isFeasibleNotNull)
1732 NilReceiverStructRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001733 else
Ted Kremeneke6449392009-04-09 04:06:51 +00001734 NilReceiverStructRetExplicit.insert(N);
Ted Kremenek899b3de2009-04-08 03:07:17 +00001735 }
1736 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001737 }
Ted Kremenekfe630b92009-04-09 05:45:56 +00001738 else {
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001739 ASTContext& Ctx = getContext();
Ted Kremenekfe630b92009-04-09 05:45:56 +00001740 if (RetTy != Ctx.VoidTy) {
1741 if (BR.getParentMap().isConsumedExpr(ME)) {
1742 // sizeof(void *)
1743 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
1744 // sizeof(return type)
1745 const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001746
Ted Kremenekfe630b92009-04-09 05:45:56 +00001747 if(voidPtrSize < returnTypeSize) {
1748 if (NodeTy* N = Builder->generateNode(ME, StNull, Pred)) {
1749 N->markAsSink();
1750 if(isFeasibleNotNull)
1751 NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
Ted Kremenekf8769c82009-04-09 06:02:06 +00001752 else
Ted Kremenekfe630b92009-04-09 05:45:56 +00001753 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
Ted Kremenekfe630b92009-04-09 05:45:56 +00001754 }
1755 }
1756 else if (!isFeasibleNotNull) {
1757 // Handle the safe cases where the return value is 0 if the
1758 // receiver is nil.
1759 //
1760 // FIXME: For now take the conservative approach that we only
1761 // return null values if we *know* that the receiver is nil.
1762 // This is because we can have surprises like:
1763 //
1764 // ... = [[NSScreens screens] objectAtIndex:0];
1765 //
1766 // What can happen is that [... screens] could return nil, but
1767 // it most likely isn't nil. We should assume the semantics
1768 // of this case unless we have *a lot* more knowledge.
1769 //
Ted Kremenek8e5fb282009-04-09 16:46:55 +00001770 SVal V = ValMgr.makeZeroVal(ME->getType());
Ted Kremenekfe630b92009-04-09 05:45:56 +00001771 MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
Ted Kremeneke6449392009-04-09 04:06:51 +00001772 return;
1773 }
Ted Kremenek899b3de2009-04-08 03:07:17 +00001774 }
Ted Kremeneke8dbf062009-04-09 00:00:02 +00001775 }
Ted Kremenek21fe8372009-02-19 04:06:22 +00001776 }
Ted Kremenekda9ae602009-04-08 18:51:08 +00001777 // We have handled the cases where the receiver is nil. The remainder
Ted Kremenekf8769c82009-04-09 06:02:06 +00001778 // of this method should assume that the receiver is not nil.
1779 if (!StNotNull)
1780 return;
1781
Ted Kremenekda9ae602009-04-08 18:51:08 +00001782 state = StNotNull;
Ted Kremenek21fe8372009-02-19 04:06:22 +00001783 }
1784
Ted Kremeneke448ab42008-05-01 18:33:28 +00001785 // Check if the "raise" message was sent.
1786 if (ME->getSelector() == RaiseSel)
1787 RaisesException = true;
1788 }
1789 else {
1790
1791 IdentifierInfo* ClsName = ME->getClassName();
1792 Selector S = ME->getSelector();
1793
1794 // Check for special instance methods.
1795
1796 if (!NSExceptionII) {
1797 ASTContext& Ctx = getContext();
1798
1799 NSExceptionII = &Ctx.Idents.get("NSException");
1800 }
1801
1802 if (ClsName == NSExceptionII) {
1803
1804 enum { NUM_RAISE_SELECTORS = 2 };
1805
1806 // Lazily create a cache of the selectors.
1807
1808 if (!NSExceptionInstanceRaiseSelectors) {
1809
1810 ASTContext& Ctx = getContext();
1811
1812 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
1813
1814 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
1815 unsigned idx = 0;
1816
1817 // raise:format:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001818 II.push_back(&Ctx.Idents.get("raise"));
1819 II.push_back(&Ctx.Idents.get("format"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001820 NSExceptionInstanceRaiseSelectors[idx++] =
1821 Ctx.Selectors.getSelector(II.size(), &II[0]);
1822
1823 // raise:format::arguments:
Ted Kremenek6ff6f8b2008-05-02 17:12:56 +00001824 II.push_back(&Ctx.Idents.get("arguments"));
Ted Kremeneke448ab42008-05-01 18:33:28 +00001825 NSExceptionInstanceRaiseSelectors[idx++] =
1826 Ctx.Selectors.getSelector(II.size(), &II[0]);
1827 }
1828
1829 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
1830 if (S == NSExceptionInstanceRaiseSelectors[i]) {
1831 RaisesException = true; break;
1832 }
1833 }
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001834 }
1835
1836 // Check for any arguments that are uninitialized/undefined.
1837
1838 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
1839 I != E; ++I) {
1840
Ted Kremeneka8538d92009-02-13 01:45:31 +00001841 if (GetSVal(state, *I).isUndef()) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001842
1843 // Generate an error node for passing an uninitialized/undefined value
1844 // as an argument to a message expression. This node is a sink.
Ted Kremeneka8538d92009-02-13 01:45:31 +00001845 NodeTy* N = Builder->generateNode(ME, state, Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001846
1847 if (N) {
1848 N->markAsSink();
1849 MsgExprUndefArgs[N] = *I;
1850 }
1851
1852 return;
1853 }
Ted Kremeneke448ab42008-05-01 18:33:28 +00001854 }
1855
1856 // Check if we raise an exception. For now treat these as sinks. Eventually
1857 // we will want to handle exceptions properly.
1858
1859 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1860
1861 if (RaisesException)
1862 Builder->BuildSinks = true;
1863
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001864 // Dispatch to plug-in transfer function.
1865
1866 unsigned size = Dst.size();
Ted Kremenek186350f2008-04-23 20:12:28 +00001867 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00001868
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001869 EvalObjCMessageExpr(Dst, ME, Pred);
1870
1871 // Handle the case where no nodes where generated. Auto-generate that
1872 // contains the updated state if we aren't generating sinks.
1873
Ted Kremenekb0533962008-04-18 20:35:30 +00001874 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremeneka8538d92009-02-13 01:45:31 +00001875 MakeNode(Dst, ME, Pred, state);
Ted Kremeneke695e1c2008-04-15 23:06:53 +00001876}
1877
1878//===----------------------------------------------------------------------===//
1879// Transfer functions: Miscellaneous statements.
1880//===----------------------------------------------------------------------===//
1881
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001882void GRExprEngine::VisitCastPointerToInteger(SVal V, const GRState* state,
1883 QualType PtrTy,
1884 Expr* CastE, NodeTy* Pred,
1885 NodeSet& Dst) {
1886 if (!V.isUnknownOrUndef()) {
1887 // FIXME: Determine if the number of bits of the target type is
1888 // equal or exceeds the number of bits to store the pointer value.
Ted Kremeneke121da42009-03-05 03:42:31 +00001889 // If not, flag an error.
Ted Kremenekac78d6b2009-03-05 03:44:53 +00001890 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, EvalCast(cast<Loc>(V),
1891 CastE->getType())));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001892 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001893 else
1894 MakeNode(Dst, CastE, Pred, BindExpr(state, CastE, V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001895}
1896
1897
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001898void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001899 NodeSet S1;
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001900 QualType T = CastE->getType();
Zhongxing Xu933c3e12008-10-21 06:54:23 +00001901 QualType ExTy = Ex->getType();
Zhongxing Xued340f72008-10-22 08:02:16 +00001902
Zhongxing Xud3118bd2008-10-31 07:26:14 +00001903 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregor49badde2008-10-27 19:41:14 +00001904 T = ExCast->getTypeAsWritten();
1905
Zhongxing Xued340f72008-10-22 08:02:16 +00001906 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00001907 VisitLValue(Ex, Pred, S1);
Ted Kremenek65cfb732008-03-04 22:16:08 +00001908 else
1909 Visit(Ex, Pred, S1);
1910
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001911 // Check for casting to "void".
Ted Kremenekdc402902009-03-04 00:14:35 +00001912 if (T->isVoidType()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001913 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
Ted Kremenek5d3003a2008-02-19 18:52:54 +00001914 Dst.Add(*I1);
1915
Ted Kremenek874d63f2008-01-24 02:02:54 +00001916 return;
1917 }
1918
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001919 // FIXME: The rest of this should probably just go into EvalCall, and
1920 // let the transfer function object be responsible for constructing
1921 // nodes.
1922
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00001923 for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
Ted Kremenek874d63f2008-01-24 02:02:54 +00001924 NodeTy* N = *I1;
Ted Kremeneka8538d92009-02-13 01:45:31 +00001925 const GRState* state = GetState(N);
1926 SVal V = GetSVal(state, Ex);
Ted Kremenekc5302912009-03-05 20:22:13 +00001927 ASTContext& C = getContext();
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001928
1929 // Unknown?
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001930 if (V.isUnknown()) {
1931 Dst.Add(N);
1932 continue;
1933 }
1934
1935 // Undefined?
Ted Kremenekc5302912009-03-05 20:22:13 +00001936 if (V.isUndef())
1937 goto PassThrough;
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001938
1939 // For const casts, just propagate the value.
Ted Kremeneka8fe39f2008-09-19 20:51:22 +00001940 if (C.getCanonicalType(T).getUnqualifiedType() ==
Ted Kremenekc5302912009-03-05 20:22:13 +00001941 C.getCanonicalType(ExTy).getUnqualifiedType())
1942 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001943
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001944 // Check for casts from pointers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001945 if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00001946 VisitCastPointerToInteger(V, state, ExTy, CastE, N, Dst);
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001947 continue;
1948 }
1949
1950 // Check for casts from integers to pointers.
Ted Kremenek16aac322009-03-05 02:33:55 +00001951 if (Loc::IsLocType(T) && ExTy->isIntegerType()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001952 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001953 // Just unpackage the lval and return it.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001954 V = LV->getLoc();
Ted Kremeneka8538d92009-02-13 01:45:31 +00001955 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
Ted Kremenekc5302912009-03-05 20:22:13 +00001956 continue;
Ted Kremenek0fe33bc2008-04-22 21:10:18 +00001957 }
Ted Kremeneke121da42009-03-05 03:42:31 +00001958
Ted Kremenekc5302912009-03-05 20:22:13 +00001959 goto DispatchCast;
Ted Kremenek16aac322009-03-05 02:33:55 +00001960 }
1961
1962 // Just pass through function and block pointers.
1963 if (ExTy->isBlockPointerType() || ExTy->isFunctionPointerType()) {
1964 assert(Loc::IsLocType(T));
Ted Kremenekc5302912009-03-05 20:22:13 +00001965 goto PassThrough;
Ted Kremenek16aac322009-03-05 02:33:55 +00001966 }
1967
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001968 // Check for casts from array type to another type.
Zhongxing Xue1911af2008-10-23 03:10:39 +00001969 if (ExTy->isArrayType()) {
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001970 // We will always decay to a pointer.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +00001971 V = StateMgr.ArrayToPointer(cast<Loc>(V));
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001972
1973 // Are we casting from an array to a pointer? If so just pass on
1974 // the decayed value.
Ted Kremenekc5302912009-03-05 20:22:13 +00001975 if (T->isPointerType())
1976 goto PassThrough;
Ted Kremeneke1c2a672009-01-13 01:04:21 +00001977
1978 // Are we casting from an array to an integer? If so, cast the decayed
1979 // pointer value to an integer.
1980 assert(T->isIntegerType());
1981 QualType ElemTy = cast<ArrayType>(ExTy)->getElementType();
1982 QualType PointerTy = getContext().getPointerType(ElemTy);
Ted Kremeneka8538d92009-02-13 01:45:31 +00001983 VisitCastPointerToInteger(V, state, PointerTy, CastE, N, Dst);
Zhongxing Xue1911af2008-10-23 03:10:39 +00001984 continue;
1985 }
1986
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001987 // Check for casts from a region to a specific type.
Ted Kremenek5c42f9b2009-03-05 22:47:06 +00001988 if (loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(&V)) {
1989 // FIXME: For TypedViewRegions, we should handle the case where the
1990 // underlying symbolic pointer is a function pointer or
1991 // block pointer.
1992
1993 // FIXME: We should handle the case where we strip off view layers to get
1994 // to a desugared type.
1995
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001996 assert(Loc::IsLocType(T));
Zhongxing Xua1718c72009-04-03 07:33:13 +00001997 // We get a symbolic function pointer for a dereference of a function
1998 // pointer, but it is of function type. Example:
1999
2000 // struct FPRec {
2001 // void (*my_func)(int * x);
2002 // };
2003 //
2004 // int bar(int x);
2005 //
2006 // int f1_a(struct FPRec* foo) {
2007 // int x;
2008 // (*foo->my_func)(&x);
2009 // return bar(x)+1; // no-warning
2010 // }
2011
2012 assert(Loc::IsLocType(ExTy) || ExTy->isFunctionType());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002013
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002014 const MemRegion* R = RV->getRegion();
2015 StoreManager& StoreMgr = getStoreManager();
2016
2017 // Delegate to store manager to get the result of casting a region
2018 // to a different type.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002019 const StoreManager::CastResult& Res = StoreMgr.CastRegion(state, R, T);
Ted Kremenek6eddeb12008-12-13 21:49:13 +00002020
2021 // Inspect the result. If the MemRegion* returned is NULL, this
2022 // expression evaluates to UnknownVal.
2023 R = Res.getRegion();
2024 if (R) { V = loc::MemRegionVal(R); } else { V = UnknownVal(); }
2025
2026 // Generate the new node in the ExplodedGraph.
2027 MakeNode(Dst, CastE, N, BindExpr(Res.getState(), CastE, V));
Ted Kremenekabb042f2008-12-13 19:24:37 +00002028 continue;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00002029 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +00002030 // All other cases.
Ted Kremenekc5302912009-03-05 20:22:13 +00002031 DispatchCast: {
2032 MakeNode(Dst, CastE, N, BindExpr(state, CastE,
2033 EvalCast(V, CastE->getType())));
2034 continue;
2035 }
2036
2037 PassThrough: {
2038 MakeNode(Dst, CastE, N, BindExpr(state, CastE, V));
2039 }
Ted Kremenek874d63f2008-01-24 02:02:54 +00002040 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002041}
2042
Ted Kremenek4f090272008-10-27 21:54:31 +00002043void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002044 NodeTy* Pred, NodeSet& Dst,
2045 bool asLValue) {
Ted Kremenek4f090272008-10-27 21:54:31 +00002046 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
2047 NodeSet Tmp;
2048 Visit(ILE, Pred, Tmp);
2049
2050 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002051 const GRState* state = GetState(*I);
2052 SVal ILV = GetSVal(state, ILE);
2053 state = StateMgr.BindCompoundLiteral(state, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +00002054
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002055 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002056 MakeNode(Dst, CL, *I, BindExpr(state, CL, StateMgr.GetLValue(state, CL)));
Zhongxing Xuf22679e2008-11-07 10:38:33 +00002057 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002058 MakeNode(Dst, CL, *I, BindExpr(state, CL, ILV));
Ted Kremenek4f090272008-10-27 21:54:31 +00002059 }
2060}
2061
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002062void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002063
Ted Kremenek8369a8b2008-10-06 18:43:53 +00002064 // The CFG has one DeclStmt per Decl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002065 Decl* D = *DS->decl_begin();
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002066
2067 if (!D || !isa<VarDecl>(D))
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002068 return;
Ted Kremenek9de04c42008-01-24 20:55:43 +00002069
Ted Kremenekefd59942008-12-08 22:47:34 +00002070 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenekaf337412008-11-12 19:24:17 +00002071 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002072
2073 // FIXME: static variables may have an initializer, but the second
2074 // time a function is called those values may not be current.
2075 NodeSet Tmp;
2076
Ted Kremenekaf337412008-11-12 19:24:17 +00002077 if (InitEx)
2078 Visit(InitEx, Pred, Tmp);
Ted Kremeneke6c62e32008-08-28 18:34:26 +00002079
2080 if (Tmp.empty())
2081 Tmp.Add(Pred);
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002082
2083 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002084 const GRState* state = GetState(*I);
Ted Kremenekaf337412008-11-12 19:24:17 +00002085 unsigned Count = Builder->getCurrentBlockCount();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002086
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002087 // Check if 'VD' is a VLA and if so check if has a non-zero size.
2088 QualType T = getContext().getCanonicalType(VD->getType());
2089 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
2090 // FIXME: Handle multi-dimensional VLAs.
2091
2092 Expr* SE = VLA->getSizeExpr();
2093 SVal Size = GetSVal(state, SE);
2094
2095 if (Size.isUndef()) {
2096 if (NodeTy* N = Builder->generateNode(DS, state, Pred)) {
2097 N->markAsSink();
2098 ExplicitBadSizedVLA.insert(N);
2099 }
2100 continue;
2101 }
2102
2103 bool isFeasibleZero = false;
2104 const GRState* ZeroSt = Assume(state, Size, false, isFeasibleZero);
2105
2106 bool isFeasibleNotZero = false;
2107 state = Assume(state, Size, true, isFeasibleNotZero);
2108
2109 if (isFeasibleZero) {
2110 if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
2111 N->markAsSink();
2112 if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
2113 else ExplicitBadSizedVLA.insert(N);
2114 }
2115 }
2116
2117 if (!isFeasibleNotZero)
2118 continue;
2119 }
2120
Zhongxing Xu4193eca2008-12-20 06:32:12 +00002121 // Decls without InitExpr are not initialized explicitly.
Ted Kremenekaf337412008-11-12 19:24:17 +00002122 if (InitEx) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002123 SVal InitVal = GetSVal(state, InitEx);
Ted Kremenekaf337412008-11-12 19:24:17 +00002124 QualType T = VD->getType();
2125
2126 // Recover some path-sensitivity if a scalar value evaluated to
2127 // UnknownVal.
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002128 if (InitVal.isUnknown() ||
2129 !getConstraintManager().canReasonAbout(InitVal)) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002130 InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
Ted Kremenekaf337412008-11-12 19:24:17 +00002131 }
2132
Ted Kremeneka8538d92009-02-13 01:45:31 +00002133 state = StateMgr.BindDecl(state, VD, InitVal);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002134
2135 // The next thing to do is check if the GRTransferFuncs object wants to
2136 // update the state based on the new binding. If the GRTransferFunc
2137 // object doesn't do anything, just auto-propagate the current state.
2138 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
2139 getTF().EvalBind(BuilderRef, loc::MemRegionVal(StateMgr.getRegion(VD)),
2140 InitVal);
2141 }
2142 else {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002143 state = StateMgr.BindDeclWithNoInit(state, VD);
Ted Kremenek5b8d9012009-02-14 01:54:57 +00002144 MakeNode(Dst, DS, *I, state);
Ted Kremenekefd59942008-12-08 22:47:34 +00002145 }
Ted Kremenek5b7dcce2008-04-22 22:25:27 +00002146 }
Ted Kremenek9de04c42008-01-24 20:55:43 +00002147}
Ted Kremenek874d63f2008-01-24 02:02:54 +00002148
Ted Kremenekf75b1862008-10-30 17:47:32 +00002149namespace {
2150 // This class is used by VisitInitListExpr as an item in a worklist
2151 // for processing the values contained in an InitListExpr.
2152class VISIBILITY_HIDDEN InitListWLItem {
2153public:
2154 llvm::ImmutableList<SVal> Vals;
2155 GRExprEngine::NodeTy* N;
2156 InitListExpr::reverse_iterator Itr;
2157
2158 InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
2159 InitListExpr::reverse_iterator itr)
2160 : Vals(vals), N(n), Itr(itr) {}
2161};
2162}
2163
2164
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002165void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
2166 NodeSet& Dst) {
Ted Kremeneka49e3672008-10-30 23:14:36 +00002167
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002168 const GRState* state = GetState(Pred);
Ted Kremenek76dba7b2008-11-13 05:05:34 +00002169 QualType T = getContext().getCanonicalType(E->getType());
Ted Kremenekf75b1862008-10-30 17:47:32 +00002170 unsigned NumInitElements = E->getNumInits();
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002171
Zhongxing Xu05d1c572008-10-30 05:35:59 +00002172 if (T->isArrayType() || T->isStructureType()) {
Ted Kremenekf75b1862008-10-30 17:47:32 +00002173
Ted Kremeneka49e3672008-10-30 23:14:36 +00002174 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Ted Kremenekf75b1862008-10-30 17:47:32 +00002175
Ted Kremeneka49e3672008-10-30 23:14:36 +00002176 // Handle base case where the initializer has no elements.
2177 // e.g: static int* myArray[] = {};
2178 if (NumInitElements == 0) {
2179 SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
2180 MakeNode(Dst, E, Pred, BindExpr(state, E, V));
2181 return;
2182 }
2183
2184 // Create a worklist to process the initializers.
2185 llvm::SmallVector<InitListWLItem, 10> WorkList;
2186 WorkList.reserve(NumInitElements);
2187 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002188 InitListExpr::reverse_iterator ItrEnd = E->rend();
2189
Ted Kremeneka49e3672008-10-30 23:14:36 +00002190 // Process the worklist until it is empty.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002191 while (!WorkList.empty()) {
2192 InitListWLItem X = WorkList.back();
2193 WorkList.pop_back();
2194
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002195 NodeSet Tmp;
Ted Kremenekf75b1862008-10-30 17:47:32 +00002196 Visit(*X.Itr, X.N, Tmp);
2197
2198 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002199
Ted Kremenekf75b1862008-10-30 17:47:32 +00002200 for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
2201 // Get the last initializer value.
2202 state = GetState(*NI);
2203 SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
2204
2205 // Construct the new list of values by prepending the new value to
2206 // the already constructed list.
2207 llvm::ImmutableList<SVal> NewVals =
2208 getBasicVals().consVals(InitV, X.Vals);
2209
2210 if (NewItr == ItrEnd) {
Zhongxing Xua189dca2008-10-31 03:01:26 +00002211 // Now we have a list holding all init values. Make CompoundValData.
Ted Kremenekf75b1862008-10-30 17:47:32 +00002212 SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002213
Ted Kremenekf75b1862008-10-30 17:47:32 +00002214 // Make final state and node.
Ted Kremenek4456da52008-10-30 18:37:08 +00002215 MakeNode(Dst, E, *NI, BindExpr(state, E, V));
Ted Kremenekf75b1862008-10-30 17:47:32 +00002216 }
2217 else {
2218 // Still some initializer values to go. Push them onto the worklist.
2219 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2220 }
2221 }
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002222 }
Ted Kremenek87903072008-10-30 18:34:31 +00002223
2224 return;
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002225 }
2226
Ted Kremenek062e2f92008-11-13 06:10:40 +00002227 if (T->isUnionType() || T->isVectorType()) {
2228 // FIXME: to be implemented.
2229 // Note: That vectors can return true for T->isIntegerType()
2230 MakeNode(Dst, E, Pred, state);
2231 return;
2232 }
2233
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002234 if (Loc::IsLocType(T) || T->isIntegerType()) {
2235 assert (E->getNumInits() == 1);
2236 NodeSet Tmp;
2237 Expr* Init = E->getInit(0);
2238 Visit(Init, Pred, Tmp);
2239 for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
2240 state = GetState(*I);
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002241 MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, Init)));
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002242 }
2243 return;
2244 }
2245
Zhongxing Xuc4f87062008-10-30 05:02:23 +00002246
2247 printf("InitListExpr type = %s\n", T.getAsString().c_str());
2248 assert(0 && "unprocessed InitListExpr type");
2249}
Ted Kremenekf233d482008-02-05 00:26:40 +00002250
Sebastian Redl05189992008-11-11 17:56:53 +00002251/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2252void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
2253 NodeTy* Pred,
2254 NodeSet& Dst) {
2255 QualType T = Ex->getTypeOfArgument();
Ted Kremenek87e80342008-03-15 03:13:20 +00002256 uint64_t amt;
2257
2258 if (Ex->isSizeOf()) {
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002259 if (T == getContext().VoidTy) {
2260 // sizeof(void) == 1 byte.
2261 amt = 1;
2262 }
2263 else if (!T.getTypePtr()->isConstantSizeType()) {
2264 // FIXME: Add support for VLAs.
Ted Kremenek87e80342008-03-15 03:13:20 +00002265 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002266 }
2267 else if (T->isObjCInterfaceType()) {
2268 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2269 // the compiler has laid out its representation. Just report Unknown
2270 // for these.
Ted Kremenekf342d182008-04-30 21:31:12 +00002271 return;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002272 }
2273 else {
2274 // All other cases.
Ted Kremenek87e80342008-03-15 03:13:20 +00002275 amt = getContext().getTypeSize(T) / 8;
Ted Kremenek55f7bcb2008-12-15 18:51:00 +00002276 }
Ted Kremenek87e80342008-03-15 03:13:20 +00002277 }
2278 else // Get alignment of the type.
Ted Kremenek897781a2008-03-15 03:13:55 +00002279 amt = getContext().getTypeAlign(T) / 8;
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002280
Ted Kremenek0e561a32008-03-21 21:30:14 +00002281 MakeNode(Dst, Ex, Pred,
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +00002282 BindExpr(GetState(Pred), Ex,
2283 NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
Ted Kremenekd9435bf2008-02-12 19:49:57 +00002284}
2285
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002286
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002287void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002288 NodeSet& Dst, bool asLValue) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002289
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002290 switch (U->getOpcode()) {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002291
2292 default:
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002293 break;
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002294
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002295 case UnaryOperator::Deref: {
2296
2297 Expr* Ex = U->getSubExpr()->IgnoreParens();
2298 NodeSet Tmp;
2299 Visit(Ex, Pred, Tmp);
2300
2301 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002302
Ted Kremeneka8538d92009-02-13 01:45:31 +00002303 const GRState* state = GetState(*I);
2304 SVal location = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002305
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002306 if (asLValue)
Ted Kremeneka8538d92009-02-13 01:45:31 +00002307 MakeNode(Dst, U, *I, BindExpr(state, U, location));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002308 else
Ted Kremeneka8538d92009-02-13 01:45:31 +00002309 EvalLoad(Dst, U, *I, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002310 }
2311
2312 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002313 }
Ted Kremeneka084bb62008-04-30 21:45:55 +00002314
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002315 case UnaryOperator::Real: {
2316
2317 Expr* Ex = U->getSubExpr()->IgnoreParens();
2318 NodeSet Tmp;
2319 Visit(Ex, Pred, Tmp);
2320
2321 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
2322
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002323 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002324 if (Ex->getType()->isAnyComplexType()) {
2325 // Just report "Unknown."
2326 Dst.Add(*I);
2327 continue;
2328 }
2329
2330 // For all other types, UnaryOperator::Real is an identity operation.
2331 assert (U->getType() == Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002332 const GRState* state = GetState(*I);
2333 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002334 }
2335
2336 return;
2337 }
2338
2339 case UnaryOperator::Imag: {
2340
2341 Expr* Ex = U->getSubExpr()->IgnoreParens();
2342 NodeSet Tmp;
2343 Visit(Ex, Pred, Tmp);
2344
2345 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002346 // FIXME: We don't have complex SValues yet.
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002347 if (Ex->getType()->isAnyComplexType()) {
2348 // Just report "Unknown."
2349 Dst.Add(*I);
2350 continue;
2351 }
2352
2353 // For all other types, UnaryOperator::Float returns 0.
2354 assert (Ex->getType()->isIntegerType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002355 const GRState* state = GetState(*I);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002356 SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002357 MakeNode(Dst, U, *I, BindExpr(state, U, X));
Ted Kremenekb8e26e62008-06-19 17:55:38 +00002358 }
2359
2360 return;
2361 }
2362
2363 // FIXME: Just report "Unknown" for OffsetOf.
Ted Kremeneka084bb62008-04-30 21:45:55 +00002364 case UnaryOperator::OffsetOf:
Ted Kremeneka084bb62008-04-30 21:45:55 +00002365 Dst.Add(Pred);
2366 return;
2367
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002368 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002369 case UnaryOperator::Extension: {
2370
2371 // Unary "+" is a no-op, similar to a parentheses. We still have places
2372 // where it may be a block-level expression, so we need to
2373 // generate an extra node that just propagates the value of the
2374 // subexpression.
2375
2376 Expr* Ex = U->getSubExpr()->IgnoreParens();
2377 NodeSet Tmp;
2378 Visit(Ex, Pred, Tmp);
2379
2380 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002381 const GRState* state = GetState(*I);
2382 MakeNode(Dst, U, *I, BindExpr(state, U, GetSVal(state, Ex)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002383 }
2384
2385 return;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002386 }
Ted Kremenek7b8009a2008-01-24 02:28:56 +00002387
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002388 case UnaryOperator::AddrOf: {
Ted Kremenekd8e9f0d2008-02-20 04:02:35 +00002389
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002390 assert(!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002391 Expr* Ex = U->getSubExpr()->IgnoreParens();
2392 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002393 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002394
2395 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002396 const GRState* state = GetState(*I);
2397 SVal V = GetSVal(state, Ex);
2398 state = BindExpr(state, U, V);
2399 MakeNode(Dst, U, *I, state);
Ted Kremenek89063af2008-02-21 19:15:37 +00002400 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002401
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002402 return;
2403 }
2404
2405 case UnaryOperator::LNot:
2406 case UnaryOperator::Minus:
2407 case UnaryOperator::Not: {
2408
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002409 assert (!asLValue);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002410 Expr* Ex = U->getSubExpr()->IgnoreParens();
2411 NodeSet Tmp;
2412 Visit(Ex, Pred, Tmp);
2413
2414 for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002415 const GRState* state = GetState(*I);
Ted Kremenek855cd902008-09-30 05:32:44 +00002416
2417 // Get the value of the subexpression.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002418 SVal V = GetSVal(state, Ex);
Ted Kremenek855cd902008-09-30 05:32:44 +00002419
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002420 if (V.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002421 MakeNode(Dst, U, *I, BindExpr(state, U, V));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +00002422 continue;
2423 }
2424
Ted Kremenek60595da2008-11-15 04:01:56 +00002425// QualType DstT = getContext().getCanonicalType(U->getType());
2426// QualType SrcT = getContext().getCanonicalType(Ex->getType());
2427//
2428// if (DstT != SrcT) // Perform promotions.
2429// V = EvalCast(V, DstT);
2430//
2431// if (V.isUnknownOrUndef()) {
2432// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2433// continue;
2434// }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002435
2436 switch (U->getOpcode()) {
2437 default:
2438 assert(false && "Invalid Opcode.");
2439 break;
2440
2441 case UnaryOperator::Not:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002442 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002443 state = BindExpr(state, U, EvalComplement(cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002444 break;
2445
2446 case UnaryOperator::Minus:
Ted Kremenek60a6e0c2008-10-01 00:21:14 +00002447 // FIXME: Do we need to handle promotions?
Ted Kremeneka8538d92009-02-13 01:45:31 +00002448 state = BindExpr(state, U, EvalMinus(U, cast<NonLoc>(V)));
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002449 break;
2450
2451 case UnaryOperator::LNot:
2452
2453 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2454 //
2455 // Note: technically we do "E == 0", but this is the same in the
2456 // transfer functions as "0 == E".
2457
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002458 if (isa<Loc>(V)) {
Ted Kremenekda9ae602009-04-08 18:51:08 +00002459 Loc X = Loc::MakeNull(getBasicVals());
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002460 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
2461 U->getType());
Ted Kremeneka8538d92009-02-13 01:45:31 +00002462 state = BindExpr(state, U, Result);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002463 }
2464 else {
Ted Kremenek60595da2008-11-15 04:01:56 +00002465 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002466#if 0
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002467 SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
Ted Kremeneka8538d92009-02-13 01:45:31 +00002468 state = SetSVal(state, U, Result);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002469#else
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002470 EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I,
2471 U->getType());
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002472 continue;
2473#endif
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002474 }
2475
2476 break;
2477 }
2478
Ted Kremeneka8538d92009-02-13 01:45:31 +00002479 MakeNode(Dst, U, *I, state);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002480 }
2481
2482 return;
2483 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002484 }
2485
2486 // Handle ++ and -- (both pre- and post-increment).
2487
2488 assert (U->isIncrementDecrementOp());
2489 NodeSet Tmp;
2490 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002491 VisitLValue(Ex, Pred, Tmp);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002492
2493 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
2494
Ted Kremeneka8538d92009-02-13 01:45:31 +00002495 const GRState* state = GetState(*I);
2496 SVal V1 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002497
2498 // Perform a load.
2499 NodeSet Tmp2;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002500 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002501
2502 for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
2503
Ted Kremeneka8538d92009-02-13 01:45:31 +00002504 state = GetState(*I2);
2505 SVal V2 = GetSVal(state, Ex);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002506
2507 // Propagate unknown and undefined values.
2508 if (V2.isUnknownOrUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002509 MakeNode(Dst, U, *I2, BindExpr(state, U, V2));
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002510 continue;
2511 }
2512
Ted Kremenek21028dd2009-03-11 03:54:24 +00002513 // Handle all other values.
Ted Kremenek50d0ac22008-02-15 22:09:30 +00002514 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2515 : BinaryOperator::Sub;
Ted Kremenek21028dd2009-03-11 03:54:24 +00002516
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002517 SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U), U->getType());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002518
2519 // Conjure a new symbol if necessary to recover precision.
2520 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result))
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002521 Result = ValMgr.getConjuredSymbolVal(Ex,
2522 Builder->getCurrentBlockCount());
Ted Kremenekbb9b2712009-03-20 20:10:45 +00002523
Ted Kremeneka8538d92009-02-13 01:45:31 +00002524 state = BindExpr(state, U, U->isPostfix() ? V2 : Result);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002525
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002526 // Perform the store.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002527 EvalStore(Dst, U, *I2, state, V1, Result);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002528 }
Ted Kremenek469ecbd2008-04-21 23:43:38 +00002529 }
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002530}
2531
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002532void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
2533 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
2534}
2535
2536void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2537 AsmStmt::outputs_iterator I,
2538 AsmStmt::outputs_iterator E,
2539 NodeTy* Pred, NodeSet& Dst) {
2540 if (I == E) {
2541 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2542 return;
2543 }
2544
2545 NodeSet Tmp;
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002546 VisitLValue(*I, Pred, Tmp);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002547
2548 ++I;
2549
2550 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2551 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2552}
2553
2554void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2555 AsmStmt::inputs_iterator I,
2556 AsmStmt::inputs_iterator E,
2557 NodeTy* Pred, NodeSet& Dst) {
2558 if (I == E) {
2559
2560 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002561 // should evaluate to Locs. Nuke all of their values.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002562
2563 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2564 // which interprets the inline asm and stores proper results in the
2565 // outputs.
2566
Ted Kremeneka8538d92009-02-13 01:45:31 +00002567 const GRState* state = GetState(Pred);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002568
2569 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2570 OE = A->end_outputs(); OI != OE; ++OI) {
2571
Ted Kremeneka8538d92009-02-13 01:45:31 +00002572 SVal X = GetSVal(state, *OI);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002573 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002574
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002575 if (isa<Loc>(X))
Ted Kremeneka8538d92009-02-13 01:45:31 +00002576 state = BindLoc(state, cast<Loc>(X), UnknownVal());
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002577 }
2578
Ted Kremeneka8538d92009-02-13 01:45:31 +00002579 MakeNode(Dst, A, Pred, state);
Ted Kremenekef44bfb2008-03-17 21:11:24 +00002580 return;
2581 }
2582
2583 NodeSet Tmp;
2584 Visit(*I, Pred, Tmp);
2585
2586 ++I;
2587
2588 for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
2589 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2590}
2591
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002592void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
2593 assert (Builder && "GRStmtNodeBuilder must be defined.");
2594
2595 unsigned size = Dst.size();
Ted Kremenekb0533962008-04-18 20:35:30 +00002596
Ted Kremenek186350f2008-04-23 20:12:28 +00002597 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2598 SaveOr OldHasGen(Builder->HasGeneratedNode);
Ted Kremenekb0533962008-04-18 20:35:30 +00002599
Ted Kremenek729a9a22008-07-17 23:15:45 +00002600 getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002601
Ted Kremenekb0533962008-04-18 20:35:30 +00002602 // Handle the case where no nodes where generated.
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002603
Ted Kremenekb0533962008-04-18 20:35:30 +00002604 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002605 MakeNode(Dst, S, Pred, GetState(Pred));
2606}
2607
Ted Kremenek02737ed2008-03-31 15:02:58 +00002608void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
2609
2610 Expr* R = S->getRetValue();
2611
2612 if (!R) {
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002613 EvalReturn(Dst, S, Pred);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002614 return;
2615 }
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002616
Ted Kremenek5917d782008-11-21 00:27:44 +00002617 NodeSet Tmp;
2618 Visit(R, Pred, Tmp);
Ted Kremenek02737ed2008-03-31 15:02:58 +00002619
Ted Kremenek5917d782008-11-21 00:27:44 +00002620 for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
2621 SVal X = GetSVal((*I)->getState(), R);
2622
2623 // Check if we return the address of a stack variable.
2624 if (isa<loc::MemRegionVal>(X)) {
2625 // Determine if the value is on the stack.
2626 const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
Ted Kremenek02737ed2008-03-31 15:02:58 +00002627
Ted Kremenek5917d782008-11-21 00:27:44 +00002628 if (R && getStateManager().hasStackStorage(R)) {
2629 // Create a special node representing the error.
2630 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2631 N->markAsSink();
2632 RetsStackAddr.insert(N);
2633 }
2634 continue;
2635 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002636 }
Ted Kremenek5917d782008-11-21 00:27:44 +00002637 // Check if we return an undefined value.
2638 else if (X.isUndef()) {
2639 if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
2640 N->markAsSink();
2641 RetsUndef.insert(N);
2642 }
2643 continue;
2644 }
2645
Ted Kremenek6b31e8e2008-04-16 23:05:51 +00002646 EvalReturn(Dst, S, *I);
Ted Kremenek5917d782008-11-21 00:27:44 +00002647 }
Ted Kremenek02737ed2008-03-31 15:02:58 +00002648}
Ted Kremenek55deb972008-03-25 00:34:37 +00002649
Ted Kremeneke695e1c2008-04-15 23:06:53 +00002650//===----------------------------------------------------------------------===//
2651// Transfer functions: Binary operators.
2652//===----------------------------------------------------------------------===//
2653
Ted Kremeneka8538d92009-02-13 01:45:31 +00002654const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* state,
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002655 NodeTy* Pred, SVal Denom) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002656
2657 // Divide by undefined? (potentially zero)
2658
2659 if (Denom.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002660 NodeTy* DivUndef = Builder->generateNode(Ex, state, Pred);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002661
2662 if (DivUndef) {
2663 DivUndef->markAsSink();
2664 ExplicitBadDivides.insert(DivUndef);
2665 }
2666
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002667 return 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002668 }
2669
2670 // Check for divide/remainder-by-zero.
2671 // First, "assume" that the denominator is 0 or undefined.
2672
2673 bool isFeasibleZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002674 const GRState* ZeroSt = Assume(state, Denom, false, isFeasibleZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002675
2676 // Second, "assume" that the denominator cannot be 0.
2677
2678 bool isFeasibleNotZero = false;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002679 state = Assume(state, Denom, true, isFeasibleNotZero);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002680
2681 // Create the node for the divide-by-zero (if it occurred).
2682
2683 if (isFeasibleZero)
2684 if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
2685 DivZeroNode->markAsSink();
2686
2687 if (isFeasibleNotZero)
2688 ImplicitBadDivides.insert(DivZeroNode);
2689 else
2690 ExplicitBadDivides.insert(DivZeroNode);
2691
2692 }
2693
Ted Kremeneka8538d92009-02-13 01:45:31 +00002694 return isFeasibleNotZero ? state : 0;
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002695}
2696
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002697void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00002698 GRExprEngine::NodeTy* Pred,
2699 GRExprEngine::NodeSet& Dst) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002700
2701 NodeSet Tmp1;
2702 Expr* LHS = B->getLHS()->IgnoreParens();
2703 Expr* RHS = B->getRHS()->IgnoreParens();
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002704
Ted Kremenek759623e2008-12-06 02:39:30 +00002705 // FIXME: Add proper support for ObjCKVCRefExpr.
2706 if (isa<ObjCKVCRefExpr>(LHS)) {
2707 Visit(RHS, Pred, Dst);
2708 return;
2709 }
2710
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002711 if (B->isAssignmentOp())
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +00002712 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek5b6dc2d2008-02-07 01:08:27 +00002713 else
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002714 Visit(LHS, Pred, Tmp1);
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002715
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002716 for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002717
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002718 SVal LeftV = GetSVal((*I1)->getState(), LHS);
Ted Kremeneke00fe3f2008-01-17 00:52:48 +00002719
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002720 // Process the RHS.
2721
2722 NodeSet Tmp2;
2723 Visit(RHS, *I1, Tmp2);
2724
2725 // With both the LHS and RHS evaluated, process the operation itself.
2726
2727 for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002728
Ted Kremeneka8538d92009-02-13 01:45:31 +00002729 const GRState* state = GetState(*I2);
2730 const GRState* OldSt = state;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002731
Ted Kremeneka8538d92009-02-13 01:45:31 +00002732 SVal RightV = GetSVal(state, RHS);
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002733 BinaryOperator::Opcode Op = B->getOpcode();
2734
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002735 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002736
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002737 case BinaryOperator::Assign: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002738
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002739 // EXPERIMENTAL: "Conjured" symbols.
Ted Kremenekfd301942008-10-17 22:23:12 +00002740 // FIXME: Handle structs.
2741 QualType T = RHS->getType();
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002742
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002743 if ((RightV.isUnknown() ||
2744 !getConstraintManager().canReasonAbout(RightV))
2745 && (Loc::IsLocType(T) ||
2746 (T->isScalarType() && T->isIntegerType()))) {
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002747 unsigned Count = Builder->getCurrentBlockCount();
2748 RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002749 }
2750
Ted Kremenek361fa8e2008-03-12 21:45:47 +00002751 // Simulate the effects of a "store": bind the value of the RHS
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002752 // to the L-Value represented by the LHS.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002753 EvalStore(Dst, B, LHS, *I2, BindExpr(state, B, RightV), LeftV,
2754 RightV);
Ted Kremeneke38718e2008-04-16 18:21:25 +00002755 continue;
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002756 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002757
2758 case BinaryOperator::Div:
2759 case BinaryOperator::Rem:
2760
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002761 // Special checking for integer denominators.
Ted Kremenek062e2f92008-11-13 06:10:40 +00002762 if (RHS->getType()->isIntegerType() &&
2763 RHS->getType()->isScalarType()) {
2764
Ted Kremeneka8538d92009-02-13 01:45:31 +00002765 state = CheckDivideZero(B, state, *I2, RightV);
2766 if (!state) continue;
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002767 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002768
2769 // FALL-THROUGH.
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002770
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002771 default: {
2772
2773 if (B->isAssignmentOp())
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002774 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002775
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002776 // Process non-assignements except commas or short-circuited
2777 // logical expressions (LAnd and LOr).
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002778
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002779 SVal Result = EvalBinOp(Op, LeftV, RightV, B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002780
2781 if (Result.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002782 if (OldSt != state) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002783 // Generate a new node if we have already created a new state.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002784 MakeNode(Dst, B, *I2, state);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002785 }
2786 else
2787 Dst.Add(*I2);
2788
Ted Kremenek89063af2008-02-21 19:15:37 +00002789 continue;
2790 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002791
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002792 if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002793
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002794 // The operands were *not* undefined, but the result is undefined.
2795 // This is a special node that should be flagged as an error.
Ted Kremenek3c8d0c52008-02-25 18:42:54 +00002796
Ted Kremeneka8538d92009-02-13 01:45:31 +00002797 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I2)) {
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00002798 UndefNode->markAsSink();
2799 UndefResults.insert(UndefNode);
2800 }
2801
2802 continue;
2803 }
2804
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002805 // Otherwise, create a new node.
2806
Ted Kremeneka8538d92009-02-13 01:45:31 +00002807 MakeNode(Dst, B, *I2, BindExpr(state, B, Result));
Ted Kremeneke38718e2008-04-16 18:21:25 +00002808 continue;
Ted Kremenekcf78b6a2008-02-06 22:50:25 +00002809 }
Ted Kremenekab2b8c52008-01-23 19:59:44 +00002810 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +00002811
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002812 assert (B->isCompoundAssignmentOp());
2813
Ted Kremenekcad29962009-02-07 00:52:24 +00002814 switch (Op) {
2815 default:
2816 assert(0 && "Invalid opcode for compound assignment.");
2817 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2818 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2819 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2820 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2821 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2822 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2823 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2824 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2825 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2826 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek934e3e92008-10-27 23:02:39 +00002827 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002828
2829 // Perform a load (the LHS). This performs the checks for
2830 // null dereferences, and so on.
2831 NodeSet Tmp3;
Ted Kremeneka8538d92009-02-13 01:45:31 +00002832 SVal location = GetSVal(state, LHS);
2833 EvalLoad(Tmp3, LHS, *I2, state, location);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002834
2835 for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
2836
Ted Kremeneka8538d92009-02-13 01:45:31 +00002837 state = GetState(*I3);
2838 SVal V = GetSVal(state, LHS);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002839
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002840 // Check for divide-by-zero.
2841 if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
Ted Kremenek062e2f92008-11-13 06:10:40 +00002842 && RHS->getType()->isIntegerType()
2843 && RHS->getType()->isScalarType()) {
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002844
2845 // CheckDivideZero returns a new state where the denominator
2846 // is assumed to be non-zero.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002847 state = CheckDivideZero(B, state, *I3, RightV);
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002848
Ted Kremeneka8538d92009-02-13 01:45:31 +00002849 if (!state)
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002850 continue;
2851 }
2852
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002853 // Propagate undefined values (left-side).
2854 if (V.isUndef()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002855 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, V), location, V);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002856 continue;
2857 }
2858
2859 // Propagate unknown values (left and right-side).
2860 if (RightV.isUnknown() || V.isUnknown()) {
Ted Kremeneka8538d92009-02-13 01:45:31 +00002861 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, UnknownVal()),
2862 location, UnknownVal());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002863 continue;
2864 }
2865
2866 // At this point:
2867 //
2868 // The LHS is not Undef/Unknown.
2869 // The RHS is not Unknown.
2870
2871 // Get the computation type.
Eli Friedmanab3a8522009-03-28 01:22:36 +00002872 QualType CTy = cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek60595da2008-11-15 04:01:56 +00002873 CTy = getContext().getCanonicalType(CTy);
Eli Friedmanab3a8522009-03-28 01:22:36 +00002874
2875 QualType CLHSTy = cast<CompoundAssignOperator>(B)->getComputationLHSType();
2876 CLHSTy = getContext().getCanonicalType(CTy);
2877
Ted Kremenek60595da2008-11-15 04:01:56 +00002878 QualType LTy = getContext().getCanonicalType(LHS->getType());
2879 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedmanab3a8522009-03-28 01:22:36 +00002880
2881 // Promote LHS.
2882 V = EvalCast(V, CLHSTy);
2883
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002884 // Evaluate operands and promote to result type.
Ted Kremenekc13b6e22008-10-20 23:40:25 +00002885 if (RightV.isUndef()) {
Ted Kremenek82bae3f2008-09-20 01:50:34 +00002886 // Propagate undefined values (right-side).
Ted Kremeneke121da42009-03-05 03:42:31 +00002887 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, RightV), location,
Ted Kremeneka8538d92009-02-13 01:45:31 +00002888 RightV);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002889 continue;
2890 }
2891
Ted Kremenek60595da2008-11-15 04:01:56 +00002892 // Compute the result of the operation.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002893 SVal Result = EvalCast(EvalBinOp(Op, V, RightV, CTy), B->getType());
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002894
2895 if (Result.isUndef()) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002896 // The operands were not undefined, but the result is undefined.
Ted Kremeneka8538d92009-02-13 01:45:31 +00002897 if (NodeTy* UndefNode = Builder->generateNode(B, state, *I3)) {
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002898 UndefNode->markAsSink();
2899 UndefResults.insert(UndefNode);
2900 }
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002901 continue;
2902 }
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002903
2904 // EXPERIMENTAL: "Conjured" symbols.
2905 // FIXME: Handle structs.
Ted Kremenek60595da2008-11-15 04:01:56 +00002906
2907 SVal LHSVal;
2908
Ted Kremenek276c6ac2009-03-11 02:24:48 +00002909 if ((Result.isUnknown() ||
2910 !getConstraintManager().canReasonAbout(Result))
2911 && (Loc::IsLocType(CTy)
2912 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Ted Kremenek0944ccc2008-10-21 19:49:01 +00002913
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002914 unsigned Count = Builder->getCurrentBlockCount();
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002915
Ted Kremenek60595da2008-11-15 04:01:56 +00002916 // The symbolic value is actually for the type of the left-hand side
2917 // expression, not the computation type, as this is the value the
2918 // LValue on the LHS will bind to.
Ted Kremenek8d7f5482009-04-09 22:22:44 +00002919 LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
Ted Kremenek60595da2008-11-15 04:01:56 +00002920
Zhongxing Xu1c0c2332008-11-23 05:52:28 +00002921 // However, we need to convert the symbol to the computation type.
Ted Kremenek60595da2008-11-15 04:01:56 +00002922 Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
Ted Kremenek9ff267d2008-10-20 23:13:25 +00002923 }
Ted Kremenek60595da2008-11-15 04:01:56 +00002924 else {
2925 // The left-hand side may bind to a different value then the
2926 // computation type.
2927 LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
2928 }
2929
Ted Kremeneka8538d92009-02-13 01:45:31 +00002930 EvalStore(Dst, B, LHS, *I3, BindExpr(state, B, Result), location,
2931 LHSVal);
Ted Kremenek1b8bd4d2008-04-29 21:04:26 +00002932 }
Ted Kremenekcb448ca2008-01-16 00:53:15 +00002933 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002934 }
Ted Kremenekd27f8162008-01-15 23:55:06 +00002935}
Ted Kremenekee985462008-01-16 18:18:48 +00002936
2937//===----------------------------------------------------------------------===//
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002938// Transfer-function Helpers.
2939//===----------------------------------------------------------------------===//
2940
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002941void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002942 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002943 NonLoc L, NonLoc R,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002944 ExplodedNode<GRState>* Pred, QualType T) {
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002945
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002946 GRStateSet OStates;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002947 EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R, T);
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002948
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002949 for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002950 MakeNode(Dst, Ex, Pred, *I);
2951}
2952
Ted Kremeneka8538d92009-02-13 01:45:31 +00002953void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* state,
Ted Kremenek6297a8e2008-07-18 05:53:58 +00002954 Expr* Ex, BinaryOperator::Opcode Op,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002955 NonLoc L, NonLoc R, QualType T) {
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002956
Ted Kremeneka8538d92009-02-13 01:45:31 +00002957 GRStateSet::AutoPopulate AP(OStates, state);
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002958 if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, state, Ex, Op, L, R, T);
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002959}
2960
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002961SVal GRExprEngine::EvalBinOp(BinaryOperator::Opcode Op, SVal L, SVal R,
2962 QualType T) {
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002963
2964 if (L.isUndef() || R.isUndef())
2965 return UndefinedVal();
2966
2967 if (L.isUnknown() || R.isUnknown())
2968 return UnknownVal();
2969
2970 if (isa<Loc>(L)) {
2971 if (isa<Loc>(R))
2972 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<Loc>(R));
2973 else
2974 return getTF().EvalBinOp(*this, Op, cast<Loc>(L), cast<NonLoc>(R));
2975 }
2976
2977 if (isa<Loc>(R)) {
2978 // Support pointer arithmetic where the increment/decrement operand
2979 // is on the left and the pointer on the right.
2980
2981 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
2982
2983 // Commute the operands.
2984 return getTF().EvalBinOp(*this, Op, cast<Loc>(R),
2985 cast<NonLoc>(L));
2986 }
2987 else
2988 return getTF().DetermEvalBinOpNN(*this, Op, cast<NonLoc>(L),
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002989 cast<NonLoc>(R), T);
Ted Kremenek1e2b1fc2009-01-30 19:27:39 +00002990}
2991
Ted Kremenekdf7533b2008-07-17 21:27:31 +00002992//===----------------------------------------------------------------------===//
Ted Kremeneke01c9872008-02-14 22:36:46 +00002993// Visualization.
Ted Kremenekee985462008-01-16 18:18:48 +00002994//===----------------------------------------------------------------------===//
2995
Ted Kremenekaa66a322008-01-16 21:46:15 +00002996#ifndef NDEBUG
Ted Kremenek4d4dd852008-02-13 17:41:41 +00002997static GRExprEngine* GraphPrintCheckerState;
Ted Kremeneke97ca062008-03-07 20:57:30 +00002998static SourceManager* GraphPrintSourceManager;
Ted Kremenek3b4f6702008-01-30 23:24:39 +00002999
Ted Kremenekaa66a322008-01-16 21:46:15 +00003000namespace llvm {
3001template<>
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003002struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
Ted Kremenekaa66a322008-01-16 21:46:15 +00003003 public DefaultDOTGraphTraits {
Ted Kremenek016f52f2008-02-08 21:10:02 +00003004
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003005 static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
3006
3007 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek9dca0622008-02-19 00:22:37 +00003008 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003009 GraphPrintCheckerState->isUndefDeref(N) ||
3010 GraphPrintCheckerState->isUndefStore(N) ||
3011 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek4d839b42008-03-07 19:04:53 +00003012 GraphPrintCheckerState->isExplicitBadDivide(N) ||
3013 GraphPrintCheckerState->isImplicitBadDivide(N) ||
Ted Kremenek5e03fcb2008-02-29 23:14:48 +00003014 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek2ded35a2008-02-29 23:53:11 +00003015 GraphPrintCheckerState->isBadCall(N) ||
3016 GraphPrintCheckerState->isUndefArg(N))
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003017 return "color=\"red\",style=\"filled\"";
3018
Ted Kremenek8cc13ea2008-02-28 20:32:03 +00003019 if (GraphPrintCheckerState->isNoReturnCall(N))
3020 return "color=\"blue\",style=\"filled\"";
3021
Ted Kremeneka3fadfc2008-02-14 22:54:53 +00003022 return "";
3023 }
Ted Kremeneked4de312008-02-06 03:56:15 +00003024
Ted Kremenek4d4dd852008-02-13 17:41:41 +00003025 static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
Ted Kremenekaa66a322008-01-16 21:46:15 +00003026 std::ostringstream Out;
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003027
3028 // Program Location.
Ted Kremenekaa66a322008-01-16 21:46:15 +00003029 ProgramPoint Loc = N->getLocation();
3030
3031 switch (Loc.getKind()) {
3032 case ProgramPoint::BlockEntranceKind:
3033 Out << "Block Entrance: B"
3034 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
3035 break;
3036
3037 case ProgramPoint::BlockExitKind:
3038 assert (false);
3039 break;
3040
Ted Kremenekaa66a322008-01-16 21:46:15 +00003041 default: {
Ted Kremenek8c354752008-12-16 22:02:27 +00003042 if (isa<PostStmt>(Loc)) {
3043 const PostStmt& L = cast<PostStmt>(Loc);
3044 Stmt* S = L.getStmt();
3045 SourceLocation SLoc = S->getLocStart();
3046
3047 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
3048 llvm::raw_os_ostream OutS(Out);
3049 S->printPretty(OutS);
3050 OutS.flush();
3051
3052 if (SLoc.isFileID()) {
3053 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003054 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3055 << " col="
3056 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
3057 << "\\l";
Ted Kremenek8c354752008-12-16 22:02:27 +00003058 }
3059
3060 if (GraphPrintCheckerState->isImplicitNullDeref(N))
3061 Out << "\\|Implicit-Null Dereference.\\l";
3062 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
3063 Out << "\\|Explicit-Null Dereference.\\l";
3064 else if (GraphPrintCheckerState->isUndefDeref(N))
3065 Out << "\\|Dereference of undefialied value.\\l";
3066 else if (GraphPrintCheckerState->isUndefStore(N))
3067 Out << "\\|Store to Undefined Loc.";
3068 else if (GraphPrintCheckerState->isExplicitBadDivide(N))
3069 Out << "\\|Explicit divide-by zero or undefined value.";
3070 else if (GraphPrintCheckerState->isImplicitBadDivide(N))
3071 Out << "\\|Implicit divide-by zero or undefined value.";
3072 else if (GraphPrintCheckerState->isUndefResult(N))
3073 Out << "\\|Result of operation is undefined.";
3074 else if (GraphPrintCheckerState->isNoReturnCall(N))
3075 Out << "\\|Call to function marked \"noreturn\".";
3076 else if (GraphPrintCheckerState->isBadCall(N))
3077 Out << "\\|Call to NULL/Undefined.";
3078 else if (GraphPrintCheckerState->isUndefArg(N))
3079 Out << "\\|Argument in call is undefined";
3080
3081 break;
3082 }
3083
Ted Kremenekaa66a322008-01-16 21:46:15 +00003084 const BlockEdge& E = cast<BlockEdge>(Loc);
3085 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
3086 << E.getDst()->getBlockID() << ')';
Ted Kremenekb38911f2008-01-30 23:03:39 +00003087
3088 if (Stmt* T = E.getSrc()->getTerminator()) {
Ted Kremeneke97ca062008-03-07 20:57:30 +00003089
3090 SourceLocation SLoc = T->getLocStart();
3091
Ted Kremenekb38911f2008-01-30 23:03:39 +00003092 Out << "\\|Terminator: ";
Ted Kremeneke97ca062008-03-07 20:57:30 +00003093
Ted Kremeneka95d3752008-09-13 05:16:45 +00003094 llvm::raw_os_ostream OutS(Out);
3095 E.getSrc()->printTerminator(OutS);
3096 OutS.flush();
Ted Kremenekb38911f2008-01-30 23:03:39 +00003097
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003098 if (SLoc.isFileID()) {
3099 Out << "\\lline="
Chris Lattner7da5aea2009-02-04 00:55:58 +00003100 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
3101 << " col="
3102 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek9b5551d2008-03-09 03:30:59 +00003103 }
Ted Kremeneke97ca062008-03-07 20:57:30 +00003104
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003105 if (isa<SwitchStmt>(T)) {
3106 Stmt* Label = E.getDst()->getLabel();
3107
3108 if (Label) {
3109 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
3110 Out << "\\lcase ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003111 llvm::raw_os_ostream OutS(Out);
3112 C->getLHS()->printPretty(OutS);
3113 OutS.flush();
3114
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003115 if (Stmt* RHS = C->getRHS()) {
3116 Out << " .. ";
Ted Kremeneka95d3752008-09-13 05:16:45 +00003117 RHS->printPretty(OutS);
3118 OutS.flush();
Ted Kremenekdaeb9a72008-02-13 23:08:21 +00003119 }
3120
3121 Out << ":";
3122 }
3123 else {
3124 assert (isa<DefaultStmt>(Label));
3125 Out << "\\ldefault:";
3126 }
3127 }
3128 else
3129 Out << "\\l(implicit) default:";
3130 }
3131 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremenekb38911f2008-01-30 23:03:39 +00003132 // FIXME
3133 }
3134 else {
3135 Out << "\\lCondition: ";
3136 if (*E.getSrc()->succ_begin() == E.getDst())
3137 Out << "true";
3138 else
3139 Out << "false";
3140 }
3141
3142 Out << "\\l";
3143 }
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003144
Ted Kremenek4a4e5242008-02-28 09:25:22 +00003145 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3146 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003147 }
Ted Kremenekaa66a322008-01-16 21:46:15 +00003148 }
3149 }
3150
Ted Kremenekaed9b6a2008-02-28 10:21:43 +00003151 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenek016f52f2008-02-08 21:10:02 +00003152
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003153 GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
3154 state.printDOT(Out);
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003155
Ted Kremenek803c9ed2008-01-23 22:30:44 +00003156 Out << "\\l";
Ted Kremenekaa66a322008-01-16 21:46:15 +00003157 return Out.str();
3158 }
3159};
3160} // end llvm namespace
3161#endif
3162
Ted Kremenekffe0f432008-03-07 22:58:01 +00003163#ifndef NDEBUG
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003164template <typename ITERATOR>
3165GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
3166
3167template <>
3168GRExprEngine::NodeTy*
3169GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
3170 (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
3171 return I->first;
3172}
Ted Kremenekffe0f432008-03-07 22:58:01 +00003173#endif
3174
3175void GRExprEngine::ViewGraph(bool trim) {
Ted Kremenek493d7a22008-03-11 18:25:33 +00003176#ifndef NDEBUG
Ted Kremenekffe0f432008-03-07 22:58:01 +00003177 if (trim) {
Ted Kremenekcb612922008-04-18 19:23:43 +00003178 std::vector<NodeTy*> Src;
Ted Kremenek940abcc2009-03-11 01:41:22 +00003179
3180 // Flush any outstanding reports to make sure we cover all the nodes.
3181 // This does not cause them to get displayed.
3182 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3183 const_cast<BugType*>(*I)->FlushReports(BR);
3184
3185 // Iterate through the reports and get their nodes.
3186 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
3187 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) {
3188 const BugReportEquivClass& EQ = *I2;
3189 const BugReport &R = **EQ.begin();
3190 NodeTy *N = const_cast<NodeTy*>(R.getEndNode());
3191 if (N) Src.push_back(N);
3192 }
3193 }
Ted Kremenekcb612922008-04-18 19:23:43 +00003194
Ted Kremenek7ec07fd2008-03-12 17:18:20 +00003195 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenekffe0f432008-03-07 22:58:01 +00003196 }
Ted Kremenek493d7a22008-03-11 18:25:33 +00003197 else {
3198 GraphPrintCheckerState = this;
3199 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenekae6814e2008-08-13 21:24:49 +00003200
Ted Kremenekffe0f432008-03-07 22:58:01 +00003201 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Ted Kremenek493d7a22008-03-11 18:25:33 +00003202
3203 GraphPrintCheckerState = NULL;
3204 GraphPrintSourceManager = NULL;
3205 }
3206#endif
3207}
3208
3209void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
3210#ifndef NDEBUG
3211 GraphPrintCheckerState = this;
3212 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek1c72ef02008-08-16 00:49:49 +00003213
Ted Kremenekcf118d42009-02-04 23:49:09 +00003214 std::auto_ptr<GRExprEngine::GraphTy> TrimmedG(G.Trim(Beg, End).first);
Ted Kremenek493d7a22008-03-11 18:25:33 +00003215
Ted Kremenekcf118d42009-02-04 23:49:09 +00003216 if (!TrimmedG.get())
Ted Kremenek493d7a22008-03-11 18:25:33 +00003217 llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekcf118d42009-02-04 23:49:09 +00003218 else
Ted Kremenek493d7a22008-03-11 18:25:33 +00003219 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
Ted Kremenekffe0f432008-03-07 22:58:01 +00003220
Ted Kremenek3b4f6702008-01-30 23:24:39 +00003221 GraphPrintCheckerState = NULL;
Ted Kremeneke97ca062008-03-07 20:57:30 +00003222 GraphPrintSourceManager = NULL;
Ted Kremeneke01c9872008-02-14 22:36:46 +00003223#endif
Ted Kremenekee985462008-01-16 18:18:48 +00003224}