From 972edf0534d8a50f87fac1d0ff34eb22f593df11 Mon Sep 17 00:00:00 2001 From: Sebastian Redl Date: Sun, 19 Feb 2012 16:03:09 +0000 Subject: [PATCH] Make heap-allocation of std::initializer_list 'work'. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150931 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGClass.cpp | 2 +- lib/CodeGen/CGDecl.cpp | 2 +- lib/CodeGen/CGExprAgg.cpp | 12 +++++------ lib/CodeGen/CGExprCXX.cpp | 2 ++ lib/CodeGen/CodeGenFunction.h | 2 +- .../cxx0x-initializer-stdinitializerlist.cpp | 20 +++++++++++++++++++ 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/CodeGen/CGClass.cpp b/lib/CodeGen/CGClass.cpp index f269b675a0..2cea434f62 100644 --- a/lib/CodeGen/CGClass.cpp +++ b/lib/CodeGen/CGClass.cpp @@ -456,7 +456,7 @@ static void EmitAggMemberInitializer(CodeGenFunction &CGF, // Now, outside of the initializer cleanup scope, destroy the backing array // for a std::initializer_list member. - CGF.MaybeEmitStdInitializerListCleanup(LV, Init); + CGF.MaybeEmitStdInitializerListCleanup(LV.getAddress(), Init); return; } diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 83d74106f3..fcea927c63 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -1093,7 +1093,7 @@ void CodeGenFunction::EmitExprAsInit(const Expr *init, AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased)); - MaybeEmitStdInitializerListCleanup(lvalue, init); + MaybeEmitStdInitializerListCleanup(lvalue.getAddress(), init); } } diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp index 13f5bc47d5..afe70a5dda 100644 --- a/lib/CodeGen/CGExprAgg.cpp +++ b/lib/CodeGen/CGExprAgg.cpp @@ -1263,19 +1263,17 @@ void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, Alignment, isVolatile); } -void CodeGenFunction::MaybeEmitStdInitializerListCleanup(LValue lvalue, - const Expr *init) { +void CodeGenFunction::MaybeEmitStdInitializerListCleanup(llvm::Value *loc, + const Expr *init) { const ExprWithCleanups *cleanups = dyn_cast(init); - if (!cleanups) - return; // Nothing interesting here. - init = cleanups->getSubExpr(); + if (cleanups) + init = cleanups->getSubExpr(); if (isa(init) && cast(init)->initializesStdInitializerList()) { // We initialized this std::initializer_list with an initializer list. // A backing array was created. Push a cleanup for it. - EmitStdInitializerListCleanup(lvalue.getAddress(), - cast(init)); + EmitStdInitializerListCleanup(loc, cast(init)); } } diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index 548bbc9e61..7c7bdd9eda 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -762,6 +762,8 @@ static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased); CGF.EmitAggExpr(Init, Slot); + + CGF.MaybeEmitStdInitializerListCleanup(NewPtr, Init); } } diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 95420e5344..6327fa2225 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -1811,7 +1811,7 @@ public: llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E); llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE); - void MaybeEmitStdInitializerListCleanup(LValue lvalue, const Expr *init); + void MaybeEmitStdInitializerListCleanup(llvm::Value *loc, const Expr *init); void EmitStdInitializerListCleanup(llvm::Value *loc, const InitListExpr *init); diff --git a/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp b/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp index 55e3992238..2d7ff46ef7 100644 --- a/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp +++ b/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp @@ -195,3 +195,23 @@ haslist2::haslist2() // CHECK: call void @_ZN10destroyme2D1Ev // CHECK: call void @_ZN10destroyme1D1Ev } + +void fn10() { + // CHECK: define void @_Z4fn10v + // CHECK: alloca [3 x i32] + // CHECK: call noalias i8* @_Znwm + // CHECK: store i32 1 + // CHECK: store i32 2 + // CHECK: store i32 3 + // CHECK: store i32* + // CHECK: store i{{32|64}} 3 + (void) new std::initializer_list {1, 2, 3}; +} + +void fn11() { + // CHECK: define void @_Z4fn11v + (void) new std::initializer_list {destroyme1(), destroyme1()}; + // CHECK: call void @_ZN10destroyme1D1Ev + destroyme2 dm2; + // CHECK: call void @_ZN10destroyme2D1Ev +} -- 2.40.0