@@ -268,9 +268,9 @@ void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
268268 llvm_unreachable (" bad evaluation kind" );
269269}
270270
271- static void
272- pushTemporaryCleanup (CodeGenFunction &CGF, const MaterializeTemporaryExpr *M ,
273- const Expr *E, Address ReferenceTemporary) {
271+ void CodeGenFunction::pushTemporaryCleanup ( const MaterializeTemporaryExpr *M,
272+ const Expr *E ,
273+ Address ReferenceTemporary) {
274274 // Objective-C++ ARC:
275275 // If we are binding a reference to a temporary that has ownership, we
276276 // need to perform retain/release operations on the temporary.
@@ -305,9 +305,9 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
305305 CleanupKind CleanupKind;
306306 if (Lifetime == Qualifiers::OCL_Strong) {
307307 const ValueDecl *VD = M->getExtendingDecl ();
308- bool Precise =
309- VD && isa<VarDecl>(VD) && VD->hasAttr <ObjCPreciseLifetimeAttr>();
310- CleanupKind = CGF. getARCCleanupKind ();
308+ bool Precise = isa_and_nonnull<VarDecl>(VD) &&
309+ VD->hasAttr <ObjCPreciseLifetimeAttr>();
310+ CleanupKind = getARCCleanupKind ();
311311 Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise
312312 : &CodeGenFunction::destroyARCStrongImprecise;
313313 } else {
@@ -317,13 +317,12 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
317317 Destroy = &CodeGenFunction::destroyARCWeak;
318318 }
319319 if (Duration == SD_FullExpression)
320- CGF.pushDestroy (CleanupKind, ReferenceTemporary,
321- M->getType (), *Destroy,
322- CleanupKind & EHCleanup);
320+ pushDestroy (CleanupKind, ReferenceTemporary, M->getType (), *Destroy,
321+ CleanupKind & EHCleanup);
323322 else
324- CGF. pushLifetimeExtendedDestroy (CleanupKind, ReferenceTemporary,
325- M->getType (),
326- *Destroy, CleanupKind & EHCleanup);
323+ pushLifetimeExtendedDestroy (CleanupKind, ReferenceTemporary,
324+ M->getType (), *Destroy ,
325+ CleanupKind & EHCleanup);
327326 return ;
328327
329328 case SD_Dynamic:
@@ -352,32 +351,31 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
352351 llvm::FunctionCallee CleanupFn;
353352 llvm::Constant *CleanupArg;
354353 if (E->getType ()->isArrayType ()) {
355- CleanupFn = CodeGenFunction (CGF. CGM ).generateDestroyHelper (
356- ReferenceTemporary, E->getType (),
357- CodeGenFunction::destroyCXXObject, CGF. getLangOpts ().Exceptions ,
354+ CleanupFn = CodeGenFunction (CGM).generateDestroyHelper (
355+ ReferenceTemporary, E->getType (), CodeGenFunction::destroyCXXObject,
356+ getLangOpts ().Exceptions ,
358357 dyn_cast_or_null<VarDecl>(M->getExtendingDecl ()));
359- CleanupArg = llvm::Constant::getNullValue (CGF. Int8PtrTy );
358+ CleanupArg = llvm::Constant::getNullValue (Int8PtrTy);
360359 } else {
361- CleanupFn = CGF. CGM .getAddrAndTypeOfCXXStructor (
360+ CleanupFn = CGM.getAddrAndTypeOfCXXStructor (
362361 GlobalDecl (ReferenceTemporaryDtor, Dtor_Complete));
363- CleanupArg = cast<llvm::Constant>(ReferenceTemporary.emitRawPointer (CGF));
362+ CleanupArg =
363+ cast<llvm::Constant>(ReferenceTemporary.emitRawPointer (*this ));
364364 }
365- CGF. CGM .getCXXABI ().registerGlobalDtor (
366- CGF , *cast<VarDecl>(M->getExtendingDecl ()), CleanupFn, CleanupArg);
365+ CGM.getCXXABI ().registerGlobalDtor (
366+ * this , *cast<VarDecl>(M->getExtendingDecl ()), CleanupFn, CleanupArg);
367367 break ;
368368 }
369369
370370 case SD_FullExpression:
371- CGF.pushDestroy (NormalAndEHCleanup, ReferenceTemporary, E->getType (),
372- CodeGenFunction::destroyCXXObject,
373- CGF.getLangOpts ().Exceptions );
371+ pushDestroy (NormalAndEHCleanup, ReferenceTemporary, E->getType (),
372+ CodeGenFunction::destroyCXXObject, getLangOpts ().Exceptions );
374373 break ;
375374
376375 case SD_Automatic:
377- CGF.pushLifetimeExtendedDestroy (NormalAndEHCleanup,
378- ReferenceTemporary, E->getType (),
379- CodeGenFunction::destroyCXXObject,
380- CGF.getLangOpts ().Exceptions );
376+ pushLifetimeExtendedDestroy (NormalAndEHCleanup, ReferenceTemporary,
377+ E->getType (), CodeGenFunction::destroyCXXObject,
378+ getLangOpts ().Exceptions );
381379 break ;
382380
383381 case SD_Dynamic:
@@ -484,7 +482,7 @@ EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
484482 }
485483 }
486484
487- pushTemporaryCleanup (* this , M, E, Object);
485+ pushTemporaryCleanup (M, E, Object);
488486 return RefTempDst;
489487 }
490488
@@ -573,7 +571,13 @@ EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
573571 }
574572 EmitAnyExprToMem (E, Object, Qualifiers (), /* IsInit*/ true );
575573 }
576- pushTemporaryCleanup (*this , M, E, Object);
574+
575+ // If this temporary extended by for-range variable, delay to emitting
576+ // cleanup.
577+ if (!CurLexicalScope->isExtendedByForRangeVar (M))
578+ CurLexicalScope->addForRangeInitTemp (M, Object);
579+ else
580+ pushTemporaryCleanup (M, E, Object);
577581
578582 // Perform derived-to-base casts and/or field accesses, to get from the
579583 // temporary object we created (and, potentially, for which we extended
0 commit comments