OSDN Git Service

* Makefile.def (target_modules): Add libssp.
[pf3gnuchains/gcc-fork.git] / gcc / c-format.c
1 /* Check calls to formatted I/O functions (-Wformat).
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "toplev.h"
29 #include "c-common.h"
30 #include "intl.h"
31 #include "diagnostic.h"
32 #include "langhooks.h"
33 #include "c-format.h"
34 \f
35 /* Set format warning options according to a -Wformat=n option.  */
36
37 void
38 set_Wformat (int setting)
39 {
40   warn_format = setting;
41   warn_format_extra_args = setting;
42   warn_format_zero_length = setting;
43   if (setting != 1)
44     {
45       warn_format_nonliteral = setting;
46       warn_format_security = setting;
47       warn_format_y2k = setting;
48     }
49   /* Make sure not to disable -Wnonnull if -Wformat=0 is specified.  */
50   if (setting)
51     warn_nonnull = setting;
52 }
53
54 \f
55 /* Handle attributes associated with format checking.  */
56
57 /* This must be in the same order as format_types, except for
58    format_type_error.  Target-specific format types do not have
59    matching enum values.  */
60 enum format_type { printf_format_type, asm_fprintf_format_type,
61                    gcc_diag_format_type, gcc_cdiag_format_type,
62                    gcc_cxxdiag_format_type,
63                    scanf_format_type, strftime_format_type,
64                    strfmon_format_type, format_type_error = -1};
65
66 typedef struct function_format_info
67 {
68   int format_type;                      /* type of format (printf, scanf, etc.) */
69   unsigned HOST_WIDE_INT format_num;    /* number of format argument */
70   unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
71 } function_format_info;
72
73 static bool decode_format_attr (tree, function_format_info *, int);
74 static int decode_format_type (const char *);
75
76 static bool check_format_string (tree argument,
77                                  unsigned HOST_WIDE_INT format_num,
78                                  int flags, bool *no_add_attrs);
79 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
80                           int validated_p);
81
82
83 /* Handle a "format_arg" attribute; arguments as in
84    struct attribute_spec.handler.  */
85 tree
86 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
87                              tree args, int flags, bool *no_add_attrs)
88 {
89   tree type = *node;
90   tree format_num_expr = TREE_VALUE (args);
91   unsigned HOST_WIDE_INT format_num = 0;
92   tree argument;
93
94   if (!get_constant (format_num_expr, &format_num, 0))
95     {
96       error ("format string has invalid operand number");
97       *no_add_attrs = true;
98       return NULL_TREE;
99     }
100
101   argument = TYPE_ARG_TYPES (type);
102   if (argument)
103     {
104       if (!check_format_string (argument, format_num, flags, no_add_attrs))
105         return NULL_TREE;
106     }
107
108   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
109       || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
110           != char_type_node))
111     {
112       if (!(flags & (int) ATTR_FLAG_BUILT_IN))
113         error ("function does not return string type");
114       *no_add_attrs = true;
115       return NULL_TREE;
116     }
117
118   return NULL_TREE;
119 }
120
121 /* Verify that the format_num argument is actually a string, in case
122    the format attribute is in error.  */
123 static bool
124 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
125                      int flags, bool *no_add_attrs)
126 {
127   unsigned HOST_WIDE_INT i;
128
129   for (i = 1; i != format_num; i++)
130     {
131       if (argument == 0)
132         break;
133       argument = TREE_CHAIN (argument);
134     }
135
136   if (!argument
137       || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
138       || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
139           != char_type_node))
140     {
141       if (!(flags & (int) ATTR_FLAG_BUILT_IN))
142         error ("format string argument not a string type");
143       *no_add_attrs = true;
144       return false;
145     }
146
147   return true;
148 }
149
150 /* Verify EXPR is a constant, and store its value.
151    If validated_p is true there should be no errors.
152    Returns true on success, false otherwise.  */
153 static bool
154 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
155 {
156   if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
157     {
158       gcc_assert (!validated_p);
159       return false;
160     }
161
162   *value = TREE_INT_CST_LOW (expr);
163
164   return true;
165 }
166
167 /* Decode the arguments to a "format" attribute into a
168    function_format_info structure.  It is already known that the list
169    is of the right length.  If VALIDATED_P is true, then these
170    attributes have already been validated and must not be erroneous;
171    if false, it will give an error message.  Returns true if the
172    attributes are successfully decoded, false otherwise.  */
173
174 static bool
175 decode_format_attr (tree args, function_format_info *info, int validated_p)
176 {
177   tree format_type_id = TREE_VALUE (args);
178   tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
179   tree first_arg_num_expr
180     = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
181
182   if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
183     {
184       gcc_assert (!validated_p);
185       error ("unrecognized format specifier");
186       return false;
187     }
188   else
189     {
190       const char *p = IDENTIFIER_POINTER (format_type_id);
191
192       info->format_type = decode_format_type (p);
193
194       if (info->format_type == format_type_error)
195         {
196           gcc_assert (!validated_p);
197           warning (OPT_Wformat, "%qE is an unrecognized format function type",
198                    format_type_id);
199           return false;
200         }
201     }
202
203   if (!get_constant (format_num_expr, &info->format_num, validated_p))
204     {
205       error ("format string has invalid operand number");
206       return false;
207     }
208
209   if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
210     {
211       error ("%<...%> has invalid operand number");
212       return false;
213     }
214
215   if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
216     {
217       gcc_assert (!validated_p);
218       error ("format string argument follows the args to be formatted");
219       return false;
220     }
221
222   return true;
223 }
224 \f
225 /* Check a call to a format function against a parameter list.  */
226
227 /* The C standard version C++ is treated as equivalent to
228    or inheriting from, for the purpose of format features supported.  */
229 #define CPLUSPLUS_STD_VER       STD_C94
230 /* The C standard version we are checking formats against when pedantic.  */
231 #define C_STD_VER               ((int) (c_dialect_cxx ()                   \
232                                  ? CPLUSPLUS_STD_VER                       \
233                                  : (flag_isoc99                            \
234                                     ? STD_C99                              \
235                                     : (flag_isoc94 ? STD_C94 : STD_C89))))
236 /* The name to give to the standard version we are warning about when
237    pedantic.  FEATURE_VER is the version in which the feature warned out
238    appeared, which is higher than C_STD_VER.  */
239 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx ()               \
240                                  ? "ISO C++"                    \
241                                  : ((FEATURE_VER) == STD_EXT    \
242                                     ? "ISO C"                   \
243                                     : "ISO C90"))
244 /* Adjust a C standard version, which may be STD_C9L, to account for
245    -Wno-long-long.  Returns other standard versions unchanged.  */
246 #define ADJ_STD(VER)            ((int) ((VER) == STD_C9L                      \
247                                        ? (warn_long_long ? STD_C99 : STD_C89) \
248                                        : (VER)))
249
250 /* Structure describing details of a type expected in format checking,
251    and the type to check against it.  */
252 typedef struct format_wanted_type
253 {
254   /* The type wanted.  */
255   tree wanted_type;
256   /* The name of this type to use in diagnostics.  */
257   const char *wanted_type_name;
258   /* The level of indirection through pointers at which this type occurs.  */
259   int pointer_count;
260   /* Whether, when pointer_count is 1, to allow any character type when
261      pedantic, rather than just the character or void type specified.  */
262   int char_lenient_flag;
263   /* Whether the argument, dereferenced once, is written into and so the
264      argument must not be a pointer to a const-qualified type.  */
265   int writing_in_flag;
266   /* Whether the argument, dereferenced once, is read from and so
267      must not be a NULL pointer.  */
268   int reading_from_flag;
269   /* If warnings should be of the form "field precision should have
270      type 'int'", the name to use (in this case "field precision"),
271      otherwise NULL, for "format expects type 'long'" type
272      messages.  */
273   const char *name;
274   /* The actual parameter to check against the wanted type.  */
275   tree param;
276   /* The argument number of that parameter.  */
277   int arg_num;
278   /* The next type to check for this format conversion, or NULL if none.  */
279   struct format_wanted_type *next;
280 } format_wanted_type;
281
282
283 static const format_length_info printf_length_specs[] =
284 {
285   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
286   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
287   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
288   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
289   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
290   { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
291   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
292   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
293   { NULL, 0, 0, NULL, 0, 0 }
294 };
295
296 /* Length specifiers valid for asm_fprintf.  */
297 static const format_length_info asm_fprintf_length_specs[] =
298 {
299   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
300   { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
301   { NULL, 0, 0, NULL, 0, 0 }
302 };
303
304 /* Length specifiers valid for GCC diagnostics.  */
305 static const format_length_info gcc_diag_length_specs[] =
306 {
307   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
308   { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
309   { NULL, 0, 0, NULL, 0, 0 }
310 };
311
312 /* The custom diagnostics all accept the same length specifiers.  */
313 #define gcc_cdiag_length_specs gcc_diag_length_specs
314 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
315
316 /* This differs from printf_length_specs only in that "Z" is not accepted.  */
317 static const format_length_info scanf_length_specs[] =
318 {
319   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
320   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
321   { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
322   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
323   { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
324   { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
325   { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
326   { NULL, 0, 0, NULL, 0, 0 }
327 };
328
329
330 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
331    make no sense for a format type not part of any C standard version.  */
332 static const format_length_info strfmon_length_specs[] =
333 {
334   /* A GNU extension.  */
335   { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
336   { NULL, 0, 0, NULL, 0, 0 }
337 };
338
339 static const format_flag_spec printf_flag_specs[] =
340 {
341   { ' ',  0, 0, N_("' ' flag"),        N_("the ' ' printf flag"),              STD_C89 },
342   { '+',  0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
343   { '#',  0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
344   { '0',  0, 0, N_("'0' flag"),        N_("the '0' printf flag"),              STD_C89 },
345   { '-',  0, 0, N_("'-' flag"),        N_("the '-' printf flag"),              STD_C89 },
346   { '\'', 0, 0, N_("''' flag"),        N_("the ''' printf flag"),              STD_EXT },
347   { 'I',  0, 0, N_("'I' flag"),        N_("the 'I' printf flag"),              STD_EXT },
348   { 'w',  0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
349   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
350   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
351   { 0, 0, 0, NULL, NULL, 0 }
352 };
353
354
355 static const format_flag_pair printf_flag_pairs[] =
356 {
357   { ' ', '+', 1, 0   },
358   { '0', '-', 1, 0   },
359   { '0', 'p', 1, 'i' },
360   { 0, 0, 0, 0 }
361 };
362
363 static const format_flag_spec asm_fprintf_flag_specs[] =
364 {
365   { ' ',  0, 0, N_("' ' flag"),        N_("the ' ' printf flag"),              STD_C89 },
366   { '+',  0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
367   { '#',  0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
368   { '0',  0, 0, N_("'0' flag"),        N_("the '0' printf flag"),              STD_C89 },
369   { '-',  0, 0, N_("'-' flag"),        N_("the '-' printf flag"),              STD_C89 },
370   { 'w',  0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
371   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
372   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
373   { 0, 0, 0, NULL, NULL, 0 }
374 };
375
376 static const format_flag_pair asm_fprintf_flag_pairs[] =
377 {
378   { ' ', '+', 1, 0   },
379   { '0', '-', 1, 0   },
380   { '0', 'p', 1, 'i' },
381   { 0, 0, 0, 0 }
382 };
383
384 static const format_flag_pair gcc_diag_flag_pairs[] =
385 {
386   { 0, 0, 0, 0 }
387 };
388
389 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
390 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
391
392 static const format_flag_spec gcc_diag_flag_specs[] =
393 {
394   { 'q',  0, 0, N_("'q' flag"),        N_("the 'q' diagnostic flag"),          STD_C89 },
395   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
396   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
397   { 0, 0, 0, NULL, NULL, 0 }
398 };
399
400 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
401
402 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
403 {
404   { '+',  0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
405   { '#',  0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
406   { 'q',  0, 0, N_("'q' flag"),        N_("the 'q' diagnostic flag"),          STD_C89 },
407   { 'p',  0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
408   { 'L',  0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
409   { 0, 0, 0, NULL, NULL, 0 }
410 };
411
412 static const format_flag_spec scanf_flag_specs[] =
413 {
414   { '*',  0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
415   { 'a',  0, 0, N_("'a' flag"),               N_("the 'a' scanf flag"),                       STD_EXT },
416   { 'w',  0, 0, N_("field width"),            N_("field width in scanf format"),              STD_C89 },
417   { 'L',  0, 0, N_("length modifier"),        N_("length modifier in scanf format"),          STD_C89 },
418   { '\'', 0, 0, N_("''' flag"),               N_("the ''' scanf flag"),                       STD_EXT },
419   { 'I',  0, 0, N_("'I' flag"),               N_("the 'I' scanf flag"),                       STD_EXT },
420   { 0, 0, 0, NULL, NULL, 0 }
421 };
422
423
424 static const format_flag_pair scanf_flag_pairs[] =
425 {
426   { '*', 'L', 0, 0 },
427   { 0, 0, 0, 0 }
428 };
429
430
431 static const format_flag_spec strftime_flag_specs[] =
432 {
433   { '_', 0,   0, N_("'_' flag"),     N_("the '_' strftime flag"),          STD_EXT },
434   { '-', 0,   0, N_("'-' flag"),     N_("the '-' strftime flag"),          STD_EXT },
435   { '0', 0,   0, N_("'0' flag"),     N_("the '0' strftime flag"),          STD_EXT },
436   { '^', 0,   0, N_("'^' flag"),     N_("the '^' strftime flag"),          STD_EXT },
437   { '#', 0,   0, N_("'#' flag"),     N_("the '#' strftime flag"),          STD_EXT },
438   { 'w', 0,   0, N_("field width"),  N_("field width in strftime format"), STD_EXT },
439   { 'E', 0,   0, N_("'E' modifier"), N_("the 'E' strftime modifier"),      STD_C99 },
440   { 'O', 0,   0, N_("'O' modifier"), N_("the 'O' strftime modifier"),      STD_C99 },
441   { 'O', 'o', 0, NULL,               N_("the 'O' modifier"),               STD_EXT },
442   { 0, 0, 0, NULL, NULL, 0 }
443 };
444
445
446 static const format_flag_pair strftime_flag_pairs[] =
447 {
448   { 'E', 'O', 0, 0 },
449   { '_', '-', 0, 0 },
450   { '_', '0', 0, 0 },
451   { '-', '0', 0, 0 },
452   { '^', '#', 0, 0 },
453   { 0, 0, 0, 0 }
454 };
455
456
457 static const format_flag_spec strfmon_flag_specs[] =
458 {
459   { '=',  0, 1, N_("fill character"),  N_("fill character in strfmon format"),  STD_C89 },
460   { '^',  0, 0, N_("'^' flag"),        N_("the '^' strfmon flag"),              STD_C89 },
461   { '+',  0, 0, N_("'+' flag"),        N_("the '+' strfmon flag"),              STD_C89 },
462   { '(',  0, 0, N_("'(' flag"),        N_("the '(' strfmon flag"),              STD_C89 },
463   { '!',  0, 0, N_("'!' flag"),        N_("the '!' strfmon flag"),              STD_C89 },
464   { '-',  0, 0, N_("'-' flag"),        N_("the '-' strfmon flag"),              STD_C89 },
465   { 'w',  0, 0, N_("field width"),     N_("field width in strfmon format"),     STD_C89 },
466   { '#',  0, 0, N_("left precision"),  N_("left precision in strfmon format"),  STD_C89 },
467   { 'p',  0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
468   { 'L',  0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
469   { 0, 0, 0, NULL, NULL, 0 }
470 };
471
472 static const format_flag_pair strfmon_flag_pairs[] =
473 {
474   { '+', '(', 0, 0 },
475   { 0, 0, 0, 0 }
476 };
477
478
479 static const format_char_info print_char_table[] =
480 {
481   /* C89 conversion specifiers.  */
482   { "di",  0, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  TEX_LL,  T99_SST, T99_PD,  T99_IM  }, "-wp0 +'I",  "i",  NULL },
483   { "oxX", 0, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "-wp0#",     "i",  NULL },
484   { "u",   0, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "-wp0'I",    "i",  NULL },
485   { "fgG", 0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#'I", "",   NULL },
486   { "eE",  0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#I",  "",   NULL },
487   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T94_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",        "",   NULL },
488   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",       "cR", NULL },
489   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",        "c",  NULL },
490   { "n",   1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",          "W",  NULL },
491   /* C99 conversion specifiers.  */
492   { "F",   0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#'I", "",   NULL },
493   { "aA",  0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +#",   "",   NULL },
494   /* X/Open conversion specifiers.  */
495   { "C",   0, STD_EXT, { TEX_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",        "",   NULL },
496   { "S",   1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",       "R",  NULL },
497   /* GNU conversion specifiers.  */
498   { "m",   0, STD_EXT, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",       "",   NULL },
499   { NULL,  0, 0, NOLENGTHS, NULL, NULL, NULL }
500 };
501
502 static const format_char_info asm_fprintf_char_table[] =
503 {
504   /* C89 conversion specifiers.  */
505   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +",  "i", NULL },
506   { "oxX", 0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0#",   "i", NULL },
507   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0",    "i", NULL },
508   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       "", NULL },
509   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",    "cR", NULL },
510
511   /* asm_fprintf conversion specifiers.  */
512   { "O",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
513   { "R",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
514   { "I",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
515   { "L",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
516   { "U",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
517   { "r",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",  "", NULL },
518   { "@",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
519   { NULL,  0, 0, NOLENGTHS, NULL, NULL, NULL }
520 };
521
522 static const format_char_info gcc_diag_char_table[] =
523 {
524   /* C89 conversion specifiers.  */
525   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
526   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
527   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
528   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
529   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
530   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
531
532   /* Custom conversion specifiers.  */
533
534   /* %H will require "location_t" at runtime.  */
535   { "H",   0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
536
537   /* These will require a "tree" at runtime.  */
538   { "J", 0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",    "",   NULL },
539
540   { "<>'", 0, STD_C89, NOARGUMENTS, "",      "",   NULL },
541   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
542   { NULL,  0, 0, NOLENGTHS, NULL, NULL, NULL }
543 };
544
545 static const format_char_info gcc_cdiag_char_table[] =
546 {
547   /* C89 conversion specifiers.  */
548   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
549   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
550   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
551   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
552   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
553   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
554
555   /* Custom conversion specifiers.  */
556
557   /* %H will require "location_t" at runtime.  */
558   { "H",   0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
559
560   /* These will require a "tree" at runtime.  */
561   { "DEFJT", 0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q", "",   NULL },
562
563   { "<>'", 0, STD_C89, NOARGUMENTS, "",      "",   NULL },
564   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
565   { NULL,  0, 0, NOLENGTHS, NULL, NULL, NULL }
566 };
567
568 static const format_char_info gcc_cxxdiag_char_table[] =
569 {
570   /* C89 conversion specifiers.  */
571   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
572   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
573   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
574   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
575   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
576   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
577
578   /* Custom conversion specifiers.  */
579
580   /* %H will require "location_t" at runtime.  */
581   { "H",   0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
582
583   /* These will require a "tree" at runtime.  */
584   { "ADEFJTV",0,STD_C89,{ T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+#",   "",   NULL },
585
586   /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.)  */
587   { "CLOPQ",0,STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
588
589   { "<>'", 0, STD_C89, NOARGUMENTS, "",      "",   NULL },
590   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
591   { NULL,  0, 0, NOLENGTHS, NULL, NULL, NULL }
592 };
593
594 static const format_char_info scan_char_table[] =
595 {
596   /* C89 conversion specifiers.  */
597   { "di",    1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  TEX_LL,  T99_SST, T99_PD,  T99_IM  }, "*w'I", "W",   NULL },
598   { "u",     1, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "*w'I", "W",   NULL },
599   { "oxX",   1, STD_C89, { T89_UI,  T99_UC,  T89_US,  T89_UL,  T9L_ULL, TEX_ULL, T99_ST,  T99_UPD, T99_UIM }, "*w",   "W",   NULL },
600   { "efgEG", 1, STD_C89, { T89_F,   BADLEN,  BADLEN,  T89_D,   BADLEN,  T89_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w'",  "W",   NULL },
601   { "c",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "cW",  NULL },
602   { "s",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "cW",  NULL },
603   { "[",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "cW[", NULL },
604   { "p",     2, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "W",   NULL },
605   { "n",     1, STD_C89, { T89_I,   T99_SC,  T89_S,   T89_L,   T9L_LL,  BADLEN,  T99_SST, T99_PD,  T99_IM  }, "",     "W",   NULL },
606   /* C99 conversion specifiers.  */
607   { "FaA",   1, STD_C99, { T99_F,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN  }, "*w'",  "W",   NULL },
608   /* X/Open conversion specifiers.  */
609   { "C",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*w",   "W",   NULL },
610   { "S",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "*aw",  "W",   NULL },
611   { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
612 };
613
614 static const format_char_info time_char_table[] =
615 {
616   /* C89 conversion specifiers.  */
617   { "ABZab",            0, STD_C89, NOLENGTHS, "^#",     "",   NULL },
618   { "cx",               0, STD_C89, NOLENGTHS, "E",      "3",  NULL },
619   { "HIMSUWdmw",        0, STD_C89, NOLENGTHS, "-_0Ow",  "",   NULL },
620   { "j",                0, STD_C89, NOLENGTHS, "-_0Ow",  "o",  NULL },
621   { "p",                0, STD_C89, NOLENGTHS, "#",      "",   NULL },
622   { "X",                0, STD_C89, NOLENGTHS, "E",      "",   NULL },
623   { "y",                0, STD_C89, NOLENGTHS, "EO-_0w", "4",  NULL },
624   { "Y",                0, STD_C89, NOLENGTHS, "-_0EOw", "o",  NULL },
625   { "%",                0, STD_C89, NOLENGTHS, "",       "",   NULL },
626   /* C99 conversion specifiers.  */
627   { "C",                0, STD_C99, NOLENGTHS, "-_0EOw", "o",  NULL },
628   { "D",                0, STD_C99, NOLENGTHS, "",       "2",  NULL },
629   { "eVu",              0, STD_C99, NOLENGTHS, "-_0Ow",  "",   NULL },
630   { "FRTnrt",           0, STD_C99, NOLENGTHS, "",       "",   NULL },
631   { "g",                0, STD_C99, NOLENGTHS, "O-_0w",  "2o", NULL },
632   { "G",                0, STD_C99, NOLENGTHS, "-_0Ow",  "o",  NULL },
633   { "h",                0, STD_C99, NOLENGTHS, "^#",     "",   NULL },
634   { "z",                0, STD_C99, NOLENGTHS, "O",      "o",  NULL },
635   /* GNU conversion specifiers.  */
636   { "kls",              0, STD_EXT, NOLENGTHS, "-_0Ow",  "",   NULL },
637   { "P",                0, STD_EXT, NOLENGTHS, "",       "",   NULL },
638   { NULL,               0, 0, NOLENGTHS, NULL, NULL, NULL }
639 };
640
641 static const format_char_info monetary_char_table[] =
642 {
643   { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
644   { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
645 };
646
647 /* This must be in the same order as enum format_type.  */
648 static const format_kind_info format_types_orig[] =
649 {
650   { "printf",   printf_length_specs,  print_char_table, " +#0-'I", NULL, 
651     printf_flag_specs, printf_flag_pairs,
652     FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
653     'w', 0, 'p', 0, 'L',
654     &integer_type_node, &integer_type_node
655   },
656   { "asm_fprintf",   asm_fprintf_length_specs,  asm_fprintf_char_table, " +#0-", NULL, 
657     asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
658     FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
659     'w', 0, 'p', 0, 'L',
660     NULL, NULL
661   },
662   { "gcc_diag",   gcc_diag_length_specs,  gcc_diag_char_table, "q", NULL, 
663     gcc_diag_flag_specs, gcc_diag_flag_pairs,
664     FMT_FLAG_ARG_CONVERT,
665     0, 0, 'p', 0, 'L',
666     NULL, &integer_type_node
667   },
668   { "gcc_cdiag",   gcc_cdiag_length_specs,  gcc_cdiag_char_table, "q", NULL, 
669     gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
670     FMT_FLAG_ARG_CONVERT,
671     0, 0, 'p', 0, 'L',
672     NULL, &integer_type_node
673   },
674   { "gcc_cxxdiag",   gcc_cxxdiag_length_specs,  gcc_cxxdiag_char_table, "q+#", NULL, 
675     gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
676     FMT_FLAG_ARG_CONVERT,
677     0, 0, 'p', 0, 'L',
678     NULL, &integer_type_node
679   },
680   { "scanf",    scanf_length_specs,   scan_char_table,  "*'I", NULL, 
681     scanf_flag_specs, scanf_flag_pairs,
682     FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
683     'w', 0, 0, '*', 'L',
684     NULL, NULL
685   },
686   { "strftime", NULL,                 time_char_table,  "_-0^#", "EO",
687     strftime_flag_specs, strftime_flag_pairs,
688     FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
689     NULL, NULL
690   },
691   { "strfmon",  strfmon_length_specs, monetary_char_table, "=^+(!-", NULL, 
692     strfmon_flag_specs, strfmon_flag_pairs,
693     FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
694     NULL, NULL
695   }
696 };
697
698 /* This layer of indirection allows GCC to reassign format_types with
699    new data if necessary, while still allowing the original data to be
700    const.  */
701 static const format_kind_info *format_types = format_types_orig;
702 /* We can modify this one.  We also add target-specific format types
703    to the end of the array.  */
704 static format_kind_info *dynamic_format_types;
705
706 static int n_format_types = ARRAY_SIZE (format_types_orig);
707
708 /* Structure detailing the results of checking a format function call
709    where the format expression may be a conditional expression with
710    many leaves resulting from nested conditional expressions.  */
711 typedef struct
712 {
713   /* Number of leaves of the format argument that could not be checked
714      as they were not string literals.  */
715   int number_non_literal;
716   /* Number of leaves of the format argument that were null pointers or
717      string literals, but had extra format arguments.  */
718   int number_extra_args;
719   /* Number of leaves of the format argument that were null pointers or
720      string literals, but had extra format arguments and used $ operand
721      numbers.  */
722   int number_dollar_extra_args;
723   /* Number of leaves of the format argument that were wide string
724      literals.  */
725   int number_wide;
726   /* Number of leaves of the format argument that were empty strings.  */
727   int number_empty;
728   /* Number of leaves of the format argument that were unterminated
729      strings.  */
730   int number_unterminated;
731   /* Number of leaves of the format argument that were not counted above.  */
732   int number_other;
733 } format_check_results;
734
735 typedef struct
736 {
737   format_check_results *res;
738   function_format_info *info;
739   tree params;
740 } format_check_context;
741
742 static void check_format_info (function_format_info *, tree);
743 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
744 static void check_format_info_main (format_check_results *,
745                                     function_format_info *,
746                                     const char *, int, tree,
747                                     unsigned HOST_WIDE_INT);
748
749 static void init_dollar_format_checking (int, tree);
750 static int maybe_read_dollar_number (const char **, int,
751                                      tree, tree *, const format_kind_info *);
752 static bool avoid_dollar_number (const char *);
753 static void finish_dollar_format_checking (format_check_results *, int);
754
755 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
756                                               int, const char *);
757
758 static void check_format_types (format_wanted_type *, const char *, int);
759 static void format_type_warning (const char *, const char *, int, tree,
760                                  int, const char *, tree, int);
761
762 /* Decode a format type from a string, returning the type, or
763    format_type_error if not valid, in which case the caller should print an
764    error message.  */
765 static int
766 decode_format_type (const char *s)
767 {
768   int i;
769   int slen;
770   slen = strlen (s);
771   for (i = 0; i < n_format_types; i++)
772     {
773       int alen;
774       if (!strcmp (s, format_types[i].name))
775         return i;
776       alen = strlen (format_types[i].name);
777       if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
778           && s[slen - 1] == '_' && s[slen - 2] == '_'
779           && !strncmp (s + 2, format_types[i].name, alen))
780         return i;
781     }
782   return format_type_error;
783 }
784
785 \f
786 /* Check the argument list of a call to printf, scanf, etc.
787    ATTRS are the attributes on the function type.
788    PARAMS is the list of argument values.  Also, if -Wmissing-format-attribute,
789    warn for calls to vprintf or vscanf in functions with no such format
790    attribute themselves.  */
791
792 void
793 check_function_format (tree attrs, tree params)
794 {
795   tree a;
796
797   /* See if this function has any format attributes.  */
798   for (a = attrs; a; a = TREE_CHAIN (a))
799     {
800       if (is_attribute_p ("format", TREE_PURPOSE (a)))
801         {
802           /* Yup; check it.  */
803           function_format_info info;
804           decode_format_attr (TREE_VALUE (a), &info, 1);
805           check_format_info (&info, params);
806           if (warn_missing_format_attribute && info.first_arg_num == 0
807               && (format_types[info.format_type].flags
808                   & (int) FMT_FLAG_ARG_CONVERT))
809             {
810               tree c;
811               for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
812                    c;
813                    c = TREE_CHAIN (c))
814                 if (is_attribute_p ("format", TREE_PURPOSE (c))
815                     && (decode_format_type (IDENTIFIER_POINTER
816                                             (TREE_VALUE (TREE_VALUE (c))))
817                         == info.format_type))
818                   break;
819               if (c == NULL_TREE)
820                 {
821                   /* Check if the current function has a parameter to which
822                      the format attribute could be attached; if not, it
823                      can't be a candidate for a format attribute, despite
824                      the vprintf-like or vscanf-like call.  */
825                   tree args;
826                   for (args = DECL_ARGUMENTS (current_function_decl);
827                        args != 0;
828                        args = TREE_CHAIN (args))
829                     {
830                       if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
831                           && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
832                               == char_type_node))
833                         break;
834                     }
835                   if (args != 0)
836                     warning (OPT_Wattributes, "function might be possible "
837                              "candidate for %qs format attribute",
838                              format_types[info.format_type].name);
839                 }
840             }
841         }
842     }
843 }
844
845
846 /* Variables used by the checking of $ operand number formats.  */
847 static char *dollar_arguments_used = NULL;
848 static char *dollar_arguments_pointer_p = NULL;
849 static int dollar_arguments_alloc = 0;
850 static int dollar_arguments_count;
851 static int dollar_first_arg_num;
852 static int dollar_max_arg_used;
853 static int dollar_format_warned;
854
855 /* Initialize the checking for a format string that may contain $
856    parameter number specifications; we will need to keep track of whether
857    each parameter has been used.  FIRST_ARG_NUM is the number of the first
858    argument that is a parameter to the format, or 0 for a vprintf-style
859    function; PARAMS is the list of arguments starting at this argument.  */
860
861 static void
862 init_dollar_format_checking (int first_arg_num, tree params)
863 {
864   tree oparams = params;
865
866   dollar_first_arg_num = first_arg_num;
867   dollar_arguments_count = 0;
868   dollar_max_arg_used = 0;
869   dollar_format_warned = 0;
870   if (first_arg_num > 0)
871     {
872       while (params)
873         {
874           dollar_arguments_count++;
875           params = TREE_CHAIN (params);
876         }
877     }
878   if (dollar_arguments_alloc < dollar_arguments_count)
879     {
880       if (dollar_arguments_used)
881         free (dollar_arguments_used);
882       if (dollar_arguments_pointer_p)
883         free (dollar_arguments_pointer_p);
884       dollar_arguments_alloc = dollar_arguments_count;
885       dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
886       dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
887     }
888   if (dollar_arguments_alloc)
889     {
890       memset (dollar_arguments_used, 0, dollar_arguments_alloc);
891       if (first_arg_num > 0)
892         {
893           int i = 0;
894           params = oparams;
895           while (params)
896             {
897               dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
898                                                == POINTER_TYPE);
899               params = TREE_CHAIN (params);
900               i++;
901             }
902         }
903     }
904 }
905
906
907 /* Look for a decimal number followed by a $ in *FORMAT.  If DOLLAR_NEEDED
908    is set, it is an error if one is not found; otherwise, it is OK.  If
909    such a number is found, check whether it is within range and mark that
910    numbered operand as being used for later checking.  Returns the operand
911    number if found and within range, zero if no such number was found and
912    this is OK, or -1 on error.  PARAMS points to the first operand of the
913    format; PARAM_PTR is made to point to the parameter referred to.  If
914    a $ format is found, *FORMAT is updated to point just after it.  */
915
916 static int
917 maybe_read_dollar_number (const char **format,
918                           int dollar_needed, tree params, tree *param_ptr,
919                           const format_kind_info *fki)
920 {
921   int argnum;
922   int overflow_flag;
923   const char *fcp = *format;
924   if (!ISDIGIT (*fcp))
925     {
926       if (dollar_needed)
927         {
928           warning (OPT_Wformat, "missing $ operand number in format");
929           return -1;
930         }
931       else
932         return 0;
933     }
934   argnum = 0;
935   overflow_flag = 0;
936   while (ISDIGIT (*fcp))
937     {
938       int nargnum;
939       nargnum = 10 * argnum + (*fcp - '0');
940       if (nargnum < 0 || nargnum / 10 != argnum)
941         overflow_flag = 1;
942       argnum = nargnum;
943       fcp++;
944     }
945   if (*fcp != '$')
946     {
947       if (dollar_needed)
948         {
949           warning (OPT_Wformat, "missing $ operand number in format");
950           return -1;
951         }
952       else
953         return 0;
954     }
955   *format = fcp + 1;
956   if (pedantic && !dollar_format_warned)
957     {
958       warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
959                C_STD_NAME (STD_EXT));
960       dollar_format_warned = 1;
961     }
962   if (overflow_flag || argnum == 0
963       || (dollar_first_arg_num && argnum > dollar_arguments_count))
964     {
965       warning (OPT_Wformat, "operand number out of range in format");
966       return -1;
967     }
968   if (argnum > dollar_max_arg_used)
969     dollar_max_arg_used = argnum;
970   /* For vprintf-style functions we may need to allocate more memory to
971      track which arguments are used.  */
972   while (dollar_arguments_alloc < dollar_max_arg_used)
973     {
974       int nalloc;
975       nalloc = 2 * dollar_arguments_alloc + 16;
976       dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
977                                           nalloc);
978       dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
979                                                nalloc);
980       memset (dollar_arguments_used + dollar_arguments_alloc, 0,
981               nalloc - dollar_arguments_alloc);
982       dollar_arguments_alloc = nalloc;
983     }
984   if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
985       && dollar_arguments_used[argnum - 1] == 1)
986     {
987       dollar_arguments_used[argnum - 1] = 2;
988       warning (OPT_Wformat, "format argument %d used more than once in %s format",
989                argnum, fki->name);
990     }
991   else
992     dollar_arguments_used[argnum - 1] = 1;
993   if (dollar_first_arg_num)
994     {
995       int i;
996       *param_ptr = params;
997       for (i = 1; i < argnum && *param_ptr != 0; i++)
998         *param_ptr = TREE_CHAIN (*param_ptr);
999
1000       /* This case shouldn't be caught here.  */
1001       gcc_assert (*param_ptr);
1002     }
1003   else
1004     *param_ptr = 0;
1005   return argnum;
1006 }
1007
1008 /* Ensure that FORMAT does not start with a decimal number followed by
1009    a $; give a diagnostic and return true if it does, false otherwise.  */
1010
1011 static bool
1012 avoid_dollar_number (const char *format)
1013 {
1014   if (!ISDIGIT (*format))
1015     return false;
1016   while (ISDIGIT (*format))
1017     format++;
1018   if (*format == '$')
1019     {
1020       warning (OPT_Wformat, "$ operand number used after format without operand number");
1021       return true;
1022     }
1023   return false;
1024 }
1025
1026
1027 /* Finish the checking for a format string that used $ operand number formats
1028    instead of non-$ formats.  We check for unused operands before used ones
1029    (a serious error, since the implementation of the format function
1030    can't know what types to pass to va_arg to find the later arguments).
1031    and for unused operands at the end of the format (if we know how many
1032    arguments the format had, so not for vprintf).  If there were operand
1033    numbers out of range on a non-vprintf-style format, we won't have reached
1034    here.  If POINTER_GAP_OK, unused arguments are OK if all arguments are
1035    pointers.  */
1036
1037 static void
1038 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1039 {
1040   int i;
1041   bool found_pointer_gap = false;
1042   for (i = 0; i < dollar_max_arg_used; i++)
1043     {
1044       if (!dollar_arguments_used[i])
1045         {
1046           if (pointer_gap_ok && (dollar_first_arg_num == 0
1047                                  || dollar_arguments_pointer_p[i]))
1048             found_pointer_gap = true;
1049           else
1050             warning (OPT_Wformat,
1051                      "format argument %d unused before used argument %d in $-style format",
1052                      i + 1, dollar_max_arg_used);
1053         }
1054     }
1055   if (found_pointer_gap
1056       || (dollar_first_arg_num
1057           && dollar_max_arg_used < dollar_arguments_count))
1058     {
1059       res->number_other--;
1060       res->number_dollar_extra_args++;
1061     }
1062 }
1063
1064
1065 /* Retrieve the specification for a format flag.  SPEC contains the
1066    specifications for format flags for the applicable kind of format.
1067    FLAG is the flag in question.  If PREDICATES is NULL, the basic
1068    spec for that flag must be retrieved and must exist.  If
1069    PREDICATES is not NULL, it is a string listing possible predicates
1070    for the spec entry; if an entry predicated on any of these is
1071    found, it is returned, otherwise NULL is returned.  */
1072
1073 static const format_flag_spec *
1074 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1075 {
1076   int i;
1077   for (i = 0; spec[i].flag_char != 0; i++)
1078     {
1079       if (spec[i].flag_char != flag)
1080         continue;
1081       if (predicates != NULL)
1082         {
1083           if (spec[i].predicate != 0
1084               && strchr (predicates, spec[i].predicate) != 0)
1085             return &spec[i];
1086         }
1087       else if (spec[i].predicate == 0)
1088         return &spec[i];
1089     }
1090   gcc_assert (predicates);
1091   return NULL;
1092 }
1093
1094
1095 /* Check the argument list of a call to printf, scanf, etc.
1096    INFO points to the function_format_info structure.
1097    PARAMS is the list of argument values.  */
1098
1099 static void
1100 check_format_info (function_format_info *info, tree params)
1101 {
1102   format_check_context format_ctx;
1103   unsigned HOST_WIDE_INT arg_num;
1104   tree format_tree;
1105   format_check_results res;
1106   /* Skip to format argument.  If the argument isn't available, there's
1107      no work for us to do; prototype checking will catch the problem.  */
1108   for (arg_num = 1; ; ++arg_num)
1109     {
1110       if (params == 0)
1111         return;
1112       if (arg_num == info->format_num)
1113         break;
1114       params = TREE_CHAIN (params);
1115     }
1116   format_tree = TREE_VALUE (params);
1117   params = TREE_CHAIN (params);
1118   if (format_tree == 0)
1119     return;
1120
1121   res.number_non_literal = 0;
1122   res.number_extra_args = 0;
1123   res.number_dollar_extra_args = 0;
1124   res.number_wide = 0;
1125   res.number_empty = 0;
1126   res.number_unterminated = 0;
1127   res.number_other = 0;
1128
1129   format_ctx.res = &res;
1130   format_ctx.info = info;
1131   format_ctx.params = params;
1132
1133   check_function_arguments_recurse (check_format_arg, &format_ctx,
1134                                     format_tree, arg_num);
1135
1136   if (res.number_non_literal > 0)
1137     {
1138       /* Functions taking a va_list normally pass a non-literal format
1139          string.  These functions typically are declared with
1140          first_arg_num == 0, so avoid warning in those cases.  */
1141       if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1142         {
1143           /* For strftime-like formats, warn for not checking the format
1144              string; but there are no arguments to check.  */
1145           warning (OPT_Wformat_nonliteral,
1146                    "format not a string literal, format string not checked");
1147         }
1148       else if (info->first_arg_num != 0)
1149         {
1150           /* If there are no arguments for the format at all, we may have
1151              printf (foo) which is likely to be a security hole.  */
1152           while (arg_num + 1 < info->first_arg_num)
1153             {
1154               if (params == 0)
1155                 break;
1156               params = TREE_CHAIN (params);
1157               ++arg_num;
1158             }
1159           if (params == 0 && warn_format_security)
1160             warning (OPT_Wformat_security,
1161                      "format not a string literal and no format arguments");
1162           else if (params == 0 && warn_format_nonliteral)
1163             warning (OPT_Wformat_nonliteral,
1164                      "format not a string literal and no format arguments");
1165           else
1166             warning (OPT_Wformat_nonliteral,
1167                      "format not a string literal, argument types not checked");
1168         }
1169     }
1170
1171   /* If there were extra arguments to the format, normally warn.  However,
1172      the standard does say extra arguments are ignored, so in the specific
1173      case where we have multiple leaves (conditional expressions or
1174      ngettext) allow extra arguments if at least one leaf didn't have extra
1175      arguments, but was otherwise OK (either non-literal or checked OK).
1176      If the format is an empty string, this should be counted similarly to the
1177      case of extra format arguments.  */
1178   if (res.number_extra_args > 0 && res.number_non_literal == 0
1179       && res.number_other == 0)
1180     warning (OPT_Wformat_extra_args, "too many arguments for format");
1181   if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1182       && res.number_other == 0)
1183     warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1184   if (res.number_empty > 0 && res.number_non_literal == 0
1185       && res.number_other == 0)
1186     warning (OPT_Wformat_zero_length, "zero-length %s format string",
1187              format_types[info->format_type].name);
1188
1189   if (res.number_wide > 0)
1190     warning (OPT_Wformat, "format is a wide character string");
1191
1192   if (res.number_unterminated > 0)
1193     warning (OPT_Wformat, "unterminated format string");
1194 }
1195
1196 /* Callback from check_function_arguments_recurse to check a
1197    format string.  FORMAT_TREE is the format parameter.  ARG_NUM
1198    is the number of the format argument.  CTX points to a
1199    format_check_context.  */
1200
1201 static void
1202 check_format_arg (void *ctx, tree format_tree,
1203                   unsigned HOST_WIDE_INT arg_num)
1204 {
1205   format_check_context *format_ctx = (format_check_context *) ctx;
1206   format_check_results *res = format_ctx->res;
1207   function_format_info *info = format_ctx->info;
1208   tree params = format_ctx->params;
1209
1210   int format_length;
1211   HOST_WIDE_INT offset;
1212   const char *format_chars;
1213   tree array_size = 0;
1214   tree array_init;
1215
1216   if (integer_zerop (format_tree))
1217     {
1218       /* Skip to first argument to check, so we can see if this format
1219          has any arguments (it shouldn't).  */
1220       while (arg_num + 1 < info->first_arg_num)
1221         {
1222           if (params == 0)
1223             return;
1224           params = TREE_CHAIN (params);
1225           ++arg_num;
1226         }
1227
1228       if (params == 0)
1229         res->number_other++;
1230       else
1231         res->number_extra_args++;
1232
1233       return;
1234     }
1235
1236   offset = 0;
1237   if (TREE_CODE (format_tree) == PLUS_EXPR)
1238     {
1239       tree arg0, arg1;
1240
1241       arg0 = TREE_OPERAND (format_tree, 0);
1242       arg1 = TREE_OPERAND (format_tree, 1);
1243       STRIP_NOPS (arg0);
1244       STRIP_NOPS (arg1);
1245       if (TREE_CODE (arg1) == INTEGER_CST)
1246         format_tree = arg0;
1247       else if (TREE_CODE (arg0) == INTEGER_CST)
1248         {
1249           format_tree = arg1;
1250           arg1 = arg0;
1251         }
1252       else
1253         {
1254           res->number_non_literal++;
1255           return;
1256         }
1257       if (!host_integerp (arg1, 0)
1258           || (offset = tree_low_cst (arg1, 0)) < 0)
1259         {
1260           res->number_non_literal++;
1261           return;
1262         }
1263     }
1264   if (TREE_CODE (format_tree) != ADDR_EXPR)
1265     {
1266       res->number_non_literal++;
1267       return;
1268     }
1269   format_tree = TREE_OPERAND (format_tree, 0);
1270   if (TREE_CODE (format_tree) == ARRAY_REF
1271       && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1272       && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1273     format_tree = TREE_OPERAND (format_tree, 0);
1274   if (TREE_CODE (format_tree) == VAR_DECL
1275       && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1276       && (array_init = decl_constant_value (format_tree)) != format_tree
1277       && TREE_CODE (array_init) == STRING_CST)
1278     {
1279       /* Extract the string constant initializer.  Note that this may include
1280          a trailing NUL character that is not in the array (e.g.
1281          const char a[3] = "foo";).  */
1282       array_size = DECL_SIZE_UNIT (format_tree);
1283       format_tree = array_init;
1284     }
1285   if (TREE_CODE (format_tree) != STRING_CST)
1286     {
1287       res->number_non_literal++;
1288       return;
1289     }
1290   if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1291     {
1292       res->number_wide++;
1293       return;
1294     }
1295   format_chars = TREE_STRING_POINTER (format_tree);
1296   format_length = TREE_STRING_LENGTH (format_tree);
1297   if (array_size != 0)
1298     {
1299       /* Variable length arrays can't be initialized.  */
1300       gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1301       
1302       if (host_integerp (array_size, 0))
1303         {
1304           HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1305           if (array_size_value > 0
1306               && array_size_value == (int) array_size_value
1307               && format_length > array_size_value)
1308             format_length = array_size_value;
1309         }
1310     }
1311   if (offset)
1312     {
1313       if (offset >= format_length)
1314         {
1315           res->number_non_literal++;
1316           return;
1317         }
1318       format_chars += offset;
1319       format_length -= offset;
1320     }
1321   if (format_length < 1)
1322     {
1323       res->number_unterminated++;
1324       return;
1325     }
1326   if (format_length == 1)
1327     {
1328       res->number_empty++;
1329       return;
1330     }
1331   if (format_chars[--format_length] != 0)
1332     {
1333       res->number_unterminated++;
1334       return;
1335     }
1336
1337   /* Skip to first argument to check.  */
1338   while (arg_num + 1 < info->first_arg_num)
1339     {
1340       if (params == 0)
1341         return;
1342       params = TREE_CHAIN (params);
1343       ++arg_num;
1344     }
1345   /* Provisionally increment res->number_other; check_format_info_main
1346      will decrement it if it finds there are extra arguments, but this way
1347      need not adjust it for every return.  */
1348   res->number_other++;
1349   check_format_info_main (res, info, format_chars, format_length,
1350                           params, arg_num);
1351 }
1352
1353
1354 /* Do the main part of checking a call to a format function.  FORMAT_CHARS
1355    is the NUL-terminated format string (which at this point may contain
1356    internal NUL characters); FORMAT_LENGTH is its length (excluding the
1357    terminating NUL character).  ARG_NUM is one less than the number of
1358    the first format argument to check; PARAMS points to that format
1359    argument in the list of arguments.  */
1360
1361 static void
1362 check_format_info_main (format_check_results *res,
1363                         function_format_info *info, const char *format_chars,
1364                         int format_length, tree params,
1365                         unsigned HOST_WIDE_INT arg_num)
1366 {
1367   const char *orig_format_chars = format_chars;
1368   tree first_fillin_param = params;
1369
1370   const format_kind_info *fki = &format_types[info->format_type];
1371   const format_flag_spec *flag_specs = fki->flag_specs;
1372   const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1373
1374   /* -1 if no conversions taking an operand have been found; 0 if one has
1375      and it didn't use $; 1 if $ formats are in use.  */
1376   int has_operand_number = -1;
1377
1378   init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1379
1380   while (1)
1381     {
1382       int i;
1383       int suppressed = FALSE;
1384       const char *length_chars = NULL;
1385       enum format_lengths length_chars_val = FMT_LEN_none;
1386       enum format_std_version length_chars_std = STD_C89;
1387       int format_char;
1388       tree cur_param;
1389       tree wanted_type;
1390       int main_arg_num = 0;
1391       tree main_arg_params = 0;
1392       enum format_std_version wanted_type_std;
1393       const char *wanted_type_name;
1394       format_wanted_type width_wanted_type;
1395       format_wanted_type precision_wanted_type;
1396       format_wanted_type main_wanted_type;
1397       format_wanted_type *first_wanted_type = NULL;
1398       format_wanted_type *last_wanted_type = NULL;
1399       const format_length_info *fli = NULL;
1400       const format_char_info *fci = NULL;
1401       char flag_chars[256];
1402       int aflag = 0;
1403       const char *format_start = format_chars;
1404       if (*format_chars == 0)
1405         {
1406           if (format_chars - orig_format_chars != format_length)
1407             warning (OPT_Wformat, "embedded %<\\0%> in format");
1408           if (info->first_arg_num != 0 && params != 0
1409               && has_operand_number <= 0)
1410             {
1411               res->number_other--;
1412               res->number_extra_args++;
1413             }
1414           if (has_operand_number > 0)
1415             finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
1416           return;
1417         }
1418       if (*format_chars++ != '%')
1419         continue;
1420       if (*format_chars == 0)
1421         {
1422           warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1423           continue;
1424         }
1425       if (*format_chars == '%')
1426         {
1427           ++format_chars;
1428           continue;
1429         }
1430       flag_chars[0] = 0;
1431
1432       if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1433         {
1434           /* Possibly read a $ operand number at the start of the format.
1435              If one was previously used, one is required here.  If one
1436              is not used here, we can't immediately conclude this is a
1437              format without them, since it could be printf %m or scanf %*.  */
1438           int opnum;
1439           opnum = maybe_read_dollar_number (&format_chars, 0,
1440                                             first_fillin_param,
1441                                             &main_arg_params, fki);
1442           if (opnum == -1)
1443             return;
1444           else if (opnum > 0)
1445             {
1446               has_operand_number = 1;
1447               main_arg_num = opnum + info->first_arg_num - 1;
1448             }
1449         }
1450       else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1451         {
1452           if (avoid_dollar_number (format_chars))
1453             return;
1454         }
1455
1456       /* Read any format flags, but do not yet validate them beyond removing
1457          duplicates, since in general validation depends on the rest of
1458          the format.  */
1459       while (*format_chars != 0
1460              && strchr (fki->flag_chars, *format_chars) != 0)
1461         {
1462           const format_flag_spec *s = get_flag_spec (flag_specs,
1463                                                      *format_chars, NULL);
1464           if (strchr (flag_chars, *format_chars) != 0)
1465             {
1466               warning (OPT_Wformat, "repeated %s in format", _(s->name));
1467             }
1468           else
1469             {
1470               i = strlen (flag_chars);
1471               flag_chars[i++] = *format_chars;
1472               flag_chars[i] = 0;
1473             }
1474           if (s->skip_next_char)
1475             {
1476               ++format_chars;
1477               if (*format_chars == 0)
1478                 {
1479                   warning (OPT_Wformat, "missing fill character at end of strfmon format");
1480                   return;
1481                 }
1482             }
1483           ++format_chars;
1484         }
1485
1486       /* Read any format width, possibly * or *m$.  */
1487       if (fki->width_char != 0)
1488         {
1489           if (fki->width_type != NULL && *format_chars == '*')
1490             {
1491               i = strlen (flag_chars);
1492               flag_chars[i++] = fki->width_char;
1493               flag_chars[i] = 0;
1494               /* "...a field width...may be indicated by an asterisk.
1495                  In this case, an int argument supplies the field width..."  */
1496               ++format_chars;
1497               if (has_operand_number != 0)
1498                 {
1499                   int opnum;
1500                   opnum = maybe_read_dollar_number (&format_chars,
1501                                                     has_operand_number == 1,
1502                                                     first_fillin_param,
1503                                                     &params, fki);
1504                   if (opnum == -1)
1505                     return;
1506                   else if (opnum > 0)
1507                     {
1508                       has_operand_number = 1;
1509                       arg_num = opnum + info->first_arg_num - 1;
1510                     }
1511                   else
1512                     has_operand_number = 0;
1513                 }
1514               else
1515                 {
1516                   if (avoid_dollar_number (format_chars))
1517                     return;
1518                 }
1519               if (info->first_arg_num != 0)
1520                 {
1521                   if (params == 0)
1522                     {
1523                       warning (OPT_Wformat, "too few arguments for format");
1524                       return;
1525                     }
1526                   cur_param = TREE_VALUE (params);
1527                   if (has_operand_number <= 0)
1528                     {
1529                       params = TREE_CHAIN (params);
1530                       ++arg_num;
1531                     }
1532                   width_wanted_type.wanted_type = *fki->width_type;
1533                   width_wanted_type.wanted_type_name = NULL;
1534                   width_wanted_type.pointer_count = 0;
1535                   width_wanted_type.char_lenient_flag = 0;
1536                   width_wanted_type.writing_in_flag = 0;
1537                   width_wanted_type.reading_from_flag = 0;
1538                   width_wanted_type.name = _("field width");
1539                   width_wanted_type.param = cur_param;
1540                   width_wanted_type.arg_num = arg_num;
1541                   width_wanted_type.next = NULL;
1542                   if (last_wanted_type != 0)
1543                     last_wanted_type->next = &width_wanted_type;
1544                   if (first_wanted_type == 0)
1545                     first_wanted_type = &width_wanted_type;
1546                   last_wanted_type = &width_wanted_type;
1547                 }
1548             }
1549           else
1550             {
1551               /* Possibly read a numeric width.  If the width is zero,
1552                  we complain if appropriate.  */
1553               int non_zero_width_char = FALSE;
1554               int found_width = FALSE;
1555               while (ISDIGIT (*format_chars))
1556                 {
1557                   found_width = TRUE;
1558                   if (*format_chars != '0')
1559                     non_zero_width_char = TRUE;
1560                   ++format_chars;
1561                 }
1562               if (found_width && !non_zero_width_char &&
1563                   (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1564                 warning (OPT_Wformat, "zero width in %s format", fki->name);
1565               if (found_width)
1566                 {
1567                   i = strlen (flag_chars);
1568                   flag_chars[i++] = fki->width_char;
1569                   flag_chars[i] = 0;
1570                 }
1571             }
1572         }
1573
1574       /* Read any format left precision (must be a number, not *).  */
1575       if (fki->left_precision_char != 0 && *format_chars == '#')
1576         {
1577           ++format_chars;
1578           i = strlen (flag_chars);
1579           flag_chars[i++] = fki->left_precision_char;
1580           flag_chars[i] = 0;
1581           if (!ISDIGIT (*format_chars))
1582             warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1583           while (ISDIGIT (*format_chars))
1584             ++format_chars;
1585         }
1586
1587       /* Read any format precision, possibly * or *m$.  */
1588       if (fki->precision_char != 0 && *format_chars == '.')
1589         {
1590           ++format_chars;
1591           i = strlen (flag_chars);
1592           flag_chars[i++] = fki->precision_char;
1593           flag_chars[i] = 0;
1594           if (fki->precision_type != NULL && *format_chars == '*')
1595             {
1596               /* "...a...precision...may be indicated by an asterisk.
1597                  In this case, an int argument supplies the...precision."  */
1598               ++format_chars;
1599               if (has_operand_number != 0)
1600                 {
1601                   int opnum;
1602                   opnum = maybe_read_dollar_number (&format_chars,
1603                                                     has_operand_number == 1,
1604                                                     first_fillin_param,
1605                                                     &params, fki);
1606                   if (opnum == -1)
1607                     return;
1608                   else if (opnum > 0)
1609                     {
1610                       has_operand_number = 1;
1611                       arg_num = opnum + info->first_arg_num - 1;
1612                     }
1613                   else
1614                     has_operand_number = 0;
1615                 }
1616               else
1617                 {
1618                   if (avoid_dollar_number (format_chars))
1619                     return;
1620                 }
1621               if (info->first_arg_num != 0)
1622                 {
1623                   if (params == 0)
1624                     {
1625                       warning (OPT_Wformat, "too few arguments for format");
1626                       return;
1627                     }
1628                   cur_param = TREE_VALUE (params);
1629                   if (has_operand_number <= 0)
1630                     {
1631                       params = TREE_CHAIN (params);
1632                       ++arg_num;
1633                     }
1634                   precision_wanted_type.wanted_type = *fki->precision_type;
1635                   precision_wanted_type.wanted_type_name = NULL;
1636                   precision_wanted_type.pointer_count = 0;
1637                   precision_wanted_type.char_lenient_flag = 0;
1638                   precision_wanted_type.writing_in_flag = 0;
1639                   precision_wanted_type.reading_from_flag = 0;
1640                   precision_wanted_type.name = _("field precision");
1641                   precision_wanted_type.param = cur_param;
1642                   precision_wanted_type.arg_num = arg_num;
1643                   precision_wanted_type.next = NULL;
1644                   if (last_wanted_type != 0)
1645                     last_wanted_type->next = &precision_wanted_type;
1646                   if (first_wanted_type == 0)
1647                     first_wanted_type = &precision_wanted_type;
1648                   last_wanted_type = &precision_wanted_type;
1649                 }
1650             }
1651           else
1652             {
1653               if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1654                   && !ISDIGIT (*format_chars))
1655                 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1656               while (ISDIGIT (*format_chars))
1657                 ++format_chars;
1658             }
1659         }
1660
1661       /* Read any length modifier, if this kind of format has them.  */
1662       fli = fki->length_char_specs;
1663       length_chars = NULL;
1664       length_chars_val = FMT_LEN_none;
1665       length_chars_std = STD_C89;
1666       if (fli)
1667         {
1668           while (fli->name != 0 && fli->name[0] != *format_chars)
1669             fli++;
1670           if (fli->name != 0)
1671             {
1672               format_chars++;
1673               if (fli->double_name != 0 && fli->name[0] == *format_chars)
1674                 {
1675                   format_chars++;
1676                   length_chars = fli->double_name;
1677                   length_chars_val = fli->double_index;
1678                   length_chars_std = fli->double_std;
1679                 }
1680               else
1681                 {
1682                   length_chars = fli->name;
1683                   length_chars_val = fli->index;
1684                   length_chars_std = fli->std;
1685                 }
1686               i = strlen (flag_chars);
1687               flag_chars[i++] = fki->length_code_char;
1688               flag_chars[i] = 0;
1689             }
1690           if (pedantic)
1691             {
1692               /* Warn if the length modifier is non-standard.  */
1693               if (ADJ_STD (length_chars_std) > C_STD_VER)
1694                 warning (OPT_Wformat,
1695                          "%s does not support the %qs %s length modifier",
1696                          C_STD_NAME (length_chars_std), length_chars,
1697                          fki->name);
1698             }
1699         }
1700
1701       /* Read any modifier (strftime E/O).  */
1702       if (fki->modifier_chars != NULL)
1703         {
1704           while (*format_chars != 0
1705                  && strchr (fki->modifier_chars, *format_chars) != 0)
1706             {
1707               if (strchr (flag_chars, *format_chars) != 0)
1708                 {
1709                   const format_flag_spec *s = get_flag_spec (flag_specs,
1710                                                              *format_chars, NULL);
1711                   warning (OPT_Wformat, "repeated %s in format", _(s->name));
1712                 }
1713               else
1714                 {
1715                   i = strlen (flag_chars);
1716                   flag_chars[i++] = *format_chars;
1717                   flag_chars[i] = 0;
1718                 }
1719               ++format_chars;
1720             }
1721         }
1722
1723       /* Handle the scanf allocation kludge.  */
1724       if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1725         {
1726           if (*format_chars == 'a' && !flag_isoc99)
1727             {
1728               if (format_chars[1] == 's' || format_chars[1] == 'S'
1729                   || format_chars[1] == '[')
1730                 {
1731                   /* 'a' is used as a flag.  */
1732                   i = strlen (flag_chars);
1733                   flag_chars[i++] = 'a';
1734                   flag_chars[i] = 0;
1735                   format_chars++;
1736                 }
1737             }
1738         }
1739
1740       format_char = *format_chars;
1741       if (format_char == 0
1742           || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1743               && format_char == '%'))
1744         {
1745           warning (OPT_Wformat, "conversion lacks type at end of format");
1746           continue;
1747         }
1748       format_chars++;
1749       fci = fki->conversion_specs;
1750       while (fci->format_chars != 0
1751              && strchr (fci->format_chars, format_char) == 0)
1752           ++fci;
1753       if (fci->format_chars == 0)
1754         {
1755           if (ISGRAPH (format_char))
1756             warning (OPT_Wformat, "unknown conversion type character %qc in format",
1757                      format_char);
1758           else
1759             warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
1760                      format_char);
1761           continue;
1762         }
1763       if (pedantic)
1764         {
1765           if (ADJ_STD (fci->std) > C_STD_VER)
1766             warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
1767                      C_STD_NAME (fci->std), format_char, fki->name);
1768         }
1769
1770       /* Validate the individual flags used, removing any that are invalid.  */
1771       {
1772         int d = 0;
1773         for (i = 0; flag_chars[i] != 0; i++)
1774           {
1775             const format_flag_spec *s = get_flag_spec (flag_specs,
1776                                                        flag_chars[i], NULL);
1777             flag_chars[i - d] = flag_chars[i];
1778             if (flag_chars[i] == fki->length_code_char)
1779               continue;
1780             if (strchr (fci->flag_chars, flag_chars[i]) == 0)
1781               {
1782                 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
1783                          _(s->name), format_char, fki->name);
1784                 d++;
1785                 continue;
1786               }
1787             if (pedantic)
1788               {
1789                 const format_flag_spec *t;
1790                 if (ADJ_STD (s->std) > C_STD_VER)
1791                   warning (OPT_Wformat, "%s does not support %s",
1792                            C_STD_NAME (s->std), _(s->long_name));
1793                 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
1794                 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1795                   {
1796                     const char *long_name = (t->long_name != NULL
1797                                              ? t->long_name
1798                                              : s->long_name);
1799                     if (ADJ_STD (t->std) > C_STD_VER)
1800                       warning (OPT_Wformat,
1801                                "%s does not support %s with the %<%%%c%> %s format",
1802                                C_STD_NAME (t->std), _(long_name),
1803                                format_char, fki->name);
1804                   }
1805               }
1806           }
1807         flag_chars[i - d] = 0;
1808       }
1809
1810       if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1811           && strchr (flag_chars, 'a') != 0)
1812         aflag = 1;
1813
1814       if (fki->suppression_char
1815           && strchr (flag_chars, fki->suppression_char) != 0)
1816         suppressed = 1;
1817
1818       /* Validate the pairs of flags used.  */
1819       for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
1820         {
1821           const format_flag_spec *s, *t;
1822           if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
1823             continue;
1824           if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
1825             continue;
1826           if (bad_flag_pairs[i].predicate != 0
1827               && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
1828             continue;
1829           s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
1830           t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
1831           if (bad_flag_pairs[i].ignored)
1832             {
1833               if (bad_flag_pairs[i].predicate != 0)
1834                 warning (OPT_Wformat,
1835                          "%s ignored with %s and %<%%%c%> %s format",
1836                          _(s->name), _(t->name), format_char,
1837                          fki->name);
1838               else
1839                 warning (OPT_Wformat, "%s ignored with %s in %s format",
1840                          _(s->name), _(t->name), fki->name);
1841             }
1842           else
1843             {
1844               if (bad_flag_pairs[i].predicate != 0)
1845                 warning (OPT_Wformat,
1846                          "use of %s and %s together with %<%%%c%> %s format",
1847                          _(s->name), _(t->name), format_char,
1848                          fki->name);
1849               else
1850                 warning (OPT_Wformat, "use of %s and %s together in %s format",
1851                          _(s->name), _(t->name), fki->name);
1852             }
1853         }
1854
1855       /* Give Y2K warnings.  */
1856       if (warn_format_y2k)
1857         {
1858           int y2k_level = 0;
1859           if (strchr (fci->flags2, '4') != 0)
1860             if (strchr (flag_chars, 'E') != 0)
1861               y2k_level = 3;
1862             else
1863               y2k_level = 2;
1864           else if (strchr (fci->flags2, '3') != 0)
1865             y2k_level = 3;
1866           else if (strchr (fci->flags2, '2') != 0)
1867             y2k_level = 2;
1868           if (y2k_level == 3)
1869             warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1870                      "year in some locales", format_char);
1871           else if (y2k_level == 2)
1872             warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
1873                      "year", format_char);
1874         }
1875
1876       if (strchr (fci->flags2, '[') != 0)
1877         {
1878           /* Skip over scan set, in case it happens to have '%' in it.  */
1879           if (*format_chars == '^')
1880             ++format_chars;
1881           /* Find closing bracket; if one is hit immediately, then
1882              it's part of the scan set rather than a terminator.  */
1883           if (*format_chars == ']')
1884             ++format_chars;
1885           while (*format_chars && *format_chars != ']')
1886             ++format_chars;
1887           if (*format_chars != ']')
1888             /* The end of the format string was reached.  */
1889             warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
1890         }
1891
1892       wanted_type = 0;
1893       wanted_type_name = 0;
1894       if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
1895         {
1896           wanted_type = (fci->types[length_chars_val].type
1897                          ? *fci->types[length_chars_val].type : 0);
1898           wanted_type_name = fci->types[length_chars_val].name;
1899           wanted_type_std = fci->types[length_chars_val].std;
1900           if (wanted_type == 0)
1901             {
1902               warning (OPT_Wformat,
1903                        "use of %qs length modifier with %qc type character",
1904                        length_chars, format_char);
1905               /* Heuristic: skip one argument when an invalid length/type
1906                  combination is encountered.  */
1907               arg_num++;
1908               if (params == 0)
1909                 {
1910                   warning (OPT_Wformat, "too few arguments for format");
1911                   return;
1912                 }
1913               params = TREE_CHAIN (params);
1914               continue;
1915             }
1916           else if (pedantic
1917                    /* Warn if non-standard, provided it is more non-standard
1918                       than the length and type characters that may already
1919                       have been warned for.  */
1920                    && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
1921                    && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
1922             {
1923               if (ADJ_STD (wanted_type_std) > C_STD_VER)
1924                 warning (OPT_Wformat,
1925                          "%s does not support the %<%%%s%c%> %s format",
1926                          C_STD_NAME (wanted_type_std), length_chars,
1927                          format_char, fki->name);
1928             }
1929         }
1930
1931       main_wanted_type.next = NULL;
1932
1933       /* Finally. . .check type of argument against desired type!  */
1934       if (info->first_arg_num == 0)
1935         continue;
1936       if ((fci->pointer_count == 0 && wanted_type == void_type_node)
1937           || suppressed)
1938         {
1939           if (main_arg_num != 0)
1940             {
1941               if (suppressed)
1942                 warning (OPT_Wformat, "operand number specified with "
1943                          "suppressed assignment");
1944               else
1945                 warning (OPT_Wformat, "operand number specified for format "
1946                          "taking no argument");
1947             }
1948         }
1949       else
1950         {
1951           format_wanted_type *wanted_type_ptr;
1952
1953           if (main_arg_num != 0)
1954             {
1955               arg_num = main_arg_num;
1956               params = main_arg_params;
1957             }
1958           else
1959             {
1960               ++arg_num;
1961               if (has_operand_number > 0)
1962                 {
1963                   warning (OPT_Wformat, "missing $ operand number in format");
1964                   return;
1965                 }
1966               else
1967                 has_operand_number = 0;
1968             }
1969
1970           wanted_type_ptr = &main_wanted_type;
1971           while (fci)
1972             {
1973               if (params == 0)
1974                 {
1975                   warning (OPT_Wformat, "too few arguments for format");
1976                   return;
1977                 }
1978
1979               cur_param = TREE_VALUE (params);
1980               params = TREE_CHAIN (params);
1981
1982               wanted_type_ptr->wanted_type = wanted_type;
1983               wanted_type_ptr->wanted_type_name = wanted_type_name;
1984               wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
1985               wanted_type_ptr->char_lenient_flag = 0;
1986               if (strchr (fci->flags2, 'c') != 0)
1987                 wanted_type_ptr->char_lenient_flag = 1;
1988               wanted_type_ptr->writing_in_flag = 0;
1989               wanted_type_ptr->reading_from_flag = 0;
1990               if (aflag)
1991                 wanted_type_ptr->writing_in_flag = 1;
1992               else
1993                 {
1994                   if (strchr (fci->flags2, 'W') != 0)
1995                     wanted_type_ptr->writing_in_flag = 1;
1996                   if (strchr (fci->flags2, 'R') != 0)
1997                     wanted_type_ptr->reading_from_flag = 1;
1998                 }
1999               wanted_type_ptr->name = NULL;
2000               wanted_type_ptr->param = cur_param;
2001               wanted_type_ptr->arg_num = arg_num;
2002               wanted_type_ptr->next = NULL;
2003               if (last_wanted_type != 0)
2004                 last_wanted_type->next = wanted_type_ptr;
2005               if (first_wanted_type == 0)
2006                 first_wanted_type = wanted_type_ptr;
2007               last_wanted_type = wanted_type_ptr;
2008
2009               fci = fci->chain;
2010               if (fci)
2011                 {
2012                   wanted_type_ptr = ggc_alloc (sizeof (main_wanted_type));
2013                   arg_num++;
2014                   wanted_type = *fci->types[length_chars_val].type;
2015                   wanted_type_name = fci->types[length_chars_val].name;
2016                 }
2017             }
2018         }
2019
2020       if (first_wanted_type != 0)
2021         check_format_types (first_wanted_type, format_start,
2022                             format_chars - format_start);
2023
2024       if (main_wanted_type.next != NULL)
2025         {
2026           format_wanted_type *wanted_type_ptr = main_wanted_type.next;
2027           while (wanted_type_ptr)
2028             {
2029               format_wanted_type *next = wanted_type_ptr->next;
2030               ggc_free (wanted_type_ptr);
2031               wanted_type_ptr = next;
2032             }
2033         }
2034     }
2035 }
2036
2037
2038 /* Check the argument types from a single format conversion (possibly
2039    including width and precision arguments).  */
2040 static void
2041 check_format_types (format_wanted_type *types, const char *format_start,
2042                     int format_length)
2043 {
2044   for (; types != 0; types = types->next)
2045     {
2046       tree cur_param;
2047       tree cur_type;
2048       tree orig_cur_type;
2049       tree wanted_type;
2050       int arg_num;
2051       int i;
2052       int char_type_flag;
2053       cur_param = types->param;
2054       cur_type = TREE_TYPE (cur_param);
2055       if (cur_type == error_mark_node)
2056         continue;
2057       orig_cur_type = cur_type;
2058       char_type_flag = 0;
2059       wanted_type = types->wanted_type;
2060       arg_num = types->arg_num;
2061
2062       /* The following should not occur here.  */
2063       gcc_assert (wanted_type);
2064       gcc_assert (wanted_type != void_type_node || types->pointer_count);
2065
2066       if (types->pointer_count == 0)
2067         wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2068
2069       wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2070
2071       STRIP_NOPS (cur_param);
2072
2073       /* Check the types of any additional pointer arguments
2074          that precede the "real" argument.  */
2075       for (i = 0; i < types->pointer_count; ++i)
2076         {
2077           if (TREE_CODE (cur_type) == POINTER_TYPE)
2078             {
2079               cur_type = TREE_TYPE (cur_type);
2080               if (cur_type == error_mark_node)
2081                 break;
2082
2083               /* Check for writing through a NULL pointer.  */
2084               if (types->writing_in_flag
2085                   && i == 0
2086                   && cur_param != 0
2087                   && integer_zerop (cur_param))
2088                 warning (OPT_Wformat, "writing through null pointer "
2089                          "(argument %d)", arg_num);
2090
2091               /* Check for reading through a NULL pointer.  */
2092               if (types->reading_from_flag
2093                   && i == 0
2094                   && cur_param != 0
2095                   && integer_zerop (cur_param))
2096                 warning (OPT_Wformat, "reading through null pointer "
2097                          "(argument %d)", arg_num);
2098
2099               if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2100                 cur_param = TREE_OPERAND (cur_param, 0);
2101               else
2102                 cur_param = 0;
2103
2104               /* See if this is an attempt to write into a const type with
2105                  scanf or with printf "%n".  Note: the writing in happens
2106                  at the first indirection only, if for example
2107                  void * const * is passed to scanf %p; passing
2108                  const void ** is simply passing an incompatible type.  */
2109               if (types->writing_in_flag
2110                   && i == 0
2111                   && (TYPE_READONLY (cur_type)
2112                       || (cur_param != 0
2113                           && (CONSTANT_CLASS_P (cur_param)
2114                               || (DECL_P (cur_param)
2115                                   && TREE_READONLY (cur_param))))))
2116                 warning (OPT_Wformat, "writing into constant object "
2117                          "(argument %d)", arg_num);
2118
2119               /* If there are extra type qualifiers beyond the first
2120                  indirection, then this makes the types technically
2121                  incompatible.  */
2122               if (i > 0
2123                   && pedantic
2124                   && (TYPE_READONLY (cur_type)
2125                       || TYPE_VOLATILE (cur_type)
2126                       || TYPE_RESTRICT (cur_type)))
2127                 warning (OPT_Wformat, "extra type qualifiers in format "
2128                          "argument (argument %d)",
2129                          arg_num);
2130
2131             }
2132           else
2133             {
2134               format_type_warning (types->name, format_start, format_length,
2135                                    wanted_type, types->pointer_count,
2136                                    types->wanted_type_name, orig_cur_type,
2137                                    arg_num);
2138               break;
2139             }
2140         }
2141
2142       if (i < types->pointer_count)
2143         continue;
2144
2145       cur_type = TYPE_MAIN_VARIANT (cur_type);
2146
2147       /* Check whether the argument type is a character type.  This leniency
2148          only applies to certain formats, flagged with 'c'.
2149       */
2150       if (types->char_lenient_flag)
2151         char_type_flag = (cur_type == char_type_node
2152                           || cur_type == signed_char_type_node
2153                           || cur_type == unsigned_char_type_node);
2154
2155       /* Check the type of the "real" argument, if there's a type we want.  */
2156       if (wanted_type == cur_type)
2157         continue;
2158       /* If we want 'void *', allow any pointer type.
2159          (Anything else would already have got a warning.)
2160          With -pedantic, only allow pointers to void and to character
2161          types.  */
2162       if (wanted_type == void_type_node
2163           && (!pedantic || (i == 1 && char_type_flag)))
2164         continue;
2165       /* Don't warn about differences merely in signedness, unless
2166          -pedantic.  With -pedantic, warn if the type is a pointer
2167          target and not a character type, and for character types at
2168          a second level of indirection.  */
2169       if (TREE_CODE (wanted_type) == INTEGER_TYPE
2170           && TREE_CODE (cur_type) == INTEGER_TYPE
2171           && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2172           && (TYPE_UNSIGNED (wanted_type)
2173               ? wanted_type == c_common_unsigned_type (cur_type)
2174               : wanted_type == c_common_signed_type (cur_type)))
2175         continue;
2176       /* Likewise, "signed char", "unsigned char" and "char" are
2177          equivalent but the above test won't consider them equivalent.  */
2178       if (wanted_type == char_type_node
2179           && (!pedantic || i < 2)
2180           && char_type_flag)
2181         continue;
2182       /* Now we have a type mismatch.  */
2183       format_type_warning (types->name, format_start, format_length,
2184                            wanted_type, types->pointer_count,
2185                            types->wanted_type_name, orig_cur_type, arg_num);
2186     }
2187 }
2188
2189
2190 /* Give a warning about a format argument of different type from that
2191    expected.  DESCR is a description such as "field precision", or
2192    NULL for an ordinary format.  For an ordinary format, FORMAT_START
2193    points to where the format starts in the format string and
2194    FORMAT_LENGTH is its length.  WANTED_TYPE is the type the argument
2195    should have after POINTER_COUNT pointer dereferences.
2196    WANTED_NAME_NAME is a possibly more friendly name of WANTED_TYPE,
2197    or NULL if the ordinary name of the type should be used.  ARG_TYPE
2198    is the type of the actual argument.  ARG_NUM is the number of that
2199    argument.  */
2200 static void
2201 format_type_warning (const char *descr, const char *format_start,
2202                      int format_length, tree wanted_type, int pointer_count,
2203                      const char *wanted_type_name, tree arg_type, int arg_num)
2204 {
2205   char *p;
2206   /* If ARG_TYPE is a typedef with a misleading name (for example,
2207      size_t but not the standard size_t expected by printf %zu), avoid
2208      printing the typedef name.  */
2209   if (wanted_type_name
2210       && TYPE_NAME (arg_type)
2211       && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2212       && DECL_NAME (TYPE_NAME (arg_type))
2213       && !strcmp (wanted_type_name,
2214                   lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2215     arg_type = TYPE_MAIN_VARIANT (arg_type);
2216   /* The format type and name exclude any '*' for pointers, so those
2217      must be formatted manually.  For all the types we currently have,
2218      this is adequate, but formats taking pointers to functions or
2219      arrays would require the full type to be built up in order to
2220      print it with %T.  */
2221   p = alloca (pointer_count + 2);
2222   if (pointer_count == 0)
2223     p[0] = 0;
2224   else if (c_dialect_cxx ())
2225     {
2226       memset (p, '*', pointer_count);
2227       p[pointer_count] = 0;
2228     }
2229   else
2230     {
2231       p[0] = ' ';
2232       memset (p + 1, '*', pointer_count);
2233       p[pointer_count + 1] = 0;
2234     }
2235   if (wanted_type_name)
2236     {
2237       if (descr)
2238         warning (OPT_Wformat, "%s should have type %<%s%s%>, "
2239                  "but argument %d has type %qT",
2240                  descr, wanted_type_name, p, arg_num, arg_type);
2241       else
2242         warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
2243                  "but argument %d has type %qT",
2244                  format_length, format_start, wanted_type_name, p,
2245                  arg_num, arg_type);
2246     }
2247   else
2248     {
2249       if (descr)
2250         warning (OPT_Wformat, "%s should have type %<%T%s%>, "
2251                  "but argument %d has type %qT",
2252                  descr, wanted_type, p, arg_num, arg_type);
2253       else
2254         warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
2255                  "but argument %d has type %qT",
2256                  format_length, format_start, wanted_type, p, arg_num, arg_type);
2257     }
2258 }
2259
2260
2261 /* Given a format_char_info array FCI, and a character C, this function
2262    returns the index into the conversion_specs where that specifier's
2263    data is located.  The character must exist.  */
2264 static unsigned int
2265 find_char_info_specifier_index (const format_char_info *fci, int c)
2266 {
2267   unsigned i;
2268
2269   for (i = 0; fci->format_chars; i++, fci++)
2270     if (strchr (fci->format_chars, c))
2271       return i;
2272   
2273   /* We shouldn't be looking for a non-existent specifier.  */
2274   gcc_unreachable ();
2275 }
2276
2277 /* Given a format_length_info array FLI, and a character C, this
2278    function returns the index into the conversion_specs where that
2279    modifier's data is located.  The character must exist.  */
2280 static unsigned int
2281 find_length_info_modifier_index (const format_length_info *fli, int c)
2282 {
2283   unsigned i;
2284
2285   for (i = 0; fli->name; i++, fli++)
2286     if (strchr (fli->name, c))
2287       return i;
2288   
2289   /* We shouldn't be looking for a non-existent modifier.  */
2290   gcc_unreachable ();
2291 }
2292
2293 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2294    use in GCC's __asm_fprintf__ custom format attribute.  You must
2295    have set dynamic_format_types before calling this function.  */
2296 static void
2297 init_dynamic_asm_fprintf_info (void)
2298 {
2299   static tree hwi;
2300
2301   if (!hwi)
2302     {
2303       format_length_info *new_asm_fprintf_length_specs;
2304       unsigned int i;
2305           
2306       /* Find the underlying type for HOST_WIDE_INT.  For the %w
2307          length modifier to work, one must have issued: "typedef
2308          HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2309          prior to using that modifier.  */
2310       hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2311       if (!hwi)
2312         {
2313           error ("%<__gcc_host_wide_int__%> is not defined as a type");
2314           return;
2315         }
2316       hwi = identifier_global_value (hwi);
2317       if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2318         {
2319           error ("%<__gcc_host_wide_int__%> is not defined as a type");
2320           return;
2321         }
2322       hwi = DECL_ORIGINAL_TYPE (hwi);
2323       gcc_assert (hwi);
2324       if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2325         {
2326           error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2327                  " or %<long long%>");
2328           return;
2329         }
2330
2331       /* Create a new (writable) copy of asm_fprintf_length_specs.  */
2332       new_asm_fprintf_length_specs = (format_length_info *)
2333                                      xmemdup (asm_fprintf_length_specs,
2334                                               sizeof (asm_fprintf_length_specs),
2335                                               sizeof (asm_fprintf_length_specs));
2336
2337       /* HOST_WIDE_INT must be one of 'long' or 'long long'.  */
2338       i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2339       if (hwi == long_integer_type_node)
2340         new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2341       else if (hwi == long_long_integer_type_node)
2342         new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2343       else
2344         gcc_unreachable ();
2345
2346       /* Assign the new data for use.  */
2347       dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2348         new_asm_fprintf_length_specs;
2349     }
2350 }
2351
2352 /* Determine the types of "tree" and "location_t" in the code being
2353    compiled for use in GCC's diagnostic custom format attributes.  You
2354    must have set dynamic_format_types before calling this function.  */
2355 static void
2356 init_dynamic_diag_info (void)
2357 {
2358   static tree t, loc, hwi;
2359
2360   if (!loc || !t || !hwi)
2361     {
2362       static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
2363       static format_length_info *diag_ls;
2364       unsigned int i;
2365
2366       /* For the GCC-diagnostics custom format specifiers to work, one
2367          must have declared 'tree' and/or 'location_t' prior to using
2368          those attributes.  If we haven't seen these declarations then
2369          you shouldn't use the specifiers requiring these types.
2370          However we don't force a hard ICE because we may see only one
2371          or the other type.  */
2372       if ((loc = maybe_get_identifier ("location_t")))
2373         {
2374           loc = identifier_global_value (loc);
2375           if (loc)
2376             {
2377               if (TREE_CODE (loc) != TYPE_DECL)
2378                 {
2379                   error ("%<location_t%> is not defined as a type");
2380                   loc = 0;
2381                 }
2382               else
2383                 loc = TREE_TYPE (loc);
2384             }
2385         }
2386
2387       /* We need to grab the underlying 'union tree_node' so peek into
2388          an extra type level.  */
2389       if ((t = maybe_get_identifier ("tree")))
2390         {
2391           t = identifier_global_value (t);
2392           if (t)
2393             {
2394               if (TREE_CODE (t) != TYPE_DECL)
2395                 {
2396                   error ("%<tree%> is not defined as a type");
2397                   t = 0;
2398                 }
2399               else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2400                 {
2401                   error ("%<tree%> is not defined as a pointer type");
2402                   t = 0;
2403                 }
2404               else
2405                 t = TREE_TYPE (TREE_TYPE (t));
2406             }
2407         }
2408     
2409       /* Find the underlying type for HOST_WIDE_INT.  For the %w
2410          length modifier to work, one must have issued: "typedef
2411          HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2412          prior to using that modifier.  */
2413       if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2414         {
2415           hwi = identifier_global_value (hwi);
2416           if (hwi)
2417             {
2418               if (TREE_CODE (hwi) != TYPE_DECL)
2419                 {
2420                   error ("%<__gcc_host_wide_int__%> is not defined as a type");
2421                   hwi = 0;
2422                 }
2423               else
2424                 {
2425                   hwi = DECL_ORIGINAL_TYPE (hwi);
2426                   gcc_assert (hwi);
2427                   if (hwi != long_integer_type_node
2428                       && hwi != long_long_integer_type_node)
2429                     {
2430                       error ("%<__gcc_host_wide_int__%> is not defined"
2431                              " as %<long%> or %<long long%>");
2432                       hwi = 0;
2433                     }
2434                 }
2435             }
2436         }
2437       
2438       /* Assign the new data for use.  */
2439
2440       /* All the GCC diag formats use the same length specs.  */
2441       if (!diag_ls)
2442         dynamic_format_types[gcc_diag_format_type].length_char_specs =
2443           dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2444           dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2445           diag_ls = (format_length_info *)
2446                     xmemdup (gcc_diag_length_specs,
2447                              sizeof (gcc_diag_length_specs),
2448                              sizeof (gcc_diag_length_specs)); 
2449       if (hwi)
2450         {
2451           /* HOST_WIDE_INT must be one of 'long' or 'long long'.  */
2452           i = find_length_info_modifier_index (diag_ls, 'w');
2453           if (hwi == long_integer_type_node)
2454             diag_ls[i].index = FMT_LEN_l;
2455           else if (hwi == long_long_integer_type_node)
2456             diag_ls[i].index = FMT_LEN_ll;
2457           else
2458             gcc_unreachable ();
2459         }
2460
2461       /* Handle the __gcc_diag__ format specifics.  */
2462       if (!diag_fci)
2463         dynamic_format_types[gcc_diag_format_type].conversion_specs =
2464           diag_fci = (format_char_info *)
2465                      xmemdup (gcc_diag_char_table,
2466                               sizeof (gcc_diag_char_table),
2467                               sizeof (gcc_diag_char_table));
2468       if (loc)
2469         {
2470           i = find_char_info_specifier_index (diag_fci, 'H');
2471           diag_fci[i].types[0].type = &loc;
2472           diag_fci[i].pointer_count = 1;
2473         }
2474       if (t)
2475         {
2476           i = find_char_info_specifier_index (diag_fci, 'J');
2477           diag_fci[i].types[0].type = &t;
2478           diag_fci[i].pointer_count = 1;
2479         }
2480
2481       /* Handle the __gcc_cdiag__ format specifics.  */
2482       if (!cdiag_fci)
2483         dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2484           cdiag_fci = (format_char_info *)
2485                       xmemdup (gcc_cdiag_char_table,
2486                                sizeof (gcc_cdiag_char_table),
2487                                sizeof (gcc_cdiag_char_table));
2488       if (loc)
2489         {
2490           i = find_char_info_specifier_index (cdiag_fci, 'H');
2491           cdiag_fci[i].types[0].type = &loc;
2492           cdiag_fci[i].pointer_count = 1;
2493         }
2494       if (t)
2495         {
2496           /* All specifiers taking a tree share the same struct.  */
2497           i = find_char_info_specifier_index (cdiag_fci, 'D');
2498           cdiag_fci[i].types[0].type = &t;
2499           cdiag_fci[i].pointer_count = 1;
2500           i = find_char_info_specifier_index (cdiag_fci, 'J');
2501           cdiag_fci[i].types[0].type = &t;
2502           cdiag_fci[i].pointer_count = 1;
2503         }
2504
2505       /* Handle the __gcc_cxxdiag__ format specifics.  */
2506       if (!cxxdiag_fci)
2507         dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2508           cxxdiag_fci = (format_char_info *)
2509                         xmemdup (gcc_cxxdiag_char_table,
2510                                  sizeof (gcc_cxxdiag_char_table),
2511                                  sizeof (gcc_cxxdiag_char_table));
2512       if (loc)
2513         {
2514           i = find_char_info_specifier_index (cxxdiag_fci, 'H');
2515           cxxdiag_fci[i].types[0].type = &loc;
2516           cxxdiag_fci[i].pointer_count = 1;
2517         }
2518       if (t)
2519         {
2520           /* All specifiers taking a tree share the same struct.  */
2521           i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2522           cxxdiag_fci[i].types[0].type = &t;
2523           cxxdiag_fci[i].pointer_count = 1;
2524           i = find_char_info_specifier_index (cxxdiag_fci, 'J');
2525           cxxdiag_fci[i].types[0].type = &t;
2526           cxxdiag_fci[i].pointer_count = 1;
2527         }
2528     }
2529 }
2530
2531 #ifdef TARGET_FORMAT_TYPES
2532 extern const format_kind_info TARGET_FORMAT_TYPES[];
2533 #endif
2534
2535 /* Handle a "format" attribute; arguments as in
2536    struct attribute_spec.handler.  */
2537 tree
2538 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2539                          int flags, bool *no_add_attrs)
2540 {
2541   tree type = *node;
2542   function_format_info info;
2543   tree argument;
2544
2545 #ifdef TARGET_FORMAT_TYPES
2546   /* If the target provides additional format types, we need to
2547      add them to FORMAT_TYPES at first use.  */
2548   if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2549     {
2550       dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
2551                                       * sizeof (dynamic_format_types[0]));
2552       memcpy (dynamic_format_types, format_types_orig,
2553               sizeof (format_types_orig));
2554       memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2555               TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2556
2557       format_types = dynamic_format_types;
2558       n_format_types += TARGET_N_FORMAT_TYPES;
2559     }
2560 #endif
2561
2562   if (!decode_format_attr (args, &info, 0))
2563     {
2564       *no_add_attrs = true;
2565       return NULL_TREE;
2566     }
2567
2568   argument = TYPE_ARG_TYPES (type);
2569   if (argument)
2570     {
2571       if (!check_format_string (argument, info.format_num, flags,
2572                                 no_add_attrs))
2573         return NULL_TREE;
2574
2575       if (info.first_arg_num != 0)
2576         {
2577           unsigned HOST_WIDE_INT arg_num = 1;
2578
2579           /* Verify that first_arg_num points to the last arg,
2580              the ...  */
2581           while (argument)
2582             arg_num++, argument = TREE_CHAIN (argument);
2583
2584           if (arg_num != info.first_arg_num)
2585             {
2586               if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2587                 error ("args to be formatted is not %<...%>");
2588               *no_add_attrs = true;
2589               return NULL_TREE;
2590             }
2591         }
2592     }
2593
2594   if (info.format_type == strftime_format_type && info.first_arg_num != 0)
2595     {
2596       error ("strftime formats cannot format arguments");
2597       *no_add_attrs = true;
2598       return NULL_TREE;
2599     }
2600
2601   /* If this is a custom GCC-internal format type, we have to
2602      initialize certain bits a runtime.  */
2603   if (info.format_type == asm_fprintf_format_type
2604       || info.format_type == gcc_diag_format_type
2605       || info.format_type == gcc_cdiag_format_type
2606       || info.format_type == gcc_cxxdiag_format_type)
2607     {
2608       /* Our first time through, we have to make sure that our
2609          format_type data is allocated dynamically and is modifiable.  */
2610       if (!dynamic_format_types)
2611         format_types = dynamic_format_types = (format_kind_info *)
2612           xmemdup (format_types_orig, sizeof (format_types_orig),
2613                    sizeof (format_types_orig));
2614
2615       /* If this is format __asm_fprintf__, we have to initialize
2616          GCC's notion of HOST_WIDE_INT for checking %wd.  */
2617       if (info.format_type == asm_fprintf_format_type)
2618         init_dynamic_asm_fprintf_info ();
2619       /* If this is one of the diagnostic attributes, then we have to
2620          initialize 'location_t' and 'tree' at runtime.  */
2621       else if (info.format_type == gcc_diag_format_type
2622                || info.format_type == gcc_cdiag_format_type
2623                || info.format_type == gcc_cxxdiag_format_type)
2624         init_dynamic_diag_info ();
2625       else
2626         gcc_unreachable ();
2627     }
2628
2629   return NULL_TREE;
2630 }