From fc1e6c79bbfe0e9a58bb792996d51f42e36e3d6a Mon Sep 17 00:00:00 2001 From: John McCall Date: Sat, 18 Sep 2010 00:58:34 +0000 Subject: [PATCH] Fix a bug with binding l-values to elided temporaries, and leave a couple helpful asserts behind. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114250 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 10 +++++++--- lib/CodeGen/CGExprCXX.cpp | 8 +++++--- test/CodeGenCXX/temporaries.cpp | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 9c1a3cf249..924ca3ec4b 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -1964,6 +1964,8 @@ LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { } LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { + assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() + && "binding l-value to type which needs a temporary"); AggValueSlot Slot = CreateAggTemp(E->getType(), "tmp"); EmitCXXConstructExpr(E, Slot); return MakeAddrLValue(Slot.getAddr(), E->getType()); @@ -1976,9 +1978,11 @@ CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { LValue CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { - LValue LV = EmitLValue(E->getSubExpr()); - EmitCXXTemporary(E->getTemporary(), LV.getAddress()); - return LV; + AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); + Slot.setLifetimeExternallyManaged(); + EmitAggExpr(E->getSubExpr(), Slot); + EmitCXXTemporary(E->getTemporary(), Slot.getAddr()); + return MakeAddrLValue(Slot.getAddr(), E->getType()); } LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index 4dbfa7ee4f..aadceff8be 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -280,10 +280,12 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, if (CD->isTrivial() && CD->isDefaultConstructor()) return; - // Code gen optimization to eliminate copy constructor and return - // its first argument instead, if in fact that argument is a temporary - // object. + // Elide the constructor if we're constructing from a temporary. + // The temporary check is required because Sema sets this on NRVO + // returns. if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { + assert(getContext().hasSameUnqualifiedType(E->getType(), + E->getArg(0)->getType())); if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { EmitAggExpr(E->getArg(0), Dest); return; diff --git a/test/CodeGenCXX/temporaries.cpp b/test/CodeGenCXX/temporaries.cpp index 7996ed656d..3bf1dbd9f4 100644 --- a/test/CodeGenCXX/temporaries.cpp +++ b/test/CodeGenCXX/temporaries.cpp @@ -340,7 +340,11 @@ namespace PR7556 { } namespace Elision { - struct A { A(); A(const A &); ~A(); void *p; }; + struct A { + A(); A(const A &); ~A(); + void *p; + void foo() const; + }; void foo(); A fooA(); @@ -475,4 +479,17 @@ namespace Elision { // CHECK: call void @_ZN7Elision1AD1Ev([[A]]* [[X]]) } + + // Reduced from webkit. + // CHECK: define void @_ZN7Elision5test6EPKNS_1CE([[C:%.*]]* + struct C { operator A() const; }; + void test6(const C *x) { + // CHECK: [[T0:%.*]] = alloca [[A]], align 8 + // CHECK: [[X:%.*]] = load [[C]]** {{%.*}}, align 8 + // CHECK-NEXT: call void @_ZNK7Elision1CcvNS_1AEEv([[A]]* sret [[T0]], [[C]]* [[X]]) + // CHECK-NEXT: call void @_ZNK7Elision1A3fooEv([[A]]* [[T0]]) + // CHECK-NEXT: call void @_ZN7Elision1AD1Ev([[A]]* [[T0]]) + // CHECK-NEXT: ret void + A(*x).foo(); + } }