Clang Project

clang_source_code/test/SemaObjCXX/Inputs/nullability-consistency-arrays.h
1#include <stdarg.h>
2
3void firstThingInTheFileThatNeedsNullabilityIsAnArray(int ints[]);
4#if ARRAYS_CHECKED
5// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
6// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
7// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
8#endif
9
10int *secondThingInTheFileThatNeedsNullabilityIsAPointer;
11#if !ARRAYS_CHECKED
12// expected-warning@-2 {{pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)}}
13// expected-note@-3 {{insert '_Nullable' if the pointer may be null}}
14// expected-note@-4 {{insert '_Nonnull' if the pointer should never be null}}
15#endif
16
17int *_Nonnull triggerConsistencyWarnings;
18
19void test(
20    int ints[],
21#if ARRAYS_CHECKED
22// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
23// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
24// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
25#endif
26    void *ptrs[], // expected-warning {{pointer is missing a nullability type specifier}}
27// expected-note@-1 {{insert '_Nullable' if the pointer may be null}}
28// expected-note@-2 {{insert '_Nonnull' if the pointer should never be null}}
29#if ARRAYS_CHECKED
30// expected-warning@-4 {{array parameter is missing a nullability type specifier}}
31// expected-note@-5 {{insert '_Nullable' if the array parameter may be null}}
32// expected-note@-6 {{insert '_Nonnull' if the array parameter should never be null}}
33#endif
34    void **nestedPtrs[]); // expected-warning 2 {{pointer is missing a nullability type specifier}}
35// expected-note@-1 2 {{insert '_Nullable' if the pointer may be null}}
36// expected-note@-2 2 {{insert '_Nonnull' if the pointer should never be null}}
37#if ARRAYS_CHECKED
38// expected-warning@-4 {{array parameter is missing a nullability type specifier}}
39// expected-note@-5 {{insert '_Nullable' if the array parameter may be null}}
40// expected-note@-6 {{insert '_Nonnull' if the array parameter should never be null}}
41#endif
42
43void testArraysOK(
44    int ints[_Nonnull],
45    void *ptrs[_Nonnull], // expected-warning {{pointer is missing a nullability type specifier}}
46// expected-note@-1 {{insert '_Nullable' if the pointer may be null}}
47// expected-note@-2 {{insert '_Nonnull' if the pointer should never be null}}
48    void **nestedPtrs[_Nonnull]); // expected-warning 2 {{pointer is missing a nullability type specifier}}
49// expected-note@-1 2 {{insert '_Nullable' if the pointer may be null}}
50// expected-note@-2 2 {{insert '_Nonnull' if the pointer should never be null}}
51void testAllOK(
52    int ints[_Nonnull],
53    void * _Nullable ptrs[_Nonnull],
54    void * _Nullable * _Nullable nestedPtrs[_Nonnull]);
55
56void testVAList(va_list ok); // no warning
57
58#if __cplusplus
59// Carefully construct a test case such that if a platform's va_list is an array
60// or pointer type, it gets tested, but otherwise it does not.
61template<class T, class F>
62struct pointer_like_or { typedef F type; };
63template<class T, class F>
64struct pointer_like_or<T*, F> { typedef T *type; };
65template<class T, class F>
66struct pointer_like_or<T* const, F> { typedef T * const type; };
67template<class T, class F>
68struct pointer_like_or<T[], F> { typedef T type[]; };
69template<class T, class F, unsigned size>
70struct pointer_like_or<T[size], F> { typedef T type[size]; };
71
72void testVAListWithNullability(
73  pointer_like_or<va_list, void*>::type _Nonnull x); // no errors
74#endif
75
76void nestedArrays(int x[5][1]) {}
77#if ARRAYS_CHECKED
78// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
79// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
80// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
81#endif
82void nestedArraysOK(int x[_Nonnull 5][1]) {}
83
84#if !__cplusplus
85void staticOK(int x[static 5][1]){}
86#endif
87
88int globalArraysDoNotNeedNullability[5];
89
90typedef int INTS[4];
91
92void typedefTest(
93    INTS x,
94#if ARRAYS_CHECKED
95// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
96// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
97// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
98#endif
99    INTS _Nonnull x2,
100    _Nonnull INTS x3,
101    INTS y[2],
102#if ARRAYS_CHECKED
103// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
104// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
105// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
106#endif
107    INTS y2[_Nonnull 2]);
108
109
110#pragma clang assume_nonnull begin
111void testAssumeNonnull(
112  int ints[],
113#if ARRAYS_CHECKED
114// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
115// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
116// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
117#endif
118  void *ptrs[],
119#if ARRAYS_CHECKED
120// expected-warning@-2 {{array parameter is missing a nullability type specifier}}
121// expected-note@-3 {{insert '_Nullable' if the array parameter may be null}}
122// expected-note@-4 {{insert '_Nonnull' if the array parameter should never be null}}
123#endif
124  void **nestedPtrs[]); // expected-warning 2 {{pointer is missing a nullability type specifier}}
125// expected-note@-1 2 {{insert '_Nullable' if the pointer may be null}}
126// expected-note@-2 2 {{insert '_Nonnull' if the pointer should never be null}}
127#if ARRAYS_CHECKED
128// expected-warning@-4 {{array parameter is missing a nullability type specifier}}
129// expected-note@-5 {{insert '_Nullable' if the array parameter may be null}}
130// expected-note@-6 {{insert '_Nonnull' if the array parameter should never be null}}
131#endif
132
133void testAssumeNonnullAllOK(
134    int ints[_Nonnull],
135    void * _Nullable ptrs[_Nonnull],
136    void * _Nullable * _Nullable nestedPtrs[_Nonnull]);
137void testAssumeNonnullAllOK2(
138    int ints[_Nonnull],
139    void * ptrs[_Nonnull], // backwards-compatibility
140    void * _Nullable * _Nullable nestedPtrs[_Nonnull]);
141#pragma clang assume_nonnull end
142