2012-08-01 13:04:58 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm %s -o - | FileCheck %s
|
2009-10-17 03:20:59 +08:00
|
|
|
|
|
|
|
struct A {
|
2012-08-01 13:04:58 +08:00
|
|
|
A(const A&);
|
|
|
|
A();
|
|
|
|
~A();
|
2009-10-17 03:20:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct B : public A {
|
2012-08-01 13:04:58 +08:00
|
|
|
B();
|
|
|
|
B(const B& Other);
|
|
|
|
~B();
|
Rework our handling of copy construction of temporaries, which was a
poor (and wrong) approximation of the actual rules governing when to
build a copy and when it can be elided.
The correct implementation is actually simpler than the
approximation. When we only enumerate constructors as part of
initialization (e.g., for direct initialization or when we're copying
from a class type or one of its derived classes), we don't create a
copy. When we enumerate all conversion functions, we do create a
copy. Before, we created some extra copies and missed some
others. The new test copy-initialization.cpp shows a case where we
missed creating a (required, non-elidable) copy as part of a
user-defined conversion, which resulted in a miscompile. This commit
also fixes PR6757, where the missing copy made us reject well-formed
code in the ternary operator.
This commit also cleans up our handling of copy elision in the case
where we create an extra copy of a temporary object, which became
necessary now that we produce the right copies. The code that seeks to
find the temporary object being copied has moved into
Expr::getTemporaryObject(); it used to have two different
not-quite-the-same implementations, one in Sema and one in CodeGen.
Note that we still do not attempt to perform the named return value
optimization, so we miss copy elisions for return values and throw
expressions.
llvm-svn: 100196
2010-04-03 02:24:57 +08:00
|
|
|
};
|
2009-10-17 03:20:59 +08:00
|
|
|
|
|
|
|
struct C : public B {
|
2012-08-01 13:04:58 +08:00
|
|
|
C();
|
|
|
|
C(const C& Other);
|
|
|
|
~C();
|
2009-10-17 03:20:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct X {
|
2012-08-01 13:04:58 +08:00
|
|
|
operator B&();
|
|
|
|
operator C&();
|
|
|
|
X(const X&);
|
|
|
|
X();
|
|
|
|
~X();
|
|
|
|
B b;
|
|
|
|
C c;
|
2009-10-17 03:20:59 +08:00
|
|
|
};
|
|
|
|
|
2012-08-01 13:04:58 +08:00
|
|
|
void test0_helper(A);
|
|
|
|
void test0(X x) {
|
|
|
|
test0_helper(x);
|
|
|
|
// CHECK: define void @_Z5test01X(
|
|
|
|
// CHECK: [[TMP:%.*]] = alloca [[A:%.*]], align
|
|
|
|
// CHECK-NEXT: [[T0:%.*]] = call [[B:%.*]]* @_ZN1XcvR1BEv(
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = bitcast [[B]]* [[T0]] to [[A]]*
|
|
|
|
// CHECK-NEXT: call void @_ZN1AC1ERKS_([[A]]* [[TMP]], [[A]]* [[T1]])
|
|
|
|
// CHECK-NEXT: call void @_Z12test0_helper1A([[A]]* [[TMP]])
|
|
|
|
// CHECK-NEXT: call void @_ZN1AD1Ev([[A]]* [[TMP]])
|
|
|
|
// CHECK-NEXT: ret void
|
2009-10-17 03:20:59 +08:00
|
|
|
}
|
|
|
|
|
2009-10-20 03:18:20 +08:00
|
|
|
struct Base;
|
|
|
|
|
|
|
|
struct Root {
|
2012-08-01 13:04:58 +08:00
|
|
|
operator Base&();
|
2009-10-20 03:18:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Derived;
|
|
|
|
|
|
|
|
struct Base : Root {
|
2012-08-01 13:04:58 +08:00
|
|
|
Base(const Base &);
|
|
|
|
Base();
|
|
|
|
operator Derived &();
|
2009-10-20 03:18:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Derived : Base {
|
|
|
|
};
|
|
|
|
|
2012-08-01 13:04:58 +08:00
|
|
|
void test1_helper(Base);
|
|
|
|
void test1(Derived bb) {
|
|
|
|
// CHECK: define void @_Z5test17Derived(
|
|
|
|
// CHECK-NOT: call {{.*}} @_ZN4BasecvR7DerivedEv(
|
|
|
|
// CHECK: call void @_ZN4BaseC1ERKS_(
|
|
|
|
// CHECK-NOT: call {{.*}} @_ZN4BasecvR7DerivedEv(
|
|
|
|
// CHECK: call void @_Z12test1_helper4Base(
|
|
|
|
test1_helper(bb);
|
2009-10-20 03:18:20 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 13:04:58 +08:00
|
|
|
// Don't crash after devirtualizing a derived-to-base conversion
|
|
|
|
// to an empty base allocated at offset zero.
|
|
|
|
// rdar://problem/11993704
|
|
|
|
class Test2a {};
|
|
|
|
class Test2b final : public virtual Test2a {};
|
|
|
|
void test2(Test2b &x) {
|
|
|
|
Test2a &y = x;
|
|
|
|
// CHECK: define void @_Z5test2R6Test2b(
|
|
|
|
// CHECK: [[X:%.*]] = alloca [[B:%.*]]*, align 8
|
|
|
|
// CHECK-NEXT: [[Y:%.*]] = alloca [[A:%.*]]*, align 8
|
|
|
|
// CHECK-NEXT: store [[B]]* {{%.*}}, [[B]]** [[X]], align 8
|
|
|
|
// CHECK-NEXT: [[T0:%.*]] = load [[B]]** [[X]], align 8
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = bitcast [[B]]* [[T0]] to [[A]]*
|
|
|
|
// CHECK-NEXT: store [[A]]* [[T1]], [[A]]** [[Y]], align 8
|
|
|
|
// CHECK-NEXT: ret void
|
|
|
|
}
|