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 
99*bc74eb1cSJordan Rose 
100*bc74eb1cSJordan Rose /// Returns a region representing the first element of a (possibly
101*bc74eb1cSJordan Rose /// multi-dimensional) array.
102*bc74eb1cSJordan Rose ///
103*bc74eb1cSJordan Rose /// On return, \p Ty will be set to the base type of the array.
104*bc74eb1cSJordan Rose ///
105*bc74eb1cSJordan Rose /// If the type is not an array type at all, the original value is returned.
106*bc74eb1cSJordan Rose static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue,
107*bc74eb1cSJordan Rose                                   QualType &Ty) {
108*bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
109*bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
110*bc74eb1cSJordan Rose 
111*bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
112*bc74eb1cSJordan Rose     Ty = AT->getElementType();
113*bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
114*bc74eb1cSJordan Rose   }
115*bc74eb1cSJordan Rose 
116*bc74eb1cSJordan Rose   return LValue;
117*bc74eb1cSJordan Rose }
118*bc74eb1cSJordan 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;
126*bc74eb1cSJordan Rose 
127*bc74eb1cSJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
128*bc74eb1cSJordan Rose   // For now, we just run the first constructor (which should still invalidate
129*bc74eb1cSJordan 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) {
144*bc74eb1cSJordan Rose               SVal LValue = State->getLValue(Var, LCtx);
14520edae87SJordan Rose               QualType Ty = Var->getType();
146*bc74eb1cSJordan Rose               LValue = makeZeroElementRegion(State, LValue, Ty);
147*bc74eb1cSJordan 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 
163*bc74eb1cSJordan Rose         const ValueDecl *Field;
164*bc74eb1cSJordan Rose         SVal FieldVal;
16554529a34SJordan Rose         if (Init->isIndirectMemberInitializer()) {
166*bc74eb1cSJordan Rose           Field = Init->getIndirectMember();
167*bc74eb1cSJordan Rose           FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
16854529a34SJordan Rose         } else {
169*bc74eb1cSJordan Rose           Field = Init->getMember();
170*bc74eb1cSJordan Rose           FieldVal = State->getLValue(Init->getMember(), ThisVal);
17154529a34SJordan Rose         }
172*bc74eb1cSJordan Rose 
173*bc74eb1cSJordan Rose         QualType Ty = Field->getType();
174*bc74eb1cSJordan Rose         FieldVal = makeZeroElementRegion(State, FieldVal, Ty);
175*bc74eb1cSJordan 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   }
190b970505dSJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
191b970505dSJordan Rose   case CXXConstructExpr::CK_VirtualBase:
192b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
193b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
194b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
195b970505dSJordan Rose                                               LCtx->getCurrentStackFrame());
19605375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
197b970505dSJordan Rose 
198b970505dSJordan Rose     if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
199b970505dSJordan Rose       Target = ThisVal.getAsRegion();
200b970505dSJordan Rose     } else {
201b970505dSJordan Rose       // Cast to the base type.
202599f85abSJordan Rose       bool IsVirtual =
203599f85abSJordan Rose         (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
204599f85abSJordan Rose       SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
205599f85abSJordan Rose                                                          IsVirtual);
20620edae87SJordan Rose       Target = BaseVal.getAsRegion();
207b970505dSJordan Rose     }
208b970505dSJordan Rose     break;
209b970505dSJordan Rose   }
210b970505dSJordan Rose   }
211b970505dSJordan Rose 
212fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
213fcd016e5SJordan Rose   CallEventRef<CXXConstructorCall> Call =
214fcd016e5SJordan Rose     CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
21510e25031STed Kremenek 
216f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
217f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
218afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
219afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit,
220fcd016e5SJordan Rose                                             *Call, *this);
22110e25031STed Kremenek 
22288bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
22388bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
22488bb563cSJordan Rose 
225*bc74eb1cSJordan Rose   bool IsArray = isa<ElementRegion>(Target);
22688bb563cSJordan Rose   if (CE->getConstructor()->isTrivial() &&
22788bb563cSJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
22888bb563cSJordan Rose       !IsArray) {
22988bb563cSJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
23088bb563cSJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
23188bb563cSJordan Rose          I != E; ++I)
23288bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
23388bb563cSJordan Rose 
23488bb563cSJordan Rose   } else {
235afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
236f3c12ac3SJordan Rose          I != E; ++I)
237fcd016e5SJordan Rose       defaultEvalCall(Bldr, *I, *Call);
23888bb563cSJordan Rose   }
23910e25031STed Kremenek 
240afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
24188bb563cSJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
242fcd016e5SJordan Rose                                              *Call, *this);
243afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
24410e25031STed Kremenek }
24510e25031STed Kremenek 
246b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
24710e25031STed Kremenek                                     const MemRegion *Dest,
24810e25031STed Kremenek                                     const Stmt *S,
2492bc9674bSJordan Rose                                     bool IsBaseDtor,
25010e25031STed Kremenek                                     ExplodedNode *Pred,
25110e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
252fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
253fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
254fcd016e5SJordan Rose 
25520edae87SJordan Rose   // FIXME: We need to run the same destructor on every element of the array.
25620edae87SJordan Rose   // This workaround will just run the first destructor (which will still
25720edae87SJordan Rose   // invalidate the entire array).
258*bc74eb1cSJordan Rose   SVal DestVal = loc::MemRegionVal(Dest);
259*bc74eb1cSJordan Rose   DestVal = makeZeroElementRegion(State, DestVal, ObjectType);
260*bc74eb1cSJordan Rose   Dest = DestVal.getAsRegion();
26120edae87SJordan Rose 
262b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
263b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
264b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
265b970505dSJordan Rose 
266fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
267fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
2682bc9674bSJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
269142adc49STed Kremenek 
27017a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
27117a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
27217a8757aSJordan Rose                                 "Error evaluating destructor");
27317a8757aSJordan Rose 
2744ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
2754ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
276fcd016e5SJordan Rose                                             *Call, *this);
27710e25031STed Kremenek 
2784ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
279d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
2804ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
2814ee71b8aSJordan Rose        I != E; ++I)
282fcd016e5SJordan Rose     defaultEvalCall(Bldr, *I, *Call);
2834ee71b8aSJordan Rose 
2844ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
2854ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
286fcd016e5SJordan Rose                                              *Call, *this);
28710e25031STed Kremenek }
28810e25031STed Kremenek 
28910e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
29010e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
291a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
292a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
293a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
294a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
29510e25031STed Kremenek 
296d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
297d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
2988b662704SAnton Yartsev   DefinedOrUnknownSVal symVal = UnknownVal();
2998b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
30010e25031STed Kremenek 
3018b662704SAnton Yartsev   bool IsStandardGlobalOpNewFunction = false;
3028b662704SAnton Yartsev   if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) {
3038b662704SAnton Yartsev     if (FD->getNumParams() == 2) {
3048b662704SAnton Yartsev       QualType T = FD->getParamDecl(1)->getType();
3058b662704SAnton Yartsev       if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
3068b662704SAnton Yartsev         // NoThrow placement new behaves as a standard new.
3078b662704SAnton Yartsev         IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t");
3088b662704SAnton Yartsev     }
3098b662704SAnton Yartsev     else
3108b662704SAnton Yartsev       // Placement forms are considered non-standard.
3118b662704SAnton Yartsev       IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1);
3128b662704SAnton Yartsev   }
3138b662704SAnton Yartsev 
3148b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
3158b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
3168b662704SAnton Yartsev   // a custom global allocator.
3178b662704SAnton Yartsev   if (IsStandardGlobalOpNewFunction)
3188b662704SAnton Yartsev     symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
3198b662704SAnton Yartsev   else
3208b662704SAnton Yartsev     symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, CNE->getType(),
3218b662704SAnton Yartsev                                           blockCount);
3228b662704SAnton Yartsev 
3238b662704SAnton Yartsev   ProgramStateRef State = Pred->getState();
324fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
325fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
326fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
327fcd016e5SJordan Rose 
328a4ee064cSJordan Rose   // Invalidate placement args.
329fcd016e5SJordan Rose   // FIXME: Once we figure out how we want allocators to work,
330fcd016e5SJordan Rose   // we should be using the usual pre-/(default-)eval-/post-call checks here.
331fcd016e5SJordan Rose   State = Call->invalidateRegions(blockCount);
332b8cb8359SJordan Rose   if (!State)
333b8cb8359SJordan Rose     return;
334a4ee064cSJordan Rose 
3353957fd58SJordan Rose   // If we're compiling with exceptions enabled, and this allocation function
3363957fd58SJordan Rose   // is not declared as non-throwing, failures /must/ be signalled by
3373957fd58SJordan Rose   // exceptions, and thus the return value will never be NULL.
3383957fd58SJordan Rose   // C++11 [basic.stc.dynamic.allocation]p3.
3393957fd58SJordan Rose   if (FD && getContext().getLangOpts().CXXExceptions) {
3403957fd58SJordan Rose     QualType Ty = FD->getType();
3413957fd58SJordan Rose     if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>())
3423957fd58SJordan Rose       if (!ProtoType->isNothrow(getContext()))
3433957fd58SJordan Rose         State = State->assume(symVal, true);
3443957fd58SJordan Rose   }
3453957fd58SJordan Rose 
346b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
347b8cb8359SJordan Rose 
34810e25031STed Kremenek   if (CNE->isArray()) {
34910e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
35010e25031STed Kremenek     // For now, just return a symbolicated region.
3512fdacbc5SDavid Blaikie     const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion();
352a4ee064cSJordan Rose     QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
353a4ee064cSJordan Rose     const ElementRegion *EleReg =
354a4ee064cSJordan Rose       getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
35544320367SJordan Rose     State = State->BindExpr(CNE, Pred->getLocationContext(),
356632e3b7eSTed Kremenek                             loc::MemRegionVal(EleReg));
35744320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
35810e25031STed Kremenek     return;
35910e25031STed Kremenek   }
36010e25031STed Kremenek 
361a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
362a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
363a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
364a4ee064cSJordan Rose   // the constructor call anyway.)
365b8cb8359SJordan Rose   SVal Result = symVal;
36644320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
36744320367SJordan Rose     // Non-array placement new should always return the placement location.
36844320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
369b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
370aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
37144320367SJordan Rose   }
37244320367SJordan Rose 
373b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
374b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
3758f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
3768f6b4b04SJordan Rose   if (!NewN)
3778f6b4b04SJordan Rose     return;
3783503cb35SJordan Rose 
3795089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
3805089f3b3SJordan Rose   // initializer. Copy the value over.
3815089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
3825089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
3833503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
3848f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
3855089f3b3SJordan Rose 
3863503cb35SJordan Rose       assert(!CNE->getType()->getPointeeCXXRecordDecl());
387b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
388b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
3893503cb35SJordan Rose     }
3903503cb35SJordan Rose   }
39110e25031STed Kremenek }
39210e25031STed Kremenek 
39310e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
39410e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
395d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
39664aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
39764aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
39810e25031STed Kremenek }
39910e25031STed Kremenek 
4008fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
4018fdb59f9STed Kremenek                                    ExplodedNode *Pred,
4028fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
4038fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
404b1279b5cSTed Kremenek   if (!VD) {
405b1279b5cSTed Kremenek     Dst.Add(Pred);
406b1279b5cSTed Kremenek     return;
407b1279b5cSTed Kremenek   }
408b1279b5cSTed Kremenek 
4098fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
410d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
411d94854a4STed Kremenek                                         currBldrCtx->blockCount());
4128fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
4138fdb59f9STed Kremenek   state = state->bindLoc(state->getLValue(VD, LCtx), V);
4148fdb59f9STed Kremenek 
415d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
4168fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
4178fdb59f9STed Kremenek }
4188fdb59f9STed Kremenek 
41910e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
42010e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
421d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
422e594034fSAnna Zaks 
42310e25031STed Kremenek   // Get the this object region from StoreManager.
424632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
42510e25031STed Kremenek   const MemRegion *R =
42610e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
42710e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
428632e3b7eSTed Kremenek                                                     LCtx);
42910e25031STed Kremenek 
43049b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
43110e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
432632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
43310e25031STed Kremenek }
434