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