OSDN Git Service

Index: gcc/ChangeLog
[pf3gnuchains/gcc-fork.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 cdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_cdiag__);
25 extern int cxxdiag (const char *, ...) ATTRIBUTE_DIAG(__gcc_cxxdiag__);
26
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[])
35 {
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 ("%ld%li%lo%lu%lx", l, l, ul, ul, ul);
44   cdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul);
45   cxxdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul);
46   diag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull);
47   cdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull);
48   cxxdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull);
49   diag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull);
50   cdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull);
51   cxxdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull);
52   diag ("%.*s", i, s);
53   cdiag ("%.*s", i, s);
54   cxxdiag ("%.*s", i, s);
55
56   /* Extensions provided in the diagnostic framework.  */
57   diag ("%m");
58   cdiag ("%m");
59   cxxdiag ("%m");
60   diag ("%H", loc);
61   cdiag ("%H", loc);
62   cxxdiag ("%H", loc);
63   diag ("%J", t1);
64   cdiag ("%J", t1);
65   cxxdiag ("%J", t1);
66
67   cdiag ("%D%F%T", t1, t1, t1);
68   cdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
69   cxxdiag ("%A%D%E%F%T%V", t1, t1, t1, t1, t1, t1);
70   cxxdiag ("%D%D%D%D", t1, t2, *t3, t4[5]);
71   cxxdiag ("%#A%#D%#E%#F%#T%#V", t1, t1, t1, t1, t1, t1);
72   cxxdiag ("%+A%+D%+E%+F%+T%+V", t1, t1, t1, t1, t1, t1);
73   cxxdiag ("%+#A%+#D%+#E%+#F%+#T%+#V", t1, t1, t1, t1, t1, t1);
74   cxxdiag ("%C%L%O%P%Q", i, i, i, i, i);
75
76   /* Bad stuff with extensions.  */
77   diag ("%m", i); /* { dg-warning "format" "extra arg" } */
78   cdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
79   cxxdiag ("%m", i); /* { dg-warning "format" "extra arg" } */
80   diag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
81   cdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
82   cxxdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */
83   diag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
84   cdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
85   cxxdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */
86   diag ("%H"); /* { dg-warning "format" "missing arg" } */
87   cdiag ("%H"); /* { dg-warning "format" "missing arg" } */
88   cxxdiag ("%H"); /* { dg-warning "format" "missing arg" } */
89   diag ("%J"); /* { dg-warning "format" "missing arg" } */
90   cdiag ("%J"); /* { dg-warning "format" "missing arg" } */
91   cxxdiag ("%J"); /* { dg-warning "format" "missing arg" } */
92   diag ("%H", i); /* { dg-warning "format" "wrong arg" } */
93   cdiag ("%H", i); /* { dg-warning "format" "wrong arg" } */
94   cxxdiag ("%H", i); /* { dg-warning "format" "wrong arg" } */
95   diag ("%H", p); /* { dg-warning "format" "wrong arg" } */
96   cdiag ("%H", p); /* { dg-warning "format" "wrong arg" } */
97   cxxdiag ("%H", p); /* { dg-warning "format" "wrong arg" } */
98   diag ("%J", loc); /* { dg-warning "format" "wrong arg" } */
99   cdiag ("%J", loc); /* { dg-warning "format" "wrong arg" } */
100   cxxdiag ("%J", loc); /* { dg-warning "format" "wrong arg" } */
101   diag ("%#H", loc); /* { dg-warning "format" "bogus modifier" } */
102   cdiag ("%#H", loc); /* { dg-warning "format" "bogus modifier" } */
103   cxxdiag ("%#H", loc); /* { dg-warning "format" "bogus modifier" } */
104   diag ("%+H", loc); /* { dg-warning "format" "bogus modifier" } */
105   cdiag ("%+H", loc); /* { dg-warning "format" "bogus modifier" } */
106   cxxdiag ("%+H", loc); /* { dg-warning "format" "bogus modifier" } */
107   diag ("%D", t1); /* { dg-warning "format" "bogus tree" } */
108   cdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */
109   cdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */
110   cdiag ("%+D", t1); /* { dg-warning "format" "bogus modifier" } */
111   cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */
112   cxxdiag ("%C", l); /* { dg-warning "format" "wrong arg" } */
113   cxxdiag ("%C", i, i); /* { dg-warning "format" "extra arg" } */
114   cxxdiag ("%#C", i); /* { dg-warning "format" "bogus modifier" } */
115   cxxdiag ("%+C", i); /* { dg-warning "format" "bogus modifier" } */
116   cdiag ("%D"); /* { dg-warning "format" "missing arg" } */
117   cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */
118   cdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
119   cxxdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */
120   cdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
121   cxxdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */
122
123   /* Standard specifiers not accepted in the diagnostic framework.  */
124   diag ("%X\n", u); /* { dg-warning "format" "HEX" } */
125   diag ("%f\n", d); /* { dg-warning "format" "float" } */
126   diag ("%e\n", d); /* { dg-warning "format" "float" } */
127   diag ("%E\n", d); /* { dg-warning "format" "float" } */
128   diag ("%g\n", d); /* { dg-warning "format" "float" } */
129   diag ("%G\n", d); /* { dg-warning "format" "float" } */
130   diag ("%n\n", n); /* { dg-warning "format" "counter" } */
131   diag ("%hd\n", i); /* { dg-warning "format" "conversion" } */
132
133   /* Various tests of bad argument types.  */
134   diag ("%-d", i); /* { dg-warning "format" "bad flag" } */
135   cdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
136   cxxdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */
137   diag ("% d", i); /* { dg-warning "format" "bad flag" } */
138   cdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
139   cxxdiag ("% d", i); /* { dg-warning "format" "bad flag" } */
140   diag ("%#o", u); /* { dg-warning "format" "bad flag" } */
141   cdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
142   cxxdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */
143   diag ("%0d", i); /* { dg-warning "format" "bad flag" } */
144   cdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
145   cxxdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */
146   diag ("%08d", i); /* { dg-warning "format" "bad flag" } */
147   cdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
148   cxxdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */
149   diag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
150   cdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
151   cxxdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */
152   diag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
153   cdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
154   cxxdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */
155   diag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
156   cdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
157   cxxdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */
158   diag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
159   cdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
160   cxxdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */
161   diag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
162   cdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
163   cxxdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */
164   diag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
165   cdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
166   cxxdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */
167   diag ("%d %lu\n", i, ul);
168   diag ("%d", l); /* { dg-warning "format" "bad argument types" } */
169   diag ("%wd", l); /* { dg-warning "format" "bad argument types" } */
170   diag ("%d", ll); /* { dg-warning "format" "bad argument types" } */
171   diag ("%*s", i, s); /* { dg-warning "format" "bad * argument types" } */
172   diag ("%*.*s", i, i, s); /* { dg-warning "format" "bad * argument types" } */
173   diag ("%*d\n", i1, i); /* { dg-warning "format" "bad * argument types" } */
174   diag ("%.*d\n", i2, i); /* { dg-warning "format" "bad * argument types" } */
175   diag ("%*.*ld\n", i1, i2, l); /* { dg-warning "format" "bad * argument types" } */
176   diag ("%ld", i); /* { dg-warning "format" "bad argument types" } */
177   diag ("%s", n); /* { dg-warning "format" "bad argument types" } */
178
179   /* Wrong number of arguments.  */
180   diag ("%d%d", i); /* { dg-warning "arguments" "wrong number of args" } */
181   diag ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */
182   /* Miscellaneous bogus constructions.  */
183   diag (""); /* { dg-warning "zero-length" "warning for empty format" } */
184   diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
185   diag ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */
186   diag ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
187   diag (NULL); /* { dg-warning "null" "null format string warning" } */
188   diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */
189   diag ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
190   diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
191
192   /* Make sure we still get warnings for regular printf.  */
193   printf ("%d\n", ll); /* { dg-warning "format" "bad argument types" } */
194 }