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 
112a82ffe9dSArtem Dergachev std::pair<ProgramStateRef, SVal> ExprEngine::handleConstructionContext(
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 
119afb158c2SArtem Dergachev   // See if we're constructing an existing region by looking at the
120afb158c2SArtem Dergachev   // current construction context.
121661ab34aSArtem Dergachev   if (CC) {
1224068481bSArtem Dergachev     switch (CC->getKind()) {
12353b8ce0eSArtem Dergachev     case ConstructionContext::CXX17ElidedCopyVariableKind:
1244068481bSArtem Dergachev     case ConstructionContext::SimpleVariableKind: {
12553b8ce0eSArtem Dergachev       const auto *DSCC = cast<VariableConstructionContext>(CC);
1264068481bSArtem Dergachev       const auto *DS = DSCC->getDeclStmt();
127e231bd34SArtem Dergachev       const auto *Var = cast<VarDecl>(DS->getSingleDecl());
128bc74eb1cSJordan Rose       SVal LValue = State->getLValue(Var, LCtx);
12920edae87SJordan Rose       QualType Ty = Var->getType();
1301fe52474SArtem Dergachev       LValue =
1311fe52474SArtem Dergachev           makeZeroElementRegion(State, LValue, Ty, CallOpts.IsArrayCtorOrDtor);
1321fe52474SArtem Dergachev       State =
1331fe52474SArtem Dergachev           addObjectUnderConstruction(State, DSCC->getDeclStmt(), LCtx, LValue);
1341fe52474SArtem Dergachev       return std::make_pair(State, LValue);
13520edae87SJordan Rose     }
13653b8ce0eSArtem Dergachev     case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind:
137922455feSArtem Dergachev     case ConstructionContext::SimpleConstructorInitializerKind: {
1384068481bSArtem Dergachev       const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC);
1394068481bSArtem Dergachev       const auto *Init = ICC->getCXXCtorInitializer();
14054529a34SJordan Rose       assert(Init->isAnyMemberInitializer());
14154529a34SJordan Rose       const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
142412c0af2SDevin Coughlin       Loc ThisPtr =
14317906498SArtem Dergachev       SVB.getCXXThis(CurCtor, LCtx->getStackFrame());
14454529a34SJordan Rose       SVal ThisVal = State->getSVal(ThisPtr);
14554529a34SJordan Rose 
146bc74eb1cSJordan Rose       const ValueDecl *Field;
147bc74eb1cSJordan Rose       SVal FieldVal;
14854529a34SJordan Rose       if (Init->isIndirectMemberInitializer()) {
149bc74eb1cSJordan Rose         Field = Init->getIndirectMember();
150bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
15154529a34SJordan Rose       } else {
152bc74eb1cSJordan Rose         Field = Init->getMember();
153bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getMember(), ThisVal);
15454529a34SJordan Rose       }
155bc74eb1cSJordan Rose 
156bc74eb1cSJordan Rose       QualType Ty = Field->getType();
157690ab040SArtem Dergachev       FieldVal = makeZeroElementRegion(State, FieldVal, Ty,
158168e29f6SArtem Dergachev                                        CallOpts.IsArrayCtorOrDtor);
159a84374dcSArtem Dergachev       State = addObjectUnderConstruction(State, Init, LCtx, FieldVal);
160239452caSArtem Dergachev       return std::make_pair(State, FieldVal);
16154529a34SJordan Rose     }
1624068481bSArtem Dergachev     case ConstructionContext::NewAllocatedObjectKind: {
163549f9cd4SKristof Umann       if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
1644068481bSArtem Dergachev         const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC);
1654068481bSArtem Dergachev         const auto *NE = NECC->getCXXNewExpr();
166f83d5479SArtem Dergachev         SVal V = *getObjectUnderConstruction(State, NE, LCtx);
167f83d5479SArtem Dergachev         if (const SubRegion *MR =
168f83d5479SArtem Dergachev                 dyn_cast_or_null<SubRegion>(V.getAsRegion())) {
1694068481bSArtem Dergachev           if (NE->isArray()) {
1704068481bSArtem Dergachev             // TODO: In fact, we need to call the constructor for every
1714068481bSArtem Dergachev             // allocated element, not just the first one!
1724068481bSArtem Dergachev             CallOpts.IsArrayCtorOrDtor = true;
173239452caSArtem Dergachev             return std::make_pair(
174239452caSArtem Dergachev                 State, loc::MemRegionVal(getStoreManager().GetElementZeroRegion(
175239452caSArtem Dergachev                            MR, NE->getType()->getPointeeType())));
1764068481bSArtem Dergachev           }
177239452caSArtem Dergachev           return std::make_pair(State, V);
1784068481bSArtem Dergachev         }
179f83d5479SArtem Dergachev         // TODO: Detect when the allocator returns a null pointer.
180f83d5479SArtem Dergachev         // Constructor shall not be called in this case.
1814068481bSArtem Dergachev       }
1824068481bSArtem Dergachev       break;
1834068481bSArtem Dergachev     }
184f28d7f17SArtem Dergachev     case ConstructionContext::SimpleReturnedValueKind:
185f28d7f17SArtem Dergachev     case ConstructionContext::CXX17ElidedCopyReturnedValueKind: {
186239452caSArtem Dergachev       // The temporary is to be managed by the parent stack frame.
187239452caSArtem Dergachev       // So build it in the parent stack frame if we're not in the
188239452caSArtem Dergachev       // top frame of the analysis.
189dd18b11bSGeorge Karpenkov       const StackFrameContext *SFC = LCtx->getStackFrame();
190239452caSArtem Dergachev       if (const LocationContext *CallerLCtx = SFC->getParent()) {
191239452caSArtem Dergachev         auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()]
192239452caSArtem Dergachev                        .getAs<CFGCXXRecordTypedCall>();
193239452caSArtem Dergachev         if (!RTC) {
194239452caSArtem Dergachev           // We were unable to find the correct construction context for the
195239452caSArtem Dergachev           // call in the parent stack frame. This is equivalent to not being
196239452caSArtem Dergachev           // able to find construction context at all.
197239452caSArtem Dergachev           break;
198239452caSArtem Dergachev         }
199b3fc9df4SArtem Dergachev         if (isa<BlockInvocationContext>(CallerLCtx)) {
200b3fc9df4SArtem Dergachev           // Unwrap block invocation contexts. They're mostly part of
201b3fc9df4SArtem Dergachev           // the current stack frame.
202b3fc9df4SArtem Dergachev           CallerLCtx = CallerLCtx->getParent();
203b3fc9df4SArtem Dergachev           assert(!isa<BlockInvocationContext>(CallerLCtx));
204b3fc9df4SArtem Dergachev         }
205a82ffe9dSArtem Dergachev         return handleConstructionContext(
206f28d7f17SArtem Dergachev             cast<Expr>(SFC->getCallSite()), State, CallerLCtx,
207f28d7f17SArtem Dergachev             RTC->getConstructionContext(), CallOpts);
208239452caSArtem Dergachev       } else {
20917906498SArtem Dergachev         // We are on the top frame of the analysis. We do not know where is the
21017906498SArtem Dergachev         // object returned to. Conjure a symbolic region for the return value.
21117906498SArtem Dergachev         // TODO: We probably need a new MemRegion kind to represent the storage
21217906498SArtem Dergachev         // of that SymbolicRegion, so that we cound produce a fancy symbol
21317906498SArtem Dergachev         // instead of an anonymous conjured symbol.
21417906498SArtem Dergachev         // TODO: Do we need to track the region to avoid having it dead
21517906498SArtem Dergachev         // too early? It does die too early, at least in C++17, but because
21617906498SArtem Dergachev         // putting anything into a SymbolicRegion causes an immediate escape,
21717906498SArtem Dergachev         // it doesn't cause any leak false positives.
21817906498SArtem Dergachev         const auto *RCC = cast<ReturnedValueConstructionContext>(CC);
21917906498SArtem Dergachev         // Make sure that this doesn't coincide with any other symbol
22017906498SArtem Dergachev         // conjured for the returned expression.
22117906498SArtem Dergachev         static const int TopLevelSymRegionTag = 0;
22217906498SArtem Dergachev         const Expr *RetE = RCC->getReturnStmt()->getRetValue();
22317906498SArtem Dergachev         assert(RetE && "Void returns should not have a construction context");
22417906498SArtem Dergachev         QualType ReturnTy = RetE->getType();
22517906498SArtem Dergachev         QualType RegionTy = ACtx.getPointerType(ReturnTy);
22617906498SArtem Dergachev         SVal V = SVB.conjureSymbolVal(&TopLevelSymRegionTag, RetE, SFC,
22717906498SArtem Dergachev                                       RegionTy, currBldrCtx->blockCount());
228239452caSArtem Dergachev         return std::make_pair(State, V);
229239452caSArtem Dergachev       }
230f28d7f17SArtem Dergachev       llvm_unreachable("Unhandled return value construction context!");
231239452caSArtem Dergachev     }
2329a209ad1SArtem Dergachev     case ConstructionContext::ElidedTemporaryObjectKind: {
233549f9cd4SKristof Umann       assert(AMgr.getAnalyzerOptions().ShouldElideConstructors);
2349a209ad1SArtem Dergachev       const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC);
2359a209ad1SArtem Dergachev       const CXXBindTemporaryExpr *BTE = TCC->getCXXBindTemporaryExpr();
2369a209ad1SArtem Dergachev       const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr();
2379a209ad1SArtem Dergachev       const CXXConstructExpr *CE = TCC->getConstructorAfterElision();
2389a209ad1SArtem Dergachev 
2399a209ad1SArtem Dergachev       // Support pre-C++17 copy elision. We'll have the elidable copy
2409a209ad1SArtem Dergachev       // constructor in the AST and in the CFG, but we'll skip it
2419a209ad1SArtem Dergachev       // and construct directly into the final object. This call
2429a209ad1SArtem Dergachev       // also sets the CallOpts flags for us.
2439a209ad1SArtem Dergachev       SVal V;
244a657a32cSArtem Dergachev       // If the elided copy/move constructor is not supported, there's still
245a657a32cSArtem Dergachev       // benefit in trying to model the non-elided constructor.
246a657a32cSArtem Dergachev       // Stash our state before trying to elide, as it'll get overwritten.
247a657a32cSArtem Dergachev       ProgramStateRef PreElideState = State;
248a657a32cSArtem Dergachev       EvalCallOptions PreElideCallOpts = CallOpts;
249a657a32cSArtem Dergachev 
250a82ffe9dSArtem Dergachev       std::tie(State, V) = handleConstructionContext(
2519a209ad1SArtem Dergachev           CE, State, LCtx, TCC->getConstructionContextAfterElision(), CallOpts);
2529a209ad1SArtem Dergachev 
253a657a32cSArtem Dergachev       // FIXME: This definition of "copy elision has not failed" is unreliable.
254a657a32cSArtem Dergachev       // It doesn't indicate that the constructor will actually be inlined
255a657a32cSArtem Dergachev       // later; it is still up to evalCall() to decide.
256a657a32cSArtem Dergachev       if (!CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion) {
2579a209ad1SArtem Dergachev         // Remember that we've elided the constructor.
2589a209ad1SArtem Dergachev         State = addObjectUnderConstruction(State, CE, LCtx, V);
2599a209ad1SArtem Dergachev 
2609a209ad1SArtem Dergachev         // Remember that we've elided the destructor.
2619a209ad1SArtem Dergachev         if (BTE)
2629a209ad1SArtem Dergachev           State = elideDestructor(State, BTE, LCtx);
2639a209ad1SArtem Dergachev 
2649a209ad1SArtem Dergachev         // Instead of materialization, shamelessly return
2659a209ad1SArtem Dergachev         // the final object destination.
2669a209ad1SArtem Dergachev         if (MTE)
2679a209ad1SArtem Dergachev           State = addObjectUnderConstruction(State, MTE, LCtx, V);
2689a209ad1SArtem Dergachev 
2699a209ad1SArtem Dergachev         return std::make_pair(State, V);
270a657a32cSArtem Dergachev       } else {
271a657a32cSArtem Dergachev         // Copy elision failed. Revert the changes and proceed as if we have
272a657a32cSArtem Dergachev         // a simple temporary.
273a657a32cSArtem Dergachev         State = PreElideState;
274a657a32cSArtem Dergachev         CallOpts = PreElideCallOpts;
275a657a32cSArtem Dergachev       }
276080b8709SSimon Pilgrim       LLVM_FALLTHROUGH;
2779a209ad1SArtem Dergachev     }
278ff267df0SArtem Dergachev     case ConstructionContext::SimpleTemporaryObjectKind: {
279a657a32cSArtem Dergachev       const auto *TCC = cast<TemporaryObjectConstructionContext>(CC);
280239452caSArtem Dergachev       const CXXBindTemporaryExpr *BTE = TCC->getCXXBindTemporaryExpr();
281239452caSArtem Dergachev       const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr();
282d18639bdSArtem Dergachev       SVal V = UnknownVal();
283239452caSArtem Dergachev 
284239452caSArtem Dergachev       if (MTE) {
2858cd7961aSArtem Dergachev         if (const ValueDecl *VD = MTE->getExtendingDecl()) {
286643102dfSArtem Dergachev           assert(MTE->getStorageDuration() != SD_FullExpression);
287643102dfSArtem Dergachev           if (!VD->getType()->isReferenceType()) {
288643102dfSArtem Dergachev             // We're lifetime-extended by a surrounding aggregate.
289643102dfSArtem Dergachev             // Automatic destructors aren't quite working in this case
290643102dfSArtem Dergachev             // on the CFG side. We should warn the caller about that.
291310bca01SArtem Dergachev             // FIXME: Is there a better way to retrieve this information from
292310bca01SArtem Dergachev             // the MaterializeTemporaryExpr?
293310bca01SArtem Dergachev             CallOpts.IsTemporaryLifetimeExtendedViaAggregate = true;
294310bca01SArtem Dergachev           }
2958cd7961aSArtem Dergachev         }
2964fb586c3SArtem Dergachev 
2974fb586c3SArtem Dergachev         if (MTE->getStorageDuration() == SD_Static ||
2984fb586c3SArtem Dergachev             MTE->getStorageDuration() == SD_Thread)
2994fb586c3SArtem Dergachev           V = loc::MemRegionVal(MRMgr.getCXXStaticTempObjectRegion(E));
300239452caSArtem Dergachev       }
301239452caSArtem Dergachev 
3024fb586c3SArtem Dergachev       if (V.isUnknown())
3034fb586c3SArtem Dergachev         V = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
304239452caSArtem Dergachev 
305239452caSArtem Dergachev       if (BTE)
306239452caSArtem Dergachev         State = addObjectUnderConstruction(State, BTE, LCtx, V);
307239452caSArtem Dergachev 
308239452caSArtem Dergachev       if (MTE)
309239452caSArtem Dergachev         State = addObjectUnderConstruction(State, MTE, LCtx, V);
310239452caSArtem Dergachev 
3114068481bSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
312239452caSArtem Dergachev       return std::make_pair(State, V);
3134068481bSArtem Dergachev     }
314a657a32cSArtem Dergachev     case ConstructionContext::ArgumentKind: {
3153ccf14ebSArtem Dergachev       // Arguments are technically temporaries.
3163ccf14ebSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
3173ccf14ebSArtem Dergachev 
3183ccf14ebSArtem Dergachev       const auto *ACC = cast<ArgumentConstructionContext>(CC);
3193ccf14ebSArtem Dergachev       const Expr *E = ACC->getCallLikeExpr();
3203ccf14ebSArtem Dergachev       unsigned Idx = ACC->getIndex();
3213ccf14ebSArtem Dergachev       const CXXBindTemporaryExpr *BTE = ACC->getCXXBindTemporaryExpr();
3223ccf14ebSArtem Dergachev 
3233ccf14ebSArtem Dergachev       CallEventManager &CEMgr = getStateManager().getCallEventManager();
3243ccf14ebSArtem Dergachev       SVal V = UnknownVal();
3253ccf14ebSArtem Dergachev       auto getArgLoc = [&](CallEventRef<> Caller) -> Optional<SVal> {
3267740c6d6SCsaba Dabis         const LocationContext *FutureSFC =
3277740c6d6SCsaba Dabis             Caller->getCalleeStackFrame(currBldrCtx->blockCount());
3283ccf14ebSArtem Dergachev         // Return early if we are unable to reliably foresee
3293ccf14ebSArtem Dergachev         // the future stack frame.
3303ccf14ebSArtem Dergachev         if (!FutureSFC)
3313ccf14ebSArtem Dergachev           return None;
3323ccf14ebSArtem Dergachev 
3333ccf14ebSArtem Dergachev         // This should be equivalent to Caller->getDecl() for now, but
3343ccf14ebSArtem Dergachev         // FutureSFC->getDecl() is likely to support better stuff (like
3353ccf14ebSArtem Dergachev         // virtual functions) earlier.
3363ccf14ebSArtem Dergachev         const Decl *CalleeD = FutureSFC->getDecl();
3373ccf14ebSArtem Dergachev 
3383ccf14ebSArtem Dergachev         // FIXME: Support for variadic arguments is not implemented here yet.
3393ccf14ebSArtem Dergachev         if (CallEvent::isVariadic(CalleeD))
3403ccf14ebSArtem Dergachev           return None;
3413ccf14ebSArtem Dergachev 
3423ccf14ebSArtem Dergachev         // Operator arguments do not correspond to operator parameters
3433ccf14ebSArtem Dergachev         // because this-argument is implemented as a normal argument in
3443ccf14ebSArtem Dergachev         // operator call expressions but not in operator declarations.
3453ccf14ebSArtem Dergachev         const VarRegion *VR = Caller->getParameterLocation(
3467740c6d6SCsaba Dabis             *Caller->getAdjustedParameterIndex(Idx), currBldrCtx->blockCount());
3473ccf14ebSArtem Dergachev         if (!VR)
3483ccf14ebSArtem Dergachev           return None;
3493ccf14ebSArtem Dergachev 
3503ccf14ebSArtem Dergachev         return loc::MemRegionVal(VR);
3513ccf14ebSArtem Dergachev       };
3523ccf14ebSArtem Dergachev 
3533ccf14ebSArtem Dergachev       if (const auto *CE = dyn_cast<CallExpr>(E)) {
3543ccf14ebSArtem Dergachev         CallEventRef<> Caller = CEMgr.getSimpleCall(CE, State, LCtx);
3553ccf14ebSArtem Dergachev         if (auto OptV = getArgLoc(Caller))
3563ccf14ebSArtem Dergachev           V = *OptV;
3573ccf14ebSArtem Dergachev         else
358a657a32cSArtem Dergachev           break;
3593ccf14ebSArtem Dergachev         State = addObjectUnderConstruction(State, {CE, Idx}, LCtx, V);
3603ccf14ebSArtem Dergachev       } else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) {
3613ccf14ebSArtem Dergachev         // Don't bother figuring out the target region for the future
3623ccf14ebSArtem Dergachev         // constructor because we won't need it.
3633ccf14ebSArtem Dergachev         CallEventRef<> Caller =
3643ccf14ebSArtem Dergachev             CEMgr.getCXXConstructorCall(CCE, /*Target=*/nullptr, State, LCtx);
3653ccf14ebSArtem Dergachev         if (auto OptV = getArgLoc(Caller))
3663ccf14ebSArtem Dergachev           V = *OptV;
3673ccf14ebSArtem Dergachev         else
3683ccf14ebSArtem Dergachev           break;
3693ccf14ebSArtem Dergachev         State = addObjectUnderConstruction(State, {CCE, Idx}, LCtx, V);
3703ccf14ebSArtem Dergachev       } else if (const auto *ME = dyn_cast<ObjCMessageExpr>(E)) {
3713ccf14ebSArtem Dergachev         CallEventRef<> Caller = CEMgr.getObjCMethodCall(ME, State, LCtx);
3723ccf14ebSArtem Dergachev         if (auto OptV = getArgLoc(Caller))
3733ccf14ebSArtem Dergachev           V = *OptV;
3743ccf14ebSArtem Dergachev         else
3753ccf14ebSArtem Dergachev           break;
3763ccf14ebSArtem Dergachev         State = addObjectUnderConstruction(State, {ME, Idx}, LCtx, V);
3773ccf14ebSArtem Dergachev       }
3783ccf14ebSArtem Dergachev 
3793ccf14ebSArtem Dergachev       assert(!V.isUnknown());
3803ccf14ebSArtem Dergachev 
3813ccf14ebSArtem Dergachev       if (BTE)
3823ccf14ebSArtem Dergachev         State = addObjectUnderConstruction(State, BTE, LCtx, V);
3833ccf14ebSArtem Dergachev 
3843ccf14ebSArtem Dergachev       return std::make_pair(State, V);
385a657a32cSArtem Dergachev     }
3864068481bSArtem Dergachev     }
38705375eb4SJordan Rose   }
388c9318304SJordan Rose   // If we couldn't find an existing region to construct into, assume we're
389690ab040SArtem Dergachev   // constructing a temporary. Notify the caller of our failure.
390168e29f6SArtem Dergachev   CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
391239452caSArtem Dergachev   return std::make_pair(
392239452caSArtem Dergachev       State, loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)));
393c9318304SJordan Rose }
39405375eb4SJordan Rose 
395a82ffe9dSArtem Dergachev void ExprEngine::handleConstructor(const Expr *E,
39617f41608SJordan Rose                                    ExplodedNode *Pred,
39717f41608SJordan Rose                                    ExplodedNodeSet &destNodes) {
398a82ffe9dSArtem Dergachev   const auto *CE = dyn_cast<CXXConstructExpr>(E);
399a82ffe9dSArtem Dergachev   const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(E);
400a82ffe9dSArtem Dergachev   assert(CE || CIE);
401a82ffe9dSArtem Dergachev 
40217f41608SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
40317f41608SJordan Rose   ProgramStateRef State = Pred->getState();
40417f41608SJordan Rose 
405f83d5479SArtem Dergachev   SVal Target = UnknownVal();
40617f41608SJordan Rose 
407a82ffe9dSArtem Dergachev   if (CE) {
4089a209ad1SArtem Dergachev     if (Optional<SVal> ElidedTarget =
4099a209ad1SArtem Dergachev             getObjectUnderConstruction(State, CE, LCtx)) {
410a82ffe9dSArtem Dergachev       // We've previously modeled an elidable constructor by pretending that it
411a82ffe9dSArtem Dergachev       // in fact constructs into the correct target. This constructor can
412a82ffe9dSArtem Dergachev       // therefore be skipped.
4139a209ad1SArtem Dergachev       Target = *ElidedTarget;
4149a209ad1SArtem Dergachev       StmtNodeBuilder Bldr(Pred, destNodes, *currBldrCtx);
4159a209ad1SArtem Dergachev       State = finishObjectConstruction(State, CE, LCtx);
4169a209ad1SArtem Dergachev       if (auto L = Target.getAs<Loc>())
4179a209ad1SArtem Dergachev         State = State->BindExpr(CE, LCtx, State->getSVal(*L, CE->getType()));
4189a209ad1SArtem Dergachev       Bldr.generateNode(CE, Pred, State);
4199a209ad1SArtem Dergachev       return;
4209a209ad1SArtem Dergachev     }
421a82ffe9dSArtem Dergachev   }
4229a209ad1SArtem Dergachev 
42317f41608SJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
42417f41608SJordan Rose   // For now, we just run the first constructor (which should still invalidate
42517f41608SJordan Rose   // the entire array).
42617f41608SJordan Rose 
427690ab040SArtem Dergachev   EvalCallOptions CallOpts;
428661ab34aSArtem Dergachev   auto C = getCurrentCFGElement().getAs<CFGConstructor>();
4294068481bSArtem Dergachev   assert(C || getCurrentCFGElement().getAs<CFGStmt>());
430661ab34aSArtem Dergachev   const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr;
431661ab34aSArtem Dergachev 
432a82ffe9dSArtem Dergachev   const CXXConstructExpr::ConstructionKind CK =
433a82ffe9dSArtem Dergachev       CE ? CE->getConstructionKind() : CIE->getConstructionKind();
434a82ffe9dSArtem Dergachev   switch (CK) {
43517f41608SJordan Rose   case CXXConstructExpr::CK_Complete: {
436a82ffe9dSArtem Dergachev     // Inherited constructors are always base class constructors.
437a82ffe9dSArtem Dergachev     assert(CE && !CIE && "A complete constructor is inherited?!");
438a82ffe9dSArtem Dergachev 
439a82ffe9dSArtem Dergachev     // The target region is found from construction context.
440239452caSArtem Dergachev     std::tie(State, Target) =
441a82ffe9dSArtem Dergachev         handleConstructionContext(CE, State, LCtx, CC, CallOpts);
442b970505dSJordan Rose     break;
443b970505dSJordan Rose   }
444192a7474SArtem Dergachev   case CXXConstructExpr::CK_VirtualBase: {
445b3b976f0SJordan Rose     // Make sure we are not calling virtual base class initializers twice.
446b3b976f0SJordan Rose     // Only the most-derived object should initialize virtual base classes.
447192a7474SArtem Dergachev     const auto *OuterCtor = dyn_cast_or_null<CXXConstructExpr>(
448192a7474SArtem Dergachev         LCtx->getStackFrame()->getCallSite());
449192a7474SArtem Dergachev     assert(
450192a7474SArtem Dergachev         (!OuterCtor ||
451192a7474SArtem Dergachev          OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Complete ||
452192a7474SArtem Dergachev          OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Delegating) &&
453192a7474SArtem Dergachev         ("This virtual base should have already been initialized by "
454192a7474SArtem Dergachev          "the most derived class!"));
455192a7474SArtem Dergachev     (void)OuterCtor;
4564dc0b1acSReid Kleckner     LLVM_FALLTHROUGH;
457192a7474SArtem Dergachev   }
458b3b976f0SJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
459e8ba3ec4SArtem Dergachev     // In C++17, classes with non-virtual bases may be aggregates, so they would
460e8ba3ec4SArtem Dergachev     // be initialized as aggregates without a constructor call, so we may have
461e8ba3ec4SArtem Dergachev     // a base class constructed directly into an initializer list without
462e8ba3ec4SArtem Dergachev     // having the derived-class constructor call on the previous stack frame.
463e8ba3ec4SArtem Dergachev     // Initializer lists may be nested into more initializer lists that
464e8ba3ec4SArtem Dergachev     // correspond to surrounding aggregate initializations.
465e8ba3ec4SArtem Dergachev     // FIXME: For now this code essentially bails out. We need to find the
466e8ba3ec4SArtem Dergachev     // correct target region and set it.
467e8ba3ec4SArtem Dergachev     // FIXME: Instead of relying on the ParentMap, we should have the
468e8ba3ec4SArtem Dergachev     // trigger-statement (InitListExpr in this case) passed down from CFG or
469e8ba3ec4SArtem Dergachev     // otherwise always available during construction.
470a82ffe9dSArtem Dergachev     if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(E))) {
471e8ba3ec4SArtem Dergachev       MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
472a82ffe9dSArtem Dergachev       Target = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
473168e29f6SArtem Dergachev       CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
474e8ba3ec4SArtem Dergachev       break;
475e8ba3ec4SArtem Dergachev     }
4764dc0b1acSReid Kleckner     LLVM_FALLTHROUGH;
477b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
478b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
479b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
480dd18b11bSGeorge Karpenkov                                               LCtx->getStackFrame());
48105375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
482b970505dSJordan Rose 
483a82ffe9dSArtem Dergachev     if (CK == CXXConstructExpr::CK_Delegating) {
484f83d5479SArtem Dergachev       Target = ThisVal;
485b970505dSJordan Rose     } else {
486b970505dSJordan Rose       // Cast to the base type.
487a82ffe9dSArtem Dergachev       bool IsVirtual = (CK == CXXConstructExpr::CK_VirtualBase);
488a82ffe9dSArtem Dergachev       SVal BaseVal =
489a82ffe9dSArtem Dergachev           getStoreManager().evalDerivedToBase(ThisVal, E->getType(), IsVirtual);
490f83d5479SArtem Dergachev       Target = BaseVal;
491b970505dSJordan Rose     }
492b970505dSJordan Rose     break;
493b970505dSJordan Rose   }
494b970505dSJordan Rose   }
495b970505dSJordan Rose 
496239452caSArtem Dergachev   if (State != Pred->getState()) {
497239452caSArtem Dergachev     static SimpleProgramPointTag T("ExprEngine",
498239452caSArtem Dergachev                                    "Prepare for object construction");
499239452caSArtem Dergachev     ExplodedNodeSet DstPrepare;
500239452caSArtem Dergachev     StmtNodeBuilder BldrPrepare(Pred, DstPrepare, *currBldrCtx);
501a82ffe9dSArtem Dergachev     BldrPrepare.generateNode(E, Pred, State, &T, ProgramPoint::PreStmtKind);
502239452caSArtem Dergachev     assert(DstPrepare.size() <= 1);
503239452caSArtem Dergachev     if (DstPrepare.size() == 0)
504239452caSArtem Dergachev       return;
505239452caSArtem Dergachev     Pred = *BldrPrepare.begin();
506239452caSArtem Dergachev   }
507239452caSArtem Dergachev 
508a82ffe9dSArtem Dergachev   const MemRegion *TargetRegion = Target.getAsRegion();
509fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
510a82ffe9dSArtem Dergachev   CallEventRef<> Call =
511a82ffe9dSArtem Dergachev       CIE ? (CallEventRef<>)CEMgr.getCXXInheritedConstructorCall(
512a82ffe9dSArtem Dergachev                 CIE, TargetRegion, State, LCtx)
513a82ffe9dSArtem Dergachev           : (CallEventRef<>)CEMgr.getCXXConstructorCall(
514a82ffe9dSArtem Dergachev                 CE, TargetRegion, State, LCtx);
51510e25031STed Kremenek 
516f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
517a82ffe9dSArtem Dergachev   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, E, *this);
518b8e28654SJordan Rose 
519b8e28654SJordan Rose   ExplodedNodeSet PreInitialized;
520a82ffe9dSArtem Dergachev   if (CE) {
521a82ffe9dSArtem Dergachev     // FIXME: Is it possible and/or useful to do this before PreStmt?
522b8e28654SJordan Rose     StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
523b8e28654SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
524b8e28654SJordan Rose                                    E = DstPreVisit.end();
525b8e28654SJordan Rose          I != E; ++I) {
526b8e28654SJordan Rose       ProgramStateRef State = (*I)->getState();
527661ab34aSArtem Dergachev       if (CE->requiresZeroInitialization()) {
528b8e28654SJordan Rose         // FIXME: Once we properly handle constructors in new-expressions, we'll
529b8e28654SJordan Rose         // need to invalidate the region before setting a default value, to make
530b8e28654SJordan Rose         // sure there aren't any lingering bindings around. This probably needs
531b8e28654SJordan Rose         // to happen regardless of whether or not the object is zero-initialized
532b8e28654SJordan Rose         // to handle random fields of a placement-initialized object picking up
533b8e28654SJordan Rose         // old bindings. We might only want to do it when we need to, though.
534b8e28654SJordan Rose         // FIXME: This isn't actually correct for arrays -- we need to zero-
535b8e28654SJordan Rose         // initialize the entire array, not just the first element -- but our
536b8e28654SJordan Rose         // handling of arrays everywhere else is weak as well, so this shouldn't
537b8e28654SJordan Rose         // actually make things worse. Placement new makes this tricky as well,
538b8e28654SJordan Rose         // since it's then possible to be initializing one part of a multi-
539b8e28654SJordan Rose         // dimensional array.
540f83d5479SArtem Dergachev         State = State->bindDefaultZero(Target, LCtx);
541661ab34aSArtem Dergachev       }
542661ab34aSArtem Dergachev 
5430dbb783cSCraig Topper       Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
5440dbb783cSCraig Topper                         ProgramPoint::PreStmtKind);
545b8e28654SJordan Rose     }
546a82ffe9dSArtem Dergachev   } else {
547a82ffe9dSArtem Dergachev     PreInitialized = DstPreVisit;
548b8e28654SJordan Rose   }
549b8e28654SJordan Rose 
550afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
551b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
552fcd016e5SJordan Rose                                             *Call, *this);
55310e25031STed Kremenek 
55488bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
55588bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
55688bb563cSJordan Rose 
557a82ffe9dSArtem Dergachev   if (CE && CE->getConstructor()->isTrivial() &&
558e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
559168e29f6SArtem Dergachev       !CallOpts.IsArrayCtorOrDtor) {
560e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
561e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
56288bb563cSJordan Rose          I != E; ++I)
56388bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
56488bb563cSJordan Rose 
56588bb563cSJordan Rose   } else {
566afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
567f3c12ac3SJordan Rose          I != E; ++I)
568690ab040SArtem Dergachev       defaultEvalCall(Bldr, *I, *Call, CallOpts);
56988bb563cSJordan Rose   }
57010e25031STed Kremenek 
5712a8c18d9SAlexander Kornienko   // If the CFG was constructed without elements for temporary destructors
5725b1ee2faSDevin Coughlin   // and the just-called constructor created a temporary object then
5735b1ee2faSDevin Coughlin   // stop exploration if the temporary object has a noreturn constructor.
5745b1ee2faSDevin Coughlin   // This can lose coverage because the destructor, if it were present
5755b1ee2faSDevin Coughlin   // in the CFG, would be called at the end of the full expression or
5765b1ee2faSDevin Coughlin   // later (for life-time extended temporaries) -- but avoids infeasible
5775b1ee2faSDevin Coughlin   // paths when no-return temporary destructors are used for assertions.
5785b1ee2faSDevin Coughlin   const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
5795b1ee2faSDevin Coughlin   if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
580*f2be30deSKirstóf Umann     if (llvm::isa_and_nonnull<CXXTempObjectRegion>(TargetRegion) &&
581a82ffe9dSArtem Dergachev         cast<CXXConstructorDecl>(Call->getDecl())
582*f2be30deSKirstóf Umann             ->getParent()
583*f2be30deSKirstóf Umann             ->isAnyDestructorNoReturn()) {
5845b1ee2faSDevin Coughlin 
5853da72051SArtem Dergachev       // If we've inlined the constructor, then DstEvaluated would be empty.
5863da72051SArtem Dergachev       // In this case we still want a sink, which could be implemented
5873da72051SArtem Dergachev       // in processCallExit. But we don't have that implemented at the moment,
5883da72051SArtem Dergachev       // so if you hit this assertion, see if you can avoid inlining
5893da72051SArtem Dergachev       // the respective constructor when analyzer-config cfg-temporary-dtors
5903da72051SArtem Dergachev       // is set to false.
5913da72051SArtem Dergachev       // Otherwise there's nothing wrong with inlining such constructor.
5923da72051SArtem Dergachev       assert(!DstEvaluated.empty() &&
5933da72051SArtem Dergachev              "We should not have inlined this constructor!");
5943da72051SArtem Dergachev 
5955b1ee2faSDevin Coughlin       for (ExplodedNode *N : DstEvaluated) {
596a82ffe9dSArtem Dergachev         Bldr.generateSink(E, N, N->getState());
5975b1ee2faSDevin Coughlin       }
5985b1ee2faSDevin Coughlin 
5995b1ee2faSDevin Coughlin       // There is no need to run the PostCall and PostStmt checker
6005b1ee2faSDevin Coughlin       // callbacks because we just generated sinks on all nodes in th
6015b1ee2faSDevin Coughlin       // frontier.
6025b1ee2faSDevin Coughlin       return;
6035b1ee2faSDevin Coughlin     }
6045b1ee2faSDevin Coughlin   }
6055b1ee2faSDevin Coughlin 
6063ccf14ebSArtem Dergachev   ExplodedNodeSet DstPostArgumentCleanup;
607*f2be30deSKirstóf Umann   for (ExplodedNode *I : DstEvaluated)
6083ccf14ebSArtem Dergachev     finishArgumentConstruction(DstPostArgumentCleanup, I, *Call);
6093ccf14ebSArtem Dergachev 
6103ccf14ebSArtem Dergachev   // If there were other constructors called for object-type arguments
6113ccf14ebSArtem Dergachev   // of this constructor, clean them up.
612afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
6133ccf14ebSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostCall,
6143ccf14ebSArtem Dergachev                                              DstPostArgumentCleanup,
615fcd016e5SJordan Rose                                              *Call, *this);
616a82ffe9dSArtem Dergachev   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, E, *this);
617a82ffe9dSArtem Dergachev }
618a82ffe9dSArtem Dergachev 
619a82ffe9dSArtem Dergachev void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
620a82ffe9dSArtem Dergachev                                        ExplodedNode *Pred,
621a82ffe9dSArtem Dergachev                                        ExplodedNodeSet &Dst) {
622a82ffe9dSArtem Dergachev   handleConstructor(CE, Pred, Dst);
623a82ffe9dSArtem Dergachev }
624a82ffe9dSArtem Dergachev 
625a82ffe9dSArtem Dergachev void ExprEngine::VisitCXXInheritedCtorInitExpr(
626a82ffe9dSArtem Dergachev     const CXXInheritedCtorInitExpr *CE, ExplodedNode *Pred,
627a82ffe9dSArtem Dergachev     ExplodedNodeSet &Dst) {
628a82ffe9dSArtem Dergachev   handleConstructor(CE, Pred, Dst);
62910e25031STed Kremenek }
63010e25031STed Kremenek 
631b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
63210e25031STed Kremenek                                     const MemRegion *Dest,
63310e25031STed Kremenek                                     const Stmt *S,
6342bc9674bSJordan Rose                                     bool IsBaseDtor,
63510e25031STed Kremenek                                     ExplodedNode *Pred,
636168e29f6SArtem Dergachev                                     ExplodedNodeSet &Dst,
6378eb7a74bSArtem Dergachev                                     EvalCallOptions &CallOpts) {
638097a0497SArtem Dergachev   assert(S && "A destructor without a trigger!");
639fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
640fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
641fcd016e5SJordan Rose 
642b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
643b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
644b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
645097a0497SArtem Dergachev   // FIXME: There should always be a Decl, otherwise the destructor call
646097a0497SArtem Dergachev   // shouldn't have been added to the CFG in the first place.
647097a0497SArtem Dergachev   if (!DtorDecl) {
648097a0497SArtem Dergachev     // Skip the invalid destructor. We cannot simply return because
649097a0497SArtem Dergachev     // it would interrupt the analysis instead.
650097a0497SArtem Dergachev     static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
651097a0497SArtem Dergachev     // FIXME: PostImplicitCall with a null decl may crash elsewhere anyway.
652097a0497SArtem Dergachev     PostImplicitCall PP(/*Decl=*/nullptr, S->getEndLoc(), LCtx, &T);
653097a0497SArtem Dergachev     NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
654097a0497SArtem Dergachev     Bldr.generateNode(PP, Pred->getState(), Pred);
655097a0497SArtem Dergachev     return;
656097a0497SArtem Dergachev   }
657097a0497SArtem Dergachev 
6588eb7a74bSArtem Dergachev   if (!Dest) {
6598eb7a74bSArtem Dergachev     // We're trying to destroy something that is not a region. This may happen
6608eb7a74bSArtem Dergachev     // for a variety of reasons (unknown target region, concrete integer instead
6618eb7a74bSArtem Dergachev     // of target region, etc.). The current code makes an attempt to recover.
6628eb7a74bSArtem Dergachev     // FIXME: We probably don't really need to recover when we're dealing
6638eb7a74bSArtem Dergachev     // with concrete integers specifically.
6648eb7a74bSArtem Dergachev     CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
6658eb7a74bSArtem Dergachev     if (const Expr *E = dyn_cast_or_null<Expr>(S)) {
6668eb7a74bSArtem Dergachev       Dest = MRMgr.getCXXTempObjectRegion(E, Pred->getLocationContext());
6678eb7a74bSArtem Dergachev     } else {
6688eb7a74bSArtem Dergachev       static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
6698eb7a74bSArtem Dergachev       NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
6708eb7a74bSArtem Dergachev       Bldr.generateSink(Pred->getLocation().withTag(&T),
6718eb7a74bSArtem Dergachev                         Pred->getState(), Pred);
6728eb7a74bSArtem Dergachev       return;
6738eb7a74bSArtem Dergachev     }
6748eb7a74bSArtem Dergachev   }
6758eb7a74bSArtem Dergachev 
676fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
677fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
6782bc9674bSJordan Rose       CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
679142adc49STed Kremenek 
68017a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
68117a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
68217a8757aSJordan Rose                                 "Error evaluating destructor");
68317a8757aSJordan Rose 
6844ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
6854ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
686fcd016e5SJordan Rose                                             *Call, *this);
68710e25031STed Kremenek 
6884ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
689d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
6904ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
6914ee71b8aSJordan Rose        I != E; ++I)
692690ab040SArtem Dergachev     defaultEvalCall(Bldr, *I, *Call, CallOpts);
6934ee71b8aSJordan Rose 
6944ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
695fcd016e5SJordan Rose                                              *Call, *this);
69610e25031STed Kremenek }
69710e25031STed Kremenek 
6988b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
6998b808d64SJordan Rose                                           ExplodedNode *Pred,
7008b808d64SJordan Rose                                           ExplodedNodeSet &Dst) {
7018b808d64SJordan Rose   ProgramStateRef State = Pred->getState();
7028b808d64SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
7038b808d64SJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
704a6e4358fSStephen Kelly                                 CNE->getBeginLoc(),
7058b808d64SJordan Rose                                 "Error evaluating New Allocator Call");
7068b808d64SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
7078b808d64SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
7088b808d64SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
7098b808d64SJordan Rose 
7108b808d64SJordan Rose   ExplodedNodeSet DstPreCall;
7118b808d64SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
7128b808d64SJordan Rose                                             *Call, *this);
7138b808d64SJordan Rose 
71455796302SArtem Dergachev   ExplodedNodeSet DstPostCall;
71555796302SArtem Dergachev   StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
716*f2be30deSKirstóf Umann   for (ExplodedNode *I : DstPreCall) {
7171084de52SArtem Dergachev     // FIXME: Provide evalCall for checkers?
71855796302SArtem Dergachev     defaultEvalCall(CallBldr, I, *Call);
7191084de52SArtem Dergachev   }
720beba5307SArtem Dergachev   // If the call is inlined, DstPostCall will be empty and we bail out now.
72155796302SArtem Dergachev 
72255796302SArtem Dergachev   // Store return value of operator new() for future use, until the actual
72355796302SArtem Dergachev   // CXXNewExpr gets processed.
72455796302SArtem Dergachev   ExplodedNodeSet DstPostValue;
72555796302SArtem Dergachev   StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
726*f2be30deSKirstóf Umann   for (ExplodedNode *I : DstPostCall) {
727beba5307SArtem Dergachev     // FIXME: Because CNE serves as the "call site" for the allocator (due to
728beba5307SArtem Dergachev     // lack of a better expression in the AST), the conjured return value symbol
729beba5307SArtem Dergachev     // is going to be of the same type (C++ object pointer type). Technically
730beba5307SArtem Dergachev     // this is not correct because the operator new's prototype always says that
731beba5307SArtem Dergachev     // it returns a 'void *'. So we should change the type of the symbol,
732beba5307SArtem Dergachev     // and then evaluate the cast over the symbolic pointer from 'void *' to
733beba5307SArtem Dergachev     // the object pointer type. But without changing the symbol's type it
734beba5307SArtem Dergachev     // is breaking too much to evaluate the no-op symbolic cast over it, so we
735beba5307SArtem Dergachev     // skip it for now.
73655796302SArtem Dergachev     ProgramStateRef State = I->getState();
73750e0372fSArtem Dergachev     SVal RetVal = State->getSVal(CNE, LCtx);
73850e0372fSArtem Dergachev 
73950e0372fSArtem Dergachev     // If this allocation function is not declared as non-throwing, failures
74050e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
74150e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
74250e0372fSArtem Dergachev     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
74350e0372fSArtem Dergachev     // where new can return NULL. If we end up supporting that option, we can
74450e0372fSArtem Dergachev     // consider adding a check for it here.
74550e0372fSArtem Dergachev     // C++11 [basic.stc.dynamic.allocation]p3.
74650e0372fSArtem Dergachev     if (const FunctionDecl *FD = CNE->getOperatorNew()) {
74750e0372fSArtem Dergachev       QualType Ty = FD->getType();
74850e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
749eaf11ad7SRichard Smith         if (!ProtoType->isNothrow())
75050e0372fSArtem Dergachev           State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
75150e0372fSArtem Dergachev     }
75250e0372fSArtem Dergachev 
753f83d5479SArtem Dergachev     ValueBldr.generateNode(
754f83d5479SArtem Dergachev         CNE, I, addObjectUnderConstruction(State, CNE, LCtx, RetVal));
75555796302SArtem Dergachev   }
75655796302SArtem Dergachev 
75713b2026bSArtem Dergachev   ExplodedNodeSet DstPostPostCallCallback;
75813b2026bSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
75913b2026bSArtem Dergachev                                              DstPostValue, *Call, *this);
760*f2be30deSKirstóf Umann   for (ExplodedNode *I : DstPostPostCallCallback) {
761*f2be30deSKirstóf Umann     getCheckerManager().runCheckersForNewAllocator(*Call, Dst, I, *this);
76213b2026bSArtem Dergachev   }
7638b808d64SJordan Rose }
7648b808d64SJordan Rose 
76510e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
76610e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
767a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
768a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
769a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
770a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
77110e25031STed Kremenek 
772d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
773d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
77455796302SArtem Dergachev   SVal symVal = UnknownVal();
7758b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
77610e25031STed Kremenek 
7771084de52SArtem Dergachev   bool IsStandardGlobalOpNewFunction =
7781084de52SArtem Dergachev       FD->isReplaceableGlobalAllocationFunction();
7798b662704SAnton Yartsev 
78055796302SArtem Dergachev   ProgramStateRef State = Pred->getState();
78155796302SArtem Dergachev 
78255796302SArtem Dergachev   // Retrieve the stored operator new() return value.
783549f9cd4SKristof Umann   if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
784f83d5479SArtem Dergachev     symVal = *getObjectUnderConstruction(State, CNE, LCtx);
785f83d5479SArtem Dergachev     State = finishObjectConstruction(State, CNE, LCtx);
78655796302SArtem Dergachev   }
78755796302SArtem Dergachev 
7888b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
7898b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
7908b662704SAnton Yartsev   // a custom global allocator.
79155796302SArtem Dergachev   if (symVal.isUnknown()) {
7928b662704SAnton Yartsev     if (IsStandardGlobalOpNewFunction)
7938b662704SAnton Yartsev       symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
7948b662704SAnton Yartsev     else
7950dbb783cSCraig Topper       symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
7968b662704SAnton Yartsev                                             blockCount);
79755796302SArtem Dergachev   }
7988b662704SAnton Yartsev 
799fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
800fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
801fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
802fcd016e5SJordan Rose 
803549f9cd4SKristof Umann   if (!AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
804a4ee064cSJordan Rose     // Invalidate placement args.
805fcd016e5SJordan Rose     // FIXME: Once we figure out how we want allocators to work,
80672649423SKristof Umann     // we should be using the usual pre-/(default-)eval-/post-call checkers
80772649423SKristof Umann     // here.
808fcd016e5SJordan Rose     State = Call->invalidateRegions(blockCount);
809b8cb8359SJordan Rose     if (!State)
810b8cb8359SJordan Rose       return;
811a4ee064cSJordan Rose 
812fce1b03eSPavel Labath     // If this allocation function is not declared as non-throwing, failures
81350e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
81450e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
815fce1b03eSPavel Labath     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
816fce1b03eSPavel Labath     // where new can return NULL. If we end up supporting that option, we can
817fce1b03eSPavel Labath     // consider adding a check for it here.
8183957fd58SJordan Rose     // C++11 [basic.stc.dynamic.allocation]p3.
819fce1b03eSPavel Labath     if (FD) {
8203957fd58SJordan Rose       QualType Ty = FD->getType();
82150e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
822eaf11ad7SRichard Smith         if (!ProtoType->isNothrow())
82355796302SArtem Dergachev           if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
82455796302SArtem Dergachev             State = State->assume(*dSymVal, true);
8253957fd58SJordan Rose     }
82650e0372fSArtem Dergachev   }
8273957fd58SJordan Rose 
828b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
829b8cb8359SJordan Rose 
830beba5307SArtem Dergachev   SVal Result = symVal;
831beba5307SArtem Dergachev 
83210e25031STed Kremenek   if (CNE->isArray()) {
83310e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
83410e25031STed Kremenek     // For now, just return a symbolicated region.
835630f7dafSArtem Dergachev     if (const auto *NewReg = cast_or_null<SubRegion>(symVal.getAsRegion())) {
836630f7dafSArtem Dergachev       QualType ObjTy = CNE->getType()->getPointeeType();
837a4ee064cSJordan Rose       const ElementRegion *EleReg =
838a4ee064cSJordan Rose           getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
839beba5307SArtem Dergachev       Result = loc::MemRegionVal(EleReg);
840beba5307SArtem Dergachev     }
841beba5307SArtem Dergachev     State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
84244320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
84310e25031STed Kremenek     return;
84410e25031STed Kremenek   }
84510e25031STed Kremenek 
846a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
847a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
848a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
849a4ee064cSJordan Rose   // the constructor call anyway.)
85044320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
85144320367SJordan Rose     // Non-array placement new should always return the placement location.
85244320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
853b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
854aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
85544320367SJordan Rose   }
85644320367SJordan Rose 
857b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
858b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
8598f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
8608f6b4b04SJordan Rose   if (!NewN)
8618f6b4b04SJordan Rose     return;
8623503cb35SJordan Rose 
8635089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
8645089f3b3SJordan Rose   // initializer. Copy the value over.
8655089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
8665089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
8673503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
8688f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
869b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
870b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
8713503cb35SJordan Rose     }
8723503cb35SJordan Rose   }
87310e25031STed Kremenek }
87410e25031STed Kremenek 
87510e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
87610e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
8779d69072fSKirstóf Umann 
8789d69072fSKirstóf Umann   CallEventManager &CEMgr = getStateManager().getCallEventManager();
8799d69072fSKirstóf Umann   CallEventRef<CXXDeallocatorCall> Call = CEMgr.getCXXDeallocatorCall(
8809d69072fSKirstóf Umann       CDE, Pred->getState(), Pred->getLocationContext());
8819d69072fSKirstóf Umann 
8829d69072fSKirstóf Umann   ExplodedNodeSet DstPreCall;
8839d69072fSKirstóf Umann   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, *Call, *this);
8849d69072fSKirstóf Umann 
8859d69072fSKirstóf Umann   getCheckerManager().runCheckersForPostCall(Dst, DstPreCall, *Call, *this);
88610e25031STed Kremenek }
88710e25031STed Kremenek 
8889d69072fSKirstóf Umann void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred,
8898fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
8908fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
891b1279b5cSTed Kremenek   if (!VD) {
892b1279b5cSTed Kremenek     Dst.Add(Pred);
893b1279b5cSTed Kremenek     return;
894b1279b5cSTed Kremenek   }
895b1279b5cSTed Kremenek 
8968fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
897d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
898d94854a4STed Kremenek                                         currBldrCtx->blockCount());
8998fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
900b570195cSAnna Zaks   state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
9018fdb59f9STed Kremenek 
902d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
9038fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
9048fdb59f9STed Kremenek }
9058fdb59f9STed Kremenek 
90610e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
90710e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
908d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
909e594034fSAnna Zaks 
91010e25031STed Kremenek   // Get the this object region from StoreManager.
911632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
91210e25031STed Kremenek   const MemRegion *R =
91310e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
91410e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
915632e3b7eSTed Kremenek                                                     LCtx);
91610e25031STed Kremenek 
91749b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
91810e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
919632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
92010e25031STed Kremenek }
92115843343SGabor Horvath 
92215843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
92315843343SGabor Horvath                                  ExplodedNodeSet &Dst) {
92415843343SGabor Horvath   const LocationContext *LocCtxt = Pred->getLocationContext();
92515843343SGabor Horvath 
92615843343SGabor Horvath   // Get the region of the lambda itself.
92715843343SGabor Horvath   const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
92815843343SGabor Horvath       LE, LocCtxt);
92915843343SGabor Horvath   SVal V = loc::MemRegionVal(R);
93015843343SGabor Horvath 
93115843343SGabor Horvath   ProgramStateRef State = Pred->getState();
93215843343SGabor Horvath 
93315843343SGabor Horvath   // If we created a new MemRegion for the lambda, we should explicitly bind
93415843343SGabor Horvath   // the captures.
93515843343SGabor Horvath   CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
93615843343SGabor Horvath   for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
93715843343SGabor Horvath                                                e = LE->capture_init_end();
93815843343SGabor Horvath        i != e; ++i, ++CurField) {
9397bdca8b2SDevin Coughlin     FieldDecl *FieldForCapture = *CurField;
9407bdca8b2SDevin Coughlin     SVal FieldLoc = State->getLValue(FieldForCapture, V);
9417bdca8b2SDevin Coughlin 
9427bdca8b2SDevin Coughlin     SVal InitVal;
9437bdca8b2SDevin Coughlin     if (!FieldForCapture->hasCapturedVLAType()) {
9447bdca8b2SDevin Coughlin       Expr *InitExpr = *i;
9457bdca8b2SDevin Coughlin       assert(InitExpr && "Capture missing initialization expression");
9467bdca8b2SDevin Coughlin       InitVal = State->getSVal(InitExpr, LocCtxt);
9477bdca8b2SDevin Coughlin     } else {
9487bdca8b2SDevin Coughlin       // The field stores the length of a captured variable-length array.
9497bdca8b2SDevin Coughlin       // These captures don't have initialization expressions; instead we
9507bdca8b2SDevin Coughlin       // get the length from the VLAType size expression.
9517bdca8b2SDevin Coughlin       Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
9527bdca8b2SDevin Coughlin       InitVal = State->getSVal(SizeExpr, LocCtxt);
9537bdca8b2SDevin Coughlin     }
9547bdca8b2SDevin Coughlin 
955b570195cSAnna Zaks     State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
95615843343SGabor Horvath   }
95715843343SGabor Horvath 
95815843343SGabor Horvath   // Decay the Loc into an RValue, because there might be a
95915843343SGabor Horvath   // MaterializeTemporaryExpr node above this one which expects the bound value
96015843343SGabor Horvath   // to be an RValue.
96115843343SGabor Horvath   SVal LambdaRVal = State->getSVal(R);
96215843343SGabor Horvath 
96315843343SGabor Horvath   ExplodedNodeSet Tmp;
96415843343SGabor Horvath   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
96515843343SGabor Horvath   // FIXME: is this the right program point kind?
96615843343SGabor Horvath   Bldr.generateNode(LE, Pred,
96715843343SGabor Horvath                     State->BindExpr(LE, LocCtxt, LambdaRVal),
96815843343SGabor Horvath                     nullptr, ProgramPoint::PostLValueKind);
96915843343SGabor Horvath 
97015843343SGabor Horvath   // FIXME: Move all post/pre visits to ::Visit().
97115843343SGabor Horvath   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
97215843343SGabor Horvath }
973