PR23334: Perform semantic checking of lambda capture initialization in the right context.

Previously we'd try to perform checks on the captures from the middle of
parsing the lambda's body, at the point where we detected that a variable
needed to be captured. This was wrong in a number of subtle ways. In
PR23334, we couldn't correctly handle the list of potential odr-uses
resulting from the capture, and our attempt to recover from that resulted
in a use-after-free.

We now defer building the initialization expression until we leave the lambda
body and return to the enclosing context, where the initialization does the
right thing. This patch only covers lambda-expressions, but we should apply
the same change to blocks and captured statements too.

llvm-svn: 235921
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp
index 647c76d..872248e 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp
@@ -24,7 +24,6 @@
 struct Boom {
   Boom(const Boom&) { 
     T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} \
-    // expected-error{{cannot initialize a variable of type 'float *' with an rvalue of type 'int'}} \
     // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'int'}}
   }
   void tickle() const;
@@ -34,9 +33,11 @@
               Boom<double> boom_double) {
   const std::type_info &ti1
     = typeid([=,&p]() -> P& { boom_int.tickle(); return p; }()); // expected-note{{in instantiation of member function 'Boom<int>::Boom' requested here}}
+  // This does not cause the instantiation of the Boom copy constructor,
+  // because the copy-initialization of the capture of boom_float occurs in an
+  // unevaluated operand.
   const std::type_info &ti2
-    = typeid([=]() -> int { boom_float.tickle(); return 0; }()); // expected-error{{lambda expression in an unevaluated operand}} \
-  // expected-note{{in instantiation of member function 'Boom<float>::Boom' requested here}}
+    = typeid([=]() -> int { boom_float.tickle(); return 0; }()); // expected-error{{lambda expression in an unevaluated operand}}
 
   auto foo = [=]() -> int { boom_double.tickle(); return 0; }; // expected-note{{in instantiation of member function 'Boom<double>::Boom' requested here}}
 }
diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/templates.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/templates.cpp
index 90cbf02..c18bb7d 100644
--- a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/templates.cpp
+++ b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/templates.cpp
@@ -69,8 +69,7 @@
   template<typename T>
   struct Boom {
     Boom(const Boom&) { 
-      T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} \
-      // expected-error{{cannot initialize a variable of type 'float *' with an rvalue of type 'int'}}
+      T* x = 1; // expected-error{{cannot initialize a variable of type 'float *' with an rvalue of type 'int'}}
     }
     void tickle() const;
   };
@@ -79,7 +78,7 @@
   void odr_used(R &r, Boom<T> boom) {
     const std::type_info &ti
       = typeid([=,&r] () -> R& { // expected-error{{lambda expression in an unevaluated operand}}
-          boom.tickle(); // expected-note{{in instantiation of member function}}
+          boom.tickle();
           return r; 
         }()); 
   }
diff --git a/clang/test/SemaCXX/PR23334.cpp b/clang/test/SemaCXX/PR23334.cpp
new file mode 100644
index 0000000..cd6b4f4
--- /dev/null
+++ b/clang/test/SemaCXX/PR23334.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s -Wno-unused
+
+// This must be at the start of the file (the failure depends on a SmallPtrSet
+// not having been reallocated yet).
+void fn1() {
+  // expected-no-diagnostics
+  constexpr int kIsolationClass = 0;
+  const int kBytesPerConnection = 0;
+  [=] { kIsolationClass, kBytesPerConnection, kBytesPerConnection; };
+}
diff --git a/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp b/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
index 90ecf69..f4c67fb 100644
--- a/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
+++ b/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
@@ -899,8 +899,10 @@
     int L2 = ([](auto i) { return i; })(2);
     void fooG(T i = ([] (auto i) { return i; })(2)) { }
     int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}\
-                                            //expected-note{{non-literal type}}
-    int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}}
+                                            //expected-note{{non-literal type}}\
+                                            //expected-error{{inside of a constant expression}}
+    int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}} \
+                                             //expected-error{{inside of a constant expression}}
     int (*fpG)(T) = [](auto i) { return i; };
     void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
     template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp
index f023574..5769a0c 100644
--- a/clang/test/SemaCXX/uninitialized.cpp
+++ b/clang/test/SemaCXX/uninitialized.cpp
@@ -1428,3 +1428,12 @@
   A(int (*) [7]) : a(rvalueref::notmove(a)) {}
 };
 }
+
+void array_capture(bool b) {
+  const char fname[] = "array_capture";
+  if (b) {
+    int unused; // expected-warning {{unused variable}}
+  } else {
+    [fname]{};
+  }
+}