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