Clang Project

clang_source_code/test/SemaOpenCL/invalid-block.cl
1// RUN: %clang_cc1 -verify -fblocks -cl-std=CL2.0 %s
2
3// OpenCL v2.0 s6.12.5
4void f0(int (^const bl)());
5// All blocks declarations must be const qualified and initialized.
6void f1() {
7  int (^bl1)(void) = ^() {
8    return 1;
9  };
10  int (^const bl2)(void) = ^() {
11    return 1;
12  };
13  f0(bl1);
14  f0(bl2);
15  bl1 = bl2;          // expected-error{{invalid operands to binary expression ('int (__generic ^const)(void)' and 'int (__generic ^const)(void)')}}
16  int (^const bl3)(); // 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) = ^() { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}}
21  return 1;
22};
23void f2() {
24  extern int (^bl)(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 of a function.
30typedef int (^bl_t)(void);
31bl_t f3(bl_t bl); // expected-error{{declaring function return value of type 'bl_t' (aka 'int (__generic ^const)(void)') is not allowed}}
32
33struct bl_s {
34  int (^bl)(void); // expected-error {{the 'int (__generic ^const)(void)' type cannot be used to declare a structure or union field}}
35};
36
37void f4() {
38  __block int a = 10; // expected-error {{the __block storage type is not permitted}}
39}
40
41// A block with variadic argument is not allowed.
42int (^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}}
43  return 0;
44};
45typedef int (^bl1_t)(int, ...); // expected-error {{invalid prototype, variadic arguments are not allowed in OpenCL}}
46
47// A block can't be used to declare an array
48typedef int (^bl2_t)(int);
49void f5(int i) {
50  bl2_t bl1 = ^(int i) {
51    return 1;
52  };
53  bl2_t bl2 = ^(int i) {
54    return 2;
55  };
56  bl2_t arr[] = {bl1, bl2}; // expected-error {{array of 'bl2_t' (aka 'int (__generic ^const)(int)') type is invalid in OpenCL}}
57  int tmp = i ? bl1(i)      // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
58              : bl2(i);     // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
59}
60// A block pointer type and all pointer operations are disallowed
61void f6(bl2_t *bl_ptr) { // expected-error{{pointer to type '__generic bl2_t' (aka 'int (__generic ^const __generic)(int)') is invalid in OpenCL}}
62  bl2_t bl = ^(int i) {
63    return 1;
64  };
65  bl2_t *p; // expected-error {{pointer to type '__generic bl2_t' (aka 'int (__generic ^const __generic)(int)') is invalid in OpenCL}}
66  *bl;      // expected-error {{invalid argument type 'bl2_t' (aka 'int (__generic ^const)(int)') to unary expression}}
67  &bl;      // expected-error {{invalid argument type 'bl2_t' (aka 'int (__generic ^const)(int)') to unary expression}}
68}
69// A block can't reference another block
70kernel void f7() {
71  bl2_t bl1 = ^(int i) {
72    return 1;
73  };
74  void (^bl2)(void) = ^{
75    int i = bl1(1); // expected-error {{cannot refer to a block inside block}}
76  };
77  void (^bl3)(void) = ^{
78  };
79  void (^bl4)(void) = ^{
80    bl3(); // expected-error {{cannot refer to a block inside block}}
81  };
82  return;
83}
84
85// Taking address of a capture is not allowed
86int g;
87kernel void f8(int a1) {
88  int a2;
89  void (^bl)(void) = ^(void) {
90    &g; //expected-warning{{expression result unused}}
91    &a1; //expected-error{{taking address of a capture is not allowed}}
92    &a2; //expected-error{{taking address of a capture is not allowed}}
93  };
94}
95