1*cf49cae2SMichael Benfield // RUN: %clang_cc1 -std=c++11 %s -Wunused -Wno-unused-but-set-variable -Wno-unused-lambda-capture -verify 2f02455e5SDouglas Gregor odr_used()3f02455e5SDouglas Gregorvoid odr_used() { 4f02455e5SDouglas Gregor int i = 17; 5f02455e5SDouglas Gregor [i]{}(); 6f02455e5SDouglas Gregor } 7f02455e5SDouglas Gregor 8f02455e5SDouglas Gregor struct ReachingThis { static_fooReachingThis9f02455e5SDouglas Gregor static void static_foo() { 10f02455e5SDouglas Gregor (void)[this](){}; // expected-error{{'this' cannot be captured in this context}} 11f02455e5SDouglas Gregor 12f02455e5SDouglas Gregor struct Local { 13f02455e5SDouglas Gregor int i; 14f02455e5SDouglas Gregor 15f02455e5SDouglas Gregor void bar() { 16f02455e5SDouglas Gregor (void)[this](){}; 17f02455e5SDouglas Gregor (void)[&](){i = 7; }; 18f02455e5SDouglas Gregor } 19f02455e5SDouglas Gregor }; 20f02455e5SDouglas Gregor } 21f02455e5SDouglas Gregor fooReachingThis22f02455e5SDouglas Gregor void foo() { 23f02455e5SDouglas Gregor (void)[this](){}; 24f02455e5SDouglas Gregor 25f02455e5SDouglas Gregor struct Local { 26f02455e5SDouglas Gregor int i; 27f02455e5SDouglas Gregor 28f02455e5SDouglas Gregor static void static_bar() { 29f02455e5SDouglas Gregor (void)[this](){}; // expected-error{{'this' cannot be captured in this context}} 30fa0a1f53SRichard Smith (void)[&](){i = 7; }; // expected-error{{invalid use of member 'i' in static member function}} 31f02455e5SDouglas Gregor } 32f02455e5SDouglas Gregor }; 33f02455e5SDouglas Gregor } 34f02455e5SDouglas Gregor }; 35f02455e5SDouglas Gregor immediately_enclosing(int i)36f02455e5SDouglas Gregorvoid immediately_enclosing(int i) { // expected-note{{'i' declared here}} 37f02455e5SDouglas Gregor [i]() { 38f02455e5SDouglas Gregor [i] {}(); 39f02455e5SDouglas Gregor }(); 40f02455e5SDouglas Gregor 41f02455e5SDouglas Gregor [=]() { 42f02455e5SDouglas Gregor [i] {}(); 43f02455e5SDouglas Gregor }(); 44f02455e5SDouglas Gregor 45cb559c8dSNathan James []() { // expected-note{{lambda expression begins here}} expected-note 2 {{capture 'i' by}} expected-note 2 {{default capture by}} 46f02455e5SDouglas Gregor [i] {}(); // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}} 47f02455e5SDouglas Gregor }(); 48f02455e5SDouglas Gregor } 4901db64f0SDouglas Gregor f1(int i)5001db64f0SDouglas Gregorvoid f1(int i) { // expected-note{{declared here}} 5101db64f0SDouglas Gregor int const N = 20; 5201db64f0SDouglas Gregor auto m1 = [=]{ 5301db64f0SDouglas Gregor int const M = 30; 5401db64f0SDouglas Gregor auto m2 = [i]{ 55c6237c6eSEli Friedman int x[N][M]; 56c6237c6eSEli Friedman x[0][0] = i; 5701db64f0SDouglas Gregor }; 5801db64f0SDouglas Gregor (void)N; 5901db64f0SDouglas Gregor (void)M; 6001db64f0SDouglas Gregor (void)m2; 6101db64f0SDouglas Gregor }; 6201db64f0SDouglas Gregor struct s1 { 6301db64f0SDouglas Gregor int f; 6401db64f0SDouglas Gregor void work(int n) { // expected-note{{declared here}} 6501db64f0SDouglas Gregor int m = n*n; 6601db64f0SDouglas Gregor int j = 40; // expected-note{{declared here}} 67cb559c8dSNathan James auto m3 = [this, m] { // expected-note 3{{lambda expression begins here}} expected-note 2 {{capture 'i' by}} expected-note 2 {{capture 'j' by}} expected-note 2 {{capture 'n' by}} 6801db64f0SDouglas Gregor auto m4 = [&,j] { // expected-error{{variable 'j' cannot be implicitly captured in a lambda with no capture-default specified}} 6901db64f0SDouglas Gregor int x = n; // expected-error{{variable 'n' cannot be implicitly captured in a lambda with no capture-default specified}} 7001db64f0SDouglas Gregor x += m; 7181495f34SDouglas Gregor x += i; // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}} 7201db64f0SDouglas Gregor x += f; 7301db64f0SDouglas Gregor }; 7401db64f0SDouglas Gregor }; 7501db64f0SDouglas Gregor } 7601db64f0SDouglas Gregor }; 7701db64f0SDouglas Gregor } 78