@@ -274,9 +274,9 @@ void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
274274 llvm_unreachable (" bad evaluation kind" );
275275}
276276
277- static void
278- pushTemporaryCleanup (CodeGenFunction &CGF, const MaterializeTemporaryExpr *M ,
279- const Expr *E, Address ReferenceTemporary) {
277+ void CodeGenFunction::pushTemporaryCleanup ( const MaterializeTemporaryExpr *M,
278+ const Expr *E ,
279+ Address ReferenceTemporary) {
280280 // Objective-C++ ARC:
281281 // If we are binding a reference to a temporary that has ownership, we
282282 // need to perform retain/release operations on the temporary.
@@ -311,9 +311,9 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
311311 CleanupKind CleanupKind;
312312 if (Lifetime == Qualifiers::OCL_Strong) {
313313 const ValueDecl *VD = M->getExtendingDecl ();
314- bool Precise =
315- VD && isa<VarDecl>(VD) && VD->hasAttr <ObjCPreciseLifetimeAttr>();
316- CleanupKind = CGF. getARCCleanupKind ();
314+ bool Precise = isa_and_nonnull<VarDecl>(VD) &&
315+ VD->hasAttr <ObjCPreciseLifetimeAttr>();
316+ CleanupKind = getARCCleanupKind ();
317317 Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise
318318 : &CodeGenFunction::destroyARCStrongImprecise;
319319 } else {
@@ -323,13 +323,12 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
323323 Destroy = &CodeGenFunction::destroyARCWeak;
324324 }
325325 if (Duration == SD_FullExpression)
326- CGF.pushDestroy (CleanupKind, ReferenceTemporary,
327- M->getType (), *Destroy,
328- CleanupKind & EHCleanup);
326+ pushDestroy (CleanupKind, ReferenceTemporary, M->getType (), *Destroy,
327+ CleanupKind & EHCleanup);
329328 else
330- CGF. pushLifetimeExtendedDestroy (CleanupKind, ReferenceTemporary,
331- M->getType (),
332- *Destroy, CleanupKind & EHCleanup);
329+ pushLifetimeExtendedDestroy (CleanupKind, ReferenceTemporary,
330+ M->getType (), *Destroy ,
331+ CleanupKind & EHCleanup);
333332 return ;
334333
335334 case SD_Dynamic:
@@ -358,32 +357,31 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
358357 llvm::FunctionCallee CleanupFn;
359358 llvm::Constant *CleanupArg;
360359 if (E->getType ()->isArrayType ()) {
361- CleanupFn = CodeGenFunction (CGF. CGM ).generateDestroyHelper (
362- ReferenceTemporary, E->getType (),
363- CodeGenFunction::destroyCXXObject, CGF. getLangOpts ().Exceptions ,
360+ CleanupFn = CodeGenFunction (CGM).generateDestroyHelper (
361+ ReferenceTemporary, E->getType (), CodeGenFunction::destroyCXXObject,
362+ getLangOpts ().Exceptions ,
364363 dyn_cast_or_null<VarDecl>(M->getExtendingDecl ()));
365- CleanupArg = llvm::Constant::getNullValue (CGF. Int8PtrTy );
364+ CleanupArg = llvm::Constant::getNullValue (Int8PtrTy);
366365 } else {
367- CleanupFn = CGF. CGM .getAddrAndTypeOfCXXStructor (
366+ CleanupFn = CGM.getAddrAndTypeOfCXXStructor (
368367 GlobalDecl (ReferenceTemporaryDtor, Dtor_Complete));
369- CleanupArg = cast<llvm::Constant>(ReferenceTemporary.emitRawPointer (CGF));
368+ CleanupArg =
369+ cast<llvm::Constant>(ReferenceTemporary.emitRawPointer (*this ));
370370 }
371- CGF. CGM .getCXXABI ().registerGlobalDtor (
372- CGF , *cast<VarDecl>(M->getExtendingDecl ()), CleanupFn, CleanupArg);
371+ CGM.getCXXABI ().registerGlobalDtor (
372+ * this , *cast<VarDecl>(M->getExtendingDecl ()), CleanupFn, CleanupArg);
373373 break ;
374374 }
375375
376376 case SD_FullExpression:
377- CGF.pushDestroy (NormalAndEHCleanup, ReferenceTemporary, E->getType (),
378- CodeGenFunction::destroyCXXObject,
379- CGF.getLangOpts ().Exceptions );
377+ pushDestroy (NormalAndEHCleanup, ReferenceTemporary, E->getType (),
378+ CodeGenFunction::destroyCXXObject, getLangOpts ().Exceptions );
380379 break ;
381380
382381 case SD_Automatic:
383- CGF.pushLifetimeExtendedDestroy (NormalAndEHCleanup,
384- ReferenceTemporary, E->getType (),
385- CodeGenFunction::destroyCXXObject,
386- CGF.getLangOpts ().Exceptions );
382+ pushLifetimeExtendedDestroy (NormalAndEHCleanup, ReferenceTemporary,
383+ E->getType (), CodeGenFunction::destroyCXXObject,
384+ getLangOpts ().Exceptions );
387385 break ;
388386
389387 case SD_Dynamic:
@@ -490,7 +488,7 @@ EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
490488 }
491489 }
492490
493- pushTemporaryCleanup (* this , M, E, Object);
491+ pushTemporaryCleanup (M, E, Object);
494492 return RefTempDst;
495493 }
496494
@@ -579,7 +577,13 @@ EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
579577 }
580578 EmitAnyExprToMem (E, Object, Qualifiers (), /* IsInit*/ true );
581579 }
582- pushTemporaryCleanup (*this , M, E, Object);
580+
581+ // If this temporary extended by for-range variable, delay to emitting
582+ // cleanup.
583+ if (CurLexicalScope && CurLexicalScope->isExtendedByForRangeVar (M))
584+ CurLexicalScope->addForRangeInitTemp (M, Object);
585+ else
586+ pushTemporaryCleanup (M, E, Object);
583587
584588 // Perform derived-to-base casts and/or field accesses, to get from the
585589 // temporary object we created (and, potentially, for which we extended
0 commit comments