brintos

brintos / llvm-project-archived public Read only

0
0
Text · 13.5 KiB · 5af473e Raw
253 lines · c
1// RUN: %clang_cc1 -fsyntax-only -fblocks -Wnullable-to-nonnull-conversion -Wno-nullability-declspec %s -verify2 3#if __has_feature(nullability)4#else5#  error nullability feature should be defined6#endif7 8typedef int * int_ptr;9 10// Parse nullability type specifiers.11// This note requires C11.12#if __STDC_VERSION__ > 199901L13// expected-note@+2{{'_Nonnull' specified here}}14#endif15typedef int * _Nonnull nonnull_int_ptr;16typedef int * _Nullable nullable_int_ptr;17typedef int * _Null_unspecified null_unspecified_int_ptr;18 19// Redundant nullability type specifiers.20typedef int * _Nonnull _Nonnull redundant_1; // expected-warning{{duplicate nullability specifier '_Nonnull'}}21 22// Conflicting nullability type specifiers.23typedef int * _Nonnull _Nullable conflicting_1; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}24typedef int * _Null_unspecified _Nonnull conflicting_2; // expected-error{{nullability specifier '_Nonnull' conflicts with existing specifier '_Null_unspecified'}}25 26// Redundant nullability specifiers via a typedef are okay.27typedef nonnull_int_ptr _Nonnull redundant_okay_1;28 29// Conflicting nullability specifiers via a typedef are not.30// Some of these errors require C11.31#if __STDC_VERSION__ > 199901L32typedef nonnull_int_ptr _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}33#endif34typedef nonnull_int_ptr nonnull_int_ptr_typedef;35#if __STDC_VERSION__ > 199901L36typedef nonnull_int_ptr_typedef _Nullable conflicting_2; // expected-error{{nullability specifier '_Nullable' conflicts with existing specifier '_Nonnull'}}37#endif38typedef nonnull_int_ptr_typedef nonnull_int_ptr_typedef_typedef;39typedef nonnull_int_ptr_typedef_typedef _Null_unspecified conflicting_3; // expected-error{{nullability specifier '_Null_unspecified' conflicts with existing specifier '_Nonnull'}}40 41// Nullability applies to all pointer types.42typedef int (* _Nonnull function_pointer_type_1)(int, int);43typedef int (^ _Nonnull block_type_1)(int, int);44 45// Nullability must be on a pointer type.46typedef int _Nonnull int_type_1; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}47 48// Nullability can move out to a pointer/block pointer declarator49// (with a suppressed warning).50typedef _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);59typedef void (* function_pointer_type_5)(int_ptr _Nonnull);60 61void acceptFunctionPtr(_Nonnull int *(*)(void));62void acceptBlockPtr(_Nonnull int *(^)(void));63 64void testBlockFunctionPtrNullability(void) {65  float *fp;66  fp = (function_pointer_type_3)0; // expected-error{{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)')}}68  fp = (function_pointer_type_4)0; // expected-error{{from 'function_pointer_type_4' (aka 'int * _Nonnull (*)(int, int)')}}69  fp = (function_pointer_type_5)0; // expected-error{{from 'function_pointer_type_5' (aka 'void (*)(int * _Nonnull)')}}70  fp = (block_type_4)0; // expected-error{{from incompatible type 'block_type_4' (aka 'int_ptr  _Nonnull (^)(int, int)')}}71 72  acceptFunctionPtr(0); // no-warning73  acceptBlockPtr(0); // no-warning74}75 76// Moving nullability where it creates a conflict.77typedef _Nonnull int * _Nullable *  conflict_int_ptr_ptr_2; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}78 79// Nullability is not part of the canonical type.80typedef int * _Nonnull ambiguous_int_ptr;81// Redefining a typedef is a C11 feature.82#if __STDC_VERSION__ > 199901L83typedef int * ambiguous_int_ptr;84typedef int * _Nullable ambiguous_int_ptr;85#endif86 87// Printing of nullability.88float f;89int * _Nonnull ip_1 = &f; // expected-error{{incompatible pointer types initializing 'int * _Nonnull' with an expression of type 'float *'}}90 91// Check printing of nullability specifiers.92void printing_nullability(void) {93  int * _Nonnull iptr;94  float *fptr = iptr; // expected-error{{incompatible pointer types initializing 'float *' with an expression of type 'int * _Nonnull'}}95 96  int * * _Nonnull iptrptr;97  float **fptrptr = iptrptr; // expected-error{{incompatible pointer types initializing 'float **' with an expression of type 'int ** _Nonnull'}}98 99  int * _Nullable * _Nonnull iptrptr2;100  float * *fptrptr2 = iptrptr2; // expected-error{{incompatible pointer types initializing 'float **' with an expression of type 'int * _Nullable * _Nonnull'}}101}102 103// 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);107 108void test_accepts_nonnull_null_pointer_literal(void) {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 114// Check returning nil from a _Nonnull-returning function.115_Nonnull int *returns_int_ptr(int x) {116  if (x) {117    return 0; // expected-warning{{null returned from function that requires a non-null return value}}118  }119 120  return (_Nonnull int *)0;121}122 123// Check nullable-to-nonnull conversions.124void nullable_to_nonnull(_Nullable int *ptr) {125  int *a = ptr; // okay126  _Nonnull int *b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}127  b = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}128  __auto_type _Nonnull c = ptr; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nullable _Nonnull'}}129 130  accepts_nonnull_1(ptr); // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}131}132 133// Check nullability of conditional expressions.134void conditional_expr(int c) {135  int * _Nonnull p;136  int * _Nonnull nonnullP;137  int * _Nullable nullableP;138  int * _Null_unspecified unspecifiedP;139  int *noneP;140 141  p = c ? nonnullP : nonnullP;142  p = c ? nonnullP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}143  p = c ? nonnullP : unspecifiedP;144  p = c ? nonnullP : noneP;145  p = c ? nullableP : nonnullP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}146  p = c ? nullableP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}147  p = c ? nullableP : unspecifiedP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}148  p = c ? nullableP : noneP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}149  p = c ? unspecifiedP : nonnullP;150  p = c ? unspecifiedP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}151  p = c ? unspecifiedP : unspecifiedP;152  p = c ? unspecifiedP : noneP;153  p = c ? noneP : nonnullP;154  p = c ? noneP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}155  p = c ? noneP : unspecifiedP;156  p = c ? noneP : noneP;157 158  // Check that we don't remove all sugar when creating a new QualType for the159  // conditional expression.160  typedef int *IntP;161  typedef IntP _Nonnull NonnullIntP0;162  typedef NonnullIntP0 _Nonnull NonnullIntP1;163  typedef IntP _Nullable NullableIntP0;164  typedef NullableIntP0 _Nullable NullableIntP1;165  NonnullIntP1 nonnullP2;166  NullableIntP1 nullableP2;167 168  p = c ? nonnullP2 : nonnullP2;169  p = c ? nonnullP2 : nullableP2; // expected-warning{{implicit conversion from nullable pointer 'IntP _Nullable' (aka 'int *') to non-nullable pointer type 'int * _Nonnull'}}170  p = c ? nullableP2 : nonnullP2; // expected-warning{{implicit conversion from nullable pointer 'IntP _Nullable' (aka 'int *') to non-nullable pointer type 'int * _Nonnull'}}171  p = c ? nullableP2 : nullableP2; // expected-warning{{implicit conversion from nullable pointer 'NullableIntP1' (aka 'int *') to non-nullable pointer type 'int * _Nonnull'}}172}173 174// Check nullability of binary conditional expressions.175void binary_conditional_expr(void) {176  int * _Nonnull p;177  int * _Nonnull nonnullP;178  int * _Nullable nullableP;179  int * _Null_unspecified unspecifiedP;180  int *noneP;181 182  p = nonnullP ?: nonnullP;183  p = nonnullP ?: nullableP;184  p = nonnullP ?: unspecifiedP;185  p = nonnullP ?: noneP;186  p = nullableP ?: nonnullP;187  p = nullableP ?: nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}188  p = nullableP ?: unspecifiedP;189  p = nullableP ?: noneP;190  p = unspecifiedP ?: nonnullP;191  p = unspecifiedP ?: nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}192  p = unspecifiedP ?: unspecifiedP;193  p = unspecifiedP ?: noneP;194  p = noneP ?: nonnullP;195  p = noneP ?: nullableP; // expected-warning{{implicit conversion from nullable pointer 'int * _Nullable' to non-nullable pointer type 'int * _Nonnull'}}196  p = noneP ?: unspecifiedP;197  p = noneP ?: noneP;198}199 200extern int GLOBAL_LENGTH;201 202// Nullability can appear on arrays when the arrays are in parameter lists.203void arrays(int ints[_Nonnull],204            void *ptrs[_Nullable],205            void **nestedPtrs[_Nullable],206            void * _Null_unspecified * _Nonnull nestedPtrs2[_Nullable],207            int fixedSize[_Nonnull 2],208            int staticSize[_Nonnull static 2],209            int staticSize2[static _Nonnull 2],210            int starSize[_Nonnull *],211            int vla[_Nonnull GLOBAL_LENGTH],212            void ** _Nullable reference);213void testDecayedType(void) {214  int produceAnErrorMessage = arrays; // expected-error {{incompatible pointer to integer conversion initializing 'int' with an expression of type 'void (int * _Nonnull, void ** _Nullable, void *** _Nullable, void * _Null_unspecified * _Nonnull * _Nullable, int * _Nonnull, int * _Nonnull, int * _Nonnull, int * _Nonnull, int * _Nonnull, void ** _Nullable)'}}215}216 217int notInFunction[_Nullable 3]; // expected-error {{nullability specifier '_Nullable' cannot be applied to non-pointer type 'int[3]'}}218 219void nestedArrays(int x[5][_Nonnull 1]) {} // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int[1]'}}220void nestedArrays2(int x[5][_Nonnull 1][2]) {} // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int[1][2]'}}221void nestedArraysOK(int x[_Nonnull 5][1]) {} // ok222 223void nullabilityOnBase(_Nonnull int x[1], // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}224                       int _Nonnull y[1]); // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int'}}225 226typedef int INTS[4];227typedef int BAD_INTS[_Nonnull 4]; // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int[4]'}}228 229void typedefTest(INTS _Nonnull x,230                 _Nonnull INTS xx,231                 INTS _Nonnull y[2], // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}232                 INTS z[_Nonnull 2]);233 234INTS _Nonnull x; // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}235_Nonnull INTS x; // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}236 237void arraysInBlocks(void) {238  typedef int INTS[4];239  void (^simple)(int [_Nonnull 2]) = ^(int x[_Nonnull 2]) {};240  simple(0); // expected-warning {{null passed to a callee that requires a non-null argument}}241  void (^nested)(void *_Nullable x[_Nonnull 2]) = ^(void *_Nullable x[_Nonnull 2]) {};242  nested(0); // expected-warning {{null passed to a callee that requires a non-null argument}}243  void (^nestedBad)(int x[2][_Nonnull 2]) = // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int[2]'}}244    ^(int x[2][_Nonnull 2]) {}; // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'int[2]'}}245 246  void (^withTypedef)(INTS _Nonnull) = ^(INTS _Nonnull x) {};247  withTypedef(0); // expected-warning {{null passed to a callee that requires a non-null argument}}248  void (^withTypedefBad)(INTS _Nonnull [2]) = // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}249      ^(INTS _Nonnull x[2]) {}; // expected-error {{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'INTS' (aka 'int[4]')}}250}251 252struct _Nullable NotCplusplusClass {}; // expected-error {{'_Nullable' attribute only applies to classes}}253