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 
51*3d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core
52*3d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine.
5388bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
54*3d7b7f52SJordan Rose                                     const CallEvent &Call) {
55*3d7b7f52SJordan Rose   SVal ThisVal;
56*3d7b7f52SJordan Rose   bool AlwaysReturnsLValue;
57*3d7b7f52SJordan Rose   if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
58*3d7b7f52SJordan Rose     assert(Ctor->getDecl()->isTrivial());
59*3d7b7f52SJordan Rose     assert(Ctor->getDecl()->isCopyOrMoveConstructor());
60*3d7b7f52SJordan Rose     ThisVal = Ctor->getCXXThisVal();
61*3d7b7f52SJordan Rose     AlwaysReturnsLValue = false;
62*3d7b7f52SJordan Rose   } else {
63*3d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
64*3d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
65*3d7b7f52SJordan Rose            OO_Equal);
66*3d7b7f52SJordan Rose     ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
67*3d7b7f52SJordan Rose     AlwaysReturnsLValue = true;
68*3d7b7f52SJordan 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 
77*3d7b7f52SJordan Rose   // If the value being copied is not unknown, load from its location to get
78*3d7b7f52SJordan Rose   // an aggregate rvalue.
7905785d16SDavid Blaikie   if (Optional<Loc> L = V.getAs<Loc>())
8088bb563cSJordan Rose     V = Pred->getState()->getSVal(*L);
81*3d7b7f52SJordan Rose   else
82*3d7b7f52SJordan Rose     assert(V.isUnknown());
8388bb563cSJordan Rose 
84*3d7b7f52SJordan Rose   const Expr *CallExpr = Call.getOriginExpr();
85*3d7b7f52SJordan Rose   evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
8688bb563cSJordan Rose 
87*3d7b7f52SJordan 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();
91*3d7b7f52SJordan Rose     if (AlwaysReturnsLValue)
92*3d7b7f52SJordan Rose       State = State->BindExpr(CallExpr, LCtx, ThisVal);
93*3d7b7f52SJordan 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.
277d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
27810e25031STed Kremenek 
279d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
280d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
281d227833cSTed Kremenek   DefinedOrUnknownSVal symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx,
282d227833cSTed Kremenek                                                              CNE->getType(),
283d227833cSTed Kremenek                                                              blockCount);
28444320367SJordan Rose   ProgramStateRef State = Pred->getState();
28510e25031STed Kremenek 
286fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
287fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
288fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
289fcd016e5SJordan Rose 
290a4ee064cSJordan Rose   // Invalidate placement args.
291fcd016e5SJordan Rose   // FIXME: Once we figure out how we want allocators to work,
292fcd016e5SJordan Rose   // we should be using the usual pre-/(default-)eval-/post-call checks here.
293fcd016e5SJordan Rose   State = Call->invalidateRegions(blockCount);
294a4ee064cSJordan Rose 
2953957fd58SJordan Rose   // If we're compiling with exceptions enabled, and this allocation function
2963957fd58SJordan Rose   // is not declared as non-throwing, failures /must/ be signalled by
2973957fd58SJordan Rose   // exceptions, and thus the return value will never be NULL.
2983957fd58SJordan Rose   // C++11 [basic.stc.dynamic.allocation]p3.
2993957fd58SJordan Rose   FunctionDecl *FD = CNE->getOperatorNew();
3003957fd58SJordan Rose   if (FD && getContext().getLangOpts().CXXExceptions) {
3013957fd58SJordan Rose     QualType Ty = FD->getType();
3023957fd58SJordan Rose     if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>())
3033957fd58SJordan Rose       if (!ProtoType->isNothrow(getContext()))
3043957fd58SJordan Rose         State = State->assume(symVal, true);
3053957fd58SJordan Rose   }
3063957fd58SJordan Rose 
30710e25031STed Kremenek   if (CNE->isArray()) {
30810e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
30910e25031STed Kremenek     // For now, just return a symbolicated region.
3102fdacbc5SDavid Blaikie     const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion();
311a4ee064cSJordan Rose     QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
312a4ee064cSJordan Rose     const ElementRegion *EleReg =
313a4ee064cSJordan Rose       getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
31444320367SJordan Rose     State = State->BindExpr(CNE, Pred->getLocationContext(),
315632e3b7eSTed Kremenek                             loc::MemRegionVal(EleReg));
31644320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
31710e25031STed Kremenek     return;
31810e25031STed Kremenek   }
31910e25031STed Kremenek 
320a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
321a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
322a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
323a4ee064cSJordan Rose   // the constructor call anyway.)
32444320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
32544320367SJordan Rose     // Non-array placement new should always return the placement location.
32644320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
327aaf83184SJordan Rose     SVal Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
328aaf83184SJordan Rose                                        CNE->getPlacementArg(0)->getType());
329aaf83184SJordan Rose     State = State->BindExpr(CNE, LCtx, Result);
33044320367SJordan Rose   } else {
331a4ee064cSJordan Rose     State = State->BindExpr(CNE, LCtx, symVal);
33244320367SJordan Rose   }
33344320367SJordan Rose 
3345089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
3355089f3b3SJordan Rose   // initializer. Copy the value over.
3365089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
3375089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
3385089f3b3SJordan Rose       QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
3395089f3b3SJordan Rose       (void)ObjTy;
3405089f3b3SJordan Rose       assert(!ObjTy->isRecordType());
3415089f3b3SJordan Rose       SVal Location = State->getSVal(CNE, LCtx);
34205785d16SDavid Blaikie       if (Optional<Loc> LV = Location.getAs<Loc>())
3432fdacbc5SDavid Blaikie         State = State->bindLoc(*LV, State->getSVal(Init, LCtx));
3445089f3b3SJordan Rose     }
3455089f3b3SJordan Rose   }
3465089f3b3SJordan Rose 
34744320367SJordan Rose   Bldr.generateNode(CNE, Pred, State);
34810e25031STed Kremenek }
34910e25031STed Kremenek 
35010e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
35110e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
352d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
35364aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
35464aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
35510e25031STed Kremenek }
35610e25031STed Kremenek 
3578fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
3588fdb59f9STed Kremenek                                    ExplodedNode *Pred,
3598fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
3608fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
361b1279b5cSTed Kremenek   if (!VD) {
362b1279b5cSTed Kremenek     Dst.Add(Pred);
363b1279b5cSTed Kremenek     return;
364b1279b5cSTed Kremenek   }
365b1279b5cSTed Kremenek 
3668fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
367d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
368d94854a4STed Kremenek                                         currBldrCtx->blockCount());
3698fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
3708fdb59f9STed Kremenek   state = state->bindLoc(state->getLValue(VD, LCtx), V);
3718fdb59f9STed Kremenek 
372d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
3738fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
3748fdb59f9STed Kremenek }
3758fdb59f9STed Kremenek 
37610e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
37710e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
378d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
379e594034fSAnna Zaks 
38010e25031STed Kremenek   // Get the this object region from StoreManager.
381632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
38210e25031STed Kremenek   const MemRegion *R =
38310e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
38410e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
385632e3b7eSTed Kremenek                                                     LCtx);
38610e25031STed Kremenek 
38749b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
38810e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
389632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
39010e25031STed Kremenek }
391