Skip to content

Commit

Permalink
Fix a bug with binding l-values to elided temporaries, and leave a co…
Browse files Browse the repository at this point in the history
…uple

helpful asserts behind.

llvm-svn: 114250
  • Loading branch information
rjmccall committed Sep 18, 2010
1 parent 79d6af3 commit 8ea46b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
10 changes: 7 additions & 3 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/CodeGen/CGExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion clang/test/CodeGenCXX/temporaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 8ea46b6

Please sign in to comment.