1 /* Test for printf formats. Formats using C99 features, including cases
2 where C99 specifies some aspect of the format to be ignored or where
3 the behavior is undefined.
5 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
6 /* { dg-do compile } */
7 /* { dg-options "-std=iso9899:1999 -pedantic -Wformat" } */
12 foo (int i
, unsigned int u
, double d
, char *s
, void *p
, int *n
,
13 long double ld
, wint_t lc
, wchar_t *ls
, long long int ll
,
14 unsigned long long int ull
, signed char *ss
, unsigned char *us
,
15 long long int *lln
, intmax_t j
, uintmax_t uj
, intmax_t *jn
,
16 size_t z
, signed_size_t sz
, signed_size_t
*zn
,
17 ptrdiff_t t
, ptrdiff_t *tn
)
19 /* See ISO/IEC 9899:1999 (E) subclause 7.19.6.1 (pages 273-281).
20 We do not repeat here most of the checks for correct C90 formats
21 or completely broken formats.
23 /* Valid and invalid %h, %hh, %l, %ll, %j, %z, %t, %L constructions. */
24 printf ("%hf", d
); /* { dg-warning "length" "bad use of %h" } */
25 printf ("%hF", d
); /* { dg-warning "length" "bad use of %h" } */
26 printf ("%he", d
); /* { dg-warning "length" "bad use of %h" } */
27 printf ("%hE", d
); /* { dg-warning "length" "bad use of %h" } */
28 printf ("%hg", d
); /* { dg-warning "length" "bad use of %h" } */
29 printf ("%hG", d
); /* { dg-warning "length" "bad use of %h" } */
30 printf ("%ha", d
); /* { dg-warning "length" "bad use of %h" } */
31 printf ("%hA", d
); /* { dg-warning "length" "bad use of %h" } */
32 printf ("%hc", i
); /* { dg-warning "length" "bad use of %h" } */
33 printf ("%hs", s
); /* { dg-warning "length" "bad use of %h" } */
34 printf ("%hp", p
); /* { dg-warning "length" "bad use of %h" } */
35 printf ("%hhd%hhi%hho%hhu%hhx%hhX", i
, i
, u
, u
, u
, u
);
37 printf ("%hhf", d
); /* { dg-warning "length" "bad use of %hh" } */
38 printf ("%hhF", d
); /* { dg-warning "length" "bad use of %hh" } */
39 printf ("%hhe", d
); /* { dg-warning "length" "bad use of %hh" } */
40 printf ("%hhE", d
); /* { dg-warning "length" "bad use of %hh" } */
41 printf ("%hhg", d
); /* { dg-warning "length" "bad use of %hh" } */
42 printf ("%hhG", d
); /* { dg-warning "length" "bad use of %hh" } */
43 printf ("%hha", d
); /* { dg-warning "length" "bad use of %hh" } */
44 printf ("%hhA", d
); /* { dg-warning "length" "bad use of %hh" } */
45 printf ("%hhc", i
); /* { dg-warning "length" "bad use of %hh" } */
46 printf ("%hhs", s
); /* { dg-warning "length" "bad use of %hh" } */
47 printf ("%hhp", p
); /* { dg-warning "length" "bad use of %hh" } */
50 printf ("%lf%lF%le%lE%lg%lG%la%lA", d
, d
, d
, d
, d
, d
, d
, d
);
51 printf ("%lp", p
); /* { dg-warning "length|C" "bad use of %l" } */
52 printf ("%lld%lli%llo%llu%llx%llX", ll
, ll
, ull
, ull
, ull
, ull
);
54 printf ("%llf", d
); /* { dg-warning "length" "bad use of %ll" } */
55 printf ("%llF", d
); /* { dg-warning "length" "bad use of %ll" } */
56 printf ("%lle", d
); /* { dg-warning "length" "bad use of %ll" } */
57 printf ("%llE", d
); /* { dg-warning "length" "bad use of %ll" } */
58 printf ("%llg", d
); /* { dg-warning "length" "bad use of %ll" } */
59 printf ("%llG", d
); /* { dg-warning "length" "bad use of %ll" } */
60 printf ("%lla", d
); /* { dg-warning "length" "bad use of %ll" } */
61 printf ("%llA", d
); /* { dg-warning "length" "bad use of %ll" } */
62 printf ("%llc", i
); /* { dg-warning "length" "bad use of %ll" } */
63 printf ("%lls", s
); /* { dg-warning "length" "bad use of %ll" } */
64 printf ("%llp", p
); /* { dg-warning "length" "bad use of %ll" } */
65 printf ("%jd%ji%jo%ju%jx%jX", j
, j
, uj
, uj
, uj
, uj
); /* { dg-bogus "length" "bogus %j warning" } */
66 printf ("%jn", jn
); /* { dg-bogus "length" "bogus %j warning" } */
67 printf ("%jf", d
); /* { dg-warning "length" "bad use of %j" } */
68 printf ("%jF", d
); /* { dg-warning "length" "bad use of %j" } */
69 printf ("%je", d
); /* { dg-warning "length" "bad use of %j" } */
70 printf ("%jE", d
); /* { dg-warning "length" "bad use of %j" } */
71 printf ("%jg", d
); /* { dg-warning "length" "bad use of %j" } */
72 printf ("%jG", d
); /* { dg-warning "length" "bad use of %j" } */
73 printf ("%ja", d
); /* { dg-warning "length" "bad use of %j" } */
74 printf ("%jA", d
); /* { dg-warning "length" "bad use of %j" } */
75 printf ("%jc", i
); /* { dg-warning "length" "bad use of %j" } */
76 printf ("%js", s
); /* { dg-warning "length" "bad use of %j" } */
77 printf ("%jp", p
); /* { dg-warning "length" "bad use of %j" } */
78 printf ("%zd%zi%zo%zu%zx%zX", sz
, sz
, z
, z
, z
, z
);
80 printf ("%zf", d
); /* { dg-warning "length" "bad use of %z" } */
81 printf ("%zF", d
); /* { dg-warning "length" "bad use of %z" } */
82 printf ("%ze", d
); /* { dg-warning "length" "bad use of %z" } */
83 printf ("%zE", d
); /* { dg-warning "length" "bad use of %z" } */
84 printf ("%zg", d
); /* { dg-warning "length" "bad use of %z" } */
85 printf ("%zG", d
); /* { dg-warning "length" "bad use of %z" } */
86 printf ("%za", d
); /* { dg-warning "length" "bad use of %z" } */
87 printf ("%zA", d
); /* { dg-warning "length" "bad use of %z" } */
88 printf ("%zc", i
); /* { dg-warning "length" "bad use of %z" } */
89 printf ("%zs", s
); /* { dg-warning "length" "bad use of %z" } */
90 printf ("%zp", p
); /* { dg-warning "length" "bad use of %z" } */
91 printf ("%td%ti%to%tu%tx%tX", t
, t
, t
, t
, t
, t
);
93 printf ("%tf", d
); /* { dg-warning "length" "bad use of %t" } */
94 printf ("%tF", d
); /* { dg-warning "length" "bad use of %t" } */
95 printf ("%te", d
); /* { dg-warning "length" "bad use of %t" } */
96 printf ("%tE", d
); /* { dg-warning "length" "bad use of %t" } */
97 printf ("%tg", d
); /* { dg-warning "length" "bad use of %t" } */
98 printf ("%tG", d
); /* { dg-warning "length" "bad use of %t" } */
99 printf ("%ta", d
); /* { dg-warning "length" "bad use of %t" } */
100 printf ("%tA", d
); /* { dg-warning "length" "bad use of %t" } */
101 printf ("%tc", i
); /* { dg-warning "length" "bad use of %t" } */
102 printf ("%ts", s
); /* { dg-warning "length" "bad use of %t" } */
103 printf ("%tp", p
); /* { dg-warning "length" "bad use of %t" } */
104 printf ("%Le%LE%Lf%LF%Lg%LG%La%LA", ld
, ld
, ld
, ld
, ld
, ld
, ld
, ld
);
105 /* These next six are accepted by GCC as referring to long long,
106 but -pedantic correctly warns.
108 printf ("%Ld", ll
); /* { dg-warning "does not support" "bad use of %L" } */
109 printf ("%Li", ll
); /* { dg-warning "does not support" "bad use of %L" } */
110 printf ("%Lo", ull
); /* { dg-warning "does not support" "bad use of %L" } */
111 printf ("%Lu", ull
); /* { dg-warning "does not support" "bad use of %L" } */
112 printf ("%Lx", ull
); /* { dg-warning "does not support" "bad use of %L" } */
113 printf ("%LX", ull
); /* { dg-warning "does not support" "bad use of %L" } */
114 printf ("%Lc", i
); /* { dg-warning "length" "bad use of %L" } */
115 printf ("%Ls", s
); /* { dg-warning "length" "bad use of %L" } */
116 printf ("%Lp", p
); /* { dg-warning "length" "bad use of %L" } */
117 printf ("%Ln", n
); /* { dg-warning "length" "bad use of %L" } */
118 /* Valid uses of each bare conversion. */
119 printf ("%d%i%o%u%x%X%f%F%e%E%g%G%a%A%c%s%p%n%%", i
, i
, u
, u
, u
, u
,
120 d
, d
, d
, d
, d
, d
, d
, d
, i
, s
, p
, n
);
121 /* Uses of the - flag (valid on all non-%, non-n conversions). */
122 printf ("%-d%-i%-o%-u%-x%-X%-f%-F%-e%-E%-g%-G%-a%-A%-c%-s%-p", i
, i
,
123 u
, u
, u
, u
, d
, d
, d
, d
, d
, d
, d
, d
, i
, s
, p
);
124 printf ("%-n", n
); /* { dg-warning "flag" "bad use of %-n" } */
125 /* Uses of the + flag (valid on signed conversions only). */
126 printf ("%+d%+i%+f%+F%+e%+E%+g%+G%+a%+A\n", i
, i
, d
, d
, d
, d
, d
, d
, d
, d
);
127 printf ("%+o", u
); /* { dg-warning "flag" "bad use of + flag" } */
128 printf ("%+u", u
); /* { dg-warning "flag" "bad use of + flag" } */
129 printf ("%+x", u
); /* { dg-warning "flag" "bad use of + flag" } */
130 printf ("%+X", u
); /* { dg-warning "flag" "bad use of + flag" } */
131 printf ("%+c", i
); /* { dg-warning "flag" "bad use of + flag" } */
132 printf ("%+s", s
); /* { dg-warning "flag" "bad use of + flag" } */
133 printf ("%+p", p
); /* { dg-warning "flag" "bad use of + flag" } */
134 printf ("%+n", n
); /* { dg-warning "flag" "bad use of + flag" } */
135 /* Uses of the space flag (valid on signed conversions only, and ignored
138 printf ("% +d", i
); /* { dg-warning "use of both|ignored" "use of space and + flags" } */
139 printf ("%+ d", i
); /* { dg-warning "use of both|ignored" "use of space and + flags" } */
140 printf ("% d% i% f% F% e% E% g% G% a% A\n", i
, i
, d
, d
, d
, d
, d
, d
, d
, d
);
141 printf ("% o", u
); /* { dg-warning "flag" "bad use of space flag" } */
142 printf ("% u", u
); /* { dg-warning "flag" "bad use of space flag" } */
143 printf ("% x", u
); /* { dg-warning "flag" "bad use of space flag" } */
144 printf ("% X", u
); /* { dg-warning "flag" "bad use of space flag" } */
145 printf ("% c", i
); /* { dg-warning "flag" "bad use of space flag" } */
146 printf ("% s", s
); /* { dg-warning "flag" "bad use of space flag" } */
147 printf ("% p", p
); /* { dg-warning "flag" "bad use of space flag" } */
148 printf ("% n", n
); /* { dg-warning "flag" "bad use of space flag" } */
149 /* Uses of the # flag. */
150 printf ("%#o%#x%#X%#e%#E%#f%#F%#g%#G%#a%#A", u
, u
, u
, d
, d
, d
, d
,
152 printf ("%#d", i
); /* { dg-warning "flag" "bad use of # flag" } */
153 printf ("%#i", i
); /* { dg-warning "flag" "bad use of # flag" } */
154 printf ("%#u", u
); /* { dg-warning "flag" "bad use of # flag" } */
155 printf ("%#c", i
); /* { dg-warning "flag" "bad use of # flag" } */
156 printf ("%#s", s
); /* { dg-warning "flag" "bad use of # flag" } */
157 printf ("%#p", p
); /* { dg-warning "flag" "bad use of # flag" } */
158 printf ("%#n", n
); /* { dg-warning "flag" "bad use of # flag" } */
159 /* Uses of the 0 flag. */
160 printf ("%08d%08i%08o%08u%08x%08X%08e%08E%08f%08F%08g%08G%08a%08A", i
, i
,
161 u
, u
, u
, u
, d
, d
, d
, d
, d
, d
, d
, d
);
162 printf ("%0c", i
); /* { dg-warning "flag" "bad use of 0 flag" } */
163 printf ("%0s", s
); /* { dg-warning "flag" "bad use of 0 flag" } */
164 printf ("%0p", p
); /* { dg-warning "flag" "bad use of 0 flag" } */
165 printf ("%0n", n
); /* { dg-warning "flag" "bad use of 0 flag" } */
166 /* 0 flag ignored with precision for certain types, not others. */
167 printf ("%08.5d", i
); /* { dg-warning "ignored" "0 flag ignored with precision" } */
168 printf ("%08.5i", i
); /* { dg-warning "ignored" "0 flag ignored with precision" } */
169 printf ("%08.5o", u
); /* { dg-warning "ignored" "0 flag ignored with precision" } */
170 printf ("%08.5u", u
); /* { dg-warning "ignored" "0 flag ignored with precision" } */
171 printf ("%08.5x", u
); /* { dg-warning "ignored" "0 flag ignored with precision" } */
172 printf ("%08.5X", u
); /* { dg-warning "ignored" "0 flag ignored with precision" } */
173 printf ("%08.5f%08.5F%08.5e%08.5E%08.5g%08.5G%08.5a%08.5A",
174 d
, d
, d
, d
, d
, d
, d
, d
);
175 /* 0 flag ignored with - flag. */
176 printf ("%-08d", i
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
177 printf ("%-08i", i
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
178 printf ("%-08o", u
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
179 printf ("%-08u", u
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
180 printf ("%-08x", u
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
181 printf ("%-08X", u
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
182 printf ("%-08e", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
183 printf ("%-08E", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
184 printf ("%-08f", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
185 printf ("%-08F", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
186 printf ("%-08g", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
187 printf ("%-08G", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
188 printf ("%-08a", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
189 printf ("%-08A", d
); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
190 /* Various tests of bad argument types. Mostly covered in c90-printf-1.c;
191 here just test for pointer target sign with %hhn. (Probably allowed
192 by the standard, but a bad idea, so GCC should diagnose if what
193 is used is not signed char *.)
195 printf ("%hhn", s
); /* { dg-warning "format" "%hhn plain char" } */
196 printf ("%hhn", us
); /* { dg-warning "format" "%hhn unsigned char" } */