110e25031STed Kremenek //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
210e25031STed Kremenek //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610e25031STed Kremenek //
710e25031STed Kremenek //===----------------------------------------------------------------------===//
810e25031STed Kremenek //
910e25031STed Kremenek //  This file defines the C++ expression evaluation engine.
1010e25031STed Kremenek //
1110e25031STed Kremenek //===----------------------------------------------------------------------===//
1210e25031STed Kremenek 
1310e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
144068481bSArtem Dergachev #include "clang/Analysis/ConstructionContext.h"
1510e25031STed Kremenek #include "clang/AST/DeclCXX.h"
168fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h"
17e8ba3ec4SArtem Dergachev #include "clang/AST/ParentMap.h"
1817a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h"
193a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h"
203a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
213a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
2210e25031STed Kremenek 
2310e25031STed Kremenek using namespace clang;
2410e25031STed Kremenek using namespace ento;
2510e25031STed Kremenek 
2610e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
2710e25031STed Kremenek                                           ExplodedNode *Pred,
2810e25031STed Kremenek                                           ExplodedNodeSet &Dst) {
29d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
30b0561b33STyker   const Expr *tempExpr = ME->getSubExpr()->IgnoreParens();
3149b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
32632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
3310e25031STed Kremenek 
34861a1740SJordan Rose   state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME);
355772f82dSJordan Rose   Bldr.generateNode(ME, Pred, state);
3610e25031STed Kremenek }
3710e25031STed Kremenek 
383d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core
393d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine.
4088bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
413d7b7f52SJordan Rose                                     const CallEvent &Call) {
423d7b7f52SJordan Rose   SVal ThisVal;
433d7b7f52SJordan Rose   bool AlwaysReturnsLValue;
444449e7f0SArtem Dergachev   const CXXRecordDecl *ThisRD = nullptr;
453d7b7f52SJordan Rose   if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
463d7b7f52SJordan Rose     assert(Ctor->getDecl()->isTrivial());
473d7b7f52SJordan Rose     assert(Ctor->getDecl()->isCopyOrMoveConstructor());
483d7b7f52SJordan Rose     ThisVal = Ctor->getCXXThisVal();
494449e7f0SArtem Dergachev     ThisRD = Ctor->getDecl()->getParent();
503d7b7f52SJordan Rose     AlwaysReturnsLValue = false;
513d7b7f52SJordan Rose   } else {
523d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
533d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
543d7b7f52SJordan Rose            OO_Equal);
553d7b7f52SJordan Rose     ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
564449e7f0SArtem Dergachev     ThisRD = cast<CXXMethodDecl>(Call.getDecl())->getParent();
573d7b7f52SJordan Rose     AlwaysReturnsLValue = true;
583d7b7f52SJordan Rose   }
5988bb563cSJordan Rose 
604449e7f0SArtem Dergachev   assert(ThisRD);
614449e7f0SArtem Dergachev   if (ThisRD->isEmpty()) {
624449e7f0SArtem Dergachev     // Do nothing for empty classes. Otherwise it'd retrieve an UnknownVal
634449e7f0SArtem Dergachev     // and bind it and RegionStore would think that the actual value
644449e7f0SArtem Dergachev     // in this region at this offset is unknown.
654449e7f0SArtem Dergachev     return;
664449e7f0SArtem Dergachev   }
674449e7f0SArtem Dergachev 
6888bb563cSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
6988bb563cSJordan Rose 
7088bb563cSJordan Rose   ExplodedNodeSet Dst;
7188bb563cSJordan Rose   Bldr.takeNodes(Pred);
7288bb563cSJordan Rose 
7388bb563cSJordan Rose   SVal V = Call.getArgSVal(0);
7488bb563cSJordan Rose 
753d7b7f52SJordan Rose   // If the value being copied is not unknown, load from its location to get
763d7b7f52SJordan Rose   // an aggregate rvalue.
7705785d16SDavid Blaikie   if (Optional<Loc> L = V.getAs<Loc>())
7888bb563cSJordan Rose     V = Pred->getState()->getSVal(*L);
793d7b7f52SJordan Rose   else
8075f9d3acSArtem Dergachev     assert(V.isUnknownOrUndef());
8188bb563cSJordan Rose 
823d7b7f52SJordan Rose   const Expr *CallExpr = Call.getOriginExpr();
833d7b7f52SJordan Rose   evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
8488bb563cSJordan Rose 
853d7b7f52SJordan Rose   PostStmt PS(CallExpr, LCtx);
8688bb563cSJordan Rose   for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
8788bb563cSJordan Rose        I != E; ++I) {
8888bb563cSJordan Rose     ProgramStateRef State = (*I)->getState();
893d7b7f52SJordan Rose     if (AlwaysReturnsLValue)
903d7b7f52SJordan Rose       State = State->BindExpr(CallExpr, LCtx, ThisVal);
913d7b7f52SJordan Rose     else
9288bb563cSJordan Rose       State = bindReturnValue(Call, LCtx, State);
9388bb563cSJordan Rose     Bldr.generateNode(PS, State, *I);
9488bb563cSJordan Rose   }
9588bb563cSJordan Rose }
9688bb563cSJordan Rose 
97bc74eb1cSJordan Rose 
98168e29f6SArtem Dergachev SVal ExprEngine::makeZeroElementRegion(ProgramStateRef State, SVal LValue,
99690ab040SArtem Dergachev                                        QualType &Ty, bool &IsArray) {
100bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
101bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
102bc74eb1cSJordan Rose 
103bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
104bc74eb1cSJordan Rose     Ty = AT->getElementType();
105bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
106690ab040SArtem Dergachev     IsArray = true;
107bc74eb1cSJordan Rose   }
108bc74eb1cSJordan Rose 
109bc74eb1cSJordan Rose   return LValue;
110bc74eb1cSJordan Rose }
111bc74eb1cSJordan Rose 
112813734daSAdam Balogh SVal ExprEngine::computeObjectUnderConstruction(
113239452caSArtem Dergachev     const Expr *E, ProgramStateRef State, const LocationContext *LCtx,
114239452caSArtem Dergachev     const ConstructionContext *CC, EvalCallOptions &CallOpts) {
11517906498SArtem Dergachev   SValBuilder &SVB = getSValBuilder();
11617906498SArtem Dergachev   MemRegionManager &MRMgr = SVB.getRegionManager();
11717906498SArtem Dergachev   ASTContext &ACtx = SVB.getContext();
11805375eb4SJordan Rose 
119813734daSAdam Balogh   // Compute the target region by exploring the construction context.
120661ab34aSArtem Dergachev   if (CC) {
1214068481bSArtem Dergachev     switch (CC->getKind()) {
12253b8ce0eSArtem Dergachev     case ConstructionContext::CXX17ElidedCopyVariableKind:
1234068481bSArtem Dergachev     case ConstructionContext::SimpleVariableKind: {
12453b8ce0eSArtem Dergachev       const auto *DSCC = cast<VariableConstructionContext>(CC);
1254068481bSArtem Dergachev       const auto *DS = DSCC->getDeclStmt();
126e231bd34SArtem Dergachev       const auto *Var = cast<VarDecl>(DS->getSingleDecl());
12720edae87SJordan Rose       QualType Ty = Var->getType();
128813734daSAdam Balogh       return makeZeroElementRegion(State, State->getLValue(Var, LCtx), Ty,
129813734daSAdam Balogh                                    CallOpts.IsArrayCtorOrDtor);
13020edae87SJordan Rose     }
13153b8ce0eSArtem Dergachev     case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind:
132922455feSArtem Dergachev     case ConstructionContext::SimpleConstructorInitializerKind: {
1334068481bSArtem Dergachev       const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC);
1344068481bSArtem Dergachev       const auto *Init = ICC->getCXXCtorInitializer();
13554529a34SJordan Rose       assert(Init->isAnyMemberInitializer());
13654529a34SJordan Rose       const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
137813734daSAdam Balogh       Loc ThisPtr = SVB.getCXXThis(CurCtor, LCtx->getStackFrame());
13854529a34SJordan Rose       SVal ThisVal = State->getSVal(ThisPtr);
13954529a34SJordan Rose 
140bc74eb1cSJordan Rose       const ValueDecl *Field;
141bc74eb1cSJordan Rose       SVal FieldVal;
14254529a34SJordan Rose       if (Init->isIndirectMemberInitializer()) {
143bc74eb1cSJordan Rose         Field = Init->getIndirectMember();
144bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
14554529a34SJordan Rose       } else {
146bc74eb1cSJordan Rose         Field = Init->getMember();
147bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getMember(), ThisVal);
14854529a34SJordan Rose       }
149bc74eb1cSJordan Rose 
150bc74eb1cSJordan Rose       QualType Ty = Field->getType();
151813734daSAdam Balogh       return makeZeroElementRegion(State, FieldVal, Ty,
152168e29f6SArtem Dergachev                                    CallOpts.IsArrayCtorOrDtor);
15354529a34SJordan Rose     }
1544068481bSArtem Dergachev     case ConstructionContext::NewAllocatedObjectKind: {
155549f9cd4SKristof Umann       if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
1564068481bSArtem Dergachev         const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC);
1574068481bSArtem Dergachev         const auto *NE = NECC->getCXXNewExpr();
158f83d5479SArtem Dergachev         SVal V = *getObjectUnderConstruction(State, NE, LCtx);
159f83d5479SArtem Dergachev         if (const SubRegion *MR =
160f83d5479SArtem Dergachev                 dyn_cast_or_null<SubRegion>(V.getAsRegion())) {
1614068481bSArtem Dergachev           if (NE->isArray()) {
1624068481bSArtem Dergachev             // TODO: In fact, we need to call the constructor for every
1634068481bSArtem Dergachev             // allocated element, not just the first one!
1644068481bSArtem Dergachev             CallOpts.IsArrayCtorOrDtor = true;
165813734daSAdam Balogh             return loc::MemRegionVal(getStoreManager().GetElementZeroRegion(
166813734daSAdam Balogh                 MR, NE->getType()->getPointeeType()));
1674068481bSArtem Dergachev           }
168813734daSAdam Balogh           return  V;
1694068481bSArtem Dergachev         }
170f83d5479SArtem Dergachev         // TODO: Detect when the allocator returns a null pointer.
171f83d5479SArtem Dergachev         // Constructor shall not be called in this case.
1724068481bSArtem Dergachev       }
1734068481bSArtem Dergachev       break;
1744068481bSArtem Dergachev     }
175f28d7f17SArtem Dergachev     case ConstructionContext::SimpleReturnedValueKind:
176f28d7f17SArtem Dergachev     case ConstructionContext::CXX17ElidedCopyReturnedValueKind: {
177239452caSArtem Dergachev       // The temporary is to be managed by the parent stack frame.
178239452caSArtem Dergachev       // So build it in the parent stack frame if we're not in the
179239452caSArtem Dergachev       // top frame of the analysis.
180dd18b11bSGeorge Karpenkov       const StackFrameContext *SFC = LCtx->getStackFrame();
181239452caSArtem Dergachev       if (const LocationContext *CallerLCtx = SFC->getParent()) {
182239452caSArtem Dergachev         auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()]
183239452caSArtem Dergachev                        .getAs<CFGCXXRecordTypedCall>();
184239452caSArtem Dergachev         if (!RTC) {
185239452caSArtem Dergachev           // We were unable to find the correct construction context for the
186239452caSArtem Dergachev           // call in the parent stack frame. This is equivalent to not being
187239452caSArtem Dergachev           // able to find construction context at all.
188239452caSArtem Dergachev           break;
189239452caSArtem Dergachev         }
190b3fc9df4SArtem Dergachev         if (isa<BlockInvocationContext>(CallerLCtx)) {
191b3fc9df4SArtem Dergachev           // Unwrap block invocation contexts. They're mostly part of
192b3fc9df4SArtem Dergachev           // the current stack frame.
193b3fc9df4SArtem Dergachev           CallerLCtx = CallerLCtx->getParent();
194b3fc9df4SArtem Dergachev           assert(!isa<BlockInvocationContext>(CallerLCtx));
195b3fc9df4SArtem Dergachev         }
196813734daSAdam Balogh         return computeObjectUnderConstruction(
197f28d7f17SArtem Dergachev             cast<Expr>(SFC->getCallSite()), State, CallerLCtx,
198f28d7f17SArtem Dergachev             RTC->getConstructionContext(), CallOpts);
199239452caSArtem Dergachev       } else {
20017906498SArtem Dergachev         // We are on the top frame of the analysis. We do not know where is the
20117906498SArtem Dergachev         // object returned to. Conjure a symbolic region for the return value.
20217906498SArtem Dergachev         // TODO: We probably need a new MemRegion kind to represent the storage
20317906498SArtem Dergachev         // of that SymbolicRegion, so that we cound produce a fancy symbol
20417906498SArtem Dergachev         // instead of an anonymous conjured symbol.
20517906498SArtem Dergachev         // TODO: Do we need to track the region to avoid having it dead
20617906498SArtem Dergachev         // too early? It does die too early, at least in C++17, but because
20717906498SArtem Dergachev         // putting anything into a SymbolicRegion causes an immediate escape,
20817906498SArtem Dergachev         // it doesn't cause any leak false positives.
20917906498SArtem Dergachev         const auto *RCC = cast<ReturnedValueConstructionContext>(CC);
21017906498SArtem Dergachev         // Make sure that this doesn't coincide with any other symbol
21117906498SArtem Dergachev         // conjured for the returned expression.
21217906498SArtem Dergachev         static const int TopLevelSymRegionTag = 0;
21317906498SArtem Dergachev         const Expr *RetE = RCC->getReturnStmt()->getRetValue();
21417906498SArtem Dergachev         assert(RetE && "Void returns should not have a construction context");
21517906498SArtem Dergachev         QualType ReturnTy = RetE->getType();
21617906498SArtem Dergachev         QualType RegionTy = ACtx.getPointerType(ReturnTy);
217813734daSAdam Balogh         return SVB.conjureSymbolVal(&TopLevelSymRegionTag, RetE, SFC, RegionTy,
218813734daSAdam Balogh                                     currBldrCtx->blockCount());
219239452caSArtem Dergachev       }
220f28d7f17SArtem Dergachev       llvm_unreachable("Unhandled return value construction context!");
221239452caSArtem Dergachev     }
2229a209ad1SArtem Dergachev     case ConstructionContext::ElidedTemporaryObjectKind: {
223549f9cd4SKristof Umann       assert(AMgr.getAnalyzerOptions().ShouldElideConstructors);
2249a209ad1SArtem Dergachev       const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC);
2259a209ad1SArtem Dergachev 
2269a209ad1SArtem Dergachev       // Support pre-C++17 copy elision. We'll have the elidable copy
2279a209ad1SArtem Dergachev       // constructor in the AST and in the CFG, but we'll skip it
2289a209ad1SArtem Dergachev       // and construct directly into the final object. This call
2299a209ad1SArtem Dergachev       // also sets the CallOpts flags for us.
230a657a32cSArtem Dergachev       // If the elided copy/move constructor is not supported, there's still
231a657a32cSArtem Dergachev       // benefit in trying to model the non-elided constructor.
232a657a32cSArtem Dergachev       // Stash our state before trying to elide, as it'll get overwritten.
233a657a32cSArtem Dergachev       ProgramStateRef PreElideState = State;
234a657a32cSArtem Dergachev       EvalCallOptions PreElideCallOpts = CallOpts;
235a657a32cSArtem Dergachev 
236813734daSAdam Balogh       SVal V = computeObjectUnderConstruction(
237813734daSAdam Balogh           TCC->getConstructorAfterElision(), State, LCtx,
238813734daSAdam Balogh           TCC->getConstructionContextAfterElision(), CallOpts);
2399a209ad1SArtem Dergachev 
240a657a32cSArtem Dergachev       // FIXME: This definition of "copy elision has not failed" is unreliable.
241a657a32cSArtem Dergachev       // It doesn't indicate that the constructor will actually be inlined
242813734daSAdam Balogh       // later; this is still up to evalCall() to decide.
243813734daSAdam Balogh       if (!CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion)
244813734daSAdam Balogh         return V;
2459a209ad1SArtem Dergachev 
246a657a32cSArtem Dergachev       // Copy elision failed. Revert the changes and proceed as if we have
247a657a32cSArtem Dergachev       // a simple temporary.
248a657a32cSArtem Dergachev       CallOpts = PreElideCallOpts;
249813734daSAdam Balogh       CallOpts.IsElidableCtorThatHasNotBeenElided = true;
250080b8709SSimon Pilgrim       LLVM_FALLTHROUGH;
2519a209ad1SArtem Dergachev     }
252ff267df0SArtem Dergachev     case ConstructionContext::SimpleTemporaryObjectKind: {
253a657a32cSArtem Dergachev       const auto *TCC = cast<TemporaryObjectConstructionContext>(CC);
254239452caSArtem Dergachev       const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr();
255239452caSArtem Dergachev 
256813734daSAdam Balogh       CallOpts.IsTemporaryCtorOrDtor = true;
257239452caSArtem Dergachev       if (MTE) {
2588cd7961aSArtem Dergachev         if (const ValueDecl *VD = MTE->getExtendingDecl()) {
259643102dfSArtem Dergachev           assert(MTE->getStorageDuration() != SD_FullExpression);
260643102dfSArtem Dergachev           if (!VD->getType()->isReferenceType()) {
261643102dfSArtem Dergachev             // We're lifetime-extended by a surrounding aggregate.
262643102dfSArtem Dergachev             // Automatic destructors aren't quite working in this case
263643102dfSArtem Dergachev             // on the CFG side. We should warn the caller about that.
264310bca01SArtem Dergachev             // FIXME: Is there a better way to retrieve this information from
265310bca01SArtem Dergachev             // the MaterializeTemporaryExpr?
266310bca01SArtem Dergachev             CallOpts.IsTemporaryLifetimeExtendedViaAggregate = true;
267310bca01SArtem Dergachev           }
2688cd7961aSArtem Dergachev         }
2694fb586c3SArtem Dergachev 
2704fb586c3SArtem Dergachev         if (MTE->getStorageDuration() == SD_Static ||
2714fb586c3SArtem Dergachev             MTE->getStorageDuration() == SD_Thread)
272813734daSAdam Balogh           return loc::MemRegionVal(MRMgr.getCXXStaticTempObjectRegion(E));
273239452caSArtem Dergachev       }
274239452caSArtem Dergachev 
275813734daSAdam Balogh       return loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
2764068481bSArtem Dergachev     }
277a657a32cSArtem Dergachev     case ConstructionContext::ArgumentKind: {
2783ccf14ebSArtem Dergachev       // Arguments are technically temporaries.
2793ccf14ebSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
2803ccf14ebSArtem Dergachev 
2813ccf14ebSArtem Dergachev       const auto *ACC = cast<ArgumentConstructionContext>(CC);
2823ccf14ebSArtem Dergachev       const Expr *E = ACC->getCallLikeExpr();
2833ccf14ebSArtem Dergachev       unsigned Idx = ACC->getIndex();
2843ccf14ebSArtem Dergachev 
2853ccf14ebSArtem Dergachev       CallEventManager &CEMgr = getStateManager().getCallEventManager();
2863ccf14ebSArtem Dergachev       auto getArgLoc = [&](CallEventRef<> Caller) -> Optional<SVal> {
2877740c6d6SCsaba Dabis         const LocationContext *FutureSFC =
2887740c6d6SCsaba Dabis             Caller->getCalleeStackFrame(currBldrCtx->blockCount());
2893ccf14ebSArtem Dergachev         // Return early if we are unable to reliably foresee
2903ccf14ebSArtem Dergachev         // the future stack frame.
2913ccf14ebSArtem Dergachev         if (!FutureSFC)
2923ccf14ebSArtem Dergachev           return None;
2933ccf14ebSArtem Dergachev 
2943ccf14ebSArtem Dergachev         // This should be equivalent to Caller->getDecl() for now, but
2953ccf14ebSArtem Dergachev         // FutureSFC->getDecl() is likely to support better stuff (like
2963ccf14ebSArtem Dergachev         // virtual functions) earlier.
2973ccf14ebSArtem Dergachev         const Decl *CalleeD = FutureSFC->getDecl();
2983ccf14ebSArtem Dergachev 
2993ccf14ebSArtem Dergachev         // FIXME: Support for variadic arguments is not implemented here yet.
3003ccf14ebSArtem Dergachev         if (CallEvent::isVariadic(CalleeD))
3013ccf14ebSArtem Dergachev           return None;
3023ccf14ebSArtem Dergachev 
3033ccf14ebSArtem Dergachev         // Operator arguments do not correspond to operator parameters
3043ccf14ebSArtem Dergachev         // because this-argument is implemented as a normal argument in
3053ccf14ebSArtem Dergachev         // operator call expressions but not in operator declarations.
30698db1f99SAdam Balogh         const TypedValueRegion *TVR = Caller->getParameterLocation(
3077740c6d6SCsaba Dabis             *Caller->getAdjustedParameterIndex(Idx), currBldrCtx->blockCount());
30898db1f99SAdam Balogh         if (!TVR)
3093ccf14ebSArtem Dergachev           return None;
3103ccf14ebSArtem Dergachev 
31198db1f99SAdam Balogh         return loc::MemRegionVal(TVR);
3123ccf14ebSArtem Dergachev       };
3133ccf14ebSArtem Dergachev 
3143ccf14ebSArtem Dergachev       if (const auto *CE = dyn_cast<CallExpr>(E)) {
3153ccf14ebSArtem Dergachev         CallEventRef<> Caller = CEMgr.getSimpleCall(CE, State, LCtx);
316813734daSAdam Balogh         if (Optional<SVal> V = getArgLoc(Caller))
317813734daSAdam Balogh           return *V;
3183ccf14ebSArtem Dergachev         else
319a657a32cSArtem Dergachev           break;
3203ccf14ebSArtem Dergachev       } else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) {
3213ccf14ebSArtem Dergachev         // Don't bother figuring out the target region for the future
3223ccf14ebSArtem Dergachev         // constructor because we won't need it.
3233ccf14ebSArtem Dergachev         CallEventRef<> Caller =
3243ccf14ebSArtem Dergachev             CEMgr.getCXXConstructorCall(CCE, /*Target=*/nullptr, State, LCtx);
325813734daSAdam Balogh         if (Optional<SVal> V = getArgLoc(Caller))
326813734daSAdam Balogh           return *V;
3273ccf14ebSArtem Dergachev         else
3283ccf14ebSArtem Dergachev           break;
3293ccf14ebSArtem Dergachev       } else if (const auto *ME = dyn_cast<ObjCMessageExpr>(E)) {
3303ccf14ebSArtem Dergachev         CallEventRef<> Caller = CEMgr.getObjCMethodCall(ME, State, LCtx);
331813734daSAdam Balogh         if (Optional<SVal> V = getArgLoc(Caller))
332813734daSAdam Balogh           return *V;
3333ccf14ebSArtem Dergachev         else
3343ccf14ebSArtem Dergachev           break;
335813734daSAdam Balogh       }
336813734daSAdam Balogh     }
337813734daSAdam Balogh     } // switch (CC->getKind())
3383ccf14ebSArtem Dergachev   }
3393ccf14ebSArtem Dergachev 
340c9318304SJordan Rose   // If we couldn't find an existing region to construct into, assume we're
341690ab040SArtem Dergachev   // constructing a temporary. Notify the caller of our failure.
342168e29f6SArtem Dergachev   CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
343813734daSAdam Balogh   return loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
344813734daSAdam Balogh }
345813734daSAdam Balogh 
346813734daSAdam Balogh ProgramStateRef ExprEngine::updateObjectsUnderConstruction(
347813734daSAdam Balogh     SVal V, const Expr *E, ProgramStateRef State, const LocationContext *LCtx,
348813734daSAdam Balogh     const ConstructionContext *CC, const EvalCallOptions &CallOpts) {
349813734daSAdam Balogh   if (CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion) {
350813734daSAdam Balogh     // Sounds like we failed to find the target region and therefore
351813734daSAdam Balogh     // copy elision failed. There's nothing we can do about it here.
352813734daSAdam Balogh     return State;
353813734daSAdam Balogh   }
354813734daSAdam Balogh 
355813734daSAdam Balogh   // See if we're constructing an existing region by looking at the
356813734daSAdam Balogh   // current construction context.
357813734daSAdam Balogh   assert(CC && "Computed target region without construction context?");
358813734daSAdam Balogh   switch (CC->getKind()) {
359813734daSAdam Balogh   case ConstructionContext::CXX17ElidedCopyVariableKind:
360813734daSAdam Balogh   case ConstructionContext::SimpleVariableKind: {
361813734daSAdam Balogh     const auto *DSCC = cast<VariableConstructionContext>(CC);
362813734daSAdam Balogh     return addObjectUnderConstruction(State, DSCC->getDeclStmt(), LCtx, V);
363813734daSAdam Balogh     }
364813734daSAdam Balogh     case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind:
365813734daSAdam Balogh     case ConstructionContext::SimpleConstructorInitializerKind: {
366813734daSAdam Balogh       const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC);
367813734daSAdam Balogh       return addObjectUnderConstruction(State, ICC->getCXXCtorInitializer(),
368813734daSAdam Balogh                                         LCtx, V);
369813734daSAdam Balogh     }
370813734daSAdam Balogh     case ConstructionContext::NewAllocatedObjectKind: {
371813734daSAdam Balogh       return State;
372813734daSAdam Balogh     }
373813734daSAdam Balogh     case ConstructionContext::SimpleReturnedValueKind:
374813734daSAdam Balogh     case ConstructionContext::CXX17ElidedCopyReturnedValueKind: {
375813734daSAdam Balogh       const StackFrameContext *SFC = LCtx->getStackFrame();
376813734daSAdam Balogh       const LocationContext *CallerLCtx = SFC->getParent();
377813734daSAdam Balogh       if (!CallerLCtx) {
378813734daSAdam Balogh         // No extra work is necessary in top frame.
379813734daSAdam Balogh         return State;
380813734daSAdam Balogh       }
381813734daSAdam Balogh 
382813734daSAdam Balogh       auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()]
383813734daSAdam Balogh                      .getAs<CFGCXXRecordTypedCall>();
384813734daSAdam Balogh       assert(RTC && "Could not have had a target region without it");
385813734daSAdam Balogh       if (isa<BlockInvocationContext>(CallerLCtx)) {
386813734daSAdam Balogh         // Unwrap block invocation contexts. They're mostly part of
387813734daSAdam Balogh         // the current stack frame.
388813734daSAdam Balogh         CallerLCtx = CallerLCtx->getParent();
389813734daSAdam Balogh         assert(!isa<BlockInvocationContext>(CallerLCtx));
390813734daSAdam Balogh       }
391813734daSAdam Balogh 
392813734daSAdam Balogh       return updateObjectsUnderConstruction(V,
393813734daSAdam Balogh           cast<Expr>(SFC->getCallSite()), State, CallerLCtx,
394813734daSAdam Balogh           RTC->getConstructionContext(), CallOpts);
395813734daSAdam Balogh     }
396813734daSAdam Balogh     case ConstructionContext::ElidedTemporaryObjectKind: {
397813734daSAdam Balogh       assert(AMgr.getAnalyzerOptions().ShouldElideConstructors);
398813734daSAdam Balogh       if (!CallOpts.IsElidableCtorThatHasNotBeenElided) {
399813734daSAdam Balogh         const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC);
400813734daSAdam Balogh         State = updateObjectsUnderConstruction(
401813734daSAdam Balogh             V, TCC->getConstructorAfterElision(), State, LCtx,
402813734daSAdam Balogh             TCC->getConstructionContextAfterElision(), CallOpts);
403813734daSAdam Balogh 
404813734daSAdam Balogh         // Remember that we've elided the constructor.
405813734daSAdam Balogh         State = addObjectUnderConstruction(
406813734daSAdam Balogh             State, TCC->getConstructorAfterElision(), LCtx, V);
407813734daSAdam Balogh 
408813734daSAdam Balogh         // Remember that we've elided the destructor.
409813734daSAdam Balogh         if (const auto *BTE = TCC->getCXXBindTemporaryExpr())
410813734daSAdam Balogh           State = elideDestructor(State, BTE, LCtx);
411813734daSAdam Balogh 
412813734daSAdam Balogh         // Instead of materialization, shamelessly return
413813734daSAdam Balogh         // the final object destination.
414813734daSAdam Balogh         if (const auto *MTE = TCC->getMaterializedTemporaryExpr())
415813734daSAdam Balogh           State = addObjectUnderConstruction(State, MTE, LCtx, V);
416813734daSAdam Balogh 
417813734daSAdam Balogh         return State;
418813734daSAdam Balogh       }
419813734daSAdam Balogh       // If we decided not to elide the constructor, proceed as if
420813734daSAdam Balogh       // it's a simple temporary.
421813734daSAdam Balogh       LLVM_FALLTHROUGH;
422813734daSAdam Balogh     }
423813734daSAdam Balogh     case ConstructionContext::SimpleTemporaryObjectKind: {
424813734daSAdam Balogh       const auto *TCC = cast<TemporaryObjectConstructionContext>(CC);
425813734daSAdam Balogh       if (const auto *BTE = TCC->getCXXBindTemporaryExpr())
426813734daSAdam Balogh         State = addObjectUnderConstruction(State, BTE, LCtx, V);
427813734daSAdam Balogh 
428813734daSAdam Balogh       if (const auto *MTE = TCC->getMaterializedTemporaryExpr())
429813734daSAdam Balogh         State = addObjectUnderConstruction(State, MTE, LCtx, V);
430813734daSAdam Balogh 
431813734daSAdam Balogh       return State;
432813734daSAdam Balogh     }
433813734daSAdam Balogh     case ConstructionContext::ArgumentKind: {
434813734daSAdam Balogh       const auto *ACC = cast<ArgumentConstructionContext>(CC);
435813734daSAdam Balogh       if (const auto *BTE = ACC->getCXXBindTemporaryExpr())
436813734daSAdam Balogh         State = addObjectUnderConstruction(State, BTE, LCtx, V);
437813734daSAdam Balogh 
438813734daSAdam Balogh       return addObjectUnderConstruction(
439813734daSAdam Balogh           State, {ACC->getCallLikeExpr(), ACC->getIndex()}, LCtx, V);
440813734daSAdam Balogh     }
441813734daSAdam Balogh   }
442813734daSAdam Balogh   llvm_unreachable("Unhandled construction context!");
443c9318304SJordan Rose }
44405375eb4SJordan Rose 
445a82ffe9dSArtem Dergachev void ExprEngine::handleConstructor(const Expr *E,
44617f41608SJordan Rose                                    ExplodedNode *Pred,
44717f41608SJordan Rose                                    ExplodedNodeSet &destNodes) {
448a82ffe9dSArtem Dergachev   const auto *CE = dyn_cast<CXXConstructExpr>(E);
449a82ffe9dSArtem Dergachev   const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(E);
450a82ffe9dSArtem Dergachev   assert(CE || CIE);
451a82ffe9dSArtem Dergachev 
45217f41608SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
45317f41608SJordan Rose   ProgramStateRef State = Pred->getState();
45417f41608SJordan Rose 
455f83d5479SArtem Dergachev   SVal Target = UnknownVal();
45617f41608SJordan Rose 
457a82ffe9dSArtem Dergachev   if (CE) {
4589a209ad1SArtem Dergachev     if (Optional<SVal> ElidedTarget =
4599a209ad1SArtem Dergachev             getObjectUnderConstruction(State, CE, LCtx)) {
460a82ffe9dSArtem Dergachev       // We've previously modeled an elidable constructor by pretending that it
461a82ffe9dSArtem Dergachev       // in fact constructs into the correct target. This constructor can
462a82ffe9dSArtem Dergachev       // therefore be skipped.
4639a209ad1SArtem Dergachev       Target = *ElidedTarget;
4649a209ad1SArtem Dergachev       StmtNodeBuilder Bldr(Pred, destNodes, *currBldrCtx);
4659a209ad1SArtem Dergachev       State = finishObjectConstruction(State, CE, LCtx);
4669a209ad1SArtem Dergachev       if (auto L = Target.getAs<Loc>())
4679a209ad1SArtem Dergachev         State = State->BindExpr(CE, LCtx, State->getSVal(*L, CE->getType()));
4689a209ad1SArtem Dergachev       Bldr.generateNode(CE, Pred, State);
4699a209ad1SArtem Dergachev       return;
4709a209ad1SArtem Dergachev     }
471a82ffe9dSArtem Dergachev   }
4729a209ad1SArtem Dergachev 
47317f41608SJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
47417f41608SJordan Rose   // For now, we just run the first constructor (which should still invalidate
47517f41608SJordan Rose   // the entire array).
47617f41608SJordan Rose 
477690ab040SArtem Dergachev   EvalCallOptions CallOpts;
478661ab34aSArtem Dergachev   auto C = getCurrentCFGElement().getAs<CFGConstructor>();
4794068481bSArtem Dergachev   assert(C || getCurrentCFGElement().getAs<CFGStmt>());
480661ab34aSArtem Dergachev   const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr;
481661ab34aSArtem Dergachev 
482a82ffe9dSArtem Dergachev   const CXXConstructExpr::ConstructionKind CK =
483a82ffe9dSArtem Dergachev       CE ? CE->getConstructionKind() : CIE->getConstructionKind();
484a82ffe9dSArtem Dergachev   switch (CK) {
48517f41608SJordan Rose   case CXXConstructExpr::CK_Complete: {
486a82ffe9dSArtem Dergachev     // Inherited constructors are always base class constructors.
487a82ffe9dSArtem Dergachev     assert(CE && !CIE && "A complete constructor is inherited?!");
488a82ffe9dSArtem Dergachev 
489a82ffe9dSArtem Dergachev     // The target region is found from construction context.
490239452caSArtem Dergachev     std::tie(State, Target) =
491a82ffe9dSArtem Dergachev         handleConstructionContext(CE, State, LCtx, CC, CallOpts);
492b970505dSJordan Rose     break;
493b970505dSJordan Rose   }
494192a7474SArtem Dergachev   case CXXConstructExpr::CK_VirtualBase: {
495b3b976f0SJordan Rose     // Make sure we are not calling virtual base class initializers twice.
496b3b976f0SJordan Rose     // Only the most-derived object should initialize virtual base classes.
497192a7474SArtem Dergachev     const auto *OuterCtor = dyn_cast_or_null<CXXConstructExpr>(
498192a7474SArtem Dergachev         LCtx->getStackFrame()->getCallSite());
499192a7474SArtem Dergachev     assert(
500192a7474SArtem Dergachev         (!OuterCtor ||
501192a7474SArtem Dergachev          OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Complete ||
502192a7474SArtem Dergachev          OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Delegating) &&
503192a7474SArtem Dergachev         ("This virtual base should have already been initialized by "
504192a7474SArtem Dergachev          "the most derived class!"));
505192a7474SArtem Dergachev     (void)OuterCtor;
5064dc0b1acSReid Kleckner     LLVM_FALLTHROUGH;
507192a7474SArtem Dergachev   }
508b3b976f0SJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
509e8ba3ec4SArtem Dergachev     // In C++17, classes with non-virtual bases may be aggregates, so they would
510e8ba3ec4SArtem Dergachev     // be initialized as aggregates without a constructor call, so we may have
511e8ba3ec4SArtem Dergachev     // a base class constructed directly into an initializer list without
512e8ba3ec4SArtem Dergachev     // having the derived-class constructor call on the previous stack frame.
513e8ba3ec4SArtem Dergachev     // Initializer lists may be nested into more initializer lists that
514e8ba3ec4SArtem Dergachev     // correspond to surrounding aggregate initializations.
515e8ba3ec4SArtem Dergachev     // FIXME: For now this code essentially bails out. We need to find the
516e8ba3ec4SArtem Dergachev     // correct target region and set it.
517e8ba3ec4SArtem Dergachev     // FIXME: Instead of relying on the ParentMap, we should have the
518e8ba3ec4SArtem Dergachev     // trigger-statement (InitListExpr in this case) passed down from CFG or
519e8ba3ec4SArtem Dergachev     // otherwise always available during construction.
520a82ffe9dSArtem Dergachev     if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(E))) {
521e8ba3ec4SArtem Dergachev       MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
522a82ffe9dSArtem Dergachev       Target = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
523168e29f6SArtem Dergachev       CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
524e8ba3ec4SArtem Dergachev       break;
525e8ba3ec4SArtem Dergachev     }
5264dc0b1acSReid Kleckner     LLVM_FALLTHROUGH;
527b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
528b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
529b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
530dd18b11bSGeorge Karpenkov                                               LCtx->getStackFrame());
53105375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
532b970505dSJordan Rose 
533a82ffe9dSArtem Dergachev     if (CK == CXXConstructExpr::CK_Delegating) {
534f83d5479SArtem Dergachev       Target = ThisVal;
535b970505dSJordan Rose     } else {
536b970505dSJordan Rose       // Cast to the base type.
537a82ffe9dSArtem Dergachev       bool IsVirtual = (CK == CXXConstructExpr::CK_VirtualBase);
538a82ffe9dSArtem Dergachev       SVal BaseVal =
539a82ffe9dSArtem Dergachev           getStoreManager().evalDerivedToBase(ThisVal, E->getType(), IsVirtual);
540f83d5479SArtem Dergachev       Target = BaseVal;
541b970505dSJordan Rose     }
542b970505dSJordan Rose     break;
543b970505dSJordan Rose   }
544b970505dSJordan Rose   }
545b970505dSJordan Rose 
546239452caSArtem Dergachev   if (State != Pred->getState()) {
547239452caSArtem Dergachev     static SimpleProgramPointTag T("ExprEngine",
548239452caSArtem Dergachev                                    "Prepare for object construction");
549239452caSArtem Dergachev     ExplodedNodeSet DstPrepare;
550239452caSArtem Dergachev     StmtNodeBuilder BldrPrepare(Pred, DstPrepare, *currBldrCtx);
551a82ffe9dSArtem Dergachev     BldrPrepare.generateNode(E, Pred, State, &T, ProgramPoint::PreStmtKind);
552239452caSArtem Dergachev     assert(DstPrepare.size() <= 1);
553239452caSArtem Dergachev     if (DstPrepare.size() == 0)
554239452caSArtem Dergachev       return;
555239452caSArtem Dergachev     Pred = *BldrPrepare.begin();
556239452caSArtem Dergachev   }
557239452caSArtem Dergachev 
558a82ffe9dSArtem Dergachev   const MemRegion *TargetRegion = Target.getAsRegion();
559fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
560a82ffe9dSArtem Dergachev   CallEventRef<> Call =
561a82ffe9dSArtem Dergachev       CIE ? (CallEventRef<>)CEMgr.getCXXInheritedConstructorCall(
562a82ffe9dSArtem Dergachev                 CIE, TargetRegion, State, LCtx)
563a82ffe9dSArtem Dergachev           : (CallEventRef<>)CEMgr.getCXXConstructorCall(
564a82ffe9dSArtem Dergachev                 CE, TargetRegion, State, LCtx);
56510e25031STed Kremenek 
566f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
567a82ffe9dSArtem Dergachev   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, E, *this);
568b8e28654SJordan Rose 
569b8e28654SJordan Rose   ExplodedNodeSet PreInitialized;
570a82ffe9dSArtem Dergachev   if (CE) {
571a82ffe9dSArtem Dergachev     // FIXME: Is it possible and/or useful to do this before PreStmt?
572b8e28654SJordan Rose     StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
573b8e28654SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
574b8e28654SJordan Rose                                    E = DstPreVisit.end();
575b8e28654SJordan Rose          I != E; ++I) {
576b8e28654SJordan Rose       ProgramStateRef State = (*I)->getState();
577661ab34aSArtem Dergachev       if (CE->requiresZeroInitialization()) {
578b8e28654SJordan Rose         // FIXME: Once we properly handle constructors in new-expressions, we'll
579b8e28654SJordan Rose         // need to invalidate the region before setting a default value, to make
580b8e28654SJordan Rose         // sure there aren't any lingering bindings around. This probably needs
581b8e28654SJordan Rose         // to happen regardless of whether or not the object is zero-initialized
582b8e28654SJordan Rose         // to handle random fields of a placement-initialized object picking up
583b8e28654SJordan Rose         // old bindings. We might only want to do it when we need to, though.
584b8e28654SJordan Rose         // FIXME: This isn't actually correct for arrays -- we need to zero-
585b8e28654SJordan Rose         // initialize the entire array, not just the first element -- but our
586b8e28654SJordan Rose         // handling of arrays everywhere else is weak as well, so this shouldn't
587b8e28654SJordan Rose         // actually make things worse. Placement new makes this tricky as well,
588b8e28654SJordan Rose         // since it's then possible to be initializing one part of a multi-
589b8e28654SJordan Rose         // dimensional array.
590f83d5479SArtem Dergachev         State = State->bindDefaultZero(Target, LCtx);
591661ab34aSArtem Dergachev       }
592661ab34aSArtem Dergachev 
5930dbb783cSCraig Topper       Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
5940dbb783cSCraig Topper                         ProgramPoint::PreStmtKind);
595b8e28654SJordan Rose     }
596a82ffe9dSArtem Dergachev   } else {
597a82ffe9dSArtem Dergachev     PreInitialized = DstPreVisit;
598b8e28654SJordan Rose   }
599b8e28654SJordan Rose 
600afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
601b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
602fcd016e5SJordan Rose                                             *Call, *this);
60310e25031STed Kremenek 
60488bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
60588bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
60688bb563cSJordan Rose 
607a82ffe9dSArtem Dergachev   if (CE && CE->getConstructor()->isTrivial() &&
608e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
609168e29f6SArtem Dergachev       !CallOpts.IsArrayCtorOrDtor) {
610e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
611e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
61288bb563cSJordan Rose          I != E; ++I)
61388bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
61488bb563cSJordan Rose 
61588bb563cSJordan Rose   } else {
616afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
617f3c12ac3SJordan Rose          I != E; ++I)
618*37c1bf21SNithin Vadukkumchery Rajendrakumar       getCheckerManager().runCheckersForEvalCall(DstEvaluated, *I, *Call, *this,
619*37c1bf21SNithin Vadukkumchery Rajendrakumar                                                  CallOpts);
62088bb563cSJordan Rose   }
62110e25031STed Kremenek 
6222a8c18d9SAlexander Kornienko   // If the CFG was constructed without elements for temporary destructors
6235b1ee2faSDevin Coughlin   // and the just-called constructor created a temporary object then
6245b1ee2faSDevin Coughlin   // stop exploration if the temporary object has a noreturn constructor.
6255b1ee2faSDevin Coughlin   // This can lose coverage because the destructor, if it were present
6265b1ee2faSDevin Coughlin   // in the CFG, would be called at the end of the full expression or
6275b1ee2faSDevin Coughlin   // later (for life-time extended temporaries) -- but avoids infeasible
6285b1ee2faSDevin Coughlin   // paths when no-return temporary destructors are used for assertions.
6295b1ee2faSDevin Coughlin   const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
6305b1ee2faSDevin Coughlin   if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
631f2be30deSKirstóf Umann     if (llvm::isa_and_nonnull<CXXTempObjectRegion>(TargetRegion) &&
632a82ffe9dSArtem Dergachev         cast<CXXConstructorDecl>(Call->getDecl())
633f2be30deSKirstóf Umann             ->getParent()
634f2be30deSKirstóf Umann             ->isAnyDestructorNoReturn()) {
6355b1ee2faSDevin Coughlin 
6363da72051SArtem Dergachev       // If we've inlined the constructor, then DstEvaluated would be empty.
6373da72051SArtem Dergachev       // In this case we still want a sink, which could be implemented
6383da72051SArtem Dergachev       // in processCallExit. But we don't have that implemented at the moment,
6393da72051SArtem Dergachev       // so if you hit this assertion, see if you can avoid inlining
6403da72051SArtem Dergachev       // the respective constructor when analyzer-config cfg-temporary-dtors
6413da72051SArtem Dergachev       // is set to false.
6423da72051SArtem Dergachev       // Otherwise there's nothing wrong with inlining such constructor.
6433da72051SArtem Dergachev       assert(!DstEvaluated.empty() &&
6443da72051SArtem Dergachev              "We should not have inlined this constructor!");
6453da72051SArtem Dergachev 
6465b1ee2faSDevin Coughlin       for (ExplodedNode *N : DstEvaluated) {
647a82ffe9dSArtem Dergachev         Bldr.generateSink(E, N, N->getState());
6485b1ee2faSDevin Coughlin       }
6495b1ee2faSDevin Coughlin 
6505b1ee2faSDevin Coughlin       // There is no need to run the PostCall and PostStmt checker
6515b1ee2faSDevin Coughlin       // callbacks because we just generated sinks on all nodes in th
6525b1ee2faSDevin Coughlin       // frontier.
6535b1ee2faSDevin Coughlin       return;
6545b1ee2faSDevin Coughlin     }
6555b1ee2faSDevin Coughlin   }
6565b1ee2faSDevin Coughlin 
6573ccf14ebSArtem Dergachev   ExplodedNodeSet DstPostArgumentCleanup;
658f2be30deSKirstóf Umann   for (ExplodedNode *I : DstEvaluated)
6593ccf14ebSArtem Dergachev     finishArgumentConstruction(DstPostArgumentCleanup, I, *Call);
6603ccf14ebSArtem Dergachev 
6613ccf14ebSArtem Dergachev   // If there were other constructors called for object-type arguments
6623ccf14ebSArtem Dergachev   // of this constructor, clean them up.
663afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
6643ccf14ebSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostCall,
6653ccf14ebSArtem Dergachev                                              DstPostArgumentCleanup,
666fcd016e5SJordan Rose                                              *Call, *this);
667a82ffe9dSArtem Dergachev   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, E, *this);
668a82ffe9dSArtem Dergachev }
669a82ffe9dSArtem Dergachev 
670a82ffe9dSArtem Dergachev void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
671a82ffe9dSArtem Dergachev                                        ExplodedNode *Pred,
672a82ffe9dSArtem Dergachev                                        ExplodedNodeSet &Dst) {
673a82ffe9dSArtem Dergachev   handleConstructor(CE, Pred, Dst);
674a82ffe9dSArtem Dergachev }
675a82ffe9dSArtem Dergachev 
676a82ffe9dSArtem Dergachev void ExprEngine::VisitCXXInheritedCtorInitExpr(
677a82ffe9dSArtem Dergachev     const CXXInheritedCtorInitExpr *CE, ExplodedNode *Pred,
678a82ffe9dSArtem Dergachev     ExplodedNodeSet &Dst) {
679a82ffe9dSArtem Dergachev   handleConstructor(CE, Pred, Dst);
68010e25031STed Kremenek }
68110e25031STed Kremenek 
682b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
68310e25031STed Kremenek                                     const MemRegion *Dest,
68410e25031STed Kremenek                                     const Stmt *S,
6852bc9674bSJordan Rose                                     bool IsBaseDtor,
68610e25031STed Kremenek                                     ExplodedNode *Pred,
687168e29f6SArtem Dergachev                                     ExplodedNodeSet &Dst,
6888eb7a74bSArtem Dergachev                                     EvalCallOptions &CallOpts) {
689097a0497SArtem Dergachev   assert(S && "A destructor without a trigger!");
690fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
691fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
692fcd016e5SJordan Rose 
693b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
694b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
695b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
696097a0497SArtem Dergachev   // FIXME: There should always be a Decl, otherwise the destructor call
697097a0497SArtem Dergachev   // shouldn't have been added to the CFG in the first place.
698097a0497SArtem Dergachev   if (!DtorDecl) {
699097a0497SArtem Dergachev     // Skip the invalid destructor. We cannot simply return because
700097a0497SArtem Dergachev     // it would interrupt the analysis instead.
701097a0497SArtem Dergachev     static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
702097a0497SArtem Dergachev     // FIXME: PostImplicitCall with a null decl may crash elsewhere anyway.
703097a0497SArtem Dergachev     PostImplicitCall PP(/*Decl=*/nullptr, S->getEndLoc(), LCtx, &T);
704097a0497SArtem Dergachev     NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
705097a0497SArtem Dergachev     Bldr.generateNode(PP, Pred->getState(), Pred);
706097a0497SArtem Dergachev     return;
707097a0497SArtem Dergachev   }
708097a0497SArtem Dergachev 
7098eb7a74bSArtem Dergachev   if (!Dest) {
7108eb7a74bSArtem Dergachev     // We're trying to destroy something that is not a region. This may happen
7118eb7a74bSArtem Dergachev     // for a variety of reasons (unknown target region, concrete integer instead
7128eb7a74bSArtem Dergachev     // of target region, etc.). The current code makes an attempt to recover.
7138eb7a74bSArtem Dergachev     // FIXME: We probably don't really need to recover when we're dealing
7148eb7a74bSArtem Dergachev     // with concrete integers specifically.
7158eb7a74bSArtem Dergachev     CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
7168eb7a74bSArtem Dergachev     if (const Expr *E = dyn_cast_or_null<Expr>(S)) {
7178eb7a74bSArtem Dergachev       Dest = MRMgr.getCXXTempObjectRegion(E, Pred->getLocationContext());
7188eb7a74bSArtem Dergachev     } else {
7198eb7a74bSArtem Dergachev       static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
7208eb7a74bSArtem Dergachev       NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
7218eb7a74bSArtem Dergachev       Bldr.generateSink(Pred->getLocation().withTag(&T),
7228eb7a74bSArtem Dergachev                         Pred->getState(), Pred);
7238eb7a74bSArtem Dergachev       return;
7248eb7a74bSArtem Dergachev     }
7258eb7a74bSArtem Dergachev   }
7268eb7a74bSArtem Dergachev 
727fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
728fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
7292bc9674bSJordan Rose       CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
730142adc49STed Kremenek 
73117a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
73217a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
73317a8757aSJordan Rose                                 "Error evaluating destructor");
73417a8757aSJordan Rose 
7354ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
7364ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
737fcd016e5SJordan Rose                                             *Call, *this);
73810e25031STed Kremenek 
7394ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
740d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
7414ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
7424ee71b8aSJordan Rose        I != E; ++I)
743690ab040SArtem Dergachev     defaultEvalCall(Bldr, *I, *Call, CallOpts);
7444ee71b8aSJordan Rose 
7454ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
746fcd016e5SJordan Rose                                              *Call, *this);
74710e25031STed Kremenek }
74810e25031STed Kremenek 
7498b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
7508b808d64SJordan Rose                                           ExplodedNode *Pred,
7518b808d64SJordan Rose                                           ExplodedNodeSet &Dst) {
7528b808d64SJordan Rose   ProgramStateRef State = Pred->getState();
7538b808d64SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
7548b808d64SJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
755a6e4358fSStephen Kelly                                 CNE->getBeginLoc(),
7568b808d64SJordan Rose                                 "Error evaluating New Allocator Call");
7578b808d64SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
7588b808d64SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
7598b808d64SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
7608b808d64SJordan Rose 
7618b808d64SJordan Rose   ExplodedNodeSet DstPreCall;
7628b808d64SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
7638b808d64SJordan Rose                                             *Call, *this);
7648b808d64SJordan Rose 
76555796302SArtem Dergachev   ExplodedNodeSet DstPostCall;
76655796302SArtem Dergachev   StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
767f2be30deSKirstóf Umann   for (ExplodedNode *I : DstPreCall) {
7681084de52SArtem Dergachev     // FIXME: Provide evalCall for checkers?
76955796302SArtem Dergachev     defaultEvalCall(CallBldr, I, *Call);
7701084de52SArtem Dergachev   }
771beba5307SArtem Dergachev   // If the call is inlined, DstPostCall will be empty and we bail out now.
77255796302SArtem Dergachev 
77355796302SArtem Dergachev   // Store return value of operator new() for future use, until the actual
77455796302SArtem Dergachev   // CXXNewExpr gets processed.
77555796302SArtem Dergachev   ExplodedNodeSet DstPostValue;
77655796302SArtem Dergachev   StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
777f2be30deSKirstóf Umann   for (ExplodedNode *I : DstPostCall) {
778beba5307SArtem Dergachev     // FIXME: Because CNE serves as the "call site" for the allocator (due to
779beba5307SArtem Dergachev     // lack of a better expression in the AST), the conjured return value symbol
780beba5307SArtem Dergachev     // is going to be of the same type (C++ object pointer type). Technically
781beba5307SArtem Dergachev     // this is not correct because the operator new's prototype always says that
782beba5307SArtem Dergachev     // it returns a 'void *'. So we should change the type of the symbol,
783beba5307SArtem Dergachev     // and then evaluate the cast over the symbolic pointer from 'void *' to
784beba5307SArtem Dergachev     // the object pointer type. But without changing the symbol's type it
785beba5307SArtem Dergachev     // is breaking too much to evaluate the no-op symbolic cast over it, so we
786beba5307SArtem Dergachev     // skip it for now.
78755796302SArtem Dergachev     ProgramStateRef State = I->getState();
78850e0372fSArtem Dergachev     SVal RetVal = State->getSVal(CNE, LCtx);
78950e0372fSArtem Dergachev 
79050e0372fSArtem Dergachev     // If this allocation function is not declared as non-throwing, failures
79150e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
79250e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
79350e0372fSArtem Dergachev     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
79450e0372fSArtem Dergachev     // where new can return NULL. If we end up supporting that option, we can
79550e0372fSArtem Dergachev     // consider adding a check for it here.
79650e0372fSArtem Dergachev     // C++11 [basic.stc.dynamic.allocation]p3.
79750e0372fSArtem Dergachev     if (const FunctionDecl *FD = CNE->getOperatorNew()) {
79850e0372fSArtem Dergachev       QualType Ty = FD->getType();
79950e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
800eaf11ad7SRichard Smith         if (!ProtoType->isNothrow())
80150e0372fSArtem Dergachev           State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
80250e0372fSArtem Dergachev     }
80350e0372fSArtem Dergachev 
804f83d5479SArtem Dergachev     ValueBldr.generateNode(
805f83d5479SArtem Dergachev         CNE, I, addObjectUnderConstruction(State, CNE, LCtx, RetVal));
80655796302SArtem Dergachev   }
80755796302SArtem Dergachev 
80813b2026bSArtem Dergachev   ExplodedNodeSet DstPostPostCallCallback;
80913b2026bSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
81013b2026bSArtem Dergachev                                              DstPostValue, *Call, *this);
811f2be30deSKirstóf Umann   for (ExplodedNode *I : DstPostPostCallCallback) {
812f2be30deSKirstóf Umann     getCheckerManager().runCheckersForNewAllocator(*Call, Dst, I, *this);
81313b2026bSArtem Dergachev   }
8148b808d64SJordan Rose }
8158b808d64SJordan Rose 
81610e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
81710e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
818a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
819a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
820a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
821a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
82210e25031STed Kremenek 
823d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
824d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
82555796302SArtem Dergachev   SVal symVal = UnknownVal();
8268b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
82710e25031STed Kremenek 
8281084de52SArtem Dergachev   bool IsStandardGlobalOpNewFunction =
8291084de52SArtem Dergachev       FD->isReplaceableGlobalAllocationFunction();
8308b662704SAnton Yartsev 
83155796302SArtem Dergachev   ProgramStateRef State = Pred->getState();
83255796302SArtem Dergachev 
83355796302SArtem Dergachev   // Retrieve the stored operator new() return value.
834549f9cd4SKristof Umann   if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
835f83d5479SArtem Dergachev     symVal = *getObjectUnderConstruction(State, CNE, LCtx);
836f83d5479SArtem Dergachev     State = finishObjectConstruction(State, CNE, LCtx);
83755796302SArtem Dergachev   }
83855796302SArtem Dergachev 
8398b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
8408b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
8418b662704SAnton Yartsev   // a custom global allocator.
84255796302SArtem Dergachev   if (symVal.isUnknown()) {
8438b662704SAnton Yartsev     if (IsStandardGlobalOpNewFunction)
8448b662704SAnton Yartsev       symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
8458b662704SAnton Yartsev     else
8460dbb783cSCraig Topper       symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
8478b662704SAnton Yartsev                                             blockCount);
84855796302SArtem Dergachev   }
8498b662704SAnton Yartsev 
850fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
851fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
852fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
853fcd016e5SJordan Rose 
854549f9cd4SKristof Umann   if (!AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
855a4ee064cSJordan Rose     // Invalidate placement args.
856fcd016e5SJordan Rose     // FIXME: Once we figure out how we want allocators to work,
85772649423SKristof Umann     // we should be using the usual pre-/(default-)eval-/post-call checkers
85872649423SKristof Umann     // here.
859fcd016e5SJordan Rose     State = Call->invalidateRegions(blockCount);
860b8cb8359SJordan Rose     if (!State)
861b8cb8359SJordan Rose       return;
862a4ee064cSJordan Rose 
863fce1b03eSPavel Labath     // If this allocation function is not declared as non-throwing, failures
86450e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
86550e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
866fce1b03eSPavel Labath     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
867fce1b03eSPavel Labath     // where new can return NULL. If we end up supporting that option, we can
868fce1b03eSPavel Labath     // consider adding a check for it here.
8693957fd58SJordan Rose     // C++11 [basic.stc.dynamic.allocation]p3.
870fce1b03eSPavel Labath     if (FD) {
8713957fd58SJordan Rose       QualType Ty = FD->getType();
87250e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
873eaf11ad7SRichard Smith         if (!ProtoType->isNothrow())
87455796302SArtem Dergachev           if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
87555796302SArtem Dergachev             State = State->assume(*dSymVal, true);
8763957fd58SJordan Rose     }
87750e0372fSArtem Dergachev   }
8783957fd58SJordan Rose 
879b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
880b8cb8359SJordan Rose 
881beba5307SArtem Dergachev   SVal Result = symVal;
882beba5307SArtem Dergachev 
88310e25031STed Kremenek   if (CNE->isArray()) {
88410e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
88510e25031STed Kremenek     // For now, just return a symbolicated region.
886630f7dafSArtem Dergachev     if (const auto *NewReg = cast_or_null<SubRegion>(symVal.getAsRegion())) {
887630f7dafSArtem Dergachev       QualType ObjTy = CNE->getType()->getPointeeType();
888a4ee064cSJordan Rose       const ElementRegion *EleReg =
889a4ee064cSJordan Rose           getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
890beba5307SArtem Dergachev       Result = loc::MemRegionVal(EleReg);
891beba5307SArtem Dergachev     }
892beba5307SArtem Dergachev     State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
89344320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
89410e25031STed Kremenek     return;
89510e25031STed Kremenek   }
89610e25031STed Kremenek 
897a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
898a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
899a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
900a4ee064cSJordan Rose   // the constructor call anyway.)
90144320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
90244320367SJordan Rose     // Non-array placement new should always return the placement location.
90344320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
904b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
905aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
90644320367SJordan Rose   }
90744320367SJordan Rose 
908b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
909b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
9108f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
9118f6b4b04SJordan Rose   if (!NewN)
9128f6b4b04SJordan Rose     return;
9133503cb35SJordan Rose 
9145089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
9155089f3b3SJordan Rose   // initializer. Copy the value over.
9165089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
9175089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
9183503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
9198f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
920b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
921b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
9223503cb35SJordan Rose     }
9233503cb35SJordan Rose   }
92410e25031STed Kremenek }
92510e25031STed Kremenek 
92610e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
92710e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
9289d69072fSKirstóf Umann 
9299d69072fSKirstóf Umann   CallEventManager &CEMgr = getStateManager().getCallEventManager();
9309d69072fSKirstóf Umann   CallEventRef<CXXDeallocatorCall> Call = CEMgr.getCXXDeallocatorCall(
9319d69072fSKirstóf Umann       CDE, Pred->getState(), Pred->getLocationContext());
9329d69072fSKirstóf Umann 
9339d69072fSKirstóf Umann   ExplodedNodeSet DstPreCall;
9349d69072fSKirstóf Umann   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, *Call, *this);
9359d69072fSKirstóf Umann 
9369d69072fSKirstóf Umann   getCheckerManager().runCheckersForPostCall(Dst, DstPreCall, *Call, *this);
93710e25031STed Kremenek }
93810e25031STed Kremenek 
9399d69072fSKirstóf Umann void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred,
9408fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
9418fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
942b1279b5cSTed Kremenek   if (!VD) {
943b1279b5cSTed Kremenek     Dst.Add(Pred);
944b1279b5cSTed Kremenek     return;
945b1279b5cSTed Kremenek   }
946b1279b5cSTed Kremenek 
9478fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
948d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
949d94854a4STed Kremenek                                         currBldrCtx->blockCount());
9508fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
951b570195cSAnna Zaks   state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
9528fdb59f9STed Kremenek 
953d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
9548fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
9558fdb59f9STed Kremenek }
9568fdb59f9STed Kremenek 
95710e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
95810e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
959d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
960e594034fSAnna Zaks 
96110e25031STed Kremenek   // Get the this object region from StoreManager.
962632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
96310e25031STed Kremenek   const MemRegion *R =
96410e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
96510e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
966632e3b7eSTed Kremenek                                                     LCtx);
96710e25031STed Kremenek 
96849b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
96910e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
970632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
97110e25031STed Kremenek }
97215843343SGabor Horvath 
97315843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
97415843343SGabor Horvath                                  ExplodedNodeSet &Dst) {
97515843343SGabor Horvath   const LocationContext *LocCtxt = Pred->getLocationContext();
97615843343SGabor Horvath 
97715843343SGabor Horvath   // Get the region of the lambda itself.
97815843343SGabor Horvath   const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
97915843343SGabor Horvath       LE, LocCtxt);
98015843343SGabor Horvath   SVal V = loc::MemRegionVal(R);
98115843343SGabor Horvath 
98215843343SGabor Horvath   ProgramStateRef State = Pred->getState();
98315843343SGabor Horvath 
98415843343SGabor Horvath   // If we created a new MemRegion for the lambda, we should explicitly bind
98515843343SGabor Horvath   // the captures.
98615843343SGabor Horvath   CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
98715843343SGabor Horvath   for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
98815843343SGabor Horvath                                                e = LE->capture_init_end();
98915843343SGabor Horvath        i != e; ++i, ++CurField) {
9907bdca8b2SDevin Coughlin     FieldDecl *FieldForCapture = *CurField;
9917bdca8b2SDevin Coughlin     SVal FieldLoc = State->getLValue(FieldForCapture, V);
9927bdca8b2SDevin Coughlin 
9937bdca8b2SDevin Coughlin     SVal InitVal;
9947bdca8b2SDevin Coughlin     if (!FieldForCapture->hasCapturedVLAType()) {
9957bdca8b2SDevin Coughlin       Expr *InitExpr = *i;
9967bdca8b2SDevin Coughlin       assert(InitExpr && "Capture missing initialization expression");
9977bdca8b2SDevin Coughlin       InitVal = State->getSVal(InitExpr, LocCtxt);
9987bdca8b2SDevin Coughlin     } else {
9997bdca8b2SDevin Coughlin       // The field stores the length of a captured variable-length array.
10007bdca8b2SDevin Coughlin       // These captures don't have initialization expressions; instead we
10017bdca8b2SDevin Coughlin       // get the length from the VLAType size expression.
10027bdca8b2SDevin Coughlin       Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
10037bdca8b2SDevin Coughlin       InitVal = State->getSVal(SizeExpr, LocCtxt);
10047bdca8b2SDevin Coughlin     }
10057bdca8b2SDevin Coughlin 
1006b570195cSAnna Zaks     State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
100715843343SGabor Horvath   }
100815843343SGabor Horvath 
100915843343SGabor Horvath   // Decay the Loc into an RValue, because there might be a
101015843343SGabor Horvath   // MaterializeTemporaryExpr node above this one which expects the bound value
101115843343SGabor Horvath   // to be an RValue.
101215843343SGabor Horvath   SVal LambdaRVal = State->getSVal(R);
101315843343SGabor Horvath 
101415843343SGabor Horvath   ExplodedNodeSet Tmp;
101515843343SGabor Horvath   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
101615843343SGabor Horvath   // FIXME: is this the right program point kind?
101715843343SGabor Horvath   Bldr.generateNode(LE, Pred,
101815843343SGabor Horvath                     State->BindExpr(LE, LocCtxt, LambdaRVal),
101915843343SGabor Horvath                     nullptr, ProgramPoint::PostLValueKind);
102015843343SGabor Horvath 
102115843343SGabor Horvath   // FIXME: Move all post/pre visits to ::Visit().
102215843343SGabor Horvath   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
102315843343SGabor Horvath }
1024