PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / gcc.dg / format / gcc_diag-1.c
blob4dcdb05b8b9c566c36261a169b106aacda58b73a
1 /* Test for GCC diagnositc formats. */
2 /* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
3 /* { dg-do compile } */
4 /* { dg-options "-Wformat" } */
6 #include "format.h"
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
16 const char *file;
17 int line;
18 } location_t;
20 union tree_node;
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__);
28 void
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,
35 tree *t3, tree t4[], int *v, unsigned v_len)
37 /* Acceptable C90 specifiers, flags and modifiers. */
38 diag ("%%");
39 tdiag ("%%");
40 cdiag ("%%");
41 cxxdiag ("%%");
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);
62 diag ("%.*s", i, s);
63 tdiag ("%.*s", i, s);
64 cdiag ("%.*s", i, s);
65 cxxdiag ("%.*s", i, s);
67 /* Extensions provided in the diagnostic framework. */
68 diag ("%m");
69 tdiag ("%m");
70 cdiag ("%m");
71 cxxdiag ("%m");
73 /* Quote directives to avoid "warning: conversion used unquoted." */
74 tdiag ("%<%D%F%T%V%>", t1, t1, t1, t1);
75 tdiag ("%<%+D%+F%+T%+V%>", t1, t1, t1, t1);
76 tdiag ("%q+D%q+F%q+T%q+V", t1, t1, t1, t1);
77 tdiag ("%<%D%D%D%D%>", t1, t2, *t3, t4[5]);
78 cdiag ("%<%D%F%T%V%>", t1, t1, t1, t1);
79 cdiag ("%<%+D%+F%+T%+V%>", t1, t1, t1, t1);
80 cdiag ("%q+D%q+F%q+T%q+V", t1, t1, t1, t1);
81 cdiag ("%<%D%D%D%D%>", t1, t2, *t3, t4[5]);
82 cdiag ("%<%E%>", t1);
83 cxxdiag ("%<%A%D%E%F%T%V%>", t1, t1, t1, t1, t1, t1);
84 cxxdiag ("%<%D%D%D%D%>", t1, t2, *t3, t4[5]);
85 cxxdiag ("%<%#A%#D%#E%#F%#T%#V%>", t1, t1, t1, t1, t1, t1);
86 cxxdiag ("%<%+A%+D%+E%+F%+T%+V%>", t1, t1, t1, t1, t1, t1);
87 cxxdiag ("%<%+#A%+#D%+#E%+#F%+#T%+#V%>", t1, t1, t1, t1, t1, t1);
88 cxxdiag ("%C%L%O%P%Q", i, i, i, i, i);
90 tdiag ("%v%qv%#v", i, i, i);
91 cdiag ("%v%qv%#v", i, i, i);
92 cxxdiag ("%v%qv%#v", i, i, i);
94 tdiag ("%Z", v, v_len);
95 cdiag ("%Z", v, v_len);
96 cxxdiag ("%Z", v, v_len);
98 /* Bad stuff with extensions. */
99 diag ("%m", i); /* { dg-warning "format" "extra arg" } */
100 tdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
101 cdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
102 cxxdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
103 diag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
104 tdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
105 cdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
106 cxxdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
107 diag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
108 tdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
109 cdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
110 cxxdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
111 diag ("%D", t1); /* { dg-warning "format" "bogus tree" } */
112 tdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
113 tdiag ("%E", t1);
114 tdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
115 cdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
116 cdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
117 cdiag ("%<%+D%>", t1);
118 cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */
119 cxxdiag ("%C", l); /* { dg-warning "format" "wrong arg" } */
120 cxxdiag ("%C", i, i); /* { dg-warning "format" "extra arg" } */
121 cxxdiag ("%#C", i); /* { dg-warning "format" "bogus modifier" } */
122 cxxdiag ("%+C", i); /* { dg-warning "format" "bogus modifier" } */
123 tdiag ("%D"); /* { dg-warning "format" "missing arg" } */
124 cdiag ("%D"); /* { dg-warning "format" "missing arg" } */
125 cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */
126 tdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
127 cdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
128 cxxdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
129 tdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
130 cdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
131 cxxdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
133 tdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */
134 cdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */
135 cxxdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */
137 tdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */
138 cdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */
139 cxxdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */
141 tdiag ("%Z"); /* { dg-warning "format" "missing arg" } */
142 tdiag ("%Z", t1); /* { dg-warning "format" "wrong arg" } */
144 /* Standard specifiers not accepted in the diagnostic framework. */
145 diag ("%X\n", u); /* { dg-warning "format" "HEX" } */
146 diag ("%f\n", d); /* { dg-warning "format" "float" } */
147 diag ("%e\n", d); /* { dg-warning "format" "float" } */
148 diag ("%E\n", d); /* { dg-warning "format" "float" } */
149 diag ("%g\n", d); /* { dg-warning "format" "float" } */
150 diag ("%G\n", d); /* { dg-warning "format" "float" } */
151 diag ("%n\n", n); /* { dg-warning "format" "counter" } */
152 diag ("%hd\n", i); /* { dg-warning "format" "conversion" } */
154 /* Various tests of bad argument types. */
155 diag ("%-d", i); /* { dg-warning "format" "bad flag" } */
156 tdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
157 cdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
158 cxxdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
159 diag ("% d", i); /* { dg-warning "format" "bad flag" } */
160 tdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
161 cdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
162 cxxdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
163 diag ("%#o", u); /* { dg-warning "format" "bad flag" } */
164 tdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
165 cdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
166 cxxdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
167 diag ("%0d", i); /* { dg-warning "format" "bad flag" } */
168 tdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
169 cdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
170 cxxdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
171 diag ("%08d", i); /* { dg-warning "format" "bad flag" } */
172 tdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
173 cdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
174 cxxdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
175 diag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
176 tdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
177 cdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
178 cxxdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
179 diag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
180 tdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
181 cdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
182 cxxdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
183 diag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
184 tdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
185 cdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
186 cxxdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
187 diag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
188 tdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
189 cdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
190 cxxdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
191 diag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
192 tdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
193 cdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
194 cxxdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
195 diag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
196 tdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
197 cdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
198 cxxdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
199 diag ("%d %lu\n", i, ul);
200 diag ("%d", l); /* { dg-warning "format" "bad argument types" } */
201 diag ("%wd", l); /* { dg-warning "format" "bad argument types" } */
202 diag ("%d", ll); /* { dg-warning "format" "bad argument types" } */
203 diag ("%*s", i, s); /* { dg-warning "format" "bad * argument types" } */
204 diag ("%*.*s", i, i, s); /* { dg-warning "format" "bad * argument types" } */
205 diag ("%*d\n", i1, i); /* { dg-warning "format" "bad * argument types" } */
206 diag ("%.*d\n", i2, i); /* { dg-warning "format" "bad * argument types" } */
207 diag ("%*.*ld\n", i1, i2, l); /* { dg-warning "format" "bad * argument types" } */
208 diag ("%ld", i); /* { dg-warning "format" "bad argument types" } */
209 diag ("%s", n); /* { dg-warning "format" "bad argument types" } */
211 /* Wrong number of arguments. */
212 diag ("%d%d", i); /* { dg-warning "matching" "wrong number of args" } */
213 diag ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */
214 /* Miscellaneous bogus constructions. */
215 diag (""); /* { dg-warning "zero-length" "warning for empty format" } */
216 diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
217 diag ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */
218 diag ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
219 diag (NULL); /* { dg-warning "null" "null format string warning" } */
220 diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */
221 diag ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
222 diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
224 /* Make sure we still get warnings for regular printf. */
225 printf ("%d\n", ll); /* { dg-warning "format" "bad argument types" } */