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