xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/cxx0x-lambda-expressions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc enum E { e };
4f4a2713aSLionel Sambuc 
id(int n)5*0a6a1f1dSLionel Sambuc constexpr int id(int n) { return n; }
6*0a6a1f1dSLionel Sambuc 
7f4a2713aSLionel Sambuc class C {
8f4a2713aSLionel Sambuc 
f()9f4a2713aSLionel Sambuc   int f() {
10f4a2713aSLionel Sambuc     int foo, bar;
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc     []; // expected-error {{expected body of lambda expression}}
13f4a2713aSLionel Sambuc     [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
14f4a2713aSLionel Sambuc     [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
15f4a2713aSLionel Sambuc     [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
16f4a2713aSLionel Sambuc     [&this] {}; // expected-error {{'this' cannot be captured by reference}}
17f4a2713aSLionel Sambuc     [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
18f4a2713aSLionel Sambuc     [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
19f4a2713aSLionel Sambuc     [] {};
20f4a2713aSLionel Sambuc     [=] (int i) {};
21f4a2713aSLionel Sambuc     [&] (int) mutable -> void {};
22f4a2713aSLionel Sambuc     [foo,bar] () { return 3; };
23f4a2713aSLionel Sambuc     [=,&foo] () {};
24f4a2713aSLionel Sambuc     [&,foo] () {};
25f4a2713aSLionel Sambuc     [this] () {};
26f4a2713aSLionel Sambuc     [] () -> class C { return C(); };
27f4a2713aSLionel Sambuc     [] () -> enum E { return e; };
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc     [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
30f4a2713aSLionel Sambuc     [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
31f4a2713aSLionel Sambuc     [](int) -> {}; // PR13652 expected-error {{expected a type}}
32f4a2713aSLionel Sambuc     return 1;
33f4a2713aSLionel Sambuc   }
34f4a2713aSLionel Sambuc 
designator_or_lambda()35f4a2713aSLionel Sambuc   void designator_or_lambda() {
36f4a2713aSLionel Sambuc     typedef int T;
37f4a2713aSLionel Sambuc     const int b = 0;
38f4a2713aSLionel Sambuc     const int c = 1;
39*0a6a1f1dSLionel Sambuc     int d;
40*0a6a1f1dSLionel Sambuc     int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
41f4a2713aSLionel Sambuc     int a2[1] = {[b] = 1 };
42*0a6a1f1dSLionel Sambuc     int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
43f4a2713aSLionel Sambuc     int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
44f4a2713aSLionel Sambuc     int a5[3] = { []{return 0;}() };
45f4a2713aSLionel Sambuc     int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
46*0a6a1f1dSLionel Sambuc     int a7[1] = {[d(0)] { return d; } ()}; // expected-warning{{extension}}
47*0a6a1f1dSLionel Sambuc     int a8[1] = {[d = 0] { return d; } ()}; // expected-warning{{extension}}
48*0a6a1f1dSLionel Sambuc     int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
49*0a6a1f1dSLionel Sambuc     int a10[1] = {[id(0)] { return id; } ()}; // expected-warning{{extension}}
50*0a6a1f1dSLionel Sambuc     int a11[1] = {[id(0)] = 1};
51f4a2713aSLionel Sambuc   }
52f4a2713aSLionel Sambuc 
delete_lambda(int * p)53f4a2713aSLionel Sambuc   void delete_lambda(int *p) {
54f4a2713aSLionel Sambuc     delete [] p;
55f4a2713aSLionel Sambuc     delete [] (int*) { new int }; // ok, compound-literal, not lambda
56f4a2713aSLionel Sambuc     delete [] { return new int; } (); // expected-error{{expected expression}}
57f4a2713aSLionel Sambuc     delete [&] { return new int; } (); // ok, lambda
58f4a2713aSLionel Sambuc   }
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc   // We support init-captures in C++11 as an extension.
61f4a2713aSLionel Sambuc   int z;
init_capture()62f4a2713aSLionel Sambuc   void init_capture() {
63f4a2713aSLionel Sambuc     [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
64*0a6a1f1dSLionel Sambuc     [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}} expected-warning{{will change meaning in a future version}}
65f4a2713aSLionel Sambuc     [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}}
66f4a2713aSLionel Sambuc     [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
67f4a2713aSLionel Sambuc     [a([&b = z]{})](){}; // expected-warning 2{{extension}}
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc     int x = 4;
70f4a2713aSLionel Sambuc     auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}}
71f4a2713aSLionel Sambuc       r += 2;
72f4a2713aSLionel Sambuc       return x + 2;
73f4a2713aSLionel Sambuc     } ();
74f4a2713aSLionel Sambuc   }
75*0a6a1f1dSLionel Sambuc 
attributes()76*0a6a1f1dSLionel Sambuc   void attributes() {
77*0a6a1f1dSLionel Sambuc     [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}}
78*0a6a1f1dSLionel Sambuc     [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
79*0a6a1f1dSLionel Sambuc     []() [[]]
80*0a6a1f1dSLionel Sambuc       mutable {}; // expected-error {{expected body of lambda expression}}
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc     []() [[]] {};
83*0a6a1f1dSLionel Sambuc     []() [[]] -> void {};
84*0a6a1f1dSLionel Sambuc     []() mutable [[]] -> void {};
85*0a6a1f1dSLionel Sambuc     []() mutable noexcept [[]] -> void {};
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc     // Testing GNU-style attributes on lambdas -- the attribute is specified
88*0a6a1f1dSLionel Sambuc     // before the mutable specifier instead of after (unlike C++11).
89*0a6a1f1dSLionel Sambuc     []() __attribute__((noreturn)) mutable { while(1); };
90*0a6a1f1dSLionel Sambuc     []() mutable
91*0a6a1f1dSLionel Sambuc       __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
92*0a6a1f1dSLionel Sambuc   }
93f4a2713aSLionel Sambuc };
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc template <typename>
PR22122()96*0a6a1f1dSLionel Sambuc void PR22122() {
97*0a6a1f1dSLionel Sambuc   [](int) -> {}; // expected-error {{expected a type}}
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc template void PR22122<int>();
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc struct S {
103*0a6a1f1dSLionel Sambuc   template <typename T>
104*0a6a1f1dSLionel Sambuc   void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
105*0a6a1f1dSLionel Sambuc } s;
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc struct U {
108*0a6a1f1dSLionel Sambuc   template <typename T>
109*0a6a1f1dSLionel Sambuc   void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
110*0a6a1f1dSLionel Sambuc } *U;
111