xref: /llvm-project/clang/test/SemaOpenCL/invalid-block.cl (revision 957213f60b258a5f6cab04e50c78c0a0c1c304c2)
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
4void 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.
6void f1(void) {
7  int (^bl1)(void) = ^(void) {
8    return 1;
9  };
10  int (^const bl2)(void) = ^(void) {
11    return 1;
12  };
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}}
17}
18
19// A block with extern storage class is not allowed.
20extern int (^bl)(void) = ^(void) { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}}
21  return 1;
22};
23void f2() {
24  extern int (^bl)(void) = ^(void) { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}}
25    return 1;
26  };
27}
28
29// A block cannot be the return value or parameter of a function.
30typedef int (^bl_t)(void);
31bl_t f3a(int);     // expected-error{{declaring function return value of type 'bl_t' (aka 'int (__generic ^const)(void)') is not allowed}}
32bl_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}}
35void 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'}}
39  };
40}
41
42struct bl_s {
43  int (^bl)(void); // expected-error {{the 'int (__generic ^const)(void)' type cannot be used to declare a structure or union field}}
44};
45
46void f4() {
47  __block int a = 10; // expected-error {{the __block storage type is not permitted}}
48}
49
50// A block with variadic argument is not allowed.
51int (^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;
53};
54typedef int (^bl1_t)(int, ...); // expected-error {{invalid prototype, variadic arguments are not allowed in OpenCL}}
55
56// A block can't be used to declare an array
57typedef int (^bl2_t)(int);
58void f5(int i) {
59  bl2_t bl1 = ^(int i) {
60    return 1;
61  };
62  bl2_t bl2 = ^(int i) {
63    return 2;
64  };
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}}
68  bl2_t bref = i ? bl1      // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
69                 : bl2;     // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
70}
71// A block pointer type and all pointer operations are disallowed
72void f6(bl2_t *bl_ptr) { // expected-error{{pointer to type 'bl2_t' (aka 'int (__generic ^const)(__private int)') is invalid in OpenCL}}
73  bl2_t bl = ^(int i) {
74    return 1;
75  };
76  bl2_t *p; // expected-error {{pointer to type 'bl2_t' (aka 'int (__generic ^const)(__private int)') is invalid in OpenCL}}
77  *bl;      // expected-error {{invalid argument type '__private bl2_t' (aka 'int (__generic ^const __private)(__private int)') to unary expression}}
78  &bl;      // expected-error {{invalid argument type '__private bl2_t' (aka 'int (__generic ^const __private)(__private int)') to unary expression}}
79}
80// A block can't reference another block
81kernel void f7(void) {
82  bl2_t bl1 = ^(int i) {
83    return 1;
84  };
85  void (^bl2)(void) = ^{
86    int i = bl1(1); // expected-error {{cannot refer to a block inside block}}
87  };
88  void (^bl3)(void) = ^{
89  };
90  void (^bl4)(void) = ^{
91    bl3(); // expected-error {{cannot refer to a block inside block}}
92  };
93  return;
94}
95
96// Taking address of a capture is not allowed
97int g;
98kernel void f8(int a1) {
99  int a2;
100  void (^bl)(void) = ^(void) {
101    &g; //expected-warning{{expression result unused}}
102    &a1; //expected-error{{taking address of a capture is not allowed}}
103    &a2; //expected-error{{taking address of a capture is not allowed}}
104  };
105}
106