2005-07-02 Zack Weinberg <zack@codesourcery.com>
[official-gcc.git] / gcc / testsuite / gcc.dg / format / gcc_diag-1.c
blobe7340063df08ecfe449e864cccaa83aed7d4c7b4
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 cdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_cdiag__);
25 extern int cxxdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_cxxdiag__);
27 void
28 foo (int i, int i1, int i2, unsigned int u, double d, char *s, void *p,
29 int *n, short int *hn, long int l, unsigned long int ul,
30 long int *ln, long double ld, wint_t lc, wchar_t *ls, llong ll,
31 ullong ull, unsigned int *un, const int *cn, signed char *ss,
32 unsigned char *us, const signed char *css, unsigned int u1,
33 unsigned int u2, location_t *loc, tree t1, union tree_node *t2,
34 tree *t3, tree t4[])
36 /* Acceptable C90 specifiers, flags and modifiers. */
37 diag ("%%");
38 cdiag ("%%");
39 cxxdiag ("%%");
40 diag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p);
41 cdiag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p);
42 cxxdiag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p);
43 diag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p);
44 cdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p);
45 cxxdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p);
46 diag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul);
47 cdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul);
48 cxxdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul);
49 diag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull);
50 cdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull);
51 cxxdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull);
52 diag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull);
53 cdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull);
54 cxxdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull);
55 diag ("%.*s", i, s);
56 cdiag ("%.*s", i, s);
57 cxxdiag ("%.*s", i, s);
59 /* Extensions provided in the diagnostic framework. */
60 diag ("%m");
61 cdiag ("%m");
62 cxxdiag ("%m");
63 diag ("%H", loc);
64 cdiag ("%H", loc);
65 cxxdiag ("%H", loc);
66 diag ("%J", t1);
67 cdiag ("%J", t1);
68 cxxdiag ("%J", t1);
70 cdiag ("%D%F%T", t1, t1, t1);
71 cdiag ("%+D%+F%+T", t1, t1, t1);
72 cdiag ("%q+D%q+F%q+T", t1, t1, t1);
73 cdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
74 cxxdiag ("%A%D%E%F%T%V", t1, t1, t1, t1, t1, t1);
75 cxxdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
76 cxxdiag ("%#A%#D%#E%#F%#T%#V", t1, t1, t1, t1, t1, t1);
77 cxxdiag ("%+A%+D%+E%+F%+T%+V", t1, t1, t1, t1, t1, t1);
78 cxxdiag ("%+#A%+#D%+#E%+#F%+#T%+#V", t1, t1, t1, t1, t1, t1);
79 cxxdiag ("%C%L%O%P%Q", i, i, i, i, i);
81 /* Bad stuff with extensions. */
82 diag ("%m", i); /* { dg-warning "format" "extra arg" } */
83 cdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
84 cxxdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
85 diag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
86 cdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
87 cxxdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
88 diag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
89 cdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
90 cxxdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
91 diag ("%H"); /* { dg-warning "format" "missing arg" } */
92 cdiag ("%H"); /* { dg-warning "format" "missing arg" } */
93 cxxdiag ("%H"); /* { dg-warning "format" "missing arg" } */
94 diag ("%J"); /* { dg-warning "format" "missing arg" } */
95 cdiag ("%J"); /* { dg-warning "format" "missing arg" } */
96 cxxdiag ("%J"); /* { dg-warning "format" "missing arg" } */
97 diag ("%H", i); /* { dg-warning "format" "wrong arg" } */
98 cdiag ("%H", i); /* { dg-warning "format" "wrong arg" } */
99 cxxdiag ("%H", i); /* { dg-warning "format" "wrong arg" } */
100 diag ("%H", p); /* { dg-warning "format" "wrong arg" } */
101 cdiag ("%H", p); /* { dg-warning "format" "wrong arg" } */
102 cxxdiag ("%H", p); /* { dg-warning "format" "wrong arg" } */
103 diag ("%J", loc); /* { dg-warning "format" "wrong arg" } */
104 cdiag ("%J", loc); /* { dg-warning "format" "wrong arg" } */
105 cxxdiag ("%J", loc); /* { dg-warning "format" "wrong arg" } */
106 diag ("%#H", loc); /* { dg-warning "format" "bogus modifier" } */
107 cdiag ("%#H", loc); /* { dg-warning "format" "bogus modifier" } */
108 cxxdiag ("%#H", loc); /* { dg-warning "format" "bogus modifier" } */
109 diag ("%+H", loc); /* { dg-warning "format" "bogus modifier" } */
110 cdiag ("%+H", loc); /* { dg-warning "format" "bogus modifier" } */
111 cxxdiag ("%+H", loc); /* { dg-warning "format" "bogus modifier" } */
112 diag ("%D", t1); /* { dg-warning "format" "bogus tree" } */
113 cdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
114 cdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
115 cdiag ("%+D", t1);
116 cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */
117 cxxdiag ("%C", l); /* { dg-warning "format" "wrong arg" } */
118 cxxdiag ("%C", i, i); /* { dg-warning "format" "extra arg" } */
119 cxxdiag ("%#C", i); /* { dg-warning "format" "bogus modifier" } */
120 cxxdiag ("%+C", i); /* { dg-warning "format" "bogus modifier" } */
121 cdiag ("%D"); /* { dg-warning "format" "missing arg" } */
122 cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */
123 cdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
124 cxxdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
125 cdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
126 cxxdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
128 /* Standard specifiers not accepted in the diagnostic framework. */
129 diag ("%X\n", u); /* { dg-warning "format" "HEX" } */
130 diag ("%f\n", d); /* { dg-warning "format" "float" } */
131 diag ("%e\n", d); /* { dg-warning "format" "float" } */
132 diag ("%E\n", d); /* { dg-warning "format" "float" } */
133 diag ("%g\n", d); /* { dg-warning "format" "float" } */
134 diag ("%G\n", d); /* { dg-warning "format" "float" } */
135 diag ("%n\n", n); /* { dg-warning "format" "counter" } */
136 diag ("%hd\n", i); /* { dg-warning "format" "conversion" } */
138 /* Various tests of bad argument types. */
139 diag ("%-d", i); /* { dg-warning "format" "bad flag" } */
140 cdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
141 cxxdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
142 diag ("% d", i); /* { dg-warning "format" "bad flag" } */
143 cdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
144 cxxdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
145 diag ("%#o", u); /* { dg-warning "format" "bad flag" } */
146 cdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
147 cxxdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
148 diag ("%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 cdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
153 cxxdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
154 diag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
155 cdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
156 cxxdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
157 diag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
158 cdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
159 cxxdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
160 diag ("%-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 ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
164 cdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
165 cxxdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
166 diag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
167 cdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
168 cxxdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
169 diag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
170 cdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
171 cxxdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
172 diag ("%d %lu\n", i, ul);
173 diag ("%d", l); /* { dg-warning "format" "bad argument types" } */
174 diag ("%wd", l); /* { dg-warning "format" "bad argument types" } */
175 diag ("%d", ll); /* { dg-warning "format" "bad argument types" } */
176 diag ("%*s", i, s); /* { dg-warning "format" "bad * argument types" } */
177 diag ("%*.*s", i, i, s); /* { dg-warning "format" "bad * argument types" } */
178 diag ("%*d\n", i1, i); /* { dg-warning "format" "bad * argument types" } */
179 diag ("%.*d\n", i2, i); /* { dg-warning "format" "bad * argument types" } */
180 diag ("%*.*ld\n", i1, i2, l); /* { dg-warning "format" "bad * argument types" } */
181 diag ("%ld", i); /* { dg-warning "format" "bad argument types" } */
182 diag ("%s", n); /* { dg-warning "format" "bad argument types" } */
184 /* Wrong number of arguments. */
185 diag ("%d%d", i); /* { dg-warning "arguments" "wrong number of args" } */
186 diag ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */
187 /* Miscellaneous bogus constructions. */
188 diag (""); /* { dg-warning "zero-length" "warning for empty format" } */
189 diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
190 diag ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */
191 diag ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
192 diag (NULL); /* { dg-warning "null" "null format string warning" } */
193 diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */
194 diag ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
195 diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
197 /* Make sure we still get warnings for regular printf. */
198 printf ("%d\n", ll); /* { dg-warning "format" "bad argument types" } */