invalid-block.cl 3.6 KB

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