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