blob: 610478100719a2a4cd35277478a74802b90959e3 [file] [log] [blame]
Ted Kremenek64de2072008-02-14 22:13:12 +00001//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
Ted Kremeneka0be8262008-01-31 02:35:41 +00002//
Ted Kremenek6f4a9ef2008-01-31 06:49:09 +00003// The LLVM Compiler Infrastructure
Ted Kremenekde8d62b2008-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 Kremenek64de2072008-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 Kremenekde8d62b2008-01-15 23:55:06 +000013//
14//===----------------------------------------------------------------------===//
15
Zhongxing Xuc6123a12009-11-24 08:24:26 +000016#include "GRExprEngineInternalChecks.h"
Ted Kremenek64de2072008-02-14 22:13:12 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremeneke68c0fc2009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek49513cc2009-07-22 21:43:51 +000019#include "clang/Analysis/PathSensitive/Checker.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000020#include "clang/AST/ParentMap.h"
21#include "clang/AST/StmtObjC.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek6947a792008-03-07 20:57:30 +000024#include "clang/Basic/SourceManager.h"
Ted Kremenek91076ca2009-03-11 02:41:36 +000025#include "clang/Basic/PrettyStackTrace.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000027#include "llvm/ADT/ImmutableList.h"
Douglas Gregorf7b87cb2009-10-29 00:41:01 +000028#include "llvm/ADT/StringSwitch.h"
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000029
Ted Kremenekc0258412008-02-27 06:07:00 +000030#ifndef NDEBUG
31#include "llvm/Support/GraphWriter.h"
Ted Kremenekc0258412008-02-27 06:07:00 +000032#endif
33
Ted Kremenekbd8957b2008-02-14 22:16:04 +000034using namespace clang;
35using llvm::dyn_cast;
36using llvm::cast;
37using llvm::APSInt;
Ted Kremenek0a8d3762008-01-23 19:59:44 +000038
Ted Kremenek667cacb2008-04-15 23:06:53 +000039//===----------------------------------------------------------------------===//
Ted Kremenekd0fe8042009-11-25 21:51:20 +000040// Utility functions.
41//===----------------------------------------------------------------------===//
42
43static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
44 IdentifierInfo* II = &Ctx.Idents.get(name);
45 return Ctx.Selectors.getSelector(0, &II);
46}
47
48//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +000049// Batch auditor. DEPRECATED.
Ted Kremenek667cacb2008-04-15 23:06:53 +000050//===----------------------------------------------------------------------===//
51
Ted Kremenekc50e1a12008-07-11 18:37:32 +000052namespace {
53
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000054class MappedBatchAuditor : public GRSimpleAPICheck {
Ted Kremenekc50e1a12008-07-11 18:37:32 +000055 typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
56 typedef llvm::DenseMap<void*,Checks> MapTy;
Mike Stump11289f42009-09-09 15:08:12 +000057
Ted Kremenekc50e1a12008-07-11 18:37:32 +000058 MapTy M;
59 Checks::Factory F;
Ted Kremenek4967c892009-03-30 17:53:05 +000060 Checks AllStmts;
Ted Kremenekc50e1a12008-07-11 18:37:32 +000061
62public:
Ted Kremenek4967c892009-03-30 17:53:05 +000063 MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
64 F(Alloc), AllStmts(F.GetEmptyList()) {}
Mike Stump11289f42009-09-09 15:08:12 +000065
Ted Kremenekc50e1a12008-07-11 18:37:32 +000066 virtual ~MappedBatchAuditor() {
67 llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenekc50e1a12008-07-11 18:37:32 +000069 for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
70 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
71
72 GRSimpleAPICheck* check = *I;
Mike Stump11289f42009-09-09 15:08:12 +000073
Ted Kremenekc50e1a12008-07-11 18:37:32 +000074 if (AlreadyVisited.count(check))
75 continue;
Mike Stump11289f42009-09-09 15:08:12 +000076
Ted Kremenekc50e1a12008-07-11 18:37:32 +000077 AlreadyVisited.insert(check);
78 delete check;
79 }
80 }
81
Ted Kremenek4967c892009-03-30 17:53:05 +000082 void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
Ted Kremenekc50e1a12008-07-11 18:37:32 +000083 assert (A && "Check cannot be null.");
84 void* key = reinterpret_cast<void*>((uintptr_t) C);
85 MapTy::iterator I = M.find(key);
86 M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Ted Kremenek4967c892009-03-30 17:53:05 +000089 void AddCheck(GRSimpleAPICheck *A) {
90 assert (A && "Check cannot be null.");
Mike Stump11289f42009-09-09 15:08:12 +000091 AllStmts = F.Concat(A, AllStmts);
Ted Kremenek4967c892009-03-30 17:53:05 +000092 }
Ted Kremenekfc5d0672009-02-04 23:49:09 +000093
Zhongxing Xu107f7592009-08-06 12:48:26 +000094 virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) {
Ted Kremenek4967c892009-03-30 17:53:05 +000095 // First handle the auditors that accept all statements.
96 bool isSink = false;
97 for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
98 isSink |= (*I)->Audit(N, VMgr);
Mike Stump11289f42009-09-09 15:08:12 +000099
Ted Kremenek4967c892009-03-30 17:53:05 +0000100 // Next handle the auditors that accept only specific statements.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +0000101 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000102 void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
103 MapTy::iterator MI = M.find(key);
Mike Stump11289f42009-09-09 15:08:12 +0000104 if (MI != M.end()) {
Ted Kremenek4967c892009-03-30 17:53:05 +0000105 for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
106 isSink |= (*I)->Audit(N, VMgr);
107 }
Mike Stump11289f42009-09-09 15:08:12 +0000108
109 return isSink;
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000110 }
111};
112
113} // end anonymous namespace
114
115//===----------------------------------------------------------------------===//
Ted Kremenek49513cc2009-07-22 21:43:51 +0000116// Checker worklist routines.
117//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000118
Zhongxing Xuaf353292009-12-02 05:49:12 +0000119void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000120 ExplodedNodeSet &Src, bool isPrevisit) {
Mike Stump11289f42009-09-09 15:08:12 +0000121
Ted Kremenek49513cc2009-07-22 21:43:51 +0000122 if (Checkers.empty()) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000123 Dst.insert(Src);
Zhongxing Xuaf353292009-12-02 05:49:12 +0000124 return;
Ted Kremenek49513cc2009-07-22 21:43:51 +0000125 }
Mike Stump11289f42009-09-09 15:08:12 +0000126
Zhongxing Xu107f7592009-08-06 12:48:26 +0000127 ExplodedNodeSet Tmp;
128 ExplodedNodeSet *PrevSet = &Src;
Mike Stump11289f42009-09-09 15:08:12 +0000129
Zhongxing Xuaf353292009-12-02 05:49:12 +0000130 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E;++I){
Ted Kremenek32c32892009-12-09 02:45:41 +0000131 ExplodedNodeSet *CurrSet = 0;
132 if (I+1 == E)
133 CurrSet = &Dst;
134 else {
135 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
136 CurrSet->clear();
137 }
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000138 void *tag = I->first;
139 Checker *checker = I->second;
Ted Kremenekacdc8172009-11-25 21:40:22 +0000140
Zhongxing Xu107f7592009-08-06 12:48:26 +0000141 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Zhongxing Xuaf353292009-12-02 05:49:12 +0000142 NI != NE; ++NI)
143 checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, tag, isPrevisit);
Ted Kremenek49513cc2009-07-22 21:43:51 +0000144 PrevSet = CurrSet;
145 }
146
147 // Don't autotransition. The CheckerContext objects should do this
148 // automatically.
Zhongxing Xuaf353292009-12-02 05:49:12 +0000149}
150
151void GRExprEngine::CheckerEvalNilReceiver(const ObjCMessageExpr *ME,
152 ExplodedNodeSet &Dst,
153 const GRState *state,
154 ExplodedNode *Pred) {
155 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
156 void *tag = I->first;
157 Checker *checker = I->second;
158
159 if (checker->GR_EvalNilReceiver(Dst, *Builder, *this, ME, Pred, state, tag))
160 break;
161 }
Ted Kremenek49513cc2009-07-22 21:43:51 +0000162}
163
Zhongxing Xu175447f2009-12-07 09:17:35 +0000164// CheckerEvalCall returns true if one of the checkers processed the node.
165// This may return void when all call evaluation logic goes to some checker
166// in the future.
167bool GRExprEngine::CheckerEvalCall(const CallExpr *CE,
168 ExplodedNodeSet &Dst,
169 ExplodedNode *Pred) {
170 bool Evaluated = false;
171
172 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
173 void *tag = I->first;
174 Checker *checker = I->second;
175
176 if (checker->GR_EvalCallExpr(Dst, *Builder, *this, CE, Pred, tag)) {
177 Evaluated = true;
178 break;
179 }
180 }
181
182 return Evaluated;
183}
184
Ted Kremenekef910042009-11-04 04:24:16 +0000185// FIXME: This is largely copy-paste from CheckerVisit(). Need to
186// unify.
Ted Kremenek209e31b2009-11-05 00:42:23 +0000187void GRExprEngine::CheckerVisitBind(const Stmt *AssignE, const Stmt *StoreE,
188 ExplodedNodeSet &Dst,
Ted Kremenekef910042009-11-04 04:24:16 +0000189 ExplodedNodeSet &Src,
190 SVal location, SVal val, bool isPrevisit) {
191
192 if (Checkers.empty()) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000193 Dst.insert(Src);
Ted Kremenekef910042009-11-04 04:24:16 +0000194 return;
195 }
196
197 ExplodedNodeSet Tmp;
198 ExplodedNodeSet *PrevSet = &Src;
199
200 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
201 {
Ted Kremenek32c32892009-12-09 02:45:41 +0000202 ExplodedNodeSet *CurrSet = 0;
203 if (I+1 == E)
204 CurrSet = &Dst;
205 else {
206 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
207 CurrSet->clear();
208 }
Ted Kremenekef910042009-11-04 04:24:16 +0000209
Ted Kremenekef910042009-11-04 04:24:16 +0000210 void *tag = I->first;
211 Checker *checker = I->second;
212
213 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
214 NI != NE; ++NI)
Ted Kremenek209e31b2009-11-05 00:42:23 +0000215 checker->GR_VisitBind(*CurrSet, *Builder, *this, AssignE, StoreE,
216 *NI, tag, location, val, isPrevisit);
Ted Kremenekef910042009-11-04 04:24:16 +0000217
218 // Update which NodeSet is the current one.
219 PrevSet = CurrSet;
220 }
221
222 // Don't autotransition. The CheckerContext objects should do this
223 // automatically.
224}
Ted Kremenek49513cc2009-07-22 21:43:51 +0000225//===----------------------------------------------------------------------===//
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000226// Engine construction and deletion.
227//===----------------------------------------------------------------------===//
228
Ted Kremenekd0fe8042009-11-25 21:51:20 +0000229static void RegisterInternalChecks(GRExprEngine &Eng) {
230 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
231 // are different than what probably many checks will do since they don't
232 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
233 // analyzing a function. Generation of BugReport objects is done via a call
234 // to 'FlushReports' from BugReporter.
235 // The following checks do not need to have their associated BugTypes
236 // explicitly registered with the BugReporter. If they issue any BugReports,
237 // their associated BugType will get registered with the BugReporter
238 // automatically. Note that the check itself is owned by the GRExprEngine
239 // object.
240 RegisterAttrNonNullChecker(Eng);
241 RegisterCallAndMessageChecker(Eng);
242 RegisterDereferenceChecker(Eng);
243 RegisterVLASizeChecker(Eng);
244 RegisterDivZeroChecker(Eng);
245 RegisterReturnStackAddressChecker(Eng);
246 RegisterReturnUndefChecker(Eng);
247 RegisterUndefinedArraySubscriptChecker(Eng);
248 RegisterUndefinedAssignmentChecker(Eng);
249 RegisterUndefBranchChecker(Eng);
250 RegisterUndefResultChecker(Eng);
Zhongxing Xu175447f2009-12-07 09:17:35 +0000251
252 // This is not a checker yet.
253 RegisterNoReturnFunctionChecker(Eng);
Zhongxing Xufe2f9012009-12-08 09:07:59 +0000254 RegisterBuiltinFunctionChecker(Eng);
Ted Kremenek7f824732008-05-01 18:33:28 +0000255}
256
Zhongxing Xu342950e2009-08-25 06:51:30 +0000257GRExprEngine::GRExprEngine(AnalysisManager &mgr)
Zhongxing Xue1190f72009-08-15 03:17:38 +0000258 : AMgr(mgr),
Mike Stump11289f42009-09-09 15:08:12 +0000259 CoreEngine(mgr.getASTContext(), *this),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000260 G(CoreEngine.getGraph()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000261 Builder(NULL),
Mike Stump11289f42009-09-09 15:08:12 +0000262 StateMgr(G.getContext(), mgr.getStoreManagerCreator(),
Zhongxing Xu342950e2009-08-25 06:51:30 +0000263 mgr.getConstraintManagerCreator(), G.getAllocator()),
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000264 SymMgr(StateMgr.getSymbolManager()),
Ted Kremenekf8cb51c2009-04-09 16:46:55 +0000265 ValMgr(StateMgr.getValueManager()),
Ted Kremenekf267a152009-07-16 01:32:00 +0000266 SVator(ValMgr.getSValuator()),
Ted Kremenek7f824732008-05-01 18:33:28 +0000267 CurrentStmt(NULL),
Zhongxing Xu4ee570a2008-12-22 08:30:52 +0000268 NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
Mike Stump11289f42009-09-09 15:08:12 +0000269 RaiseSel(GetNullarySelector("raise", G.getContext())),
Ted Kremenekefb50032009-11-25 21:45:48 +0000270 BR(mgr, *this)
271{
272 // Register internal checks.
Ted Kremenekd0fe8042009-11-25 21:51:20 +0000273 RegisterInternalChecks(*this);
Ted Kremenekefb50032009-11-25 21:45:48 +0000274}
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000275
Mike Stump11289f42009-09-09 15:08:12 +0000276GRExprEngine::~GRExprEngine() {
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000277 BR.FlushReports();
Ted Kremenek7f824732008-05-01 18:33:28 +0000278 delete [] NSExceptionInstanceRaiseSelectors;
Ted Kremenek6f2a7052009-10-30 17:47:32 +0000279 for (CheckersOrdered::iterator I=Checkers.begin(), E=Checkers.end(); I!=E;++I)
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000280 delete I->second;
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000281}
282
Ted Kremenek667cacb2008-04-15 23:06:53 +0000283//===----------------------------------------------------------------------===//
284// Utility methods.
285//===----------------------------------------------------------------------===//
286
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000287void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
Ted Kremenek9c32a1e2008-07-17 23:15:45 +0000288 StateMgr.TF = tf;
Ted Kremenek0fbbb082009-11-03 23:30:34 +0000289 tf->RegisterChecks(*this);
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000290 tf->RegisterPrinters(getStateManager().Printers);
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000291}
292
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000293void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
294 if (!BatchAuditor)
295 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000297 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
Ted Kremenek7acc3a32008-04-09 21:41:14 +0000298}
299
Ted Kremenek4967c892009-03-30 17:53:05 +0000300void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
301 if (!BatchAuditor)
302 BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
303
304 ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
305}
306
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000307const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) {
308 const GRState *state = StateMgr.getInitialState(InitLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000310 // Preconditions.
311
Ted Kremenek50546632009-04-10 00:59:50 +0000312 // FIXME: It would be nice if we had a more general mechanism to add
313 // such preconditions. Some day.
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000314 const Decl *D = InitLoc->getDecl();
315
316 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
317 // Precondition: the first argument of 'main' is an integer guaranteed
318 // to be > 0.
Daniel Dunbar07d07852009-10-18 21:17:35 +0000319 if (FD->getIdentifier()->getName() == "main" &&
Ted Kremenek50546632009-04-10 00:59:50 +0000320 FD->getNumParams() > 0) {
321 const ParmVarDecl *PD = FD->getParamDecl(0);
322 QualType T = PD->getType();
323 if (T->isIntegerType())
Ted Kremenek14536f62009-08-21 22:28:32 +0000324 if (const MemRegion *R = state->getRegion(PD, InitLoc)) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000325 SVal V = state->getSVal(loc::MemRegionVal(R));
Ted Kremenek7020eae2009-09-11 22:07:28 +0000326 SVal Constraint_untested = EvalBinOp(state, BinaryOperator::GT, V,
327 ValMgr.makeZeroVal(T),
328 getContext().IntTy);
Ted Kremenekf9906842009-06-18 22:57:13 +0000329
Ted Kremenek7020eae2009-09-11 22:07:28 +0000330 if (DefinedOrUnknownSVal *Constraint =
331 dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested)) {
332 if (const GRState *newState = state->Assume(*Constraint, true))
333 state = newState;
334 }
Ted Kremenek50546632009-04-10 00:59:50 +0000335 }
336 }
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000337 }
338 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
339 // Precondition: 'self' is always non-null upon entry to an Objective-C
340 // method.
341 const ImplicitParamDecl *SelfD = MD->getSelfDecl();
342 const MemRegion *R = state->getRegion(SelfD, InitLoc);
343 SVal V = state->getSVal(loc::MemRegionVal(R));
344
345 if (const Loc *LV = dyn_cast<Loc>(&V)) {
346 // Assume that the pointer value in 'self' is non-null.
Ted Kremenek7020eae2009-09-11 22:07:28 +0000347 state = state->Assume(*LV, true);
Ted Kremenek84c6f0a2009-09-09 20:36:12 +0000348 assert(state && "'self' cannot be null");
349 }
350 }
351
Ted Kremenek50546632009-04-10 00:59:50 +0000352 return state;
Ted Kremenek723fe3f2008-02-04 21:59:01 +0000353}
354
Ted Kremenek667cacb2008-04-15 23:06:53 +0000355//===----------------------------------------------------------------------===//
356// Top-level transfer function logic (Dispatcher).
357//===----------------------------------------------------------------------===//
358
Zhongxing Xu107f7592009-08-06 12:48:26 +0000359void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000360
Ted Kremenek91076ca2009-03-11 02:41:36 +0000361 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
362 S->getLocStart(),
363 "Error evaluating statement");
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenek667cacb2008-04-15 23:06:53 +0000365 Builder = &builder;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000366 EntryNode = builder.getLastNode();
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek667cacb2008-04-15 23:06:53 +0000368 CurrentStmt = S;
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremenek667cacb2008-04-15 23:06:53 +0000370 // Set up our simple checks.
Ted Kremenekc50e1a12008-07-11 18:37:32 +0000371 if (BatchAuditor)
372 Builder->setAuditor(BatchAuditor.get());
Mike Stump11289f42009-09-09 15:08:12 +0000373
374 // Create the cleaned state.
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000375 SymbolReaper SymReaper(Builder->getBasePredecessor()->getLiveVariables(),
376 SymMgr);
Zhongxing Xu3ca89b92009-08-27 06:55:26 +0000377 CleanedState = AMgr.shouldPurgeDead()
378 ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
379 : EntryNode->getState();
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000380
Ted Kremenek3812b762008-04-24 18:31:42 +0000381 // Process any special transfer function for dead symbols.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000382 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000384 if (!SymReaper.hasDeadSymbols())
Ted Kremenekae8014c2008-04-24 23:35:58 +0000385 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000386 else {
387 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000388 SaveOr OldHasGen(Builder->HasGeneratedNode);
389
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000390 SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
391 Builder->PurgingDeadSymbols = true;
Mike Stump11289f42009-09-09 15:08:12 +0000392
Zhongxing Xua4276b02009-11-13 06:53:04 +0000393 // FIXME: This should soon be removed.
394 ExplodedNodeSet Tmp2;
395 getTF().EvalDeadSymbols(Tmp2, *this, *Builder, EntryNode, S,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000396 CleanedState, SymReaper);
Ted Kremenekae8014c2008-04-24 23:35:58 +0000397
Zhongxing Xua4276b02009-11-13 06:53:04 +0000398 if (Checkers.empty())
399 Tmp = Tmp2;
400 else {
401 ExplodedNodeSet Tmp3;
402 ExplodedNodeSet *SrcSet = &Tmp2;
403 for (CheckersOrdered::iterator I = Checkers.begin(), E = Checkers.end();
404 I != E; ++I) {
Ted Kremenek32c32892009-12-09 02:45:41 +0000405 ExplodedNodeSet *DstSet = 0;
406 if (I+1 == E)
407 DstSet = &Tmp;
408 else {
409 DstSet = (SrcSet == &Tmp2) ? &Tmp3 : &Tmp2;
410 DstSet->clear();
411 }
412
Zhongxing Xua4276b02009-11-13 06:53:04 +0000413 void *tag = I->first;
414 Checker *checker = I->second;
415 for (ExplodedNodeSet::iterator NI = SrcSet->begin(), NE = SrcSet->end();
416 NI != NE; ++NI)
417 checker->GR_EvalDeadSymbols(*DstSet, *Builder, *this, S, *NI,
418 SymReaper, tag);
419 SrcSet = DstSet;
420 }
421 }
422
Ted Kremenekae8014c2008-04-24 23:35:58 +0000423 if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
424 Tmp.Add(EntryNode);
Ted Kremenek3812b762008-04-24 18:31:42 +0000425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenekae8014c2008-04-24 23:35:58 +0000427 bool HasAutoGenerated = false;
428
Zhongxing Xu107f7592009-08-06 12:48:26 +0000429 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenekae8014c2008-04-24 23:35:58 +0000430
Zhongxing Xu107f7592009-08-06 12:48:26 +0000431 ExplodedNodeSet Dst;
Mike Stump11289f42009-09-09 15:08:12 +0000432
433 // Set the cleaned state.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000434 Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
Mike Stump11289f42009-09-09 15:08:12 +0000435
436 // Visit the statement.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000437 Visit(S, *I, Dst);
438
439 // Do we need to auto-generate a node? We only need to do this to generate
440 // a node with a "cleaned" state; GRCoreEngine will actually handle
Mike Stump11289f42009-09-09 15:08:12 +0000441 // auto-transitions for other cases.
Ted Kremenekae8014c2008-04-24 23:35:58 +0000442 if (Dst.size() == 1 && *Dst.begin() == EntryNode
443 && !Builder->HasGeneratedNode && !HasAutoGenerated) {
444 HasAutoGenerated = true;
445 builder.generateNode(S, GetState(EntryNode), *I);
446 }
Ted Kremenek3812b762008-04-24 18:31:42 +0000447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Ted Kremenek667cacb2008-04-15 23:06:53 +0000449 // NULL out these variables to cleanup.
Ted Kremenek667cacb2008-04-15 23:06:53 +0000450 CleanedState = NULL;
Ted Kremenekae8014c2008-04-24 23:35:58 +0000451 EntryNode = NULL;
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000452
Ted Kremenekbc9118b2008-07-17 21:27:31 +0000453 CurrentStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Ted Kremenekae8014c2008-04-24 23:35:58 +0000455 Builder = NULL;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000456}
457
Mike Stump11289f42009-09-09 15:08:12 +0000458void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek91076ca2009-03-11 02:41:36 +0000459 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
460 S->getLocStart(),
461 "Error evaluating statement");
462
Ted Kremenek667cacb2008-04-15 23:06:53 +0000463 // FIXME: add metadata to the CFG so that we can disable
464 // this check when we KNOW that there is no block-level subexpression.
465 // The motivation is that this check requires a hashtable lookup.
Mike Stump11289f42009-09-09 15:08:12 +0000466
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000467 if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000468 Dst.Add(Pred);
469 return;
470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Ted Kremenek667cacb2008-04-15 23:06:53 +0000472 switch (S->getStmtClass()) {
Mike Stump11289f42009-09-09 15:08:12 +0000473
Ted Kremenek667cacb2008-04-15 23:06:53 +0000474 default:
475 // Cases we intentionally have "default" handle:
476 // AddrLabelExpr, IntegerLiteral, CharacterLiteral
Mike Stump11289f42009-09-09 15:08:12 +0000477
Ted Kremenek667cacb2008-04-15 23:06:53 +0000478 Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
479 break;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000481 case Stmt::ArraySubscriptExprClass:
482 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
483 break;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek667cacb2008-04-15 23:06:53 +0000485 case Stmt::AsmStmtClass:
486 VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
487 break;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000488
489 case Stmt::BlockDeclRefExprClass:
490 VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), Pred, Dst, false);
491 break;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenekcfe223f2009-11-25 01:33:13 +0000493 case Stmt::BlockExprClass:
494 VisitBlockExpr(cast<BlockExpr>(S), Pred, Dst);
495 break;
496
Ted Kremenek667cacb2008-04-15 23:06:53 +0000497 case Stmt::BinaryOperatorClass: {
498 BinaryOperator* B = cast<BinaryOperator>(S);
Mike Stump11289f42009-09-09 15:08:12 +0000499
Ted Kremenek667cacb2008-04-15 23:06:53 +0000500 if (B->isLogicalOp()) {
501 VisitLogicalExpr(B, Pred, Dst);
502 break;
503 }
504 else if (B->getOpcode() == BinaryOperator::Comma) {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000505 const GRState* state = GetState(Pred);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000506 MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
Ted Kremenek667cacb2008-04-15 23:06:53 +0000507 break;
508 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000509
Zhongxing Xu342950e2009-08-25 06:51:30 +0000510 if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000511 ExplodedNodeSet Tmp;
Zhongxing Xub9eda672009-10-30 07:19:39 +0000512 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp, false);
Mike Stump11289f42009-09-09 15:08:12 +0000513 EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000514 }
515 else
Zhongxing Xub9eda672009-10-30 07:19:39 +0000516 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false);
Ted Kremenekdc3f50f2009-02-25 22:32:02 +0000517
Ted Kremenek667cacb2008-04-15 23:06:53 +0000518 break;
519 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000520
Douglas Gregor993603d2008-11-14 16:09:21 +0000521 case Stmt::CallExprClass:
522 case Stmt::CXXOperatorCallExprClass: {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000523 CallExpr* C = cast<CallExpr>(S);
524 VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
Ted Kremenek537f6382008-11-14 19:47:18 +0000525 break;
Ted Kremenek667cacb2008-04-15 23:06:53 +0000526 }
Ted Kremenek537f6382008-11-14 19:47:18 +0000527
Ted Kremenek667cacb2008-04-15 23:06:53 +0000528 // FIXME: ChooseExpr is really a constant. We need to fix
529 // the CFG do not model them as explicit control-flow.
Mike Stump11289f42009-09-09 15:08:12 +0000530
Ted Kremenek667cacb2008-04-15 23:06:53 +0000531 case Stmt::ChooseExprClass: { // __builtin_choose_expr
532 ChooseExpr* C = cast<ChooseExpr>(S);
533 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
534 break;
535 }
Mike Stump11289f42009-09-09 15:08:12 +0000536
Ted Kremenek667cacb2008-04-15 23:06:53 +0000537 case Stmt::CompoundAssignOperatorClass:
Zhongxing Xub9eda672009-10-30 07:19:39 +0000538 VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000539 break;
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000540
541 case Stmt::CompoundLiteralExprClass:
542 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
543 break;
Mike Stump11289f42009-09-09 15:08:12 +0000544
Ted Kremenek667cacb2008-04-15 23:06:53 +0000545 case Stmt::ConditionalOperatorClass: { // '?' operator
546 ConditionalOperator* C = cast<ConditionalOperator>(S);
547 VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
548 break;
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Ted Kremenek667cacb2008-04-15 23:06:53 +0000551 case Stmt::DeclRefExprClass:
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000552 VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000553 break;
Mike Stump11289f42009-09-09 15:08:12 +0000554
Ted Kremenek667cacb2008-04-15 23:06:53 +0000555 case Stmt::DeclStmtClass:
556 VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
557 break;
Mike Stump11289f42009-09-09 15:08:12 +0000558
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000559 case Stmt::ImplicitCastExprClass:
Douglas Gregorf19b2312008-10-28 15:36:24 +0000560 case Stmt::CStyleCastExprClass: {
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000561 CastExpr* C = cast<CastExpr>(S);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000562 VisitCast(C, C->getSubExpr(), Pred, Dst);
563 break;
564 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +0000565
566 case Stmt::InitListExprClass:
567 VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
568 break;
Mike Stump11289f42009-09-09 15:08:12 +0000569
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000570 case Stmt::MemberExprClass:
Ted Kremenek38213f92008-04-21 23:43:38 +0000571 VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
572 break;
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000574 case Stmt::ObjCIvarRefExprClass:
575 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
576 break;
Ted Kremenek17810802008-11-12 19:24:17 +0000577
578 case Stmt::ObjCForCollectionStmtClass:
579 VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
580 break;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Ted Kremenek667cacb2008-04-15 23:06:53 +0000582 case Stmt::ObjCMessageExprClass: {
583 VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
584 break;
585 }
Mike Stump11289f42009-09-09 15:08:12 +0000586
Ted Kremenek1857ff42008-12-09 20:18:58 +0000587 case Stmt::ObjCAtThrowStmtClass: {
588 // FIXME: This is not complete. We basically treat @throw as
589 // an abort.
590 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
591 Builder->BuildSinks = true;
592 MakeNode(Dst, S, Pred, GetState(Pred));
593 break;
594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Ted Kremenek667cacb2008-04-15 23:06:53 +0000596 case Stmt::ParenExprClass:
Ted Kremenekda5cdda2008-04-22 04:56:29 +0000597 Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000598 break;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000600 case Stmt::ReturnStmtClass:
601 VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
602 break;
Mike Stump11289f42009-09-09 15:08:12 +0000603
Sebastian Redl6f282892008-11-11 17:56:53 +0000604 case Stmt::SizeOfAlignOfExprClass:
605 VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000606 break;
Mike Stump11289f42009-09-09 15:08:12 +0000607
Ted Kremenek667cacb2008-04-15 23:06:53 +0000608 case Stmt::StmtExprClass: {
609 StmtExpr* SE = cast<StmtExpr>(S);
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000610
611 if (SE->getSubStmt()->body_empty()) {
612 // Empty statement expression.
613 assert(SE->getType() == getContext().VoidTy
614 && "Empty statement expression must have void type.");
615 Dst.Add(Pred);
616 break;
617 }
Mike Stump11289f42009-09-09 15:08:12 +0000618
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000619 if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
620 const GRState* state = GetState(Pred);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000621 MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
Ted Kremenekd25fb7a62009-02-14 05:55:08 +0000622 }
Ted Kremenek667cacb2008-04-15 23:06:53 +0000623 else
624 Dst.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Ted Kremenek667cacb2008-04-15 23:06:53 +0000626 break;
627 }
Zhongxing Xud2fa1e02008-11-30 05:49:49 +0000628
629 case Stmt::StringLiteralClass:
630 VisitLValue(cast<StringLiteral>(S), Pred, Dst);
631 break;
Mike Stump11289f42009-09-09 15:08:12 +0000632
Ted Kremenek891642e2009-03-18 23:49:26 +0000633 case Stmt::UnaryOperatorClass: {
634 UnaryOperator *U = cast<UnaryOperator>(S);
Zhongxing Xu342950e2009-08-25 06:51:30 +0000635 if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000636 ExplodedNodeSet Tmp;
Ted Kremenek891642e2009-03-18 23:49:26 +0000637 VisitUnaryOperator(U, Pred, Tmp, false);
638 EvalEagerlyAssume(Dst, Tmp, U);
639 }
640 else
641 VisitUnaryOperator(U, Pred, Dst, false);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000642 break;
Ted Kremenek891642e2009-03-18 23:49:26 +0000643 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000644 }
645}
646
Mike Stump11289f42009-09-09 15:08:12 +0000647void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000648 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000649
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000650 Ex = Ex->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +0000651
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000652 if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000653 Dst.Add(Pred);
654 return;
655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000657 switch (Ex->getStmtClass()) {
Mike Stump11289f42009-09-09 15:08:12 +0000658
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000659 case Stmt::ArraySubscriptExprClass:
660 VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
661 return;
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenek04af9f22009-12-07 22:05:27 +0000663 case Stmt::BlockDeclRefExprClass:
664 VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(Ex), Pred, Dst, true);
665 return;
666
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000667 case Stmt::DeclRefExprClass:
668 VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
669 return;
Mike Stump11289f42009-09-09 15:08:12 +0000670
Ted Kremenek12dd55b2008-10-17 00:03:18 +0000671 case Stmt::ObjCIvarRefExprClass:
672 VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
673 return;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000675 case Stmt::UnaryOperatorClass:
676 VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
677 return;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenekfa5a3d02008-04-29 21:04:26 +0000679 case Stmt::MemberExprClass:
680 VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
681 return;
Mike Stump11289f42009-09-09 15:08:12 +0000682
Ted Kremenekbf263682008-10-27 21:54:31 +0000683 case Stmt::CompoundLiteralExprClass:
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000684 VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
Ted Kremenekbf263682008-10-27 21:54:31 +0000685 return;
Mike Stump11289f42009-09-09 15:08:12 +0000686
Ted Kremenek58700462008-10-17 17:24:14 +0000687 case Stmt::ObjCPropertyRefExprClass:
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000688 case Stmt::ObjCImplicitSetterGetterRefExprClass:
Ted Kremenek58700462008-10-17 17:24:14 +0000689 // FIXME: Property assignments are lvalues, but not really "locations".
690 // e.g.: self.x = something;
691 // Here the "self.x" really can translate to a method call (setter) when
692 // the assignment is made. Moreover, the entire assignment expression
693 // evaluate to whatever "something" is, not calling the "getter" for
694 // the property (which would make sense since it can have side effects).
695 // We'll probably treat this as a location, but not one that we can
696 // take the address of. Perhaps we need a new SVal class for cases
697 // like thsis?
698 // Note that we have a similar problem for bitfields, since they don't
699 // have "locations" in the sense that we can take their address.
700 Dst.Add(Pred);
Ted Kremenek8f5dc292008-10-18 04:08:49 +0000701 return;
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000702
703 case Stmt::StringLiteralClass: {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000704 const GRState* state = GetState(Pred);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000705 SVal V = state->getLValue(cast<StringLiteral>(Ex));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000706 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000707 return;
708 }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Zhongxing Xub9eda672009-10-30 07:19:39 +0000710 case Stmt::BinaryOperatorClass:
711 case Stmt::CompoundAssignOperatorClass:
712 VisitBinaryOperator(cast<BinaryOperator>(Ex), Pred, Dst, true);
713 return;
714
Ted Kremenek850422e2008-10-18 04:15:35 +0000715 default:
716 // Arbitrary subexpressions can return aggregate temporaries that
717 // can be used in a lvalue context. We need to enhance our support
718 // of such temporaries in both the environment and the store, so right
719 // now we just do a regular visit.
Douglas Gregorddb24852009-01-30 17:31:00 +0000720 assert ((Ex->getType()->isAggregateType()) &&
Ted Kremeneke69a1fa2008-10-25 20:09:21 +0000721 "Other kinds of expressions with non-aggregate/union types do"
722 " not have lvalues.");
Mike Stump11289f42009-09-09 15:08:12 +0000723
Ted Kremenek850422e2008-10-18 04:15:35 +0000724 Visit(Ex, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000725 }
726}
727
728//===----------------------------------------------------------------------===//
729// Block entrance. (Update counters).
730//===----------------------------------------------------------------------===//
731
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000732bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
Ted Kremenek667cacb2008-04-15 23:06:53 +0000733 GRBlockCounter BC) {
Mike Stump11289f42009-09-09 15:08:12 +0000734
Ted Kremenek667cacb2008-04-15 23:06:53 +0000735 return BC.getNumVisited(B->getBlockID()) < 3;
736}
737
738//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +0000739// Generic node creation.
740//===----------------------------------------------------------------------===//
741
Zhongxing Xu107f7592009-08-06 12:48:26 +0000742ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
743 ExplodedNode* Pred, const GRState* St,
744 ProgramPoint::Kind K, const void *tag) {
Ted Kremenekdf240002009-04-11 00:11:10 +0000745 assert (Builder && "GRStmtNodeBuilder not present.");
746 SaveAndRestore<const void*> OldTag(Builder->Tag);
747 Builder->Tag = tag;
748 return Builder->MakeNode(Dst, S, Pred, St, K);
749}
750
751//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +0000752// Branch processing.
753//===----------------------------------------------------------------------===//
754
Ted Kremenek17d541d2009-02-13 01:45:31 +0000755const GRState* GRExprEngine::MarkBranch(const GRState* state,
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000756 Stmt* Terminator,
757 bool branchTaken) {
Mike Stump11289f42009-09-09 15:08:12 +0000758
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000759 switch (Terminator->getStmtClass()) {
760 default:
Ted Kremenek17d541d2009-02-13 01:45:31 +0000761 return state;
Mike Stump11289f42009-09-09 15:08:12 +0000762
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000763 case Stmt::BinaryOperatorClass: { // '&&' and '||'
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000765 BinaryOperator* B = cast<BinaryOperator>(Terminator);
766 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +0000767
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000768 assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +0000769
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000770 // For &&, if we take the true branch, then the value of the whole
771 // expression is that of the RHS expression.
772 //
773 // For ||, if we take the false branch, then the value of the whole
774 // expression is that of the RHS expression.
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000776 Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
Mike Stump11289f42009-09-09 15:08:12 +0000777 (Op == BinaryOperator::LOr && !branchTaken)
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000778 ? B->getRHS() : B->getLHS();
Mike Stump11289f42009-09-09 15:08:12 +0000779
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000780 return state->BindExpr(B, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000783 case Stmt::ConditionalOperatorClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000784
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000785 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000786
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000787 // For ?, if branchTaken == true then the value is either the LHS or
788 // the condition itself. (GNU extension).
Mike Stump11289f42009-09-09 15:08:12 +0000789
790 Expr* Ex;
791
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000792 if (branchTaken)
Mike Stump11289f42009-09-09 15:08:12 +0000793 Ex = C->getLHS() ? C->getLHS() : C->getCond();
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000794 else
795 Ex = C->getRHS();
Mike Stump11289f42009-09-09 15:08:12 +0000796
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000797 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000798 }
Mike Stump11289f42009-09-09 15:08:12 +0000799
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000800 case Stmt::ChooseExprClass: { // ?:
Mike Stump11289f42009-09-09 15:08:12 +0000801
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000802 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +0000803
804 Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000805 return state->BindExpr(C, UndefinedVal(Ex));
Ted Kremenekf3a4b962008-02-26 19:05:15 +0000806 }
807 }
808}
809
Ted Kremenek22358bd2009-03-13 16:32:54 +0000810/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
811/// to try to recover some path-sensitivity for casts of symbolic
812/// integers that promote their values (which are currently not tracked well).
813/// This function returns the SVal bound to Condition->IgnoreCasts if all the
814// cast(s) did was sign-extend the original value.
815static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
816 Stmt* Condition, ASTContext& Ctx) {
817
818 Expr *Ex = dyn_cast<Expr>(Condition);
819 if (!Ex)
820 return UnknownVal();
821
822 uint64_t bits = 0;
823 bool bitsInit = false;
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek22358bd2009-03-13 16:32:54 +0000825 while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
826 QualType T = CE->getType();
827
828 if (!T->isIntegerType())
829 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000830
Ted Kremenek22358bd2009-03-13 16:32:54 +0000831 uint64_t newBits = Ctx.getTypeSize(T);
832 if (!bitsInit || newBits < bits) {
833 bitsInit = true;
834 bits = newBits;
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenek22358bd2009-03-13 16:32:54 +0000837 Ex = CE->getSubExpr();
838 }
839
840 // We reached a non-cast. Is it a symbolic value?
841 QualType T = Ex->getType();
842
843 if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
844 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000845
Ted Kremenek095f1a92009-06-18 23:58:37 +0000846 return state->getSVal(Ex);
Ted Kremenek22358bd2009-03-13 16:32:54 +0000847}
848
Ted Kremenek17810802008-11-12 19:24:17 +0000849void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000850 GRBranchNodeBuilder& builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000851
Ted Kremenek8db4b112008-02-15 22:29:00 +0000852 // Check for NULL conditions; e.g. "for(;;)"
Mike Stump11289f42009-09-09 15:08:12 +0000853 if (!Condition) {
Ted Kremenek8db4b112008-02-15 22:29:00 +0000854 builder.markInfeasible(false);
Ted Kremenek8db4b112008-02-15 22:29:00 +0000855 return;
856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek32c41ec2009-03-11 03:54:24 +0000858 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
859 Condition->getLocStart(),
860 "Error evaluating branch");
Ted Kremenek907a7112009-08-27 01:39:13 +0000861
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000862 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end();I!=E;++I) {
863 void *tag = I->first;
864 Checker *checker = I->second;
865 checker->VisitBranchCondition(builder, *this, Condition, tag);
866 }
867
868 // If the branch condition is undefined, return;
869 if (!builder.isFeasible(true) && !builder.isFeasible(false))
870 return;
871
Mike Stump11289f42009-09-09 15:08:12 +0000872 const GRState* PrevState = builder.getState();
Ted Kremenek7020eae2009-09-11 22:07:28 +0000873 SVal X = PrevState->getSVal(Condition);
Mike Stump11289f42009-09-09 15:08:12 +0000874
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000875 if (X.isUnknown()) {
876 // Give it a chance to recover from unknown.
877 if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
878 if (Ex->getType()->isIntegerType()) {
879 // Try to recover some path-sensitivity. Right now casts of symbolic
880 // integers that promote their values are currently not tracked well.
881 // If 'Condition' is such an expression, try and recover the
882 // underlying value and use that instead.
883 SVal recovered = RecoverCastedSymbol(getStateManager(),
884 builder.getState(), Condition,
885 getContext());
886
887 if (!recovered.isUnknown()) {
888 X = recovered;
889 }
Ted Kremenek22358bd2009-03-13 16:32:54 +0000890 }
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000891 }
892 // If the condition is still unknown, give up.
893 if (X.isUnknown()) {
894 builder.generateNode(MarkBranch(PrevState, Term, true), true);
895 builder.generateNode(MarkBranch(PrevState, Term, false), false);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000896 return;
Mike Stump11289f42009-09-09 15:08:12 +0000897 }
Ted Kremeneka50d9852008-01-30 23:03:39 +0000898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000900 DefinedSVal V = cast<DefinedSVal>(X);
901
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000902 // Process the true branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000903 if (builder.isFeasible(true)) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000904 if (const GRState *state = PrevState->Assume(V, true))
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000905 builder.generateNode(MarkBranch(state, Term, true), true);
906 else
907 builder.markInfeasible(true);
908 }
Mike Stump11289f42009-09-09 15:08:12 +0000909
910 // Process the false branch.
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000911 if (builder.isFeasible(false)) {
Zhongxing Xu56dd5f02009-11-23 03:20:54 +0000912 if (const GRState *state = PrevState->Assume(V, false))
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000913 builder.generateNode(MarkBranch(state, Term, false), false);
914 else
915 builder.markInfeasible(false);
916 }
Ted Kremenek7ff18932008-01-29 23:32:35 +0000917}
918
Ted Kremenekf6c62f32008-02-13 17:41:41 +0000919/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
Ted Kremenek7022efb2008-02-13 00:24:44 +0000920/// nodes by processing the 'effects' of a computed goto jump.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000921void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000922
Mike Stump11289f42009-09-09 15:08:12 +0000923 const GRState *state = builder.getState();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000924 SVal V = state->getSVal(builder.getTarget());
Mike Stump11289f42009-09-09 15:08:12 +0000925
Ted Kremenek7022efb2008-02-13 00:24:44 +0000926 // Three possibilities:
927 //
928 // (1) We know the computed label.
Ted Kremenek93d1fed2008-02-28 09:25:22 +0000929 // (2) The label is NULL (or some other constant), or Undefined.
Ted Kremenek7022efb2008-02-13 00:24:44 +0000930 // (3) We have no clue about the label. Dispatch to all targets.
931 //
Mike Stump11289f42009-09-09 15:08:12 +0000932
Zhongxing Xu107f7592009-08-06 12:48:26 +0000933 typedef GRIndirectGotoNodeBuilder::iterator iterator;
Ted Kremenek7022efb2008-02-13 00:24:44 +0000934
Zhongxing Xu27f17422008-10-17 05:57:07 +0000935 if (isa<loc::GotoLabel>(V)) {
936 LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
Mike Stump11289f42009-09-09 15:08:12 +0000937
Ted Kremenek7022efb2008-02-13 00:24:44 +0000938 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
Ted Kremenek2bba9012008-02-13 17:27:37 +0000939 if (I.getLabel() == L) {
Ted Kremenek17d541d2009-02-13 01:45:31 +0000940 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000941 return;
942 }
943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
Ted Kremenek7022efb2008-02-13 00:24:44 +0000945 assert (false && "No block with label.");
946 return;
947 }
948
Zhongxing Xu27f17422008-10-17 05:57:07 +0000949 if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000950 // Dispatch to the first target and mark it as a sink.
Zhongxing Xu9e200792009-11-24 07:06:39 +0000951 //ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
952 // FIXME: add checker visit.
953 // UndefBranches.insert(N);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000954 return;
955 }
Mike Stump11289f42009-09-09 15:08:12 +0000956
Ted Kremenek7022efb2008-02-13 00:24:44 +0000957 // This is really a catch-all. We don't support symbolics yet.
Ted Kremenek9c03f682009-04-23 17:49:43 +0000958 // FIXME: Implement dispatch for symbolic pointers.
Mike Stump11289f42009-09-09 15:08:12 +0000959
Ted Kremenek7022efb2008-02-13 00:24:44 +0000960 for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
Ted Kremenek17d541d2009-02-13 01:45:31 +0000961 builder.generateNode(I, state);
Ted Kremenek7022efb2008-02-13 00:24:44 +0000962}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000963
Ted Kremenek667cacb2008-04-15 23:06:53 +0000964
965void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000966 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000967
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000968 assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
Mike Stump11289f42009-09-09 15:08:12 +0000969
Ted Kremenek17d541d2009-02-13 01:45:31 +0000970 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +0000971 SVal X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +0000972
Ted Kremenek667cacb2008-04-15 23:06:53 +0000973 assert (X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +0000974
Ted Kremenekc55f0cd2009-06-19 17:10:32 +0000975 Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
Mike Stump11289f42009-09-09 15:08:12 +0000976 assert(SE);
Ted Kremenek907a7112009-08-27 01:39:13 +0000977 X = state->getSVal(SE);
Mike Stump11289f42009-09-09 15:08:12 +0000978
Ted Kremenek667cacb2008-04-15 23:06:53 +0000979 // Make sure that we invalidate the previous binding.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +0000980 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
Ted Kremenek667cacb2008-04-15 23:06:53 +0000981}
982
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +0000983/// ProcessEndPath - Called by GRCoreEngine. Used to generate end-of-path
984/// nodes when the control reaches the end of a function.
985void GRExprEngine::ProcessEndPath(GREndPathNodeBuilder& builder) {
986 getTF().EvalEndPath(*this, builder);
987 StateMgr.EndPath(builder.getState());
Zhongxing Xu4668c7e2009-11-17 07:54:15 +0000988 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E;++I){
989 void *tag = I->first;
990 Checker *checker = I->second;
991 checker->EvalEndPath(builder, tag, *this);
992 }
Ted Kremenek1a0dd2e2009-11-14 01:05:20 +0000993}
994
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000995/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
996/// nodes by processing the 'effects' of a switch statement.
Mike Stump11289f42009-09-09 15:08:12 +0000997void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
998 typedef GRSwitchNodeBuilder::iterator iterator;
999 const GRState* state = builder.getState();
Ted Kremenek346169f2008-02-18 22:57:02 +00001000 Expr* CondE = builder.getCondition();
Ted Kremenek7020eae2009-09-11 22:07:28 +00001001 SVal CondV_untested = state->getSVal(CondE);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001002
Ted Kremenek7020eae2009-09-11 22:07:28 +00001003 if (CondV_untested.isUndef()) {
Zhongxing Xu9e200792009-11-24 07:06:39 +00001004 //ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
1005 // FIXME: add checker
1006 //UndefBranches.insert(N);
1007
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001008 return;
1009 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001010 DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
Ted Kremenek346169f2008-02-18 22:57:02 +00001011
Ted Kremenek7020eae2009-09-11 22:07:28 +00001012 const GRState *DefaultSt = state;
Ted Kremenekf9906842009-06-18 22:57:13 +00001013 bool defaultIsFeasible = false;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001015 for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001016 CaseStmt* Case = cast<CaseStmt>(I.getCase());
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001017
1018 // Evaluate the LHS of the case value.
1019 Expr::EvalResult V1;
Mike Stump11289f42009-09-09 15:08:12 +00001020 bool b = Case->getLHS()->Evaluate(V1, getContext());
1021
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001022 // Sanity checks. These go away in Release builds.
Mike Stump11289f42009-09-09 15:08:12 +00001023 assert(b && V1.Val.isInt() && !V1.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001024 && "Case condition must evaluate to an integer constant.");
Mike Stump11289f42009-09-09 15:08:12 +00001025 b = b; // silence unused variable warning
1026 assert(V1.Val.getInt().getBitWidth() ==
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001027 getContext().getTypeSize(CondE->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001028
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001029 // Get the RHS of the case, if it exists.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001030 Expr::EvalResult V2;
Mike Stump11289f42009-09-09 15:08:12 +00001031
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001032 if (Expr* E = Case->getRHS()) {
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001033 b = E->Evaluate(V2, getContext());
Mike Stump11289f42009-09-09 15:08:12 +00001034 assert(b && V2.Val.isInt() && !V2.HasSideEffects
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001035 && "Case condition must evaluate to an integer constant.");
1036 b = b; // silence unused variable warning
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001037 }
Ted Kremenek9eae4032008-03-17 22:17:56 +00001038 else
1039 V2 = V1;
Mike Stump11289f42009-09-09 15:08:12 +00001040
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001041 // FIXME: Eventually we should replace the logic below with a range
1042 // comparison, rather than concretize the values within the range.
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001043 // This should be easy once we have "ranges" for NonLVals.
Mike Stump11289f42009-09-09 15:08:12 +00001044
Ted Kremenek9eae4032008-03-17 22:17:56 +00001045 do {
Mike Stump11289f42009-09-09 15:08:12 +00001046 nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
Ted Kremenek7020eae2009-09-11 22:07:28 +00001047 DefinedOrUnknownSVal Res = SVator.EvalEQ(DefaultSt, CondV, CaseVal);
1048
Mike Stump11289f42009-09-09 15:08:12 +00001049 // Now "assume" that the case matches.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001050 if (const GRState* stateNew = state->Assume(Res, true)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001051 builder.generateCaseStmtNode(I, stateNew);
Mike Stump11289f42009-09-09 15:08:12 +00001052
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001053 // If CondV evaluates to a constant, then we know that this
1054 // is the *only* case that we can take, so stop evaluating the
1055 // others.
Zhongxing Xu27f17422008-10-17 05:57:07 +00001056 if (isa<nonloc::ConcreteInt>(CondV))
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001057 return;
1058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001060 // Now "assume" that the case doesn't match. Add this state
1061 // to the default state (if it is feasible).
Ted Kremenek7020eae2009-09-11 22:07:28 +00001062 if (const GRState *stateNew = DefaultSt->Assume(Res, false)) {
Ted Kremenekf9906842009-06-18 22:57:13 +00001063 defaultIsFeasible = true;
1064 DefaultSt = stateNew;
Ted Kremenekd2419a02008-04-23 05:03:18 +00001065 }
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001066
Ted Kremenek9eae4032008-03-17 22:17:56 +00001067 // Concretize the next value in the range.
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001068 if (V1.Val.getInt() == V2.Val.getInt())
Ted Kremenek9eae4032008-03-17 22:17:56 +00001069 break;
Mike Stump11289f42009-09-09 15:08:12 +00001070
Ted Kremenek1ab188f2009-01-17 01:54:16 +00001071 ++V1.Val.getInt();
1072 assert (V1.Val.getInt() <= V2.Val.getInt());
Mike Stump11289f42009-09-09 15:08:12 +00001073
Ted Kremenek9eae4032008-03-17 22:17:56 +00001074 } while (true);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001075 }
Mike Stump11289f42009-09-09 15:08:12 +00001076
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001077 // If we reach here, than we know that the default branch is
Mike Stump11289f42009-09-09 15:08:12 +00001078 // possible.
Ted Kremenekf9906842009-06-18 22:57:13 +00001079 if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001080}
1081
Ted Kremenek667cacb2008-04-15 23:06:53 +00001082//===----------------------------------------------------------------------===//
1083// Transfer functions: logical operations ('&&', '||').
1084//===----------------------------------------------------------------------===//
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00001085
Zhongxing Xu107f7592009-08-06 12:48:26 +00001086void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
1087 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001088
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001089 assert(B->getOpcode() == BinaryOperator::LAnd ||
1090 B->getOpcode() == BinaryOperator::LOr);
Mike Stump11289f42009-09-09 15:08:12 +00001091
Zhongxing Xu94ec6492009-08-25 03:33:41 +00001092 assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
Mike Stump11289f42009-09-09 15:08:12 +00001093
Ted Kremenek17d541d2009-02-13 01:45:31 +00001094 const GRState* state = GetState(Pred);
Ted Kremenek907a7112009-08-27 01:39:13 +00001095 SVal X = state->getSVal(B);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001096 assert(X.isUndef());
Mike Stump11289f42009-09-09 15:08:12 +00001097
Ted Kremenek7020eae2009-09-11 22:07:28 +00001098 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001099 assert(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001100
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001101 if (Ex == B->getRHS()) {
Ted Kremenek907a7112009-08-27 01:39:13 +00001102 X = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00001103
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001104 // Handle undefined values.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00001105 if (X.isUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001106 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenekbc543902008-02-26 19:40:44 +00001107 return;
1108 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001109
1110 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
Mike Stump11289f42009-09-09 15:08:12 +00001111
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001112 // We took the RHS. Because the value of the '&&' or '||' expression must
1113 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
1114 // or 1. Alternatively, we could take a lazy approach, and calculate this
1115 // value later when necessary. We don't have the machinery in place for
1116 // this right now, and since most logical expressions are used for branches,
Mike Stump11289f42009-09-09 15:08:12 +00001117 // the payoff is not likely to be large. Instead, we do eager evaluation.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001118 if (const GRState *newState = state->Assume(XD, true))
Mike Stump11289f42009-09-09 15:08:12 +00001119 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001120 newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
Mike Stump11289f42009-09-09 15:08:12 +00001121
Ted Kremenek7020eae2009-09-11 22:07:28 +00001122 if (const GRState *newState = state->Assume(XD, false))
Mike Stump11289f42009-09-09 15:08:12 +00001123 MakeNode(Dst, B, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001124 newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001125 }
1126 else {
Ted Kremenekf3a4b962008-02-26 19:05:15 +00001127 // We took the LHS expression. Depending on whether we are '&&' or
1128 // '||' we know what the value of the expression is via properties of
1129 // the short-circuiting.
Mike Stump11289f42009-09-09 15:08:12 +00001130 X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001131 B->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001132 MakeNode(Dst, B, Pred, state->BindExpr(B, X));
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001133 }
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00001134}
Mike Stump11289f42009-09-09 15:08:12 +00001135
Ted Kremenek667cacb2008-04-15 23:06:53 +00001136//===----------------------------------------------------------------------===//
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001137// Transfer functions: Loads and stores.
Ted Kremenek667cacb2008-04-15 23:06:53 +00001138//===----------------------------------------------------------------------===//
Ted Kremenekde8d62b2008-01-15 23:55:06 +00001139
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001140void GRExprEngine::VisitBlockExpr(BlockExpr *BE, ExplodedNode *Pred,
1141 ExplodedNodeSet &Dst) {
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001142
1143 ExplodedNodeSet Tmp;
1144
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001145 CanQualType T = getContext().getCanonicalType(BE->getType());
Ted Kremenekb63ad7a2009-11-25 23:53:07 +00001146 SVal V = ValMgr.getBlockPointer(BE->getBlockDecl(), T,
1147 Pred->getLocationContext());
1148
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001149 MakeNode(Tmp, BE, Pred, GetState(Pred)->BindExpr(BE, V),
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001150 ProgramPoint::PostLValueKind);
Ted Kremeneke6929ff2009-11-25 22:23:25 +00001151
1152 // Post-visit the BlockExpr.
1153 CheckerVisit(BE, Dst, Tmp, false);
Ted Kremenekcfe223f2009-11-25 01:33:13 +00001154}
1155
Mike Stump11289f42009-09-09 15:08:12 +00001156void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
Ted Kremenek14536f62009-08-21 22:28:32 +00001157 ExplodedNodeSet &Dst, bool asLValue) {
Ted Kremenek04af9f22009-12-07 22:05:27 +00001158 VisitCommonDeclRefExpr(Ex, Ex->getDecl(), Pred, Dst, asLValue);
1159}
1160
1161void GRExprEngine::VisitBlockDeclRefExpr(BlockDeclRefExpr *Ex,
1162 ExplodedNode *Pred,
1163 ExplodedNodeSet &Dst, bool asLValue) {
1164 VisitCommonDeclRefExpr(Ex, Ex->getDecl(), Pred, Dst, asLValue);
1165}
1166
1167void GRExprEngine::VisitCommonDeclRefExpr(Expr *Ex, const NamedDecl *D,
1168 ExplodedNode *Pred,
1169 ExplodedNodeSet &Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001170
Ted Kremenek7020eae2009-09-11 22:07:28 +00001171 const GRState *state = GetState(Pred);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001172
1173 if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
1174
Ted Kremenek14536f62009-08-21 22:28:32 +00001175 SVal V = state->getLValue(VD, Pred->getLocationContext());
Zhongxing Xu252fe5c2008-10-17 02:20:14 +00001176
Zhongxing Xu232c7922008-10-16 06:09:51 +00001177 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001178 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001179 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001180 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001181 EvalLoad(Dst, Ex, Pred, state, V);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001182 return;
1183
1184 } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
1185 assert(!asLValue && "EnumConstantDecl does not have lvalue.");
1186
Zhongxing Xud09b5202009-06-23 06:13:19 +00001187 SVal V = ValMgr.makeIntVal(ED->getInitVal());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001188 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
Zhongxing Xu232c7922008-10-16 06:09:51 +00001189 return;
1190
1191 } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek1624a472009-09-23 01:30:01 +00001192 // This code is valid regardless of the value of 'isLValue'.
Zhongxing Xuac129432009-04-20 05:24:46 +00001193 SVal V = ValMgr.getFunctionPointer(FD);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001194 MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001195 ProgramPoint::PostLValueKind);
Zhongxing Xu232c7922008-10-16 06:09:51 +00001196 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Zhongxing Xu232c7922008-10-16 06:09:51 +00001199 assert (false &&
1200 "ValueDecl support for this ValueDecl not implemented.");
Ted Kremenek88da1de2008-02-07 04:16:04 +00001201}
1202
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001203/// VisitArraySubscriptExpr - Transfer function for array accesses
Mike Stump11289f42009-09-09 15:08:12 +00001204void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001205 ExplodedNode* Pred,
1206 ExplodedNodeSet& Dst, bool asLValue){
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001208 Expr* Base = A->getBase()->IgnoreParens();
Ted Kremenek10246e82008-04-29 23:24:44 +00001209 Expr* Idx = A->getIdx()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001210 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Ted Kremenekcce27f52009-02-24 02:23:11 +00001212 if (Base->getType()->isVectorType()) {
1213 // For vector types get its lvalue.
1214 // FIXME: This may not be correct. Is the rvalue of a vector its location?
1215 // In fact, I think this is just a hack. We need to get the right
1216 // semantics.
1217 VisitLValue(Base, Pred, Tmp);
1218 }
Mike Stump11289f42009-09-09 15:08:12 +00001219 else
Ted Kremenekcce27f52009-02-24 02:23:11 +00001220 Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
Mike Stump11289f42009-09-09 15:08:12 +00001221
Zhongxing Xu107f7592009-08-06 12:48:26 +00001222 for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
1223 ExplodedNodeSet Tmp2;
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001224 Visit(Idx, *I1, Tmp2); // Evaluate the index.
Mike Stump11289f42009-09-09 15:08:12 +00001225
Zhongxing Xub1667122009-11-11 13:42:54 +00001226 ExplodedNodeSet Tmp3;
1227 CheckerVisit(A, Tmp3, Tmp2, true);
1228
1229 for (ExplodedNodeSet::iterator I2=Tmp3.begin(),E2=Tmp3.end();I2!=E2; ++I2) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001230 const GRState* state = GetState(*I2);
Zhongxing Xu7d6387b2009-10-14 03:33:08 +00001231 SVal V = state->getLValue(A->getType(), state->getSVal(Idx),
1232 state->getSVal(Base));
Ted Kremenek10246e82008-04-29 23:24:44 +00001233
Zhongxing Xu232c7922008-10-16 06:09:51 +00001234 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001235 MakeNode(Dst, A, *I2, state->BindExpr(A, V),
Ted Kremeneka6e08322009-05-07 18:27:16 +00001236 ProgramPoint::PostLValueKind);
Ted Kremenek10246e82008-04-29 23:24:44 +00001237 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001238 EvalLoad(Dst, A, *I2, state, V);
Ted Kremenek10246e82008-04-29 23:24:44 +00001239 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001240 }
Ted Kremenekda5cdda2008-04-22 04:56:29 +00001241}
1242
Ted Kremenek38213f92008-04-21 23:43:38 +00001243/// VisitMemberExpr - Transfer function for member expressions.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001244void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
1245 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001246
Ted Kremenek38213f92008-04-21 23:43:38 +00001247 Expr* Base = M->getBase()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00001248 ExplodedNodeSet Tmp;
Mike Stump11289f42009-09-09 15:08:12 +00001249
1250 if (M->isArrow())
Ted Kremenekfef1f302008-10-18 03:28:48 +00001251 Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
1252 else
1253 VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
Mike Stump11289f42009-09-09 15:08:12 +00001254
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001255 FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
1256 if (!Field) // FIXME: skipping member expressions for non-fields
1257 return;
1258
Zhongxing Xu107f7592009-08-06 12:48:26 +00001259 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001260 const GRState* state = GetState(*I);
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001261 // FIXME: Should we insert some assumption logic in here to determine
1262 // if "Base" is a valid piece of memory? Before we put this assumption
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001263 // later when using FieldOffset lvals (which we no longer have).
Zhongxing Xub9eda672009-10-30 07:19:39 +00001264 SVal L = state->getLValue(Field, state->getSVal(Base));
Ted Kremenek3ad391d2008-10-17 00:51:01 +00001265
Zhongxing Xub9eda672009-10-30 07:19:39 +00001266 if (asLValue)
1267 MakeNode(Dst, M, *I, state->BindExpr(M, L), ProgramPoint::PostLValueKind);
1268 else
1269 EvalLoad(Dst, M, *I, state, L);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001270 }
Ted Kremenek38213f92008-04-21 23:43:38 +00001271}
1272
Ted Kremenek17d541d2009-02-13 01:45:31 +00001273/// EvalBind - Handle the semantics of binding a value to a specific location.
1274/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001275void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Stmt *AssignE,
1276 Stmt* StoreE, ExplodedNode* Pred,
Ted Kremenekb006b822009-11-04 00:09:15 +00001277 const GRState* state, SVal location, SVal Val,
1278 bool atDeclInit) {
Ted Kremenekef910042009-11-04 04:24:16 +00001279
1280
1281 // Do a previsit of the bind.
1282 ExplodedNodeSet CheckedSet, Src;
1283 Src.Add(Pred);
Ted Kremenek209e31b2009-11-05 00:42:23 +00001284 CheckerVisitBind(AssignE, StoreE, CheckedSet, Src, location, Val, true);
Ted Kremenekef910042009-11-04 04:24:16 +00001285
1286 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
1287 I!=E; ++I) {
1288
1289 if (Pred != *I)
1290 state = GetState(*I);
1291
1292 const GRState* newState = 0;
Ted Kremenek17d541d2009-02-13 01:45:31 +00001293
Ted Kremenekef910042009-11-04 04:24:16 +00001294 if (atDeclInit) {
1295 const VarRegion *VR =
1296 cast<VarRegion>(cast<loc::MemRegionVal>(location).getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001297
Ted Kremenekef910042009-11-04 04:24:16 +00001298 newState = state->bindDecl(VR, Val);
Ted Kremenekb006b822009-11-04 00:09:15 +00001299 }
1300 else {
Ted Kremenekef910042009-11-04 04:24:16 +00001301 if (location.isUnknown()) {
1302 // We know that the new state will be the same as the old state since
1303 // the location of the binding is "unknown". Consequently, there
1304 // is no reason to just create a new node.
1305 newState = state;
1306 }
1307 else {
1308 // We are binding to a value other than 'unknown'. Perform the binding
1309 // using the StoreManager.
1310 newState = state->bindLoc(cast<Loc>(location), Val);
1311 }
Ted Kremenekb006b822009-11-04 00:09:15 +00001312 }
Ted Kremenekef910042009-11-04 04:24:16 +00001313
1314 // The next thing to do is check if the GRTransferFuncs object wants to
1315 // update the state based on the new binding. If the GRTransferFunc object
1316 // doesn't do anything, just auto-propagate the current state.
Ted Kremenek209e31b2009-11-05 00:42:23 +00001317 GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, newState, StoreE,
Ted Kremenekef910042009-11-04 04:24:16 +00001318 newState != state);
1319
1320 getTF().EvalBind(BuilderRef, location, Val);
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001321 }
Ted Kremenek17d541d2009-02-13 01:45:31 +00001322}
1323
1324/// EvalStore - Handle the semantics of a store via an assignment.
1325/// @param Dst The node set to store generated state nodes
1326/// @param Ex The expression representing the location of the store
1327/// @param state The current simulation state
1328/// @param location The location to store the value
1329/// @param Val The value to be stored
Ted Kremenek209e31b2009-11-05 00:42:23 +00001330void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr *AssignE,
1331 Expr* StoreE,
1332 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001333 const GRState* state, SVal location, SVal Val,
1334 const void *tag) {
Mike Stump11289f42009-09-09 15:08:12 +00001335
Ted Kremenek209e31b2009-11-05 00:42:23 +00001336 assert(Builder && "GRStmtNodeBuilder must be defined.");
Mike Stump11289f42009-09-09 15:08:12 +00001337
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001338 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001339 ExplodedNodeSet Tmp;
1340 EvalLocation(Tmp, StoreE, Pred, state, location, tag, false);
Mike Stump11289f42009-09-09 15:08:12 +00001341
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001342 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001343 return;
Ted Kremenekc072b822008-04-18 20:35:30 +00001344
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001345 assert(!location.isUndef());
Ted Kremenek17d541d2009-02-13 01:45:31 +00001346
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001347 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind,
1348 ProgramPoint::PostStoreKind);
1349 SaveAndRestore<const void*> OldTag(Builder->Tag, tag);
1350
Mike Stump11289f42009-09-09 15:08:12 +00001351 // Proceed with the store.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001352 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
1353 EvalBind(Dst, AssignE, StoreE, *NI, GetState(*NI), location, Val);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001354}
1355
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001356void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr *Ex, ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +00001357 const GRState* state, SVal location,
Zhongxing Xu731f4622009-11-16 04:49:44 +00001358 const void *tag, QualType LoadTy) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001359
Mike Stump11289f42009-09-09 15:08:12 +00001360 // Evaluate the location (checks for bad dereferences).
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001361 ExplodedNodeSet Tmp;
1362 EvalLocation(Tmp, Ex, Pred, state, location, tag, true);
Mike Stump11289f42009-09-09 15:08:12 +00001363
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001364 if (Tmp.empty())
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001365 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001366
1367 assert(!location.isUndef());
1368
1369 SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
1370 SaveAndRestore<const void*> OldTag(Builder->Tag);
Mike Stump11289f42009-09-09 15:08:12 +00001371
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001372 // Proceed with the load.
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001373 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1374 state = GetState(*NI);
1375 if (location.isUnknown()) {
1376 // This is important. We must nuke the old binding.
1377 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, UnknownVal()),
1378 ProgramPoint::PostLoadKind, tag);
1379 }
1380 else {
Zhongxing Xu731f4622009-11-16 04:49:44 +00001381 SVal V = state->getSVal(cast<Loc>(location), LoadTy.isNull() ?
1382 Ex->getType() : LoadTy);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001383 MakeNode(Dst, Ex, *NI, state->BindExpr(Ex, V), ProgramPoint::PostLoadKind,
1384 tag);
1385 }
Zhongxing Xu33178a02008-11-28 08:34:30 +00001386 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00001387}
1388
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001389void GRExprEngine::EvalLocation(ExplodedNodeSet &Dst, Stmt *S,
1390 ExplodedNode* Pred,
1391 const GRState* state, SVal location,
1392 const void *tag, bool isLoad) {
Zhongxing Xuab0ae212009-11-20 03:50:46 +00001393 // Early checks for performance reason.
1394 if (location.isUnknown() || Checkers.empty()) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001395 Dst.Add(Pred);
1396 return;
Ted Kremenekfac290d2009-11-02 23:19:29 +00001397 }
1398
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001399 ExplodedNodeSet Src, Tmp;
1400 Src.Add(Pred);
1401 ExplodedNodeSet *PrevSet = &Src;
1402
1403 for (CheckersOrdered::iterator I=Checkers.begin(),E=Checkers.end(); I!=E; ++I)
1404 {
Ted Kremenek32c32892009-12-09 02:45:41 +00001405 ExplodedNodeSet *CurrSet = 0;
1406 if (I+1 == E)
1407 CurrSet = &Dst;
1408 else {
1409 CurrSet = (PrevSet == &Tmp) ? &Src : &Tmp;
1410 CurrSet->clear();
1411 }
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001412
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001413 void *tag = I->first;
1414 Checker *checker = I->second;
1415
1416 for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
Ted Kremenekf5735152009-11-23 22:22:01 +00001417 NI != NE; ++NI) {
1418 // Use the 'state' argument only when the predecessor node is the
1419 // same as Pred. This allows us to catch updates to the state.
1420 checker->GR_VisitLocation(*CurrSet, *Builder, *this, S, *NI,
1421 *NI == Pred ? state : GetState(*NI),
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001422 location, tag, isLoad);
Ted Kremenekf5735152009-11-23 22:22:01 +00001423 }
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001424
1425 // Update which NodeSet is the current one.
1426 PrevSet = CurrSet;
1427 }
Ted Kremenek90c7cb62008-04-16 18:39:06 +00001428}
1429
Ted Kremenek667cacb2008-04-15 23:06:53 +00001430//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +00001431// Transfer function: OSAtomics.
1432//
1433// FIXME: Eventually refactor into a more "plugin" infrastructure.
1434//===----------------------------------------------------------------------===//
1435
1436// Mac OS X:
1437// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
1438// atomic.3.html
1439//
Zhongxing Xu20227f72009-08-06 01:32:16 +00001440static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst,
Ted Kremenekdf240002009-04-11 00:11:10 +00001441 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001442 GRStmtNodeBuilder& Builder,
Zhongxing Xu223f5112009-11-16 02:52:18 +00001443 CallExpr* CE, ExplodedNode* Pred) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001444
1445 // Not enough arguments to match OSAtomicCompareAndSwap?
1446 if (CE->getNumArgs() != 3)
1447 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001448
Ted Kremenekdf240002009-04-11 00:11:10 +00001449 ASTContext &C = Engine.getContext();
1450 Expr *oldValueExpr = CE->getArg(0);
1451 QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
1452
1453 Expr *newValueExpr = CE->getArg(1);
1454 QualType newValueType = C.getCanonicalType(newValueExpr->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001455
Ted Kremenekdf240002009-04-11 00:11:10 +00001456 // Do the types of 'oldValue' and 'newValue' match?
1457 if (oldValueType != newValueType)
1458 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001459
Ted Kremenekdf240002009-04-11 00:11:10 +00001460 Expr *theValueExpr = CE->getArg(2);
Ted Kremenekeb01ba62009-08-01 05:59:39 +00001461 const PointerType *theValueType =
1462 theValueExpr->getType()->getAs<PointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00001463
Ted Kremenekdf240002009-04-11 00:11:10 +00001464 // theValueType not a pointer?
1465 if (!theValueType)
1466 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001467
Ted Kremenekdf240002009-04-11 00:11:10 +00001468 QualType theValueTypePointee =
1469 C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
Mike Stump11289f42009-09-09 15:08:12 +00001470
Ted Kremenekdf240002009-04-11 00:11:10 +00001471 // The pointee must match newValueType and oldValueType.
1472 if (theValueTypePointee != newValueType)
1473 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001474
Ted Kremenekdf240002009-04-11 00:11:10 +00001475 static unsigned magic_load = 0;
1476 static unsigned magic_store = 0;
1477
1478 const void *OSAtomicLoadTag = &magic_load;
1479 const void *OSAtomicStoreTag = &magic_store;
Mike Stump11289f42009-09-09 15:08:12 +00001480
Ted Kremenekdf240002009-04-11 00:11:10 +00001481 // Load 'theValue'.
Ted Kremenekdf240002009-04-11 00:11:10 +00001482 const GRState *state = Pred->getState();
Zhongxing Xu20227f72009-08-06 01:32:16 +00001483 ExplodedNodeSet Tmp;
Ted Kremenek095f1a92009-06-18 23:58:37 +00001484 SVal location = state->getSVal(theValueExpr);
Zhongxing Xu731f4622009-11-16 04:49:44 +00001485 // Here we should use the value type of the region as the load type.
1486 const MemRegion *R = location.getAsRegion();
1487 QualType LoadTy;
1488 if (R)
1489 LoadTy = cast<TypedRegion>(R)->getValueType(C);
1490 Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag,
1491 LoadTy);
Ted Kremenekdf240002009-04-11 00:11:10 +00001492
Zhongxing Xu20227f72009-08-06 01:32:16 +00001493 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
Ted Kremenekdf240002009-04-11 00:11:10 +00001494 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00001495
Zhongxing Xu20227f72009-08-06 01:32:16 +00001496 ExplodedNode *N = *I;
Ted Kremenekdf240002009-04-11 00:11:10 +00001497 const GRState *stateLoad = N->getState();
Ted Kremenek7020eae2009-09-11 22:07:28 +00001498 SVal theValueVal_untested = stateLoad->getSVal(theValueExpr);
1499 SVal oldValueVal_untested = stateLoad->getSVal(oldValueExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001500
Ted Kremeneka1f9c7f2009-07-20 20:38:59 +00001501 // FIXME: Issue an error.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001502 if (theValueVal_untested.isUndef() || oldValueVal_untested.isUndef()) {
Mike Stump11289f42009-09-09 15:08:12 +00001503 return false;
Ted Kremeneka1f9c7f2009-07-20 20:38:59 +00001504 }
Ted Kremenek7020eae2009-09-11 22:07:28 +00001505
1506 DefinedOrUnknownSVal theValueVal =
1507 cast<DefinedOrUnknownSVal>(theValueVal_untested);
1508 DefinedOrUnknownSVal oldValueVal =
1509 cast<DefinedOrUnknownSVal>(oldValueVal_untested);
Mike Stump11289f42009-09-09 15:08:12 +00001510
Ted Kremenek3a459dc2009-08-25 18:44:25 +00001511 SValuator &SVator = Engine.getSValuator();
Mike Stump11289f42009-09-09 15:08:12 +00001512
Ted Kremenekdf240002009-04-11 00:11:10 +00001513 // Perform the comparison.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001514 DefinedOrUnknownSVal Cmp = SVator.EvalEQ(stateLoad, theValueVal,
1515 oldValueVal);
Ted Kremenekf9906842009-06-18 22:57:13 +00001516
Ted Kremenek7020eae2009-09-11 22:07:28 +00001517 const GRState *stateEqual = stateLoad->Assume(Cmp, true);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Ted Kremenekdf240002009-04-11 00:11:10 +00001519 // Were they equal?
Ted Kremenekf9906842009-06-18 22:57:13 +00001520 if (stateEqual) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001521 // Perform the store.
Zhongxing Xu20227f72009-08-06 01:32:16 +00001522 ExplodedNodeSet TmpStore;
Ted Kremenekac7c7242009-07-21 21:03:30 +00001523 SVal val = stateEqual->getSVal(newValueExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001524
Ted Kremenekac7c7242009-07-21 21:03:30 +00001525 // Handle implicit value casts.
1526 if (const TypedRegion *R =
1527 dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
Ted Kremenek3a459dc2009-08-25 18:44:25 +00001528 llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C),
1529 newValueExpr->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001530 }
1531
Ted Kremenek209e31b2009-11-05 00:42:23 +00001532 Engine.EvalStore(TmpStore, NULL, theValueExpr, N, stateEqual, location,
Ted Kremenekac7c7242009-07-21 21:03:30 +00001533 val, OSAtomicStoreTag);
Mike Stump11289f42009-09-09 15:08:12 +00001534
Ted Kremenekdf240002009-04-11 00:11:10 +00001535 // Now bind the result of the comparison.
Zhongxing Xu20227f72009-08-06 01:32:16 +00001536 for (ExplodedNodeSet::iterator I2 = TmpStore.begin(),
Ted Kremenekdf240002009-04-11 00:11:10 +00001537 E2 = TmpStore.end(); I2 != E2; ++I2) {
Zhongxing Xu20227f72009-08-06 01:32:16 +00001538 ExplodedNode *predNew = *I2;
Ted Kremenekdf240002009-04-11 00:11:10 +00001539 const GRState *stateNew = predNew->getState();
1540 SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001541 Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res));
Ted Kremenekdf240002009-04-11 00:11:10 +00001542 }
1543 }
Mike Stump11289f42009-09-09 15:08:12 +00001544
Ted Kremenekdf240002009-04-11 00:11:10 +00001545 // Were they not equal?
Ted Kremenek7020eae2009-09-11 22:07:28 +00001546 if (const GRState *stateNotEqual = stateLoad->Assume(Cmp, false)) {
Ted Kremenekdf240002009-04-11 00:11:10 +00001547 SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001548 Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res));
Ted Kremenekdf240002009-04-11 00:11:10 +00001549 }
1550 }
Mike Stump11289f42009-09-09 15:08:12 +00001551
Ted Kremenekdf240002009-04-11 00:11:10 +00001552 return true;
1553}
1554
Zhongxing Xu20227f72009-08-06 01:32:16 +00001555static bool EvalOSAtomic(ExplodedNodeSet& Dst,
Ted Kremenekdf240002009-04-11 00:11:10 +00001556 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001557 GRStmtNodeBuilder& Builder,
Ted Kremenekdf240002009-04-11 00:11:10 +00001558 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001559 ExplodedNode* Pred) {
Zhongxing Xuac129432009-04-20 05:24:46 +00001560 const FunctionDecl* FD = L.getAsFunctionDecl();
1561 if (!FD)
Ted Kremenekdf240002009-04-11 00:11:10 +00001562 return false;
Zhongxing Xuac129432009-04-20 05:24:46 +00001563
Ted Kremenekdf240002009-04-11 00:11:10 +00001564 const char *FName = FD->getNameAsCString();
Mike Stump11289f42009-09-09 15:08:12 +00001565
Ted Kremenekdf240002009-04-11 00:11:10 +00001566 // Check for compare and swap.
Ted Kremenek4531be12009-04-11 00:54:13 +00001567 if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
1568 strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
Zhongxing Xu223f5112009-11-16 02:52:18 +00001569 return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, Pred);
Ted Kremenek4531be12009-04-11 00:54:13 +00001570
Ted Kremenekdf240002009-04-11 00:11:10 +00001571 // FIXME: Other atomics.
1572 return false;
1573}
1574
1575//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001576// Transfer function: Function calls.
1577//===----------------------------------------------------------------------===//
Ted Kremenekdf240002009-04-11 00:11:10 +00001578
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001579bool GRExprEngine::EvalBuiltinFunction(const FunctionDecl *FD, CallExpr *CE,
1580 ExplodedNode *Pred,
1581 ExplodedNodeSet &Dst) {
1582 if (!FD)
1583 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001584
Douglas Gregor15fc9562009-09-12 00:22:50 +00001585 unsigned id = FD->getBuiltinID();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001586 if (!id)
1587 return false;
1588
1589 const GRState *state = Pred->getState();
1590
1591 switch (id) {
1592 case Builtin::BI__builtin_expect: {
1593 // For __builtin_expect, just return the value of the subexpression.
Mike Stump11289f42009-09-09 15:08:12 +00001594 assert (CE->arg_begin() != CE->arg_end());
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001595 SVal X = state->getSVal(*(CE->arg_begin()));
1596 MakeNode(Dst, CE, Pred, state->BindExpr(CE, X));
1597 return true;
1598 }
Mike Stump11289f42009-09-09 15:08:12 +00001599
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001600 case Builtin::BI__builtin_alloca: {
1601 // FIXME: Refactor into StoreManager itself?
1602 MemRegionManager& RM = getStateManager().getRegionManager();
1603 const MemRegion* R =
Ted Kremenek04af9f22009-12-07 22:05:27 +00001604 RM.getAllocaRegion(CE, Builder->getCurrentBlockCount(),
1605 Pred->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +00001606
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001607 // Set the extent of the region in bytes. This enables us to use the
1608 // SVal of the argument directly. If we save the extent in bits, we
1609 // cannot represent values like symbol*8.
1610 SVal Extent = state->getSVal(*(CE->arg_begin()));
1611 state = getStoreManager().setExtent(state, R, Extent);
1612 MakeNode(Dst, CE, Pred, state->BindExpr(CE, loc::MemRegionVal(R)));
1613 return true;
1614 }
1615 }
1616
1617 return false;
1618}
1619
Zhongxing Xu107f7592009-08-06 12:48:26 +00001620void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001621 CallExpr::arg_iterator AI,
1622 CallExpr::arg_iterator AE,
Mike Stump11289f42009-09-09 15:08:12 +00001623 ExplodedNodeSet& Dst) {
Douglas Gregor6b754842008-10-28 00:22:11 +00001624 // Determine the type of function we're calling (if available).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001625 const FunctionProtoType *Proto = NULL;
Douglas Gregor6b754842008-10-28 00:22:11 +00001626 QualType FnType = CE->getCallee()->IgnoreParens()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001627 if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
John McCall9dd450b2009-09-21 23:43:11 +00001628 Proto = FnTypePtr->getPointeeType()->getAs<FunctionProtoType>();
Douglas Gregor6b754842008-10-28 00:22:11 +00001629
1630 VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
1631}
1632
Zhongxing Xu107f7592009-08-06 12:48:26 +00001633void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred,
Douglas Gregor6b754842008-10-28 00:22:11 +00001634 CallExpr::arg_iterator AI,
1635 CallExpr::arg_iterator AE,
Mike Stump11289f42009-09-09 15:08:12 +00001636 ExplodedNodeSet& Dst,
1637 const FunctionProtoType *Proto,
Douglas Gregor6b754842008-10-28 00:22:11 +00001638 unsigned ParamIdx) {
Mike Stump11289f42009-09-09 15:08:12 +00001639
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001640 // Process the arguments.
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001641 if (AI != AE) {
Douglas Gregor6b754842008-10-28 00:22:11 +00001642 // If the call argument is being bound to a reference parameter,
1643 // visit it as an lvalue, not an rvalue.
1644 bool VisitAsLvalue = false;
1645 if (Proto && ParamIdx < Proto->getNumArgs())
1646 VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
1647
Mike Stump11289f42009-09-09 15:08:12 +00001648 ExplodedNodeSet DstTmp;
Douglas Gregor6b754842008-10-28 00:22:11 +00001649 if (VisitAsLvalue)
Mike Stump11289f42009-09-09 15:08:12 +00001650 VisitLValue(*AI, Pred, DstTmp);
Douglas Gregor6b754842008-10-28 00:22:11 +00001651 else
Mike Stump11289f42009-09-09 15:08:12 +00001652 Visit(*AI, Pred, DstTmp);
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001653 ++AI;
Mike Stump11289f42009-09-09 15:08:12 +00001654
Zhongxing Xu107f7592009-08-06 12:48:26 +00001655 for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE;
1656 ++DI)
Douglas Gregor6b754842008-10-28 00:22:11 +00001657 VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
Mike Stump11289f42009-09-09 15:08:12 +00001658
Ted Kremeneke0188e62008-02-19 01:44:53 +00001659 return;
1660 }
1661
1662 // If we reach here we have processed all of the arguments. Evaluate
1663 // the callee expression.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001664 ExplodedNodeSet DstTmp;
Ted Kremenekdd43aee2008-04-23 20:12:28 +00001665 Expr* Callee = CE->getCallee()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00001666
Ted Kremenek49513cc2009-07-22 21:43:51 +00001667 { // Enter new scope to make the lifetime of 'DstTmp2' bounded.
Zhongxing Xu107f7592009-08-06 12:48:26 +00001668 ExplodedNodeSet DstTmp2;
Ted Kremenek49513cc2009-07-22 21:43:51 +00001669 Visit(Callee, Pred, DstTmp2);
Mike Stump11289f42009-09-09 15:08:12 +00001670
Ted Kremenek49513cc2009-07-22 21:43:51 +00001671 // Perform the previsit of the CallExpr, storing the results in DstTmp.
1672 CheckerVisit(CE, DstTmp, DstTmp2, true);
1673 }
Mike Stump11289f42009-09-09 15:08:12 +00001674
Ted Kremeneke0188e62008-02-19 01:44:53 +00001675 // Finally, evaluate the function call.
Ted Kremenek32c32892009-12-09 02:45:41 +00001676 ExplodedNodeSet DstTmp3;
1677
Mike Stump11289f42009-09-09 15:08:12 +00001678 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Zhongxing Xu88f07cd2009-09-05 05:00:57 +00001679 DI != DE; ++DI) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00001680
Ted Kremenek17d541d2009-02-13 01:45:31 +00001681 const GRState* state = GetState(*DI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001682 SVal L = state->getSVal(Callee);
Ted Kremeneke0188e62008-02-19 01:44:53 +00001683
Ted Kremenek8efd6b4e2008-03-03 16:47:31 +00001684 // FIXME: Add support for symbolic function calls (calls involving
1685 // function pointer values that are symbolic).
Ted Kremenek70342362008-03-05 21:15:02 +00001686 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
Ted Kremenek32c32892009-12-09 02:45:41 +00001687 ExplodedNodeSet DstChecker;
Mike Stump11289f42009-09-09 15:08:12 +00001688
Zhongxing Xu175447f2009-12-07 09:17:35 +00001689 // If the callee is processed by a checker, skip the rest logic.
1690 if (CheckerEvalCall(CE, DstChecker, *DI))
1691 DstTmp3 = DstChecker;
1692 else {
Ted Kremenek32c32892009-12-09 02:45:41 +00001693 for (ExplodedNodeSet::iterator DI_Checker = DstChecker.begin(),
1694 DE_Checker = DstChecker.end();
1695 DI_Checker != DE_Checker; ++DI_Checker) {
Zhongxing Xu175447f2009-12-07 09:17:35 +00001696
Ted Kremenek32c32892009-12-09 02:45:41 +00001697 // Dispatch to the plug-in transfer function.
1698 unsigned OldSize = DstTmp3.size();
1699 SaveOr OldHasGen(Builder->HasGeneratedNode);
1700 Pred = *DI_Checker;
1701
1702 // Dispatch to transfer function logic to handle the call itself.
1703 // FIXME: Allow us to chain together transfer functions.
1704 assert(Builder && "GRStmtNodeBuilder must be defined.");
Ted Kremeneka971afb2009-11-12 04:35:08 +00001705
Ted Kremenek32c32892009-12-09 02:45:41 +00001706 if (!EvalOSAtomic(DstTmp3, *this, *Builder, CE, L, Pred))
1707 getTF().EvalCall(DstTmp3, *this, *Builder, CE, L, Pred);
Zhongxing Xu175447f2009-12-07 09:17:35 +00001708
Ted Kremenek32c32892009-12-09 02:45:41 +00001709 // Handle the case where no nodes where generated. Auto-generate that
1710 // contains the updated state if we aren't generating sinks.
1711 if (!Builder->BuildSinks && DstTmp3.size() == OldSize &&
1712 !Builder->HasGeneratedNode)
1713 MakeNode(DstTmp3, CE, Pred, state);
1714 }
Zhongxing Xu175447f2009-12-07 09:17:35 +00001715 }
Ted Kremeneke0188e62008-02-19 01:44:53 +00001716 }
Ted Kremenek32c32892009-12-09 02:45:41 +00001717
1718 // Perform the post-condition check of the CallExpr.
1719 CheckerVisit(CE, Dst, DstTmp3, false);
Ted Kremeneke0188e62008-02-19 01:44:53 +00001720}
1721
Ted Kremenek667cacb2008-04-15 23:06:53 +00001722//===----------------------------------------------------------------------===//
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001723// Transfer function: Objective-C ivar references.
1724//===----------------------------------------------------------------------===//
1725
Ted Kremenek111a6bd2009-02-28 20:50:43 +00001726static std::pair<const void*,const void*> EagerlyAssumeTag
1727 = std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
1728
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001729void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
1730 Expr *Ex) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001731 for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
1732 ExplodedNode *Pred = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001733
Ted Kremenekff290ca2009-02-25 23:32:10 +00001734 // Test if the previous node was as the same expression. This can happen
1735 // when the expression fails to evaluate to anything meaningful and
1736 // (as an optimization) we don't generate a node.
Mike Stump11289f42009-09-09 15:08:12 +00001737 ProgramPoint P = Pred->getLocation();
Ted Kremenekff290ca2009-02-25 23:32:10 +00001738 if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
Mike Stump11289f42009-09-09 15:08:12 +00001739 Dst.Add(Pred);
Ted Kremenekff290ca2009-02-25 23:32:10 +00001740 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001741 }
Ted Kremenekff290ca2009-02-25 23:32:10 +00001742
Mike Stump11289f42009-09-09 15:08:12 +00001743 const GRState* state = Pred->getState();
1744 SVal V = state->getSVal(Ex);
Ted Kremenek7020eae2009-09-11 22:07:28 +00001745 if (nonloc::SymExprVal *SEV = dyn_cast<nonloc::SymExprVal>(&V)) {
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001746 // First assume that the condition is true.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001747 if (const GRState *stateTrue = state->Assume(*SEV, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00001748 stateTrue = stateTrue->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001749 ValMgr.makeIntVal(1U, Ex->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001750 Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
Zhongxing Xue1190f72009-08-15 03:17:38 +00001751 &EagerlyAssumeTag, Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001752 stateTrue, Pred));
1753 }
Mike Stump11289f42009-09-09 15:08:12 +00001754
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001755 // Next, assume that the condition is false.
Ted Kremenek7020eae2009-09-11 22:07:28 +00001756 if (const GRState *stateFalse = state->Assume(*SEV, false)) {
Mike Stump11289f42009-09-09 15:08:12 +00001757 stateFalse = stateFalse->BindExpr(Ex,
Ted Kremenek907a7112009-08-27 01:39:13 +00001758 ValMgr.makeIntVal(0U, Ex->getType()));
Zhongxing Xue1190f72009-08-15 03:17:38 +00001759 Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
1760 Pred->getLocationContext()),
Ted Kremenekdc3f50f2009-02-25 22:32:02 +00001761 stateFalse, Pred));
1762 }
1763 }
1764 else
1765 Dst.Add(Pred);
1766 }
1767}
1768
1769//===----------------------------------------------------------------------===//
1770// Transfer function: Objective-C ivar references.
1771//===----------------------------------------------------------------------===//
1772
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001773void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, ExplodedNode* Pred,
1774 ExplodedNodeSet& Dst, bool asLValue) {
Mike Stump11289f42009-09-09 15:08:12 +00001775
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001776 Expr* Base = cast<Expr>(Ex->getBase());
Zhongxing Xu107f7592009-08-06 12:48:26 +00001777 ExplodedNodeSet Tmp;
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001778 Visit(Base, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001779
Zhongxing Xu107f7592009-08-06 12:48:26 +00001780 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00001781 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001782 SVal BaseVal = state->getSVal(Base);
1783 SVal location = state->getLValue(Ex->getDecl(), BaseVal);
Mike Stump11289f42009-09-09 15:08:12 +00001784
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001785 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00001786 MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001787 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00001788 EvalLoad(Dst, Ex, *I, state, location);
Ted Kremenek12dd55b2008-10-17 00:03:18 +00001789 }
1790}
1791
1792//===----------------------------------------------------------------------===//
Ted Kremenek17810802008-11-12 19:24:17 +00001793// Transfer function: Objective-C fast enumeration 'for' statements.
1794//===----------------------------------------------------------------------===//
1795
1796void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001797 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001798
Ted Kremenek17810802008-11-12 19:24:17 +00001799 // ObjCForCollectionStmts are processed in two places. This method
1800 // handles the case where an ObjCForCollectionStmt* occurs as one of the
1801 // statements within a basic block. This transfer function does two things:
1802 //
1803 // (1) binds the next container value to 'element'. This creates a new
1804 // node in the ExplodedGraph.
1805 //
1806 // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
1807 // whether or not the container has any more elements. This value
1808 // will be tested in ProcessBranch. We need to explicitly bind
1809 // this value because a container can contain nil elements.
Mike Stump11289f42009-09-09 15:08:12 +00001810 //
Ted Kremenek17810802008-11-12 19:24:17 +00001811 // FIXME: Eventually this logic should actually do dispatches to
1812 // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
1813 // This will require simulating a temporary NSFastEnumerationState, either
1814 // through an SVal or through the use of MemRegions. This value can
1815 // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
1816 // terminates we reclaim the temporary (it goes out of scope) and we
1817 // we can test if the SVal is 0 or if the MemRegion is null (depending
1818 // on what approach we take).
1819 //
1820 // For now: simulate (1) by assigning either a symbol or nil if the
1821 // container is empty. Thus this transfer function will by default
1822 // result in state splitting.
Mike Stump11289f42009-09-09 15:08:12 +00001823
Ted Kremenek537f6382008-11-14 19:47:18 +00001824 Stmt* elem = S->getElement();
1825 SVal ElementV;
Mike Stump11289f42009-09-09 15:08:12 +00001826
Ted Kremenek17810802008-11-12 19:24:17 +00001827 if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
Chris Lattner529efc72009-03-28 06:33:19 +00001828 VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
Ted Kremenek17810802008-11-12 19:24:17 +00001829 assert (ElemD->getInit() == 0);
Ted Kremenek14536f62009-08-21 22:28:32 +00001830 ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
Ted Kremenek537f6382008-11-14 19:47:18 +00001831 VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
1832 return;
Ted Kremenek17810802008-11-12 19:24:17 +00001833 }
Ted Kremenek537f6382008-11-14 19:47:18 +00001834
Zhongxing Xu107f7592009-08-06 12:48:26 +00001835 ExplodedNodeSet Tmp;
Ted Kremenek537f6382008-11-14 19:47:18 +00001836 VisitLValue(cast<Expr>(elem), Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001837
Zhongxing Xu107f7592009-08-06 12:48:26 +00001838 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001839 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00001840 VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
Ted Kremenek537f6382008-11-14 19:47:18 +00001841 }
1842}
1843
1844void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001845 ExplodedNode* Pred, ExplodedNodeSet& Dst,
Ted Kremenek537f6382008-11-14 19:47:18 +00001846 SVal ElementV) {
Ted Kremenek537f6382008-11-14 19:47:18 +00001847
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001848 // Check if the location we are writing back to is a null pointer.
Ted Kremenek537f6382008-11-14 19:47:18 +00001849 Stmt* elem = S->getElement();
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001850 ExplodedNodeSet Tmp;
1851 EvalLocation(Tmp, elem, Pred, GetState(Pred), ElementV, NULL, false);
1852
1853 if (Tmp.empty())
Ted Kremenek537f6382008-11-14 19:47:18 +00001854 return;
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001855
1856 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
1857 Pred = *NI;
1858 const GRState *state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001859
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001860 // Handle the case where the container still has elements.
1861 SVal TrueV = ValMgr.makeTruthVal(1);
1862 const GRState *hasElems = state->BindExpr(S, TrueV);
Ted Kremenek537f6382008-11-14 19:47:18 +00001863
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001864 // Handle the case where the container has no elements.
1865 SVal FalseV = ValMgr.makeTruthVal(0);
1866 const GRState *noElems = state->BindExpr(S, FalseV);
Mike Stump11289f42009-09-09 15:08:12 +00001867
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001868 if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
1869 if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) {
1870 // FIXME: The proper thing to do is to really iterate over the
1871 // container. We will do this with dispatch logic to the store.
1872 // For now, just 'conjure' up a symbolic value.
1873 QualType T = R->getValueType(getContext());
1874 assert(Loc::IsLocType(T));
1875 unsigned Count = Builder->getCurrentBlockCount();
1876 SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
1877 SVal V = ValMgr.makeLoc(Sym);
1878 hasElems = hasElems->bindLoc(ElementV, V);
Mike Stump11289f42009-09-09 15:08:12 +00001879
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001880 // Bind the location to 'nil' on the false branch.
1881 SVal nilV = ValMgr.makeIntVal(0, T);
1882 noElems = noElems->bindLoc(ElementV, nilV);
1883 }
Ted Kremenekdf317922008-11-12 21:12:46 +00001884
Ted Kremenek5e1f78a2009-11-11 03:26:34 +00001885 // Create the new nodes.
1886 MakeNode(Dst, S, Pred, hasElems);
1887 MakeNode(Dst, S, Pred, noElems);
1888 }
Ted Kremenek17810802008-11-12 19:24:17 +00001889}
1890
1891//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +00001892// Transfer function: Objective-C message expressions.
1893//===----------------------------------------------------------------------===//
1894
Zhongxing Xu107f7592009-08-06 12:48:26 +00001895void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
1896 ExplodedNodeSet& Dst){
Mike Stump11289f42009-09-09 15:08:12 +00001897
Ted Kremenek667cacb2008-04-15 23:06:53 +00001898 VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
1899 Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00001900}
Ted Kremenek667cacb2008-04-15 23:06:53 +00001901
1902void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
Zhongxing Xu4de1c852008-10-31 07:26:14 +00001903 ObjCMessageExpr::arg_iterator AI,
1904 ObjCMessageExpr::arg_iterator AE,
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001905 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek667cacb2008-04-15 23:06:53 +00001906 if (AI == AE) {
Mike Stump11289f42009-09-09 15:08:12 +00001907
Ted Kremenek667cacb2008-04-15 23:06:53 +00001908 // Process the receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001909
Ted Kremenek667cacb2008-04-15 23:06:53 +00001910 if (Expr* Receiver = ME->getReceiver()) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00001911 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001912 Visit(Receiver, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001913
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001914 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE;
1915 ++NI)
Daniel Dunbar8e31e772009-07-23 04:41:06 +00001916 VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00001917
Ted Kremenek667cacb2008-04-15 23:06:53 +00001918 return;
1919 }
Mike Stump11289f42009-09-09 15:08:12 +00001920
Daniel Dunbar8e31e772009-07-23 04:41:06 +00001921 VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
Ted Kremenek667cacb2008-04-15 23:06:53 +00001922 return;
1923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Zhongxing Xu107f7592009-08-06 12:48:26 +00001925 ExplodedNodeSet Tmp;
Ted Kremenek667cacb2008-04-15 23:06:53 +00001926 Visit(*AI, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00001927
Ted Kremenek667cacb2008-04-15 23:06:53 +00001928 ++AI;
Mike Stump11289f42009-09-09 15:08:12 +00001929
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00001930 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end();NI != NE;++NI)
Ted Kremenek667cacb2008-04-15 23:06:53 +00001931 VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
1932}
1933
1934void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001935 ExplodedNode* Pred,
1936 ExplodedNodeSet& Dst) {
Mike Stump11289f42009-09-09 15:08:12 +00001937
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001938 // Handle previsits checks.
1939 ExplodedNodeSet Src, DstTmp;
Ted Kremenek005e8a02009-11-24 21:41:28 +00001940 Src.Add(Pred);
1941
Zhongxing Xuaf353292009-12-02 05:49:12 +00001942 CheckerVisit(ME, DstTmp, Src, true);
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001943
Ted Kremenek667cacb2008-04-15 23:06:53 +00001944 unsigned size = Dst.size();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001945
Ted Kremenek18c7cee2009-11-03 08:03:59 +00001946 for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001947 DI!=DE; ++DI) {
1948 Pred = *DI;
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001949 bool RaisesException = false;
1950
Zhongxing Xuaf353292009-12-02 05:49:12 +00001951 if (const Expr *Receiver = ME->getReceiver()) {
1952 const GRState *state = Pred->getState();
1953
1954 // Bifurcate the state into nil and non-nil ones.
1955 DefinedOrUnknownSVal receiverVal =
1956 cast<DefinedOrUnknownSVal>(state->getSVal(Receiver));
1957
1958 const GRState *notNilState, *nilState;
1959 llvm::tie(notNilState, nilState) = state->Assume(receiverVal);
1960
1961 // There are three cases: can be nil or non-nil, must be nil, must be
1962 // non-nil. We handle must be nil, and merge the rest two into non-nil.
1963 if (nilState && !notNilState) {
1964 CheckerEvalNilReceiver(ME, Dst, nilState, Pred);
1965 return;
1966 }
1967
1968 assert(notNilState);
1969
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001970 // Check if the "raise" message was sent.
1971 if (ME->getSelector() == RaiseSel)
1972 RaisesException = true;
Zhongxing Xuaf353292009-12-02 05:49:12 +00001973
1974 // Check if we raise an exception. For now treat these as sinks.
1975 // Eventually we will want to handle exceptions properly.
1976 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
1977 if (RaisesException)
1978 Builder->BuildSinks = true;
1979
1980 // Dispatch to plug-in transfer function.
1981 SaveOr OldHasGen(Builder->HasGeneratedNode);
1982 EvalObjCMessageExpr(Dst, ME, Pred, notNilState);
Ted Kremenekcaf2c512009-11-21 01:25:37 +00001983 }
1984 else {
1985
1986 IdentifierInfo* ClsName = ME->getClassName();
1987 Selector S = ME->getSelector();
1988
1989 // Check for special instance methods.
1990
1991 if (!NSExceptionII) {
1992 ASTContext& Ctx = getContext();
1993
1994 NSExceptionII = &Ctx.Idents.get("NSException");
1995 }
1996
1997 if (ClsName == NSExceptionII) {
1998
1999 enum { NUM_RAISE_SELECTORS = 2 };
2000
2001 // Lazily create a cache of the selectors.
2002
2003 if (!NSExceptionInstanceRaiseSelectors) {
2004
2005 ASTContext& Ctx = getContext();
2006
2007 NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
2008
2009 llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
2010 unsigned idx = 0;
2011
2012 // raise:format:
2013 II.push_back(&Ctx.Idents.get("raise"));
2014 II.push_back(&Ctx.Idents.get("format"));
2015 NSExceptionInstanceRaiseSelectors[idx++] =
2016 Ctx.Selectors.getSelector(II.size(), &II[0]);
2017
2018 // raise:format::arguments:
2019 II.push_back(&Ctx.Idents.get("arguments"));
2020 NSExceptionInstanceRaiseSelectors[idx++] =
2021 Ctx.Selectors.getSelector(II.size(), &II[0]);
2022 }
2023
2024 for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
2025 if (S == NSExceptionInstanceRaiseSelectors[i]) {
2026 RaisesException = true; break;
2027 }
2028 }
Zhongxing Xuaf353292009-12-02 05:49:12 +00002029
2030 // Check if we raise an exception. For now treat these as sinks.
2031 // Eventually we will want to handle exceptions properly.
2032 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2033 if (RaisesException)
2034 Builder->BuildSinks = true;
2035
2036 // Dispatch to plug-in transfer function.
2037 SaveOr OldHasGen(Builder->HasGeneratedNode);
2038 EvalObjCMessageExpr(Dst, ME, Pred, Builder->GetState(Pred));
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002039 }
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002040 }
Mike Stump11289f42009-09-09 15:08:12 +00002041
Ted Kremenek667cacb2008-04-15 23:06:53 +00002042 // Handle the case where no nodes where generated. Auto-generate that
2043 // contains the updated state if we aren't generating sinks.
Ted Kremenekc072b822008-04-18 20:35:30 +00002044 if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
Ted Kremenekcaf2c512009-11-21 01:25:37 +00002045 MakeNode(Dst, ME, Pred, GetState(Pred));
Ted Kremenek667cacb2008-04-15 23:06:53 +00002046}
2047
2048//===----------------------------------------------------------------------===//
2049// Transfer functions: Miscellaneous statements.
2050//===----------------------------------------------------------------------===//
2051
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002052void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred,
2053 ExplodedNodeSet& Dst){
Zhongxing Xu107f7592009-08-06 12:48:26 +00002054 ExplodedNodeSet S1;
Ted Kremenek9fd25312008-02-19 18:52:54 +00002055 QualType T = CastE->getType();
Zhongxing Xudab76fd2008-10-21 06:54:23 +00002056 QualType ExTy = Ex->getType();
Zhongxing Xuc2721522008-10-22 08:02:16 +00002057
Zhongxing Xu4de1c852008-10-31 07:26:14 +00002058 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
Douglas Gregore200adc2008-10-27 19:41:14 +00002059 T = ExCast->getTypeAsWritten();
2060
Zhongxing Xuc2721522008-10-22 08:02:16 +00002061 if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002062 VisitLValue(Ex, Pred, S1);
Ted Kremenek9f3f8272008-03-04 22:16:08 +00002063 else
2064 Visit(Ex, Pred, S1);
Mike Stump11289f42009-09-09 15:08:12 +00002065
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002066 ExplodedNodeSet S2;
2067 CheckerVisit(CastE, S2, S1, true);
2068
Ted Kremenekeccf3e52008-04-22 21:10:18 +00002069 // Check for casting to "void".
Mike Stump11289f42009-09-09 15:08:12 +00002070 if (T->isVoidType()) {
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002071 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I)
2072 Dst.Add(*I);
Ted Kremenek33d82852008-01-24 02:02:54 +00002073 return;
2074 }
Ted Kremenekac7c7242009-07-21 21:03:30 +00002075
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002076 for (ExplodedNodeSet::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
2077 ExplodedNode* N = *I;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002078 const GRState* state = GetState(N);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002079 SVal V = state->getSVal(Ex);
Ted Kremenekac7c7242009-07-21 21:03:30 +00002080 const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002081 state = Res.getState()->BindExpr(CastE, Res.getSVal());
Ted Kremenekac7c7242009-07-21 21:03:30 +00002082 MakeNode(Dst, CastE, N, state);
Ted Kremenek33d82852008-01-24 02:02:54 +00002083 }
Ted Kremenek05352742008-01-24 20:55:43 +00002084}
2085
Ted Kremenekbf263682008-10-27 21:54:31 +00002086void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
Mike Stump11289f42009-09-09 15:08:12 +00002087 ExplodedNode* Pred,
2088 ExplodedNodeSet& Dst,
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002089 bool asLValue) {
Ted Kremenekbf263682008-10-27 21:54:31 +00002090 InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002091 ExplodedNodeSet Tmp;
Ted Kremenekbf263682008-10-27 21:54:31 +00002092 Visit(ILE, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002093
Zhongxing Xu107f7592009-08-06 12:48:26 +00002094 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002095 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002096 SVal ILV = state->getSVal(ILE);
Ted Kremenek04af9f22009-12-07 22:05:27 +00002097 const LocationContext *LC = (*I)->getLocationContext();
2098 state = state->bindCompoundLiteral(CL, LC, ILV);
Ted Kremenekbf263682008-10-27 21:54:31 +00002099
Ted Kremenek04af9f22009-12-07 22:05:27 +00002100 if (asLValue) {
2101 MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL, LC)));
2102 }
Zhongxing Xu2c677c32008-11-07 10:38:33 +00002103 else
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002104 MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
Ted Kremenekbf263682008-10-27 21:54:31 +00002105 }
2106}
2107
Ted Kremenek14536f62009-08-21 22:28:32 +00002108void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +00002109 ExplodedNodeSet& Dst) {
Ted Kremenek3b427152008-04-22 22:25:27 +00002110
Mike Stump11289f42009-09-09 15:08:12 +00002111 // The CFG has one DeclStmt per Decl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002112 Decl* D = *DS->decl_begin();
Mike Stump11289f42009-09-09 15:08:12 +00002113
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002114 if (!D || !isa<VarDecl>(D))
Ted Kremenek3b427152008-04-22 22:25:27 +00002115 return;
Mike Stump11289f42009-09-09 15:08:12 +00002116
2117 const VarDecl* VD = dyn_cast<VarDecl>(D);
Ted Kremenek17810802008-11-12 19:24:17 +00002118 Expr* InitEx = const_cast<Expr*>(VD->getInit());
Ted Kremenek3b427152008-04-22 22:25:27 +00002119
2120 // FIXME: static variables may have an initializer, but the second
2121 // time a function is called those values may not be current.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002122 ExplodedNodeSet Tmp;
Ted Kremenek3b427152008-04-22 22:25:27 +00002123
Ted Kremenek17810802008-11-12 19:24:17 +00002124 if (InitEx)
2125 Visit(InitEx, Pred, Tmp);
Ted Kremenekfc311292009-07-17 23:48:26 +00002126 else
Ted Kremenekb45e6b92008-08-28 18:34:26 +00002127 Tmp.Add(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002128
Ted Kremenekae3361d2009-11-07 03:56:57 +00002129 ExplodedNodeSet Tmp2;
2130 CheckerVisit(DS, Tmp2, Tmp, true);
2131
2132 for (ExplodedNodeSet::iterator I=Tmp2.begin(), E=Tmp2.end(); I!=E; ++I) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002133 ExplodedNode *N = *I;
Ted Kremenekae3361d2009-11-07 03:56:57 +00002134 const GRState *state = GetState(N);
Zhongxing Xu27fee832009-11-03 12:13:38 +00002135
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00002136 // Decls without InitExpr are not initialized explicitly.
Zhongxing Xu27fee832009-11-03 12:13:38 +00002137 const LocationContext *LC = N->getLocationContext();
Ted Kremenek14536f62009-08-21 22:28:32 +00002138
Ted Kremenek17810802008-11-12 19:24:17 +00002139 if (InitEx) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002140 SVal InitVal = state->getSVal(InitEx);
Ted Kremenek17810802008-11-12 19:24:17 +00002141 QualType T = VD->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002142
Ted Kremenek17810802008-11-12 19:24:17 +00002143 // Recover some path-sensitivity if a scalar value evaluated to
2144 // UnknownVal.
Mike Stump11289f42009-09-09 15:08:12 +00002145 if (InitVal.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002146 !getConstraintManager().canReasonAbout(InitVal)) {
Zhongxing Xu27fee832009-11-03 12:13:38 +00002147 InitVal = ValMgr.getConjuredSymbolVal(NULL, InitEx,
2148 Builder->getCurrentBlockCount());
Mike Stump11289f42009-09-09 15:08:12 +00002149 }
Ted Kremenekb006b822009-11-04 00:09:15 +00002150
Ted Kremenek209e31b2009-11-05 00:42:23 +00002151 EvalBind(Dst, DS, DS, *I, state,
2152 loc::MemRegionVal(state->getRegion(VD, LC)), InitVal, true);
Mike Stump11289f42009-09-09 15:08:12 +00002153 }
Ted Kremenek13363532009-02-14 01:54:57 +00002154 else {
Ted Kremenekb006b822009-11-04 00:09:15 +00002155 state = state->bindDeclWithNoInit(state->getRegion(VD, LC));
Ted Kremenek13363532009-02-14 01:54:57 +00002156 MakeNode(Dst, DS, *I, state);
Ted Kremenek8f7afdd2008-12-08 22:47:34 +00002157 }
Ted Kremenek3b427152008-04-22 22:25:27 +00002158 }
Ted Kremenek05352742008-01-24 20:55:43 +00002159}
Ted Kremenek33d82852008-01-24 02:02:54 +00002160
Ted Kremenekf68bf632008-10-30 17:47:32 +00002161namespace {
2162 // This class is used by VisitInitListExpr as an item in a worklist
2163 // for processing the values contained in an InitListExpr.
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002164class InitListWLItem {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002165public:
2166 llvm::ImmutableList<SVal> Vals;
Zhongxing Xu107f7592009-08-06 12:48:26 +00002167 ExplodedNode* N;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002168 InitListExpr::reverse_iterator Itr;
Mike Stump11289f42009-09-09 15:08:12 +00002169
Zhongxing Xu107f7592009-08-06 12:48:26 +00002170 InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
2171 InitListExpr::reverse_iterator itr)
Ted Kremenekf68bf632008-10-30 17:47:32 +00002172 : Vals(vals), N(n), Itr(itr) {}
2173};
2174}
2175
2176
Mike Stump11289f42009-09-09 15:08:12 +00002177void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002178 ExplodedNodeSet& Dst) {
Ted Kremenek828e6df2008-10-30 23:14:36 +00002179
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002180 const GRState* state = GetState(Pred);
Ted Kremenek45698bf2008-11-13 05:05:34 +00002181 QualType T = getContext().getCanonicalType(E->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002182 unsigned NumInitElements = E->getNumInits();
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002183
Ted Kremeneka41d9dd2009-07-28 20:46:55 +00002184 if (T->isArrayType() || T->isStructureType() ||
2185 T->isUnionType() || T->isVectorType()) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002186
Ted Kremenek828e6df2008-10-30 23:14:36 +00002187 llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
Mike Stump11289f42009-09-09 15:08:12 +00002188
Ted Kremenek828e6df2008-10-30 23:14:36 +00002189 // Handle base case where the initializer has no elements.
2190 // e.g: static int* myArray[] = {};
2191 if (NumInitElements == 0) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002192 SVal V = ValMgr.makeCompoundVal(T, StartVals);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002193 MakeNode(Dst, E, Pred, state->BindExpr(E, V));
Ted Kremenek828e6df2008-10-30 23:14:36 +00002194 return;
Mike Stump11289f42009-09-09 15:08:12 +00002195 }
2196
Ted Kremenek828e6df2008-10-30 23:14:36 +00002197 // Create a worklist to process the initializers.
2198 llvm::SmallVector<InitListWLItem, 10> WorkList;
Mike Stump11289f42009-09-09 15:08:12 +00002199 WorkList.reserve(NumInitElements);
2200 WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002201 InitListExpr::reverse_iterator ItrEnd = E->rend();
Ted Kremenek30030012009-09-22 21:19:14 +00002202 assert(!(E->rbegin() == E->rend()));
Mike Stump11289f42009-09-09 15:08:12 +00002203
Ted Kremenek828e6df2008-10-30 23:14:36 +00002204 // Process the worklist until it is empty.
Ted Kremenekf68bf632008-10-30 17:47:32 +00002205 while (!WorkList.empty()) {
2206 InitListWLItem X = WorkList.back();
2207 WorkList.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +00002208
Zhongxing Xu107f7592009-08-06 12:48:26 +00002209 ExplodedNodeSet Tmp;
Ted Kremenekf68bf632008-10-30 17:47:32 +00002210 Visit(*X.Itr, X.N, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002211
Ted Kremenekf68bf632008-10-30 17:47:32 +00002212 InitListExpr::reverse_iterator NewItr = X.Itr + 1;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002213
Zhongxing Xu107f7592009-08-06 12:48:26 +00002214 for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
Ted Kremenekf68bf632008-10-30 17:47:32 +00002215 // Get the last initializer value.
2216 state = GetState(*NI);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002217 SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
Mike Stump11289f42009-09-09 15:08:12 +00002218
Ted Kremenekf68bf632008-10-30 17:47:32 +00002219 // Construct the new list of values by prepending the new value to
2220 // the already constructed list.
2221 llvm::ImmutableList<SVal> NewVals =
2222 getBasicVals().consVals(InitV, X.Vals);
Mike Stump11289f42009-09-09 15:08:12 +00002223
Ted Kremenekf68bf632008-10-30 17:47:32 +00002224 if (NewItr == ItrEnd) {
Zhongxing Xu121a53a2008-10-31 03:01:26 +00002225 // Now we have a list holding all init values. Make CompoundValData.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002226 SVal V = ValMgr.makeCompoundVal(T, NewVals);
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002227
Ted Kremenekf68bf632008-10-30 17:47:32 +00002228 // Make final state and node.
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002229 MakeNode(Dst, E, *NI, state->BindExpr(E, V));
Ted Kremenekf68bf632008-10-30 17:47:32 +00002230 }
2231 else {
2232 // Still some initializer values to go. Push them onto the worklist.
2233 WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
2234 }
2235 }
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002236 }
Mike Stump11289f42009-09-09 15:08:12 +00002237
Ted Kremenek28f41ba2008-10-30 18:34:31 +00002238 return;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002239 }
2240
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002241 if (Loc::IsLocType(T) || T->isIntegerType()) {
2242 assert (E->getNumInits() == 1);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002243 ExplodedNodeSet Tmp;
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002244 Expr* Init = E->getInit(0);
2245 Visit(Init, Pred, Tmp);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002246 for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002247 state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002248 MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002249 }
2250 return;
2251 }
2252
Zhongxing Xub281cdd2008-10-30 05:02:23 +00002253 assert(0 && "unprocessed InitListExpr type");
2254}
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +00002255
Sebastian Redl6f282892008-11-11 17:56:53 +00002256/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
2257void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002258 ExplodedNode* Pred,
2259 ExplodedNodeSet& Dst) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002260 QualType T = Ex->getTypeOfArgument();
Mike Stump11289f42009-09-09 15:08:12 +00002261 uint64_t amt;
2262
Ted Kremenekae5b7862008-03-15 03:13:20 +00002263 if (Ex->isSizeOf()) {
Mike Stump11289f42009-09-09 15:08:12 +00002264 if (T == getContext().VoidTy) {
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002265 // sizeof(void) == 1 byte.
2266 amt = 1;
2267 }
2268 else if (!T.getTypePtr()->isConstantSizeType()) {
2269 // FIXME: Add support for VLAs.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002270 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002271 }
2272 else if (T->isObjCInterfaceType()) {
2273 // Some code tries to take the sizeof an ObjCInterfaceType, relying that
2274 // the compiler has laid out its representation. Just report Unknown
Mike Stump11289f42009-09-09 15:08:12 +00002275 // for these.
Ted Kremenek99057462008-04-30 21:31:12 +00002276 return;
Ted Kremenek4299d5d2008-12-15 18:51:00 +00002277 }
2278 else {
2279 // All other cases.
Ted Kremenekae5b7862008-03-15 03:13:20 +00002280 amt = getContext().getTypeSize(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002281 }
Ted Kremenekae5b7862008-03-15 03:13:20 +00002282 }
2283 else // Get alignment of the type.
Ted Kremenek88ba7502008-03-15 03:13:55 +00002284 amt = getContext().getTypeAlign(T) / 8;
Mike Stump11289f42009-09-09 15:08:12 +00002285
Ted Kremenek181f7232008-03-21 21:30:14 +00002286 MakeNode(Dst, Ex, Pred,
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002287 GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
Ted Kremenek002bf742008-02-12 19:49:57 +00002288}
2289
Ted Kremenekb597bb92008-02-20 04:02:35 +00002290
Zhongxing Xu107f7592009-08-06 12:48:26 +00002291void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
2292 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002293
Ted Kremenekb597bb92008-02-20 04:02:35 +00002294 switch (U->getOpcode()) {
Mike Stump11289f42009-09-09 15:08:12 +00002295
Ted Kremenekb597bb92008-02-20 04:02:35 +00002296 default:
Ted Kremenekb597bb92008-02-20 04:02:35 +00002297 break;
Mike Stump11289f42009-09-09 15:08:12 +00002298
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002299 case UnaryOperator::Deref: {
Mike Stump11289f42009-09-09 15:08:12 +00002300
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002301 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002302 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002303 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002304
Zhongxing Xu107f7592009-08-06 12:48:26 +00002305 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002306
Ted Kremenek17d541d2009-02-13 01:45:31 +00002307 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002308 SVal location = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002309
Zhongxing Xu232c7922008-10-16 06:09:51 +00002310 if (asLValue)
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002311 MakeNode(Dst, U, *I, state->BindExpr(U, location),
Ted Kremeneka6e08322009-05-07 18:27:16 +00002312 ProgramPoint::PostLValueKind);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002313 else
Ted Kremenek17d541d2009-02-13 01:45:31 +00002314 EvalLoad(Dst, U, *I, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002315 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002316
2317 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002318 }
Mike Stump11289f42009-09-09 15:08:12 +00002319
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002320 case UnaryOperator::Real: {
Mike Stump11289f42009-09-09 15:08:12 +00002321
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002322 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002323 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002324 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002325
Zhongxing Xu107f7592009-08-06 12:48:26 +00002326 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002327
Zhongxing Xu27f17422008-10-17 05:57:07 +00002328 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002329 if (Ex->getType()->isAnyComplexType()) {
2330 // Just report "Unknown."
2331 Dst.Add(*I);
2332 continue;
2333 }
Mike Stump11289f42009-09-09 15:08:12 +00002334
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002335 // For all other types, UnaryOperator::Real is an identity operation.
2336 assert (U->getType() == Ex->getType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002337 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002338 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Mike Stump11289f42009-09-09 15:08:12 +00002339 }
2340
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002341 return;
2342 }
Mike Stump11289f42009-09-09 15:08:12 +00002343
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002344 case UnaryOperator::Imag: {
Mike Stump11289f42009-09-09 15:08:12 +00002345
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002346 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002347 ExplodedNodeSet Tmp;
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002348 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002349
Zhongxing Xu107f7592009-08-06 12:48:26 +00002350 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Zhongxing Xu27f17422008-10-17 05:57:07 +00002351 // FIXME: We don't have complex SValues yet.
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002352 if (Ex->getType()->isAnyComplexType()) {
2353 // Just report "Unknown."
2354 Dst.Add(*I);
2355 continue;
2356 }
Mike Stump11289f42009-09-09 15:08:12 +00002357
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002358 // For all other types, UnaryOperator::Float returns 0.
2359 assert (Ex->getType()->isIntegerType());
Ted Kremenek17d541d2009-02-13 01:45:31 +00002360 const GRState* state = GetState(*I);
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002361 SVal X = ValMgr.makeZeroVal(Ex->getType());
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002362 MakeNode(Dst, U, *I, state->BindExpr(U, X));
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002363 }
Mike Stump11289f42009-09-09 15:08:12 +00002364
Ted Kremenek46c82ab2008-06-19 17:55:38 +00002365 return;
2366 }
Mike Stump11289f42009-09-09 15:08:12 +00002367
Ted Kremenek27347132009-09-15 00:40:32 +00002368 case UnaryOperator::OffsetOf: {
Ted Kremenekb6622942009-09-15 04:19:09 +00002369 Expr::EvalResult Res;
2370 if (U->Evaluate(Res, getContext()) && Res.Val.isInt()) {
2371 const APSInt &IV = Res.Val.getInt();
2372 assert(IV.getBitWidth() == getContext().getTypeSize(U->getType()));
2373 assert(U->getType()->isIntegerType());
2374 assert(IV.isSigned() == U->getType()->isSignedIntegerType());
2375 SVal X = ValMgr.makeIntVal(IV);
2376 MakeNode(Dst, U, Pred, GetState(Pred)->BindExpr(U, X));
2377 return;
2378 }
2379 // FIXME: Handle the case where __builtin_offsetof is not a constant.
2380 Dst.Add(Pred);
Ted Kremenekca67cab2008-04-30 21:45:55 +00002381 return;
Ted Kremenek27347132009-09-15 00:40:32 +00002382 }
Mike Stump11289f42009-09-09 15:08:12 +00002383
Zhongxing Xu232c7922008-10-16 06:09:51 +00002384 case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002385 case UnaryOperator::Extension: {
Mike Stump11289f42009-09-09 15:08:12 +00002386
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002387 // Unary "+" is a no-op, similar to a parentheses. We still have places
2388 // where it may be a block-level expression, so we need to
2389 // generate an extra node that just propagates the value of the
2390 // subexpression.
2391
2392 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002393 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002394 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002395
2396 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002397 const GRState* state = GetState(*I);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002398 MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002399 }
Mike Stump11289f42009-09-09 15:08:12 +00002400
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002401 return;
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002402 }
Mike Stump11289f42009-09-09 15:08:12 +00002403
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002404 case UnaryOperator::AddrOf: {
Mike Stump11289f42009-09-09 15:08:12 +00002405
Zhongxing Xu232c7922008-10-16 06:09:51 +00002406 assert(!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002407 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002408 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002409 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002410
2411 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002412 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002413 SVal V = state->getSVal(Ex);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002414 state = state->BindExpr(U, V);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002415 MakeNode(Dst, U, *I, state);
Ted Kremenek7f8ebb72008-02-21 19:15:37 +00002416 }
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002417
Mike Stump11289f42009-09-09 15:08:12 +00002418 return;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002419 }
Mike Stump11289f42009-09-09 15:08:12 +00002420
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002421 case UnaryOperator::LNot:
2422 case UnaryOperator::Minus:
2423 case UnaryOperator::Not: {
Mike Stump11289f42009-09-09 15:08:12 +00002424
Zhongxing Xu232c7922008-10-16 06:09:51 +00002425 assert (!asLValue);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002426 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu107f7592009-08-06 12:48:26 +00002427 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002428 Visit(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002429
2430 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002431 const GRState* state = GetState(*I);
Mike Stump11289f42009-09-09 15:08:12 +00002432
Ted Kremenek76bccf62008-09-30 05:32:44 +00002433 // Get the value of the subexpression.
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002434 SVal V = state->getSVal(Ex);
Ted Kremenek76bccf62008-09-30 05:32:44 +00002435
Ted Kremenek1ca33462008-11-15 00:20:05 +00002436 if (V.isUnknownOrUndef()) {
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002437 MakeNode(Dst, U, *I, state->BindExpr(U, V));
Ted Kremenek1ca33462008-11-15 00:20:05 +00002438 continue;
2439 }
Mike Stump11289f42009-09-09 15:08:12 +00002440
Ted Kremenek44137142008-11-15 04:01:56 +00002441// QualType DstT = getContext().getCanonicalType(U->getType());
2442// QualType SrcT = getContext().getCanonicalType(Ex->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002443//
Ted Kremenek44137142008-11-15 04:01:56 +00002444// if (DstT != SrcT) // Perform promotions.
Mike Stump11289f42009-09-09 15:08:12 +00002445// V = EvalCast(V, DstT);
2446//
Ted Kremenek44137142008-11-15 04:01:56 +00002447// if (V.isUnknownOrUndef()) {
2448// MakeNode(Dst, U, *I, BindExpr(St, U, V));
2449// continue;
2450// }
Mike Stump11289f42009-09-09 15:08:12 +00002451
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002452 switch (U->getOpcode()) {
2453 default:
2454 assert(false && "Invalid Opcode.");
2455 break;
Mike Stump11289f42009-09-09 15:08:12 +00002456
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002457 case UnaryOperator::Not:
Ted Kremenekd331d092008-10-01 00:21:14 +00002458 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002459 state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002460 break;
2461
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002462 case UnaryOperator::Minus:
Ted Kremenekd331d092008-10-01 00:21:14 +00002463 // FIXME: Do we need to handle promotions?
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002464 state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
Mike Stump11289f42009-09-09 15:08:12 +00002465 break;
2466
2467 case UnaryOperator::LNot:
2468
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002469 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
2470 //
2471 // Note: technically we do "E == 0", but this is the same in the
2472 // transfer functions as "0 == E".
Ted Kremenek1642bda2009-06-26 00:05:51 +00002473 SVal Result;
Mike Stump11289f42009-09-09 15:08:12 +00002474
Zhongxing Xu27f17422008-10-17 05:57:07 +00002475 if (isa<Loc>(V)) {
Zhongxing Xu7718ae42009-06-23 09:02:15 +00002476 Loc X = ValMgr.makeNull();
Ted Kremenek1642bda2009-06-26 00:05:51 +00002477 Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
2478 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002479 }
2480 else {
Ted Kremenek44137142008-11-15 04:01:56 +00002481 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
Ted Kremenek8ec57712009-10-06 01:39:48 +00002482 Result = EvalBinOp(state, BinaryOperator::EQ, cast<NonLoc>(V), X,
Ted Kremenek1642bda2009-06-26 00:05:51 +00002483 U->getType());
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002484 }
Mike Stump11289f42009-09-09 15:08:12 +00002485
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002486 state = state->BindExpr(U, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002487
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002488 break;
2489 }
Mike Stump11289f42009-09-09 15:08:12 +00002490
Ted Kremenek17d541d2009-02-13 01:45:31 +00002491 MakeNode(Dst, U, *I, state);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002492 }
Mike Stump11289f42009-09-09 15:08:12 +00002493
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002494 return;
2495 }
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002496 }
2497
2498 // Handle ++ and -- (both pre- and post-increment).
2499
2500 assert (U->isIncrementDecrementOp());
Zhongxing Xu107f7592009-08-06 12:48:26 +00002501 ExplodedNodeSet Tmp;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002502 Expr* Ex = U->getSubExpr()->IgnoreParens();
Zhongxing Xu232c7922008-10-16 06:09:51 +00002503 VisitLValue(Ex, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002504
Zhongxing Xu107f7592009-08-06 12:48:26 +00002505 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002506
Ted Kremenek17d541d2009-02-13 01:45:31 +00002507 const GRState* state = GetState(*I);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002508 SVal V1 = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002509
2510 // Perform a load.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002511 ExplodedNodeSet Tmp2;
Ted Kremenek17d541d2009-02-13 01:45:31 +00002512 EvalLoad(Tmp2, Ex, *I, state, V1);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002513
Zhongxing Xu107f7592009-08-06 12:48:26 +00002514 for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
Mike Stump11289f42009-09-09 15:08:12 +00002515
Ted Kremenek17d541d2009-02-13 01:45:31 +00002516 state = GetState(*I2);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002517 SVal V2_untested = state->getSVal(Ex);
Mike Stump11289f42009-09-09 15:08:12 +00002518
2519 // Propagate unknown and undefined values.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002520 if (V2_untested.isUnknownOrUndef()) {
2521 MakeNode(Dst, U, *I2, state->BindExpr(U, V2_untested));
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002522 continue;
Ted Kremenek7020eae2009-09-11 22:07:28 +00002523 }
2524 DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
Mike Stump11289f42009-09-09 15:08:12 +00002525
2526 // Handle all other values.
Ted Kremeneke81734b2008-02-15 22:09:30 +00002527 BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
2528 : BinaryOperator::Sub;
Ted Kremenek32c41ec2009-03-11 03:54:24 +00002529
Zhongxing Xufe971652009-08-05 02:51:59 +00002530 // If the UnaryOperator has non-location type, use its type to create the
2531 // constant value. If the UnaryOperator has location type, create the
2532 // constant with int type and pointer width.
2533 SVal RHS;
2534
2535 if (U->getType()->isAnyPointerType())
2536 RHS = ValMgr.makeIntValWithPtrWidth(1, false);
2537 else
2538 RHS = ValMgr.makeIntVal(1, U->getType());
2539
Mike Stump11289f42009-09-09 15:08:12 +00002540 SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
2541
Ted Kremenek6b315332009-03-20 20:10:45 +00002542 // Conjure a new symbol if necessary to recover precision.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002543 if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
Ted Kremenek7020eae2009-09-11 22:07:28 +00002544 DefinedOrUnknownSVal SymVal =
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002545 ValMgr.getConjuredSymbolVal(NULL, Ex,
2546 Builder->getCurrentBlockCount());
Ted Kremenek7020eae2009-09-11 22:07:28 +00002547 Result = SymVal;
Mike Stump11289f42009-09-09 15:08:12 +00002548
Ted Kremenek35f875c2009-04-21 22:38:05 +00002549 // If the value is a location, ++/-- should always preserve
Ted Kremenek1642bda2009-06-26 00:05:51 +00002550 // non-nullness. Check if the original value was non-null, and if so
Mike Stump11289f42009-09-09 15:08:12 +00002551 // propagate that constraint.
Ted Kremenek35f875c2009-04-21 22:38:05 +00002552 if (Loc::IsLocType(U->getType())) {
Ted Kremenek7020eae2009-09-11 22:07:28 +00002553 DefinedOrUnknownSVal Constraint =
2554 SVator.EvalEQ(state, V2, ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002555
Ted Kremenek7020eae2009-09-11 22:07:28 +00002556 if (!state->Assume(Constraint, true)) {
Ted Kremenek35f875c2009-04-21 22:38:05 +00002557 // It isn't feasible for the original value to be null.
2558 // Propagate this constraint.
Ted Kremenek7020eae2009-09-11 22:07:28 +00002559 Constraint = SVator.EvalEQ(state, SymVal,
2560 ValMgr.makeZeroVal(U->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00002561
Ted Kremenek7020eae2009-09-11 22:07:28 +00002562
2563 state = state->Assume(Constraint, false);
Ted Kremenekf9906842009-06-18 22:57:13 +00002564 assert(state);
Mike Stump11289f42009-09-09 15:08:12 +00002565 }
2566 }
Ted Kremenek35f875c2009-04-21 22:38:05 +00002567 }
Mike Stump11289f42009-09-09 15:08:12 +00002568
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002569 state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002570
Mike Stump11289f42009-09-09 15:08:12 +00002571 // Perform the store.
Ted Kremenek209e31b2009-11-05 00:42:23 +00002572 EvalStore(Dst, NULL, U, *I2, state, V1, Result);
Ted Kremenek43523e02008-02-07 01:08:27 +00002573 }
Ted Kremenek38213f92008-04-21 23:43:38 +00002574 }
Ted Kremenek43523e02008-02-07 01:08:27 +00002575}
2576
Zhongxing Xu107f7592009-08-06 12:48:26 +00002577void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002578 VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
Mike Stump11289f42009-09-09 15:08:12 +00002579}
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002580
2581void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
2582 AsmStmt::outputs_iterator I,
2583 AsmStmt::outputs_iterator E,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002584 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002585 if (I == E) {
2586 VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
2587 return;
2588 }
Mike Stump11289f42009-09-09 15:08:12 +00002589
Zhongxing Xu107f7592009-08-06 12:48:26 +00002590 ExplodedNodeSet Tmp;
Zhongxing Xu232c7922008-10-16 06:09:51 +00002591 VisitLValue(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002592
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002593 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002594
Zhongxing Xu107f7592009-08-06 12:48:26 +00002595 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002596 VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
2597}
2598
2599void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
2600 AsmStmt::inputs_iterator I,
2601 AsmStmt::inputs_iterator E,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002602 ExplodedNode* Pred, ExplodedNodeSet& Dst) {
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002603 if (I == E) {
Mike Stump11289f42009-09-09 15:08:12 +00002604
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002605 // We have processed both the inputs and the outputs. All of the outputs
Zhongxing Xu27f17422008-10-17 05:57:07 +00002606 // should evaluate to Locs. Nuke all of their values.
Mike Stump11289f42009-09-09 15:08:12 +00002607
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002608 // FIXME: Some day in the future it would be nice to allow a "plug-in"
2609 // which interprets the inline asm and stores proper results in the
2610 // outputs.
Mike Stump11289f42009-09-09 15:08:12 +00002611
Ted Kremenek17d541d2009-02-13 01:45:31 +00002612 const GRState* state = GetState(Pred);
Mike Stump11289f42009-09-09 15:08:12 +00002613
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002614 for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
2615 OE = A->end_outputs(); OI != OE; ++OI) {
Mike Stump11289f42009-09-09 15:08:12 +00002616
2617 SVal X = state->getSVal(*OI);
Zhongxing Xu27f17422008-10-17 05:57:07 +00002618 assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
Mike Stump11289f42009-09-09 15:08:12 +00002619
Zhongxing Xu27f17422008-10-17 05:57:07 +00002620 if (isa<Loc>(X))
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002621 state = state->bindLoc(cast<Loc>(X), UnknownVal());
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002622 }
Mike Stump11289f42009-09-09 15:08:12 +00002623
Ted Kremenek17d541d2009-02-13 01:45:31 +00002624 MakeNode(Dst, A, Pred, state);
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002625 return;
2626 }
Mike Stump11289f42009-09-09 15:08:12 +00002627
Zhongxing Xu107f7592009-08-06 12:48:26 +00002628 ExplodedNodeSet Tmp;
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002629 Visit(*I, Pred, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +00002630
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002631 ++I;
Mike Stump11289f42009-09-09 15:08:12 +00002632
Ted Kremenek17a02962009-09-03 03:02:58 +00002633 for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
Ted Kremenek7c7a3312008-03-17 21:11:24 +00002634 VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
2635}
2636
Ted Kremenekbee01e52009-11-06 02:24:13 +00002637void GRExprEngine::VisitReturnStmt(ReturnStmt *RS, ExplodedNode *Pred,
2638 ExplodedNodeSet &Dst) {
2639
2640 ExplodedNodeSet Src;
2641 if (Expr *RetE = RS->getRetValue()) {
2642 Visit(RetE, Pred, Src);
Ted Kremenekf6467742008-03-31 15:02:58 +00002643 }
Ted Kremenekbee01e52009-11-06 02:24:13 +00002644 else {
2645 Src.Add(Pred);
2646 }
2647
2648 ExplodedNodeSet CheckedSet;
2649 CheckerVisit(RS, CheckedSet, Src, true);
2650
2651 for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
2652 I != E; ++I) {
Ted Kremenek9c375152008-04-16 23:05:51 +00002653
Ted Kremenekbee01e52009-11-06 02:24:13 +00002654 assert(Builder && "GRStmtNodeBuilder must be defined.");
2655
2656 Pred = *I;
2657 unsigned size = Dst.size();
2658
2659 SaveAndRestore<bool> OldSink(Builder->BuildSinks);
2660 SaveOr OldHasGen(Builder->HasGeneratedNode);
2661
2662 getTF().EvalReturn(Dst, *this, *Builder, RS, Pred);
2663
2664 // Handle the case where no nodes where generated.
2665 if (!Builder->BuildSinks && Dst.size() == size &&
2666 !Builder->HasGeneratedNode)
2667 MakeNode(Dst, RS, Pred, GetState(Pred));
Ted Kremenek0b63f962008-11-21 00:27:44 +00002668 }
Ted Kremenekf6467742008-03-31 15:02:58 +00002669}
Ted Kremenek64100da2008-03-25 00:34:37 +00002670
Ted Kremenek667cacb2008-04-15 23:06:53 +00002671//===----------------------------------------------------------------------===//
2672// Transfer functions: Binary operators.
2673//===----------------------------------------------------------------------===//
2674
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002675void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002676 ExplodedNode* Pred,
Zhongxing Xub9eda672009-10-30 07:19:39 +00002677 ExplodedNodeSet& Dst, bool asLValue) {
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002678
Zhongxing Xu107f7592009-08-06 12:48:26 +00002679 ExplodedNodeSet Tmp1;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002680 Expr* LHS = B->getLHS()->IgnoreParens();
2681 Expr* RHS = B->getRHS()->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002682
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002683 // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
2684 if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
Mike Stump11289f42009-09-09 15:08:12 +00002685 Visit(RHS, Pred, Dst);
Ted Kremenek69d78b92008-12-06 02:39:30 +00002686 return;
2687 }
Mike Stump11289f42009-09-09 15:08:12 +00002688
Ted Kremenek43523e02008-02-07 01:08:27 +00002689 if (B->isAssignmentOp())
Zhongxing Xu232c7922008-10-16 06:09:51 +00002690 VisitLValue(LHS, Pred, Tmp1);
Ted Kremenek43523e02008-02-07 01:08:27 +00002691 else
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002692 Visit(LHS, Pred, Tmp1);
Ted Kremenekfb553542008-01-16 00:53:15 +00002693
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002694 ExplodedNodeSet Tmp3;
2695
Ted Kremenek17a02962009-09-03 03:02:58 +00002696 for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002697 SVal LeftV = (*I1)->getState()->getSVal(LHS);
Zhongxing Xu107f7592009-08-06 12:48:26 +00002698 ExplodedNodeSet Tmp2;
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002699 Visit(RHS, *I1, Tmp2);
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002700
2701 ExplodedNodeSet CheckedSet;
2702 CheckerVisit(B, CheckedSet, Tmp2, true);
Mike Stump11289f42009-09-09 15:08:12 +00002703
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002704 // With both the LHS and RHS evaluated, process the operation itself.
Mike Stump11289f42009-09-09 15:08:12 +00002705
2706 for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002707 I2 != E2; ++I2) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +00002708
Ted Kremenek7020eae2009-09-11 22:07:28 +00002709 const GRState *state = GetState(*I2);
2710 const GRState *OldSt = state;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002711 SVal RightV = state->getSVal(RHS);
Ted Kremenek7020eae2009-09-11 22:07:28 +00002712
Ted Kremenekcdd0be12008-02-06 22:50:25 +00002713 BinaryOperator::Opcode Op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00002714
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002715 if (Op == BinaryOperator::Assign) {
2716 // EXPERIMENTAL: "Conjured" symbols.
2717 // FIXME: Handle structs.
2718 QualType T = RHS->getType();
2719
2720 if ((RightV.isUnknown()||!getConstraintManager().canReasonAbout(RightV))
2721 && (Loc::IsLocType(T) || (T->isScalarType()&&T->isIntegerType()))) {
2722 unsigned Count = Builder->getCurrentBlockCount();
2723 RightV = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), Count);
2724 }
Zhongxing Xub9eda672009-10-30 07:19:39 +00002725
Ted Kremenek5c2040b12009-10-30 22:01:29 +00002726 SVal ExprVal = asLValue ? LeftV : RightV;
Zhongxing Xub9eda672009-10-30 07:19:39 +00002727
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002728 // Simulate the effects of a "store": bind the value of the RHS
2729 // to the L-Value represented by the LHS.
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002730 EvalStore(Tmp3, B, LHS, *I2, state->BindExpr(B, ExprVal), LeftV, RightV);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002731 continue;
2732 }
2733
2734 if (!B->isAssignmentOp()) {
2735 // Process non-assignments except commas or short-circuited
2736 // logical expressions (LAnd and LOr).
2737 SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
2738
2739 if (Result.isUnknown()) {
2740 if (OldSt != state) {
2741 // Generate a new node if we have already created a new state.
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002742 MakeNode(Tmp3, B, *I2, state);
Ted Kremeneke2f6d6c2008-03-12 21:45:47 +00002743 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002744 else
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002745 Tmp3.Add(*I2);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002746
Ted Kremenek2044a512008-04-16 18:21:25 +00002747 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002748 }
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002749
2750 state = state->BindExpr(B, Result);
2751
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002752 MakeNode(Tmp3, B, *I2, state);
Zhongxing Xu2ebee132009-10-21 11:42:22 +00002753 continue;
Ted Kremenek0a8d3762008-01-23 19:59:44 +00002754 }
Mike Stump11289f42009-09-09 15:08:12 +00002755
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002756 assert (B->isCompoundAssignmentOp());
2757
Ted Kremenek5d7662c2009-02-07 00:52:24 +00002758 switch (Op) {
2759 default:
2760 assert(0 && "Invalid opcode for compound assignment.");
2761 case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
2762 case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
2763 case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
2764 case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
2765 case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
2766 case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
2767 case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
2768 case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
2769 case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
2770 case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
Ted Kremenek54d399a2008-10-27 23:02:39 +00002771 }
Mike Stump11289f42009-09-09 15:08:12 +00002772
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002773 // Perform a load (the LHS). This performs the checks for
2774 // null dereferences, and so on.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002775 ExplodedNodeSet Tmp3;
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002776 SVal location = state->getSVal(LHS);
Ted Kremenek17d541d2009-02-13 01:45:31 +00002777 EvalLoad(Tmp3, LHS, *I2, state, location);
Mike Stump11289f42009-09-09 15:08:12 +00002778
2779 for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
Zhongxing Xu6e4232c2009-09-02 13:26:26 +00002780 ++I3) {
Ted Kremenek17d541d2009-02-13 01:45:31 +00002781 state = GetState(*I3);
Ted Kremenekc55f0cd2009-06-19 17:10:32 +00002782 SVal V = state->getSVal(LHS);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002783
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002784 // Get the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002785 QualType CTy =
2786 cast<CompoundAssignOperator>(B)->getComputationResultType();
Ted Kremenek44137142008-11-15 04:01:56 +00002787 CTy = getContext().getCanonicalType(CTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002788
Ted Kremenekac7c7242009-07-21 21:03:30 +00002789 QualType CLHSTy =
2790 cast<CompoundAssignOperator>(B)->getComputationLHSType();
2791 CLHSTy = getContext().getCanonicalType(CLHSTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002792
Ted Kremenek44137142008-11-15 04:01:56 +00002793 QualType LTy = getContext().getCanonicalType(LHS->getType());
2794 QualType RTy = getContext().getCanonicalType(RHS->getType());
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002795
2796 // Promote LHS.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002797 llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
Eli Friedman8b7b1b12009-03-28 01:22:36 +00002798
Mike Stump11289f42009-09-09 15:08:12 +00002799 // Compute the result of the operation.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002800 SVal Result;
2801 llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
2802 RightV, CTy),
2803 state, B->getType(), CTy);
Mike Stump11289f42009-09-09 15:08:12 +00002804
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002805 // EXPERIMENTAL: "Conjured" symbols.
2806 // FIXME: Handle structs.
Mike Stump11289f42009-09-09 15:08:12 +00002807
Ted Kremenek44137142008-11-15 04:01:56 +00002808 SVal LHSVal;
Mike Stump11289f42009-09-09 15:08:12 +00002809
2810 if ((Result.isUnknown() ||
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002811 !getConstraintManager().canReasonAbout(Result))
Mike Stump11289f42009-09-09 15:08:12 +00002812 && (Loc::IsLocType(CTy)
Ted Kremenek44c12ef2009-03-11 02:24:48 +00002813 || (CTy->isScalarType() && CTy->isIntegerType()))) {
Mike Stump11289f42009-09-09 15:08:12 +00002814
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002815 unsigned Count = Builder->getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002816
Ted Kremenek44137142008-11-15 04:01:56 +00002817 // The symbolic value is actually for the type of the left-hand side
2818 // expression, not the computation type, as this is the value the
2819 // LValue on the LHS will bind to.
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002820 LHSVal = ValMgr.getConjuredSymbolVal(NULL, B->getRHS(), LTy, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002821
Zhongxing Xuaa86cff2008-11-23 05:52:28 +00002822 // However, we need to convert the symbol to the computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002823 llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
Ted Kremenek7f8a87f2008-10-20 23:13:25 +00002824 }
Ted Kremenek44137142008-11-15 04:01:56 +00002825 else {
2826 // The left-hand side may bind to a different value then the
2827 // computation type.
Ted Kremenekac7c7242009-07-21 21:03:30 +00002828 llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
Ted Kremenek44137142008-11-15 04:01:56 +00002829 }
Mike Stump11289f42009-09-09 15:08:12 +00002830
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002831 EvalStore(Tmp3, B, LHS, *I3, state->BindExpr(B, Result),
Zhongxing Xu342950e2009-08-25 06:51:30 +00002832 location, LHSVal);
Ted Kremenekfa5a3d02008-04-29 21:04:26 +00002833 }
Ted Kremenekfb553542008-01-16 00:53:15 +00002834 }
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002835 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +00002836
2837 CheckerVisit(B, Dst, Tmp3, false);
Ted Kremenekde8d62b2008-01-15 23:55:06 +00002838}
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002839
2840//===----------------------------------------------------------------------===//
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002841// Checker registration/lookup.
2842//===----------------------------------------------------------------------===//
2843
2844Checker *GRExprEngine::lookupChecker(void *tag) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +00002845 CheckerMap::const_iterator I = CheckerM.find(tag);
Ted Kremenek6f2a7052009-10-30 17:47:32 +00002846 return (I == CheckerM.end()) ? NULL : Checkers[I->second].second;
2847}
2848
2849//===----------------------------------------------------------------------===//
Ted Kremenekd3122cb2008-02-14 22:36:46 +00002850// Visualization.
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00002851//===----------------------------------------------------------------------===//
2852
Ted Kremenekac886cb2008-01-16 21:46:15 +00002853#ifndef NDEBUG
Ted Kremenekf6c62f32008-02-13 17:41:41 +00002854static GRExprEngine* GraphPrintCheckerState;
Ted Kremenek6947a792008-03-07 20:57:30 +00002855static SourceManager* GraphPrintSourceManager;
Ted Kremenek2531fce2008-01-30 23:24:39 +00002856
Ted Kremenekac886cb2008-01-16 21:46:15 +00002857namespace llvm {
2858template<>
Douglas Gregor693ba202009-11-30 16:08:24 +00002859struct DOTGraphTraits<ExplodedNode*> :
Ted Kremenekac886cb2008-01-16 21:46:15 +00002860 public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002861
2862 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2863
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +00002864 // FIXME: Since we do not cache error nodes in GRExprEngine now, this does not
2865 // work.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002866 static std::string getNodeAttributes(const ExplodedNode* N, void*) {
Mike Stump11289f42009-09-09 15:08:12 +00002867
Ted Kremenek7cf82382009-11-11 20:16:36 +00002868#if 0
2869 // FIXME: Replace with a general scheme to tell if the node is
2870 // an error node.
Ted Kremenek5b70a222008-02-14 22:54:53 +00002871 if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
Ted Kremenek0f7130a2008-02-19 00:22:37 +00002872 GraphPrintCheckerState->isExplicitNullDeref(N) ||
Ted Kremenek93d1fed2008-02-28 09:25:22 +00002873 GraphPrintCheckerState->isUndefDeref(N) ||
2874 GraphPrintCheckerState->isUndefStore(N) ||
2875 GraphPrintCheckerState->isUndefControlFlow(N) ||
Ted Kremenek6f5fca72008-02-29 23:14:48 +00002876 GraphPrintCheckerState->isUndefResult(N) ||
Ted Kremenek51e87ea2008-02-29 23:53:11 +00002877 GraphPrintCheckerState->isBadCall(N) ||
2878 GraphPrintCheckerState->isUndefArg(N))
Ted Kremenek5b70a222008-02-14 22:54:53 +00002879 return "color=\"red\",style=\"filled\"";
Mike Stump11289f42009-09-09 15:08:12 +00002880
Ted Kremeneke0c79382008-02-28 20:32:03 +00002881 if (GraphPrintCheckerState->isNoReturnCall(N))
2882 return "color=\"blue\",style=\"filled\"";
Zhongxing Xu9e200792009-11-24 07:06:39 +00002883#endif
Ted Kremenek5b70a222008-02-14 22:54:53 +00002884 return "";
2885 }
Mike Stump11289f42009-09-09 15:08:12 +00002886
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002887 static std::string getNodeLabel(const ExplodedNode* N, void*){
Mike Stump11289f42009-09-09 15:08:12 +00002888
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002889 std::string sbuf;
2890 llvm::raw_string_ostream Out(sbuf);
Ted Kremenek930191c2008-01-23 22:30:44 +00002891
2892 // Program Location.
Ted Kremenekac886cb2008-01-16 21:46:15 +00002893 ProgramPoint Loc = N->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002894
Ted Kremenekac886cb2008-01-16 21:46:15 +00002895 switch (Loc.getKind()) {
2896 case ProgramPoint::BlockEntranceKind:
Mike Stump11289f42009-09-09 15:08:12 +00002897 Out << "Block Entrance: B"
Ted Kremenekac886cb2008-01-16 21:46:15 +00002898 << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
2899 break;
Mike Stump11289f42009-09-09 15:08:12 +00002900
Ted Kremenekac886cb2008-01-16 21:46:15 +00002901 case ProgramPoint::BlockExitKind:
2902 assert (false);
2903 break;
Mike Stump11289f42009-09-09 15:08:12 +00002904
Ted Kremenekac886cb2008-01-16 21:46:15 +00002905 default: {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002906 if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
2907 const Stmt* S = L->getStmt();
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002908 SourceLocation SLoc = S->getLocStart();
2909
Mike Stump11289f42009-09-09 15:08:12 +00002910 Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
Chris Lattnerc61089a2009-06-30 01:26:17 +00002911 LangOptions LO; // FIXME.
2912 S->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00002913
2914 if (SLoc.isFileID()) {
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002915 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00002916 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2917 << " col="
2918 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
2919 << "\\l";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002920 }
Mike Stump11289f42009-09-09 15:08:12 +00002921
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002922 if (isa<PreStmt>(Loc))
Mike Stump11289f42009-09-09 15:08:12 +00002923 Out << "\\lPreStmt\\l;";
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002924 else if (isa<PostLoad>(Loc))
Ted Kremeneka6e08322009-05-07 18:27:16 +00002925 Out << "\\lPostLoad\\l;";
2926 else if (isa<PostStore>(Loc))
2927 Out << "\\lPostStore\\l";
2928 else if (isa<PostLValue>(Loc))
2929 Out << "\\lPostLValue\\l";
Mike Stump11289f42009-09-09 15:08:12 +00002930
Ted Kremenek7cf82382009-11-11 20:16:36 +00002931#if 0
2932 // FIXME: Replace with a general scheme to determine
2933 // the name of the check.
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002934 if (GraphPrintCheckerState->isImplicitNullDeref(N))
2935 Out << "\\|Implicit-Null Dereference.\\l";
2936 else if (GraphPrintCheckerState->isExplicitNullDeref(N))
2937 Out << "\\|Explicit-Null Dereference.\\l";
2938 else if (GraphPrintCheckerState->isUndefDeref(N))
2939 Out << "\\|Dereference of undefialied value.\\l";
2940 else if (GraphPrintCheckerState->isUndefStore(N))
2941 Out << "\\|Store to Undefined Loc.";
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002942 else if (GraphPrintCheckerState->isUndefResult(N))
2943 Out << "\\|Result of operation is undefined.";
2944 else if (GraphPrintCheckerState->isNoReturnCall(N))
2945 Out << "\\|Call to function marked \"noreturn\".";
2946 else if (GraphPrintCheckerState->isBadCall(N))
2947 Out << "\\|Call to NULL/Undefined.";
2948 else if (GraphPrintCheckerState->isUndefArg(N))
2949 Out << "\\|Argument in call is undefined";
Ted Kremenek7cf82382009-11-11 20:16:36 +00002950#endif
Mike Stump11289f42009-09-09 15:08:12 +00002951
Ted Kremenek9e08ff42008-12-16 22:02:27 +00002952 break;
2953 }
2954
Ted Kremenekac886cb2008-01-16 21:46:15 +00002955 const BlockEdge& E = cast<BlockEdge>(Loc);
2956 Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
2957 << E.getDst()->getBlockID() << ')';
Mike Stump11289f42009-09-09 15:08:12 +00002958
Ted Kremeneka50d9852008-01-30 23:03:39 +00002959 if (Stmt* T = E.getSrc()->getTerminator()) {
Mike Stump11289f42009-09-09 15:08:12 +00002960
Ted Kremenek6947a792008-03-07 20:57:30 +00002961 SourceLocation SLoc = T->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00002962
Ted Kremeneka50d9852008-01-30 23:03:39 +00002963 Out << "\\|Terminator: ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002964 LangOptions LO; // FIXME.
2965 E.getSrc()->printTerminator(Out, LO);
Mike Stump11289f42009-09-09 15:08:12 +00002966
Ted Kremenek03ab1562008-03-09 03:30:59 +00002967 if (SLoc.isFileID()) {
2968 Out << "\\lline="
Chris Lattnere4ad4172009-02-04 00:55:58 +00002969 << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
2970 << " col="
2971 << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
Ted Kremenek03ab1562008-03-09 03:30:59 +00002972 }
Mike Stump11289f42009-09-09 15:08:12 +00002973
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002974 if (isa<SwitchStmt>(T)) {
2975 Stmt* Label = E.getDst()->getLabel();
Mike Stump11289f42009-09-09 15:08:12 +00002976
2977 if (Label) {
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002978 if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2979 Out << "\\lcase ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002980 LangOptions LO; // FIXME.
2981 C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
Mike Stump11289f42009-09-09 15:08:12 +00002982
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002983 if (Stmt* RHS = C->getRHS()) {
2984 Out << " .. ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002985 RHS->printPretty(Out, 0, PrintingPolicy(LO));
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002986 }
Mike Stump11289f42009-09-09 15:08:12 +00002987
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002988 Out << ":";
2989 }
2990 else {
2991 assert (isa<DefaultStmt>(Label));
2992 Out << "\\ldefault:";
2993 }
2994 }
Mike Stump11289f42009-09-09 15:08:12 +00002995 else
Ted Kremenek80ebc1d2008-02-13 23:08:21 +00002996 Out << "\\l(implicit) default:";
2997 }
2998 else if (isa<IndirectGotoStmt>(T)) {
Ted Kremeneka50d9852008-01-30 23:03:39 +00002999 // FIXME
3000 }
3001 else {
3002 Out << "\\lCondition: ";
3003 if (*E.getSrc()->succ_begin() == E.getDst())
3004 Out << "true";
3005 else
Mike Stump11289f42009-09-09 15:08:12 +00003006 Out << "false";
Ted Kremeneka50d9852008-01-30 23:03:39 +00003007 }
Mike Stump11289f42009-09-09 15:08:12 +00003008
Ted Kremeneka50d9852008-01-30 23:03:39 +00003009 Out << "\\l";
3010 }
Mike Stump11289f42009-09-09 15:08:12 +00003011
Ted Kremenek7cf82382009-11-11 20:16:36 +00003012#if 0
3013 // FIXME: Replace with a general scheme to determine
3014 // the name of the check.
Ted Kremenek93d1fed2008-02-28 09:25:22 +00003015 if (GraphPrintCheckerState->isUndefControlFlow(N)) {
3016 Out << "\\|Control-flow based on\\lUndefined value.\\l";
Ted Kremenek2531fce2008-01-30 23:24:39 +00003017 }
Ted Kremenek7cf82382009-11-11 20:16:36 +00003018#endif
Ted Kremenekac886cb2008-01-16 21:46:15 +00003019 }
3020 }
Mike Stump11289f42009-09-09 15:08:12 +00003021
Ted Kremenek22c62c12008-02-28 10:21:43 +00003022 Out << "\\|StateID: " << (void*) N->getState() << "\\|";
Ted Kremenekb54312d2008-02-08 21:10:02 +00003023
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003024 const GRState *state = N->getState();
3025 state->printDOT(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003026
Ted Kremenek930191c2008-01-23 22:30:44 +00003027 Out << "\\l";
Ted Kremenekac886cb2008-01-16 21:46:15 +00003028 return Out.str();
3029 }
3030};
Mike Stump11289f42009-09-09 15:08:12 +00003031} // end llvm namespace
Ted Kremenekac886cb2008-01-16 21:46:15 +00003032#endif
3033
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003034#ifndef NDEBUG
Ted Kremenek576b76a2008-03-12 17:18:20 +00003035template <typename ITERATOR>
Zhongxing Xu107f7592009-08-06 12:48:26 +00003036ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
Ted Kremenek576b76a2008-03-12 17:18:20 +00003037
Zhongxing Xu107f7592009-08-06 12:48:26 +00003038template <> ExplodedNode*
3039GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
3040 (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
Ted Kremenek576b76a2008-03-12 17:18:20 +00003041 return I->first;
3042}
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003043#endif
3044
3045void GRExprEngine::ViewGraph(bool trim) {
Mike Stump11289f42009-09-09 15:08:12 +00003046#ifndef NDEBUG
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003047 if (trim) {
Zhongxing Xu107f7592009-08-06 12:48:26 +00003048 std::vector<ExplodedNode*> Src;
Ted Kremenek95175052009-03-11 01:41:22 +00003049
3050 // Flush any outstanding reports to make sure we cover all the nodes.
3051 // This does not cause them to get displayed.
3052 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
3053 const_cast<BugType*>(*I)->FlushReports(BR);
3054
3055 // Iterate through the reports and get their nodes.
3056 for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
Ted Kremenek17a02962009-09-03 03:02:58 +00003057 for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
Mike Stump11289f42009-09-09 15:08:12 +00003058 I2!=E2; ++I2) {
Ted Kremenek95175052009-03-11 01:41:22 +00003059 const BugReportEquivClass& EQ = *I2;
3060 const BugReport &R = **EQ.begin();
Zhongxing Xu107f7592009-08-06 12:48:26 +00003061 ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
Ted Kremenek95175052009-03-11 01:41:22 +00003062 if (N) Src.push_back(N);
3063 }
3064 }
Mike Stump11289f42009-09-09 15:08:12 +00003065
Ted Kremenek576b76a2008-03-12 17:18:20 +00003066 ViewGraph(&Src[0], &Src[0]+Src.size());
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003067 }
Ted Kremeneka7178c72008-03-11 18:25:33 +00003068 else {
3069 GraphPrintCheckerState = this;
3070 GraphPrintSourceManager = &getContext().getSourceManager();
Ted Kremenek16306102008-08-13 21:24:49 +00003071
Ted Kremenek2bdd7762008-03-07 22:58:01 +00003072 llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
Mike Stump11289f42009-09-09 15:08:12 +00003073
Ted Kremeneka7178c72008-03-11 18:25:33 +00003074 GraphPrintCheckerState = NULL;
3075 GraphPrintSourceManager = NULL;
3076 }
3077#endif
3078}
3079
Zhongxing Xu107f7592009-08-06 12:48:26 +00003080void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
Ted Kremeneka7178c72008-03-11 18:25:33 +00003081#ifndef NDEBUG
3082 GraphPrintCheckerState = this;
3083 GraphPrintSourceManager = &getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00003084
Zhongxing Xu107f7592009-08-06 12:48:26 +00003085 std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
Ted Kremeneka7178c72008-03-11 18:25:33 +00003086
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003087 if (!TrimmedG.get())
Benjamin Kramer89b422c2009-08-23 12:08:50 +00003088 llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
Ted Kremenekfc5d0672009-02-04 23:49:09 +00003089 else
Mike Stump11289f42009-09-09 15:08:12 +00003090 llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
3091
Ted Kremenek2531fce2008-01-30 23:24:39 +00003092 GraphPrintCheckerState = NULL;
Ted Kremenek6947a792008-03-07 20:57:30 +00003093 GraphPrintSourceManager = NULL;
Ted Kremenekd3122cb2008-02-14 22:36:46 +00003094#endif
Ted Kremenek2e12c2e2008-01-16 18:18:48 +00003095}