• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -fblocks -Wnullable-to-nonnull-conversion -Wno-nullability-declspec %s -verify
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.
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;
14 
15 // Redundant nullability type specifiers.
16 typedef int * _Nonnull _Nonnull redundant_1; // expected-warning{{duplicate nullability specifier '_Nonnull'}}
17 
18 // Conflicting nullability type specifiers.
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'}}
21 
22 // Redundant nullability specifiers via a typedef are okay.
23 typedef nonnull_int_ptr _Nonnull redundant_okay_1;
24 
25 // Conflicting nullability specifiers via a typedef are not.
26 typedef nonnull_int_ptr _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}
27 typedef nonnull_int_ptr nonnull_int_ptr_typedef;
28 typedef nonnull_int_ptr_typedef _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}
29 typedef nonnull_int_ptr_typedef nonnull_int_ptr_typedef_typedef;
30 typedef nonnull_int_ptr_typedef_typedef _Null_unspecified conflicting_3; // expected-error{{nullability specifier '_Null_unspecified' conflicts with existing specifier '_Nonnull'}}
31 
32 // Nullability applies to all pointer types.
33 typedef int (* _Nonnull function_pointer_type_1)(int, int);
34 typedef int (^ _Nonnull block_type_1)(int, int);
35 
36 // Nullability must be on a pointer type.
37 typedef int _Nonnull int_type_1; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}
38 
39 // Nullability can move out to a pointer/block pointer declarator
40 // (with a suppressed warning).
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);
50 typedef void (* function_pointer_type_5)(int_ptr _Nonnull);
51 
52 void acceptFunctionPtr(_Nonnull int *(*)(void));
53 void acceptBlockPtr(_Nonnull int *(^)(void));
54 
testBlockFunctionPtrNullability()55 void testBlockFunctionPtrNullability() {
56   float *fp;
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)')}}
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)')}}
61   fp = (block_type_4)0; // expected-error{{from incompatible type 'block_type_4' (aka 'int_ptr  _Nonnull (^)(int, int)')}}
62 
63   acceptFunctionPtr(0); // no-warning
64   acceptBlockPtr(0); // no-warning
65 }
66 
67 // Moving nullability where it creates a conflict.
68 typedef _Nonnull int * _Nullable *  conflict_int_ptr_ptr_2; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}
69 
70 // Nullability is not part of the canonical type.
71 typedef int * _Nonnull ambiguous_int_ptr;
72 typedef int * ambiguous_int_ptr;
73 typedef int * _Nullable ambiguous_int_ptr;
74 
75 // Printing of nullability.
76 float f;
77 int * _Nonnull ip_1 = &f; // expected-warning{{incompatible pointer types initializing 'int * _Nonnull' with an expression of type 'float *'}}
78 
79 // Check printing of nullability specifiers.
printing_nullability(void)80 void printing_nullability(void) {
81   int * _Nonnull iptr;
82   float *fptr = iptr; // expected-warning{{incompatible pointer types initializing 'float *' with an expression of type 'int * _Nonnull'}}
83 
84   int * * _Nonnull iptrptr;
85   float **fptrptr = iptrptr; // expected-warning{{incompatible pointer types initializing 'float **' with an expression of type 'int ** _Nonnull'}}
86 
87   int * _Nullable * _Nonnull iptrptr2;
88   float * *fptrptr2 = iptrptr2; // expected-warning{{incompatible pointer types initializing 'float **' with an expression of type 'int * _Nullable * _Nonnull'}}
89 }
90 
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);
95 
test_accepts_nonnull_null_pointer_literal()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 
102 // Check returning nil from a _Nonnull-returning function.
returns_int_ptr(int x)103 _Nonnull int *returns_int_ptr(int x) {
104   if (x) {
105     return 0; // expected-warning{{null returned from function that requires a non-null return value}}
106   }
107 
108   return (_Nonnull int *)0;
109 }
110 
111 // Check nullable-to-nonnull conversions.
nullable_to_nonnull(_Nullable int * ptr)112 void nullable_to_nonnull(_Nullable int *ptr) {
113   int *a = ptr; // okay
114   _Nonnull int *b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
115   b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
116 
117   accepts_nonnull_1(ptr); // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
118 }
119