[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / captured-statements.cpp
blobd8f77e6017ab89c6cba0f0d3dae854e0678e2c63
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fblocks
3 void test_nest_lambda() {
4 int x;
5 int y;
6 [&,y]() {
7 int z;
8 #pragma clang __debug captured
10 x = y; // OK
11 y = z; // expected-error{{cannot assign to a variable captured by copy in a non-mutable lambda}}
12 z = y; // OK
14 }();
16 int a;
17 #pragma clang __debug captured
19 int b;
20 int c;
21 [&,c]() {
22 a = b; // OK
23 b = c; // OK
24 c = a; // expected-error{{cannot assign to a variable captured by copy in a non-mutable lambda}}
25 }();
29 class test_obj_capture {
30 int a;
31 void b();
32 static void test() {
33 test_obj_capture c;
34 #pragma clang __debug captured
35 { (void)c.a; } // OK
36 #pragma clang __debug captured
37 { c.b(); } // OK
41 class test_this_capture {
42 int a;
43 void b();
44 void test() {
45 #pragma clang __debug captured
46 { (void)this; } // OK
47 #pragma clang __debug captured
48 { (void)a; } // OK
49 #pragma clang __debug captured
50 { b(); } // OK
54 template <typename T>
55 void template_capture_var() {
56 T x; // expected-error{{declaration of reference variable 'x' requires an initializer}}
57 #pragma clang _debug captured
59 (void)x;
63 template <typename T>
64 class Val {
65 T v;
66 public:
67 void set(const T &v0) {
68 #pragma clang __debug captured
70 v = v0;
75 void test_capture_var() {
76 template_capture_var<int>(); // OK
77 template_capture_var<int&>(); // expected-note{{in instantiation of function template specialization 'template_capture_var<int &>' requested here}}
79 Val<float> Obj;
80 Obj.set(0.0f); // OK
83 template <typename S, typename T>
84 S template_capture_var(S x, T y) { // expected-note{{variable 'y' declared const here}}
85 #pragma clang _debug captured
87 x++;
88 y++; // expected-error{{cannot assign to variable 'y' with const-qualified type 'const int'}}
91 return x;
94 // Check if can recover from a template error.
95 void test_capture_var_error() {
96 template_capture_var<int, int>(0, 1); // OK
97 template_capture_var<int, const int>(0, 1); // expected-note{{in instantiation of function template specialization 'template_capture_var<int, const int>' requested here}}
98 template_capture_var<int, int>(0, 1); // OK
101 template <typename T>
102 void template_capture_in_lambda() {
103 T x, y;
104 [=, &y]() {
105 #pragma clang __debug captured
107 y += x;
109 }();
112 void test_lambda() {
113 template_capture_in_lambda<int>(); // OK
116 struct Foo {
117 void foo() { }
118 static void bar() { }
121 template <typename T>
122 void template_capture_func(T &t) {
123 #pragma clang __debug captured
125 t.foo();
128 #pragma clang __debug captured
130 T::bar();
134 void test_template_capture_func() {
135 Foo Obj;
136 template_capture_func(Obj);
139 template <typename T>
140 T captured_sum(const T &a, const T &b) {
141 T result;
143 #pragma clang __debug captured
145 result = a + b;
148 return result;
151 template <typename T, typename... Args>
152 T captured_sum(const T &a, const Args&... args) {
153 T result;
155 #pragma clang __debug captured
157 result = a + captured_sum(args...);
160 return result;
163 void test_capture_variadic() {
164 (void)captured_sum(1, 2, 3); // OK
165 (void)captured_sum(1, 2, 3, 4, 5); // OK
168 void test_capture_with_attributes() {
169 [[]] // expected-error {{an attribute list cannot appear here}}
170 #pragma clang __debug captured