Xiuli Pan | 89307aa | 2016-02-24 04:29:36 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -verify -fblocks -cl-std=CL2.0 %s |
| 2 | |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 3 | // OpenCL v2.0 s6.12.5 |
Anastasia Stulova | 4d85003 | 2016-07-11 13:46:02 +0000 | [diff] [blame] | 4 | void f0(int (^const bl)()); |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 5 | // All blocks declarations must be const qualified and initialized. |
| 6 | void f1() { |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 7 | int (^bl1)(void) = ^() { |
| 8 | return 1; |
| 9 | }; |
| 10 | int (^const bl2)(void) = ^() { |
| 11 | return 1; |
| 12 | }; |
Anastasia Stulova | 4d85003 | 2016-07-11 13:46:02 +0000 | [diff] [blame] | 13 | f0(bl1); |
| 14 | f0(bl2); |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 15 | bl1 = bl2; // expected-error{{invalid operands to binary expression ('int (__generic ^const)(void)' and 'int (__generic ^const)(void)')}} |
Anastasia Stulova | 4d85003 | 2016-07-11 13:46:02 +0000 | [diff] [blame] | 16 | int (^const bl3)(); // expected-error{{invalid block variable declaration - must be initialized}} |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | // A block with extern storage class is not allowed. |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 20 | extern int (^bl)(void) = ^() { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}} |
| 21 | return 1; |
| 22 | }; |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 23 | void f2() { |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 24 | extern int (^bl)(void) = ^() { // expected-error{{invalid block variable declaration - using 'extern' storage class is disallowed}} |
| 25 | return 1; |
| 26 | }; |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // A block cannot be the return value of a function. |
Anastasia Stulova | 4d85003 | 2016-07-11 13:46:02 +0000 | [diff] [blame] | 30 | typedef int (^bl_t)(void); |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 31 | bl_t f3(bl_t bl); // expected-error{{declaring function return value of type 'bl_t' (aka 'int (__generic ^const)(void)') is not allowed}} |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 32 | |
| 33 | struct bl_s { |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 34 | int (^bl)(void); // expected-error {{the 'int (__generic ^const)(void)' type cannot be used to declare a structure or union field}} |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | void 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. |
Anastasia Stulova | 7c30533 | 2016-10-28 12:59:39 +0000 | [diff] [blame] | 42 | 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}} |
Xiuli Pan | 89307aa | 2016-02-24 04:29:36 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | }; |
Anastasia Stulova | 7c30533 | 2016-10-28 12:59:39 +0000 | [diff] [blame] | 45 | typedef int (^bl1_t)(int, ...); // expected-error {{invalid prototype, variadic arguments are not allowed in OpenCL}} |
Xiuli Pan | 89307aa | 2016-02-24 04:29:36 +0000 | [diff] [blame] | 46 | |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 47 | // A block can't be used to declare an array |
Anastasia Stulova | 7c30533 | 2016-10-28 12:59:39 +0000 | [diff] [blame] | 48 | typedef int (^bl2_t)(int); |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 49 | void f5(int i) { |
Anastasia Stulova | 7c30533 | 2016-10-28 12:59:39 +0000 | [diff] [blame] | 50 | bl2_t bl1 = ^(int i) { |
| 51 | return 1; |
| 52 | }; |
| 53 | bl2_t bl2 = ^(int i) { |
| 54 | return 2; |
| 55 | }; |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 56 | bl2_t arr[] = {bl1, bl2}; // expected-error {{array of 'bl2_t' (aka 'int (__generic ^const)(int)') type is invalid in OpenCL}} |
Anastasia Stulova | 1f95cc0 | 2016-03-03 13:33:19 +0000 | [diff] [blame] | 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}} |
Xiuli Pan | 89307aa | 2016-02-24 04:29:36 +0000 | [diff] [blame] | 59 | } |
Anastasia Stulova | 4d85003 | 2016-07-11 13:46:02 +0000 | [diff] [blame] | 60 | // A block pointer type and all pointer operations are disallowed |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 61 | void f6(bl2_t *bl_ptr) { // expected-error{{pointer to type '__generic bl2_t' (aka 'int (__generic ^const __generic)(int)') is invalid in OpenCL}} |
Anastasia Stulova | 7c30533 | 2016-10-28 12:59:39 +0000 | [diff] [blame] | 62 | bl2_t bl = ^(int i) { |
| 63 | return 1; |
| 64 | }; |
Anastasia Stulova | af0a7bb | 2017-01-27 15:11:34 +0000 | [diff] [blame] | 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}} |
Xiuli Pan | 89307aa | 2016-02-24 04:29:36 +0000 | [diff] [blame] | 68 | } |
Anastasia Stulova | 9d98a31 | 2017-02-16 11:13:30 +0000 | [diff] [blame] | 69 | // A block can't reference another block |
| 70 | kernel 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 | } |
Anastasia Stulova | 257132a | 2017-09-07 17:00:33 +0000 | [diff] [blame] | 84 | |
| 85 | // Taking address of a capture is not allowed |
| 86 | int g; |
| 87 | kernel 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 | } |