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"
15*4068481bSArtem Dergachev #include "clang/Analysis/ConstructionContext.h"
1610e25031STed Kremenek #include "clang/AST/DeclCXX.h"
178fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h"
18e8ba3ec4SArtem Dergachev #include "clang/AST/ParentMap.h"
1917a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h"
203a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h"
213a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
223a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
2310e25031STed Kremenek 
2410e25031STed Kremenek using namespace clang;
2510e25031STed Kremenek using namespace ento;
2610e25031STed Kremenek 
2710e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
2810e25031STed Kremenek                                           ExplodedNode *Pred,
2910e25031STed Kremenek                                           ExplodedNodeSet &Dst) {
30d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
312a14c695STed Kremenek   const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
3249b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
33632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
3410e25031STed Kremenek 
35861a1740SJordan Rose   state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME);
365772f82dSJordan Rose   Bldr.generateNode(ME, Pred, state);
3710e25031STed Kremenek }
3810e25031STed Kremenek 
393d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core
403d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine.
4188bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
423d7b7f52SJordan Rose                                     const CallEvent &Call) {
433d7b7f52SJordan Rose   SVal ThisVal;
443d7b7f52SJordan Rose   bool AlwaysReturnsLValue;
453d7b7f52SJordan Rose   if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
463d7b7f52SJordan Rose     assert(Ctor->getDecl()->isTrivial());
473d7b7f52SJordan Rose     assert(Ctor->getDecl()->isCopyOrMoveConstructor());
483d7b7f52SJordan Rose     ThisVal = Ctor->getCXXThisVal();
493d7b7f52SJordan Rose     AlwaysReturnsLValue = false;
503d7b7f52SJordan Rose   } else {
513d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
523d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
533d7b7f52SJordan Rose            OO_Equal);
543d7b7f52SJordan Rose     ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
553d7b7f52SJordan Rose     AlwaysReturnsLValue = true;
563d7b7f52SJordan Rose   }
5788bb563cSJordan Rose 
5888bb563cSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
5988bb563cSJordan Rose 
6088bb563cSJordan Rose   ExplodedNodeSet Dst;
6188bb563cSJordan Rose   Bldr.takeNodes(Pred);
6288bb563cSJordan Rose 
6388bb563cSJordan Rose   SVal V = Call.getArgSVal(0);
6488bb563cSJordan Rose 
653d7b7f52SJordan Rose   // If the value being copied is not unknown, load from its location to get
663d7b7f52SJordan Rose   // an aggregate rvalue.
6705785d16SDavid Blaikie   if (Optional<Loc> L = V.getAs<Loc>())
6888bb563cSJordan Rose     V = Pred->getState()->getSVal(*L);
693d7b7f52SJordan Rose   else
7075f9d3acSArtem Dergachev     assert(V.isUnknownOrUndef());
7188bb563cSJordan Rose 
723d7b7f52SJordan Rose   const Expr *CallExpr = Call.getOriginExpr();
733d7b7f52SJordan Rose   evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
7488bb563cSJordan Rose 
753d7b7f52SJordan Rose   PostStmt PS(CallExpr, LCtx);
7688bb563cSJordan Rose   for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
7788bb563cSJordan Rose        I != E; ++I) {
7888bb563cSJordan Rose     ProgramStateRef State = (*I)->getState();
793d7b7f52SJordan Rose     if (AlwaysReturnsLValue)
803d7b7f52SJordan Rose       State = State->BindExpr(CallExpr, LCtx, ThisVal);
813d7b7f52SJordan Rose     else
8288bb563cSJordan Rose       State = bindReturnValue(Call, LCtx, State);
8388bb563cSJordan Rose     Bldr.generateNode(PS, State, *I);
8488bb563cSJordan Rose   }
8588bb563cSJordan Rose }
8688bb563cSJordan Rose 
87bc74eb1cSJordan Rose 
88168e29f6SArtem Dergachev SVal ExprEngine::makeZeroElementRegion(ProgramStateRef State, SVal LValue,
89690ab040SArtem Dergachev                                        QualType &Ty, bool &IsArray) {
90bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
91bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
92bc74eb1cSJordan Rose 
93bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
94bc74eb1cSJordan Rose     Ty = AT->getElementType();
95bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
96690ab040SArtem Dergachev     IsArray = true;
97bc74eb1cSJordan Rose   }
98bc74eb1cSJordan Rose 
99bc74eb1cSJordan Rose   return LValue;
100bc74eb1cSJordan Rose }
101bc74eb1cSJordan Rose 
10217f41608SJordan Rose 
103412c0af2SDevin Coughlin const MemRegion *
104412c0af2SDevin Coughlin ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
105690ab040SArtem Dergachev                                           ExplodedNode *Pred,
106661ab34aSArtem Dergachev                                           const ConstructionContext *CC,
107690ab040SArtem Dergachev                                           EvalCallOptions &CallOpts) {
108b970505dSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
10905375eb4SJordan Rose   ProgramStateRef State = Pred->getState();
11094020268SArtem Dergachev   MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
11105375eb4SJordan Rose 
112afb158c2SArtem Dergachev   // See if we're constructing an existing region by looking at the
113afb158c2SArtem Dergachev   // current construction context.
114661ab34aSArtem Dergachev   if (CC) {
115*4068481bSArtem Dergachev     switch (CC->getKind()) {
116*4068481bSArtem Dergachev     case ConstructionContext::SimpleVariableKind: {
117*4068481bSArtem Dergachev       const auto *DSCC = cast<SimpleVariableConstructionContext>(CC);
118*4068481bSArtem Dergachev       const auto *DS = DSCC->getDeclStmt();
119e231bd34SArtem Dergachev       const auto *Var = cast<VarDecl>(DS->getSingleDecl());
120bc74eb1cSJordan Rose       SVal LValue = State->getLValue(Var, LCtx);
12120edae87SJordan Rose       QualType Ty = Var->getType();
122*4068481bSArtem Dergachev       LValue =
123*4068481bSArtem Dergachev           makeZeroElementRegion(State, LValue, Ty, CallOpts.IsArrayCtorOrDtor);
12417f41608SJordan Rose       return LValue.getAsRegion();
12520edae87SJordan Rose     }
126*4068481bSArtem Dergachev     case ConstructionContext::ConstructorInitializerKind: {
127*4068481bSArtem Dergachev       const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC);
128*4068481bSArtem Dergachev       const auto *Init = ICC->getCXXCtorInitializer();
12954529a34SJordan Rose       assert(Init->isAnyMemberInitializer());
13054529a34SJordan Rose       const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
131412c0af2SDevin Coughlin       Loc ThisPtr =
132412c0af2SDevin Coughlin       getSValBuilder().getCXXThis(CurCtor, LCtx->getCurrentStackFrame());
13354529a34SJordan Rose       SVal ThisVal = State->getSVal(ThisPtr);
13454529a34SJordan Rose 
135bc74eb1cSJordan Rose       const ValueDecl *Field;
136bc74eb1cSJordan Rose       SVal FieldVal;
13754529a34SJordan Rose       if (Init->isIndirectMemberInitializer()) {
138bc74eb1cSJordan Rose         Field = Init->getIndirectMember();
139bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
14054529a34SJordan Rose       } else {
141bc74eb1cSJordan Rose         Field = Init->getMember();
142bc74eb1cSJordan Rose         FieldVal = State->getLValue(Init->getMember(), ThisVal);
14354529a34SJordan Rose       }
144bc74eb1cSJordan Rose 
145bc74eb1cSJordan Rose       QualType Ty = Field->getType();
146690ab040SArtem Dergachev       FieldVal = makeZeroElementRegion(State, FieldVal, Ty,
147168e29f6SArtem Dergachev                                        CallOpts.IsArrayCtorOrDtor);
14817f41608SJordan Rose       return FieldVal.getAsRegion();
14954529a34SJordan Rose     }
150*4068481bSArtem Dergachev     case ConstructionContext::NewAllocatedObjectKind: {
151*4068481bSArtem Dergachev       if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
152*4068481bSArtem Dergachev         const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC);
153*4068481bSArtem Dergachev         const auto *NE = NECC->getCXXNewExpr();
154*4068481bSArtem Dergachev         // TODO: Detect when the allocator returns a null pointer.
155*4068481bSArtem Dergachev         // Constructor shall not be called in this case.
156*4068481bSArtem Dergachev         if (const SubRegion *MR = dyn_cast_or_null<SubRegion>(
157*4068481bSArtem Dergachev                 getCXXNewAllocatorValue(State, NE, LCtx).getAsRegion())) {
158*4068481bSArtem Dergachev           if (NE->isArray()) {
159*4068481bSArtem Dergachev             // TODO: In fact, we need to call the constructor for every
160*4068481bSArtem Dergachev             // allocated element, not just the first one!
161*4068481bSArtem Dergachev             CallOpts.IsArrayCtorOrDtor = true;
162*4068481bSArtem Dergachev             return getStoreManager().GetElementZeroRegion(
163*4068481bSArtem Dergachev                 MR, NE->getType()->getPointeeType());
164*4068481bSArtem Dergachev           }
165*4068481bSArtem Dergachev           return MR;
166*4068481bSArtem Dergachev         }
167*4068481bSArtem Dergachev       }
168*4068481bSArtem Dergachev       break;
169*4068481bSArtem Dergachev     }
170*4068481bSArtem Dergachev     case ConstructionContext::TemporaryObjectKind: {
171*4068481bSArtem Dergachev       // TODO: Support temporaries lifetime-extended via static references.
172*4068481bSArtem Dergachev       // They'd need a getCXXStaticTempObjectRegion().
173*4068481bSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
174*4068481bSArtem Dergachev       return MRMgr.getCXXTempObjectRegion(CE, LCtx);
175*4068481bSArtem Dergachev     }
176*4068481bSArtem Dergachev     case ConstructionContext::ReturnedValueKind: {
177*4068481bSArtem Dergachev       // TODO: We should construct into a CXXBindTemporaryExpr or a
178*4068481bSArtem Dergachev       // MaterializeTemporaryExpr around the call-expression on the previous
179*4068481bSArtem Dergachev       // stack frame. Currently we re-bind the temporary to the correct region
180*4068481bSArtem Dergachev       // later, but that's not semantically correct. This of course does not
181*4068481bSArtem Dergachev       // apply when we're in the top frame. But if we are in an inlined
182*4068481bSArtem Dergachev       // function, we should be able to take the call-site CFG element,
183*4068481bSArtem Dergachev       // and it should contain (but right now it wouldn't) some sort of
184*4068481bSArtem Dergachev       // construction context that'd give us the right temporary expression.
185*4068481bSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
186*4068481bSArtem Dergachev       return MRMgr.getCXXTempObjectRegion(CE, LCtx);
187*4068481bSArtem Dergachev     }
188*4068481bSArtem Dergachev     }
18905375eb4SJordan Rose   }
190c9318304SJordan Rose   // If we couldn't find an existing region to construct into, assume we're
191690ab040SArtem Dergachev   // constructing a temporary. Notify the caller of our failure.
192168e29f6SArtem Dergachev   CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
19317f41608SJordan Rose   return MRMgr.getCXXTempObjectRegion(CE, LCtx);
194c9318304SJordan Rose }
19505375eb4SJordan Rose 
196412c0af2SDevin Coughlin const CXXConstructExpr *
197412c0af2SDevin Coughlin ExprEngine::findDirectConstructorForCurrentCFGElement() {
198412c0af2SDevin Coughlin   // Go backward in the CFG to see if the previous element (ignoring
199412c0af2SDevin Coughlin   // destructors) was a CXXConstructExpr. If so, that constructor
200412c0af2SDevin Coughlin   // was constructed directly into an existing region.
201412c0af2SDevin Coughlin   // This process is essentially the inverse of that performed in
202412c0af2SDevin Coughlin   // findElementDirectlyInitializedByCurrentConstructor().
203412c0af2SDevin Coughlin   if (currStmtIdx == 0)
204412c0af2SDevin Coughlin     return nullptr;
205412c0af2SDevin Coughlin 
206412c0af2SDevin Coughlin   const CFGBlock *B = getBuilderContext().getBlock();
207412c0af2SDevin Coughlin 
208412c0af2SDevin Coughlin   unsigned int PreviousStmtIdx = currStmtIdx - 1;
209412c0af2SDevin Coughlin   CFGElement Previous = (*B)[PreviousStmtIdx];
210412c0af2SDevin Coughlin 
211412c0af2SDevin Coughlin   while (Previous.getAs<CFGImplicitDtor>() && PreviousStmtIdx > 0) {
212412c0af2SDevin Coughlin     --PreviousStmtIdx;
213412c0af2SDevin Coughlin     Previous = (*B)[PreviousStmtIdx];
214412c0af2SDevin Coughlin   }
215412c0af2SDevin Coughlin 
216412c0af2SDevin Coughlin   if (Optional<CFGStmt> PrevStmtElem = Previous.getAs<CFGStmt>()) {
217412c0af2SDevin Coughlin     if (auto *CtorExpr = dyn_cast<CXXConstructExpr>(PrevStmtElem->getStmt())) {
218412c0af2SDevin Coughlin       return CtorExpr;
219412c0af2SDevin Coughlin     }
220412c0af2SDevin Coughlin   }
221412c0af2SDevin Coughlin 
222412c0af2SDevin Coughlin   return nullptr;
223412c0af2SDevin Coughlin }
224412c0af2SDevin Coughlin 
22517f41608SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
22617f41608SJordan Rose                                        ExplodedNode *Pred,
22717f41608SJordan Rose                                        ExplodedNodeSet &destNodes) {
22817f41608SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
22917f41608SJordan Rose   ProgramStateRef State = Pred->getState();
23017f41608SJordan Rose 
2310dbb783cSCraig Topper   const MemRegion *Target = nullptr;
23217f41608SJordan Rose 
23317f41608SJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
23417f41608SJordan Rose   // For now, we just run the first constructor (which should still invalidate
23517f41608SJordan Rose   // the entire array).
23617f41608SJordan Rose 
237690ab040SArtem Dergachev   EvalCallOptions CallOpts;
238661ab34aSArtem Dergachev   auto C = getCurrentCFGElement().getAs<CFGConstructor>();
239*4068481bSArtem Dergachev   assert(C || getCurrentCFGElement().getAs<CFGStmt>());
240661ab34aSArtem Dergachev   const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr;
241661ab34aSArtem Dergachev 
242661ab34aSArtem Dergachev   const CXXBindTemporaryExpr *BTE = nullptr;
243308e27eeSArtem Dergachev   const MaterializeTemporaryExpr *MTE = nullptr;
244690ab040SArtem Dergachev 
24517f41608SJordan Rose   switch (CE->getConstructionKind()) {
24617f41608SJordan Rose   case CXXConstructExpr::CK_Complete: {
247661ab34aSArtem Dergachev     Target = getRegionForConstructedObject(CE, Pred, CC, CallOpts);
248*4068481bSArtem Dergachev 
249*4068481bSArtem Dergachev     // In case of temporary object construction, extract data necessary for
250*4068481bSArtem Dergachev     // destruction and lifetime extension.
251*4068481bSArtem Dergachev     if (const auto *TCC =
252*4068481bSArtem Dergachev             dyn_cast_or_null<TemporaryObjectConstructionContext>(CC)) {
253*4068481bSArtem Dergachev       assert(CallOpts.IsTemporaryCtorOrDtor);
254*4068481bSArtem Dergachev       assert(!CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion);
255*4068481bSArtem Dergachev       if (AMgr.getAnalyzerOptions().includeTemporaryDtorsInCFG()) {
256*4068481bSArtem Dergachev         BTE = TCC->getCXXBindTemporaryExpr();
257*4068481bSArtem Dergachev         MTE = TCC->getMaterializedTemporaryExpr();
258*4068481bSArtem Dergachev         if (!BTE) {
259*4068481bSArtem Dergachev           // FIXME: lifetime extension for temporaries without destructors
260*4068481bSArtem Dergachev           // is not implemented yet.
261*4068481bSArtem Dergachev           MTE = nullptr;
262*4068481bSArtem Dergachev         }
263*4068481bSArtem Dergachev         if (MTE && MTE->getStorageDuration() != SD_FullExpression) {
264308e27eeSArtem Dergachev           // If the temporary is lifetime-extended, don't save the BTE,
265308e27eeSArtem Dergachev           // because we don't need a temporary destructor, but an automatic
266*4068481bSArtem Dergachev           // destructor.
267*4068481bSArtem Dergachev           BTE = nullptr;
268*4068481bSArtem Dergachev         }
269661ab34aSArtem Dergachev       }
270308e27eeSArtem Dergachev     }
271b970505dSJordan Rose     break;
272b970505dSJordan Rose   }
273b3b976f0SJordan Rose   case CXXConstructExpr::CK_VirtualBase:
274b3b976f0SJordan Rose     // Make sure we are not calling virtual base class initializers twice.
275b3b976f0SJordan Rose     // Only the most-derived object should initialize virtual base classes.
276b3b976f0SJordan Rose     if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) {
277b3b976f0SJordan Rose       const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer);
278b3b976f0SJordan Rose       if (OuterCtor) {
279b3b976f0SJordan Rose         switch (OuterCtor->getConstructionKind()) {
280b970505dSJordan Rose         case CXXConstructExpr::CK_NonVirtualBase:
281b970505dSJordan Rose         case CXXConstructExpr::CK_VirtualBase:
282b3b976f0SJordan Rose           // Bail out!
283b3b976f0SJordan Rose           destNodes.Add(Pred);
284b3b976f0SJordan Rose           return;
285b3b976f0SJordan Rose         case CXXConstructExpr::CK_Complete:
286b3b976f0SJordan Rose         case CXXConstructExpr::CK_Delegating:
287b3b976f0SJordan Rose           break;
288b3b976f0SJordan Rose         }
289b3b976f0SJordan Rose       }
290b3b976f0SJordan Rose     }
291b3b976f0SJordan Rose     // FALLTHROUGH
292b3b976f0SJordan Rose   case CXXConstructExpr::CK_NonVirtualBase:
293e8ba3ec4SArtem Dergachev     // In C++17, classes with non-virtual bases may be aggregates, so they would
294e8ba3ec4SArtem Dergachev     // be initialized as aggregates without a constructor call, so we may have
295e8ba3ec4SArtem Dergachev     // a base class constructed directly into an initializer list without
296e8ba3ec4SArtem Dergachev     // having the derived-class constructor call on the previous stack frame.
297e8ba3ec4SArtem Dergachev     // Initializer lists may be nested into more initializer lists that
298e8ba3ec4SArtem Dergachev     // correspond to surrounding aggregate initializations.
299e8ba3ec4SArtem Dergachev     // FIXME: For now this code essentially bails out. We need to find the
300e8ba3ec4SArtem Dergachev     // correct target region and set it.
301e8ba3ec4SArtem Dergachev     // FIXME: Instead of relying on the ParentMap, we should have the
302e8ba3ec4SArtem Dergachev     // trigger-statement (InitListExpr in this case) passed down from CFG or
303e8ba3ec4SArtem Dergachev     // otherwise always available during construction.
304e8ba3ec4SArtem Dergachev     if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(CE))) {
305e8ba3ec4SArtem Dergachev       MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
306e8ba3ec4SArtem Dergachev       Target = MRMgr.getCXXTempObjectRegion(CE, LCtx);
307168e29f6SArtem Dergachev       CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
308e8ba3ec4SArtem Dergachev       break;
309e8ba3ec4SArtem Dergachev     }
310e8ba3ec4SArtem Dergachev     // FALLTHROUGH
311b970505dSJordan Rose   case CXXConstructExpr::CK_Delegating: {
312b970505dSJordan Rose     const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
313b970505dSJordan Rose     Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
314b970505dSJordan Rose                                               LCtx->getCurrentStackFrame());
31505375eb4SJordan Rose     SVal ThisVal = State->getSVal(ThisPtr);
316b970505dSJordan Rose 
317b970505dSJordan Rose     if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
318b970505dSJordan Rose       Target = ThisVal.getAsRegion();
319b970505dSJordan Rose     } else {
320b970505dSJordan Rose       // Cast to the base type.
321599f85abSJordan Rose       bool IsVirtual =
322599f85abSJordan Rose         (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
323599f85abSJordan Rose       SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
324599f85abSJordan Rose                                                          IsVirtual);
32520edae87SJordan Rose       Target = BaseVal.getAsRegion();
326b970505dSJordan Rose     }
327b970505dSJordan Rose     break;
328b970505dSJordan Rose   }
329b970505dSJordan Rose   }
330b970505dSJordan Rose 
331fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
332fcd016e5SJordan Rose   CallEventRef<CXXConstructorCall> Call =
333fcd016e5SJordan Rose     CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
33410e25031STed Kremenek 
335f3c12ac3SJordan Rose   ExplodedNodeSet DstPreVisit;
336f3c12ac3SJordan Rose   getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
337b8e28654SJordan Rose 
338661ab34aSArtem Dergachev   // FIXME: Is it possible and/or useful to do this before PreStmt?
339b8e28654SJordan Rose   ExplodedNodeSet PreInitialized;
340b8e28654SJordan Rose   {
341b8e28654SJordan Rose     StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
342b8e28654SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
343b8e28654SJordan Rose                                    E = DstPreVisit.end();
344b8e28654SJordan Rose          I != E; ++I) {
345b8e28654SJordan Rose       ProgramStateRef State = (*I)->getState();
346661ab34aSArtem Dergachev       if (CE->requiresZeroInitialization()) {
347661ab34aSArtem Dergachev         // Type of the zero doesn't matter.
348661ab34aSArtem Dergachev         SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy);
349661ab34aSArtem Dergachev 
350b8e28654SJordan Rose         // FIXME: Once we properly handle constructors in new-expressions, we'll
351b8e28654SJordan Rose         // need to invalidate the region before setting a default value, to make
352b8e28654SJordan Rose         // sure there aren't any lingering bindings around. This probably needs
353b8e28654SJordan Rose         // to happen regardless of whether or not the object is zero-initialized
354b8e28654SJordan Rose         // to handle random fields of a placement-initialized object picking up
355b8e28654SJordan Rose         // old bindings. We might only want to do it when we need to, though.
356b8e28654SJordan Rose         // FIXME: This isn't actually correct for arrays -- we need to zero-
357b8e28654SJordan Rose         // initialize the entire array, not just the first element -- but our
358b8e28654SJordan Rose         // handling of arrays everywhere else is weak as well, so this shouldn't
359b8e28654SJordan Rose         // actually make things worse. Placement new makes this tricky as well,
360b8e28654SJordan Rose         // since it's then possible to be initializing one part of a multi-
361b8e28654SJordan Rose         // dimensional array.
362b570195cSAnna Zaks         State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal, LCtx);
363661ab34aSArtem Dergachev       }
364661ab34aSArtem Dergachev 
365661ab34aSArtem Dergachev       if (BTE) {
366661ab34aSArtem Dergachev         State = addInitializedTemporary(State, BTE, LCtx,
367661ab34aSArtem Dergachev                                         cast<CXXTempObjectRegion>(Target));
368661ab34aSArtem Dergachev       }
369661ab34aSArtem Dergachev 
370308e27eeSArtem Dergachev       if (MTE) {
371308e27eeSArtem Dergachev         State = addTemporaryMaterialization(State, MTE, LCtx,
372308e27eeSArtem Dergachev                                             cast<CXXTempObjectRegion>(Target));
373308e27eeSArtem Dergachev       }
374308e27eeSArtem Dergachev 
3750dbb783cSCraig Topper       Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
3760dbb783cSCraig Topper                         ProgramPoint::PreStmtKind);
377b8e28654SJordan Rose     }
378b8e28654SJordan Rose   }
379b8e28654SJordan Rose 
380afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
381b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
382fcd016e5SJordan Rose                                             *Call, *this);
38310e25031STed Kremenek 
38488bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
38588bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
38688bb563cSJordan Rose 
387e83cb092SJordan Rose   if (CE->getConstructor()->isTrivial() &&
388e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
389168e29f6SArtem Dergachev       !CallOpts.IsArrayCtorOrDtor) {
390e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
391e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
39288bb563cSJordan Rose          I != E; ++I)
39388bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
39488bb563cSJordan Rose 
39588bb563cSJordan Rose   } else {
396afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
397f3c12ac3SJordan Rose          I != E; ++I)
398690ab040SArtem Dergachev       defaultEvalCall(Bldr, *I, *Call, CallOpts);
39988bb563cSJordan Rose   }
40010e25031STed Kremenek 
4015b1ee2faSDevin Coughlin   // If the CFG was contructed without elements for temporary destructors
4025b1ee2faSDevin Coughlin   // and the just-called constructor created a temporary object then
4035b1ee2faSDevin Coughlin   // stop exploration if the temporary object has a noreturn constructor.
4045b1ee2faSDevin Coughlin   // This can lose coverage because the destructor, if it were present
4055b1ee2faSDevin Coughlin   // in the CFG, would be called at the end of the full expression or
4065b1ee2faSDevin Coughlin   // later (for life-time extended temporaries) -- but avoids infeasible
4075b1ee2faSDevin Coughlin   // paths when no-return temporary destructors are used for assertions.
4085b1ee2faSDevin Coughlin   const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
4095b1ee2faSDevin Coughlin   if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
4105b1ee2faSDevin Coughlin     const MemRegion *Target = Call->getCXXThisVal().getAsRegion();
4115b1ee2faSDevin Coughlin     if (Target && isa<CXXTempObjectRegion>(Target) &&
4125b1ee2faSDevin Coughlin         Call->getDecl()->getParent()->isAnyDestructorNoReturn()) {
4135b1ee2faSDevin Coughlin 
4143da72051SArtem Dergachev       // If we've inlined the constructor, then DstEvaluated would be empty.
4153da72051SArtem Dergachev       // In this case we still want a sink, which could be implemented
4163da72051SArtem Dergachev       // in processCallExit. But we don't have that implemented at the moment,
4173da72051SArtem Dergachev       // so if you hit this assertion, see if you can avoid inlining
4183da72051SArtem Dergachev       // the respective constructor when analyzer-config cfg-temporary-dtors
4193da72051SArtem Dergachev       // is set to false.
4203da72051SArtem Dergachev       // Otherwise there's nothing wrong with inlining such constructor.
4213da72051SArtem Dergachev       assert(!DstEvaluated.empty() &&
4223da72051SArtem Dergachev              "We should not have inlined this constructor!");
4233da72051SArtem Dergachev 
4245b1ee2faSDevin Coughlin       for (ExplodedNode *N : DstEvaluated) {
4255b1ee2faSDevin Coughlin         Bldr.generateSink(CE, N, N->getState());
4265b1ee2faSDevin Coughlin       }
4275b1ee2faSDevin Coughlin 
4285b1ee2faSDevin Coughlin       // There is no need to run the PostCall and PostStmt checker
4295b1ee2faSDevin Coughlin       // callbacks because we just generated sinks on all nodes in th
4305b1ee2faSDevin Coughlin       // frontier.
4315b1ee2faSDevin Coughlin       return;
4325b1ee2faSDevin Coughlin     }
4335b1ee2faSDevin Coughlin   }
4345b1ee2faSDevin Coughlin 
435afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
43688bb563cSJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
437fcd016e5SJordan Rose                                              *Call, *this);
438afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
43910e25031STed Kremenek }
44010e25031STed Kremenek 
441b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
44210e25031STed Kremenek                                     const MemRegion *Dest,
44310e25031STed Kremenek                                     const Stmt *S,
4442bc9674bSJordan Rose                                     bool IsBaseDtor,
44510e25031STed Kremenek                                     ExplodedNode *Pred,
446168e29f6SArtem Dergachev                                     ExplodedNodeSet &Dst,
447168e29f6SArtem Dergachev                                     const EvalCallOptions &CallOpts) {
448fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
449fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
450fcd016e5SJordan Rose 
451b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
452b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
453b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
454b970505dSJordan Rose 
455fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
456fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
4572bc9674bSJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
458142adc49STed Kremenek 
45917a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
46017a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
46117a8757aSJordan Rose                                 "Error evaluating destructor");
46217a8757aSJordan Rose 
4634ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
4644ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
465fcd016e5SJordan Rose                                             *Call, *this);
46610e25031STed Kremenek 
4674ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
468d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
4694ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
4704ee71b8aSJordan Rose        I != E; ++I)
471690ab040SArtem Dergachev     defaultEvalCall(Bldr, *I, *Call, CallOpts);
4724ee71b8aSJordan Rose 
4734ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
4744ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
475fcd016e5SJordan Rose                                              *Call, *this);
47610e25031STed Kremenek }
47710e25031STed Kremenek 
4788b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
4798b808d64SJordan Rose                                           ExplodedNode *Pred,
4808b808d64SJordan Rose                                           ExplodedNodeSet &Dst) {
4818b808d64SJordan Rose   ProgramStateRef State = Pred->getState();
4828b808d64SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
4838b808d64SJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
4848b808d64SJordan Rose                                 CNE->getStartLoc(),
4858b808d64SJordan Rose                                 "Error evaluating New Allocator Call");
4868b808d64SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
4878b808d64SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
4888b808d64SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
4898b808d64SJordan Rose 
4908b808d64SJordan Rose   ExplodedNodeSet DstPreCall;
4918b808d64SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
4928b808d64SJordan Rose                                             *Call, *this);
4938b808d64SJordan Rose 
49455796302SArtem Dergachev   ExplodedNodeSet DstPostCall;
49555796302SArtem Dergachev   StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
4961084de52SArtem Dergachev   for (auto I : DstPreCall) {
4971084de52SArtem Dergachev     // FIXME: Provide evalCall for checkers?
49855796302SArtem Dergachev     defaultEvalCall(CallBldr, I, *Call);
4991084de52SArtem Dergachev   }
500beba5307SArtem Dergachev   // If the call is inlined, DstPostCall will be empty and we bail out now.
50155796302SArtem Dergachev 
50255796302SArtem Dergachev   // Store return value of operator new() for future use, until the actual
50355796302SArtem Dergachev   // CXXNewExpr gets processed.
50455796302SArtem Dergachev   ExplodedNodeSet DstPostValue;
50555796302SArtem Dergachev   StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
50655796302SArtem Dergachev   for (auto I : DstPostCall) {
507beba5307SArtem Dergachev     // FIXME: Because CNE serves as the "call site" for the allocator (due to
508beba5307SArtem Dergachev     // lack of a better expression in the AST), the conjured return value symbol
509beba5307SArtem Dergachev     // is going to be of the same type (C++ object pointer type). Technically
510beba5307SArtem Dergachev     // this is not correct because the operator new's prototype always says that
511beba5307SArtem Dergachev     // it returns a 'void *'. So we should change the type of the symbol,
512beba5307SArtem Dergachev     // and then evaluate the cast over the symbolic pointer from 'void *' to
513beba5307SArtem Dergachev     // the object pointer type. But without changing the symbol's type it
514beba5307SArtem Dergachev     // is breaking too much to evaluate the no-op symbolic cast over it, so we
515beba5307SArtem Dergachev     // skip it for now.
51655796302SArtem Dergachev     ProgramStateRef State = I->getState();
51750e0372fSArtem Dergachev     SVal RetVal = State->getSVal(CNE, LCtx);
51850e0372fSArtem Dergachev 
51950e0372fSArtem Dergachev     // If this allocation function is not declared as non-throwing, failures
52050e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
52150e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
52250e0372fSArtem Dergachev     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
52350e0372fSArtem Dergachev     // where new can return NULL. If we end up supporting that option, we can
52450e0372fSArtem Dergachev     // consider adding a check for it here.
52550e0372fSArtem Dergachev     // C++11 [basic.stc.dynamic.allocation]p3.
52650e0372fSArtem Dergachev     if (const FunctionDecl *FD = CNE->getOperatorNew()) {
52750e0372fSArtem Dergachev       QualType Ty = FD->getType();
52850e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
52950e0372fSArtem Dergachev         if (!ProtoType->isNothrow(getContext()))
53050e0372fSArtem Dergachev           State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
53150e0372fSArtem Dergachev     }
53250e0372fSArtem Dergachev 
53350e0372fSArtem Dergachev     ValueBldr.generateNode(CNE, I,
53450e0372fSArtem Dergachev                            setCXXNewAllocatorValue(State, CNE, LCtx, RetVal));
53555796302SArtem Dergachev   }
53655796302SArtem Dergachev 
53713b2026bSArtem Dergachev   ExplodedNodeSet DstPostPostCallCallback;
53813b2026bSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
53913b2026bSArtem Dergachev                                              DstPostValue, *Call, *this);
54013b2026bSArtem Dergachev   for (auto I : DstPostPostCallCallback) {
54113b2026bSArtem Dergachev     getCheckerManager().runCheckersForNewAllocator(
54213b2026bSArtem Dergachev         CNE, getCXXNewAllocatorValue(I->getState(), CNE, LCtx), Dst, I, *this);
54313b2026bSArtem Dergachev   }
5448b808d64SJordan Rose }
5458b808d64SJordan Rose 
54610e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
54710e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
548a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
549a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
550a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
551a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
55210e25031STed Kremenek 
553d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
554d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
55555796302SArtem Dergachev   SVal symVal = UnknownVal();
5568b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
55710e25031STed Kremenek 
5581084de52SArtem Dergachev   bool IsStandardGlobalOpNewFunction =
5591084de52SArtem Dergachev       FD->isReplaceableGlobalAllocationFunction();
5608b662704SAnton Yartsev 
56155796302SArtem Dergachev   ProgramStateRef State = Pred->getState();
56255796302SArtem Dergachev 
56355796302SArtem Dergachev   // Retrieve the stored operator new() return value.
56455796302SArtem Dergachev   if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
56555796302SArtem Dergachev     symVal = getCXXNewAllocatorValue(State, CNE, LCtx);
56655796302SArtem Dergachev     State = clearCXXNewAllocatorValue(State, CNE, LCtx);
56755796302SArtem Dergachev   }
56855796302SArtem Dergachev 
5698b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
5708b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
5718b662704SAnton Yartsev   // a custom global allocator.
57255796302SArtem Dergachev   if (symVal.isUnknown()) {
5738b662704SAnton Yartsev     if (IsStandardGlobalOpNewFunction)
5748b662704SAnton Yartsev       symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
5758b662704SAnton Yartsev     else
5760dbb783cSCraig Topper       symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
5778b662704SAnton Yartsev                                             blockCount);
57855796302SArtem Dergachev   }
5798b662704SAnton Yartsev 
580fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
581fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
582fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
583fcd016e5SJordan Rose 
58455796302SArtem Dergachev   if (!AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
585a4ee064cSJordan Rose     // Invalidate placement args.
586fcd016e5SJordan Rose     // FIXME: Once we figure out how we want allocators to work,
587fcd016e5SJordan Rose     // we should be using the usual pre-/(default-)eval-/post-call checks here.
588fcd016e5SJordan Rose     State = Call->invalidateRegions(blockCount);
589b8cb8359SJordan Rose     if (!State)
590b8cb8359SJordan Rose       return;
591a4ee064cSJordan Rose 
592fce1b03eSPavel Labath     // If this allocation function is not declared as non-throwing, failures
59350e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
59450e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
595fce1b03eSPavel Labath     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
596fce1b03eSPavel Labath     // where new can return NULL. If we end up supporting that option, we can
597fce1b03eSPavel Labath     // consider adding a check for it here.
5983957fd58SJordan Rose     // C++11 [basic.stc.dynamic.allocation]p3.
599fce1b03eSPavel Labath     if (FD) {
6003957fd58SJordan Rose       QualType Ty = FD->getType();
60150e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
6023957fd58SJordan Rose         if (!ProtoType->isNothrow(getContext()))
60355796302SArtem Dergachev           if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
60455796302SArtem Dergachev             State = State->assume(*dSymVal, true);
6053957fd58SJordan Rose     }
60650e0372fSArtem Dergachev   }
6073957fd58SJordan Rose 
608b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
609b8cb8359SJordan Rose 
610beba5307SArtem Dergachev   SVal Result = symVal;
611beba5307SArtem Dergachev 
61210e25031STed Kremenek   if (CNE->isArray()) {
61310e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
61410e25031STed Kremenek     // For now, just return a symbolicated region.
6151084de52SArtem Dergachev     if (const SubRegion *NewReg =
6161084de52SArtem Dergachev             dyn_cast_or_null<SubRegion>(symVal.getAsRegion())) {
617a4ee064cSJordan Rose       QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
618a4ee064cSJordan Rose       const ElementRegion *EleReg =
619a4ee064cSJordan Rose           getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
620beba5307SArtem Dergachev       Result = loc::MemRegionVal(EleReg);
621beba5307SArtem Dergachev     }
622beba5307SArtem Dergachev     State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
62344320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
62410e25031STed Kremenek     return;
62510e25031STed Kremenek   }
62610e25031STed Kremenek 
627a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
628a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
629a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
630a4ee064cSJordan Rose   // the constructor call anyway.)
63144320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
63244320367SJordan Rose     // Non-array placement new should always return the placement location.
63344320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
634b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
635aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
63644320367SJordan Rose   }
63744320367SJordan Rose 
638b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
639b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
6408f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
6418f6b4b04SJordan Rose   if (!NewN)
6428f6b4b04SJordan Rose     return;
6433503cb35SJordan Rose 
6445089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
6455089f3b3SJordan Rose   // initializer. Copy the value over.
6465089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
6475089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
6483503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
6498f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
650b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
651b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
6523503cb35SJordan Rose     }
6533503cb35SJordan Rose   }
65410e25031STed Kremenek }
65510e25031STed Kremenek 
65610e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
65710e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
658d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
65964aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
66064aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
66110e25031STed Kremenek }
66210e25031STed Kremenek 
6638fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
6648fdb59f9STed Kremenek                                    ExplodedNode *Pred,
6658fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
6668fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
667b1279b5cSTed Kremenek   if (!VD) {
668b1279b5cSTed Kremenek     Dst.Add(Pred);
669b1279b5cSTed Kremenek     return;
670b1279b5cSTed Kremenek   }
671b1279b5cSTed Kremenek 
6728fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
673d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
674d94854a4STed Kremenek                                         currBldrCtx->blockCount());
6758fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
676b570195cSAnna Zaks   state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
6778fdb59f9STed Kremenek 
678d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
6798fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
6808fdb59f9STed Kremenek }
6818fdb59f9STed Kremenek 
68210e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
68310e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
684d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
685e594034fSAnna Zaks 
68610e25031STed Kremenek   // Get the this object region from StoreManager.
687632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
68810e25031STed Kremenek   const MemRegion *R =
68910e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
69010e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
691632e3b7eSTed Kremenek                                                     LCtx);
69210e25031STed Kremenek 
69349b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
69410e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
695632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
69610e25031STed Kremenek }
69715843343SGabor Horvath 
69815843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
69915843343SGabor Horvath                                  ExplodedNodeSet &Dst) {
70015843343SGabor Horvath   const LocationContext *LocCtxt = Pred->getLocationContext();
70115843343SGabor Horvath 
70215843343SGabor Horvath   // Get the region of the lambda itself.
70315843343SGabor Horvath   const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
70415843343SGabor Horvath       LE, LocCtxt);
70515843343SGabor Horvath   SVal V = loc::MemRegionVal(R);
70615843343SGabor Horvath 
70715843343SGabor Horvath   ProgramStateRef State = Pred->getState();
70815843343SGabor Horvath 
70915843343SGabor Horvath   // If we created a new MemRegion for the lambda, we should explicitly bind
71015843343SGabor Horvath   // the captures.
71115843343SGabor Horvath   CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
71215843343SGabor Horvath   for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
71315843343SGabor Horvath                                                e = LE->capture_init_end();
71415843343SGabor Horvath        i != e; ++i, ++CurField) {
7157bdca8b2SDevin Coughlin     FieldDecl *FieldForCapture = *CurField;
7167bdca8b2SDevin Coughlin     SVal FieldLoc = State->getLValue(FieldForCapture, V);
7177bdca8b2SDevin Coughlin 
7187bdca8b2SDevin Coughlin     SVal InitVal;
7197bdca8b2SDevin Coughlin     if (!FieldForCapture->hasCapturedVLAType()) {
7207bdca8b2SDevin Coughlin       Expr *InitExpr = *i;
7217bdca8b2SDevin Coughlin       assert(InitExpr && "Capture missing initialization expression");
7227bdca8b2SDevin Coughlin       InitVal = State->getSVal(InitExpr, LocCtxt);
7237bdca8b2SDevin Coughlin     } else {
7247bdca8b2SDevin Coughlin       // The field stores the length of a captured variable-length array.
7257bdca8b2SDevin Coughlin       // These captures don't have initialization expressions; instead we
7267bdca8b2SDevin Coughlin       // get the length from the VLAType size expression.
7277bdca8b2SDevin Coughlin       Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
7287bdca8b2SDevin Coughlin       InitVal = State->getSVal(SizeExpr, LocCtxt);
7297bdca8b2SDevin Coughlin     }
7307bdca8b2SDevin Coughlin 
731b570195cSAnna Zaks     State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
73215843343SGabor Horvath   }
73315843343SGabor Horvath 
73415843343SGabor Horvath   // Decay the Loc into an RValue, because there might be a
73515843343SGabor Horvath   // MaterializeTemporaryExpr node above this one which expects the bound value
73615843343SGabor Horvath   // to be an RValue.
73715843343SGabor Horvath   SVal LambdaRVal = State->getSVal(R);
73815843343SGabor Horvath 
73915843343SGabor Horvath   ExplodedNodeSet Tmp;
74015843343SGabor Horvath   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
74115843343SGabor Horvath   // FIXME: is this the right program point kind?
74215843343SGabor Horvath   Bldr.generateNode(LE, Pred,
74315843343SGabor Horvath                     State->BindExpr(LE, LocCtxt, LambdaRVal),
74415843343SGabor Horvath                     nullptr, ProgramPoint::PostLValueKind);
74515843343SGabor Horvath 
74615843343SGabor Horvath   // FIXME: Move all post/pre visits to ::Visit().
74715843343SGabor Horvath   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
74815843343SGabor Horvath }
749