110e25031STed Kremenek //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
210e25031STed Kremenek //
310e25031STed Kremenek //                     The LLVM Compiler Infrastructure
410e25031STed Kremenek //
510e25031STed Kremenek // This file is distributed under the University of Illinois Open Source
610e25031STed Kremenek // License. See LICENSE.TXT for details.
710e25031STed Kremenek //
810e25031STed Kremenek //===----------------------------------------------------------------------===//
910e25031STed Kremenek //
1010e25031STed Kremenek //  This file defines the C++ expression evaluation engine.
1110e25031STed Kremenek //
1210e25031STed Kremenek //===----------------------------------------------------------------------===//
1310e25031STed Kremenek 
1410e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
1510e25031STed Kremenek #include "clang/AST/DeclCXX.h"
168fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h"
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);
302a14c695STed Kremenek   const Expr *tempExpr = ME->GetTemporaryExpr()->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;
443d7b7f52SJordan Rose   if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
453d7b7f52SJordan Rose     assert(Ctor->getDecl()->isTrivial());
463d7b7f52SJordan Rose     assert(Ctor->getDecl()->isCopyOrMoveConstructor());
473d7b7f52SJordan Rose     ThisVal = Ctor->getCXXThisVal();
483d7b7f52SJordan Rose     AlwaysReturnsLValue = false;
493d7b7f52SJordan Rose   } else {
503d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
513d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
523d7b7f52SJordan Rose            OO_Equal);
533d7b7f52SJordan Rose     ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
543d7b7f52SJordan Rose     AlwaysReturnsLValue = true;
553d7b7f52SJordan Rose   }
5688bb563cSJordan Rose 
5788bb563cSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
5888bb563cSJordan Rose 
5988bb563cSJordan Rose   ExplodedNodeSet Dst;
6088bb563cSJordan Rose   Bldr.takeNodes(Pred);
6188bb563cSJordan Rose 
6288bb563cSJordan Rose   SVal V = Call.getArgSVal(0);
6388bb563cSJordan Rose 
643d7b7f52SJordan Rose   // If the value being copied is not unknown, load from its location to get
653d7b7f52SJordan Rose   // an aggregate rvalue.
6605785d16SDavid Blaikie   if (Optional<Loc> L = V.getAs<Loc>())
6788bb563cSJordan Rose     V = Pred->getState()->getSVal(*L);
683d7b7f52SJordan Rose   else
6975f9d3acSArtem Dergachev     assert(V.isUnknownOrUndef());
7088bb563cSJordan Rose 
713d7b7f52SJordan Rose   const Expr *CallExpr = Call.getOriginExpr();
723d7b7f52SJordan Rose   evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
7388bb563cSJordan Rose 
743d7b7f52SJordan Rose   PostStmt PS(CallExpr, LCtx);
7588bb563cSJordan Rose   for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
7688bb563cSJordan Rose        I != E; ++I) {
7788bb563cSJordan Rose     ProgramStateRef State = (*I)->getState();
783d7b7f52SJordan Rose     if (AlwaysReturnsLValue)
793d7b7f52SJordan Rose       State = State->BindExpr(CallExpr, LCtx, ThisVal);
803d7b7f52SJordan Rose     else
8188bb563cSJordan Rose       State = bindReturnValue(Call, LCtx, State);
8288bb563cSJordan Rose     Bldr.generateNode(PS, State, *I);
8388bb563cSJordan Rose   }
8488bb563cSJordan Rose }
8588bb563cSJordan Rose 
86bc74eb1cSJordan Rose 
87*168e29f6SArtem Dergachev SVal ExprEngine::makeZeroElementRegion(ProgramStateRef State, SVal LValue,
88690ab040SArtem Dergachev                                        QualType &Ty, bool &IsArray) {
89bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
90bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
91bc74eb1cSJordan Rose 
92bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
93bc74eb1cSJordan Rose     Ty = AT->getElementType();
94bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
95690ab040SArtem Dergachev     IsArray = true;
96bc74eb1cSJordan Rose   }
97bc74eb1cSJordan Rose 
98bc74eb1cSJordan Rose   return LValue;
99bc74eb1cSJordan Rose }
100bc74eb1cSJordan Rose 
10117f41608SJordan Rose 
102412c0af2SDevin Coughlin const MemRegion *
103412c0af2SDevin Coughlin ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
104690ab040SArtem Dergachev                                           ExplodedNode *Pred,
105690ab040SArtem Dergachev                                           EvalCallOptions &CallOpts) {
106b970505dSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
10705375eb4SJordan Rose   ProgramStateRef State = Pred->getState();
10894020268SArtem Dergachev   MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
10905375eb4SJordan Rose 
110afb158c2SArtem Dergachev   // See if we're constructing an existing region by looking at the
111afb158c2SArtem Dergachev   // current construction context.
112afb158c2SArtem Dergachev   const NodeBuilderContext &CurrBldrCtx = getBuilderContext();
113afb158c2SArtem Dergachev   const CFGBlock *B = CurrBldrCtx.getBlock();
114afb158c2SArtem Dergachev   const CFGElement &E = (*B)[currStmtIdx];
115afb158c2SArtem Dergachev   if (auto CC = E.getAs<CFGConstructor>()) {
116afb158c2SArtem Dergachev     if (const Stmt *TriggerStmt = CC->getTriggerStmt()) {
117afb158c2SArtem Dergachev       if (const CXXNewExpr *CNE = dyn_cast<CXXNewExpr>(TriggerStmt)) {
11855796302SArtem Dergachev         if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
11955796302SArtem Dergachev           // TODO: Detect when the allocator returns a null pointer.
12055796302SArtem Dergachev           // Constructor shall not be called in this case.
121beba5307SArtem Dergachev           if (const SubRegion *MR = dyn_cast_or_null<SubRegion>(
122beba5307SArtem Dergachev                   getCXXNewAllocatorValue(State, CNE, LCtx).getAsRegion())) {
123beba5307SArtem Dergachev             if (CNE->isArray()) {
124690ab040SArtem Dergachev               // TODO: In fact, we need to call the constructor for every
125690ab040SArtem Dergachev               // allocated element, not just the first one!
126*168e29f6SArtem Dergachev               CallOpts.IsArrayCtorOrDtor = true;
127beba5307SArtem Dergachev               return getStoreManager().GetElementZeroRegion(
128beba5307SArtem Dergachev                   MR, CNE->getType()->getPointeeType());
129beba5307SArtem Dergachev             }
13055796302SArtem Dergachev             return MR;
13155796302SArtem Dergachev           }
132beba5307SArtem Dergachev         }
133afb158c2SArtem Dergachev       } else if (auto *DS = dyn_cast<DeclStmt>(TriggerStmt)) {
134e231bd34SArtem Dergachev         const auto *Var = cast<VarDecl>(DS->getSingleDecl());
135bc74eb1cSJordan Rose         SVal LValue = State->getLValue(Var, LCtx);
13620edae87SJordan Rose         QualType Ty = Var->getType();
137e231bd34SArtem Dergachev         LValue = makeZeroElementRegion(State, LValue, Ty,
138*168e29f6SArtem Dergachev                                        CallOpts.IsArrayCtorOrDtor);
13917f41608SJordan Rose         return LValue.getAsRegion();
14094020268SArtem Dergachev       } else if (auto *RS = dyn_cast<ReturnStmt>(TriggerStmt)) {
14194020268SArtem Dergachev         // TODO: We should construct into a CXXBindTemporaryExpr or a
14294020268SArtem Dergachev         // MaterializeTemporaryExpr around the call-expression on the previous
14394020268SArtem Dergachev         // stack frame. Currently we re-bind the temporary to the correct region
14494020268SArtem Dergachev         // later, but that's not semantically correct. This of course does not
14594020268SArtem Dergachev         // apply when we're in the top frame. But if we are in an inlined
14694020268SArtem Dergachev         // function, we should be able to take the call-site CFG element,
14794020268SArtem Dergachev         // and it should contain (but right now it wouldn't) some sort of
14894020268SArtem Dergachev         // construction context that'd give us the right temporary expression.
149*168e29f6SArtem Dergachev         CallOpts.IsTemporaryCtorOrDtor = true;
15094020268SArtem Dergachev         return MRMgr.getCXXTempObjectRegion(CE, LCtx);
15120edae87SJordan Rose       }
152afb158c2SArtem Dergachev       // TODO: Consider other directly initialized elements.
153afb158c2SArtem Dergachev     } else if (const CXXCtorInitializer *Init = CC->getTriggerInit()) {
15454529a34SJordan Rose       assert(Init->isAnyMemberInitializer());
15554529a34SJordan Rose       const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
156412c0af2SDevin Coughlin       Loc ThisPtr =
157412c0af2SDevin Coughlin       getSValBuilder().getCXXThis(CurCtor, LCtx->getCurrentStackFrame());
15854529a34SJordan Rose       SVal ThisVal = State->getSVal(ThisPtr);
15954529a34SJordan Rose 
160bc74eb1cSJordan Rose       const ValueDecl *Field;
161bc74eb1cSJordan Rose       SVal FieldVal;
16254529a34SJordan Rose       if (Init->isIndirectMemberInitializer()) {
163bc74eb1cSJordan Rose         Field = Init->getIndirectMember();
164bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
16554529a34SJordan Rose       } else {
166bc74eb1cSJordan Rose         Field = Init->getMember();
167bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getMember(), ThisVal);
16854529a34SJordan Rose       }
169bc74eb1cSJordan Rose 
170bc74eb1cSJordan Rose       QualType Ty = Field->getType();
171690ab040SArtem Dergachev       FieldVal = makeZeroElementRegion(State, FieldVal, Ty,
172*168e29f6SArtem Dergachev                                        CallOpts.IsArrayCtorOrDtor);
17317f41608SJordan Rose       return FieldVal.getAsRegion();
17454529a34SJordan Rose     }
17554529a34SJordan Rose 
17605375eb4SJordan Rose     // FIXME: This will eventually need to handle new-expressions as well.
17717f41608SJordan Rose     // Don't forget to update the pre-constructor initialization code in
17817f41608SJordan Rose     // ExprEngine::VisitCXXConstructExpr.
17905375eb4SJordan Rose   }
180c9318304SJordan Rose   // If we couldn't find an existing region to construct into, assume we're
181690ab040SArtem Dergachev   // constructing a temporary. Notify the caller of our failure.
182*168e29f6SArtem Dergachev   CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
18317f41608SJordan Rose   return MRMgr.getCXXTempObjectRegion(CE, LCtx);
184c9318304SJordan Rose }
18505375eb4SJordan Rose 
186412c0af2SDevin Coughlin const CXXConstructExpr *
187412c0af2SDevin Coughlin ExprEngine::findDirectConstructorForCurrentCFGElement() {
188412c0af2SDevin Coughlin   // Go backward in the CFG to see if the previous element (ignoring
189412c0af2SDevin Coughlin   // destructors) was a CXXConstructExpr. If so, that constructor
190412c0af2SDevin Coughlin   // was constructed directly into an existing region.
191412c0af2SDevin Coughlin   // This process is essentially the inverse of that performed in
192412c0af2SDevin Coughlin   // findElementDirectlyInitializedByCurrentConstructor().
193412c0af2SDevin Coughlin   if (currStmtIdx == 0)
194412c0af2SDevin Coughlin     return nullptr;
195412c0af2SDevin Coughlin 
196412c0af2SDevin Coughlin   const CFGBlock *B = getBuilderContext().getBlock();
197412c0af2SDevin Coughlin 
198412c0af2SDevin Coughlin   unsigned int PreviousStmtIdx = currStmtIdx - 1;
199412c0af2SDevin Coughlin   CFGElement Previous = (*B)[PreviousStmtIdx];
200412c0af2SDevin Coughlin 
201412c0af2SDevin Coughlin   while (Previous.getAs<CFGImplicitDtor>() && PreviousStmtIdx > 0) {
202412c0af2SDevin Coughlin     --PreviousStmtIdx;
203412c0af2SDevin Coughlin     Previous = (*B)[PreviousStmtIdx];
204412c0af2SDevin Coughlin   }
205412c0af2SDevin Coughlin 
206412c0af2SDevin Coughlin   if (Optional<CFGStmt> PrevStmtElem = Previous.getAs<CFGStmt>()) {
207412c0af2SDevin Coughlin     if (auto *CtorExpr = dyn_cast<CXXConstructExpr>(PrevStmtElem->getStmt())) {
208412c0af2SDevin Coughlin       return CtorExpr;
209412c0af2SDevin Coughlin     }
210412c0af2SDevin Coughlin   }
211412c0af2SDevin Coughlin 
212412c0af2SDevin Coughlin   return nullptr;
213412c0af2SDevin Coughlin }
214412c0af2SDevin Coughlin 
21517f41608SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
21617f41608SJordan Rose                                        ExplodedNode *Pred,
21717f41608SJordan Rose                                        ExplodedNodeSet &destNodes) {
21817f41608SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
21917f41608SJordan Rose   ProgramStateRef State = Pred->getState();
22017f41608SJordan Rose 
2210dbb783cSCraig Topper   const MemRegion *Target = nullptr;
22217f41608SJordan Rose 
22317f41608SJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
22417f41608SJordan Rose   // For now, we just run the first constructor (which should still invalidate
22517f41608SJordan Rose   // the entire array).
22617f41608SJordan Rose 
227690ab040SArtem Dergachev   EvalCallOptions CallOpts;
228690ab040SArtem Dergachev 
22917f41608SJordan Rose   switch (CE->getConstructionKind()) {
23017f41608SJordan Rose   case CXXConstructExpr::CK_Complete: {
231690ab040SArtem Dergachev     Target = getRegionForConstructedObject(CE, Pred, CallOpts);
232b970505dSJordan Rose     break;
233b970505dSJordan Rose   }
234b3b976f0SJordan Rose   case CXXConstructExpr::CK_VirtualBase:
235b3b976f0SJordan Rose     // Make sure we are not calling virtual base class initializers twice.
236b3b976f0SJordan Rose     // Only the most-derived object should initialize virtual base classes.
237b3b976f0SJordan Rose     if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) {
238b3b976f0SJordan Rose       const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer);
239b3b976f0SJordan Rose       if (OuterCtor) {
240b3b976f0SJordan Rose         switch (OuterCtor->getConstructionKind()) {
241b970505dSJordan Rose         case CXXConstructExpr::CK_NonVirtualBase:
242b970505dSJordan Rose         case CXXConstructExpr::CK_VirtualBase:
243b3b976f0SJordan Rose           // Bail out!
244b3b976f0SJordan Rose           destNodes.Add(Pred);
245b3b976f0SJordan Rose           return;
246b3b976f0SJordan Rose         case CXXConstructExpr::CK_Complete:
247b3b976f0SJordan Rose         case CXXConstructExpr::CK_Delegating:
248b3b976f0SJordan Rose           break;
249b3b976f0SJordan Rose         }
250b3b976f0SJordan Rose       }
251b3b976f0SJordan Rose     }
252b3b976f0SJordan Rose     // FALLTHROUGH
253b3b976f0SJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
254e8ba3ec4SArtem Dergachev     // In C++17, classes with non-virtual bases may be aggregates, so they would
255e8ba3ec4SArtem Dergachev     // be initialized as aggregates without a constructor call, so we may have
256e8ba3ec4SArtem Dergachev     // a base class constructed directly into an initializer list without
257e8ba3ec4SArtem Dergachev     // having the derived-class constructor call on the previous stack frame.
258e8ba3ec4SArtem Dergachev     // Initializer lists may be nested into more initializer lists that
259e8ba3ec4SArtem Dergachev     // correspond to surrounding aggregate initializations.
260e8ba3ec4SArtem Dergachev     // FIXME: For now this code essentially bails out. We need to find the
261e8ba3ec4SArtem Dergachev     // correct target region and set it.
262e8ba3ec4SArtem Dergachev     // FIXME: Instead of relying on the ParentMap, we should have the
263e8ba3ec4SArtem Dergachev     // trigger-statement (InitListExpr in this case) passed down from CFG or
264e8ba3ec4SArtem Dergachev     // otherwise always available during construction.
265e8ba3ec4SArtem Dergachev     if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(CE))) {
266e8ba3ec4SArtem Dergachev       MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
267e8ba3ec4SArtem Dergachev       Target = MRMgr.getCXXTempObjectRegion(CE, LCtx);
268*168e29f6SArtem Dergachev       CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
269e8ba3ec4SArtem Dergachev       break;
270e8ba3ec4SArtem Dergachev     }
271e8ba3ec4SArtem Dergachev     // FALLTHROUGH
272b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
273b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
274b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
275b970505dSJordan Rose                                               LCtx->getCurrentStackFrame());
27605375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
277b970505dSJordan Rose 
278b970505dSJordan Rose     if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
279b970505dSJordan Rose       Target = ThisVal.getAsRegion();
280b970505dSJordan Rose     } else {
281b970505dSJordan Rose       // Cast to the base type.
282599f85abSJordan Rose       bool IsVirtual =
283599f85abSJordan Rose         (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
284599f85abSJordan Rose       SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
285599f85abSJordan Rose                                                          IsVirtual);
28620edae87SJordan Rose       Target = BaseVal.getAsRegion();
287b970505dSJordan Rose     }
288b970505dSJordan Rose     break;
289b970505dSJordan Rose   }
290b970505dSJordan Rose   }
291b970505dSJordan Rose 
292fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
293fcd016e5SJordan Rose   CallEventRef<CXXConstructorCall> Call =
294fcd016e5SJordan Rose     CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
29510e25031STed Kremenek 
296f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
297f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
298b8e28654SJordan Rose 
299b8e28654SJordan Rose   ExplodedNodeSet PreInitialized;
300b8e28654SJordan Rose   {
301b8e28654SJordan Rose     StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
302b8e28654SJordan Rose     if (CE->requiresZeroInitialization()) {
303b8e28654SJordan Rose       // Type of the zero doesn't matter.
304b8e28654SJordan Rose       SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy);
305b8e28654SJordan Rose 
306b8e28654SJordan Rose       for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
307b8e28654SJordan Rose                                      E = DstPreVisit.end();
308b8e28654SJordan Rose            I != E; ++I) {
309b8e28654SJordan Rose         ProgramStateRef State = (*I)->getState();
310b8e28654SJordan Rose         // FIXME: Once we properly handle constructors in new-expressions, we'll
311b8e28654SJordan Rose         // need to invalidate the region before setting a default value, to make
312b8e28654SJordan Rose         // sure there aren't any lingering bindings around. This probably needs
313b8e28654SJordan Rose         // to happen regardless of whether or not the object is zero-initialized
314b8e28654SJordan Rose         // to handle random fields of a placement-initialized object picking up
315b8e28654SJordan Rose         // old bindings. We might only want to do it when we need to, though.
316b8e28654SJordan Rose         // FIXME: This isn't actually correct for arrays -- we need to zero-
317b8e28654SJordan Rose         // initialize the entire array, not just the first element -- but our
318b8e28654SJordan Rose         // handling of arrays everywhere else is weak as well, so this shouldn't
319b8e28654SJordan Rose         // actually make things worse. Placement new makes this tricky as well,
320b8e28654SJordan Rose         // since it's then possible to be initializing one part of a multi-
321b8e28654SJordan Rose         // dimensional array.
322b570195cSAnna Zaks         State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal, LCtx);
3230dbb783cSCraig Topper         Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
3240dbb783cSCraig Topper                           ProgramPoint::PreStmtKind);
325b8e28654SJordan Rose       }
326b8e28654SJordan Rose     }
327b8e28654SJordan Rose   }
328b8e28654SJordan Rose 
329afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
330b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
331fcd016e5SJordan Rose                                             *Call, *this);
33210e25031STed Kremenek 
33388bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
33488bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
33588bb563cSJordan Rose 
336e83cb092SJordan Rose   if (CE->getConstructor()->isTrivial() &&
337e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
338*168e29f6SArtem Dergachev       !CallOpts.IsArrayCtorOrDtor) {
339e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
340e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
34188bb563cSJordan Rose          I != E; ++I)
34288bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
34388bb563cSJordan Rose 
34488bb563cSJordan Rose   } else {
345afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
346f3c12ac3SJordan Rose          I != E; ++I)
347690ab040SArtem Dergachev       defaultEvalCall(Bldr, *I, *Call, CallOpts);
34888bb563cSJordan Rose   }
34910e25031STed Kremenek 
3505b1ee2faSDevin Coughlin   // If the CFG was contructed without elements for temporary destructors
3515b1ee2faSDevin Coughlin   // and the just-called constructor created a temporary object then
3525b1ee2faSDevin Coughlin   // stop exploration if the temporary object has a noreturn constructor.
3535b1ee2faSDevin Coughlin   // This can lose coverage because the destructor, if it were present
3545b1ee2faSDevin Coughlin   // in the CFG, would be called at the end of the full expression or
3555b1ee2faSDevin Coughlin   // later (for life-time extended temporaries) -- but avoids infeasible
3565b1ee2faSDevin Coughlin   // paths when no-return temporary destructors are used for assertions.
3575b1ee2faSDevin Coughlin   const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
3585b1ee2faSDevin Coughlin   if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
3595b1ee2faSDevin Coughlin     const MemRegion *Target = Call->getCXXThisVal().getAsRegion();
3605b1ee2faSDevin Coughlin     if (Target && isa<CXXTempObjectRegion>(Target) &&
3615b1ee2faSDevin Coughlin         Call->getDecl()->getParent()->isAnyDestructorNoReturn()) {
3625b1ee2faSDevin Coughlin 
3633da72051SArtem Dergachev       // If we've inlined the constructor, then DstEvaluated would be empty.
3643da72051SArtem Dergachev       // In this case we still want a sink, which could be implemented
3653da72051SArtem Dergachev       // in processCallExit. But we don't have that implemented at the moment,
3663da72051SArtem Dergachev       // so if you hit this assertion, see if you can avoid inlining
3673da72051SArtem Dergachev       // the respective constructor when analyzer-config cfg-temporary-dtors
3683da72051SArtem Dergachev       // is set to false.
3693da72051SArtem Dergachev       // Otherwise there's nothing wrong with inlining such constructor.
3703da72051SArtem Dergachev       assert(!DstEvaluated.empty() &&
3713da72051SArtem Dergachev              "We should not have inlined this constructor!");
3723da72051SArtem Dergachev 
3735b1ee2faSDevin Coughlin       for (ExplodedNode *N : DstEvaluated) {
3745b1ee2faSDevin Coughlin         Bldr.generateSink(CE, N, N->getState());
3755b1ee2faSDevin Coughlin       }
3765b1ee2faSDevin Coughlin 
3775b1ee2faSDevin Coughlin       // There is no need to run the PostCall and PostStmt checker
3785b1ee2faSDevin Coughlin       // callbacks because we just generated sinks on all nodes in th
3795b1ee2faSDevin Coughlin       // frontier.
3805b1ee2faSDevin Coughlin       return;
3815b1ee2faSDevin Coughlin     }
3825b1ee2faSDevin Coughlin   }
3835b1ee2faSDevin Coughlin 
384afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
38588bb563cSJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
386fcd016e5SJordan Rose                                              *Call, *this);
387afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
38810e25031STed Kremenek }
38910e25031STed Kremenek 
390b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
39110e25031STed Kremenek                                     const MemRegion *Dest,
39210e25031STed Kremenek                                     const Stmt *S,
3932bc9674bSJordan Rose                                     bool IsBaseDtor,
39410e25031STed Kremenek                                     ExplodedNode *Pred,
395*168e29f6SArtem Dergachev                                     ExplodedNodeSet &Dst,
396*168e29f6SArtem Dergachev                                     const EvalCallOptions &CallOpts) {
397fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
398fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
399fcd016e5SJordan Rose 
400b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
401b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
402b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
403b970505dSJordan Rose 
404fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
405fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
4062bc9674bSJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
407142adc49STed Kremenek 
40817a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
40917a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
41017a8757aSJordan Rose                                 "Error evaluating destructor");
41117a8757aSJordan Rose 
4124ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
4134ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
414fcd016e5SJordan Rose                                             *Call, *this);
41510e25031STed Kremenek 
4164ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
417d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
4184ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
4194ee71b8aSJordan Rose        I != E; ++I)
420690ab040SArtem Dergachev     defaultEvalCall(Bldr, *I, *Call, CallOpts);
4214ee71b8aSJordan Rose 
4224ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
4234ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
424fcd016e5SJordan Rose                                              *Call, *this);
42510e25031STed Kremenek }
42610e25031STed Kremenek 
4278b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
4288b808d64SJordan Rose                                           ExplodedNode *Pred,
4298b808d64SJordan Rose                                           ExplodedNodeSet &Dst) {
4308b808d64SJordan Rose   ProgramStateRef State = Pred->getState();
4318b808d64SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
4328b808d64SJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
4338b808d64SJordan Rose                                 CNE->getStartLoc(),
4348b808d64SJordan Rose                                 "Error evaluating New Allocator Call");
4358b808d64SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
4368b808d64SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
4378b808d64SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
4388b808d64SJordan Rose 
4398b808d64SJordan Rose   ExplodedNodeSet DstPreCall;
4408b808d64SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
4418b808d64SJordan Rose                                             *Call, *this);
4428b808d64SJordan Rose 
44355796302SArtem Dergachev   ExplodedNodeSet DstPostCall;
44455796302SArtem Dergachev   StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
4451084de52SArtem Dergachev   for (auto I : DstPreCall) {
4461084de52SArtem Dergachev     // FIXME: Provide evalCall for checkers?
44755796302SArtem Dergachev     defaultEvalCall(CallBldr, I, *Call);
4481084de52SArtem Dergachev   }
449beba5307SArtem Dergachev   // If the call is inlined, DstPostCall will be empty and we bail out now.
45055796302SArtem Dergachev 
45155796302SArtem Dergachev   // Store return value of operator new() for future use, until the actual
45255796302SArtem Dergachev   // CXXNewExpr gets processed.
45355796302SArtem Dergachev   ExplodedNodeSet DstPostValue;
45455796302SArtem Dergachev   StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
45555796302SArtem Dergachev   for (auto I : DstPostCall) {
456beba5307SArtem Dergachev     // FIXME: Because CNE serves as the "call site" for the allocator (due to
457beba5307SArtem Dergachev     // lack of a better expression in the AST), the conjured return value symbol
458beba5307SArtem Dergachev     // is going to be of the same type (C++ object pointer type). Technically
459beba5307SArtem Dergachev     // this is not correct because the operator new's prototype always says that
460beba5307SArtem Dergachev     // it returns a 'void *'. So we should change the type of the symbol,
461beba5307SArtem Dergachev     // and then evaluate the cast over the symbolic pointer from 'void *' to
462beba5307SArtem Dergachev     // the object pointer type. But without changing the symbol's type it
463beba5307SArtem Dergachev     // is breaking too much to evaluate the no-op symbolic cast over it, so we
464beba5307SArtem Dergachev     // skip it for now.
46555796302SArtem Dergachev     ProgramStateRef State = I->getState();
46650e0372fSArtem Dergachev     SVal RetVal = State->getSVal(CNE, LCtx);
46750e0372fSArtem Dergachev 
46850e0372fSArtem Dergachev     // If this allocation function is not declared as non-throwing, failures
46950e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
47050e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
47150e0372fSArtem Dergachev     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
47250e0372fSArtem Dergachev     // where new can return NULL. If we end up supporting that option, we can
47350e0372fSArtem Dergachev     // consider adding a check for it here.
47450e0372fSArtem Dergachev     // C++11 [basic.stc.dynamic.allocation]p3.
47550e0372fSArtem Dergachev     if (const FunctionDecl *FD = CNE->getOperatorNew()) {
47650e0372fSArtem Dergachev       QualType Ty = FD->getType();
47750e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
47850e0372fSArtem Dergachev         if (!ProtoType->isNothrow(getContext()))
47950e0372fSArtem Dergachev           State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
48050e0372fSArtem Dergachev     }
48150e0372fSArtem Dergachev 
48250e0372fSArtem Dergachev     ValueBldr.generateNode(CNE, I,
48350e0372fSArtem Dergachev                            setCXXNewAllocatorValue(State, CNE, LCtx, RetVal));
48455796302SArtem Dergachev   }
48555796302SArtem Dergachev 
48613b2026bSArtem Dergachev   ExplodedNodeSet DstPostPostCallCallback;
48713b2026bSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
48813b2026bSArtem Dergachev                                              DstPostValue, *Call, *this);
48913b2026bSArtem Dergachev   for (auto I : DstPostPostCallCallback) {
49013b2026bSArtem Dergachev     getCheckerManager().runCheckersForNewAllocator(
49113b2026bSArtem Dergachev         CNE, getCXXNewAllocatorValue(I->getState(), CNE, LCtx), Dst, I, *this);
49213b2026bSArtem Dergachev   }
4938b808d64SJordan Rose }
4948b808d64SJordan Rose 
49510e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
49610e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
497a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
498a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
499a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
500a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
50110e25031STed Kremenek 
502d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
503d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
50455796302SArtem Dergachev   SVal symVal = UnknownVal();
5058b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
50610e25031STed Kremenek 
5071084de52SArtem Dergachev   bool IsStandardGlobalOpNewFunction =
5081084de52SArtem Dergachev       FD->isReplaceableGlobalAllocationFunction();
5098b662704SAnton Yartsev 
51055796302SArtem Dergachev   ProgramStateRef State = Pred->getState();
51155796302SArtem Dergachev 
51255796302SArtem Dergachev   // Retrieve the stored operator new() return value.
51355796302SArtem Dergachev   if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
51455796302SArtem Dergachev     symVal = getCXXNewAllocatorValue(State, CNE, LCtx);
51555796302SArtem Dergachev     State = clearCXXNewAllocatorValue(State, CNE, LCtx);
51655796302SArtem Dergachev   }
51755796302SArtem Dergachev 
5188b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
5198b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
5208b662704SAnton Yartsev   // a custom global allocator.
52155796302SArtem Dergachev   if (symVal.isUnknown()) {
5228b662704SAnton Yartsev     if (IsStandardGlobalOpNewFunction)
5238b662704SAnton Yartsev       symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
5248b662704SAnton Yartsev     else
5250dbb783cSCraig Topper       symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
5268b662704SAnton Yartsev                                             blockCount);
52755796302SArtem Dergachev   }
5288b662704SAnton Yartsev 
529fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
530fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
531fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
532fcd016e5SJordan Rose 
53355796302SArtem Dergachev   if (!AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
534a4ee064cSJordan Rose     // Invalidate placement args.
535fcd016e5SJordan Rose     // FIXME: Once we figure out how we want allocators to work,
536fcd016e5SJordan Rose     // we should be using the usual pre-/(default-)eval-/post-call checks here.
537fcd016e5SJordan Rose     State = Call->invalidateRegions(blockCount);
538b8cb8359SJordan Rose     if (!State)
539b8cb8359SJordan Rose       return;
540a4ee064cSJordan Rose 
541fce1b03eSPavel Labath     // If this allocation function is not declared as non-throwing, failures
54250e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
54350e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
544fce1b03eSPavel Labath     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
545fce1b03eSPavel Labath     // where new can return NULL. If we end up supporting that option, we can
546fce1b03eSPavel Labath     // consider adding a check for it here.
5473957fd58SJordan Rose     // C++11 [basic.stc.dynamic.allocation]p3.
548fce1b03eSPavel Labath     if (FD) {
5493957fd58SJordan Rose       QualType Ty = FD->getType();
55050e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
5513957fd58SJordan Rose         if (!ProtoType->isNothrow(getContext()))
55255796302SArtem Dergachev           if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
55355796302SArtem Dergachev             State = State->assume(*dSymVal, true);
5543957fd58SJordan Rose     }
55550e0372fSArtem Dergachev   }
5563957fd58SJordan Rose 
557b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
558b8cb8359SJordan Rose 
559beba5307SArtem Dergachev   SVal Result = symVal;
560beba5307SArtem Dergachev 
56110e25031STed Kremenek   if (CNE->isArray()) {
56210e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
56310e25031STed Kremenek     // For now, just return a symbolicated region.
5641084de52SArtem Dergachev     if (const SubRegion *NewReg =
5651084de52SArtem Dergachev             dyn_cast_or_null<SubRegion>(symVal.getAsRegion())) {
566a4ee064cSJordan Rose       QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
567a4ee064cSJordan Rose       const ElementRegion *EleReg =
568a4ee064cSJordan Rose           getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
569beba5307SArtem Dergachev       Result = loc::MemRegionVal(EleReg);
570beba5307SArtem Dergachev     }
571beba5307SArtem Dergachev     State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
57244320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
57310e25031STed Kremenek     return;
57410e25031STed Kremenek   }
57510e25031STed Kremenek 
576a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
577a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
578a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
579a4ee064cSJordan Rose   // the constructor call anyway.)
58044320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
58144320367SJordan Rose     // Non-array placement new should always return the placement location.
58244320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
583b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
584aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
58544320367SJordan Rose   }
58644320367SJordan Rose 
587b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
588b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
5898f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
5908f6b4b04SJordan Rose   if (!NewN)
5918f6b4b04SJordan Rose     return;
5923503cb35SJordan Rose 
5935089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
5945089f3b3SJordan Rose   // initializer. Copy the value over.
5955089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
5965089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
5973503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
5988f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
599b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
600b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
6013503cb35SJordan Rose     }
6023503cb35SJordan Rose   }
60310e25031STed Kremenek }
60410e25031STed Kremenek 
60510e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
60610e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
607d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
60864aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
60964aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
61010e25031STed Kremenek }
61110e25031STed Kremenek 
6128fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
6138fdb59f9STed Kremenek                                    ExplodedNode *Pred,
6148fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
6158fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
616b1279b5cSTed Kremenek   if (!VD) {
617b1279b5cSTed Kremenek     Dst.Add(Pred);
618b1279b5cSTed Kremenek     return;
619b1279b5cSTed Kremenek   }
620b1279b5cSTed Kremenek 
6218fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
622d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
623d94854a4STed Kremenek                                         currBldrCtx->blockCount());
6248fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
625b570195cSAnna Zaks   state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
6268fdb59f9STed Kremenek 
627d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
6288fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
6298fdb59f9STed Kremenek }
6308fdb59f9STed Kremenek 
63110e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
63210e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
633d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
634e594034fSAnna Zaks 
63510e25031STed Kremenek   // Get the this object region from StoreManager.
636632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
63710e25031STed Kremenek   const MemRegion *R =
63810e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
63910e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
640632e3b7eSTed Kremenek                                                     LCtx);
64110e25031STed Kremenek 
64249b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
64310e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
644632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
64510e25031STed Kremenek }
64615843343SGabor Horvath 
64715843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
64815843343SGabor Horvath                                  ExplodedNodeSet &Dst) {
64915843343SGabor Horvath   const LocationContext *LocCtxt = Pred->getLocationContext();
65015843343SGabor Horvath 
65115843343SGabor Horvath   // Get the region of the lambda itself.
65215843343SGabor Horvath   const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
65315843343SGabor Horvath       LE, LocCtxt);
65415843343SGabor Horvath   SVal V = loc::MemRegionVal(R);
65515843343SGabor Horvath 
65615843343SGabor Horvath   ProgramStateRef State = Pred->getState();
65715843343SGabor Horvath 
65815843343SGabor Horvath   // If we created a new MemRegion for the lambda, we should explicitly bind
65915843343SGabor Horvath   // the captures.
66015843343SGabor Horvath   CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
66115843343SGabor Horvath   for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
66215843343SGabor Horvath                                                e = LE->capture_init_end();
66315843343SGabor Horvath        i != e; ++i, ++CurField) {
6647bdca8b2SDevin Coughlin     FieldDecl *FieldForCapture = *CurField;
6657bdca8b2SDevin Coughlin     SVal FieldLoc = State->getLValue(FieldForCapture, V);
6667bdca8b2SDevin Coughlin 
6677bdca8b2SDevin Coughlin     SVal InitVal;
6687bdca8b2SDevin Coughlin     if (!FieldForCapture->hasCapturedVLAType()) {
6697bdca8b2SDevin Coughlin       Expr *InitExpr = *i;
6707bdca8b2SDevin Coughlin       assert(InitExpr && "Capture missing initialization expression");
6717bdca8b2SDevin Coughlin       InitVal = State->getSVal(InitExpr, LocCtxt);
6727bdca8b2SDevin Coughlin     } else {
6737bdca8b2SDevin Coughlin       // The field stores the length of a captured variable-length array.
6747bdca8b2SDevin Coughlin       // These captures don't have initialization expressions; instead we
6757bdca8b2SDevin Coughlin       // get the length from the VLAType size expression.
6767bdca8b2SDevin Coughlin       Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
6777bdca8b2SDevin Coughlin       InitVal = State->getSVal(SizeExpr, LocCtxt);
6787bdca8b2SDevin Coughlin     }
6797bdca8b2SDevin Coughlin 
680b570195cSAnna Zaks     State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
68115843343SGabor Horvath   }
68215843343SGabor Horvath 
68315843343SGabor Horvath   // Decay the Loc into an RValue, because there might be a
68415843343SGabor Horvath   // MaterializeTemporaryExpr node above this one which expects the bound value
68515843343SGabor Horvath   // to be an RValue.
68615843343SGabor Horvath   SVal LambdaRVal = State->getSVal(R);
68715843343SGabor Horvath 
68815843343SGabor Horvath   ExplodedNodeSet Tmp;
68915843343SGabor Horvath   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
69015843343SGabor Horvath   // FIXME: is this the right program point kind?
69115843343SGabor Horvath   Bldr.generateNode(LE, Pred,
69215843343SGabor Horvath                     State->BindExpr(LE, LocCtxt, LambdaRVal),
69315843343SGabor Horvath                     nullptr, ProgramPoint::PostLValueKind);
69415843343SGabor Horvath 
69515843343SGabor Horvath   // FIXME: Move all post/pre visits to ::Visit().
69615843343SGabor Horvath   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
69715843343SGabor Horvath }
698