diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2015-04-27 21:27:54 +0000 |
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2015-04-27 21:27:54 +0000 |
| commit | c38498f0469aba6a0fb63d99ad398cdf788c5696 (patch) | |
| tree | ed7df3fc6de1b03134e8350a770ac565f5824b60 /clang/test | |
| parent | 5b395243132dc3463c23daea1f215237565ce85f (diff) | |
| download | bcm5719-llvm-c38498f0469aba6a0fb63d99ad398cdf788c5696.tar.gz bcm5719-llvm-c38498f0469aba6a0fb63d99ad398cdf788c5696.zip | |
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
Diffstat (limited to 'clang/test')
| -rw-r--r-- | clang/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp | 7 | ||||
| -rw-r--r-- | clang/test/CXX/expr/expr.prim/expr.prim.lambda/templates.cpp | 5 | ||||
| -rw-r--r-- | clang/test/SemaCXX/PR23334.cpp | 10 | ||||
| -rw-r--r-- | clang/test/SemaCXX/cxx1y-generic-lambdas.cpp | 6 | ||||
| -rw-r--r-- | clang/test/SemaCXX/uninitialized.cpp | 9 |
5 files changed, 29 insertions, 8 deletions
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 647c76d604e..872248e77e6 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 @@ 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'}} \ // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'int'}} } void tickle() const; @@ -34,9 +33,11 @@ void odr_used(P &p, Boom<int> boom_int, Boom<float> boom_float, 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 90cbf02b2a6..c18bb7d1921 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 @@ namespace p2 { 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 @@ namespace p2 { 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 00000000000..cd6b4f4551f --- /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 90ecf6904e5..f4c67fbba0c 100644 --- a/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp +++ b/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp @@ -899,8 +899,10 @@ struct X1 { 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 f02357419f5..5769a0c028c 100644 --- a/clang/test/SemaCXX/uninitialized.cpp +++ b/clang/test/SemaCXX/uninitialized.cpp @@ -1428,3 +1428,12 @@ class A { 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]{}; + } +} |

