1 /* Test for scanf formats. Formats using C99 features, including cases
2 where C99 specifies some aspect of the format to be ignored or where
3 the behaviour is undefined.
5 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
6 /* { dg-do compile } */
7 /* { dg-options "-std=iso9899:1999 -pedantic -Wformat" } */
9 typedef __WCHAR_TYPE__ wchar_t;
10 typedef __SIZE_TYPE__ size_t;
11 typedef __PTRDIFF_TYPE__ ptrdiff_t;
13 /* Kludges to get types corresponding to size_t and ptrdiff_t. */
14 #define unsigned signed
15 typedef __SIZE_TYPE__ signed_size_t;
17 #define signed /* Type might or might not have explicit 'signed'. */
18 typedef unsigned __PTRDIFF_TYPE__ unsigned_ptrdiff_t;
21 /* These next definitions are kludges. When GCC has a <stdint.h> it
25 #if INT_MAX == LLONG_MAX
27 #elif LONG_MAX == LLONG_MAX
28 typedef long intmax_t;
30 typedef long long intmax_t;
32 #if UINT_MAX == ULLONG_MAX
33 typedef unsigned int uintmax_t;
34 #elif ULONG_MAX == ULLONG_MAX
35 typedef unsigned long uintmax_t;
37 typedef unsigned long long uintmax_t;
40 extern int scanf (const char *, ...);
43 foo (int *ip, unsigned int *uip, short int *hp, unsigned short int *uhp,
44 signed char *hhp, unsigned char *uhhp, long int *lp,
45 unsigned long int *ulp, float *fp, double *dp, long double *ldp, char *s,
46 void **pp, int *n, long long *llp, unsigned long long *ullp, wchar_t *ls,
47 short int *hn, signed char *hhn, long int *ln, long long int *lln,
48 intmax_t *jp, uintmax_t *ujp, intmax_t *jn, size_t *zp,
49 signed_size_t *szp, signed_size_t *zn, ptrdiff_t *tp,
50 unsigned_ptrdiff_t *utp, ptrdiff_t *tn)
52 /* See ISO/IEC 9899:1999 (E) subclause 7.19.6.2 (pages 281-288).
53 We do not repeat here most of the checks for correct C90 formats
54 or completely broken formats.
56 /* Valid, invalid and silly assignment-suppression
57 and width constructions.
59 scanf ("%*d%*i%*o%*u%*x%*X%*a%*A%*e%*E%*f%*F%*g%*G%*s%*[abc]%*c%*p");
60 scanf ("%*2d%*8s%*3c");
61 scanf ("%*n", n); /* { dg-warning "suppress" "suppression of %n" } */
62 scanf ("%*hd"); /* { dg-warning "together" "suppression with length" } */
63 scanf ("%2d%3i%4o%5u%6x%7X%8a%9A%10e%11E%12f%13F%14g%15G%16s%3[abc]%4c%5p",
64 ip, ip, uip, uip, uip, uip, fp, fp, fp, fp, fp, fp, fp, fp,
66 scanf ("%0d", ip); /* { dg-warning "width" "warning for zero width" } */
67 scanf ("%3n", n); /* { dg-warning "width" "width with %n" } */
68 /* Valid and invalid %h, %hh, %l, %ll, %j, %z, %t, %L constructions. */
69 scanf ("%hd%hi%ho%hu%hx%hX%hn", hp, hp, uhp, uhp, uhp, uhp, hn);
70 scanf ("%ha", fp); /* { dg-warning "length" "bad use of %h" } */
71 scanf ("%hA", fp); /* { dg-warning "length" "bad use of %h" } */
72 scanf ("%he", fp); /* { dg-warning "length" "bad use of %h" } */
73 scanf ("%hE", fp); /* { dg-warning "length" "bad use of %h" } */
74 scanf ("%hf", fp); /* { dg-warning "length" "bad use of %h" } */
75 scanf ("%hF", fp); /* { dg-warning "length" "bad use of %h" } */
76 scanf ("%hg", fp); /* { dg-warning "length" "bad use of %h" } */
77 scanf ("%hG", fp); /* { dg-warning "length" "bad use of %h" } */
78 scanf ("%hs", s); /* { dg-warning "length" "bad use of %h" } */
79 scanf ("%h[ac]", s); /* { dg-warning "length" "bad use of %h" } */
80 scanf ("%hc", s); /* { dg-warning "length" "bad use of %h" } */
81 scanf ("%hp", pp); /* { dg-warning "length" "bad use of %h" } */
82 scanf ("%hhd%hhi%hho%hhu%hhx%hhX%hhn", hhp, hhp, uhhp, uhhp, uhhp, uhhp,
84 scanf ("%hha", fp); /* { dg-warning "length" "bad use of %hh" } */
85 scanf ("%hhA", fp); /* { dg-warning "length" "bad use of %hh" } */
86 scanf ("%hhe", fp); /* { dg-warning "length" "bad use of %hh" } */
87 scanf ("%hhE", fp); /* { dg-warning "length" "bad use of %hh" } */
88 scanf ("%hhf", fp); /* { dg-warning "length" "bad use of %hh" } */
89 scanf ("%hhF", fp); /* { dg-warning "length" "bad use of %hh" } */
90 scanf ("%hhg", fp); /* { dg-warning "length" "bad use of %hh" } */
91 scanf ("%hhG", fp); /* { dg-warning "length" "bad use of %hh" } */
92 scanf ("%hhs", s); /* { dg-warning "length" "bad use of %hh" } */
93 scanf ("%hh[ac]", s); /* { dg-warning "length" "bad use of %hh" } */
94 scanf ("%hhc", s); /* { dg-warning "length" "bad use of %hh" } */
95 scanf ("%hhp", pp); /* { dg-warning "length" "bad use of %hh" } */
96 scanf ("%ld%li%lo%lu%lx%lX%ln", lp, lp, ulp, ulp, ulp, ulp, ln);
97 scanf ("%la%lA%le%lE%lf%lF%lg%lG", dp, dp, dp, dp, dp, dp, dp, dp);
98 scanf ("%lp", pp); /* { dg-warning "length" "bad use of %l" } */
100 scanf ("%l[ac]", ls);
102 scanf ("%lld%lli%llo%llu%llx%llX%lln", llp, llp, ullp, ullp, ullp, ullp,
104 scanf ("%lla", fp); /* { dg-warning "length" "bad use of %ll" } */
105 scanf ("%llA", fp); /* { dg-warning "length" "bad use of %ll" } */
106 scanf ("%lle", fp); /* { dg-warning "length" "bad use of %ll" } */
107 scanf ("%llE", fp); /* { dg-warning "length" "bad use of %ll" } */
108 scanf ("%llf", fp); /* { dg-warning "length" "bad use of %ll" } */
109 scanf ("%llF", fp); /* { dg-warning "length" "bad use of %ll" } */
110 scanf ("%llg", fp); /* { dg-warning "length" "bad use of %ll" } */
111 scanf ("%llG", fp); /* { dg-warning "length" "bad use of %ll" } */
112 scanf ("%lls", s); /* { dg-warning "length" "bad use of %ll" } */
113 scanf ("%ll[ac]", s); /* { dg-warning "length" "bad use of %ll" } */
114 scanf ("%llc", s); /* { dg-warning "length" "bad use of %ll" } */
115 scanf ("%llp", pp); /* { dg-warning "length" "bad use of %ll" } */
116 scanf ("%jd%ji%jo%ju%jx%jX%jn", jp, jp, ujp, ujp, ujp, ujp, jn); /* { dg-bogus "length" "bogus %j warning" { target *-*-* } } */
117 scanf ("%ja", fp); /* { dg-warning "length" "bad use of %j" } */
118 scanf ("%jA", fp); /* { dg-warning "length" "bad use of %j" } */
119 scanf ("%je", fp); /* { dg-warning "length" "bad use of %j" } */
120 scanf ("%jE", fp); /* { dg-warning "length" "bad use of %j" } */
121 scanf ("%jf", fp); /* { dg-warning "length" "bad use of %j" } */
122 scanf ("%jF", fp); /* { dg-warning "length" "bad use of %j" } */
123 scanf ("%jg", fp); /* { dg-warning "length" "bad use of %j" } */
124 scanf ("%jG", fp); /* { dg-warning "length" "bad use of %j" } */
125 scanf ("%js", s); /* { dg-warning "length" "bad use of %j" } */
126 scanf ("%j[ac]", s); /* { dg-warning "length" "bad use of %j" } */
127 scanf ("%jc", s); /* { dg-warning "length" "bad use of %j" } */
128 scanf ("%jp", pp); /* { dg-warning "length" "bad use of %j" } */
129 scanf ("%zd%zi%zo%zu%zx%zX%zn", szp, szp, zp, zp, zp, zp, zn);
130 scanf ("%za", fp); /* { dg-warning "length" "bad use of %z" } */
131 scanf ("%zA", fp); /* { dg-warning "length" "bad use of %z" } */
132 scanf ("%ze", fp); /* { dg-warning "length" "bad use of %z" } */
133 scanf ("%zE", fp); /* { dg-warning "length" "bad use of %z" } */
134 scanf ("%zf", fp); /* { dg-warning "length" "bad use of %z" } */
135 scanf ("%zF", fp); /* { dg-warning "length" "bad use of %z" } */
136 scanf ("%zg", fp); /* { dg-warning "length" "bad use of %z" } */
137 scanf ("%zG", fp); /* { dg-warning "length" "bad use of %z" } */
138 scanf ("%zs", s); /* { dg-warning "length" "bad use of %z" } */
139 scanf ("%z[ac]", s); /* { dg-warning "length" "bad use of %z" } */
140 scanf ("%zc", s); /* { dg-warning "length" "bad use of %z" } */
141 scanf ("%zp", pp); /* { dg-warning "length" "bad use of %z" } */
142 scanf ("%td%ti%to%tu%tx%tX%tn", tp, tp, utp, utp, utp, utp, tn);
143 scanf ("%ta", fp); /* { dg-warning "length" "bad use of %t" } */
144 scanf ("%tA", fp); /* { dg-warning "length" "bad use of %t" } */
145 scanf ("%te", fp); /* { dg-warning "length" "bad use of %t" } */
146 scanf ("%tE", fp); /* { dg-warning "length" "bad use of %t" } */
147 scanf ("%tf", fp); /* { dg-warning "length" "bad use of %t" } */
148 scanf ("%tF", fp); /* { dg-warning "length" "bad use of %t" } */
149 scanf ("%tg", fp); /* { dg-warning "length" "bad use of %t" } */
150 scanf ("%tG", fp); /* { dg-warning "length" "bad use of %t" } */
151 scanf ("%ts", s); /* { dg-warning "length" "bad use of %t" } */
152 scanf ("%t[ac]", s); /* { dg-warning "length" "bad use of %t" } */
153 scanf ("%tc", s); /* { dg-warning "length" "bad use of %t" } */
154 scanf ("%tp", pp); /* { dg-warning "length" "bad use of %t" } */
155 scanf ("%La%LA%Le%LE%Lf%LF%Lg%LG", ldp, ldp, ldp, ldp, ldp, ldp, ldp, ldp);
156 scanf ("%Ld", llp); /* { dg-warning "does not support" "bad use of %L" } */
157 scanf ("%Li", llp); /* { dg-warning "does not support" "bad use of %L" } */
158 scanf ("%Lo", ullp); /* { dg-warning "does not support" "bad use of %L" } */
159 scanf ("%Lu", ullp); /* { dg-warning "does not support" "bad use of %L" } */
160 scanf ("%Lx", ullp); /* { dg-warning "does not support" "bad use of %L" } */
161 scanf ("%LX", ullp); /* { dg-warning "does not support" "bad use of %L" } */
162 scanf ("%Ls", s); /* { dg-warning "length" "bad use of %L" } */
163 scanf ("%L[ac]", s); /* { dg-warning "length" "bad use of %L" } */
164 scanf ("%Lc", s); /* { dg-warning "length" "bad use of %L" } */
165 scanf ("%Lp", pp); /* { dg-warning "length" "bad use of %L" } */
166 scanf ("%Ln", n); /* { dg-warning "length" "bad use of %L" } */
167 /* Valid uses of each bare conversion. */
168 scanf ("%d%i%o%u%x%X%a%A%e%E%f%F%g%G%s%[abc]%c%p%n%%", ip, ip, uip, uip, uip,
169 uip, fp, fp, fp, fp, fp, fp, fp, fp, s, s, s, pp, n);
170 /* Assert that %as is not treated as an extension in C99 mode. */
173 /* Tests for bad argument types: pointer target sign with %hh. */
174 scanf ("%hhd", uhhp); /* { dg-warning "format" "%hhd sign" } */
175 scanf ("%hhu", hhp); /* { dg-warning "format" "%hhu sign" } */