OSDN Git Service

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