2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / gcc.dg / format / gcc_diag-1.c
blob46013c8699e60d3167ec88a79b5b91f3acdfecc9
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[])
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 tdiag ("%D%F%T", t1, t1, t1);
74 tdiag ("%+D%+F%+T", t1, t1, t1);
75 tdiag ("%q+D%q+F%q+T", t1, t1, t1);
76 tdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
77 cdiag ("%D%F%T", t1, t1, t1);
78 cdiag ("%+D%+F%+T", t1, t1, t1);
79 cdiag ("%q+D%q+F%q+T", 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);
89 /* Bad stuff with extensions. */
90 diag ("%m", i); /* { dg-warning "format" "extra arg" } */
91 tdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
92 cdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
93 cxxdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
94 diag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
95 tdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
96 cdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
97 cxxdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
98 diag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
99 tdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
100 cdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
101 cxxdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
102 diag ("%D", t1); /* { dg-warning "format" "bogus tree" } */
103 tdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
104 tdiag ("%E", t1);
105 tdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
106 cdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
107 cdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
108 cdiag ("%+D", t1);
109 cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */
110 cxxdiag ("%C", l); /* { dg-warning "format" "wrong arg" } */
111 cxxdiag ("%C", i, i); /* { dg-warning "format" "extra arg" } */
112 cxxdiag ("%#C", i); /* { dg-warning "format" "bogus modifier" } */
113 cxxdiag ("%+C", i); /* { dg-warning "format" "bogus modifier" } */
114 tdiag ("%D"); /* { dg-warning "format" "missing arg" } */
115 cdiag ("%D"); /* { dg-warning "format" "missing arg" } */
116 cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */
117 tdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
118 cdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
119 cxxdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
120 tdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
121 cdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
122 cxxdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
124 /* Standard specifiers not accepted in the diagnostic framework. */
125 diag ("%X\n", u); /* { dg-warning "format" "HEX" } */
126 diag ("%f\n", d); /* { dg-warning "format" "float" } */
127 diag ("%e\n", d); /* { dg-warning "format" "float" } */
128 diag ("%E\n", d); /* { dg-warning "format" "float" } */
129 diag ("%g\n", d); /* { dg-warning "format" "float" } */
130 diag ("%G\n", d); /* { dg-warning "format" "float" } */
131 diag ("%n\n", n); /* { dg-warning "format" "counter" } */
132 diag ("%hd\n", i); /* { dg-warning "format" "conversion" } */
134 /* Various tests of bad argument types. */
135 diag ("%-d", i); /* { dg-warning "format" "bad flag" } */
136 tdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
137 cdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
138 cxxdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
139 diag ("% d", i); /* { dg-warning "format" "bad flag" } */
140 tdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
141 cdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
142 cxxdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
143 diag ("%#o", u); /* { dg-warning "format" "bad flag" } */
144 tdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
145 cdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
146 cxxdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
147 diag ("%0d", i); /* { dg-warning "format" "bad flag" } */
148 tdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
149 cdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
150 cxxdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
151 diag ("%08d", i); /* { dg-warning "format" "bad flag" } */
152 tdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
153 cdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
154 cxxdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
155 diag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
156 tdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
157 cdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
158 cxxdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
159 diag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
160 tdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
161 cdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
162 cxxdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
163 diag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
164 tdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
165 cdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
166 cxxdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
167 diag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
168 tdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
169 cdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
170 cxxdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
171 diag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
172 tdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
173 cdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
174 cxxdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
175 diag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
176 tdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
177 cdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
178 cxxdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
179 diag ("%d %lu\n", i, ul);
180 diag ("%d", l); /* { dg-warning "format" "bad argument types" } */
181 diag ("%wd", l); /* { dg-warning "format" "bad argument types" } */
182 diag ("%d", ll); /* { dg-warning "format" "bad argument types" } */
183 diag ("%*s", i, s); /* { dg-warning "format" "bad * argument types" } */
184 diag ("%*.*s", i, i, s); /* { dg-warning "format" "bad * argument types" } */
185 diag ("%*d\n", i1, i); /* { dg-warning "format" "bad * argument types" } */
186 diag ("%.*d\n", i2, i); /* { dg-warning "format" "bad * argument types" } */
187 diag ("%*.*ld\n", i1, i2, l); /* { dg-warning "format" "bad * argument types" } */
188 diag ("%ld", i); /* { dg-warning "format" "bad argument types" } */
189 diag ("%s", n); /* { dg-warning "format" "bad argument types" } */
191 /* Wrong number of arguments. */
192 diag ("%d%d", i); /* { dg-warning "arguments" "wrong number of args" } */
193 diag ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */
194 /* Miscellaneous bogus constructions. */
195 diag (""); /* { dg-warning "zero-length" "warning for empty format" } */
196 diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
197 diag ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */
198 diag ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
199 diag (NULL); /* { dg-warning "null" "null format string warning" } */
200 diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */
201 diag ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
202 diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
204 /* Make sure we still get warnings for regular printf. */
205 printf ("%d\n", ll); /* { dg-warning "format" "bad argument types" } */