blob: 9d3145d068b5184b30dd6894fdc43c11478131cc [file] [log] [blame]
Douglas Gregorb4866e82015-06-19 18:13:19 +00001// RUN: %clang_cc1 -fsyntax-only -fblocks -Wnullable-to-nonnull-conversion -Wno-nullability-declspec %s -verify
Douglas Gregor261a89b2015-06-19 17:51:05 +00002
3#if __has_feature(nullability)
4#else
5# error nullability feature should be defined
6#endif
7
8typedef int * int_ptr;
9
10// Parse nullability type specifiers.
Sunil Srivastavabf010802016-04-27 19:53:03 +000011// This note requires C11.
12#if __STDC_VERSION__ > 199901L
13// expected-note@+2{{'_Nonnull' specified here}}
14#endif
15typedef int * _Nonnull nonnull_int_ptr;
Douglas Gregoraea7afd2015-06-24 22:02:08 +000016typedef int * _Nullable nullable_int_ptr;
17typedef int * _Null_unspecified null_unspecified_int_ptr;
Douglas Gregor261a89b2015-06-19 17:51:05 +000018
19// Redundant nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000020typedef int * _Nonnull _Nonnull redundant_1; // expected-warning{{duplicate nullability specifier '_Nonnull'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000021
22// Conflicting nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000023typedef int * _Nonnull _Nullable conflicting_1; // expected-error{{nullability specifier '_Nonnull' conflicts with existing specifier '_Nullable'}}
24typedef int * _Null_unspecified _Nonnull conflicting_2; // expected-error{{nullability specifier '_Null_unspecified' conflicts with existing specifier '_Nonnull'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000025
26// Redundant nullability specifiers via a typedef are okay.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000027typedef nonnull_int_ptr _Nonnull redundant_okay_1;
Douglas Gregor261a89b2015-06-19 17:51:05 +000028
29// Conflicting nullability specifiers via a typedef are not.
Sunil Srivastavabf010802016-04-27 19:53:03 +000030// Some of these errors require C11.
31#if __STDC_VERSION__ > 199901L
Douglas Gregoraea7afd2015-06-24 22:02:08 +000032typedef nonnull_int_ptr _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}
Sunil Srivastavabf010802016-04-27 19:53:03 +000033#endif
Douglas Gregor261a89b2015-06-19 17:51:05 +000034typedef nonnull_int_ptr nonnull_int_ptr_typedef;
Sunil Srivastavabf010802016-04-27 19:53:03 +000035#if __STDC_VERSION__ > 199901L
Douglas Gregoraea7afd2015-06-24 22:02:08 +000036typedef nonnull_int_ptr_typedef _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}
Sunil Srivastavabf010802016-04-27 19:53:03 +000037#endif
Douglas Gregor261a89b2015-06-19 17:51:05 +000038typedef nonnull_int_ptr_typedef nonnull_int_ptr_typedef_typedef;
Douglas Gregoraea7afd2015-06-24 22:02:08 +000039typedef nonnull_int_ptr_typedef_typedef _Null_unspecified conflicting_3; // expected-error{{nullability specifier '_Null_unspecified' conflicts with existing specifier '_Nonnull'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000040
41// Nullability applies to all pointer types.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000042typedef int (* _Nonnull function_pointer_type_1)(int, int);
43typedef int (^ _Nonnull block_type_1)(int, int);
Douglas Gregor261a89b2015-06-19 17:51:05 +000044
45// Nullability must be on a pointer type.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000046typedef int _Nonnull int_type_1; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000047
48// Nullability can move out to a pointer/block pointer declarator
49// (with a suppressed warning).
Douglas Gregoraea7afd2015-06-24 22:02:08 +000050typedef _Nonnull int * nonnull_int_ptr_2;
51typedef int _Nullable * nullable_int_ptr_2;
52typedef _Nonnull int (* function_pointer_type_2)(int, int);
53typedef _Nonnull int (^ block_type_2)(int, int);
54typedef _Nonnull int * * _Nullable nonnull_int_ptr_ptr_1;
55typedef _Nonnull int *(^ block_type_3)(int, int);
56typedef _Nonnull int *(* function_pointer_type_3)(int, int);
57typedef _Nonnull int_ptr (^ block_type_4)(int, int);
58typedef _Nonnull int_ptr (* function_pointer_type_4)(int, int);
Nikola Smiljanicfa007282015-07-16 01:06:17 +000059typedef void (* function_pointer_type_5)(int_ptr _Nonnull);
Douglas Gregor261a89b2015-06-19 17:51:05 +000060
Douglas Gregoraea7afd2015-06-24 22:02:08 +000061void acceptFunctionPtr(_Nonnull int *(*)(void));
62void acceptBlockPtr(_Nonnull int *(^)(void));
Douglas Gregor261a89b2015-06-19 17:51:05 +000063
64void testBlockFunctionPtrNullability() {
65 float *fp;
Douglas Gregoraea7afd2015-06-24 22:02:08 +000066 fp = (function_pointer_type_3)0; // expected-warning{{from 'function_pointer_type_3' (aka 'int * _Nonnull (*)(int, int)')}}
67 fp = (block_type_3)0; // expected-error{{from incompatible type 'block_type_3' (aka 'int * _Nonnull (^)(int, int)')}}
Nikola Smiljanicfa007282015-07-16 01:06:17 +000068 fp = (function_pointer_type_4)0; // expected-warning{{from 'function_pointer_type_4' (aka 'int * _Nonnull (*)(int, int)')}}
69 fp = (function_pointer_type_5)0; // expected-warning{{from 'function_pointer_type_5' (aka 'void (*)(int * _Nonnull)')}}
Douglas Gregoraea7afd2015-06-24 22:02:08 +000070 fp = (block_type_4)0; // expected-error{{from incompatible type 'block_type_4' (aka 'int_ptr _Nonnull (^)(int, int)')}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000071
72 acceptFunctionPtr(0); // no-warning
73 acceptBlockPtr(0); // no-warning
74}
75
76// Moving nullability where it creates a conflict.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000077typedef _Nonnull int * _Nullable * conflict_int_ptr_ptr_2; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000078
79// Nullability is not part of the canonical type.
Douglas Gregoraea7afd2015-06-24 22:02:08 +000080typedef int * _Nonnull ambiguous_int_ptr;
Sunil Srivastavabf010802016-04-27 19:53:03 +000081// Redefining a typedef is a C11 feature.
82#if __STDC_VERSION__ > 199901L
Douglas Gregor261a89b2015-06-19 17:51:05 +000083typedef int * ambiguous_int_ptr;
Douglas Gregoraea7afd2015-06-24 22:02:08 +000084typedef int * _Nullable ambiguous_int_ptr;
Sunil Srivastavabf010802016-04-27 19:53:03 +000085#endif
Douglas Gregor261a89b2015-06-19 17:51:05 +000086
87// Printing of nullability.
88float f;
Douglas Gregoraea7afd2015-06-24 22:02:08 +000089int * _Nonnull ip_1 = &f; // expected-warning{{incompatible pointer types initializing 'int * _Nonnull' with an expression of type 'float *'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000090
91// Check printing of nullability specifiers.
92void printing_nullability(void) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +000093 int * _Nonnull iptr;
94 float *fptr = iptr; // expected-warning{{incompatible pointer types initializing 'float *' with an expression of type 'int * _Nonnull'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000095
Douglas Gregoraea7afd2015-06-24 22:02:08 +000096 int * * _Nonnull iptrptr;
97 float **fptrptr = iptrptr; // expected-warning{{incompatible pointer types initializing 'float **' with an expression of type 'int ** _Nonnull'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +000098
Douglas Gregoraea7afd2015-06-24 22:02:08 +000099 int * _Nullable * _Nonnull iptrptr2;
100 float * *fptrptr2 = iptrptr2; // expected-warning{{incompatible pointer types initializing 'float **' with an expression of type 'int * _Nullable * _Nonnull'}}
Douglas Gregor261a89b2015-06-19 17:51:05 +0000101}
Douglas Gregorb4866e82015-06-19 18:13:19 +0000102
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000103// Check passing null to a _Nonnull argument.
104void accepts_nonnull_1(_Nonnull int *ptr);
105void (*accepts_nonnull_2)(_Nonnull int *ptr);
106void (^accepts_nonnull_3)(_Nonnull int *ptr);
Douglas Gregorb4866e82015-06-19 18:13:19 +0000107
108void test_accepts_nonnull_null_pointer_literal() {
109 accepts_nonnull_1(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
110 accepts_nonnull_2(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
111 accepts_nonnull_3(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
112}
113
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000114// Check returning nil from a _Nonnull-returning function.
115_Nonnull int *returns_int_ptr(int x) {
Douglas Gregorb4866e82015-06-19 18:13:19 +0000116 if (x) {
117 return 0; // expected-warning{{null returned from function that requires a non-null return value}}
118 }
119
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000120 return (_Nonnull int *)0;
Douglas Gregorb4866e82015-06-19 18:13:19 +0000121}
122
123// Check nullable-to-nonnull conversions.
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000124void nullable_to_nonnull(_Nullable int *ptr) {
Douglas Gregorb4866e82015-06-19 18:13:19 +0000125 int *a = ptr; // okay
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000126 _Nonnull int *b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
George Burgess IV8d141e02015-12-14 22:00:49 +0000127 b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
128
129 accepts_nonnull_1(ptr); // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
Douglas Gregorb4866e82015-06-19 18:13:19 +0000130}
Akira Hatanaka73118fd2016-07-20 01:48:11 +0000131
132// Check nullability of conditional expressions.
133void conditional_expr(int c) {
134 int * _Nonnull p;
135 int * _Nonnull nonnullP;
136 int * _Nullable nullableP;
137 int * _Null_unspecified unspecifiedP;
138 int *noneP;
139
140 p = c ? nonnullP : nonnullP;
141 p = c ? nonnullP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
142 p = c ? nonnullP : unspecifiedP;
143 p = c ? nonnullP : noneP;
144 p = c ? nullableP : nonnullP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
145 p = c ? nullableP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
146 p = c ? nullableP : unspecifiedP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
147 p = c ? nullableP : noneP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
148 p = c ? unspecifiedP : nonnullP;
149 p = c ? unspecifiedP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
150 p = c ? unspecifiedP : unspecifiedP;
151 p = c ? unspecifiedP : noneP;
152 p = c ? noneP : nonnullP;
153 p = c ? noneP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
154 p = c ? noneP : unspecifiedP;
155 p = c ? noneP : noneP;
156
157 // Check that we don't remove all sugar when creating a new QualType for the
158 // conditional expression.
159 typedef int *IntP;
160 typedef IntP _Nonnull NonnullIntP0;
161 typedef NonnullIntP0 _Nonnull NonnullIntP1;
162 typedef IntP _Nullable NullableIntP0;
163 typedef NullableIntP0 _Nullable NullableIntP1;
164 NonnullIntP1 nonnullP2;
165 NullableIntP1 nullableP2;
166
167 p = c ? nonnullP2 : nonnullP2;
168 p = c ? nonnullP2 : nullableP2; // expected-warning{{implicit conversion from nullable pointer 'IntP _Nullable' (aka 'int *') to non-nullable pointer type 'int * _Nonnull'}}
169 p = c ? nullableP2 : nonnullP2; // expected-warning{{implicit conversion from nullable pointer 'NullableIntP1' (aka 'int *') to non-nullable pointer type 'int * _Nonnull'}}
170 p = c ? nullableP2 : nullableP2; // expected-warning{{implicit conversion from nullable pointer 'NullableIntP1' (aka 'int *') to non-nullable pointer type 'int * _Nonnull'}}
171}
172
173// Check nullability of binary conditional expressions.
174void binary_conditional_expr() {
175 int * _Nonnull p;
176 int * _Nonnull nonnullP;
177 int * _Nullable nullableP;
178 int * _Null_unspecified unspecifiedP;
179 int *noneP;
180
181 p = nonnullP ?: nonnullP;
182 p = nonnullP ?: nullableP;
183 p = nonnullP ?: unspecifiedP;
184 p = nonnullP ?: noneP;
185 p = nullableP ?: nonnullP;
186 p = nullableP ?: nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
187 p = nullableP ?: unspecifiedP;
188 p = nullableP ?: noneP;
189 p = unspecifiedP ?: nonnullP;
190 p = unspecifiedP ?: nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
191 p = unspecifiedP ?: unspecifiedP;
192 p = unspecifiedP ?: noneP;
193 p = noneP ?: nonnullP;
194 p = noneP ?: nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}
195 p = noneP ?: unspecifiedP;
196 p = noneP ?: noneP;
197}