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 99bc74eb1cSJordan Rose 100bc74eb1cSJordan Rose /// Returns a region representing the first element of a (possibly 101bc74eb1cSJordan Rose /// multi-dimensional) array. 102bc74eb1cSJordan Rose /// 103bc74eb1cSJordan Rose /// On return, \p Ty will be set to the base type of the array. 104bc74eb1cSJordan Rose /// 105bc74eb1cSJordan Rose /// If the type is not an array type at all, the original value is returned. 106bc74eb1cSJordan Rose static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue, 107bc74eb1cSJordan Rose QualType &Ty) { 108bc74eb1cSJordan Rose SValBuilder &SVB = State->getStateManager().getSValBuilder(); 109bc74eb1cSJordan Rose ASTContext &Ctx = SVB.getContext(); 110bc74eb1cSJordan Rose 111bc74eb1cSJordan Rose while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) { 112bc74eb1cSJordan Rose Ty = AT->getElementType(); 113bc74eb1cSJordan Rose LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue); 114bc74eb1cSJordan Rose } 115bc74eb1cSJordan Rose 116bc74eb1cSJordan Rose return LValue; 117bc74eb1cSJordan Rose } 118bc74eb1cSJordan Rose 119f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 12010e25031STed Kremenek ExplodedNode *Pred, 12110e25031STed Kremenek ExplodedNodeSet &destNodes) { 122b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 12305375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 12405375eb4SJordan Rose 125b970505dSJordan Rose const MemRegion *Target = 0; 126bc74eb1cSJordan Rose 127bc74eb1cSJordan Rose // FIXME: Handle arrays, which run the same constructor for every element. 128bc74eb1cSJordan Rose // For now, we just run the first constructor (which should still invalidate 129bc74eb1cSJordan Rose // the entire array). 130b970505dSJordan Rose 131b970505dSJordan Rose switch (CE->getConstructionKind()) { 132b970505dSJordan Rose case CXXConstructExpr::CK_Complete: { 13305375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 13405375eb4SJordan Rose // element in the CFG. 135d94854a4STed Kremenek const CFGBlock *B = currBldrCtx->getBlock(); 136d94854a4STed Kremenek if (currStmtIdx + 1 < B->size()) { 137d94854a4STed Kremenek CFGElement Next = (*B)[currStmtIdx+1]; 13805375eb4SJordan Rose 13905375eb4SJordan Rose // Is this a constructor for a local variable? 14000be69abSDavid Blaikie if (Optional<CFGStmt> StmtElem = Next.getAs<CFGStmt>()) { 14100be69abSDavid Blaikie if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 14220edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 14320edae87SJordan Rose if (Var->getInit()->IgnoreImplicit() == CE) { 144bc74eb1cSJordan Rose SVal LValue = State->getLValue(Var, LCtx); 14520edae87SJordan Rose QualType Ty = Var->getType(); 146bc74eb1cSJordan Rose LValue = makeZeroElementRegion(State, LValue, Ty); 147bc74eb1cSJordan Rose Target = LValue.getAsRegion(); 14820edae87SJordan Rose } 14920edae87SJordan Rose } 15020edae87SJordan Rose } 15120edae87SJordan Rose } 15205375eb4SJordan Rose 15354529a34SJordan Rose // Is this a constructor for a member? 15400be69abSDavid Blaikie if (Optional<CFGInitializer> InitElem = Next.getAs<CFGInitializer>()) { 15500be69abSDavid Blaikie const CXXCtorInitializer *Init = InitElem->getInitializer(); 15654529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 15754529a34SJordan Rose 15854529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 15954529a34SJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 16054529a34SJordan Rose LCtx->getCurrentStackFrame()); 16154529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 16254529a34SJordan Rose 163bc74eb1cSJordan Rose const ValueDecl *Field; 164bc74eb1cSJordan Rose SVal FieldVal; 16554529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 166bc74eb1cSJordan Rose Field = Init->getIndirectMember(); 167bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal); 16854529a34SJordan Rose } else { 169bc74eb1cSJordan Rose Field = Init->getMember(); 170bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getMember(), ThisVal); 17154529a34SJordan Rose } 172bc74eb1cSJordan Rose 173bc74eb1cSJordan Rose QualType Ty = Field->getType(); 174bc74eb1cSJordan Rose FieldVal = makeZeroElementRegion(State, FieldVal, Ty); 175bc74eb1cSJordan Rose Target = FieldVal.getAsRegion(); 17654529a34SJordan Rose } 17754529a34SJordan Rose 17805375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 17905375eb4SJordan Rose } 18005375eb4SJordan Rose 181c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 182c9318304SJordan Rose // constructing a temporary. 183c9318304SJordan Rose if (!Target) { 184c9318304SJordan Rose MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 185c9318304SJordan Rose Target = MRMgr.getCXXTempObjectRegion(CE, LCtx); 186c9318304SJordan Rose } 18705375eb4SJordan Rose 188b970505dSJordan Rose break; 189b970505dSJordan Rose } 190*b3b976f0SJordan Rose case CXXConstructExpr::CK_VirtualBase: 191*b3b976f0SJordan Rose // Make sure we are not calling virtual base class initializers twice. 192*b3b976f0SJordan Rose // Only the most-derived object should initialize virtual base classes. 193*b3b976f0SJordan Rose if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) { 194*b3b976f0SJordan Rose const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer); 195*b3b976f0SJordan Rose if (OuterCtor) { 196*b3b976f0SJordan Rose switch (OuterCtor->getConstructionKind()) { 197b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 198b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 199*b3b976f0SJordan Rose // Bail out! 200*b3b976f0SJordan Rose destNodes.Add(Pred); 201*b3b976f0SJordan Rose return; 202*b3b976f0SJordan Rose case CXXConstructExpr::CK_Complete: 203*b3b976f0SJordan Rose case CXXConstructExpr::CK_Delegating: 204*b3b976f0SJordan Rose break; 205*b3b976f0SJordan Rose } 206*b3b976f0SJordan Rose } 207*b3b976f0SJordan Rose } 208*b3b976f0SJordan Rose // FALLTHROUGH 209*b3b976f0SJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 210b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 211b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 212b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 213b970505dSJordan Rose LCtx->getCurrentStackFrame()); 21405375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 215b970505dSJordan Rose 216b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 217b970505dSJordan Rose Target = ThisVal.getAsRegion(); 218b970505dSJordan Rose } else { 219b970505dSJordan Rose // Cast to the base type. 220599f85abSJordan Rose bool IsVirtual = 221599f85abSJordan Rose (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase); 222599f85abSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(), 223599f85abSJordan Rose IsVirtual); 22420edae87SJordan Rose Target = BaseVal.getAsRegion(); 225b970505dSJordan Rose } 226b970505dSJordan Rose break; 227b970505dSJordan Rose } 228b970505dSJordan Rose } 229b970505dSJordan Rose 230fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 231fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 232fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 23310e25031STed Kremenek 234f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 235f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 236afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 237e83cb092SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit, 238fcd016e5SJordan Rose *Call, *this); 23910e25031STed Kremenek 24088bb563cSJordan Rose ExplodedNodeSet DstEvaluated; 24188bb563cSJordan Rose StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx); 24288bb563cSJordan Rose 243e83cb092SJordan Rose bool IsArray = isa<ElementRegion>(Target); 244e83cb092SJordan Rose if (CE->getConstructor()->isTrivial() && 245e83cb092SJordan Rose CE->getConstructor()->isCopyOrMoveConstructor() && 246e83cb092SJordan Rose !IsArray) { 247e83cb092SJordan Rose // FIXME: Handle other kinds of trivial constructors as well. 248e83cb092SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 24988bb563cSJordan Rose I != E; ++I) 25088bb563cSJordan Rose performTrivialCopy(Bldr, *I, *Call); 25188bb563cSJordan Rose 25288bb563cSJordan Rose } else { 253afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 254f3c12ac3SJordan Rose I != E; ++I) 255fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 25688bb563cSJordan Rose } 25710e25031STed Kremenek 258afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 25988bb563cSJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated, 260fcd016e5SJordan Rose *Call, *this); 261afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 26210e25031STed Kremenek } 26310e25031STed Kremenek 264b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 26510e25031STed Kremenek const MemRegion *Dest, 26610e25031STed Kremenek const Stmt *S, 2672bc9674bSJordan Rose bool IsBaseDtor, 26810e25031STed Kremenek ExplodedNode *Pred, 26910e25031STed Kremenek ExplodedNodeSet &Dst) { 270fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 271fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 272fcd016e5SJordan Rose 27320edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 27420edae87SJordan Rose // This workaround will just run the first destructor (which will still 27520edae87SJordan Rose // invalidate the entire array). 276bc74eb1cSJordan Rose SVal DestVal = loc::MemRegionVal(Dest); 277bc74eb1cSJordan Rose DestVal = makeZeroElementRegion(State, DestVal, ObjectType); 278bc74eb1cSJordan Rose Dest = DestVal.getAsRegion(); 27920edae87SJordan Rose 280b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 281b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 282b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 283b970505dSJordan Rose 284fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 285fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 2862bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 287142adc49STed Kremenek 28817a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 28917a8757aSJordan Rose Call->getSourceRange().getBegin(), 29017a8757aSJordan Rose "Error evaluating destructor"); 29117a8757aSJordan Rose 2924ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 2934ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 294fcd016e5SJordan Rose *Call, *this); 29510e25031STed Kremenek 2964ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 297d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 2984ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 2994ee71b8aSJordan Rose I != E; ++I) 300fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 3014ee71b8aSJordan Rose 3024ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 3034ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 304fcd016e5SJordan Rose *Call, *this); 30510e25031STed Kremenek } 30610e25031STed Kremenek 30710e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 30810e25031STed Kremenek ExplodedNodeSet &Dst) { 309a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 310a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 311a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 312a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 31310e25031STed Kremenek 314d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 315d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3168b662704SAnton Yartsev DefinedOrUnknownSVal symVal = UnknownVal(); 3178b662704SAnton Yartsev FunctionDecl *FD = CNE->getOperatorNew(); 31810e25031STed Kremenek 3198b662704SAnton Yartsev bool IsStandardGlobalOpNewFunction = false; 3208b662704SAnton Yartsev if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) { 3218b662704SAnton Yartsev if (FD->getNumParams() == 2) { 3228b662704SAnton Yartsev QualType T = FD->getParamDecl(1)->getType(); 3238b662704SAnton Yartsev if (const IdentifierInfo *II = T.getBaseTypeIdentifier()) 3248b662704SAnton Yartsev // NoThrow placement new behaves as a standard new. 3258b662704SAnton Yartsev IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t"); 3268b662704SAnton Yartsev } 3278b662704SAnton Yartsev else 3288b662704SAnton Yartsev // Placement forms are considered non-standard. 3298b662704SAnton Yartsev IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1); 3308b662704SAnton Yartsev } 3318b662704SAnton Yartsev 3328b662704SAnton Yartsev // We assume all standard global 'operator new' functions allocate memory in 3338b662704SAnton Yartsev // heap. We realize this is an approximation that might not correctly model 3348b662704SAnton Yartsev // a custom global allocator. 3358b662704SAnton Yartsev if (IsStandardGlobalOpNewFunction) 3368b662704SAnton Yartsev symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount); 3378b662704SAnton Yartsev else 3388b662704SAnton Yartsev symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, CNE->getType(), 3398b662704SAnton Yartsev blockCount); 3408b662704SAnton Yartsev 3418b662704SAnton Yartsev ProgramStateRef State = Pred->getState(); 342fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 343fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 344fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 345fcd016e5SJordan Rose 346a4ee064cSJordan Rose // Invalidate placement args. 347fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 348fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 349fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 350b8cb8359SJordan Rose if (!State) 351b8cb8359SJordan Rose return; 352a4ee064cSJordan Rose 3533957fd58SJordan Rose // If we're compiling with exceptions enabled, and this allocation function 3543957fd58SJordan Rose // is not declared as non-throwing, failures /must/ be signalled by 3553957fd58SJordan Rose // exceptions, and thus the return value will never be NULL. 3563957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 3573957fd58SJordan Rose if (FD && getContext().getLangOpts().CXXExceptions) { 3583957fd58SJordan Rose QualType Ty = FD->getType(); 3593957fd58SJordan Rose if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>()) 3603957fd58SJordan Rose if (!ProtoType->isNothrow(getContext())) 3613957fd58SJordan Rose State = State->assume(symVal, true); 3623957fd58SJordan Rose } 3633957fd58SJordan Rose 364b8cb8359SJordan Rose StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 365b8cb8359SJordan Rose 36610e25031STed Kremenek if (CNE->isArray()) { 36710e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 36810e25031STed Kremenek // For now, just return a symbolicated region. 3692fdacbc5SDavid Blaikie const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion(); 370a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 371a4ee064cSJordan Rose const ElementRegion *EleReg = 372a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 37344320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 374632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 37544320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 37610e25031STed Kremenek return; 37710e25031STed Kremenek } 37810e25031STed Kremenek 379a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 380a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 381a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 382a4ee064cSJordan Rose // the constructor call anyway.) 383b8cb8359SJordan Rose SVal Result = symVal; 38444320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 38544320367SJordan Rose // Non-array placement new should always return the placement location. 38644320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 387b8cb8359SJordan Rose Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 388aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 38944320367SJordan Rose } 39044320367SJordan Rose 391b8cb8359SJordan Rose // Bind the address of the object, then check to see if we cached out. 392b8cb8359SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 3938f6b4b04SJordan Rose ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State); 3948f6b4b04SJordan Rose if (!NewN) 3958f6b4b04SJordan Rose return; 3963503cb35SJordan Rose 3975089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 3985089f3b3SJordan Rose // initializer. Copy the value over. 3995089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 4005089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 4013503cb35SJordan Rose assert(Bldr.getResults().size() == 1); 4028f6b4b04SJordan Rose Bldr.takeNodes(NewN); 4035089f3b3SJordan Rose 4043503cb35SJordan Rose assert(!CNE->getType()->getPointeeCXXRecordDecl()); 405b8cb8359SJordan Rose evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx), 406b8cb8359SJordan Rose /*FirstInit=*/IsStandardGlobalOpNewFunction); 4073503cb35SJordan Rose } 4083503cb35SJordan Rose } 40910e25031STed Kremenek } 41010e25031STed Kremenek 41110e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 41210e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 413d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 41464aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 41564aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 41610e25031STed Kremenek } 41710e25031STed Kremenek 4188fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 4198fdb59f9STed Kremenek ExplodedNode *Pred, 4208fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 4218fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 422b1279b5cSTed Kremenek if (!VD) { 423b1279b5cSTed Kremenek Dst.Add(Pred); 424b1279b5cSTed Kremenek return; 425b1279b5cSTed Kremenek } 426b1279b5cSTed Kremenek 4278fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 428d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 429d94854a4STed Kremenek currBldrCtx->blockCount()); 4308fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 4318fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 4328fdb59f9STed Kremenek 433d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 4348fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 4358fdb59f9STed Kremenek } 4368fdb59f9STed Kremenek 43710e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 43810e25031STed Kremenek ExplodedNodeSet &Dst) { 439d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 440e594034fSAnna Zaks 44110e25031STed Kremenek // Get the this object region from StoreManager. 442632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 44310e25031STed Kremenek const MemRegion *R = 44410e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 44510e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 446632e3b7eSTed Kremenek LCtx); 44710e25031STed Kremenek 44849b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 44910e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 450632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 45110e25031STed Kremenek } 452