| 1 |
/* Test for GCC diagnostic formats. */ |
| 2 |
/* Origin: Kaveh Ghazi <[email protected]> */ |
| 3 |
/* { dg-do compile } */ |
| 4 |
/* { dg-options "-Wformat -Wno-format-diag" } */ |
| 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 |
extern int dump (const char *, ...) ATTRIBUTE_DIAG(__gcc_dump_printf__); |
| 28 |
|
| 29 |
void |
| 30 |
foo (int i, int i1, int i2, unsigned int u, double d, char *s, void *p, |
| 31 |
int *n, short int *hn, long int l, unsigned long int ul, |
| 32 |
long int *ln, long double ld, wint_t lc, wchar_t *ls, llong ll, |
| 33 |
ullong ull, unsigned int *un, const int *cn, signed char *ss, |
| 34 |
unsigned char *us, const signed char *css, unsigned int u1, |
| 35 |
unsigned int u2, location_t *loc, tree t1, union tree_node *t2, |
| 36 |
tree *t3, tree t4[], int *v, unsigned v_len, size_t sz, ptrdiff_t pd, |
| 37 |
ssize_t ssz, unsigned_ptrdiff_t upd) |
| 38 |
{ |
| 39 |
/* Acceptable C90 specifiers, flags and modifiers. */ |
| 40 |
diag ("%%"); |
| 41 |
tdiag ("%%"); |
| 42 |
cdiag ("%%"); |
| 43 |
cxxdiag ("%%"); |
| 44 |
dump ("%%"); |
| 45 |
diag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p); |
| 46 |
tdiag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p); |
| 47 |
cdiag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p); |
| 48 |
cxxdiag ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p); |
| 49 |
dump ("%d%i%o%u%x%c%s%p%%", i, i, u, u, u, i, s, p); |
| 50 |
diag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p); |
| 51 |
tdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p); |
| 52 |
cdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p); |
| 53 |
cxxdiag ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p); |
| 54 |
dump ("%qd%qi%qo%qu%qx%qc%qs%qp%<%%%'%>", i, i, u, u, u, i, s, p); |
| 55 |
diag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul); |
| 56 |
tdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul); |
| 57 |
cdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul); |
| 58 |
cxxdiag ("%ld%li%lo%lu%lx", l, l, ul, ul, ul); |
| 59 |
dump ("%ld%li%lo%lu%lx", l, l, ul, ul, ul); |
| 60 |
diag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull); |
| 61 |
tdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull); |
| 62 |
cdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull); |
| 63 |
cxxdiag ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull); |
| 64 |
dump ("%lld%lli%llo%llu%llx", ll, ll, ull, ull, ull); |
| 65 |
diag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull); |
| 66 |
tdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull); |
| 67 |
cdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull); |
| 68 |
cxxdiag ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull); |
| 69 |
dump ("%wd%wi%wo%wu%wx", ll, ll, ull, ull, ull); |
| 70 |
diag ("%zd%zi%zo%zu%zx", ssz, ssz, sz, sz, sz); |
| 71 |
tdiag ("%zd%zi%zo%zu%zx", ssz, ssz, sz, sz, sz); |
| 72 |
cdiag ("%zd%zi%zo%zu%zx", ssz, ssz, sz, sz, sz); |
| 73 |
cxxdiag ("%zd%zi%zo%zu%zx", ssz, ssz, sz, sz, sz); |
| 74 |
dump ("%zd%zi%zo%zu%zx", ssz, ssz, sz, sz, sz); |
| 75 |
diag ("%td%ti%to%tu%tx", pd, pd, upd, upd, upd); |
| 76 |
tdiag ("%td%ti%to%tu%tx", pd, pd, upd, upd, upd); |
| 77 |
cdiag ("%td%ti%to%tu%tx", pd, pd, upd, upd, upd); |
| 78 |
cxxdiag ("%td%ti%to%tu%tx", pd, pd, upd, upd, upd); |
| 79 |
dump ("%td%ti%to%tu%tx", pd, pd, upd, upd, upd); |
| 80 |
diag ("%.*s", i, s); |
| 81 |
tdiag ("%.*s", i, s); |
| 82 |
cdiag ("%.*s", i, s); |
| 83 |
cxxdiag ("%.*s", i, s); |
| 84 |
dump ("%.*s", i, s); |
| 85 |
|
| 86 |
/* Extensions provided in the diagnostic framework. */ |
| 87 |
diag ("%m"); |
| 88 |
tdiag ("%m"); |
| 89 |
cdiag ("%m"); |
| 90 |
cxxdiag ("%m"); |
| 91 |
dump ("%m"); |
| 92 |
|
| 93 |
/* Quote directives to avoid "warning: conversion used unquoted." */ |
| 94 |
tdiag ("%<%D%F%T%V%>", t1, t1, t1, t1); |
| 95 |
tdiag ("%<%+D%+F%+T%+V%>", t1, t1, t1, t1); |
| 96 |
tdiag ("%q+D%q+F%q+T%q+V", t1, t1, t1, t1); |
| 97 |
tdiag ("%<%D%D%D%D%>", t1, t2, *t3, t4[5]); |
| 98 |
cdiag ("%<%D%F%T%V%>", t1, t1, t1, t1); |
| 99 |
cdiag ("%<%+D%+F%+T%+V%>", t1, t1, t1, t1); |
| 100 |
cdiag ("%q+D%q+F%q+T%q+V", t1, t1, t1, t1); |
| 101 |
cdiag ("%<%D%D%D%D%>", t1, t2, *t3, t4[5]); |
| 102 |
cdiag ("%<%E%>", t1); |
| 103 |
cxxdiag ("%<%A%D%E%F%T%V%>", t1, t1, t1, t1, t1, t1); |
| 104 |
cxxdiag ("%<%D%D%D%D%>", t1, t2, *t3, t4[5]); |
| 105 |
cxxdiag ("%<%#A%#D%#E%#F%#T%#V%>", t1, t1, t1, t1, t1, t1); |
| 106 |
cxxdiag ("%<%+A%+D%+E%+F%+T%+V%>", t1, t1, t1, t1, t1, t1); |
| 107 |
cxxdiag ("%<%+#A%+#D%+#E%+#F%+#T%+#V%>", t1, t1, t1, t1, t1, t1); |
| 108 |
cxxdiag ("%C%L%O%P%Q", i, i, i, i, i); |
| 109 |
|
| 110 |
tdiag ("%v", i); /* { dg-warning "format" } */ |
| 111 |
cdiag ("%v%qv%#v", i, i, i); |
| 112 |
cxxdiag ("%v", i); /* { dg-warning "format" } */ |
| 113 |
|
| 114 |
tdiag ("%Z", v, v_len); |
| 115 |
cdiag ("%Z", v, v_len); |
| 116 |
cxxdiag ("%Z", v, v_len); |
| 117 |
dump ("%Z", v, v_len); |
| 118 |
|
| 119 |
/* Bad stuff with extensions. */ |
| 120 |
diag ("%m", i); /* { dg-warning "format" "extra arg" } */ |
| 121 |
tdiag ("%m", i); /* { dg-warning "format" "extra arg" } */ |
| 122 |
cdiag ("%m", i); /* { dg-warning "format" "extra arg" } */ |
| 123 |
cxxdiag ("%m", i); /* { dg-warning "format" "extra arg" } */ |
| 124 |
dump ("%m", i); /* { dg-warning "format" "extra arg" } */ |
| 125 |
diag ("%#m"); /* { dg-warning "format" "bogus modifier" } */ |
| 126 |
tdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */ |
| 127 |
cdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */ |
| 128 |
cxxdiag ("%#m"); /* { dg-warning "format" "bogus modifier" } */ |
| 129 |
dump ("%#m"); /* { dg-warning "format" "bogus modifier" } */ |
| 130 |
diag ("%+m"); /* { dg-warning "format" "bogus modifier" } */ |
| 131 |
tdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */ |
| 132 |
cdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */ |
| 133 |
cxxdiag ("%+m"); /* { dg-warning "format" "bogus modifier" } */ |
| 134 |
dump ("%+m"); /* { dg-warning "format" "bogus modifier" } */ |
| 135 |
diag ("%D", t1); /* { dg-warning "format" "bogus tree" } */ |
| 136 |
tdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */ |
| 137 |
tdiag ("%E", t1); |
| 138 |
tdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */ |
| 139 |
cdiag ("%A", t1); /* { dg-warning "format" "bogus tree" } */ |
| 140 |
cdiag ("%#D", t1); /* { dg-warning "format" "bogus modifier" } */ |
| 141 |
cdiag ("%<%+D%>", t1); |
| 142 |
cxxdiag ("%C"); /* { dg-warning "format" "missing arg" } */ |
| 143 |
cxxdiag ("%C", l); /* { dg-warning "format" "wrong arg" } */ |
| 144 |
cxxdiag ("%C", i, i); /* { dg-warning "format" "extra arg" } */ |
| 145 |
cxxdiag ("%#C", i); /* { dg-warning "format" "bogus modifier" } */ |
| 146 |
cxxdiag ("%+C", i); /* { dg-warning "format" "bogus modifier" } */ |
| 147 |
tdiag ("%D"); /* { dg-warning "format" "missing arg" } */ |
| 148 |
cdiag ("%D"); /* { dg-warning "format" "missing arg" } */ |
| 149 |
cxxdiag ("%D"); /* { dg-warning "format" "missing arg" } */ |
| 150 |
tdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */ |
| 151 |
cdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */ |
| 152 |
cxxdiag ("%D", i); /* { dg-warning "format" "wrong arg" } */ |
| 153 |
tdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */ |
| 154 |
cdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */ |
| 155 |
cxxdiag ("%D", t1, t1); /* { dg-warning "format" "extra arg" } */ |
| 156 |
|
| 157 |
tdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */ |
| 158 |
cdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */ |
| 159 |
cxxdiag ("%V", i); /* { dg-warning "format" "wrong arg" } */ |
| 160 |
|
| 161 |
tdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */ |
| 162 |
cdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */ |
| 163 |
cxxdiag ("%v", t1); /* { dg-warning "format" "wrong arg" } */ |
| 164 |
|
| 165 |
tdiag ("%Z"); /* { dg-warning "format" "missing arg" } */ |
| 166 |
tdiag ("%Z", t1); /* { dg-warning "format" "wrong arg" } */ |
| 167 |
|
| 168 |
/* Standard specifiers not accepted in the diagnostic framework. */ |
| 169 |
diag ("%X\n", u); /* { dg-warning "format" "HEX" } */ |
| 170 |
diag ("%f\n", d); /* { dg-warning "format" "float" } */ |
| 171 |
diag ("%e\n", d); /* { dg-warning "format" "float" } */ |
| 172 |
diag ("%E\n", d); /* { dg-warning "format" "float" } */ |
| 173 |
diag ("%g\n", d); /* { dg-warning "format" "float" } */ |
| 174 |
diag ("%G\n", d); /* { dg-warning "format" "float" } */ |
| 175 |
diag ("%n\n", n); /* { dg-warning "format" "counter" } */ |
| 176 |
diag ("%hd\n", i); /* { dg-warning "format" "conversion" } */ |
| 177 |
|
| 178 |
/* Various tests of bad argument types. */ |
| 179 |
diag ("%-d", i); /* { dg-warning "format" "bad flag" } */ |
| 180 |
tdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */ |
| 181 |
cdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */ |
| 182 |
cxxdiag ("%-d", i); /* { dg-warning "format" "bad flag" } */ |
| 183 |
diag ("% d", i); /* { dg-warning "format" "bad flag" } */ |
| 184 |
tdiag ("% d", i); /* { dg-warning "format" "bad flag" } */ |
| 185 |
cdiag ("% d", i); /* { dg-warning "format" "bad flag" } */ |
| 186 |
cxxdiag ("% d", i); /* { dg-warning "format" "bad flag" } */ |
| 187 |
diag ("%#o", u); /* { dg-warning "format" "bad flag" } */ |
| 188 |
tdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */ |
| 189 |
cdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */ |
| 190 |
cxxdiag ("%#o", u); /* { dg-warning "format" "bad flag" } */ |
| 191 |
diag ("%0d", i); /* { dg-warning "format" "bad flag" } */ |
| 192 |
tdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */ |
| 193 |
cdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */ |
| 194 |
cxxdiag ("%0d", i); /* { dg-warning "format" "bad flag" } */ |
| 195 |
diag ("%08d", i); /* { dg-warning "format" "bad flag" } */ |
| 196 |
tdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */ |
| 197 |
cdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */ |
| 198 |
cxxdiag ("%08d", i); /* { dg-warning "format" "bad flag" } */ |
| 199 |
diag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 200 |
tdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 201 |
cdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 202 |
cxxdiag ("%+d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 203 |
diag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 204 |
tdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 205 |
cdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 206 |
cxxdiag ("%3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 207 |
diag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 208 |
tdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 209 |
cdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 210 |
cxxdiag ("%-3d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 211 |
diag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 212 |
tdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 213 |
cdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 214 |
cxxdiag ("%.7d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 215 |
diag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 216 |
tdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 217 |
cdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 218 |
cxxdiag ("%+9.4d\n", i); /* { dg-warning "format" "bad flag" } */ |
| 219 |
diag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */ |
| 220 |
tdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */ |
| 221 |
cdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */ |
| 222 |
cxxdiag ("%.3ld\n", l); /* { dg-warning "format" "bad flag" } */ |
| 223 |
diag ("%d %lu\n", i, ul); |
| 224 |
diag ("%d", l); /* { dg-warning "format" "bad argument types" } */ |
| 225 |
diag ("%wd", l); /* { dg-warning "format" "bad argument types" } */ |
| 226 |
diag ("%d", ll); /* { dg-warning "format" "bad argument types" } */ |
| 227 |
diag ("%*s", i, s); /* { dg-warning "format" "bad * argument types" } */ |
| 228 |
diag ("%*.*s", i, i, s); /* { dg-warning "format" "bad * argument types" } */ |
| 229 |
diag ("%*d\n", i1, i); /* { dg-warning "format" "bad * argument types" } */ |
| 230 |
diag ("%.*d\n", i2, i); /* { dg-warning "format" "bad * argument types" } */ |
| 231 |
diag ("%*.*ld\n", i1, i2, l); /* { dg-warning "format" "bad * argument types" } */ |
| 232 |
diag ("%ld", i); /* { dg-warning "format" "bad argument types" } */ |
| 233 |
diag ("%s", n); /* { dg-warning "format" "bad argument types" } */ |
| 234 |
|
| 235 |
/* Wrong number of arguments. */ |
| 236 |
diag ("%d%d", i); /* { dg-warning "matching" "wrong number of args" } */ |
| 237 |
diag ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */ |
| 238 |
/* Miscellaneous bogus constructions. */ |
| 239 |
diag (""); /* { dg-warning "zero-length" "warning for empty format" } */ |
| 240 |
diag ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */ |
| 241 |
diag ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */ |
| 242 |
diag ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */ |
| 243 |
diag (NULL); /* { dg-warning "null" "null format string warning" } */ |
| 244 |
diag ("%"); /* { dg-warning "trailing" "trailing % warning" } */ |
| 245 |
diag ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */ |
| 246 |
diag ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */ |
| 247 |
|
| 248 |
/* Make sure we still get warnings for regular printf. */ |
| 249 |
printf ("%d\n", ll); /* { dg-warning "format" "bad argument types" } */ |
| 250 |
} |