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 
87bc74eb1cSJordan Rose /// Returns a region representing the first element of a (possibly
88bc74eb1cSJordan Rose /// multi-dimensional) array.
89bc74eb1cSJordan Rose ///
90bc74eb1cSJordan Rose /// On return, \p Ty will be set to the base type of the array.
91bc74eb1cSJordan Rose ///
92bc74eb1cSJordan Rose /// If the type is not an array type at all, the original value is returned.
93bc74eb1cSJordan Rose static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue,
94bc74eb1cSJordan Rose                                   QualType &Ty) {
95bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
96bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
97bc74eb1cSJordan Rose 
98bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
99bc74eb1cSJordan Rose     Ty = AT->getElementType();
100bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
101bc74eb1cSJordan Rose   }
102bc74eb1cSJordan Rose 
103bc74eb1cSJordan Rose   return LValue;
104bc74eb1cSJordan Rose }
105bc74eb1cSJordan Rose 
10617f41608SJordan Rose 
107412c0af2SDevin Coughlin const MemRegion *
108412c0af2SDevin Coughlin ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
109412c0af2SDevin Coughlin                                           ExplodedNode *Pred) {
110b970505dSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
11105375eb4SJordan Rose   ProgramStateRef State = Pred->getState();
11205375eb4SJordan Rose 
11305375eb4SJordan Rose   // See if we're constructing an existing region by looking at the next
11405375eb4SJordan Rose   // element in the CFG.
1158a5a094eSJordan Rose 
116412c0af2SDevin Coughlin   if (auto Elem = findElementDirectlyInitializedByCurrentConstructor()) {
117412c0af2SDevin Coughlin     if (Optional<CFGStmt> StmtElem = Elem->getAs<CFGStmt>()) {
118*55796302SArtem Dergachev       if (const CXXNewExpr *CNE = dyn_cast<CXXNewExpr>(StmtElem->getStmt())) {
119*55796302SArtem Dergachev         if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
120*55796302SArtem Dergachev           // TODO: Detect when the allocator returns a null pointer.
121*55796302SArtem Dergachev           // Constructor shall not be called in this case.
122*55796302SArtem Dergachev           if (const MemRegion *MR =
123*55796302SArtem Dergachev                   getCXXNewAllocatorValue(State, CNE, LCtx).getAsRegion())
124*55796302SArtem Dergachev             return MR;
125*55796302SArtem Dergachev         }
126*55796302SArtem Dergachev       } else if (auto *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) {
127412c0af2SDevin Coughlin         if (const auto *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) {
128bcd88973SJordan Rose           if (Var->getInit() && Var->getInit()->IgnoreImplicit() == CE) {
129bc74eb1cSJordan Rose             SVal LValue = State->getLValue(Var, LCtx);
13020edae87SJordan Rose             QualType Ty = Var->getType();
131bc74eb1cSJordan Rose             LValue = makeZeroElementRegion(State, LValue, Ty);
13217f41608SJordan Rose             return LValue.getAsRegion();
13320edae87SJordan Rose           }
13420edae87SJordan Rose         }
135*55796302SArtem Dergachev       } else {
136*55796302SArtem Dergachev         llvm_unreachable("Unexpected directly initialized element!");
137*55796302SArtem Dergachev       }
138412c0af2SDevin Coughlin     } else if (Optional<CFGInitializer> InitElem = Elem->getAs<CFGInitializer>()) {
13900be69abSDavid Blaikie       const CXXCtorInitializer *Init = InitElem->getInitializer();
14054529a34SJordan Rose       assert(Init->isAnyMemberInitializer());
14154529a34SJordan Rose       const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
142412c0af2SDevin Coughlin       Loc ThisPtr =
143412c0af2SDevin Coughlin       getSValBuilder().getCXXThis(CurCtor, LCtx->getCurrentStackFrame());
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();
157bc74eb1cSJordan Rose       FieldVal = makeZeroElementRegion(State, FieldVal, Ty);
15817f41608SJordan Rose       return FieldVal.getAsRegion();
15954529a34SJordan Rose     }
16054529a34SJordan Rose 
16105375eb4SJordan Rose     // FIXME: This will eventually need to handle new-expressions as well.
16217f41608SJordan Rose     // Don't forget to update the pre-constructor initialization code in
16317f41608SJordan Rose     // ExprEngine::VisitCXXConstructExpr.
16405375eb4SJordan Rose   }
165c9318304SJordan Rose   // If we couldn't find an existing region to construct into, assume we're
166c9318304SJordan Rose   // constructing a temporary.
167412c0af2SDevin Coughlin   MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
16817f41608SJordan Rose   return MRMgr.getCXXTempObjectRegion(CE, LCtx);
169c9318304SJordan Rose }
17005375eb4SJordan Rose 
171412c0af2SDevin Coughlin /// Returns true if the initializer for \Elem can be a direct
172412c0af2SDevin Coughlin /// constructor.
173412c0af2SDevin Coughlin static bool canHaveDirectConstructor(CFGElement Elem){
174412c0af2SDevin Coughlin   // DeclStmts and CXXCtorInitializers for fields can be directly constructed.
175412c0af2SDevin Coughlin 
176412c0af2SDevin Coughlin   if (Optional<CFGStmt> StmtElem = Elem.getAs<CFGStmt>()) {
177412c0af2SDevin Coughlin     if (isa<DeclStmt>(StmtElem->getStmt())) {
178412c0af2SDevin Coughlin       return true;
179412c0af2SDevin Coughlin     }
180*55796302SArtem Dergachev     if (isa<CXXNewExpr>(StmtElem->getStmt())) {
181*55796302SArtem Dergachev       return true;
182*55796302SArtem Dergachev     }
183412c0af2SDevin Coughlin   }
184412c0af2SDevin Coughlin 
185412c0af2SDevin Coughlin   if (Elem.getKind() == CFGElement::Initializer) {
186412c0af2SDevin Coughlin     return true;
187412c0af2SDevin Coughlin   }
188412c0af2SDevin Coughlin 
189412c0af2SDevin Coughlin   return false;
190412c0af2SDevin Coughlin }
191412c0af2SDevin Coughlin 
192412c0af2SDevin Coughlin Optional<CFGElement>
193412c0af2SDevin Coughlin ExprEngine::findElementDirectlyInitializedByCurrentConstructor() {
194412c0af2SDevin Coughlin   const NodeBuilderContext &CurrBldrCtx = getBuilderContext();
195412c0af2SDevin Coughlin   // See if we're constructing an existing region by looking at the next
196412c0af2SDevin Coughlin   // element in the CFG.
197412c0af2SDevin Coughlin   const CFGBlock *B = CurrBldrCtx.getBlock();
198412c0af2SDevin Coughlin   assert(isa<CXXConstructExpr>(((*B)[currStmtIdx]).castAs<CFGStmt>().getStmt()));
199412c0af2SDevin Coughlin   unsigned int NextStmtIdx = currStmtIdx + 1;
200412c0af2SDevin Coughlin   if (NextStmtIdx >= B->size())
201412c0af2SDevin Coughlin     return None;
202412c0af2SDevin Coughlin 
203412c0af2SDevin Coughlin   CFGElement Next = (*B)[NextStmtIdx];
204412c0af2SDevin Coughlin 
205412c0af2SDevin Coughlin   // Is this a destructor? If so, we might be in the middle of an assignment
206412c0af2SDevin Coughlin   // to a local or member: look ahead one more element to see what we find.
207412c0af2SDevin Coughlin   while (Next.getAs<CFGImplicitDtor>() && NextStmtIdx + 1 < B->size()) {
208412c0af2SDevin Coughlin     ++NextStmtIdx;
209412c0af2SDevin Coughlin     Next = (*B)[NextStmtIdx];
210412c0af2SDevin Coughlin   }
211412c0af2SDevin Coughlin 
212412c0af2SDevin Coughlin   if (canHaveDirectConstructor(Next))
213412c0af2SDevin Coughlin     return Next;
214412c0af2SDevin Coughlin 
215412c0af2SDevin Coughlin   return None;
216412c0af2SDevin Coughlin }
217412c0af2SDevin Coughlin 
218412c0af2SDevin Coughlin const CXXConstructExpr *
219412c0af2SDevin Coughlin ExprEngine::findDirectConstructorForCurrentCFGElement() {
220412c0af2SDevin Coughlin   // Go backward in the CFG to see if the previous element (ignoring
221412c0af2SDevin Coughlin   // destructors) was a CXXConstructExpr. If so, that constructor
222412c0af2SDevin Coughlin   // was constructed directly into an existing region.
223412c0af2SDevin Coughlin   // This process is essentially the inverse of that performed in
224412c0af2SDevin Coughlin   // findElementDirectlyInitializedByCurrentConstructor().
225412c0af2SDevin Coughlin   if (currStmtIdx == 0)
226412c0af2SDevin Coughlin     return nullptr;
227412c0af2SDevin Coughlin 
228412c0af2SDevin Coughlin   const CFGBlock *B = getBuilderContext().getBlock();
229412c0af2SDevin Coughlin   assert(canHaveDirectConstructor((*B)[currStmtIdx]));
230412c0af2SDevin Coughlin 
231412c0af2SDevin Coughlin   unsigned int PreviousStmtIdx = currStmtIdx - 1;
232412c0af2SDevin Coughlin   CFGElement Previous = (*B)[PreviousStmtIdx];
233412c0af2SDevin Coughlin 
234412c0af2SDevin Coughlin   while (Previous.getAs<CFGImplicitDtor>() && PreviousStmtIdx > 0) {
235412c0af2SDevin Coughlin     --PreviousStmtIdx;
236412c0af2SDevin Coughlin     Previous = (*B)[PreviousStmtIdx];
237412c0af2SDevin Coughlin   }
238412c0af2SDevin Coughlin 
239412c0af2SDevin Coughlin   if (Optional<CFGStmt> PrevStmtElem = Previous.getAs<CFGStmt>()) {
240412c0af2SDevin Coughlin     if (auto *CtorExpr = dyn_cast<CXXConstructExpr>(PrevStmtElem->getStmt())) {
241412c0af2SDevin Coughlin       return CtorExpr;
242412c0af2SDevin Coughlin     }
243412c0af2SDevin Coughlin   }
244412c0af2SDevin Coughlin 
245412c0af2SDevin Coughlin   return nullptr;
246412c0af2SDevin Coughlin }
247412c0af2SDevin Coughlin 
24817f41608SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
24917f41608SJordan Rose                                        ExplodedNode *Pred,
25017f41608SJordan Rose                                        ExplodedNodeSet &destNodes) {
25117f41608SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
25217f41608SJordan Rose   ProgramStateRef State = Pred->getState();
25317f41608SJordan Rose 
2540dbb783cSCraig Topper   const MemRegion *Target = nullptr;
25517f41608SJordan Rose 
25617f41608SJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
25717f41608SJordan Rose   // For now, we just run the first constructor (which should still invalidate
25817f41608SJordan Rose   // the entire array).
25917f41608SJordan Rose 
26017f41608SJordan Rose   switch (CE->getConstructionKind()) {
26117f41608SJordan Rose   case CXXConstructExpr::CK_Complete: {
262412c0af2SDevin Coughlin     Target = getRegionForConstructedObject(CE, Pred);
263b970505dSJordan Rose     break;
264b970505dSJordan Rose   }
265b3b976f0SJordan Rose   case CXXConstructExpr::CK_VirtualBase:
266b3b976f0SJordan Rose     // Make sure we are not calling virtual base class initializers twice.
267b3b976f0SJordan Rose     // Only the most-derived object should initialize virtual base classes.
268b3b976f0SJordan Rose     if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) {
269b3b976f0SJordan Rose       const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer);
270b3b976f0SJordan Rose       if (OuterCtor) {
271b3b976f0SJordan Rose         switch (OuterCtor->getConstructionKind()) {
272b970505dSJordan Rose         case CXXConstructExpr::CK_NonVirtualBase:
273b970505dSJordan Rose         case CXXConstructExpr::CK_VirtualBase:
274b3b976f0SJordan Rose           // Bail out!
275b3b976f0SJordan Rose           destNodes.Add(Pred);
276b3b976f0SJordan Rose           return;
277b3b976f0SJordan Rose         case CXXConstructExpr::CK_Complete:
278b3b976f0SJordan Rose         case CXXConstructExpr::CK_Delegating:
279b3b976f0SJordan Rose           break;
280b3b976f0SJordan Rose         }
281b3b976f0SJordan Rose       }
282b3b976f0SJordan Rose     }
283b3b976f0SJordan Rose     // FALLTHROUGH
284b3b976f0SJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
285e8ba3ec4SArtem Dergachev     // In C++17, classes with non-virtual bases may be aggregates, so they would
286e8ba3ec4SArtem Dergachev     // be initialized as aggregates without a constructor call, so we may have
287e8ba3ec4SArtem Dergachev     // a base class constructed directly into an initializer list without
288e8ba3ec4SArtem Dergachev     // having the derived-class constructor call on the previous stack frame.
289e8ba3ec4SArtem Dergachev     // Initializer lists may be nested into more initializer lists that
290e8ba3ec4SArtem Dergachev     // correspond to surrounding aggregate initializations.
291e8ba3ec4SArtem Dergachev     // FIXME: For now this code essentially bails out. We need to find the
292e8ba3ec4SArtem Dergachev     // correct target region and set it.
293e8ba3ec4SArtem Dergachev     // FIXME: Instead of relying on the ParentMap, we should have the
294e8ba3ec4SArtem Dergachev     // trigger-statement (InitListExpr in this case) passed down from CFG or
295e8ba3ec4SArtem Dergachev     // otherwise always available during construction.
296e8ba3ec4SArtem Dergachev     if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(CE))) {
297e8ba3ec4SArtem Dergachev       MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
298e8ba3ec4SArtem Dergachev       Target = MRMgr.getCXXTempObjectRegion(CE, LCtx);
299e8ba3ec4SArtem Dergachev       break;
300e8ba3ec4SArtem Dergachev     }
301e8ba3ec4SArtem Dergachev     // FALLTHROUGH
302b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
303b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
304b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
305b970505dSJordan Rose                                               LCtx->getCurrentStackFrame());
30605375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
307b970505dSJordan Rose 
308b970505dSJordan Rose     if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
309b970505dSJordan Rose       Target = ThisVal.getAsRegion();
310b970505dSJordan Rose     } else {
311b970505dSJordan Rose       // Cast to the base type.
312599f85abSJordan Rose       bool IsVirtual =
313599f85abSJordan Rose         (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
314599f85abSJordan Rose       SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
315599f85abSJordan Rose                                                          IsVirtual);
31620edae87SJordan Rose       Target = BaseVal.getAsRegion();
317b970505dSJordan Rose     }
318b970505dSJordan Rose     break;
319b970505dSJordan Rose   }
320b970505dSJordan Rose   }
321b970505dSJordan Rose 
322fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
323fcd016e5SJordan Rose   CallEventRef<CXXConstructorCall> Call =
324fcd016e5SJordan Rose     CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
32510e25031STed Kremenek 
326f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
327f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
328b8e28654SJordan Rose 
329b8e28654SJordan Rose   ExplodedNodeSet PreInitialized;
330b8e28654SJordan Rose   {
331b8e28654SJordan Rose     StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
332b8e28654SJordan Rose     if (CE->requiresZeroInitialization()) {
333b8e28654SJordan Rose       // Type of the zero doesn't matter.
334b8e28654SJordan Rose       SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy);
335b8e28654SJordan Rose 
336b8e28654SJordan Rose       for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
337b8e28654SJordan Rose                                      E = DstPreVisit.end();
338b8e28654SJordan Rose            I != E; ++I) {
339b8e28654SJordan Rose         ProgramStateRef State = (*I)->getState();
340b8e28654SJordan Rose         // FIXME: Once we properly handle constructors in new-expressions, we'll
341b8e28654SJordan Rose         // need to invalidate the region before setting a default value, to make
342b8e28654SJordan Rose         // sure there aren't any lingering bindings around. This probably needs
343b8e28654SJordan Rose         // to happen regardless of whether or not the object is zero-initialized
344b8e28654SJordan Rose         // to handle random fields of a placement-initialized object picking up
345b8e28654SJordan Rose         // old bindings. We might only want to do it when we need to, though.
346b8e28654SJordan Rose         // FIXME: This isn't actually correct for arrays -- we need to zero-
347b8e28654SJordan Rose         // initialize the entire array, not just the first element -- but our
348b8e28654SJordan Rose         // handling of arrays everywhere else is weak as well, so this shouldn't
349b8e28654SJordan Rose         // actually make things worse. Placement new makes this tricky as well,
350b8e28654SJordan Rose         // since it's then possible to be initializing one part of a multi-
351b8e28654SJordan Rose         // dimensional array.
352b570195cSAnna Zaks         State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal, LCtx);
3530dbb783cSCraig Topper         Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
3540dbb783cSCraig Topper                           ProgramPoint::PreStmtKind);
355b8e28654SJordan Rose       }
356b8e28654SJordan Rose     }
357b8e28654SJordan Rose   }
358b8e28654SJordan Rose 
359afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
360b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
361fcd016e5SJordan Rose                                             *Call, *this);
36210e25031STed Kremenek 
36388bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
36488bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
36588bb563cSJordan Rose 
366e83cb092SJordan Rose   bool IsArray = isa<ElementRegion>(Target);
367e83cb092SJordan Rose   if (CE->getConstructor()->isTrivial() &&
368e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
369e83cb092SJordan Rose       !IsArray) {
370e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
371e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
37288bb563cSJordan Rose          I != E; ++I)
37388bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
37488bb563cSJordan Rose 
37588bb563cSJordan Rose   } else {
376afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
377f3c12ac3SJordan Rose          I != E; ++I)
378fcd016e5SJordan Rose       defaultEvalCall(Bldr, *I, *Call);
37988bb563cSJordan Rose   }
38010e25031STed Kremenek 
3815b1ee2faSDevin Coughlin   // If the CFG was contructed without elements for temporary destructors
3825b1ee2faSDevin Coughlin   // and the just-called constructor created a temporary object then
3835b1ee2faSDevin Coughlin   // stop exploration if the temporary object has a noreturn constructor.
3845b1ee2faSDevin Coughlin   // This can lose coverage because the destructor, if it were present
3855b1ee2faSDevin Coughlin   // in the CFG, would be called at the end of the full expression or
3865b1ee2faSDevin Coughlin   // later (for life-time extended temporaries) -- but avoids infeasible
3875b1ee2faSDevin Coughlin   // paths when no-return temporary destructors are used for assertions.
3885b1ee2faSDevin Coughlin   const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
3895b1ee2faSDevin Coughlin   if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
3905b1ee2faSDevin Coughlin       const MemRegion *Target = Call->getCXXThisVal().getAsRegion();
3915b1ee2faSDevin Coughlin       if (Target && isa<CXXTempObjectRegion>(Target) &&
3925b1ee2faSDevin Coughlin           Call->getDecl()->getParent()->isAnyDestructorNoReturn()) {
3935b1ee2faSDevin Coughlin 
3945b1ee2faSDevin Coughlin       for (ExplodedNode *N : DstEvaluated) {
3955b1ee2faSDevin Coughlin         Bldr.generateSink(CE, N, N->getState());
3965b1ee2faSDevin Coughlin       }
3975b1ee2faSDevin Coughlin 
3985b1ee2faSDevin Coughlin       // There is no need to run the PostCall and PostStmtchecker
3995b1ee2faSDevin Coughlin       // callbacks because we just generated sinks on all nodes in th
4005b1ee2faSDevin Coughlin       // frontier.
4015b1ee2faSDevin Coughlin       return;
4025b1ee2faSDevin Coughlin     }
4035b1ee2faSDevin Coughlin  }
4045b1ee2faSDevin Coughlin 
405afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
40688bb563cSJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
407fcd016e5SJordan Rose                                              *Call, *this);
408afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
40910e25031STed Kremenek }
41010e25031STed Kremenek 
411b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
41210e25031STed Kremenek                                     const MemRegion *Dest,
41310e25031STed Kremenek                                     const Stmt *S,
4142bc9674bSJordan Rose                                     bool IsBaseDtor,
41510e25031STed Kremenek                                     ExplodedNode *Pred,
41610e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
417fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
418fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
419fcd016e5SJordan Rose 
42020edae87SJordan Rose   // FIXME: We need to run the same destructor on every element of the array.
42120edae87SJordan Rose   // This workaround will just run the first destructor (which will still
42220edae87SJordan Rose   // invalidate the entire array).
4231ccc43d5SJordan Rose   SVal DestVal = UnknownVal();
4241ccc43d5SJordan Rose   if (Dest)
4251ccc43d5SJordan Rose     DestVal = loc::MemRegionVal(Dest);
426bc74eb1cSJordan Rose   DestVal = makeZeroElementRegion(State, DestVal, ObjectType);
427bc74eb1cSJordan Rose   Dest = DestVal.getAsRegion();
42820edae87SJordan Rose 
429b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
430b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
431b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
432b970505dSJordan Rose 
433fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
434fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
4352bc9674bSJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
436142adc49STed Kremenek 
43717a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
43817a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
43917a8757aSJordan Rose                                 "Error evaluating destructor");
44017a8757aSJordan Rose 
4414ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
4424ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
443fcd016e5SJordan Rose                                             *Call, *this);
44410e25031STed Kremenek 
4454ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
446d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
4474ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
4484ee71b8aSJordan Rose        I != E; ++I)
449fcd016e5SJordan Rose     defaultEvalCall(Bldr, *I, *Call);
4504ee71b8aSJordan Rose 
4514ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
4524ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
453fcd016e5SJordan Rose                                              *Call, *this);
45410e25031STed Kremenek }
45510e25031STed Kremenek 
4568b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
4578b808d64SJordan Rose                                           ExplodedNode *Pred,
4588b808d64SJordan Rose                                           ExplodedNodeSet &Dst) {
4598b808d64SJordan Rose   ProgramStateRef State = Pred->getState();
4608b808d64SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
4618b808d64SJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
4628b808d64SJordan Rose                                 CNE->getStartLoc(),
4638b808d64SJordan Rose                                 "Error evaluating New Allocator Call");
4648b808d64SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
4658b808d64SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
4668b808d64SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
4678b808d64SJordan Rose 
4688b808d64SJordan Rose   ExplodedNodeSet DstPreCall;
4698b808d64SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
4708b808d64SJordan Rose                                             *Call, *this);
4718b808d64SJordan Rose 
472*55796302SArtem Dergachev   ExplodedNodeSet DstPostCall;
473*55796302SArtem Dergachev   StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
474*55796302SArtem Dergachev   for (auto I : DstPreCall)
475*55796302SArtem Dergachev     defaultEvalCall(CallBldr, I, *Call);
476*55796302SArtem Dergachev 
477*55796302SArtem Dergachev   // Store return value of operator new() for future use, until the actual
478*55796302SArtem Dergachev   // CXXNewExpr gets processed.
479*55796302SArtem Dergachev   ExplodedNodeSet DstPostValue;
480*55796302SArtem Dergachev   StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
481*55796302SArtem Dergachev   for (auto I : DstPostCall) {
482*55796302SArtem Dergachev     ProgramStateRef State = I->getState();
483*55796302SArtem Dergachev     ValueBldr.generateNode(
484*55796302SArtem Dergachev         CNE, I,
485*55796302SArtem Dergachev         setCXXNewAllocatorValue(State, CNE, LCtx, State->getSVal(CNE, LCtx)));
486*55796302SArtem Dergachev   }
487*55796302SArtem Dergachev 
488*55796302SArtem Dergachev   getCheckerManager().runCheckersForPostCall(Dst, DstPostValue,
4898b808d64SJordan Rose                                              *Call, *this);
4908b808d64SJordan Rose }
4918b808d64SJordan Rose 
4928b808d64SJordan Rose 
49310e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
49410e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
495a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
496a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
497a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
498a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
49910e25031STed Kremenek 
500d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
501d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
502*55796302SArtem Dergachev   SVal symVal = UnknownVal();
5038b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
50410e25031STed Kremenek 
5058b662704SAnton Yartsev   bool IsStandardGlobalOpNewFunction = false;
5068b662704SAnton Yartsev   if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) {
5078b662704SAnton Yartsev     if (FD->getNumParams() == 2) {
5088b662704SAnton Yartsev       QualType T = FD->getParamDecl(1)->getType();
5098b662704SAnton Yartsev       if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
5108b662704SAnton Yartsev         // NoThrow placement new behaves as a standard new.
5118b662704SAnton Yartsev         IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t");
5128b662704SAnton Yartsev     }
5138b662704SAnton Yartsev     else
5148b662704SAnton Yartsev       // Placement forms are considered non-standard.
5158b662704SAnton Yartsev       IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1);
5168b662704SAnton Yartsev   }
5178b662704SAnton Yartsev 
518*55796302SArtem Dergachev   ProgramStateRef State = Pred->getState();
519*55796302SArtem Dergachev 
520*55796302SArtem Dergachev   // Retrieve the stored operator new() return value.
521*55796302SArtem Dergachev   if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
522*55796302SArtem Dergachev     symVal = getCXXNewAllocatorValue(State, CNE, LCtx);
523*55796302SArtem Dergachev     State = clearCXXNewAllocatorValue(State, CNE, LCtx);
524*55796302SArtem Dergachev   }
525*55796302SArtem Dergachev 
5268b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
5278b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
5288b662704SAnton Yartsev   // a custom global allocator.
529*55796302SArtem Dergachev   if (symVal.isUnknown()) {
5308b662704SAnton Yartsev     if (IsStandardGlobalOpNewFunction)
5318b662704SAnton Yartsev       symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
5328b662704SAnton Yartsev     else
5330dbb783cSCraig Topper       symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
5348b662704SAnton Yartsev                                             blockCount);
535*55796302SArtem Dergachev   }
5368b662704SAnton Yartsev 
537fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
538fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
539fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
540fcd016e5SJordan Rose 
541*55796302SArtem Dergachev   if (!AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
542a4ee064cSJordan Rose     // Invalidate placement args.
543fcd016e5SJordan Rose     // FIXME: Once we figure out how we want allocators to work,
544fcd016e5SJordan Rose     // we should be using the usual pre-/(default-)eval-/post-call checks here.
545fcd016e5SJordan Rose     State = Call->invalidateRegions(blockCount);
546b8cb8359SJordan Rose     if (!State)
547b8cb8359SJordan Rose       return;
548*55796302SArtem Dergachev   }
549a4ee064cSJordan Rose 
550fce1b03eSPavel Labath   // If this allocation function is not declared as non-throwing, failures
551fce1b03eSPavel Labath   // /must/ be signalled by exceptions, and thus the return value will never be
552fce1b03eSPavel Labath   // NULL. -fno-exceptions does not influence this semantics.
553fce1b03eSPavel Labath   // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
554fce1b03eSPavel Labath   // where new can return NULL. If we end up supporting that option, we can
555fce1b03eSPavel Labath   // consider adding a check for it here.
5563957fd58SJordan Rose   // C++11 [basic.stc.dynamic.allocation]p3.
557fce1b03eSPavel Labath   if (FD) {
5583957fd58SJordan Rose     QualType Ty = FD->getType();
5593957fd58SJordan Rose     if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>())
5603957fd58SJordan Rose       if (!ProtoType->isNothrow(getContext()))
561*55796302SArtem Dergachev         if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
562*55796302SArtem Dergachev           State = State->assume(*dSymVal, true);
5633957fd58SJordan Rose   }
5643957fd58SJordan Rose 
565b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
566b8cb8359SJordan Rose 
56710e25031STed Kremenek   if (CNE->isArray()) {
56810e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
56910e25031STed Kremenek     // For now, just return a symbolicated region.
5706dd11048SArtem Dergachev     const SubRegion *NewReg =
5716dd11048SArtem Dergachev         symVal.castAs<loc::MemRegionVal>().getRegionAs<SubRegion>();
572a4ee064cSJordan Rose     QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
573a4ee064cSJordan Rose     const ElementRegion *EleReg =
574a4ee064cSJordan Rose       getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
57544320367SJordan Rose     State = State->BindExpr(CNE, Pred->getLocationContext(),
576632e3b7eSTed Kremenek                             loc::MemRegionVal(EleReg));
57744320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
57810e25031STed Kremenek     return;
57910e25031STed Kremenek   }
58010e25031STed Kremenek 
581a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
582a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
583a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
584a4ee064cSJordan Rose   // the constructor call anyway.)
585b8cb8359SJordan Rose   SVal Result = symVal;
58644320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
58744320367SJordan Rose     // Non-array placement new should always return the placement location.
58844320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
589b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
590aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
59144320367SJordan Rose   }
59244320367SJordan Rose 
593b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
594b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
5958f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
5968f6b4b04SJordan Rose   if (!NewN)
5978f6b4b04SJordan Rose     return;
5983503cb35SJordan Rose 
5995089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
6005089f3b3SJordan Rose   // initializer. Copy the value over.
6015089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
6025089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
6033503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
6048f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
605b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
606b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
6073503cb35SJordan Rose     }
6083503cb35SJordan Rose   }
60910e25031STed Kremenek }
61010e25031STed Kremenek 
61110e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
61210e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
613d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
61464aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
61564aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
61610e25031STed Kremenek }
61710e25031STed Kremenek 
6188fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
6198fdb59f9STed Kremenek                                    ExplodedNode *Pred,
6208fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
6218fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
622b1279b5cSTed Kremenek   if (!VD) {
623b1279b5cSTed Kremenek     Dst.Add(Pred);
624b1279b5cSTed Kremenek     return;
625b1279b5cSTed Kremenek   }
626b1279b5cSTed Kremenek 
6278fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
628d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
629d94854a4STed Kremenek                                         currBldrCtx->blockCount());
6308fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
631b570195cSAnna Zaks   state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
6328fdb59f9STed Kremenek 
633d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
6348fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
6358fdb59f9STed Kremenek }
6368fdb59f9STed Kremenek 
63710e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
63810e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
639d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
640e594034fSAnna Zaks 
64110e25031STed Kremenek   // Get the this object region from StoreManager.
642632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
64310e25031STed Kremenek   const MemRegion *R =
64410e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
64510e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
646632e3b7eSTed Kremenek                                                     LCtx);
64710e25031STed Kremenek 
64849b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
64910e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
650632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
65110e25031STed Kremenek }
65215843343SGabor Horvath 
65315843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
65415843343SGabor Horvath                                  ExplodedNodeSet &Dst) {
65515843343SGabor Horvath   const LocationContext *LocCtxt = Pred->getLocationContext();
65615843343SGabor Horvath 
65715843343SGabor Horvath   // Get the region of the lambda itself.
65815843343SGabor Horvath   const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
65915843343SGabor Horvath       LE, LocCtxt);
66015843343SGabor Horvath   SVal V = loc::MemRegionVal(R);
66115843343SGabor Horvath 
66215843343SGabor Horvath   ProgramStateRef State = Pred->getState();
66315843343SGabor Horvath 
66415843343SGabor Horvath   // If we created a new MemRegion for the lambda, we should explicitly bind
66515843343SGabor Horvath   // the captures.
66615843343SGabor Horvath   CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
66715843343SGabor Horvath   for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
66815843343SGabor Horvath                                                e = LE->capture_init_end();
66915843343SGabor Horvath        i != e; ++i, ++CurField) {
6707bdca8b2SDevin Coughlin     FieldDecl *FieldForCapture = *CurField;
6717bdca8b2SDevin Coughlin     SVal FieldLoc = State->getLValue(FieldForCapture, V);
6727bdca8b2SDevin Coughlin 
6737bdca8b2SDevin Coughlin     SVal InitVal;
6747bdca8b2SDevin Coughlin     if (!FieldForCapture->hasCapturedVLAType()) {
6757bdca8b2SDevin Coughlin       Expr *InitExpr = *i;
6767bdca8b2SDevin Coughlin       assert(InitExpr && "Capture missing initialization expression");
6777bdca8b2SDevin Coughlin       InitVal = State->getSVal(InitExpr, LocCtxt);
6787bdca8b2SDevin Coughlin     } else {
6797bdca8b2SDevin Coughlin       // The field stores the length of a captured variable-length array.
6807bdca8b2SDevin Coughlin       // These captures don't have initialization expressions; instead we
6817bdca8b2SDevin Coughlin       // get the length from the VLAType size expression.
6827bdca8b2SDevin Coughlin       Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
6837bdca8b2SDevin Coughlin       InitVal = State->getSVal(SizeExpr, LocCtxt);
6847bdca8b2SDevin Coughlin     }
6857bdca8b2SDevin Coughlin 
686b570195cSAnna Zaks     State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
68715843343SGabor Horvath   }
68815843343SGabor Horvath 
68915843343SGabor Horvath   // Decay the Loc into an RValue, because there might be a
69015843343SGabor Horvath   // MaterializeTemporaryExpr node above this one which expects the bound value
69115843343SGabor Horvath   // to be an RValue.
69215843343SGabor Horvath   SVal LambdaRVal = State->getSVal(R);
69315843343SGabor Horvath 
69415843343SGabor Horvath   ExplodedNodeSet Tmp;
69515843343SGabor Horvath   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
69615843343SGabor Horvath   // FIXME: is this the right program point kind?
69715843343SGabor Horvath   Bldr.generateNode(LE, Pred,
69815843343SGabor Horvath                     State->BindExpr(LE, LocCtxt, LambdaRVal),
69915843343SGabor Horvath                     nullptr, ProgramPoint::PostLValueKind);
70015843343SGabor Horvath 
70115843343SGabor Horvath   // FIXME: Move all post/pre visits to ::Visit().
70215843343SGabor Horvath   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
70315843343SGabor Horvath }
704