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.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100196 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/CodeGenCXX/copy-initialization.cpp b/test/CodeGenCXX/copy-initialization.cpp
new file mode 100644
index 0000000..62b9f26
--- /dev/null
+++ b/test/CodeGenCXX/copy-initialization.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
+
+struct Foo {
+  Foo();
+  Foo(const Foo&);
+};
+
+struct Bar {
+  Bar();
+  operator const Foo&() const;
+};
+
+void f(Foo);
+
+// CHECK: define void @_Z1g3Foo(%struct.Bar* %foo)
+void g(Foo foo) {
+  // CHECK: call void @_ZN3BarC1Ev
+  // CHECK: @_ZNK3BarcvRK3FooEv
+  // CHECK: call void @_Z1f3Foo
+  f(Bar());
+  // CHECK: call void @_ZN3FooC1Ev
+  // CHECK: call void @_Z1f3Foo
+  f(Foo());
+  // CHECK: call void @_ZN3FooC1ERKS_
+  // CHECK: call void @_Z1f3Foo
+  f(foo);
+  // CHECK: ret
+}
+
diff --git a/test/CodeGenCXX/derived-to-base-conv.cpp b/test/CodeGenCXX/derived-to-base-conv.cpp
index c1a0caa..f2835b7 100644
--- a/test/CodeGenCXX/derived-to-base-conv.cpp
+++ b/test/CodeGenCXX/derived-to-base-conv.cpp
@@ -7,16 +7,21 @@
 extern "C" void exit(int);
 
 struct A {
- A (const A&) { printf("A::A(const A&)\n"); }
- A() {};
+  A (const A&) { printf("A::A(const A&)\n"); }
+  A() {};
+  ~A() { printf("A::~A()\n"); }
 }; 
 
 struct B : public A {
   B() {};
-}; 
+  B(const B& Other) : A(Other) { printf("B::B(const B&)\n"); }
+  ~B() { printf("B::~B()\n"); }
+};
 
 struct C : public B {
   C() {};
+  C(const C& Other) : B(Other) { printf("C::C(const C&)\n"); }
+  ~C() { printf("C::~C()\n"); }
 }; 
 
 struct X {
@@ -24,6 +29,7 @@
 	operator C&() {printf("X::operator C&()\n"); return c; }
  	X (const X&) { printf("X::X(const X&)\n"); }
  	X () { printf("X::X()\n"); }
+ 	~X () { printf("X::~X()\n"); }
 	B b;
 	C c;
 };
diff --git a/test/CodeGenCXX/virt.cpp b/test/CodeGenCXX/virt.cpp
index c404129..326d322 100644
--- a/test/CodeGenCXX/virt.cpp
+++ b/test/CodeGenCXX/virt.cpp
@@ -104,7 +104,7 @@
 struct test7_D : test7_B2, virtual test7_B1 {
 };
 
-// CHECK-LP64: .zerofill __DATA,__common,_d7,16,3
+// CHECK-LP64: .zerofill __DATA,__common,_d7,16,4
 
 
 struct test3_B3 { virtual void funcB3(); };
diff --git a/test/SemaCXX/conditional-expr.cpp b/test/SemaCXX/conditional-expr.cpp
index e2a966b..49bcd99 100644
--- a/test/SemaCXX/conditional-expr.cpp
+++ b/test/SemaCXX/conditional-expr.cpp
@@ -213,3 +213,30 @@
     (void)(Cond? a : S);
   }
 }
+
+namespace PR6757 {
+  struct Foo1 {
+    Foo1();
+    Foo1(const Foo1&);
+  };
+
+  struct Foo2 { };
+
+  struct Foo3 {
+    Foo3();
+    Foo3(Foo3&);
+  };
+
+  struct Bar {
+    operator const Foo1&() const;
+    operator const Foo2&() const;
+    operator const Foo3&() const;
+  };
+
+  void f() {
+    (void)(true ? Bar() : Foo1()); // okay
+    (void)(true ? Bar() : Foo2()); // okay
+    // FIXME: Diagnostic below could be improved
+    (void)(true ? Bar() : Foo3()); // expected-error{{incompatible operand types ('PR6757::Bar' and 'PR6757::Foo3')}}
+  }
+}
diff --git a/test/SemaCXX/copy-initialization.cpp b/test/SemaCXX/copy-initialization.cpp
index 3a63be0..e5b1fd7 100644
--- a/test/SemaCXX/copy-initialization.cpp
+++ b/test/SemaCXX/copy-initialization.cpp
@@ -21,3 +21,23 @@
 
 // PR3600
 void test(const foo *P) { P->bar(); } // expected-error{{cannot initialize object parameter of type 'foo' with an expression of type 'foo const'}}
+
+namespace PR6757 {
+  struct Foo {
+    Foo();
+    Foo(Foo&);
+  };
+
+  struct Bar {
+    operator const Foo&() const;
+  };
+
+  void f(Foo); // expected-note{{candidate function not viable: no known conversion from 'PR6757::Bar' to 'PR6757::Foo' for 1st argument}}
+
+  // FIXME: This isn't really the right reason for the failure. We
+  // should fail after overload resolution.
+  void g(Foo foo) {
+    f(Bar()); // expected-error{{no matching function for call to 'f'}}
+    f(foo);
+  }
+}