PR target/83368
[official-gcc.git] / gcc / testsuite / gcc.dg / builtins-nonnull.c
blob726f8e199da3c8bfd903afa3bb21b0fb974ca90f
1 /* PR c/17308 - nonnull attribute not as useful as it could be
2 PR c/78673 - sprintf missing attribute nonnull on destination argument
3 { dg-do "compile" }
4 { dg-additional-options "-O2 -Wnonnull -ftrack-macro-expansion=0 -std=c99" } */
6 #define va_list __builtin_va_list
8 typedef struct FILE FILE;
10 char* null (void)
12 return 0;
15 void sink (int, ...);
16 #define T(arg) sink (0, arg)
19 #define bzero __builtin_bzero
20 #define memcpy __builtin_memcpy
21 #define memmove __builtin_memmove
22 #define mempcpy __builtin_mempcpy
23 #define memset __builtin_memset
25 void test_memfuncs (void *s, unsigned n)
27 /* Bzero is not declared attribute nonnull (maybe it should be?)
28 but it's transformed into a call to memset() which is. */
29 bzero (null (), n); /* { dg-warning "argument 1 null where non-null expected" } */
31 T (memcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
32 T (memcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
34 T (memmove (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
35 T (memmove (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
37 T (mempcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
38 T (mempcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
40 T (memset (null (), 0, n)); /* { dg-warning "argument 1 null where non-null expected" } */
43 #undef memcpy
44 #undef memmove
45 #undef mempcpy
46 #undef memset
47 #define memcpy(d, s, n) __builtin___memcpy_chk (d, s, n, n)
48 #define memmove(d, s, n) __builtin___memmove_chk (d, s, n, n)
49 #define mempcpy(d, s, n) __builtin___mempcpy_chk (d, s, n, n)
50 #define memset(d, x, n) __builtin___memset_chk (d, x, n, n)
52 void test_memfuncs_chk (void *s, unsigned n)
54 T (memcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
55 T (memcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
57 T (memmove (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
58 T (memmove (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
60 T (mempcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
61 T (mempcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
63 T (memset (null (), 0, n)); /* { dg-warning "argument 1 null where non-null expected" } */
67 #define strcat __builtin_strcat
68 #define strchr __builtin_strchr
69 #define stpcpy __builtin_stpcpy
70 #define stpncpy __builtin_stpncpy
71 #define strcpy __builtin_strcpy
72 #define strncpy __builtin_strncpy
73 #define strlen __builtin_strlen
74 #define strncat __builtin_strncat
75 #define strstr __builtin_strstr
77 void test_strfuncs (char *s, unsigned n)
79 T (strcat (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
80 T (strcat (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
82 T (strchr (null (), 'x')); /* { dg-warning "argument 1 null where non-null expected" } */
84 T (stpcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
85 T (stpcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
87 T (stpncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
88 T (stpncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
90 T (strcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
91 T (strcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
93 T (strncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
94 T (strncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
96 T (strlen (null ())); /* { dg-warning "argument 1 null where non-null expected" } */
98 T (strncat (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
99 T (strncat (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
101 T (strstr (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
102 T (strstr (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
106 #undef strcat
107 #undef stpcpy
108 #undef stpncpy
109 #undef strcpy
110 #undef strncpy
111 #undef strncat
113 #define strcat(d, s) __builtin___strcat_chk (d, s, n)
114 #define stpcpy(d, s) __builtin___stpcpy_chk (d, s, n)
115 #define stpncpy(d, s, n) __builtin___stpncpy_chk (d, s, n, n)
116 #define strcpy(d, s) __builtin___strcpy_chk (d, s, n)
117 #define strncpy(d, s, n) __builtin___strncpy_chk (d, s, n, n)
118 #define strncat(d, s, n) __builtin___strncat_chk (d, s, n, n)
120 void test_strfuncs_chk (char *s, unsigned n)
122 T (strcat (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
123 T (strcat (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
125 T (strchr (null (), 'x')); /* { dg-warning "argument 1 null where non-null expected" } */
127 T (stpcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
128 T (stpcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
130 T (stpncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
131 T (stpncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
133 T (strcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */
134 T (strcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
136 T (strncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
137 T (strncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
139 T (strncat (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */
140 T (strncat (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */
144 #define fprintf __builtin_fprintf
145 #define fprintf_unlocked __builtin_fprintf_unlocked
146 #define vfprintf __builtin_vfprintf
147 #define printf __builtin_printf
148 #define printf_unlocked __builtin_printf_unlocked
149 #define vprintf __builtin_vprintf
150 #define sprintf __builtin_sprintf
151 #define snprintf __builtin_snprintf
152 #define vsprintf __builtin_vsprintf
153 #define vsnprintf __builtin_vsnprintf
155 void test_stdio_funcs (FILE *f, char *d, unsigned n, va_list va)
157 T (fprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */
158 T (fprintf (f, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
160 T (fprintf_unlocked (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */
161 T (fprintf_unlocked (f, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
163 T (vfprintf (null (), "%i", va));/* { dg-warning "argument 1 null where non-null expected" } */
164 T (vfprintf (f, null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */
166 T (vprintf (null (), va)); /* { dg-warning "argument 1 null where non-null expected" } */
168 T (printf (null ())); /* { dg-warning "argument 1 null where non-null expected" } */
169 T (printf_unlocked (null ())); /* { dg-warning "argument 1 null where non-null expected" } */
171 T (vprintf (null (), va)); /* { dg-warning "argument 1 null where non-null expected" } */
173 T (sprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */
174 T (sprintf (d, null ())); /* { dg-warning "argument 2 null where non-null expected" } */
176 T (snprintf (null (), n, "%i", 0));
177 T (snprintf (d, n, null ())); /* { dg-warning "argument 3 null where non-null expected" } */
179 T (vsprintf (null (), "%i", va)); /* { dg-warning "argument 1 null where non-null expected" } */
180 T (vsprintf (d, null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */
182 T (vsnprintf (null (), n, "%i", va));
183 T (vsnprintf (d, n, null (), va)); /* { dg-warning "argument 3 null where non-null expected" } */
186 #undef fprintf
187 #undef fprintf_unlocked
188 #undef vfprintf
189 #undef printf
190 #undef printf_unlocked
191 #undef vprintf
192 #undef sprintf
193 #undef snprintf
194 #undef vsprintf
195 #undef vsnprintf
197 #define fprintf(f, fmt, ...) \
198 __builtin___fprintf_chk (f, 0, fmt, __VA_ARGS__)
199 #define vfprintf(f, fmt, va) \
200 __builtin___vfprintf_chk (f, 0, fmt, va)
201 #define printf(fmt, ...) \
202 __builtin___printf_chk (0, fmt, __VA_ARGS__)
203 #define vprintf(fmt, va) \
204 __builtin___vprintf_chk (0, fmt, va)
205 #define sprintf(d, fmt, ... ) \
206 __builtin___sprintf_chk (d, 0, n, fmt, __VA_ARGS__)
207 #define snprintf(d, n, fmt, ...) \
208 __builtin___snprintf_chk (d, n, 0, n, fmt, __VA_ARGS__)
209 #define vsprintf(d, fmt, va) \
210 __builtin___vsprintf_chk (d, 0, n, fmt, va)
211 #define vsnprintf(d, n, fmt, va) \
212 __builtin___vsnprintf_chk (d, n, 0, n, fmt, va)
214 void test_stdio_funcs_chk (FILE *f, char *d, const char *fmt,
215 unsigned n, va_list va)
217 T (fprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */
218 T (fprintf (f, null (), 0)); /* { dg-warning "argument 3 null where non-null expected" } */
220 T (vfprintf (null (), "%i", va));/* { dg-warning "argument 1 null where non-null expected" } */
221 T (vfprintf (f, null (), va)); /* { dg-warning "argument 3 null where non-null expected" } */
223 T (vprintf (null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */
225 T (printf (null (), 0)); /* { dg-warning "argument 2 null where non-null expected" } */
227 T (vprintf (null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */
229 T (sprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */
230 T (sprintf (d, null (), 0)); /* { dg-warning "argument 4 null where non-null expected" } */
232 T (snprintf (null (), n, "%i", 0));
233 T (snprintf (d, n, null (), 0)); /* { dg-warning "argument 5 null where non-null expected" } */
235 T (vsprintf (null (), "%i", va)); /* { dg-warning "argument 1 null where non-null expected" } */
236 T (vsprintf (d, null (), va)); /* { dg-warning "argument 4 null where non-null expected" } */
238 T (vsnprintf (null (), n, "%i", va));
239 T (vsnprintf (d, n, null (), va)); /* { dg-warning "argument 5 null where non-null expected" } */