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