110e25031STed Kremenek //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===// 210e25031STed Kremenek // 310e25031STed Kremenek // The LLVM Compiler Infrastructure 410e25031STed Kremenek // 510e25031STed Kremenek // This file is distributed under the University of Illinois Open Source 610e25031STed Kremenek // License. See LICENSE.TXT for details. 710e25031STed Kremenek // 810e25031STed Kremenek //===----------------------------------------------------------------------===// 910e25031STed Kremenek // 1010e25031STed Kremenek // This file defines the C++ expression evaluation engine. 1110e25031STed Kremenek // 1210e25031STed Kremenek //===----------------------------------------------------------------------===// 1310e25031STed Kremenek 1410e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 1510e25031STed Kremenek #include "clang/AST/DeclCXX.h" 168fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h" 1717a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h" 183a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h" 193a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 203a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 2110e25031STed Kremenek 2210e25031STed Kremenek using namespace clang; 2310e25031STed Kremenek using namespace ento; 2410e25031STed Kremenek 2510e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 2610e25031STed Kremenek ExplodedNode *Pred, 2710e25031STed Kremenek ExplodedNodeSet &Dst) { 28d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 292a14c695STed Kremenek const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens(); 3049b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 31632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3210e25031STed Kremenek 33c9318304SJordan Rose SVal V = state->getSVal(tempExpr, LCtx); 3410e25031STed Kremenek 35fcdda361SJordan Rose // If the value is already a CXXTempObjectRegion, it is fine as it is. 36fcdda361SJordan Rose // Otherwise, create a new CXXTempObjectRegion, and copy the value into it. 37861a1740SJordan Rose // This is an optimization for when an rvalue is constructed and then 38861a1740SJordan Rose // immediately materialized. 39fcdda361SJordan Rose const MemRegion *MR = V.getAsRegion(); 40861a1740SJordan Rose if (const CXXTempObjectRegion *TR = 41861a1740SJordan Rose dyn_cast_or_null<CXXTempObjectRegion>(MR)) { 42861a1740SJordan Rose if (getContext().hasSameUnqualifiedType(TR->getValueType(), ME->getType())) 435772f82dSJordan Rose state = state->BindExpr(ME, LCtx, V); 44861a1740SJordan Rose } 4510e25031STed Kremenek 46861a1740SJordan Rose if (state == Pred->getState()) 47861a1740SJordan Rose state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME); 485772f82dSJordan Rose Bldr.generateNode(ME, Pred, state); 4910e25031STed Kremenek } 5010e25031STed Kremenek 513d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core 523d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine. 5388bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred, 543d7b7f52SJordan Rose const CallEvent &Call) { 553d7b7f52SJordan Rose SVal ThisVal; 563d7b7f52SJordan Rose bool AlwaysReturnsLValue; 573d7b7f52SJordan Rose if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) { 583d7b7f52SJordan Rose assert(Ctor->getDecl()->isTrivial()); 593d7b7f52SJordan Rose assert(Ctor->getDecl()->isCopyOrMoveConstructor()); 603d7b7f52SJordan Rose ThisVal = Ctor->getCXXThisVal(); 613d7b7f52SJordan Rose AlwaysReturnsLValue = false; 623d7b7f52SJordan Rose } else { 633d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial()); 643d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() == 653d7b7f52SJordan Rose OO_Equal); 663d7b7f52SJordan Rose ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal(); 673d7b7f52SJordan Rose AlwaysReturnsLValue = true; 683d7b7f52SJordan Rose } 6988bb563cSJordan Rose 7088bb563cSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 7188bb563cSJordan Rose 7288bb563cSJordan Rose ExplodedNodeSet Dst; 7388bb563cSJordan Rose Bldr.takeNodes(Pred); 7488bb563cSJordan Rose 7588bb563cSJordan Rose SVal V = Call.getArgSVal(0); 7688bb563cSJordan Rose 773d7b7f52SJordan Rose // If the value being copied is not unknown, load from its location to get 783d7b7f52SJordan Rose // an aggregate rvalue. 7905785d16SDavid Blaikie if (Optional<Loc> L = V.getAs<Loc>()) 8088bb563cSJordan Rose V = Pred->getState()->getSVal(*L); 813d7b7f52SJordan Rose else 823d7b7f52SJordan Rose assert(V.isUnknown()); 8388bb563cSJordan Rose 843d7b7f52SJordan Rose const Expr *CallExpr = Call.getOriginExpr(); 853d7b7f52SJordan Rose evalBind(Dst, CallExpr, Pred, ThisVal, V, true); 8688bb563cSJordan Rose 873d7b7f52SJordan Rose PostStmt PS(CallExpr, LCtx); 8888bb563cSJordan Rose for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); 8988bb563cSJordan Rose I != E; ++I) { 9088bb563cSJordan Rose ProgramStateRef State = (*I)->getState(); 913d7b7f52SJordan Rose if (AlwaysReturnsLValue) 923d7b7f52SJordan Rose State = State->BindExpr(CallExpr, LCtx, ThisVal); 933d7b7f52SJordan Rose else 9488bb563cSJordan Rose State = bindReturnValue(Call, LCtx, State); 9588bb563cSJordan Rose Bldr.generateNode(PS, State, *I); 9688bb563cSJordan Rose } 9788bb563cSJordan Rose } 9888bb563cSJordan Rose 99f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 10010e25031STed Kremenek ExplodedNode *Pred, 10110e25031STed Kremenek ExplodedNodeSet &destNodes) { 102b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 10305375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 10405375eb4SJordan Rose 105b970505dSJordan Rose const MemRegion *Target = 0; 10688bb563cSJordan Rose bool IsArray = false; 107b970505dSJordan Rose 108b970505dSJordan Rose switch (CE->getConstructionKind()) { 109b970505dSJordan Rose case CXXConstructExpr::CK_Complete: { 11005375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 11105375eb4SJordan Rose // element in the CFG. 112d94854a4STed Kremenek const CFGBlock *B = currBldrCtx->getBlock(); 113d94854a4STed Kremenek if (currStmtIdx + 1 < B->size()) { 114d94854a4STed Kremenek CFGElement Next = (*B)[currStmtIdx+1]; 11505375eb4SJordan Rose 11605375eb4SJordan Rose // Is this a constructor for a local variable? 11700be69abSDavid Blaikie if (Optional<CFGStmt> StmtElem = Next.getAs<CFGStmt>()) { 11800be69abSDavid Blaikie if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 11920edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 12020edae87SJordan Rose if (Var->getInit()->IgnoreImplicit() == CE) { 12120edae87SJordan Rose QualType Ty = Var->getType(); 12220edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(Ty)) { 12320edae87SJordan Rose // FIXME: Handle arrays, which run the same constructor for 12420edae87SJordan Rose // every element. This workaround will just run the first 12520edae87SJordan Rose // constructor (which should still invalidate the entire array). 12620edae87SJordan Rose SVal Base = State->getLValue(Var, LCtx); 12720edae87SJordan Rose Target = State->getLValue(AT->getElementType(), 12820edae87SJordan Rose getSValBuilder().makeZeroArrayIndex(), 12920edae87SJordan Rose Base).getAsRegion(); 13088bb563cSJordan Rose IsArray = true; 13120edae87SJordan Rose } else { 13205375eb4SJordan Rose Target = State->getLValue(Var, LCtx).getAsRegion(); 13320edae87SJordan Rose } 13420edae87SJordan Rose } 13520edae87SJordan Rose } 13620edae87SJordan Rose } 13720edae87SJordan Rose } 13805375eb4SJordan Rose 13954529a34SJordan Rose // Is this a constructor for a member? 14000be69abSDavid Blaikie if (Optional<CFGInitializer> InitElem = Next.getAs<CFGInitializer>()) { 14100be69abSDavid Blaikie const CXXCtorInitializer *Init = InitElem->getInitializer(); 14254529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 14354529a34SJordan Rose 14454529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 14554529a34SJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 14654529a34SJordan Rose LCtx->getCurrentStackFrame()); 14754529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 14854529a34SJordan Rose 14954529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 15054529a34SJordan Rose SVal Field = State->getLValue(Init->getIndirectMember(), ThisVal); 15120edae87SJordan Rose Target = Field.getAsRegion(); 15254529a34SJordan Rose } else { 15354529a34SJordan Rose SVal Field = State->getLValue(Init->getMember(), ThisVal); 15420edae87SJordan Rose Target = Field.getAsRegion(); 15554529a34SJordan Rose } 15654529a34SJordan Rose } 15754529a34SJordan Rose 15805375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 15905375eb4SJordan Rose } 16005375eb4SJordan Rose 161c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 162c9318304SJordan Rose // constructing a temporary. 163c9318304SJordan Rose if (!Target) { 164c9318304SJordan Rose MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 165c9318304SJordan Rose Target = MRMgr.getCXXTempObjectRegion(CE, LCtx); 166c9318304SJordan Rose } 16705375eb4SJordan Rose 168b970505dSJordan Rose break; 169b970505dSJordan Rose } 170b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 171b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 172b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 173b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 174b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 175b970505dSJordan Rose LCtx->getCurrentStackFrame()); 17605375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 177b970505dSJordan Rose 178b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 179b970505dSJordan Rose Target = ThisVal.getAsRegion(); 180b970505dSJordan Rose } else { 181b970505dSJordan Rose // Cast to the base type. 182599f85abSJordan Rose bool IsVirtual = 183599f85abSJordan Rose (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase); 184599f85abSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(), 185599f85abSJordan Rose IsVirtual); 18620edae87SJordan Rose Target = BaseVal.getAsRegion(); 187b970505dSJordan Rose } 188b970505dSJordan Rose break; 189b970505dSJordan Rose } 190b970505dSJordan Rose } 191b970505dSJordan Rose 192fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 193fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 194fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 19510e25031STed Kremenek 196f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 197f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 198afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 199afe7c2c2SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit, 200fcd016e5SJordan Rose *Call, *this); 20110e25031STed Kremenek 20288bb563cSJordan Rose ExplodedNodeSet DstEvaluated; 20388bb563cSJordan Rose StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx); 20488bb563cSJordan Rose 20588bb563cSJordan Rose if (CE->getConstructor()->isTrivial() && 20688bb563cSJordan Rose CE->getConstructor()->isCopyOrMoveConstructor() && 20788bb563cSJordan Rose !IsArray) { 20888bb563cSJordan Rose // FIXME: Handle other kinds of trivial constructors as well. 20988bb563cSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 21088bb563cSJordan Rose I != E; ++I) 21188bb563cSJordan Rose performTrivialCopy(Bldr, *I, *Call); 21288bb563cSJordan Rose 21388bb563cSJordan Rose } else { 214afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 215f3c12ac3SJordan Rose I != E; ++I) 216fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 21788bb563cSJordan Rose } 21810e25031STed Kremenek 219afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 22088bb563cSJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated, 221fcd016e5SJordan Rose *Call, *this); 222afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 22310e25031STed Kremenek } 22410e25031STed Kremenek 225b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 22610e25031STed Kremenek const MemRegion *Dest, 22710e25031STed Kremenek const Stmt *S, 2282bc9674bSJordan Rose bool IsBaseDtor, 22910e25031STed Kremenek ExplodedNode *Pred, 23010e25031STed Kremenek ExplodedNodeSet &Dst) { 231fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 232fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 233fcd016e5SJordan Rose 23420edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 23520edae87SJordan Rose // This workaround will just run the first destructor (which will still 23620edae87SJordan Rose // invalidate the entire array). 2374cfdbff3SJordan Rose // This is a loop because of multidimensional arrays. 2384cfdbff3SJordan Rose while (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) { 23920edae87SJordan Rose ObjectType = AT->getElementType(); 240fcd016e5SJordan Rose Dest = State->getLValue(ObjectType, getSValBuilder().makeZeroArrayIndex(), 24120edae87SJordan Rose loc::MemRegionVal(Dest)).getAsRegion(); 24220edae87SJordan Rose } 24320edae87SJordan Rose 244b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 245b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 246b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 247b970505dSJordan Rose 248fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 249fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 2502bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 251142adc49STed Kremenek 25217a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 25317a8757aSJordan Rose Call->getSourceRange().getBegin(), 25417a8757aSJordan Rose "Error evaluating destructor"); 25517a8757aSJordan Rose 2564ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 2574ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 258fcd016e5SJordan Rose *Call, *this); 25910e25031STed Kremenek 2604ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 261d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 2624ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 2634ee71b8aSJordan Rose I != E; ++I) 264fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 2654ee71b8aSJordan Rose 2664ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 2674ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 268fcd016e5SJordan Rose *Call, *this); 26910e25031STed Kremenek } 27010e25031STed Kremenek 27110e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 27210e25031STed Kremenek ExplodedNodeSet &Dst) { 273a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 274a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 275a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 276a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 27710e25031STed Kremenek 278d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 279d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 2808b662704SAnton Yartsev DefinedOrUnknownSVal symVal = UnknownVal(); 2818b662704SAnton Yartsev FunctionDecl *FD = CNE->getOperatorNew(); 28210e25031STed Kremenek 2838b662704SAnton Yartsev bool IsStandardGlobalOpNewFunction = false; 2848b662704SAnton Yartsev if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) { 2858b662704SAnton Yartsev if (FD->getNumParams() == 2) { 2868b662704SAnton Yartsev QualType T = FD->getParamDecl(1)->getType(); 2878b662704SAnton Yartsev if (const IdentifierInfo *II = T.getBaseTypeIdentifier()) 2888b662704SAnton Yartsev // NoThrow placement new behaves as a standard new. 2898b662704SAnton Yartsev IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t"); 2908b662704SAnton Yartsev } 2918b662704SAnton Yartsev else 2928b662704SAnton Yartsev // Placement forms are considered non-standard. 2938b662704SAnton Yartsev IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1); 2948b662704SAnton Yartsev } 2958b662704SAnton Yartsev 2968b662704SAnton Yartsev // We assume all standard global 'operator new' functions allocate memory in 2978b662704SAnton Yartsev // heap. We realize this is an approximation that might not correctly model 2988b662704SAnton Yartsev // a custom global allocator. 2998b662704SAnton Yartsev if (IsStandardGlobalOpNewFunction) 3008b662704SAnton Yartsev symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount); 3018b662704SAnton Yartsev else 3028b662704SAnton Yartsev symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, CNE->getType(), 3038b662704SAnton Yartsev blockCount); 3048b662704SAnton Yartsev 3058b662704SAnton Yartsev ProgramStateRef State = Pred->getState(); 306fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 307fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 308fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 309fcd016e5SJordan Rose 310a4ee064cSJordan Rose // Invalidate placement args. 311fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 312fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 313fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 314*b8cb8359SJordan Rose if (!State) 315*b8cb8359SJordan Rose return; 316a4ee064cSJordan Rose 3173957fd58SJordan Rose // If we're compiling with exceptions enabled, and this allocation function 3183957fd58SJordan Rose // is not declared as non-throwing, failures /must/ be signalled by 3193957fd58SJordan Rose // exceptions, and thus the return value will never be NULL. 3203957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 3213957fd58SJordan Rose if (FD && getContext().getLangOpts().CXXExceptions) { 3223957fd58SJordan Rose QualType Ty = FD->getType(); 3233957fd58SJordan Rose if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>()) 3243957fd58SJordan Rose if (!ProtoType->isNothrow(getContext())) 3253957fd58SJordan Rose State = State->assume(symVal, true); 3263957fd58SJordan Rose } 3273957fd58SJordan Rose 328*b8cb8359SJordan Rose StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 329*b8cb8359SJordan Rose 33010e25031STed Kremenek if (CNE->isArray()) { 33110e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 33210e25031STed Kremenek // For now, just return a symbolicated region. 3332fdacbc5SDavid Blaikie const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion(); 334a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 335a4ee064cSJordan Rose const ElementRegion *EleReg = 336a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 33744320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 338632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 33944320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 34010e25031STed Kremenek return; 34110e25031STed Kremenek } 34210e25031STed Kremenek 343a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 344a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 345a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 346a4ee064cSJordan Rose // the constructor call anyway.) 347*b8cb8359SJordan Rose SVal Result = symVal; 34844320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 34944320367SJordan Rose // Non-array placement new should always return the placement location. 35044320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 351*b8cb8359SJordan Rose Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 352aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 35344320367SJordan Rose } 35444320367SJordan Rose 355*b8cb8359SJordan Rose // Bind the address of the object, then check to see if we cached out. 356*b8cb8359SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 3578f6b4b04SJordan Rose ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State); 3588f6b4b04SJordan Rose if (!NewN) 3598f6b4b04SJordan Rose return; 3603503cb35SJordan Rose 3615089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 3625089f3b3SJordan Rose // initializer. Copy the value over. 3635089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 3645089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 3653503cb35SJordan Rose assert(Bldr.getResults().size() == 1); 3668f6b4b04SJordan Rose Bldr.takeNodes(NewN); 3675089f3b3SJordan Rose 3683503cb35SJordan Rose assert(!CNE->getType()->getPointeeCXXRecordDecl()); 369*b8cb8359SJordan Rose evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx), 370*b8cb8359SJordan Rose /*FirstInit=*/IsStandardGlobalOpNewFunction); 3713503cb35SJordan Rose } 3723503cb35SJordan Rose } 37310e25031STed Kremenek } 37410e25031STed Kremenek 37510e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 37610e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 377d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 37864aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 37964aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 38010e25031STed Kremenek } 38110e25031STed Kremenek 3828fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 3838fdb59f9STed Kremenek ExplodedNode *Pred, 3848fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 3858fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 386b1279b5cSTed Kremenek if (!VD) { 387b1279b5cSTed Kremenek Dst.Add(Pred); 388b1279b5cSTed Kremenek return; 389b1279b5cSTed Kremenek } 390b1279b5cSTed Kremenek 3918fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 392d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 393d94854a4STed Kremenek currBldrCtx->blockCount()); 3948fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 3958fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 3968fdb59f9STed Kremenek 397d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 3988fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 3998fdb59f9STed Kremenek } 4008fdb59f9STed Kremenek 40110e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 40210e25031STed Kremenek ExplodedNodeSet &Dst) { 403d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 404e594034fSAnna Zaks 40510e25031STed Kremenek // Get the this object region from StoreManager. 406632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 40710e25031STed Kremenek const MemRegion *R = 40810e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 40910e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 410632e3b7eSTed Kremenek LCtx); 41110e25031STed Kremenek 41249b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 41310e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 414632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 41510e25031STed Kremenek } 416