]> gcc.gnu.org Git - gcc.git/blob - gcc/testsuite/gcc.dg/format/gcc_diag-1.c
re PR tree-optimization/35503 (Warning about restricted pointers?)
[gcc.git] / gcc / testsuite / gcc.dg / format / gcc_diag-1.c
1 /* Test for GCC diagnositc formats. */
2 /* Origin: Kaveh Ghazi <ghazi@caip.rutgers.edu> */
3 /* { dg-do compile } */
4 /* { dg-options "-Wformat" } */
5
6 #include "format.h"
7
8 #define ATTRIBUTE_DIAG(F) __attribute__ ((__format__ (F, 1, 2))) __attribute__ ((__nonnull__));
9
10 /* Magic identifiers must be set before the attribute is used. */
11
12 typedef long long __gcc_host_wide_int__;
13
14 typedef struct location_s
15 {
16 const char *file;
17 int line;
18 } location_t;
19
20 union tree_node;
21 typedef union tree_node *tree;
22
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__);
27
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)
36 {
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);
66
67 /* Extensions provided in the diagnostic framework. */
68 diag ("%m");
69 tdiag ("%m");
70 cdiag ("%m");
71 cxxdiag ("%m");
72
73 tdiag ("%D%F%T%V", t1, t1, t1, t1);
74 tdiag ("%+D%+F%+T%+V", t1, t1, t1, t1);
75 tdiag ("%q+D%q+F%q+T%q+V", t1, t1, t1, t1);
76 tdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
77 cdiag ("%D%F%T%V", t1, t1, t1, t1);
78 cdiag ("%+D%+F%+T%+V", t1, t1, t1, t1);
79 cdiag ("%q+D%q+F%q+T%q+V", t1, t1, t1, t1);
80 cdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
81 cdiag ("%E", t1);
82 cxxdiag ("%A%D%E%F%T%V", t1, t1, t1, t1, t1, t1);
83 cxxdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
84 cxxdiag ("%#A%#D%#E%#F%#T%#V", t1, t1, t1, t1, t1, t1);
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 ("%C%L%O%P%Q", i, i, i, i, i);
88
89 tdiag ("%v%qv%#v", i, i, i);
90 cdiag ("%v%qv%#v", i, i, i);
91 cxxdiag ("%v%qv%#v", i, i, i);
92
93 tdiag ("%Z", v, v_len);
94 cdiag ("%Z", v, v_len);
95 cxxdiag ("%Z", v, v_len);
96
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 ("%D", t1); /* { dg-warning "format" "bogus tree" } */
111 tdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
112 tdiag ("%E", t1);
113 tdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
114 cdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
115 cdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
116 cdiag ("%+D", t1);
117 cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */
118 cxxdiag ("%C", l); /* { dg-warning "format" "wrong arg" } */
119 cxxdiag ("%C", i, i); /* { dg-warning "format" "extra arg" } */
120 cxxdiag ("%#C", i); /* { dg-warning "format" "bogus modifier" } */
121 cxxdiag ("%+C", i); /* { dg-warning "format" "bogus modifier" } */
122 tdiag ("%D"); /* { dg-warning "format" "missing arg" } */
123 cdiag ("%D"); /* { dg-warning "format" "missing arg" } */
124 cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */
125 tdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
126 cdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
127 cxxdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
128 tdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
129 cdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
130 cxxdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
131
132 tdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */
133 cdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */
134 cxxdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */
135
136 tdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */
137 cdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */
138 cxxdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */
139
140 tdiag ("%Z"); /* { dg-warning "format" "missing arg" } */
141 tdiag ("%Z", t1); /* { dg-warning "format" "wrong arg" } */
142
143 /* Standard specifiers not accepted in the diagnostic framework. */
144 diag ("%X\n", u); /* { dg-warning "format" "HEX" } */
145 diag ("%f\n", d); /* { dg-warning "format" "float" } */
146 diag ("%e\n", d); /* { dg-warning "format" "float" } */
147 diag ("%E\n", d); /* { dg-warning "format" "float" } */
148 diag ("%g\n", d); /* { dg-warning "format" "float" } */
149 diag ("%G\n", d); /* { dg-warning "format" "float" } */
150 diag ("%n\n", n); /* { dg-warning "format" "counter" } */
151 diag ("%hd\n", i); /* { dg-warning "format" "conversion" } */
152
153 /* Various tests of bad argument types. */
154 diag ("%-d", i); /* { dg-warning "format" "bad flag" } */
155 tdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
156 cdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
157 cxxdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
158 diag ("% d", i); /* { dg-warning "format" "bad flag" } */
159 tdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
160 cdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
161 cxxdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
162 diag ("%#o", u); /* { dg-warning "format" "bad flag" } */
163 tdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
164 cdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
165 cxxdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
166 diag ("%0d", i); /* { dg-warning "format" "bad flag" } */
167 tdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
168 cdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
169 cxxdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
170 diag ("%08d", i); /* { dg-warning "format" "bad flag" } */
171 tdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
172 cdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
173 cxxdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
174 diag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
175 tdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
176 cdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
177 cxxdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
178 diag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
179 tdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
180 cdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
181 cxxdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
182 diag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
183 tdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
184 cdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
185 cxxdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
186 diag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
187 tdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
188 cdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
189 cxxdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
190 diag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
191 tdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
192 cdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
193 cxxdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
194 diag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
195 tdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
196 cdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
197 cxxdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
198 diag ("%d %lu\n", i, ul);
199 diag ("%d", l); /* { dg-warning "format" "bad argument types" } */
200 diag ("%wd", l); /* { dg-warning "format" "bad argument types" } */
201 diag ("%d", ll); /* { dg-warning "format" "bad argument types" } */
202 diag ("%*s", i, s); /* { dg-warning "format" "bad * argument types" } */
203 diag ("%*.*s", i, i, s); /* { dg-warning "format" "bad * argument types" } */
204 diag ("%*d\n", i1, i); /* { dg-warning "format" "bad * argument types" } */
205 diag ("%.*d\n", i2, i); /* { dg-warning "format" "bad * argument types" } */
206 diag ("%*.*ld\n", i1, i2, l); /* { dg-warning "format" "bad * argument types" } */
207 diag ("%ld", i); /* { dg-warning "format" "bad argument types" } */
208 diag ("%s", n); /* { dg-warning "format" "bad argument types" } */
209
210 /* Wrong number of arguments. */
211 diag ("%d%d", i); /* { dg-warning "matching" "wrong number of args" } */
212 diag ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */
213 /* Miscellaneous bogus constructions. */
214 diag (""); /* { dg-warning "zero-length" "warning for empty format" } */
215 diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
216 diag ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */
217 diag ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
218 diag (NULL); /* { dg-warning "null" "null format string warning" } */
219 diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */
220 diag ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
221 diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
222
223 /* Make sure we still get warnings for regular printf. */
224 printf ("%d\n", ll); /* { dg-warning "format" "bad argument types" } */
225 }
This page took 0.056997 seconds and 5 git commands to generate.