[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / SemaOpenCL / invalid-block.cl
blob6c918d302f8018078d69c1e121f9fc3b5d80f661
1 // RUN: %clang_cc1 -verify -fblocks -cl-std=CL2.0 %s
2 // RUN: %clang_cc1 -verify -fblocks -cl-std=CL3.0 -cl-ext=-all,+__opencl_c_device_enqueue,+__opencl_c_generic_address_space,+__opencl_c_program_scope_global_variables %s
3 // OpenCL v2.0 s6.12.5
4 void f0(int (^const bl)(void)); // expected-error{{declaring function parameter of type 'int (__generic ^const __private)(void)' is not allowed}}
5 // All blocks declarations must be const qualified and initialized.
6 void f1(void) {
7 int (^bl1)(void) = ^(void) {
8 return 1;
9 };
10 int (^const bl2)(void) = ^(void) {
11 return 1;
13 f0(bl1);
14 f0(bl2);
15 bl1 = bl2; // expected-error{{invalid operands to binary expression ('int (__generic ^const __private)(void)' and 'int (__generic ^const __private)(void)')}}
16 int (^const bl3)(void); // expected-error{{invalid block variable declaration - must be initialized}}
19 // A block with extern storage class is not allowed.
20 extern int (^bl)(void) = ^(void) { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}}
21 return 1;
23 void f2() {
24 extern int (^bl)(void) = ^(void) { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}}
25 return 1;
29 // A block cannot be the return value or parameter of a function.
30 typedef int (^bl_t)(void);
31 bl_t f3a(int); // expected-error{{declaring function return value of type 'bl_t' (aka 'int (__generic ^const)(void)') is not allowed}}
32 bl_t f3b(bl_t bl);
33 // expected-error@-1{{declaring function return value of type 'bl_t' (aka 'int (__generic ^const)(void)') is not allowed}}
34 // expected-error@-2{{declaring function parameter of type '__private bl_t' (aka 'int (__generic ^const __private)(void)') is not allowed}}
35 void f3c(void) {
36 // Block with a block argument.
37 int (^const bl2)(bl_t block_arg) = ^(void) { // expected-error{{declaring function parameter of type '__private bl_t' (aka 'int (__generic ^const __private)(void)') is not allowed}}
38 return block_arg(); // expected-error{{use of undeclared identifier 'block_arg'}}
42 struct bl_s {
43 int (^bl)(void); // expected-error {{the 'int (__generic ^const)(void)' type cannot be used to declare a structure or union field}}
46 void f4() {
47 __block int a = 10; // expected-error {{the __block storage type is not permitted}}
50 // A block with variadic argument is not allowed.
51 int (^bl)(int, ...) = ^int(int I, ...) { // expected-error {{invalid prototype, variadic arguments are not allowed in OpenCL}} expected-error {{invalid prototype, variadic arguments are not allowed in OpenCL}}
52 return 0;
54 typedef int (^bl1_t)(int, ...); // expected-error {{invalid prototype, variadic arguments are not allowed in OpenCL}}
56 // A block can't be used to declare an array
57 typedef int (^bl2_t)(int);
58 void f5(int i) {
59 bl2_t bl1 = ^(int i) {
60 return 1;
62 bl2_t bl2 = ^(int i) {
63 return 2;
65 bl2_t arr[] = {bl1, bl2}; // expected-error {{array of 'bl2_t' (aka 'int (__generic ^const)(__private int)') type is invalid in OpenCL}}
66 int tmp = i ? bl1(i) // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
67 : bl2(i); // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
69 // A block pointer type and all pointer operations are disallowed
70 void f6(bl2_t *bl_ptr) { // expected-error{{pointer to type 'bl2_t' (aka 'int (__generic ^const)(__private int)') is invalid in OpenCL}}
71 bl2_t bl = ^(int i) {
72 return 1;
74 bl2_t *p; // expected-error {{pointer to type 'bl2_t' (aka 'int (__generic ^const)(__private int)') is invalid in OpenCL}}
75 *bl; // expected-error {{invalid argument type '__private bl2_t' (aka 'int (__generic ^const __private)(__private int)') to unary expression}}
76 &bl; // expected-error {{invalid argument type '__private bl2_t' (aka 'int (__generic ^const __private)(__private int)') to unary expression}}
78 // A block can't reference another block
79 kernel void f7(void) {
80 bl2_t bl1 = ^(int i) {
81 return 1;
83 void (^bl2)(void) = ^{
84 int i = bl1(1); // expected-error {{cannot refer to a block inside block}}
86 void (^bl3)(void) = ^{
88 void (^bl4)(void) = ^{
89 bl3(); // expected-error {{cannot refer to a block inside block}}
91 return;
94 // Taking address of a capture is not allowed
95 int g;
96 kernel void f8(int a1) {
97 int a2;
98 void (^bl)(void) = ^(void) {
99 &g; //expected-warning{{expression result unused}}
100 &a1; //expected-error{{taking address of a capture is not allowed}}
101 &a2; //expected-error{{taking address of a capture is not allowed}}