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"
154068481bSArtem 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;
454449e7f0SArtem Dergachev   const CXXRecordDecl *ThisRD = nullptr;
463d7b7f52SJordan Rose   if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
473d7b7f52SJordan Rose     assert(Ctor->getDecl()->isTrivial());
483d7b7f52SJordan Rose     assert(Ctor->getDecl()->isCopyOrMoveConstructor());
493d7b7f52SJordan Rose     ThisVal = Ctor->getCXXThisVal();
504449e7f0SArtem Dergachev     ThisRD = Ctor->getDecl()->getParent();
513d7b7f52SJordan Rose     AlwaysReturnsLValue = false;
523d7b7f52SJordan Rose   } else {
533d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
543d7b7f52SJordan Rose     assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
553d7b7f52SJordan Rose            OO_Equal);
563d7b7f52SJordan Rose     ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
574449e7f0SArtem Dergachev     ThisRD = cast<CXXMethodDecl>(Call.getDecl())->getParent();
583d7b7f52SJordan Rose     AlwaysReturnsLValue = true;
593d7b7f52SJordan Rose   }
6088bb563cSJordan Rose 
614449e7f0SArtem Dergachev   assert(ThisRD);
624449e7f0SArtem Dergachev   if (ThisRD->isEmpty()) {
634449e7f0SArtem Dergachev     // Do nothing for empty classes. Otherwise it'd retrieve an UnknownVal
644449e7f0SArtem Dergachev     // and bind it and RegionStore would think that the actual value
654449e7f0SArtem Dergachev     // in this region at this offset is unknown.
664449e7f0SArtem Dergachev     return;
674449e7f0SArtem Dergachev   }
684449e7f0SArtem Dergachev 
6988bb563cSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
7088bb563cSJordan Rose 
7188bb563cSJordan Rose   ExplodedNodeSet Dst;
7288bb563cSJordan Rose   Bldr.takeNodes(Pred);
7388bb563cSJordan Rose 
7488bb563cSJordan Rose   SVal V = Call.getArgSVal(0);
7588bb563cSJordan Rose 
763d7b7f52SJordan Rose   // If the value being copied is not unknown, load from its location to get
773d7b7f52SJordan Rose   // an aggregate rvalue.
7805785d16SDavid Blaikie   if (Optional<Loc> L = V.getAs<Loc>())
7988bb563cSJordan Rose     V = Pred->getState()->getSVal(*L);
803d7b7f52SJordan Rose   else
8175f9d3acSArtem Dergachev     assert(V.isUnknownOrUndef());
8288bb563cSJordan Rose 
833d7b7f52SJordan Rose   const Expr *CallExpr = Call.getOriginExpr();
843d7b7f52SJordan Rose   evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
8588bb563cSJordan Rose 
863d7b7f52SJordan Rose   PostStmt PS(CallExpr, LCtx);
8788bb563cSJordan Rose   for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
8888bb563cSJordan Rose        I != E; ++I) {
8988bb563cSJordan Rose     ProgramStateRef State = (*I)->getState();
903d7b7f52SJordan Rose     if (AlwaysReturnsLValue)
913d7b7f52SJordan Rose       State = State->BindExpr(CallExpr, LCtx, ThisVal);
923d7b7f52SJordan Rose     else
9388bb563cSJordan Rose       State = bindReturnValue(Call, LCtx, State);
9488bb563cSJordan Rose     Bldr.generateNode(PS, State, *I);
9588bb563cSJordan Rose   }
9688bb563cSJordan Rose }
9788bb563cSJordan Rose 
98bc74eb1cSJordan Rose 
99168e29f6SArtem Dergachev SVal ExprEngine::makeZeroElementRegion(ProgramStateRef State, SVal LValue,
100690ab040SArtem Dergachev                                        QualType &Ty, bool &IsArray) {
101bc74eb1cSJordan Rose   SValBuilder &SVB = State->getStateManager().getSValBuilder();
102bc74eb1cSJordan Rose   ASTContext &Ctx = SVB.getContext();
103bc74eb1cSJordan Rose 
104bc74eb1cSJordan Rose   while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
105bc74eb1cSJordan Rose     Ty = AT->getElementType();
106bc74eb1cSJordan Rose     LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
107690ab040SArtem Dergachev     IsArray = true;
108bc74eb1cSJordan Rose   }
109bc74eb1cSJordan Rose 
110bc74eb1cSJordan Rose   return LValue;
111bc74eb1cSJordan Rose }
112bc74eb1cSJordan Rose 
11317f41608SJordan Rose 
114412c0af2SDevin Coughlin const MemRegion *
115412c0af2SDevin Coughlin ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
116690ab040SArtem Dergachev                                           ExplodedNode *Pred,
117661ab34aSArtem Dergachev                                           const ConstructionContext *CC,
118690ab040SArtem Dergachev                                           EvalCallOptions &CallOpts) {
119b970505dSJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
12005375eb4SJordan Rose   ProgramStateRef State = Pred->getState();
12194020268SArtem Dergachev   MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
12205375eb4SJordan Rose 
123afb158c2SArtem Dergachev   // See if we're constructing an existing region by looking at the
124afb158c2SArtem Dergachev   // current construction context.
125661ab34aSArtem Dergachev   if (CC) {
1264068481bSArtem Dergachev     switch (CC->getKind()) {
1274068481bSArtem Dergachev     case ConstructionContext::SimpleVariableKind: {
1284068481bSArtem Dergachev       const auto *DSCC = cast<SimpleVariableConstructionContext>(CC);
1294068481bSArtem Dergachev       const auto *DS = DSCC->getDeclStmt();
130e231bd34SArtem Dergachev       const auto *Var = cast<VarDecl>(DS->getSingleDecl());
131bc74eb1cSJordan Rose       SVal LValue = State->getLValue(Var, LCtx);
13220edae87SJordan Rose       QualType Ty = Var->getType();
1334068481bSArtem Dergachev       LValue =
1344068481bSArtem Dergachev           makeZeroElementRegion(State, LValue, Ty, CallOpts.IsArrayCtorOrDtor);
13517f41608SJordan Rose       return LValue.getAsRegion();
13620edae87SJordan Rose     }
1374068481bSArtem Dergachev     case ConstructionContext::ConstructorInitializerKind: {
1384068481bSArtem Dergachev       const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC);
1394068481bSArtem Dergachev       const auto *Init = ICC->getCXXCtorInitializer();
14054529a34SJordan Rose       assert(Init->isAnyMemberInitializer());
14154529a34SJordan Rose       const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
142412c0af2SDevin Coughlin       Loc ThisPtr =
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();
157690ab040SArtem Dergachev       FieldVal = makeZeroElementRegion(State, FieldVal, Ty,
158168e29f6SArtem Dergachev                                        CallOpts.IsArrayCtorOrDtor);
15917f41608SJordan Rose       return FieldVal.getAsRegion();
16054529a34SJordan Rose     }
1614068481bSArtem Dergachev     case ConstructionContext::NewAllocatedObjectKind: {
1624068481bSArtem Dergachev       if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
1634068481bSArtem Dergachev         const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC);
1644068481bSArtem Dergachev         const auto *NE = NECC->getCXXNewExpr();
1654068481bSArtem Dergachev         // TODO: Detect when the allocator returns a null pointer.
1664068481bSArtem Dergachev         // Constructor shall not be called in this case.
1674068481bSArtem Dergachev         if (const SubRegion *MR = dyn_cast_or_null<SubRegion>(
1684068481bSArtem Dergachev                 getCXXNewAllocatorValue(State, NE, LCtx).getAsRegion())) {
1694068481bSArtem Dergachev           if (NE->isArray()) {
1704068481bSArtem Dergachev             // TODO: In fact, we need to call the constructor for every
1714068481bSArtem Dergachev             // allocated element, not just the first one!
1724068481bSArtem Dergachev             CallOpts.IsArrayCtorOrDtor = true;
1734068481bSArtem Dergachev             return getStoreManager().GetElementZeroRegion(
1744068481bSArtem Dergachev                 MR, NE->getType()->getPointeeType());
1754068481bSArtem Dergachev           }
1764068481bSArtem Dergachev           return MR;
1774068481bSArtem Dergachev         }
1784068481bSArtem Dergachev       }
1794068481bSArtem Dergachev       break;
1804068481bSArtem Dergachev     }
1814068481bSArtem Dergachev     case ConstructionContext::TemporaryObjectKind: {
1828cd7961aSArtem Dergachev       const auto *TOCC = cast<TemporaryObjectConstructionContext>(CC);
1838cd7961aSArtem Dergachev       // See if we're lifetime-extended via our field. If so, take a note.
1848cd7961aSArtem Dergachev       // Because automatic destructors aren't quite working in this case.
1858cd7961aSArtem Dergachev       if (const auto *MTE = TOCC->getMaterializedTemporaryExpr()) {
1868cd7961aSArtem Dergachev         if (const ValueDecl *VD = MTE->getExtendingDecl()) {
1878cd7961aSArtem Dergachev           assert(VD->getType()->isReferenceType());
1888cd7961aSArtem Dergachev           if (VD->getType()->getPointeeType().getCanonicalType() !=
1898cd7961aSArtem Dergachev               MTE->GetTemporaryExpr()->getType().getCanonicalType()) {
1908cd7961aSArtem Dergachev             CallOpts.IsTemporaryLifetimeExtendedViaSubobject = true;
1918cd7961aSArtem Dergachev           }
1928cd7961aSArtem Dergachev         }
1938cd7961aSArtem Dergachev       }
1944068481bSArtem Dergachev       // TODO: Support temporaries lifetime-extended via static references.
1954068481bSArtem Dergachev       // They'd need a getCXXStaticTempObjectRegion().
1964068481bSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
1974068481bSArtem Dergachev       return MRMgr.getCXXTempObjectRegion(CE, LCtx);
1984068481bSArtem Dergachev     }
1994068481bSArtem Dergachev     case ConstructionContext::ReturnedValueKind: {
200e0789678SArtem Dergachev       // The temporary is to be managed by the parent stack frame.
201e0789678SArtem Dergachev       // So build it in the parent stack frame if we're not in the
202e0789678SArtem Dergachev       // top frame of the analysis.
203e0789678SArtem Dergachev       // TODO: What exactly happens when we are? Does the temporary object live
204e0789678SArtem Dergachev       // long enough in the region store in this case? Would checkers think
205e0789678SArtem Dergachev       // that this object immediately goes out of scope?
206e0789678SArtem Dergachev       const LocationContext *TempLCtx = LCtx;
207e0789678SArtem Dergachev       if (const LocationContext *CallerLCtx =
208e0789678SArtem Dergachev               LCtx->getCurrentStackFrame()->getParent()) {
209e0789678SArtem Dergachev         TempLCtx = CallerLCtx;
210e0789678SArtem Dergachev       }
2114068481bSArtem Dergachev       CallOpts.IsTemporaryCtorOrDtor = true;
212e0789678SArtem Dergachev       return MRMgr.getCXXTempObjectRegion(CE, TempLCtx);
2134068481bSArtem Dergachev     }
2144068481bSArtem Dergachev     }
21505375eb4SJordan Rose   }
216c9318304SJordan Rose   // If we couldn't find an existing region to construct into, assume we're
217690ab040SArtem Dergachev   // constructing a temporary. Notify the caller of our failure.
218168e29f6SArtem Dergachev   CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
21917f41608SJordan Rose   return MRMgr.getCXXTempObjectRegion(CE, LCtx);
220c9318304SJordan Rose }
22105375eb4SJordan Rose 
222412c0af2SDevin Coughlin const CXXConstructExpr *
223412c0af2SDevin Coughlin ExprEngine::findDirectConstructorForCurrentCFGElement() {
224412c0af2SDevin Coughlin   // Go backward in the CFG to see if the previous element (ignoring
225412c0af2SDevin Coughlin   // destructors) was a CXXConstructExpr. If so, that constructor
226412c0af2SDevin Coughlin   // was constructed directly into an existing region.
227412c0af2SDevin Coughlin   // This process is essentially the inverse of that performed in
228412c0af2SDevin Coughlin   // findElementDirectlyInitializedByCurrentConstructor().
229412c0af2SDevin Coughlin   if (currStmtIdx == 0)
230412c0af2SDevin Coughlin     return nullptr;
231412c0af2SDevin Coughlin 
232412c0af2SDevin Coughlin   const CFGBlock *B = getBuilderContext().getBlock();
233412c0af2SDevin Coughlin 
234412c0af2SDevin Coughlin   unsigned int PreviousStmtIdx = currStmtIdx - 1;
235412c0af2SDevin Coughlin   CFGElement Previous = (*B)[PreviousStmtIdx];
236412c0af2SDevin Coughlin 
237412c0af2SDevin Coughlin   while (Previous.getAs<CFGImplicitDtor>() && PreviousStmtIdx > 0) {
238412c0af2SDevin Coughlin     --PreviousStmtIdx;
239412c0af2SDevin Coughlin     Previous = (*B)[PreviousStmtIdx];
240412c0af2SDevin Coughlin   }
241412c0af2SDevin Coughlin 
242412c0af2SDevin Coughlin   if (Optional<CFGStmt> PrevStmtElem = Previous.getAs<CFGStmt>()) {
243412c0af2SDevin Coughlin     if (auto *CtorExpr = dyn_cast<CXXConstructExpr>(PrevStmtElem->getStmt())) {
244412c0af2SDevin Coughlin       return CtorExpr;
245412c0af2SDevin Coughlin     }
246412c0af2SDevin Coughlin   }
247412c0af2SDevin Coughlin 
248412c0af2SDevin Coughlin   return nullptr;
249412c0af2SDevin Coughlin }
250412c0af2SDevin Coughlin 
25117f41608SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
25217f41608SJordan Rose                                        ExplodedNode *Pred,
25317f41608SJordan Rose                                        ExplodedNodeSet &destNodes) {
25417f41608SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
25517f41608SJordan Rose   ProgramStateRef State = Pred->getState();
25617f41608SJordan Rose 
2570dbb783cSCraig Topper   const MemRegion *Target = nullptr;
25817f41608SJordan Rose 
25917f41608SJordan Rose   // FIXME: Handle arrays, which run the same constructor for every element.
26017f41608SJordan Rose   // For now, we just run the first constructor (which should still invalidate
26117f41608SJordan Rose   // the entire array).
26217f41608SJordan Rose 
263690ab040SArtem Dergachev   EvalCallOptions CallOpts;
264661ab34aSArtem Dergachev   auto C = getCurrentCFGElement().getAs<CFGConstructor>();
2654068481bSArtem Dergachev   assert(C || getCurrentCFGElement().getAs<CFGStmt>());
266661ab34aSArtem Dergachev   const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr;
267661ab34aSArtem Dergachev 
26817f41608SJordan Rose   switch (CE->getConstructionKind()) {
26917f41608SJordan Rose   case CXXConstructExpr::CK_Complete: {
270661ab34aSArtem Dergachev     Target = getRegionForConstructedObject(CE, Pred, CC, CallOpts);
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 
365*98a24bf7SArtem Dergachev       State = addAllNecessaryTemporaryInfo(State, CC, LCtx, Target);
366308e27eeSArtem Dergachev 
3670dbb783cSCraig Topper       Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
3680dbb783cSCraig Topper                         ProgramPoint::PreStmtKind);
369b8e28654SJordan Rose     }
370b8e28654SJordan Rose   }
371b8e28654SJordan Rose 
372afe7c2c2SJordan Rose   ExplodedNodeSet DstPreCall;
373b8e28654SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
374fcd016e5SJordan Rose                                             *Call, *this);
37510e25031STed Kremenek 
37688bb563cSJordan Rose   ExplodedNodeSet DstEvaluated;
37788bb563cSJordan Rose   StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
37888bb563cSJordan Rose 
379e83cb092SJordan Rose   if (CE->getConstructor()->isTrivial() &&
380e83cb092SJordan Rose       CE->getConstructor()->isCopyOrMoveConstructor() &&
381168e29f6SArtem Dergachev       !CallOpts.IsArrayCtorOrDtor) {
382e83cb092SJordan Rose     // FIXME: Handle other kinds of trivial constructors as well.
383e83cb092SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
38488bb563cSJordan Rose          I != E; ++I)
38588bb563cSJordan Rose       performTrivialCopy(Bldr, *I, *Call);
38688bb563cSJordan Rose 
38788bb563cSJordan Rose   } else {
388afe7c2c2SJordan Rose     for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
389f3c12ac3SJordan Rose          I != E; ++I)
390690ab040SArtem Dergachev       defaultEvalCall(Bldr, *I, *Call, CallOpts);
39188bb563cSJordan Rose   }
39210e25031STed Kremenek 
3935b1ee2faSDevin Coughlin   // If the CFG was contructed without elements for temporary destructors
3945b1ee2faSDevin Coughlin   // and the just-called constructor created a temporary object then
3955b1ee2faSDevin Coughlin   // stop exploration if the temporary object has a noreturn constructor.
3965b1ee2faSDevin Coughlin   // This can lose coverage because the destructor, if it were present
3975b1ee2faSDevin Coughlin   // in the CFG, would be called at the end of the full expression or
3985b1ee2faSDevin Coughlin   // later (for life-time extended temporaries) -- but avoids infeasible
3995b1ee2faSDevin Coughlin   // paths when no-return temporary destructors are used for assertions.
4005b1ee2faSDevin Coughlin   const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
4015b1ee2faSDevin Coughlin   if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
4025b1ee2faSDevin Coughlin     const MemRegion *Target = Call->getCXXThisVal().getAsRegion();
4035b1ee2faSDevin Coughlin     if (Target && isa<CXXTempObjectRegion>(Target) &&
4045b1ee2faSDevin Coughlin         Call->getDecl()->getParent()->isAnyDestructorNoReturn()) {
4055b1ee2faSDevin Coughlin 
4063da72051SArtem Dergachev       // If we've inlined the constructor, then DstEvaluated would be empty.
4073da72051SArtem Dergachev       // In this case we still want a sink, which could be implemented
4083da72051SArtem Dergachev       // in processCallExit. But we don't have that implemented at the moment,
4093da72051SArtem Dergachev       // so if you hit this assertion, see if you can avoid inlining
4103da72051SArtem Dergachev       // the respective constructor when analyzer-config cfg-temporary-dtors
4113da72051SArtem Dergachev       // is set to false.
4123da72051SArtem Dergachev       // Otherwise there's nothing wrong with inlining such constructor.
4133da72051SArtem Dergachev       assert(!DstEvaluated.empty() &&
4143da72051SArtem Dergachev              "We should not have inlined this constructor!");
4153da72051SArtem Dergachev 
4165b1ee2faSDevin Coughlin       for (ExplodedNode *N : DstEvaluated) {
4175b1ee2faSDevin Coughlin         Bldr.generateSink(CE, N, N->getState());
4185b1ee2faSDevin Coughlin       }
4195b1ee2faSDevin Coughlin 
4205b1ee2faSDevin Coughlin       // There is no need to run the PostCall and PostStmt checker
4215b1ee2faSDevin Coughlin       // callbacks because we just generated sinks on all nodes in th
4225b1ee2faSDevin Coughlin       // frontier.
4235b1ee2faSDevin Coughlin       return;
4245b1ee2faSDevin Coughlin     }
4255b1ee2faSDevin Coughlin   }
4265b1ee2faSDevin Coughlin 
427afe7c2c2SJordan Rose   ExplodedNodeSet DstPostCall;
42888bb563cSJordan Rose   getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
429fcd016e5SJordan Rose                                              *Call, *this);
430afe7c2c2SJordan Rose   getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
43110e25031STed Kremenek }
43210e25031STed Kremenek 
433b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType,
43410e25031STed Kremenek                                     const MemRegion *Dest,
43510e25031STed Kremenek                                     const Stmt *S,
4362bc9674bSJordan Rose                                     bool IsBaseDtor,
43710e25031STed Kremenek                                     ExplodedNode *Pred,
438168e29f6SArtem Dergachev                                     ExplodedNodeSet &Dst,
439168e29f6SArtem Dergachev                                     const EvalCallOptions &CallOpts) {
440fcd016e5SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
441fcd016e5SJordan Rose   ProgramStateRef State = Pred->getState();
442fcd016e5SJordan Rose 
443b970505dSJordan Rose   const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
444b970505dSJordan Rose   assert(RecordDecl && "Only CXXRecordDecls should have destructors");
445b970505dSJordan Rose   const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
446b970505dSJordan Rose 
447fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
448fcd016e5SJordan Rose   CallEventRef<CXXDestructorCall> Call =
4492bc9674bSJordan Rose     CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
450142adc49STed Kremenek 
45117a8757aSJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
45217a8757aSJordan Rose                                 Call->getSourceRange().getBegin(),
45317a8757aSJordan Rose                                 "Error evaluating destructor");
45417a8757aSJordan Rose 
4554ee71b8aSJordan Rose   ExplodedNodeSet DstPreCall;
4564ee71b8aSJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
457fcd016e5SJordan Rose                                             *Call, *this);
45810e25031STed Kremenek 
4594ee71b8aSJordan Rose   ExplodedNodeSet DstInvalidated;
460d94854a4STed Kremenek   StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
4614ee71b8aSJordan Rose   for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
4624ee71b8aSJordan Rose        I != E; ++I)
463690ab040SArtem Dergachev     defaultEvalCall(Bldr, *I, *Call, CallOpts);
4644ee71b8aSJordan Rose 
4654ee71b8aSJordan Rose   ExplodedNodeSet DstPostCall;
4664ee71b8aSJordan Rose   getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
467fcd016e5SJordan Rose                                              *Call, *this);
46810e25031STed Kremenek }
46910e25031STed Kremenek 
4708b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
4718b808d64SJordan Rose                                           ExplodedNode *Pred,
4728b808d64SJordan Rose                                           ExplodedNodeSet &Dst) {
4738b808d64SJordan Rose   ProgramStateRef State = Pred->getState();
4748b808d64SJordan Rose   const LocationContext *LCtx = Pred->getLocationContext();
4758b808d64SJordan Rose   PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
4768b808d64SJordan Rose                                 CNE->getStartLoc(),
4778b808d64SJordan Rose                                 "Error evaluating New Allocator Call");
4788b808d64SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
4798b808d64SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
4808b808d64SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
4818b808d64SJordan Rose 
4828b808d64SJordan Rose   ExplodedNodeSet DstPreCall;
4838b808d64SJordan Rose   getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
4848b808d64SJordan Rose                                             *Call, *this);
4858b808d64SJordan Rose 
48655796302SArtem Dergachev   ExplodedNodeSet DstPostCall;
48755796302SArtem Dergachev   StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
4881084de52SArtem Dergachev   for (auto I : DstPreCall) {
4891084de52SArtem Dergachev     // FIXME: Provide evalCall for checkers?
49055796302SArtem Dergachev     defaultEvalCall(CallBldr, I, *Call);
4911084de52SArtem Dergachev   }
492beba5307SArtem Dergachev   // If the call is inlined, DstPostCall will be empty and we bail out now.
49355796302SArtem Dergachev 
49455796302SArtem Dergachev   // Store return value of operator new() for future use, until the actual
49555796302SArtem Dergachev   // CXXNewExpr gets processed.
49655796302SArtem Dergachev   ExplodedNodeSet DstPostValue;
49755796302SArtem Dergachev   StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
49855796302SArtem Dergachev   for (auto I : DstPostCall) {
499beba5307SArtem Dergachev     // FIXME: Because CNE serves as the "call site" for the allocator (due to
500beba5307SArtem Dergachev     // lack of a better expression in the AST), the conjured return value symbol
501beba5307SArtem Dergachev     // is going to be of the same type (C++ object pointer type). Technically
502beba5307SArtem Dergachev     // this is not correct because the operator new's prototype always says that
503beba5307SArtem Dergachev     // it returns a 'void *'. So we should change the type of the symbol,
504beba5307SArtem Dergachev     // and then evaluate the cast over the symbolic pointer from 'void *' to
505beba5307SArtem Dergachev     // the object pointer type. But without changing the symbol's type it
506beba5307SArtem Dergachev     // is breaking too much to evaluate the no-op symbolic cast over it, so we
507beba5307SArtem Dergachev     // skip it for now.
50855796302SArtem Dergachev     ProgramStateRef State = I->getState();
50950e0372fSArtem Dergachev     SVal RetVal = State->getSVal(CNE, LCtx);
51050e0372fSArtem Dergachev 
51150e0372fSArtem Dergachev     // If this allocation function is not declared as non-throwing, failures
51250e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
51350e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
51450e0372fSArtem Dergachev     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
51550e0372fSArtem Dergachev     // where new can return NULL. If we end up supporting that option, we can
51650e0372fSArtem Dergachev     // consider adding a check for it here.
51750e0372fSArtem Dergachev     // C++11 [basic.stc.dynamic.allocation]p3.
51850e0372fSArtem Dergachev     if (const FunctionDecl *FD = CNE->getOperatorNew()) {
51950e0372fSArtem Dergachev       QualType Ty = FD->getType();
52050e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
52150e0372fSArtem Dergachev         if (!ProtoType->isNothrow(getContext()))
52250e0372fSArtem Dergachev           State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
52350e0372fSArtem Dergachev     }
52450e0372fSArtem Dergachev 
52550e0372fSArtem Dergachev     ValueBldr.generateNode(CNE, I,
52650e0372fSArtem Dergachev                            setCXXNewAllocatorValue(State, CNE, LCtx, RetVal));
52755796302SArtem Dergachev   }
52855796302SArtem Dergachev 
52913b2026bSArtem Dergachev   ExplodedNodeSet DstPostPostCallCallback;
53013b2026bSArtem Dergachev   getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
53113b2026bSArtem Dergachev                                              DstPostValue, *Call, *this);
53213b2026bSArtem Dergachev   for (auto I : DstPostPostCallCallback) {
53313b2026bSArtem Dergachev     getCheckerManager().runCheckersForNewAllocator(
53413b2026bSArtem Dergachev         CNE, getCXXNewAllocatorValue(I->getState(), CNE, LCtx), Dst, I, *this);
53513b2026bSArtem Dergachev   }
5368b808d64SJordan Rose }
5378b808d64SJordan Rose 
53810e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
53910e25031STed Kremenek                                    ExplodedNodeSet &Dst) {
540a4ee064cSJordan Rose   // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
541a4ee064cSJordan Rose   // Also, we need to decide how allocators actually work -- they're not
542a4ee064cSJordan Rose   // really part of the CXXNewExpr because they happen BEFORE the
543a4ee064cSJordan Rose   // CXXConstructExpr subexpression. See PR12014 for some discussion.
54410e25031STed Kremenek 
545d94854a4STed Kremenek   unsigned blockCount = currBldrCtx->blockCount();
546d519cae8STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
54755796302SArtem Dergachev   SVal symVal = UnknownVal();
5488b662704SAnton Yartsev   FunctionDecl *FD = CNE->getOperatorNew();
54910e25031STed Kremenek 
5501084de52SArtem Dergachev   bool IsStandardGlobalOpNewFunction =
5511084de52SArtem Dergachev       FD->isReplaceableGlobalAllocationFunction();
5528b662704SAnton Yartsev 
55355796302SArtem Dergachev   ProgramStateRef State = Pred->getState();
55455796302SArtem Dergachev 
55555796302SArtem Dergachev   // Retrieve the stored operator new() return value.
55655796302SArtem Dergachev   if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
55755796302SArtem Dergachev     symVal = getCXXNewAllocatorValue(State, CNE, LCtx);
55855796302SArtem Dergachev     State = clearCXXNewAllocatorValue(State, CNE, LCtx);
55955796302SArtem Dergachev   }
56055796302SArtem Dergachev 
5618b662704SAnton Yartsev   // We assume all standard global 'operator new' functions allocate memory in
5628b662704SAnton Yartsev   // heap. We realize this is an approximation that might not correctly model
5638b662704SAnton Yartsev   // a custom global allocator.
56455796302SArtem Dergachev   if (symVal.isUnknown()) {
5658b662704SAnton Yartsev     if (IsStandardGlobalOpNewFunction)
5668b662704SAnton Yartsev       symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
5678b662704SAnton Yartsev     else
5680dbb783cSCraig Topper       symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
5698b662704SAnton Yartsev                                             blockCount);
57055796302SArtem Dergachev   }
5718b662704SAnton Yartsev 
572fcd016e5SJordan Rose   CallEventManager &CEMgr = getStateManager().getCallEventManager();
573fcd016e5SJordan Rose   CallEventRef<CXXAllocatorCall> Call =
574fcd016e5SJordan Rose     CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
575fcd016e5SJordan Rose 
57655796302SArtem Dergachev   if (!AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
577a4ee064cSJordan Rose     // Invalidate placement args.
578fcd016e5SJordan Rose     // FIXME: Once we figure out how we want allocators to work,
579fcd016e5SJordan Rose     // we should be using the usual pre-/(default-)eval-/post-call checks here.
580fcd016e5SJordan Rose     State = Call->invalidateRegions(blockCount);
581b8cb8359SJordan Rose     if (!State)
582b8cb8359SJordan Rose       return;
583a4ee064cSJordan Rose 
584fce1b03eSPavel Labath     // If this allocation function is not declared as non-throwing, failures
58550e0372fSArtem Dergachev     // /must/ be signalled by exceptions, and thus the return value will never
58650e0372fSArtem Dergachev     // be NULL. -fno-exceptions does not influence this semantics.
587fce1b03eSPavel Labath     // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
588fce1b03eSPavel Labath     // where new can return NULL. If we end up supporting that option, we can
589fce1b03eSPavel Labath     // consider adding a check for it here.
5903957fd58SJordan Rose     // C++11 [basic.stc.dynamic.allocation]p3.
591fce1b03eSPavel Labath     if (FD) {
5923957fd58SJordan Rose       QualType Ty = FD->getType();
59350e0372fSArtem Dergachev       if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
5943957fd58SJordan Rose         if (!ProtoType->isNothrow(getContext()))
59555796302SArtem Dergachev           if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
59655796302SArtem Dergachev             State = State->assume(*dSymVal, true);
5973957fd58SJordan Rose     }
59850e0372fSArtem Dergachev   }
5993957fd58SJordan Rose 
600b8cb8359SJordan Rose   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
601b8cb8359SJordan Rose 
602beba5307SArtem Dergachev   SVal Result = symVal;
603beba5307SArtem Dergachev 
60410e25031STed Kremenek   if (CNE->isArray()) {
60510e25031STed Kremenek     // FIXME: allocating an array requires simulating the constructors.
60610e25031STed Kremenek     // For now, just return a symbolicated region.
6071084de52SArtem Dergachev     if (const SubRegion *NewReg =
6081084de52SArtem Dergachev             dyn_cast_or_null<SubRegion>(symVal.getAsRegion())) {
609a4ee064cSJordan Rose       QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
610a4ee064cSJordan Rose       const ElementRegion *EleReg =
611a4ee064cSJordan Rose           getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
612beba5307SArtem Dergachev       Result = loc::MemRegionVal(EleReg);
613beba5307SArtem Dergachev     }
614beba5307SArtem Dergachev     State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
61544320367SJordan Rose     Bldr.generateNode(CNE, Pred, State);
61610e25031STed Kremenek     return;
61710e25031STed Kremenek   }
61810e25031STed Kremenek 
619a4ee064cSJordan Rose   // FIXME: Once we have proper support for CXXConstructExprs inside
620a4ee064cSJordan Rose   // CXXNewExpr, we need to make sure that the constructed object is not
621a4ee064cSJordan Rose   // immediately invalidated here. (The placement call should happen before
622a4ee064cSJordan Rose   // the constructor call anyway.)
62344320367SJordan Rose   if (FD && FD->isReservedGlobalPlacementOperator()) {
62444320367SJordan Rose     // Non-array placement new should always return the placement location.
62544320367SJordan Rose     SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
626b8cb8359SJordan Rose     Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
627aaf83184SJordan Rose                                   CNE->getPlacementArg(0)->getType());
62844320367SJordan Rose   }
62944320367SJordan Rose 
630b8cb8359SJordan Rose   // Bind the address of the object, then check to see if we cached out.
631b8cb8359SJordan Rose   State = State->BindExpr(CNE, LCtx, Result);
6328f6b4b04SJordan Rose   ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
6338f6b4b04SJordan Rose   if (!NewN)
6348f6b4b04SJordan Rose     return;
6353503cb35SJordan Rose 
6365089f3b3SJordan Rose   // If the type is not a record, we won't have a CXXConstructExpr as an
6375089f3b3SJordan Rose   // initializer. Copy the value over.
6385089f3b3SJordan Rose   if (const Expr *Init = CNE->getInitializer()) {
6395089f3b3SJordan Rose     if (!isa<CXXConstructExpr>(Init)) {
6403503cb35SJordan Rose       assert(Bldr.getResults().size() == 1);
6418f6b4b04SJordan Rose       Bldr.takeNodes(NewN);
642b8cb8359SJordan Rose       evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
643b8cb8359SJordan Rose                /*FirstInit=*/IsStandardGlobalOpNewFunction);
6443503cb35SJordan Rose     }
6453503cb35SJordan Rose   }
64610e25031STed Kremenek }
64710e25031STed Kremenek 
64810e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
64910e25031STed Kremenek                                     ExplodedNode *Pred, ExplodedNodeSet &Dst) {
650d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
65164aea652SErik Verbruggen   ProgramStateRef state = Pred->getState();
65264aea652SErik Verbruggen   Bldr.generateNode(CDE, Pred, state);
65310e25031STed Kremenek }
65410e25031STed Kremenek 
6558fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
6568fdb59f9STed Kremenek                                    ExplodedNode *Pred,
6578fdb59f9STed Kremenek                                    ExplodedNodeSet &Dst) {
6588fdb59f9STed Kremenek   const VarDecl *VD = CS->getExceptionDecl();
659b1279b5cSTed Kremenek   if (!VD) {
660b1279b5cSTed Kremenek     Dst.Add(Pred);
661b1279b5cSTed Kremenek     return;
662b1279b5cSTed Kremenek   }
663b1279b5cSTed Kremenek 
6648fdb59f9STed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
665d227833cSTed Kremenek   SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
666d94854a4STed Kremenek                                         currBldrCtx->blockCount());
6678fdb59f9STed Kremenek   ProgramStateRef state = Pred->getState();
668b570195cSAnna Zaks   state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
6698fdb59f9STed Kremenek 
670d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
6718fdb59f9STed Kremenek   Bldr.generateNode(CS, Pred, state);
6728fdb59f9STed Kremenek }
6738fdb59f9STed Kremenek 
67410e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
67510e25031STed Kremenek                                     ExplodedNodeSet &Dst) {
676d94854a4STed Kremenek   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
677e594034fSAnna Zaks 
67810e25031STed Kremenek   // Get the this object region from StoreManager.
679632e3b7eSTed Kremenek   const LocationContext *LCtx = Pred->getLocationContext();
68010e25031STed Kremenek   const MemRegion *R =
68110e25031STed Kremenek     svalBuilder.getRegionManager().getCXXThisRegion(
68210e25031STed Kremenek                                   getContext().getCanonicalType(TE->getType()),
683632e3b7eSTed Kremenek                                                     LCtx);
68410e25031STed Kremenek 
68549b1e38eSTed Kremenek   ProgramStateRef state = Pred->getState();
68610e25031STed Kremenek   SVal V = state->getSVal(loc::MemRegionVal(R));
687632e3b7eSTed Kremenek   Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
68810e25031STed Kremenek }
68915843343SGabor Horvath 
69015843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
69115843343SGabor Horvath                                  ExplodedNodeSet &Dst) {
69215843343SGabor Horvath   const LocationContext *LocCtxt = Pred->getLocationContext();
69315843343SGabor Horvath 
69415843343SGabor Horvath   // Get the region of the lambda itself.
69515843343SGabor Horvath   const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
69615843343SGabor Horvath       LE, LocCtxt);
69715843343SGabor Horvath   SVal V = loc::MemRegionVal(R);
69815843343SGabor Horvath 
69915843343SGabor Horvath   ProgramStateRef State = Pred->getState();
70015843343SGabor Horvath 
70115843343SGabor Horvath   // If we created a new MemRegion for the lambda, we should explicitly bind
70215843343SGabor Horvath   // the captures.
70315843343SGabor Horvath   CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
70415843343SGabor Horvath   for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
70515843343SGabor Horvath                                                e = LE->capture_init_end();
70615843343SGabor Horvath        i != e; ++i, ++CurField) {
7077bdca8b2SDevin Coughlin     FieldDecl *FieldForCapture = *CurField;
7087bdca8b2SDevin Coughlin     SVal FieldLoc = State->getLValue(FieldForCapture, V);
7097bdca8b2SDevin Coughlin 
7107bdca8b2SDevin Coughlin     SVal InitVal;
7117bdca8b2SDevin Coughlin     if (!FieldForCapture->hasCapturedVLAType()) {
7127bdca8b2SDevin Coughlin       Expr *InitExpr = *i;
7137bdca8b2SDevin Coughlin       assert(InitExpr && "Capture missing initialization expression");
7147bdca8b2SDevin Coughlin       InitVal = State->getSVal(InitExpr, LocCtxt);
7157bdca8b2SDevin Coughlin     } else {
7167bdca8b2SDevin Coughlin       // The field stores the length of a captured variable-length array.
7177bdca8b2SDevin Coughlin       // These captures don't have initialization expressions; instead we
7187bdca8b2SDevin Coughlin       // get the length from the VLAType size expression.
7197bdca8b2SDevin Coughlin       Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
7207bdca8b2SDevin Coughlin       InitVal = State->getSVal(SizeExpr, LocCtxt);
7217bdca8b2SDevin Coughlin     }
7227bdca8b2SDevin Coughlin 
723b570195cSAnna Zaks     State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
72415843343SGabor Horvath   }
72515843343SGabor Horvath 
72615843343SGabor Horvath   // Decay the Loc into an RValue, because there might be a
72715843343SGabor Horvath   // MaterializeTemporaryExpr node above this one which expects the bound value
72815843343SGabor Horvath   // to be an RValue.
72915843343SGabor Horvath   SVal LambdaRVal = State->getSVal(R);
73015843343SGabor Horvath 
73115843343SGabor Horvath   ExplodedNodeSet Tmp;
73215843343SGabor Horvath   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
73315843343SGabor Horvath   // FIXME: is this the right program point kind?
73415843343SGabor Horvath   Bldr.generateNode(LE, Pred,
73515843343SGabor Horvath                     State->BindExpr(LE, LocCtxt, LambdaRVal),
73615843343SGabor Horvath                     nullptr, ProgramPoint::PostLValueKind);
73715843343SGabor Horvath 
73815843343SGabor Horvath   // FIXME: Move all post/pre visits to ::Visit().
73915843343SGabor Horvath   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
74015843343SGabor Horvath }
741