nullability.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // RUN: %clang_cc1 -fsyntax-only -fblocks -Wnullable-to-nonnull-conversion -Wno-nullability-declspec %s -verify
  2. #if __has_feature(nullability)
  3. #else
  4. # error nullability feature should be defined
  5. #endif
  6. typedef int * int_ptr;
  7. // Parse nullability type specifiers.
  8. typedef int * _Nonnull nonnull_int_ptr; // expected-note{{'_Nonnull' specified here}}
  9. typedef int * _Nullable nullable_int_ptr;
  10. typedef int * _Null_unspecified null_unspecified_int_ptr;
  11. // Redundant nullability type specifiers.
  12. typedef int * _Nonnull _Nonnull redundant_1; // expected-warning{{duplicate nullability specifier '_Nonnull'}}
  13. // Conflicting nullability type specifiers.
  14. typedef int * _Nonnull _Nullable conflicting_1; // expected-error{{nullability specifier '_Nonnull' conflicts with existing specifier '_Nullable'}}
  15. typedef int * _Null_unspecified _Nonnull conflicting_2; // expected-error{{nullability specifier '_Null_unspecified' conflicts with existing specifier '_Nonnull'}}
  16. // Redundant nullability specifiers via a typedef are okay.
  17. typedef nonnull_int_ptr _Nonnull redundant_okay_1;
  18. // Conflicting nullability specifiers via a typedef are not.
  19. typedef nonnull_int_ptr _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}
  20. typedef nonnull_int_ptr nonnull_int_ptr_typedef;
  21. typedef nonnull_int_ptr_typedef _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}
  22. typedef nonnull_int_ptr_typedef nonnull_int_ptr_typedef_typedef;
  23. typedef nonnull_int_ptr_typedef_typedef _Null_unspecified conflicting_3; // expected-error{{nullability specifier '_Null_unspecified' conflicts with existing specifier '_Nonnull'}}
  24. // Nullability applies to all pointer types.
  25. typedef int (* _Nonnull function_pointer_type_1)(int, int);
  26. typedef int (^ _Nonnull block_type_1)(int, int);
  27. // Nullability must be on a pointer type.
  28. typedef int _Nonnull int_type_1; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}
  29. // Nullability can move out to a pointer/block pointer declarator
  30. // (with a suppressed warning).
  31. typedef _Nonnull int * nonnull_int_ptr_2;
  32. typedef int _Nullable * nullable_int_ptr_2;
  33. typedef _Nonnull int (* function_pointer_type_2)(int, int);
  34. typedef _Nonnull int (^ block_type_2)(int, int);
  35. typedef _Nonnull int * * _Nullable nonnull_int_ptr_ptr_1;
  36. typedef _Nonnull int *(^ block_type_3)(int, int);
  37. typedef _Nonnull int *(* function_pointer_type_3)(int, int);
  38. typedef _Nonnull int_ptr (^ block_type_4)(int, int);
  39. typedef _Nonnull int_ptr (* function_pointer_type_4)(int, int);
  40. void acceptFunctionPtr(_Nonnull int *(*)(void));
  41. void acceptBlockPtr(_Nonnull int *(^)(void));
  42. void testBlockFunctionPtrNullability() {
  43. float *fp;
  44. fp = (function_pointer_type_3)0; // expected-warning{{from 'function_pointer_type_3' (aka 'int * _Nonnull (*)(int, int)')}}
  45. fp = (block_type_3)0; // expected-error{{from incompatible type 'block_type_3' (aka 'int * _Nonnull (^)(int, int)')}}
  46. fp = (function_pointer_type_4)0; // expected-warning{{from 'function_pointer_type_4' (aka 'int_ptr _Nonnull (*)(int, int)')}}
  47. fp = (block_type_4)0; // expected-error{{from incompatible type 'block_type_4' (aka 'int_ptr _Nonnull (^)(int, int)')}}
  48. acceptFunctionPtr(0); // no-warning
  49. acceptBlockPtr(0); // no-warning
  50. }
  51. // Moving nullability where it creates a conflict.
  52. typedef _Nonnull int * _Nullable * conflict_int_ptr_ptr_2; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}
  53. // Nullability is not part of the canonical type.
  54. typedef int * _Nonnull ambiguous_int_ptr;
  55. typedef int * ambiguous_int_ptr;
  56. typedef int * _Nullable ambiguous_int_ptr;
  57. // Printing of nullability.
  58. float f;
  59. int * _Nonnull ip_1 = &f; // expected-warning{{incompatible pointer types initializing 'int * _Nonnull' with an expression of type 'float *'}}
  60. // Check printing of nullability specifiers.
  61. void printing_nullability(void) {
  62. int * _Nonnull iptr;
  63. float *fptr = iptr; // expected-warning{{incompatible pointer types initializing 'float *' with an expression of type 'int * _Nonnull'}}
  64. int * * _Nonnull iptrptr;
  65. float **fptrptr = iptrptr; // expected-warning{{incompatible pointer types initializing 'float **' with an expression of type 'int ** _Nonnull'}}
  66. int * _Nullable * _Nonnull iptrptr2;
  67. float * *fptrptr2 = iptrptr2; // expected-warning{{incompatible pointer types initializing 'float **' with an expression of type 'int * _Nullable * _Nonnull'}}
  68. }
  69. // Check passing null to a _Nonnull argument.
  70. void accepts_nonnull_1(_Nonnull int *ptr);
  71. void (*accepts_nonnull_2)(_Nonnull int *ptr);
  72. void (^accepts_nonnull_3)(_Nonnull int *ptr);
  73. void test_accepts_nonnull_null_pointer_literal() {
  74. accepts_nonnull_1(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
  75. accepts_nonnull_2(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
  76. accepts_nonnull_3(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
  77. }
  78. // Check returning nil from a _Nonnull-returning function.
  79. _Nonnull int *returns_int_ptr(int x) {
  80. if (x) {
  81. return 0; // expected-warning{{null returned from function that requires a non-null return value}}
  82. }
  83. return (_Nonnull int *)0;
  84. }
  85. // Check nullable-to-nonnull conversions.
  86. void nullable_to_nonnull(_Nullable int *ptr) {
  87. int *a = ptr; // okay
  88. _Nonnull int *b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
  89. }