1 /* Test for GCC diagnositc formats. */
2 /* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
3 /* { dg-do compile } */
4 /* { dg-options "-Wformat" } */
8 #define ATTRIBUTE_DIAG(F) __attribute__ ((__format__ (F, 1, 2))) __attribute__ ((__nonnull__));
10 /* Magic identifiers must be set before the attribute is used. */
12 typedef long long __gcc_host_wide_int__
;
14 typedef struct location_s
21 typedef union tree_node
*tree
;
23 extern int diag (const char *, ...) ATTRIBUTE_DIAG(__gcc_diag__
);
24 extern int tdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_tdiag__
);
25 extern int cdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_cdiag__
);
26 extern int cxxdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_cxxdiag__
);
29 foo (int i
, int i1
, int i2
, unsigned int u
, double d
, char *s
, void *p
,
30 int *n
, short int *hn
, long int l
, unsigned long int ul
,
31 long int *ln
, long double ld
, wint_t lc
, wchar_t *ls
, llong ll
,
32 ullong ull
, unsigned int *un
, const int *cn
, signed char *ss
,
33 unsigned char *us
, const signed char *css
, unsigned int u1
,
34 unsigned int u2
, location_t
*loc
, tree t1
, union tree_node
*t2
,
37 /* Acceptable C90 specifiers, flags and modifiers. */
42 diag ("%d%i%o%u%x%c%s%p%%", i
, i
, u
, u
, u
, i
, s
, p
);
43 tdiag ("%d%i%o%u%x%c%s%p%%", i
, i
, u
, u
, u
, i
, s
, p
);
44 cdiag ("%d%i%o%u%x%c%s%p%%", i
, i
, u
, u
, u
, i
, s
, p
);
45 cxxdiag ("%d%i%o%u%x%c%s%p%%", i
, i
, u
, u
, u
, i
, s
, p
);
46 diag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i
, i
, u
, u
, u
, i
, s
, p
);
47 tdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i
, i
, u
, u
, u
, i
, s
, p
);
48 cdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i
, i
, u
, u
, u
, i
, s
, p
);
49 cxxdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i
, i
, u
, u
, u
, i
, s
, p
);
50 diag ("%ld%li%lo%lu%lx", l
, l
, ul
, ul
, ul
);
51 tdiag ("%ld%li%lo%lu%lx", l
, l
, ul
, ul
, ul
);
52 cdiag ("%ld%li%lo%lu%lx", l
, l
, ul
, ul
, ul
);
53 cxxdiag ("%ld%li%lo%lu%lx", l
, l
, ul
, ul
, ul
);
54 diag ("%lld%lli%llo%llu%llx", ll
, ll
, ull
, ull
, ull
);
55 tdiag ("%lld%lli%llo%llu%llx", ll
, ll
, ull
, ull
, ull
);
56 cdiag ("%lld%lli%llo%llu%llx", ll
, ll
, ull
, ull
, ull
);
57 cxxdiag ("%lld%lli%llo%llu%llx", ll
, ll
, ull
, ull
, ull
);
58 diag ("%wd%wi%wo%wu%wx", ll
, ll
, ull
, ull
, ull
);
59 tdiag ("%wd%wi%wo%wu%wx", ll
, ll
, ull
, ull
, ull
);
60 cdiag ("%wd%wi%wo%wu%wx", ll
, ll
, ull
, ull
, ull
);
61 cxxdiag ("%wd%wi%wo%wu%wx", ll
, ll
, ull
, ull
, ull
);
65 cxxdiag ("%.*s", i
, s
);
67 /* Extensions provided in the diagnostic framework. */
81 tdiag ("%D%F%T", t1
, t1
, t1
);
82 tdiag ("%+D%+F%+T", t1
, t1
, t1
);
83 tdiag ("%q+D%q+F%q+T", t1
, t1
, t1
);
84 tdiag ("%D%D%D%D", t1
, t2
, *t3
, t4
[5]);
85 cdiag ("%D%F%T", t1
, t1
, t1
);
86 cdiag ("%+D%+F%+T", t1
, t1
, t1
);
87 cdiag ("%q+D%q+F%q+T", t1
, t1
, t1
);
88 cdiag ("%D%D%D%D", t1
, t2
, *t3
, t4
[5]);
90 cxxdiag ("%A%D%E%F%T%V", t1
, t1
, t1
, t1
, t1
, t1
);
91 cxxdiag ("%D%D%D%D", t1
, t2
, *t3
, t4
[5]);
92 cxxdiag ("%#A%#D%#E%#F%#T%#V", t1
, t1
, t1
, t1
, t1
, t1
);
93 cxxdiag ("%+A%+D%+E%+F%+T%+V", t1
, t1
, t1
, t1
, t1
, t1
);
94 cxxdiag ("%+#A%+#D%+#E%+#F%+#T%+#V", t1
, t1
, t1
, t1
, t1
, t1
);
95 cxxdiag ("%C%L%O%P%Q", i
, i
, i
, i
, i
);
97 /* Bad stuff with extensions. */
98 diag ("%m", i
); /* { dg-warning "format" "extra arg" } */
99 tdiag ("%m", i
); /* { dg-warning "format" "extra arg" } */
100 cdiag ("%m", i
); /* { dg-warning "format" "extra arg" } */
101 cxxdiag ("%m", i
); /* { dg-warning "format" "extra arg" } */
102 diag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
103 tdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
104 cdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
105 cxxdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
106 diag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
107 tdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
108 cdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
109 cxxdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
110 diag ("%H"); /* { dg-warning "format" "missing arg" } */
111 tdiag ("%H"); /* { dg-warning "format" "missing arg" } */
112 cdiag ("%H"); /* { dg-warning "format" "missing arg" } */
113 cxxdiag ("%H"); /* { dg-warning "format" "missing arg" } */
114 diag ("%J"); /* { dg-warning "format" "missing arg" } */
115 tdiag ("%J"); /* { dg-warning "format" "missing arg" } */
116 cdiag ("%J"); /* { dg-warning "format" "missing arg" } */
117 cxxdiag ("%J"); /* { dg-warning "format" "missing arg" } */
118 diag ("%H", i
); /* { dg-warning "format" "wrong arg" } */
119 tdiag ("%H", i
); /* { dg-warning "format" "wrong arg" } */
120 cdiag ("%H", i
); /* { dg-warning "format" "wrong arg" } */
121 cxxdiag ("%H", i
); /* { dg-warning "format" "wrong arg" } */
122 diag ("%H", p
); /* { dg-warning "format" "wrong arg" } */
123 tdiag ("%H", p
); /* { dg-warning "format" "wrong arg" } */
124 cdiag ("%H", p
); /* { dg-warning "format" "wrong arg" } */
125 cxxdiag ("%H", p
); /* { dg-warning "format" "wrong arg" } */
126 diag ("%J", loc
); /* { dg-warning "format" "wrong arg" } */
127 tdiag ("%J", loc
); /* { dg-warning "format" "wrong arg" } */
128 cdiag ("%J", loc
); /* { dg-warning "format" "wrong arg" } */
129 cxxdiag ("%J", loc
); /* { dg-warning "format" "wrong arg" } */
130 diag ("%#H", loc
); /* { dg-warning "format" "bogus modifier" } */
131 tdiag ("%#H", loc
); /* { dg-warning "format" "bogus modifier" } */
132 cdiag ("%#H", loc
); /* { dg-warning "format" "bogus modifier" } */
133 cxxdiag ("%#H", loc
); /* { dg-warning "format" "bogus modifier" } */
134 diag ("%+H", loc
); /* { dg-warning "format" "bogus modifier" } */
135 tdiag ("%+H", loc
); /* { dg-warning "format" "bogus modifier" } */
136 cdiag ("%+H", loc
); /* { dg-warning "format" "bogus modifier" } */
137 cxxdiag ("%+H", loc
); /* { dg-warning "format" "bogus modifier" } */
138 diag ("%D", t1
); /* { dg-warning "format" "bogus tree" } */
139 tdiag ("%A", t1
); /* { dg-warning "format" "bogus tree" } */
140 tdiag ("%E", t1
); /* { dg-warning "format" "bogus tree" } */
141 tdiag ("%#D", t1
); /* { dg-warning "format" "bogus modifier" } */
142 cdiag ("%A", t1
); /* { dg-warning "format" "bogus tree" } */
143 cdiag ("%#D", t1
); /* { dg-warning "format" "bogus modifier" } */
145 cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */
146 cxxdiag ("%C", l
); /* { dg-warning "format" "wrong arg" } */
147 cxxdiag ("%C", i
, i
); /* { dg-warning "format" "extra arg" } */
148 cxxdiag ("%#C", i
); /* { dg-warning "format" "bogus modifier" } */
149 cxxdiag ("%+C", i
); /* { dg-warning "format" "bogus modifier" } */
150 tdiag ("%D"); /* { dg-warning "format" "missing arg" } */
151 cdiag ("%D"); /* { dg-warning "format" "missing arg" } */
152 cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */
153 tdiag ("%D", i
); /* { dg-warning "format" "wrong arg" } */
154 cdiag ("%D", i
); /* { dg-warning "format" "wrong arg" } */
155 cxxdiag ("%D", i
); /* { dg-warning "format" "wrong arg" } */
156 tdiag ("%D", t1
, t1
); /* { dg-warning "format" "extra arg" } */
157 cdiag ("%D", t1
, t1
); /* { dg-warning "format" "extra arg" } */
158 cxxdiag ("%D", t1
, t1
); /* { dg-warning "format" "extra arg" } */
160 /* Standard specifiers not accepted in the diagnostic framework. */
161 diag ("%X\n", u
); /* { dg-warning "format" "HEX" } */
162 diag ("%f\n", d
); /* { dg-warning "format" "float" } */
163 diag ("%e\n", d
); /* { dg-warning "format" "float" } */
164 diag ("%E\n", d
); /* { dg-warning "format" "float" } */
165 diag ("%g\n", d
); /* { dg-warning "format" "float" } */
166 diag ("%G\n", d
); /* { dg-warning "format" "float" } */
167 diag ("%n\n", n
); /* { dg-warning "format" "counter" } */
168 diag ("%hd\n", i
); /* { dg-warning "format" "conversion" } */
170 /* Various tests of bad argument types. */
171 diag ("%-d", i
); /* { dg-warning "format" "bad flag" } */
172 tdiag ("%-d", i
); /* { dg-warning "format" "bad flag" } */
173 cdiag ("%-d", i
); /* { dg-warning "format" "bad flag" } */
174 cxxdiag ("%-d", i
); /* { dg-warning "format" "bad flag" } */
175 diag ("% d", i
); /* { dg-warning "format" "bad flag" } */
176 tdiag ("% d", i
); /* { dg-warning "format" "bad flag" } */
177 cdiag ("% d", i
); /* { dg-warning "format" "bad flag" } */
178 cxxdiag ("% d", i
); /* { dg-warning "format" "bad flag" } */
179 diag ("%#o", u
); /* { dg-warning "format" "bad flag" } */
180 tdiag ("%#o", u
); /* { dg-warning "format" "bad flag" } */
181 cdiag ("%#o", u
); /* { dg-warning "format" "bad flag" } */
182 cxxdiag ("%#o", u
); /* { dg-warning "format" "bad flag" } */
183 diag ("%0d", i
); /* { dg-warning "format" "bad flag" } */
184 tdiag ("%0d", i
); /* { dg-warning "format" "bad flag" } */
185 cdiag ("%0d", i
); /* { dg-warning "format" "bad flag" } */
186 cxxdiag ("%0d", i
); /* { dg-warning "format" "bad flag" } */
187 diag ("%08d", i
); /* { dg-warning "format" "bad flag" } */
188 tdiag ("%08d", i
); /* { dg-warning "format" "bad flag" } */
189 cdiag ("%08d", i
); /* { dg-warning "format" "bad flag" } */
190 cxxdiag ("%08d", i
); /* { dg-warning "format" "bad flag" } */
191 diag ("%+d\n", i
); /* { dg-warning "format" "bad flag" } */
192 tdiag ("%+d\n", i
); /* { dg-warning "format" "bad flag" } */
193 cdiag ("%+d\n", i
); /* { dg-warning "format" "bad flag" } */
194 cxxdiag ("%+d\n", i
); /* { dg-warning "format" "bad flag" } */
195 diag ("%3d\n", i
); /* { dg-warning "format" "bad flag" } */
196 tdiag ("%3d\n", i
); /* { dg-warning "format" "bad flag" } */
197 cdiag ("%3d\n", i
); /* { dg-warning "format" "bad flag" } */
198 cxxdiag ("%3d\n", i
); /* { dg-warning "format" "bad flag" } */
199 diag ("%-3d\n", i
); /* { dg-warning "format" "bad flag" } */
200 tdiag ("%-3d\n", i
); /* { dg-warning "format" "bad flag" } */
201 cdiag ("%-3d\n", i
); /* { dg-warning "format" "bad flag" } */
202 cxxdiag ("%-3d\n", i
); /* { dg-warning "format" "bad flag" } */
203 diag ("%.7d\n", i
); /* { dg-warning "format" "bad flag" } */
204 tdiag ("%.7d\n", i
); /* { dg-warning "format" "bad flag" } */
205 cdiag ("%.7d\n", i
); /* { dg-warning "format" "bad flag" } */
206 cxxdiag ("%.7d\n", i
); /* { dg-warning "format" "bad flag" } */
207 diag ("%+9.4d\n", i
); /* { dg-warning "format" "bad flag" } */
208 tdiag ("%+9.4d\n", i
); /* { dg-warning "format" "bad flag" } */
209 cdiag ("%+9.4d\n", i
); /* { dg-warning "format" "bad flag" } */
210 cxxdiag ("%+9.4d\n", i
); /* { dg-warning "format" "bad flag" } */
211 diag ("%.3ld\n", l
); /* { dg-warning "format" "bad flag" } */
212 tdiag ("%.3ld\n", l
); /* { dg-warning "format" "bad flag" } */
213 cdiag ("%.3ld\n", l
); /* { dg-warning "format" "bad flag" } */
214 cxxdiag ("%.3ld\n", l
); /* { dg-warning "format" "bad flag" } */
215 diag ("%d %lu\n", i
, ul
);
216 diag ("%d", l
); /* { dg-warning "format" "bad argument types" } */
217 diag ("%wd", l
); /* { dg-warning "format" "bad argument types" } */
218 diag ("%d", ll
); /* { dg-warning "format" "bad argument types" } */
219 diag ("%*s", i
, s
); /* { dg-warning "format" "bad * argument types" } */
220 diag ("%*.*s", i
, i
, s
); /* { dg-warning "format" "bad * argument types" } */
221 diag ("%*d\n", i1
, i
); /* { dg-warning "format" "bad * argument types" } */
222 diag ("%.*d\n", i2
, i
); /* { dg-warning "format" "bad * argument types" } */
223 diag ("%*.*ld\n", i1
, i2
, l
); /* { dg-warning "format" "bad * argument types" } */
224 diag ("%ld", i
); /* { dg-warning "format" "bad argument types" } */
225 diag ("%s", n
); /* { dg-warning "format" "bad argument types" } */
227 /* Wrong number of arguments. */
228 diag ("%d%d", i
); /* { dg-warning "arguments" "wrong number of args" } */
229 diag ("%d", i
, i
); /* { dg-warning "arguments" "wrong number of args" } */
230 /* Miscellaneous bogus constructions. */
231 diag (""); /* { dg-warning "zero-length" "warning for empty format" } */
232 diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
233 diag ("%d\0", i
); /* { dg-warning "embedded" "warning for embedded NUL" } */
234 diag ("%d\0%d", i
, i
); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
235 diag (NULL
); /* { dg-warning "null" "null format string warning" } */
236 diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */
237 diag ((const char *)L
"foo"); /* { dg-warning "wide" "wide string" } */
238 diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
240 /* Make sure we still get warnings for regular printf. */
241 printf ("%d\n", ll
); /* { dg-warning "format" "bad argument types" } */