1 /* Check calls to formatted I/O functions (-Wformat).
2    Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "c-target.h"
25 #include "c-common.h"
26 #include "alloc-pool.h"
27 #include "stringpool.h"
28 #include "c-objc.h"
29 #include "intl.h"
30 #include "langhooks.h"
31 #include "c-format.h"
32 #include "diagnostic.h"
33 #include "substring-locations.h"
34 #include "selftest.h"
35 #include "builtins.h"
36 #include "attribs.h"
37 
38 /* Handle attributes associated with format checking.  */
39 
40 /* This must be in the same order as format_types, except for
41    format_type_error.  Target-specific format types do not have
42    matching enum values.  */
43 enum format_type { printf_format_type, asm_fprintf_format_type,
44 		   gcc_diag_format_type, gcc_tdiag_format_type,
45 		   gcc_cdiag_format_type,
46 		   gcc_cxxdiag_format_type, gcc_gfc_format_type,
47 		   gcc_objc_string_format_type,
48 		   format_type_error = -1};
49 
50 struct function_format_info
51 {
52   int format_type;			/* type of format (printf, scanf, etc.) */
53   unsigned HOST_WIDE_INT format_num;	/* number of format argument */
54   unsigned HOST_WIDE_INT first_arg_num;	/* number of first arg (zero for varargs) */
55 };
56 
57 /* Initialized in init_dynamic_diag_info.  */
58 static GTY(()) tree local_tree_type_node;
59 static GTY(()) tree local_gcall_ptr_node;
60 static GTY(()) tree locus;
61 
62 static bool decode_format_attr (tree, function_format_info *, int);
63 static int decode_format_type (const char *);
64 
65 static bool check_format_string (tree argument,
66 				 unsigned HOST_WIDE_INT format_num,
67 				 int flags, bool *no_add_attrs,
68 				 int expected_format_type);
69 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
70 			  int validated_p);
71 static const char *convert_format_name_to_system_name (const char *attr_name);
72 
73 static int first_target_format_type;
74 static const char *format_name (int format_num);
75 static int format_flags (int format_num);
76 
77 /* Emit a warning as per format_warning_va, but construct the substring_loc
78    for the character at offset (CHAR_IDX - 1) within a string constant
79    FORMAT_STRING_CST at FMT_STRING_LOC.  */
80 
81 ATTRIBUTE_GCC_DIAG (5,6)
82 static bool
83 format_warning_at_char (location_t fmt_string_loc, tree format_string_cst,
84 			int char_idx, int opt, const char *gmsgid, ...)
85 {
86   va_list ap;
87   va_start (ap, gmsgid);
88   tree string_type = TREE_TYPE (format_string_cst);
89 
90   /* The callers are of the form:
91        format_warning (format_string_loc, format_string_cst,
92 		       format_chars - orig_format_chars,
93       where format_chars has already been incremented, so that
94       CHAR_IDX is one character beyond where the warning should
95       be emitted.  Fix it.  */
96   char_idx -= 1;
97 
98   substring_loc fmt_loc (fmt_string_loc, string_type, char_idx, char_idx,
99 			 char_idx);
100   bool warned = format_warning_va (fmt_loc, UNKNOWN_LOCATION, NULL, opt,
101 				   gmsgid, &ap);
102   va_end (ap);
103 
104   return warned;
105 }
106 
107 /* Check that we have a pointer to a string suitable for use as a format.
108    The default is to check for a char type.
109    For objective-c dialects, this is extended to include references to string
110    objects validated by objc_string_ref_type_p ().
111    Targets may also provide a string object type that can be used within c and
112    c++ and shared with their respective objective-c dialects. In this case the
113    reference to a format string is checked for validity via a hook.
114 
115    The function returns true if strref points to any string type valid for the
116    language dialect and target.  */
117 
118 static bool
119 valid_stringptr_type_p (tree strref)
120 {
121   return (strref != NULL
122 	  && TREE_CODE (strref) == POINTER_TYPE
123 	  && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
124 	      || objc_string_ref_type_p (strref)
125 	      || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
126 }
127 
128 /* Handle a "format_arg" attribute; arguments as in
129    struct attribute_spec.handler.  */
130 tree
131 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
132 			     tree args, int flags, bool *no_add_attrs)
133 {
134   tree type = *node;
135   tree format_num_expr = TREE_VALUE (args);
136   unsigned HOST_WIDE_INT format_num = 0;
137 
138   if (!get_constant (format_num_expr, &format_num, 0))
139     {
140       error ("format string has invalid operand number");
141       *no_add_attrs = true;
142       return NULL_TREE;
143     }
144 
145   if (prototype_p (type))
146     {
147       /* The format arg can be any string reference valid for the language and
148 	target.  We cannot be more specific in this case.  */
149       if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
150 	return NULL_TREE;
151     }
152 
153   if (!valid_stringptr_type_p (TREE_TYPE (type)))
154     {
155       if (!(flags & (int) ATTR_FLAG_BUILT_IN))
156 	error ("function does not return string type");
157       *no_add_attrs = true;
158       return NULL_TREE;
159     }
160 
161   return NULL_TREE;
162 }
163 
164 /* Verify that the format_num argument is actually a string reference suitable,
165    for the language dialect and target (in case the format attribute is in
166    error).  When we know the specific reference type expected, this is also
167    checked.  */
168 static bool
169 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
170 		     int flags, bool *no_add_attrs, int expected_format_type)
171 {
172   unsigned HOST_WIDE_INT i;
173   bool is_objc_sref, is_target_sref, is_char_ref;
174   tree ref;
175   int fmt_flags;
176   function_args_iterator iter;
177 
178   i = 1;
179   FOREACH_FUNCTION_ARGS (fntype, ref, iter)
180     {
181       if (i == format_num)
182 	break;
183       i++;
184     }
185 
186   if (!ref
187       || !valid_stringptr_type_p (ref))
188     {
189       if (!(flags & (int) ATTR_FLAG_BUILT_IN))
190 	error ("format string argument is not a string type");
191       *no_add_attrs = true;
192       return false;
193     }
194 
195   /* We only know that we want a suitable string reference.  */
196   if (expected_format_type < 0)
197     return true;
198 
199   /* Now check that the arg matches the expected type.  */
200   is_char_ref =
201     (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
202 
203   fmt_flags = format_flags (expected_format_type);
204   is_objc_sref = is_target_sref = false;
205   if (!is_char_ref)
206     is_objc_sref = objc_string_ref_type_p (ref);
207 
208   if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
209     {
210       if (is_char_ref)
211 	return true; /* OK, we expected a char and found one.  */
212       else
213 	{
214 	  /* We expected a char but found an extended string type.  */
215 	  if (is_objc_sref)
216 	    error ("found a %qs reference but the format argument should"
217 		   " be a string", format_name (gcc_objc_string_format_type));
218 	  else
219 	    error ("found a %qT but the format argument should be a string",
220 		   ref);
221 	  *no_add_attrs = true;
222 	  return false;
223 	}
224     }
225 
226   /* We expect a string object type as the format arg.  */
227   if (is_char_ref)
228     {
229       error ("format argument should be a %qs reference but"
230 	     " a string was found", format_name (expected_format_type));
231       *no_add_attrs = true;
232       return false;
233     }
234 
235   /* We will assert that objective-c will support either its own string type
236      or the target-supplied variant.  */
237   if (!is_objc_sref)
238     is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
239 
240   if (expected_format_type == (int) gcc_objc_string_format_type
241       && (is_objc_sref || is_target_sref))
242     return true;
243 
244   /* We will allow a target string ref to match only itself.  */
245   if (first_target_format_type
246       && expected_format_type >= first_target_format_type
247       && is_target_sref)
248     return true;
249   else
250     {
251       error ("format argument should be a %qs reference",
252 	      format_name (expected_format_type));
253       *no_add_attrs = true;
254       return false;
255     }
256 
257   gcc_unreachable ();
258 }
259 
260 /* Verify EXPR is a constant, and store its value.
261    If validated_p is true there should be no errors.
262    Returns true on success, false otherwise.  */
263 static bool
264 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
265 {
266   if (!tree_fits_uhwi_p (expr))
267     {
268       gcc_assert (!validated_p);
269       return false;
270     }
271 
272   *value = TREE_INT_CST_LOW (expr);
273 
274   return true;
275 }
276 
277 /* Decode the arguments to a "format" attribute into a
278    function_format_info structure.  It is already known that the list
279    is of the right length.  If VALIDATED_P is true, then these
280    attributes have already been validated and must not be erroneous;
281    if false, it will give an error message.  Returns true if the
282    attributes are successfully decoded, false otherwise.  */
283 
284 static bool
285 decode_format_attr (tree args, function_format_info *info, int validated_p)
286 {
287   tree format_type_id = TREE_VALUE (args);
288   tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
289   tree first_arg_num_expr
290     = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
291 
292   if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
293     {
294       gcc_assert (!validated_p);
295       error ("unrecognized format specifier");
296       return false;
297     }
298   else
299     {
300       const char *p = IDENTIFIER_POINTER (format_type_id);
301 
302       p = convert_format_name_to_system_name (p);
303 
304       info->format_type = decode_format_type (p);
305 
306       if (!c_dialect_objc ()
307 	   && info->format_type == gcc_objc_string_format_type)
308 	{
309 	  gcc_assert (!validated_p);
310 	  warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
311 		   format_type_id);
312 	  info->format_type = format_type_error;
313 	  return false;
314 	}
315 
316       if (info->format_type == format_type_error)
317 	{
318 	  gcc_assert (!validated_p);
319 	  warning (OPT_Wformat_, "%qE is an unrecognized format function type",
320 		   format_type_id);
321 	  return false;
322 	}
323     }
324 
325   if (!get_constant (format_num_expr, &info->format_num, validated_p))
326     {
327       error ("format string has invalid operand number");
328       return false;
329     }
330 
331   if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
332     {
333       error ("%<...%> has invalid operand number");
334       return false;
335     }
336 
337   if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
338     {
339       gcc_assert (!validated_p);
340       error ("format string argument follows the args to be formatted");
341       return false;
342     }
343 
344   return true;
345 }
346 
347 /* Check a call to a format function against a parameter list.  */
348 
349 /* The C standard version C++ is treated as equivalent to
350    or inheriting from, for the purpose of format features supported.  */
351 #define CPLUSPLUS_STD_VER	(cxx_dialect < cxx11 ? STD_C94 : STD_C99)
352 /* The C standard version we are checking formats against when pedantic.  */
353 #define C_STD_VER		((int) (c_dialect_cxx ()		   \
354 				 ? CPLUSPLUS_STD_VER			   \
355 				 : (flag_isoc99				   \
356 				    ? STD_C99				   \
357 				    : (flag_isoc94 ? STD_C94 : STD_C89))))
358 /* The name to give to the standard version we are warning about when
359    pedantic.  FEATURE_VER is the version in which the feature warned out
360    appeared, which is higher than C_STD_VER.  */
361 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx ()		\
362 				 ? (cxx_dialect < cxx11 ? "ISO C++98" \
363 				    : "ISO C++11")		\
364 				 : ((FEATURE_VER) == STD_EXT	\
365 				    ? "ISO C"			\
366 				    : "ISO C90"))
367 /* Adjust a C standard version, which may be STD_C9L, to account for
368    -Wno-long-long.  Returns other standard versions unchanged.  */
369 #define ADJ_STD(VER)		((int) ((VER) == STD_C9L		      \
370 				       ? (warn_long_long ? STD_C99 : STD_C89) \
371 				       : (VER)))
372 
373 /* Enum describing the kind of specifiers present in the format and
374    requiring an argument.  */
375 enum format_specifier_kind {
376   CF_KIND_FORMAT,
377   CF_KIND_FIELD_WIDTH,
378   CF_KIND_FIELD_PRECISION
379 };
380 
381 static const char *kind_descriptions[] = {
382   N_("format"),
383   N_("field width specifier"),
384   N_("field precision specifier")
385 };
386 
387 /* Structure describing details of a type expected in format checking,
388    and the type to check against it.  */
389 struct format_wanted_type
390 {
391   /* The type wanted.  */
392   tree wanted_type;
393   /* The name of this type to use in diagnostics.  */
394   const char *wanted_type_name;
395   /* Should be type checked just for scalar width identity.  */
396   int scalar_identity_flag;
397   /* The level of indirection through pointers at which this type occurs.  */
398   int pointer_count;
399   /* Whether, when pointer_count is 1, to allow any character type when
400      pedantic, rather than just the character or void type specified.  */
401   int char_lenient_flag;
402   /* Whether the argument, dereferenced once, is written into and so the
403      argument must not be a pointer to a const-qualified type.  */
404   int writing_in_flag;
405   /* Whether the argument, dereferenced once, is read from and so
406      must not be a NULL pointer.  */
407   int reading_from_flag;
408   /* The kind of specifier that this type is used for.  */
409   enum format_specifier_kind kind;
410   /* The starting character of the specifier.  This never includes the
411      initial percent sign.  */
412   const char *format_start;
413   /* The length of the specifier.  */
414   int format_length;
415   /* The actual parameter to check against the wanted type.  */
416   tree param;
417   /* The argument number of that parameter.  */
418   int arg_num;
419   /* The offset location of this argument with respect to the format
420      string location.  */
421   unsigned int offset_loc;
422   /* The next type to check for this format conversion, or NULL if none.  */
423   struct format_wanted_type *next;
424 };
425 
426 /* Convenience macro for format_length_info meaning unused.  */
427 #define NO_FMT NULL, FMT_LEN_none, STD_C89
428 
429 static const format_length_info printf_length_specs[] =
430 {
431   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
432   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
433   { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
434   { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
435   { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
436   { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
437   { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
438   { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
439   { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
440   { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
441   { NO_FMT, NO_FMT, 0 }
442 };
443 
444 /* Length specifiers valid for asm_fprintf.  */
445 static const format_length_info asm_fprintf_length_specs[] =
446 {
447   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
448   { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
449   { NO_FMT, NO_FMT, 0 }
450 };
451 
452 /* Length specifiers valid for GCC diagnostics.  */
453 static const format_length_info gcc_diag_length_specs[] =
454 {
455   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
456   { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
457   { NO_FMT, NO_FMT, 0 }
458 };
459 
460 /* The custom diagnostics all accept the same length specifiers.  */
461 #define gcc_tdiag_length_specs gcc_diag_length_specs
462 #define gcc_cdiag_length_specs gcc_diag_length_specs
463 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
464 
465 /* This differs from printf_length_specs only in that "Z" is not accepted.  */
466 static const format_length_info scanf_length_specs[] =
467 {
468   { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
469   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
470   { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
471   { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
472   { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
473   { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
474   { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
475   { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
476   { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
477   { NO_FMT, NO_FMT, 0 }
478 };
479 
480 
481 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
482    make no sense for a format type not part of any C standard version.  */
483 static const format_length_info strfmon_length_specs[] =
484 {
485   /* A GNU extension.  */
486   { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
487   { NO_FMT, NO_FMT, 0 }
488 };
489 
490 
491 /* For now, the Fortran front-end routines only use l as length modifier.  */
492 static const format_length_info gcc_gfc_length_specs[] =
493 {
494   { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
495   { NO_FMT, NO_FMT, 0 }
496 };
497 
498 
499 static const format_flag_spec printf_flag_specs[] =
500 {
501   { ' ',  0, 0, 0, N_("' ' flag"),        N_("the ' ' printf flag"),              STD_C89 },
502   { '+',  0, 0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
503   { '#',  0, 0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
504   { '0',  0, 0, 0, N_("'0' flag"),        N_("the '0' printf flag"),              STD_C89 },
505   { '-',  0, 0, 0, N_("'-' flag"),        N_("the '-' printf flag"),              STD_C89 },
506   { '\'', 0, 0, 0, N_("''' flag"),        N_("the ''' printf flag"),              STD_EXT },
507   { 'I',  0, 0, 0, N_("'I' flag"),        N_("the 'I' printf flag"),              STD_EXT },
508   { 'w',  0, 0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
509   { 'p',  0, 0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
510   { 'L',  0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
511   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
512 };
513 
514 
515 static const format_flag_pair printf_flag_pairs[] =
516 {
517   { ' ', '+', 1, 0   },
518   { '0', '-', 1, 0   },
519   { '0', 'p', 1, 'i' },
520   { 0, 0, 0, 0 }
521 };
522 
523 static const format_flag_spec asm_fprintf_flag_specs[] =
524 {
525   { ' ',  0, 0, 0, N_("' ' flag"),        N_("the ' ' printf flag"),              STD_C89 },
526   { '+',  0, 0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
527   { '#',  0, 0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
528   { '0',  0, 0, 0, N_("'0' flag"),        N_("the '0' printf flag"),              STD_C89 },
529   { '-',  0, 0, 0, N_("'-' flag"),        N_("the '-' printf flag"),              STD_C89 },
530   { 'w',  0, 0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
531   { 'p',  0, 0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
532   { 'L',  0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
533   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
534 };
535 
536 static const format_flag_pair asm_fprintf_flag_pairs[] =
537 {
538   { ' ', '+', 1, 0   },
539   { '0', '-', 1, 0   },
540   { '0', 'p', 1, 'i' },
541   { 0, 0, 0, 0 }
542 };
543 
544 static const format_flag_pair gcc_diag_flag_pairs[] =
545 {
546   { 0, 0, 0, 0 }
547 };
548 
549 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
550 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
551 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
552 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
553 
554 static const format_flag_spec gcc_diag_flag_specs[] =
555 {
556   { '+',  0, 0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
557   { '#',  0, 0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
558   { 'q',  0, 0, 1, N_("'q' flag"),        N_("the 'q' diagnostic flag"),          STD_C89 },
559   { 'p',  0, 0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
560   { 'L',  0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
561   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
562 };
563 
564 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
565 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
566 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
567 #define gcc_gfc_flag_specs gcc_diag_flag_specs
568 
569 static const format_flag_spec scanf_flag_specs[] =
570 {
571   { '*',  0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
572   { 'a',  0, 0, 0, N_("'a' flag"),               N_("the 'a' scanf flag"),                       STD_EXT },
573   { 'm',  0, 0, 0, N_("'m' flag"),               N_("the 'm' scanf flag"),                       STD_EXT },
574   { 'w',  0, 0, 0, N_("field width"),            N_("field width in scanf format"),              STD_C89 },
575   { 'L',  0, 0, 0, N_("length modifier"),        N_("length modifier in scanf format"),          STD_C89 },
576   { '\'', 0, 0, 0, N_("''' flag"),               N_("the ''' scanf flag"),                       STD_EXT },
577   { 'I',  0, 0, 0, N_("'I' flag"),               N_("the 'I' scanf flag"),                       STD_EXT },
578   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
579 };
580 
581 
582 static const format_flag_pair scanf_flag_pairs[] =
583 {
584   { '*', 'L', 0, 0 },
585   { 'a', 'm', 0, 0 },
586   { 0, 0, 0, 0 }
587 };
588 
589 
590 static const format_flag_spec strftime_flag_specs[] =
591 {
592   { '_', 0,   0, 0, N_("'_' flag"),     N_("the '_' strftime flag"),          STD_EXT },
593   { '-', 0,   0, 0, N_("'-' flag"),     N_("the '-' strftime flag"),          STD_EXT },
594   { '0', 0,   0, 0, N_("'0' flag"),     N_("the '0' strftime flag"),          STD_EXT },
595   { '^', 0,   0, 0, N_("'^' flag"),     N_("the '^' strftime flag"),          STD_EXT },
596   { '#', 0,   0, 0, N_("'#' flag"),     N_("the '#' strftime flag"),          STD_EXT },
597   { 'w', 0,   0, 0, N_("field width"),  N_("field width in strftime format"), STD_EXT },
598   { 'E', 0,   0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"),      STD_C99 },
599   { 'O', 0,   0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"),      STD_C99 },
600   { 'O', 'o', 0, 0, NULL,               N_("the 'O' modifier"),               STD_EXT },
601   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
602 };
603 
604 
605 static const format_flag_pair strftime_flag_pairs[] =
606 {
607   { 'E', 'O', 0, 0 },
608   { '_', '-', 0, 0 },
609   { '_', '0', 0, 0 },
610   { '-', '0', 0, 0 },
611   { '^', '#', 0, 0 },
612   { 0, 0, 0, 0 }
613 };
614 
615 
616 static const format_flag_spec strfmon_flag_specs[] =
617 {
618   { '=',  0, 1, 0, N_("fill character"),  N_("fill character in strfmon format"),  STD_C89 },
619   { '^',  0, 0, 0, N_("'^' flag"),        N_("the '^' strfmon flag"),              STD_C89 },
620   { '+',  0, 0, 0, N_("'+' flag"),        N_("the '+' strfmon flag"),              STD_C89 },
621   { '(',  0, 0, 0, N_("'(' flag"),        N_("the '(' strfmon flag"),              STD_C89 },
622   { '!',  0, 0, 0, N_("'!' flag"),        N_("the '!' strfmon flag"),              STD_C89 },
623   { '-',  0, 0, 0, N_("'-' flag"),        N_("the '-' strfmon flag"),              STD_C89 },
624   { 'w',  0, 0, 0, N_("field width"),     N_("field width in strfmon format"),     STD_C89 },
625   { '#',  0, 0, 0, N_("left precision"),  N_("left precision in strfmon format"),  STD_C89 },
626   { 'p',  0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
627   { 'L',  0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
628   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
629 };
630 
631 static const format_flag_pair strfmon_flag_pairs[] =
632 {
633   { '+', '(', 0, 0 },
634   { 0, 0, 0, 0 }
635 };
636 
637 
638 static const format_char_info print_char_table[] =
639 {
640   /* C89 conversion specifiers.  */
641   { "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 },
642   { "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 },
643   { "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 },
644   { "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 },
645   { "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 },
646   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T94_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-w",        "",   NULL },
647   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp",       "cR", NULL },
648   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-w",        "c",  NULL },
649   { "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 },
650   /* C99 conversion specifiers.  */
651   { "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 },
652   { "aA",  0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0 +#",   "",   NULL },
653   /* X/Open conversion specifiers.  */
654   { "C",   0, STD_EXT, { TEX_WI,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-w",        "",   NULL },
655   { "S",   1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp",       "R",  NULL },
656   /* GNU conversion specifiers.  */
657   { "m",   0, STD_EXT, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp",       "",   NULL },
658   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
659 };
660 
661 static const format_char_info asm_fprintf_char_table[] =
662 {
663   /* C89 conversion specifiers.  */
664   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0 +",  "i", NULL },
665   { "oxX", 0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0#",   "i", NULL },
666   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp0",    "i", NULL },
667   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-w",       "", NULL },
668   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "-wp",    "cR", NULL },
669 
670   /* asm_fprintf conversion specifiers.  */
671   { "O",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
672   { "R",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
673   { "I",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
674   { "L",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
675   { "U",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
676   { "r",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",  "", NULL },
677   { "z",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
678   { "@",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
679   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
680 };
681 
682 static const format_char_info gcc_diag_char_table[] =
683 {
684   /* C89 conversion specifiers.  */
685   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
686   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
687   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
688   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
689   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
690   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
691 
692   /* Custom conversion specifiers.  */
693 
694   /* G requires a "gcall*" argument at runtime.  */
695   { "G",   1, STD_C89, { T89_G,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "\"",   NULL },
696   /* K requires a "tree" argument at runtime.  */
697   { "K",   1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "\"",   NULL },
698 
699   { "r",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "//cR",   NULL },
700   { "<",   0, STD_C89, NOARGUMENTS, "",      "<",   NULL },
701   { ">",   0, STD_C89, NOARGUMENTS, "",      ">",   NULL },
702   { "'" ,  0, STD_C89, NOARGUMENTS, "",      "",    NULL },
703   { "R",   0, STD_C89, NOARGUMENTS, "",     "\\",   NULL },
704   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
705   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
706 };
707 
708 static const format_char_info gcc_tdiag_char_table[] =
709 {
710   /* C89 conversion specifiers.  */
711   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
712   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
713   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
714   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
715   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
716   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
717 
718   /* Custom conversion specifiers.  */
719 
720   /* These will require a "tree" at runtime.  */
721   { "DFTV", 1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+", "'",   NULL },
722   { "E", 1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+", "",   NULL },
723   { "K", 1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
724 
725   /* G requires a "gcall*" argument at runtime.  */
726   { "G", 1, STD_C89, { T89_G,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
727 
728   { "v",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q#",  "",   NULL },
729 
730   { "r",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "/cR",   NULL },
731   { "<",   0, STD_C89, NOARGUMENTS, "",      "<",   NULL },
732   { ">",   0, STD_C89, NOARGUMENTS, "",      ">",   NULL },
733   { "'",   0, STD_C89, NOARGUMENTS, "",      "",    NULL },
734   { "R",   0, STD_C89, NOARGUMENTS, "",     "\\",   NULL },
735   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
736   { "Z",   1, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "", &gcc_tdiag_char_table[0] },
737   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
738 };
739 
740 static const format_char_info gcc_cdiag_char_table[] =
741 {
742   /* C89 conversion specifiers.  */
743   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
744   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
745   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
746   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
747   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
748   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
749 
750   /* Custom conversion specifiers.  */
751 
752   /* These will require a "tree" at runtime.  */
753   { "DFTV", 1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+", "'",   NULL },
754   { "E",   1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+", "",   NULL },
755   { "K",   1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
756 
757   /* G requires a "gcall*" argument at runtime.  */
758   { "G",   1, STD_C89, { T89_G,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
759 
760   { "v",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q#",  "",   NULL },
761 
762   { "r",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "/cR",   NULL },
763   { "<",   0, STD_C89, NOARGUMENTS, "",      "<",  NULL },
764   { ">",   0, STD_C89, NOARGUMENTS, "",      ">",  NULL },
765   { "'",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
766   { "R",   0, STD_C89, NOARGUMENTS, "",     "\\",  NULL },
767   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
768   { "Z",   1, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "", &gcc_tdiag_char_table[0] },
769   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
770 };
771 
772 static const format_char_info gcc_cxxdiag_char_table[] =
773 {
774   /* C89 conversion specifiers.  */
775   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
776   { "ox",  0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
777   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  T9L_ULL, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
778   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
779   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "pq", "cR", NULL },
780   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "c",  NULL },
781 
782   /* Custom conversion specifiers.  */
783 
784   /* These will require a "tree" at runtime.  */
785   { "ADFHISTVX",1,STD_C89,{ T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+#",   "'",   NULL },
786   { "E", 1,STD_C89,{ T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q+#",   "",   NULL },
787   { "K", 1, STD_C89,{ T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",   "\"",   NULL },
788   { "v", 0,STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q#",  "",   NULL },
789 
790   /* G requires a "gcall*" argument at runtime.  */
791   { "G", 1, STD_C89,{ T89_G,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",   "\"",   NULL },
792 
793   /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.)  */
794   { "CLOPQ",0,STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q",  "",   NULL },
795 
796   { "r",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "/cR",   NULL },
797   { "<",   0, STD_C89, NOARGUMENTS, "",      "<",   NULL },
798   { ">",   0, STD_C89, NOARGUMENTS, "",      ">",   NULL },
799   { "'",   0, STD_C89, NOARGUMENTS, "",      "",    NULL },
800   { "R",   0, STD_C89, NOARGUMENTS, "",      "\\",  NULL },
801   { "m",   0, STD_C89, NOARGUMENTS, "q",     "",   NULL },
802   { "Z",   1, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "",    "", &gcc_tdiag_char_table[0] },
803   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
804 };
805 
806 static const format_char_info gcc_gfc_char_table[] =
807 {
808   /* C89 conversion specifiers.  */
809   { "di",  0, STD_C89, { T89_I,   BADLEN,  BADLEN,  T89_L,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q", "", NULL },
810   { "u",   0, STD_C89, { T89_UI,  BADLEN,  BADLEN,  T89_UL,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q", "", NULL },
811   { "c",   0, STD_C89, { T89_I,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q", "", NULL },
812   { "s",   1, STD_C89, { T89_C,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "q", "cR", NULL },
813 
814   /* gfc conversion specifiers.  */
815 
816   { "C",   0, STD_C89, NOARGUMENTS, "",      "",   NULL },
817 
818   /* This will require a "locus" at runtime.  */
819   { "L",   0, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "R", NULL },
820 
821   /* These will require nothing.  */
822   { "<>",0, STD_C89, NOARGUMENTS, "",      "",   NULL },
823   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
824 };
825 
826 static const format_char_info scan_char_table[] =
827 {
828   /* C89 conversion specifiers.  */
829   { "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 },
830   { "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 },
831   { "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 },
832   { "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 },
833   { "c",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*mw",   "cW",  NULL },
834   { "s",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*amw",  "cW",  NULL },
835   { "[",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*amw",  "cW[", NULL },
836   { "p",     2, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w",   "W",   NULL },
837   { "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 },
838   /* C99 conversion specifiers.  */
839   { "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 },
840   { "aA",   1, STD_C99, { T99_F,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_LD,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w'",  "W",   NULL },
841   /* X/Open conversion specifiers.  */
842   { "C",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*mw",   "W",   NULL },
843   { "S",     1, STD_EXT, { TEX_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*amw",  "W",   NULL },
844   { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
845 };
846 
847 static const format_char_info time_char_table[] =
848 {
849   /* C89 conversion specifiers.  */
850   { "ABZab",		0, STD_C89, NOLENGTHS, "^#",     "",   NULL },
851   { "cx",		0, STD_C89, NOLENGTHS, "E",      "3",  NULL },
852   { "HIMSUWdmw",	0, STD_C89, NOLENGTHS, "-_0Ow",  "",   NULL },
853   { "j",		0, STD_C89, NOLENGTHS, "-_0Ow",  "o",  NULL },
854   { "p",		0, STD_C89, NOLENGTHS, "#",      "",   NULL },
855   { "X",		0, STD_C89, NOLENGTHS, "E",      "",   NULL },
856   { "y",		0, STD_C89, NOLENGTHS, "EO-_0w", "4",  NULL },
857   { "Y",		0, STD_C89, NOLENGTHS, "-_0EOw", "o",  NULL },
858   { "%",		0, STD_C89, NOLENGTHS, "",       "",   NULL },
859   /* C99 conversion specifiers.  */
860   { "C",		0, STD_C99, NOLENGTHS, "-_0EOw", "o",  NULL },
861   { "D",		0, STD_C99, NOLENGTHS, "",       "2",  NULL },
862   { "eVu",		0, STD_C99, NOLENGTHS, "-_0Ow",  "",   NULL },
863   { "FRTnrt",		0, STD_C99, NOLENGTHS, "",       "",   NULL },
864   { "g",		0, STD_C99, NOLENGTHS, "O-_0w",  "2o", NULL },
865   { "G",		0, STD_C99, NOLENGTHS, "-_0Ow",  "o",  NULL },
866   { "h",		0, STD_C99, NOLENGTHS, "^#",     "",   NULL },
867   { "z",		0, STD_C99, NOLENGTHS, "O",      "o",  NULL },
868   /* GNU conversion specifiers.  */
869   { "kls",		0, STD_EXT, NOLENGTHS, "-_0Ow",  "",   NULL },
870   { "P",		0, STD_EXT, NOLENGTHS, "",       "",   NULL },
871 #if 1  /* DragonFly base: strftime() %+ specifier. */
872   { "+",		0, STD_EXT, NOLENGTHS, "E",      "3",  NULL },
873 #endif
874   { NULL,		0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
875 };
876 
877 static const format_char_info monetary_char_table[] =
878 {
879   { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
880   { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
881 };
882 
883 /* This must be in the same order as enum format_type.  */
884 static const format_kind_info format_types_orig[] =
885 {
886   { "gnu_printf",   printf_length_specs,  print_char_table, " +#0-'I", NULL,
887     printf_flag_specs, printf_flag_pairs,
888     FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
889     'w', 0, 'p', 0, 'L', 0,
890     &integer_type_node, &integer_type_node
891   },
892   { "asm_fprintf",   asm_fprintf_length_specs,  asm_fprintf_char_table, " +#0-", NULL,
893     asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
894     FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
895     'w', 0, 'p', 0, 'L', 0,
896     NULL, NULL
897   },
898   { "gcc_diag",   gcc_diag_length_specs,  gcc_diag_char_table, "q+#", NULL,
899     gcc_diag_flag_specs, gcc_diag_flag_pairs,
900     FMT_FLAG_ARG_CONVERT,
901     0, 0, 'p', 0, 'L', 0,
902     NULL, &integer_type_node
903   },
904   { "gcc_tdiag",   gcc_tdiag_length_specs,  gcc_tdiag_char_table, "q+#", NULL,
905     gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
906     FMT_FLAG_ARG_CONVERT,
907     0, 0, 'p', 0, 'L', 0,
908     NULL, &integer_type_node
909   },
910   { "gcc_cdiag",   gcc_cdiag_length_specs,  gcc_cdiag_char_table, "q+#", NULL,
911     gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
912     FMT_FLAG_ARG_CONVERT,
913     0, 0, 'p', 0, 'L', 0,
914     NULL, &integer_type_node
915   },
916   { "gcc_cxxdiag",   gcc_cxxdiag_length_specs,  gcc_cxxdiag_char_table, "q+#", NULL,
917     gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
918     FMT_FLAG_ARG_CONVERT,
919     0, 0, 'p', 0, 'L', 0,
920     NULL, &integer_type_node
921   },
922   { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "q+#", NULL,
923     gcc_gfc_flag_specs, gcc_gfc_flag_pairs,
924     FMT_FLAG_ARG_CONVERT,
925     0, 0, 0, 0, 0, 0,
926     NULL, NULL
927   },
928   { "NSString",   NULL,  NULL, NULL, NULL,
929     NULL, NULL,
930     FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
931     NULL, NULL
932   },
933   { "gnu_scanf",    scanf_length_specs,   scan_char_table,  "*'I", NULL,
934     scanf_flag_specs, scanf_flag_pairs,
935     FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
936     'w', 0, 0, '*', 'L', 'm',
937     NULL, NULL
938   },
939   { "gnu_strftime", NULL,                 time_char_table,  "_-0^#", "EO",
940     strftime_flag_specs, strftime_flag_pairs,
941     FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
942     NULL, NULL
943   },
944   { "gnu_strfmon",  strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
945     strfmon_flag_specs, strfmon_flag_pairs,
946     FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
947     NULL, NULL
948   }
949 };
950 
951 /* This layer of indirection allows GCC to reassign format_types with
952    new data if necessary, while still allowing the original data to be
953    const.  */
954 static const format_kind_info *format_types = format_types_orig;
955 /* We can modify this one.  We also add target-specific format types
956    to the end of the array.  */
957 static format_kind_info *dynamic_format_types;
958 
959 static int n_format_types = ARRAY_SIZE (format_types_orig);
960 
961 /* Structure detailing the results of checking a format function call
962    where the format expression may be a conditional expression with
963    many leaves resulting from nested conditional expressions.  */
964 struct format_check_results
965 {
966   /* Number of leaves of the format argument that could not be checked
967      as they were not string literals.  */
968   int number_non_literal;
969   /* Number of leaves of the format argument that were null pointers or
970      string literals, but had extra format arguments.  */
971   int number_extra_args;
972   location_t extra_arg_loc;
973   /* Number of leaves of the format argument that were null pointers or
974      string literals, but had extra format arguments and used $ operand
975      numbers.  */
976   int number_dollar_extra_args;
977   /* Number of leaves of the format argument that were wide string
978      literals.  */
979   int number_wide;
980   /* Number of leaves of the format argument that were empty strings.  */
981   int number_empty;
982   /* Number of leaves of the format argument that were unterminated
983      strings.  */
984   int number_unterminated;
985   /* Number of leaves of the format argument that were not counted above.  */
986   int number_other;
987   /* Location of the format string.  */
988   location_t format_string_loc;
989 };
990 
991 struct format_check_context
992 {
993   format_check_results *res;
994   function_format_info *info;
995   tree params;
996   vec<location_t> *arglocs;
997 };
998 
999 /* Return the format name (as specified in the original table) for the format
1000    type indicated by format_num.  */
1001 static const char *
1002 format_name (int format_num)
1003 {
1004   if (format_num >= 0 && format_num < n_format_types)
1005     return format_types[format_num].name;
1006   gcc_unreachable ();
1007 }
1008 
1009 /* Return the format flags (as specified in the original table) for the format
1010    type indicated by format_num.  */
1011 static int
1012 format_flags (int format_num)
1013 {
1014   if (format_num >= 0 && format_num < n_format_types)
1015     return format_types[format_num].flags;
1016   gcc_unreachable ();
1017 }
1018 
1019 static void check_format_info (function_format_info *, tree,
1020 			       vec<location_t> *);
1021 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
1022 static void check_format_info_main (format_check_results *,
1023 				    function_format_info *, const char *,
1024 				    location_t, tree,
1025 				    int, tree,
1026 				    unsigned HOST_WIDE_INT,
1027 				    object_allocator<format_wanted_type> &,
1028 				    vec<location_t> *);
1029 
1030 static void init_dollar_format_checking (int, tree);
1031 static int maybe_read_dollar_number (const char **, int,
1032 				     tree, tree *, const format_kind_info *);
1033 static bool avoid_dollar_number (const char *);
1034 static void finish_dollar_format_checking (format_check_results *, int);
1035 
1036 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
1037 					      int, const char *);
1038 
1039 static void check_format_types (const substring_loc &fmt_loc,
1040 				format_wanted_type *,
1041 				const format_kind_info *fki,
1042 				int offset_to_type_start,
1043 				char conversion_char,
1044 				vec<location_t> *arglocs);
1045 static void format_type_warning (const substring_loc &fmt_loc,
1046 				 location_t param_loc,
1047 				 format_wanted_type *, tree,
1048 				 tree,
1049 				 const format_kind_info *fki,
1050 				 int offset_to_type_start,
1051 				 char conversion_char);
1052 
1053 /* Decode a format type from a string, returning the type, or
1054    format_type_error if not valid, in which case the caller should print an
1055    error message.  */
1056 static int
1057 decode_format_type (const char *s)
1058 {
1059   int i;
1060   int slen;
1061 
1062   s = convert_format_name_to_system_name (s);
1063   slen = strlen (s);
1064   for (i = 0; i < n_format_types; i++)
1065     {
1066       int alen;
1067       if (!strcmp (s, format_types[i].name))
1068 	return i;
1069       alen = strlen (format_types[i].name);
1070       if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1071 	  && s[slen - 1] == '_' && s[slen - 2] == '_'
1072 	  && !strncmp (s + 2, format_types[i].name, alen))
1073 	return i;
1074     }
1075   return format_type_error;
1076 }
1077 
1078 
1079 /* Check the argument list of a call to printf, scanf, etc.
1080    ATTRS are the attributes on the function type.  There are NARGS argument
1081    values in the array ARGARRAY.
1082    Also, if -Wsuggest-attribute=format,
1083    warn for calls to vprintf or vscanf in functions with no such format
1084    attribute themselves.  */
1085 
1086 void
1087 check_function_format (tree attrs, int nargs, tree *argarray,
1088 		       vec<location_t> *arglocs)
1089 {
1090   tree a;
1091 
1092   /* See if this function has any format attributes.  */
1093   for (a = attrs; a; a = TREE_CHAIN (a))
1094     {
1095       if (is_attribute_p ("format", TREE_PURPOSE (a)))
1096 	{
1097 	  /* Yup; check it.  */
1098 	  function_format_info info;
1099 	  decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
1100 	  if (warn_format)
1101 	    {
1102 	      /* FIXME: Rewrite all the internal functions in this file
1103 		 to use the ARGARRAY directly instead of constructing this
1104 		 temporary list.  */
1105 	      tree params = NULL_TREE;
1106 	      int i;
1107 	      for (i = nargs - 1; i >= 0; i--)
1108 		params = tree_cons (NULL_TREE, argarray[i], params);
1109 	      check_format_info (&info, params, arglocs);
1110 	    }
1111 
1112 	  /* Attempt to detect whether the current function might benefit
1113 	     from the format attribute if the called function is decorated
1114 	     with it.  Avoid using calls with string literal formats for
1115 	     guidance since those are unlikely to be viable candidates.  */
1116 	  if (warn_suggest_attribute_format
1117 	      && current_function_decl != NULL_TREE
1118 	      && info.first_arg_num == 0
1119 	      && (format_types[info.format_type].flags
1120 		  & (int) FMT_FLAG_ARG_CONVERT)
1121 	      /* c_strlen will fail for a function parameter but succeed
1122 		 for a literal or constant array.  */
1123 	      && !c_strlen (argarray[info.format_num - 1], 1))
1124 	    {
1125 	      tree c;
1126 	      for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1127 		   c;
1128 		   c = TREE_CHAIN (c))
1129 		if (is_attribute_p ("format", TREE_PURPOSE (c))
1130 		    && (decode_format_type (IDENTIFIER_POINTER
1131 					    (TREE_VALUE (TREE_VALUE (c))))
1132 			== info.format_type))
1133 		  break;
1134 	      if (c == NULL_TREE)
1135 		{
1136 		  /* Check if the current function has a parameter to which
1137 		     the format attribute could be attached; if not, it
1138 		     can't be a candidate for a format attribute, despite
1139 		     the vprintf-like or vscanf-like call.  */
1140 		  tree args;
1141 		  for (args = DECL_ARGUMENTS (current_function_decl);
1142 		       args != 0;
1143 		       args = DECL_CHAIN (args))
1144 		    {
1145 		      if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1146 			  && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1147 			      == char_type_node))
1148 			break;
1149 		    }
1150 		  if (args != 0)
1151 		    warning (OPT_Wsuggest_attribute_format, "function %qD "
1152 			     "might be a candidate for %qs format attribute",
1153 			     current_function_decl,
1154 			     format_types[info.format_type].name);
1155 		}
1156 	    }
1157 	}
1158     }
1159 }
1160 
1161 
1162 /* Variables used by the checking of $ operand number formats.  */
1163 static char *dollar_arguments_used = NULL;
1164 static char *dollar_arguments_pointer_p = NULL;
1165 static int dollar_arguments_alloc = 0;
1166 static int dollar_arguments_count;
1167 static int dollar_first_arg_num;
1168 static int dollar_max_arg_used;
1169 static int dollar_format_warned;
1170 
1171 /* Initialize the checking for a format string that may contain $
1172    parameter number specifications; we will need to keep track of whether
1173    each parameter has been used.  FIRST_ARG_NUM is the number of the first
1174    argument that is a parameter to the format, or 0 for a vprintf-style
1175    function; PARAMS is the list of arguments starting at this argument.  */
1176 
1177 static void
1178 init_dollar_format_checking (int first_arg_num, tree params)
1179 {
1180   tree oparams = params;
1181 
1182   dollar_first_arg_num = first_arg_num;
1183   dollar_arguments_count = 0;
1184   dollar_max_arg_used = 0;
1185   dollar_format_warned = 0;
1186   if (first_arg_num > 0)
1187     {
1188       while (params)
1189 	{
1190 	  dollar_arguments_count++;
1191 	  params = TREE_CHAIN (params);
1192 	}
1193     }
1194   if (dollar_arguments_alloc < dollar_arguments_count)
1195     {
1196       free (dollar_arguments_used);
1197       free (dollar_arguments_pointer_p);
1198       dollar_arguments_alloc = dollar_arguments_count;
1199       dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1200       dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1201     }
1202   if (dollar_arguments_alloc)
1203     {
1204       memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1205       if (first_arg_num > 0)
1206 	{
1207 	  int i = 0;
1208 	  params = oparams;
1209 	  while (params)
1210 	    {
1211 	      dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1212 					       == POINTER_TYPE);
1213 	      params = TREE_CHAIN (params);
1214 	      i++;
1215 	    }
1216 	}
1217     }
1218 }
1219 
1220 
1221 /* Look for a decimal number followed by a $ in *FORMAT.  If DOLLAR_NEEDED
1222    is set, it is an error if one is not found; otherwise, it is OK.  If
1223    such a number is found, check whether it is within range and mark that
1224    numbered operand as being used for later checking.  Returns the operand
1225    number if found and within range, zero if no such number was found and
1226    this is OK, or -1 on error.  PARAMS points to the first operand of the
1227    format; PARAM_PTR is made to point to the parameter referred to.  If
1228    a $ format is found, *FORMAT is updated to point just after it.  */
1229 
1230 static int
1231 maybe_read_dollar_number (const char **format,
1232 			  int dollar_needed, tree params, tree *param_ptr,
1233 			  const format_kind_info *fki)
1234 {
1235   int argnum;
1236   int overflow_flag;
1237   const char *fcp = *format;
1238   if (!ISDIGIT (*fcp))
1239     {
1240       if (dollar_needed)
1241 	{
1242 	  warning (OPT_Wformat_, "missing $ operand number in format");
1243 	  return -1;
1244 	}
1245       else
1246 	return 0;
1247     }
1248   argnum = 0;
1249   overflow_flag = 0;
1250   while (ISDIGIT (*fcp))
1251     {
1252       int nargnum;
1253       nargnum = 10 * argnum + (*fcp - '0');
1254       if (nargnum < 0 || nargnum / 10 != argnum)
1255 	overflow_flag = 1;
1256       argnum = nargnum;
1257       fcp++;
1258     }
1259   if (*fcp != '$')
1260     {
1261       if (dollar_needed)
1262 	{
1263 	  warning (OPT_Wformat_, "missing $ operand number in format");
1264 	  return -1;
1265 	}
1266       else
1267 	return 0;
1268     }
1269   *format = fcp + 1;
1270   if (pedantic && !dollar_format_warned)
1271     {
1272       warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1273 	       C_STD_NAME (STD_EXT));
1274       dollar_format_warned = 1;
1275     }
1276   if (overflow_flag || argnum == 0
1277       || (dollar_first_arg_num && argnum > dollar_arguments_count))
1278     {
1279       warning (OPT_Wformat_, "operand number out of range in format");
1280       return -1;
1281     }
1282   if (argnum > dollar_max_arg_used)
1283     dollar_max_arg_used = argnum;
1284   /* For vprintf-style functions we may need to allocate more memory to
1285      track which arguments are used.  */
1286   while (dollar_arguments_alloc < dollar_max_arg_used)
1287     {
1288       int nalloc;
1289       nalloc = 2 * dollar_arguments_alloc + 16;
1290       dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1291 					  nalloc);
1292       dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1293 					       nalloc);
1294       memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1295 	      nalloc - dollar_arguments_alloc);
1296       dollar_arguments_alloc = nalloc;
1297     }
1298   if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1299       && dollar_arguments_used[argnum - 1] == 1)
1300     {
1301       dollar_arguments_used[argnum - 1] = 2;
1302       warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1303 	       argnum, fki->name);
1304     }
1305   else
1306     dollar_arguments_used[argnum - 1] = 1;
1307   if (dollar_first_arg_num)
1308     {
1309       int i;
1310       *param_ptr = params;
1311       for (i = 1; i < argnum && *param_ptr != 0; i++)
1312 	*param_ptr = TREE_CHAIN (*param_ptr);
1313 
1314       /* This case shouldn't be caught here.  */
1315       gcc_assert (*param_ptr);
1316     }
1317   else
1318     *param_ptr = 0;
1319   return argnum;
1320 }
1321 
1322 /* Ensure that FORMAT does not start with a decimal number followed by
1323    a $; give a diagnostic and return true if it does, false otherwise.  */
1324 
1325 static bool
1326 avoid_dollar_number (const char *format)
1327 {
1328   if (!ISDIGIT (*format))
1329     return false;
1330   while (ISDIGIT (*format))
1331     format++;
1332   if (*format == '$')
1333     {
1334       warning (OPT_Wformat_, "$ operand number used after format without operand number");
1335       return true;
1336     }
1337   return false;
1338 }
1339 
1340 
1341 /* Finish the checking for a format string that used $ operand number formats
1342    instead of non-$ formats.  We check for unused operands before used ones
1343    (a serious error, since the implementation of the format function
1344    can't know what types to pass to va_arg to find the later arguments).
1345    and for unused operands at the end of the format (if we know how many
1346    arguments the format had, so not for vprintf).  If there were operand
1347    numbers out of range on a non-vprintf-style format, we won't have reached
1348    here.  If POINTER_GAP_OK, unused arguments are OK if all arguments are
1349    pointers.  */
1350 
1351 static void
1352 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1353 {
1354   int i;
1355   bool found_pointer_gap = false;
1356   for (i = 0; i < dollar_max_arg_used; i++)
1357     {
1358       if (!dollar_arguments_used[i])
1359 	{
1360 	  if (pointer_gap_ok && (dollar_first_arg_num == 0
1361 				 || dollar_arguments_pointer_p[i]))
1362 	    found_pointer_gap = true;
1363 	  else
1364 	    warning_at (res->format_string_loc, OPT_Wformat_,
1365 			"format argument %d unused before used argument %d in $-style format",
1366 			i + 1, dollar_max_arg_used);
1367 	}
1368     }
1369   if (found_pointer_gap
1370       || (dollar_first_arg_num
1371 	  && dollar_max_arg_used < dollar_arguments_count))
1372     {
1373       res->number_other--;
1374       res->number_dollar_extra_args++;
1375     }
1376 }
1377 
1378 
1379 /* Retrieve the specification for a format flag.  SPEC contains the
1380    specifications for format flags for the applicable kind of format.
1381    FLAG is the flag in question.  If PREDICATES is NULL, the basic
1382    spec for that flag must be retrieved and must exist.  If
1383    PREDICATES is not NULL, it is a string listing possible predicates
1384    for the spec entry; if an entry predicated on any of these is
1385    found, it is returned, otherwise NULL is returned.  */
1386 
1387 static const format_flag_spec *
1388 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1389 {
1390   int i;
1391   for (i = 0; spec[i].flag_char != 0; i++)
1392     {
1393       if (spec[i].flag_char != flag)
1394 	continue;
1395       if (predicates != NULL)
1396 	{
1397 	  if (spec[i].predicate != 0
1398 	      && strchr (predicates, spec[i].predicate) != 0)
1399 	    return &spec[i];
1400 	}
1401       else if (spec[i].predicate == 0)
1402 	return &spec[i];
1403     }
1404   gcc_assert (predicates);
1405   return NULL;
1406 }
1407 
1408 
1409 /* Check the argument list of a call to printf, scanf, etc.
1410    INFO points to the function_format_info structure.
1411    PARAMS is the list of argument values.  */
1412 
1413 static void
1414 check_format_info (function_format_info *info, tree params,
1415 		   vec<location_t> *arglocs)
1416 {
1417   format_check_context format_ctx;
1418   unsigned HOST_WIDE_INT arg_num;
1419   tree format_tree;
1420   format_check_results res;
1421   /* Skip to format argument.  If the argument isn't available, there's
1422      no work for us to do; prototype checking will catch the problem.  */
1423   for (arg_num = 1; ; ++arg_num)
1424     {
1425       if (params == 0)
1426 	return;
1427       if (arg_num == info->format_num)
1428 	break;
1429       params = TREE_CHAIN (params);
1430     }
1431   format_tree = TREE_VALUE (params);
1432   params = TREE_CHAIN (params);
1433   if (format_tree == 0)
1434     return;
1435 
1436   res.number_non_literal = 0;
1437   res.number_extra_args = 0;
1438   res.extra_arg_loc = UNKNOWN_LOCATION;
1439   res.number_dollar_extra_args = 0;
1440   res.number_wide = 0;
1441   res.number_empty = 0;
1442   res.number_unterminated = 0;
1443   res.number_other = 0;
1444   res.format_string_loc = input_location;
1445 
1446   format_ctx.res = &res;
1447   format_ctx.info = info;
1448   format_ctx.params = params;
1449   format_ctx.arglocs = arglocs;
1450 
1451   check_function_arguments_recurse (check_format_arg, &format_ctx,
1452 				    format_tree, arg_num);
1453 
1454   location_t loc = format_ctx.res->format_string_loc;
1455 
1456   if (res.number_non_literal > 0)
1457     {
1458       /* Functions taking a va_list normally pass a non-literal format
1459 	 string.  These functions typically are declared with
1460 	 first_arg_num == 0, so avoid warning in those cases.  */
1461       if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1462 	{
1463 	  /* For strftime-like formats, warn for not checking the format
1464 	     string; but there are no arguments to check.  */
1465 	  warning_at (loc, OPT_Wformat_nonliteral,
1466 		      "format not a string literal, format string not checked");
1467 	}
1468       else if (info->first_arg_num != 0)
1469 	{
1470 	  /* If there are no arguments for the format at all, we may have
1471 	     printf (foo) which is likely to be a security hole.  */
1472 	  while (arg_num + 1 < info->first_arg_num)
1473 	    {
1474 	      if (params == 0)
1475 		break;
1476 	      params = TREE_CHAIN (params);
1477 	      ++arg_num;
1478 	    }
1479 	  if (params == 0 && warn_format_security)
1480 	    warning_at (loc, OPT_Wformat_security,
1481 			"format not a string literal and no format arguments");
1482 	  else if (params == 0 && warn_format_nonliteral)
1483 	    warning_at (loc, OPT_Wformat_nonliteral,
1484 			"format not a string literal and no format arguments");
1485 	  else
1486 	    warning_at (loc, OPT_Wformat_nonliteral,
1487 			"format not a string literal, argument types not checked");
1488 	}
1489     }
1490 
1491   /* If there were extra arguments to the format, normally warn.  However,
1492      the standard does say extra arguments are ignored, so in the specific
1493      case where we have multiple leaves (conditional expressions or
1494      ngettext) allow extra arguments if at least one leaf didn't have extra
1495      arguments, but was otherwise OK (either non-literal or checked OK).
1496      If the format is an empty string, this should be counted similarly to the
1497      case of extra format arguments.  */
1498   if (res.number_extra_args > 0 && res.number_non_literal == 0
1499       && res.number_other == 0)
1500     {
1501       if (res.extra_arg_loc == UNKNOWN_LOCATION)
1502 	res.extra_arg_loc = loc;
1503       warning_at (res.extra_arg_loc, OPT_Wformat_extra_args,
1504 		  "too many arguments for format");
1505     }
1506   if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1507       && res.number_other == 0)
1508     warning_at (loc, OPT_Wformat_extra_args, "unused arguments in $-style format");
1509   if (res.number_empty > 0 && res.number_non_literal == 0
1510       && res.number_other == 0)
1511     warning_at (loc, OPT_Wformat_zero_length, "zero-length %s format string",
1512 	     format_types[info->format_type].name);
1513 
1514   if (res.number_wide > 0)
1515     warning_at (loc, OPT_Wformat_, "format is a wide character string");
1516 
1517   if (res.number_unterminated > 0)
1518     warning_at (loc, OPT_Wformat_, "unterminated format string");
1519 }
1520 
1521 /* Callback from check_function_arguments_recurse to check a
1522    format string.  FORMAT_TREE is the format parameter.  ARG_NUM
1523    is the number of the format argument.  CTX points to a
1524    format_check_context.  */
1525 
1526 static void
1527 check_format_arg (void *ctx, tree format_tree,
1528 		  unsigned HOST_WIDE_INT arg_num)
1529 {
1530   format_check_context *format_ctx = (format_check_context *) ctx;
1531   format_check_results *res = format_ctx->res;
1532   function_format_info *info = format_ctx->info;
1533   tree params = format_ctx->params;
1534   vec<location_t> *arglocs = format_ctx->arglocs;
1535 
1536   int format_length;
1537   HOST_WIDE_INT offset;
1538   const char *format_chars;
1539   tree array_size = 0;
1540   tree array_init;
1541 
1542   location_t fmt_param_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1543 
1544   /* Pull out a constant value if the front end didn't, and handle location
1545      wrappers.  */
1546   format_tree = fold_for_warn (format_tree);
1547   STRIP_NOPS (format_tree);
1548 
1549   if (integer_zerop (format_tree))
1550     {
1551       /* Skip to first argument to check, so we can see if this format
1552 	 has any arguments (it shouldn't).  */
1553       while (arg_num + 1 < info->first_arg_num)
1554 	{
1555 	  if (params == 0)
1556 	    return;
1557 	  params = TREE_CHAIN (params);
1558 	  ++arg_num;
1559 	}
1560 
1561       if (params == 0)
1562 	res->number_other++;
1563       else
1564 	{
1565 	  if (res->number_extra_args == 0)
1566 	    res->extra_arg_loc = EXPR_LOC_OR_LOC (TREE_VALUE (params),
1567 						  input_location);
1568 	  res->number_extra_args++;
1569 	}
1570       return;
1571     }
1572 
1573   offset = 0;
1574   if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1575     {
1576       tree arg0, arg1;
1577 
1578       arg0 = TREE_OPERAND (format_tree, 0);
1579       arg1 = TREE_OPERAND (format_tree, 1);
1580       STRIP_NOPS (arg0);
1581       STRIP_NOPS (arg1);
1582       if (TREE_CODE (arg1) == INTEGER_CST)
1583 	format_tree = arg0;
1584       else
1585 	{
1586 	  res->number_non_literal++;
1587 	  return;
1588 	}
1589       /* POINTER_PLUS_EXPR offsets are to be interpreted signed.  */
1590       if (!cst_and_fits_in_hwi (arg1))
1591 	{
1592 	  res->number_non_literal++;
1593 	  return;
1594 	}
1595       offset = int_cst_value (arg1);
1596     }
1597   if (TREE_CODE (format_tree) != ADDR_EXPR)
1598     {
1599       res->number_non_literal++;
1600       return;
1601     }
1602   res->format_string_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1603   format_tree = TREE_OPERAND (format_tree, 0);
1604   if (format_types[info->format_type].flags
1605       & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1606     {
1607       bool objc_str = (info->format_type == gcc_objc_string_format_type);
1608       /* We cannot examine this string here - but we can check that it is
1609 	 a valid type.  */
1610       if (TREE_CODE (format_tree) != CONST_DECL
1611 	  || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1612 		|| (*targetcm.string_object_ref_type_p)
1613 				     ((const_tree) TREE_TYPE (format_tree))))
1614 	{
1615 	  res->number_non_literal++;
1616 	  return;
1617 	}
1618       /* Skip to first argument to check.  */
1619       while (arg_num + 1 < info->first_arg_num)
1620 	{
1621 	  if (params == 0)
1622 	    return;
1623 	  params = TREE_CHAIN (params);
1624 	  ++arg_num;
1625 	}
1626       /* So, we have a valid literal string object and one or more params.
1627 	 We need to use an external helper to parse the string into format
1628 	 info.  For Objective-C variants we provide the resource within the
1629 	 objc tree, for target variants, via a hook.  */
1630       if (objc_str)
1631 	objc_check_format_arg (format_tree, params);
1632       else if (targetcm.check_string_object_format_arg)
1633 	(*targetcm.check_string_object_format_arg) (format_tree, params);
1634       /* Else we can't handle it and retire quietly.  */
1635       return;
1636     }
1637   if (TREE_CODE (format_tree) == ARRAY_REF
1638       && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1639       && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1640     format_tree = TREE_OPERAND (format_tree, 0);
1641   if (offset < 0)
1642     {
1643       res->number_non_literal++;
1644       return;
1645     }
1646   if (VAR_P (format_tree)
1647       && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1648       && (array_init = decl_constant_value (format_tree)) != format_tree
1649       && TREE_CODE (array_init) == STRING_CST)
1650     {
1651       /* Extract the string constant initializer.  Note that this may include
1652 	 a trailing NUL character that is not in the array (e.g.
1653 	 const char a[3] = "foo";).  */
1654       array_size = DECL_SIZE_UNIT (format_tree);
1655       format_tree = array_init;
1656     }
1657   if (TREE_CODE (format_tree) != STRING_CST)
1658     {
1659       res->number_non_literal++;
1660       return;
1661     }
1662   if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1663     {
1664       res->number_wide++;
1665       return;
1666     }
1667   format_chars = TREE_STRING_POINTER (format_tree);
1668   format_length = TREE_STRING_LENGTH (format_tree);
1669   if (array_size != 0)
1670     {
1671       /* Variable length arrays can't be initialized.  */
1672       gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1673 
1674       if (tree_fits_shwi_p (array_size))
1675 	{
1676 	  HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1677 	  if (array_size_value > 0
1678 	      && array_size_value == (int) array_size_value
1679 	      && format_length > array_size_value)
1680 	    format_length = array_size_value;
1681 	}
1682     }
1683   if (offset)
1684     {
1685       if (offset >= format_length)
1686 	{
1687 	  res->number_non_literal++;
1688 	  return;
1689 	}
1690       format_chars += offset;
1691       format_length -= offset;
1692     }
1693   if (format_length < 1 || format_chars[--format_length] != 0)
1694     {
1695       res->number_unterminated++;
1696       return;
1697     }
1698   if (format_length == 0)
1699     {
1700       res->number_empty++;
1701       return;
1702     }
1703 
1704   /* Skip to first argument to check.  */
1705   while (arg_num + 1 < info->first_arg_num)
1706     {
1707       if (params == 0)
1708 	return;
1709       params = TREE_CHAIN (params);
1710       ++arg_num;
1711     }
1712   /* Provisionally increment res->number_other; check_format_info_main
1713      will decrement it if it finds there are extra arguments, but this way
1714      need not adjust it for every return.  */
1715   res->number_other++;
1716   object_allocator <format_wanted_type> fwt_pool ("format_wanted_type pool");
1717   check_format_info_main (res, info, format_chars, fmt_param_loc, format_tree,
1718 			  format_length, params, arg_num, fwt_pool, arglocs);
1719 }
1720 
1721 /* Support class for argument_parser and check_format_info_main.
1722    Tracks any flag characters that have been applied to the
1723    current argument.  */
1724 
1725 class flag_chars_t
1726 {
1727  public:
1728   flag_chars_t ();
1729   bool has_char_p (char ch) const;
1730   void add_char (char ch);
1731   void validate (const format_kind_info *fki,
1732 		 const format_char_info *fci,
1733 		 const format_flag_spec *flag_specs,
1734 		 const char * const format_chars,
1735 		 tree format_string_cst,
1736 		 location_t format_string_loc,
1737 		 const char * const orig_format_chars,
1738 		 char format_char,
1739 		 bool quoted);
1740   int get_alloc_flag (const format_kind_info *fki);
1741   int assignment_suppression_p (const format_kind_info *fki);
1742 
1743  private:
1744   char m_flag_chars[256];
1745 };
1746 
1747 /* Support struct for argument_parser and check_format_info_main.
1748    Encapsulates any length modifier applied to the current argument.  */
1749 
1750 struct length_modifier
1751 {
1752   length_modifier ()
1753   : chars (NULL), val (FMT_LEN_none), std (STD_C89),
1754     scalar_identity_flag (0)
1755   {
1756   }
1757 
1758   length_modifier (const char *chars_,
1759 		   enum format_lengths val_,
1760 		   enum format_std_version std_,
1761 		   int scalar_identity_flag_)
1762   : chars (chars_), val (val_), std (std_),
1763     scalar_identity_flag (scalar_identity_flag_)
1764   {
1765   }
1766 
1767   const char *chars;
1768   enum format_lengths val;
1769   enum format_std_version std;
1770   int scalar_identity_flag;
1771 };
1772 
1773 /* Parsing one argument within a format string.  */
1774 
1775 class argument_parser
1776 {
1777  public:
1778   argument_parser (function_format_info *info, const char *&format_chars,
1779 		   tree format_string_cst,
1780 		   const char * const orig_format_chars,
1781 		   location_t format_string_loc, flag_chars_t &flag_chars,
1782 		   int &has_operand_number, tree first_fillin_param,
1783 		   object_allocator <format_wanted_type> &fwt_pool_,
1784 		   vec<location_t> *arglocs);
1785 
1786   bool read_any_dollar ();
1787 
1788   bool read_format_flags ();
1789 
1790   bool
1791   read_any_format_width (tree &params,
1792 			 unsigned HOST_WIDE_INT &arg_num);
1793 
1794   void
1795   read_any_format_left_precision ();
1796 
1797   bool
1798   read_any_format_precision (tree &params,
1799 			     unsigned HOST_WIDE_INT &arg_num);
1800 
1801   void handle_alloc_chars ();
1802 
1803   length_modifier read_any_length_modifier ();
1804 
1805   void read_any_other_modifier ();
1806 
1807   const format_char_info *find_format_char_info (char format_char);
1808 
1809   void
1810   validate_flag_pairs (const format_char_info *fci,
1811 		       char format_char);
1812 
1813   void
1814   give_y2k_warnings (const format_char_info *fci,
1815 		     char format_char);
1816 
1817   void parse_any_scan_set (const format_char_info *fci);
1818 
1819   bool handle_conversions (const format_char_info *fci,
1820 			   const length_modifier &len_modifier,
1821 			   tree &wanted_type,
1822 			   const char *&wanted_type_name,
1823 			   unsigned HOST_WIDE_INT &arg_num,
1824 			   tree &params,
1825 			   char format_char);
1826 
1827   bool
1828   check_argument_type (const format_char_info *fci,
1829 		       const length_modifier &len_modifier,
1830 		       tree &wanted_type,
1831 		       const char *&wanted_type_name,
1832 		       const bool suppressed,
1833 		       unsigned HOST_WIDE_INT &arg_num,
1834 		       tree &params,
1835 		       const int alloc_flag,
1836 		       const char * const format_start,
1837 		       const char * const type_start,
1838 		       location_t fmt_param_loc,
1839 		       char conversion_char);
1840 
1841  private:
1842   const function_format_info *const info;
1843   const format_kind_info * const fki;
1844   const format_flag_spec * const flag_specs;
1845   const char *start_of_this_format;
1846   const char *&format_chars;
1847   const tree format_string_cst;
1848   const char * const orig_format_chars;
1849   const location_t format_string_loc;
1850   object_allocator <format_wanted_type> &fwt_pool;
1851   flag_chars_t &flag_chars;
1852   int main_arg_num;
1853   tree main_arg_params;
1854   int &has_operand_number;
1855   const tree first_fillin_param;
1856   format_wanted_type width_wanted_type;
1857   format_wanted_type precision_wanted_type;
1858  public:
1859   format_wanted_type main_wanted_type;
1860  private:
1861   format_wanted_type *first_wanted_type;
1862   format_wanted_type *last_wanted_type;
1863   vec<location_t> *arglocs;
1864 };
1865 
1866 /* flag_chars_t's constructor.  */
1867 
1868 flag_chars_t::flag_chars_t ()
1869 {
1870   m_flag_chars[0] = 0;
1871 }
1872 
1873 /* Has CH been seen as a flag within the current argument?  */
1874 
1875 bool
1876 flag_chars_t::has_char_p (char ch) const
1877 {
1878   return strchr (m_flag_chars, ch) != 0;
1879 }
1880 
1881 /* Add CH to the flags seen within the current argument.  */
1882 
1883 void
1884 flag_chars_t::add_char (char ch)
1885 {
1886   int i = strlen (m_flag_chars);
1887   m_flag_chars[i++] = ch;
1888   m_flag_chars[i] = 0;
1889 }
1890 
1891 /* Validate the individual flags used, removing any that are invalid.  */
1892 
1893 void
1894 flag_chars_t::validate (const format_kind_info *fki,
1895 			const format_char_info *fci,
1896 			const format_flag_spec *flag_specs,
1897 			const char * const format_chars,
1898 			tree format_string_cst,
1899 			location_t format_string_loc,
1900 			const char * const orig_format_chars,
1901 			char format_char,
1902 			bool quoted)
1903 {
1904   int i;
1905   int d = 0;
1906   bool quotflag = false;
1907 
1908   for (i = 0; m_flag_chars[i] != 0; i++)
1909     {
1910       const format_flag_spec *s = get_flag_spec (flag_specs,
1911 						 m_flag_chars[i], NULL);
1912       m_flag_chars[i - d] = m_flag_chars[i];
1913       if (m_flag_chars[i] == fki->length_code_char)
1914 	continue;
1915 
1916       /* Remember if a quoting flag is seen.  */
1917       quotflag |= s->quoting;
1918 
1919       if (strchr (fci->flag_chars, m_flag_chars[i]) == 0)
1920 	{
1921 	  format_warning_at_char (format_string_loc, format_string_cst,
1922 				  format_chars - orig_format_chars,
1923 				  OPT_Wformat_,
1924 				  "%s used with %<%%%c%> %s format",
1925 				  _(s->name), format_char, fki->name);
1926 	  d++;
1927 	  continue;
1928 	}
1929       if (pedantic)
1930 	{
1931 	  const format_flag_spec *t;
1932 	  if (ADJ_STD (s->std) > C_STD_VER)
1933 	    warning_at (format_string_loc, OPT_Wformat_,
1934 			"%s does not support %s",
1935 			C_STD_NAME (s->std), _(s->long_name));
1936 	  t = get_flag_spec (flag_specs, m_flag_chars[i], fci->flags2);
1937 	  if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1938 	    {
1939 	      const char *long_name = (t->long_name != NULL
1940 				       ? t->long_name
1941 				       : s->long_name);
1942 	      if (ADJ_STD (t->std) > C_STD_VER)
1943 		warning_at (format_string_loc, OPT_Wformat_,
1944 			    "%s does not support %s with"
1945 			    " the %<%%%c%> %s format",
1946 			    C_STD_NAME (t->std), _(long_name),
1947 			    format_char, fki->name);
1948 	    }
1949 	}
1950 
1951       /* Detect quoting directives used within a quoted sequence, such
1952 	 as GCC's "%<...%qE".  */
1953       if (quoted && s->quoting)
1954 	{
1955 	  format_warning_at_char (format_string_loc, format_string_cst,
1956 				  format_chars - orig_format_chars - 1,
1957 				  OPT_Wformat_,
1958 				  "%s used within a quoted sequence",
1959 				  _(s->name));
1960 	}
1961     }
1962   m_flag_chars[i - d] = 0;
1963 
1964   if (!quoted
1965       && !quotflag
1966       && strchr (fci->flags2, '\''))
1967     {
1968       format_warning_at_char (format_string_loc, format_string_cst,
1969 			      format_chars - orig_format_chars,
1970 			      OPT_Wformat_,
1971 			      "%qc conversion used unquoted",
1972 			      format_char);
1973     }
1974 }
1975 
1976 /* Determine if an assignment-allocation has been set, requiring
1977    an extra char ** for writing back a dynamically-allocated char *.
1978    This is for handling the optional 'm' character in scanf.  */
1979 
1980 int
1981 flag_chars_t::get_alloc_flag (const format_kind_info *fki)
1982 {
1983   if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1984       && has_char_p ('a'))
1985     return 1;
1986   if (fki->alloc_char && has_char_p (fki->alloc_char))
1987     return 1;
1988   return 0;
1989 }
1990 
1991 /* Determine if an assignment-suppression character was seen.
1992    ('*' in scanf, for discarding the converted input).  */
1993 
1994 int
1995 flag_chars_t::assignment_suppression_p (const format_kind_info *fki)
1996 {
1997   if (fki->suppression_char
1998       && has_char_p (fki->suppression_char))
1999     return 1;
2000   return 0;
2001 }
2002 
2003 /* Constructor for argument_parser.  Initialize for parsing one
2004    argument within a format string.  */
2005 
2006 argument_parser::
2007 argument_parser (function_format_info *info_, const char *&format_chars_,
2008 		 tree format_string_cst_,
2009 		 const char * const orig_format_chars_,
2010 		 location_t format_string_loc_,
2011 		 flag_chars_t &flag_chars_,
2012 		 int &has_operand_number_,
2013 		 tree first_fillin_param_,
2014 		 object_allocator <format_wanted_type> &fwt_pool_,
2015 		 vec<location_t> *arglocs_)
2016 : info (info_),
2017   fki (&format_types[info->format_type]),
2018   flag_specs (fki->flag_specs),
2019   start_of_this_format (format_chars_),
2020   format_chars (format_chars_),
2021   format_string_cst (format_string_cst_),
2022   orig_format_chars (orig_format_chars_),
2023   format_string_loc (format_string_loc_),
2024   fwt_pool (fwt_pool_),
2025   flag_chars (flag_chars_),
2026   main_arg_num (0),
2027   main_arg_params (NULL),
2028   has_operand_number (has_operand_number_),
2029   first_fillin_param (first_fillin_param_),
2030   first_wanted_type (NULL),
2031   last_wanted_type (NULL),
2032   arglocs (arglocs_)
2033 {
2034 }
2035 
2036 /* Handle dollars at the start of format arguments, setting up main_arg_params
2037    and main_arg_num.
2038 
2039    Return true if format parsing is to continue, false otherwise.  */
2040 
2041 bool
2042 argument_parser::read_any_dollar ()
2043 {
2044   if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
2045     {
2046       /* Possibly read a $ operand number at the start of the format.
2047 	 If one was previously used, one is required here.  If one
2048 	 is not used here, we can't immediately conclude this is a
2049 	 format without them, since it could be printf %m or scanf %*.  */
2050       int opnum;
2051       opnum = maybe_read_dollar_number (&format_chars, 0,
2052 					first_fillin_param,
2053 					&main_arg_params, fki);
2054       if (opnum == -1)
2055 	return false;
2056       else if (opnum > 0)
2057 	{
2058 	  has_operand_number = 1;
2059 	  main_arg_num = opnum + info->first_arg_num - 1;
2060 	}
2061     }
2062   else if (fki->flags & FMT_FLAG_USE_DOLLAR)
2063     {
2064       if (avoid_dollar_number (format_chars))
2065 	return false;
2066     }
2067   return true;
2068 }
2069 
2070 /* Read any format flags, but do not yet validate them beyond removing
2071    duplicates, since in general validation depends on the rest of
2072    the format.
2073 
2074    Return true if format parsing is to continue, false otherwise.  */
2075 
2076 bool
2077 argument_parser::read_format_flags ()
2078 {
2079   while (*format_chars != 0
2080 	 && strchr (fki->flag_chars, *format_chars) != 0)
2081     {
2082       const format_flag_spec *s = get_flag_spec (flag_specs,
2083 						 *format_chars, NULL);
2084       if (flag_chars.has_char_p (*format_chars))
2085 	{
2086 	  format_warning_at_char (format_string_loc, format_string_cst,
2087 				  format_chars + 1 - orig_format_chars,
2088 				  OPT_Wformat_,
2089 				  "repeated %s in format", _(s->name));
2090 	}
2091       else
2092 	flag_chars.add_char (*format_chars);
2093 
2094       if (s->skip_next_char)
2095 	{
2096 	  ++format_chars;
2097 	  if (*format_chars == 0)
2098 	    {
2099 	      warning_at (format_string_loc, OPT_Wformat_,
2100 			  "missing fill character at end of strfmon format");
2101 	      return false;
2102 	    }
2103 	}
2104       ++format_chars;
2105     }
2106 
2107   return true;
2108 }
2109 
2110 /* Read any format width, possibly * or *m$.
2111 
2112    Return true if format parsing is to continue, false otherwise.  */
2113 
2114 bool
2115 argument_parser::
2116 read_any_format_width (tree &params,
2117 		       unsigned HOST_WIDE_INT &arg_num)
2118 {
2119   if (!fki->width_char)
2120     return true;
2121 
2122   if (fki->width_type != NULL && *format_chars == '*')
2123     {
2124       flag_chars.add_char (fki->width_char);
2125       /* "...a field width...may be indicated by an asterisk.
2126 	 In this case, an int argument supplies the field width..."  */
2127       ++format_chars;
2128       if (has_operand_number != 0)
2129 	{
2130 	  int opnum;
2131 	  opnum = maybe_read_dollar_number (&format_chars,
2132 					    has_operand_number == 1,
2133 					    first_fillin_param,
2134 					    &params, fki);
2135 	  if (opnum == -1)
2136 	    return false;
2137 	  else if (opnum > 0)
2138 	    {
2139 	      has_operand_number = 1;
2140 	      arg_num = opnum + info->first_arg_num - 1;
2141 	    }
2142 	  else
2143 	    has_operand_number = 0;
2144 	}
2145       else
2146 	{
2147 	  if (avoid_dollar_number (format_chars))
2148 	    return false;
2149 	}
2150       if (info->first_arg_num != 0)
2151 	{
2152 	  tree cur_param;
2153 	  if (params == 0)
2154 	    cur_param = NULL;
2155 	  else
2156 	    {
2157 	      cur_param = TREE_VALUE (params);
2158 	      if (has_operand_number <= 0)
2159 		{
2160 		  params = TREE_CHAIN (params);
2161 		  ++arg_num;
2162 		}
2163 	    }
2164 	  width_wanted_type.wanted_type = *fki->width_type;
2165 	  width_wanted_type.wanted_type_name = NULL;
2166 	  width_wanted_type.pointer_count = 0;
2167 	  width_wanted_type.char_lenient_flag = 0;
2168 	  width_wanted_type.scalar_identity_flag = 0;
2169 	  width_wanted_type.writing_in_flag = 0;
2170 	  width_wanted_type.reading_from_flag = 0;
2171 	  width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
2172 	  width_wanted_type.format_start = format_chars - 1;
2173 	  width_wanted_type.format_length = 1;
2174 	  width_wanted_type.param = cur_param;
2175 	  width_wanted_type.arg_num = arg_num;
2176 	  width_wanted_type.offset_loc =
2177 	    format_chars - orig_format_chars;
2178 	  width_wanted_type.next = NULL;
2179 	  if (last_wanted_type != 0)
2180 	    last_wanted_type->next = &width_wanted_type;
2181 	  if (first_wanted_type == 0)
2182 	    first_wanted_type = &width_wanted_type;
2183 	  last_wanted_type = &width_wanted_type;
2184 	}
2185     }
2186   else
2187     {
2188       /* Possibly read a numeric width.  If the width is zero,
2189 	 we complain if appropriate.  */
2190       int non_zero_width_char = FALSE;
2191       int found_width = FALSE;
2192       while (ISDIGIT (*format_chars))
2193 	{
2194 	  found_width = TRUE;
2195 	  if (*format_chars != '0')
2196 	    non_zero_width_char = TRUE;
2197 	  ++format_chars;
2198 	}
2199       if (found_width && !non_zero_width_char &&
2200 	  (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
2201 	warning_at (format_string_loc, OPT_Wformat_,
2202 		    "zero width in %s format", fki->name);
2203       if (found_width)
2204 	flag_chars.add_char (fki->width_char);
2205     }
2206 
2207   return true;
2208 }
2209 
2210 /* Read any format left precision (must be a number, not *).  */
2211 void
2212 argument_parser::read_any_format_left_precision ()
2213 {
2214   if (fki->left_precision_char == 0)
2215     return;
2216   if (*format_chars != '#')
2217     return;
2218 
2219   ++format_chars;
2220   flag_chars.add_char (fki->left_precision_char);
2221   if (!ISDIGIT (*format_chars))
2222     format_warning_at_char (format_string_loc, format_string_cst,
2223 			    format_chars - orig_format_chars,
2224 			    OPT_Wformat_,
2225 			    "empty left precision in %s format", fki->name);
2226   while (ISDIGIT (*format_chars))
2227     ++format_chars;
2228 }
2229 
2230 /* Read any format precision, possibly * or *m$.
2231 
2232    Return true if format parsing is to continue, false otherwise.  */
2233 
2234 bool
2235 argument_parser::
2236 read_any_format_precision (tree &params,
2237 			   unsigned HOST_WIDE_INT &arg_num)
2238 {
2239   if (fki->precision_char == 0)
2240     return true;
2241   if (*format_chars != '.')
2242     return true;
2243 
2244   ++format_chars;
2245   flag_chars.add_char (fki->precision_char);
2246   if (fki->precision_type != NULL && *format_chars == '*')
2247     {
2248       /* "...a...precision...may be indicated by an asterisk.
2249 	 In this case, an int argument supplies the...precision."  */
2250       ++format_chars;
2251       if (has_operand_number != 0)
2252 	{
2253 	  int opnum;
2254 	  opnum = maybe_read_dollar_number (&format_chars,
2255 					    has_operand_number == 1,
2256 					    first_fillin_param,
2257 					    &params, fki);
2258 	  if (opnum == -1)
2259 	    return false;
2260 	  else if (opnum > 0)
2261 	    {
2262 	      has_operand_number = 1;
2263 	      arg_num = opnum + info->first_arg_num - 1;
2264 	    }
2265 	  else
2266 	    has_operand_number = 0;
2267 	}
2268       else
2269 	{
2270 	  if (avoid_dollar_number (format_chars))
2271 	    return false;
2272 	}
2273       if (info->first_arg_num != 0)
2274 	{
2275 	  tree cur_param;
2276 	  if (params == 0)
2277 	    cur_param = NULL;
2278 	  else
2279 	    {
2280 	      cur_param = TREE_VALUE (params);
2281 	      if (has_operand_number <= 0)
2282 		{
2283 		  params = TREE_CHAIN (params);
2284 		  ++arg_num;
2285 		}
2286 	    }
2287 	  precision_wanted_type.wanted_type = *fki->precision_type;
2288 	  precision_wanted_type.wanted_type_name = NULL;
2289 	  precision_wanted_type.pointer_count = 0;
2290 	  precision_wanted_type.char_lenient_flag = 0;
2291 	  precision_wanted_type.scalar_identity_flag = 0;
2292 	  precision_wanted_type.writing_in_flag = 0;
2293 	  precision_wanted_type.reading_from_flag = 0;
2294 	  precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
2295 	  precision_wanted_type.param = cur_param;
2296 	  precision_wanted_type.format_start = format_chars - 2;
2297 	  precision_wanted_type.format_length = 2;
2298 	  precision_wanted_type.arg_num = arg_num;
2299 	  precision_wanted_type.offset_loc =
2300 	    format_chars - orig_format_chars;
2301 	  precision_wanted_type.next = NULL;
2302 	  if (last_wanted_type != 0)
2303 	    last_wanted_type->next = &precision_wanted_type;
2304 	  if (first_wanted_type == 0)
2305 	    first_wanted_type = &precision_wanted_type;
2306 	  last_wanted_type = &precision_wanted_type;
2307 	}
2308     }
2309   else
2310     {
2311       if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
2312 	  && !ISDIGIT (*format_chars))
2313 	format_warning_at_char (format_string_loc, format_string_cst,
2314 				format_chars - orig_format_chars,
2315 				OPT_Wformat_,
2316 				"empty precision in %s format", fki->name);
2317       while (ISDIGIT (*format_chars))
2318 	++format_chars;
2319     }
2320 
2321   return true;
2322 }
2323 
2324 /* Parse any assignment-allocation flags, which request an extra
2325    char ** for writing back a dynamically-allocated char *.
2326    This is for handling the optional 'm' character in scanf,
2327    and, before C99, 'a' (for compatibility with a non-standard
2328    GNU libc extension).  */
2329 
2330 void
2331 argument_parser::handle_alloc_chars ()
2332 {
2333   if (fki->alloc_char && fki->alloc_char == *format_chars)
2334     {
2335       flag_chars.add_char (fki->alloc_char);
2336       format_chars++;
2337     }
2338 
2339   /* Handle the scanf allocation kludge.  */
2340   if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2341     {
2342       if (*format_chars == 'a' && !flag_isoc99)
2343 	{
2344 	  if (format_chars[1] == 's' || format_chars[1] == 'S'
2345 	      || format_chars[1] == '[')
2346 	    {
2347 	      /* 'a' is used as a flag.  */
2348 	      flag_chars.add_char ('a');
2349 	      format_chars++;
2350 	    }
2351 	}
2352     }
2353 }
2354 
2355 /* Look for length modifiers within the current format argument,
2356    returning a length_modifier instance describing it (or the
2357    default if one is not found).
2358 
2359    Issue warnings about non-standard modifiers.  */
2360 
2361 length_modifier
2362 argument_parser::read_any_length_modifier ()
2363 {
2364   length_modifier result;
2365 
2366   const format_length_info *fli = fki->length_char_specs;
2367   if (!fli)
2368     return result;
2369 
2370   while (fli->name != 0
2371 	 && strncmp (fli->name, format_chars, strlen (fli->name)))
2372     fli++;
2373   if (fli->name != 0)
2374     {
2375       format_chars += strlen (fli->name);
2376       if (fli->double_name != 0 && fli->name[0] == *format_chars)
2377 	{
2378 	  format_chars++;
2379 	  result = length_modifier (fli->double_name, fli->double_index,
2380 				    fli->double_std, 0);
2381 	}
2382       else
2383 	{
2384 	  result = length_modifier (fli->name, fli->index, fli->std,
2385 				    fli->scalar_identity_flag);
2386 	}
2387       flag_chars.add_char (fki->length_code_char);
2388     }
2389   if (pedantic)
2390     {
2391       /* Warn if the length modifier is non-standard.  */
2392       if (ADJ_STD (result.std) > C_STD_VER)
2393 	warning_at (format_string_loc, OPT_Wformat_,
2394 		    "%s does not support the %qs %s length modifier",
2395 		    C_STD_NAME (result.std), result.chars,
2396 		    fki->name);
2397     }
2398 
2399   return result;
2400 }
2401 
2402 /* Read any other modifier (strftime E/O).  */
2403 
2404 void
2405 argument_parser::read_any_other_modifier ()
2406 {
2407   if (fki->modifier_chars == NULL)
2408     return;
2409 
2410   while (*format_chars != 0
2411 	 && strchr (fki->modifier_chars, *format_chars) != 0)
2412     {
2413       if (flag_chars.has_char_p (*format_chars))
2414 	{
2415 	  const format_flag_spec *s = get_flag_spec (flag_specs,
2416 						     *format_chars, NULL);
2417 	  format_warning_at_char (format_string_loc, format_string_cst,
2418 				  format_chars - orig_format_chars,
2419 				  OPT_Wformat_,
2420 				  "repeated %s in format", _(s->name));
2421 	}
2422       else
2423 	flag_chars.add_char (*format_chars);
2424       ++format_chars;
2425     }
2426 }
2427 
2428 /* Return the format_char_info corresponding to FORMAT_CHAR,
2429    potentially issuing a warning if the format char is
2430    not supported in the C standard version we are checking
2431    against.
2432 
2433    Issue a warning and return NULL if it is not found.
2434 
2435    Issue warnings about non-standard modifiers.  */
2436 
2437 const format_char_info *
2438 argument_parser::find_format_char_info (char format_char)
2439 {
2440   const format_char_info *fci = fki->conversion_specs;
2441 
2442   while (fci->format_chars != 0
2443 	 && strchr (fci->format_chars, format_char) == 0)
2444     ++fci;
2445   if (fci->format_chars == 0)
2446     {
2447       format_warning_at_char (format_string_loc, format_string_cst,
2448 			      format_chars - orig_format_chars,
2449 			      OPT_Wformat_,
2450 			      "unknown conversion type character"
2451 			      " %qc in format",
2452 			      format_char);
2453       return NULL;
2454     }
2455 
2456   if (pedantic)
2457     {
2458       if (ADJ_STD (fci->std) > C_STD_VER)
2459 	format_warning_at_char (format_string_loc, format_string_cst,
2460 				format_chars - orig_format_chars,
2461 				OPT_Wformat_,
2462 				"%s does not support the %<%%%c%> %s format",
2463 				C_STD_NAME (fci->std), format_char, fki->name);
2464     }
2465 
2466   return fci;
2467 }
2468 
2469 /* Validate the pairs of flags used.
2470    Issue warnings about incompatible combinations of flags.  */
2471 
2472 void
2473 argument_parser::validate_flag_pairs (const format_char_info *fci,
2474 				      char format_char)
2475 {
2476   const format_flag_pair * const bad_flag_pairs = fki->bad_flag_pairs;
2477 
2478   for (int i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2479     {
2480       const format_flag_spec *s, *t;
2481       if (!flag_chars.has_char_p (bad_flag_pairs[i].flag_char1))
2482 	continue;
2483       if (!flag_chars.has_char_p (bad_flag_pairs[i].flag_char2))
2484 	continue;
2485       if (bad_flag_pairs[i].predicate != 0
2486 	  && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2487 	continue;
2488       s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2489       t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2490       if (bad_flag_pairs[i].ignored)
2491 	{
2492 	  if (bad_flag_pairs[i].predicate != 0)
2493 	    warning_at (format_string_loc, OPT_Wformat_,
2494 			"%s ignored with %s and %<%%%c%> %s format",
2495 			_(s->name), _(t->name), format_char,
2496 			fki->name);
2497 	  else
2498 	    warning_at (format_string_loc, OPT_Wformat_,
2499 			"%s ignored with %s in %s format",
2500 			_(s->name), _(t->name), fki->name);
2501 	}
2502       else
2503 	{
2504 	  if (bad_flag_pairs[i].predicate != 0)
2505 	    warning_at (format_string_loc, OPT_Wformat_,
2506 			"use of %s and %s together with %<%%%c%> %s format",
2507 			_(s->name), _(t->name), format_char,
2508 			fki->name);
2509 	  else
2510 	    warning_at (format_string_loc, OPT_Wformat_,
2511 			"use of %s and %s together in %s format",
2512 			_(s->name), _(t->name), fki->name);
2513 	}
2514     }
2515 }
2516 
2517 /* Give Y2K warnings.  */
2518 
2519 void
2520 argument_parser::give_y2k_warnings (const format_char_info *fci,
2521 				    char format_char)
2522 {
2523   if (!warn_format_y2k)
2524     return;
2525 
2526   int y2k_level = 0;
2527   if (strchr (fci->flags2, '4') != 0)
2528     if (flag_chars.has_char_p ('E'))
2529       y2k_level = 3;
2530     else
2531       y2k_level = 2;
2532   else if (strchr (fci->flags2, '3') != 0)
2533     y2k_level = 3;
2534   else if (strchr (fci->flags2, '2') != 0)
2535     y2k_level = 2;
2536   if (y2k_level == 3)
2537     warning_at (format_string_loc, OPT_Wformat_y2k,
2538 		"%<%%%c%> yields only last 2 digits of "
2539 		"year in some locales", format_char);
2540   else if (y2k_level == 2)
2541     warning_at (format_string_loc, OPT_Wformat_y2k,
2542 		"%<%%%c%> yields only last 2 digits of year",
2543 		format_char);
2544 }
2545 
2546 /* Parse any "scan sets" enclosed in square brackets, e.g.
2547    for scanf-style calls.  */
2548 
2549 void
2550 argument_parser::parse_any_scan_set (const format_char_info *fci)
2551 {
2552   if (strchr (fci->flags2, '[') == NULL)
2553     return;
2554 
2555   /* Skip over scan set, in case it happens to have '%' in it.  */
2556   if (*format_chars == '^')
2557     ++format_chars;
2558   /* Find closing bracket; if one is hit immediately, then
2559      it's part of the scan set rather than a terminator.  */
2560   if (*format_chars == ']')
2561     ++format_chars;
2562   while (*format_chars && *format_chars != ']')
2563     ++format_chars;
2564   if (*format_chars != ']')
2565     /* The end of the format string was reached.  */
2566     format_warning_at_char (format_string_loc, format_string_cst,
2567 			    format_chars - orig_format_chars,
2568 			    OPT_Wformat_,
2569 			    "no closing %<]%> for %<%%[%> format");
2570 }
2571 
2572 /* Return true if this argument is to be continued to be parsed,
2573    false to skip to next argument.  */
2574 
2575 bool
2576 argument_parser::handle_conversions (const format_char_info *fci,
2577 				     const length_modifier &len_modifier,
2578 				     tree &wanted_type,
2579 				     const char *&wanted_type_name,
2580 				     unsigned HOST_WIDE_INT &arg_num,
2581 				     tree &params,
2582 				     char format_char)
2583 {
2584   enum format_std_version wanted_type_std;
2585 
2586   if (!(fki->flags & (int) FMT_FLAG_ARG_CONVERT))
2587     return true;
2588 
2589   wanted_type = (fci->types[len_modifier.val].type
2590 		 ? *fci->types[len_modifier.val].type : 0);
2591   wanted_type_name = fci->types[len_modifier.val].name;
2592   wanted_type_std = fci->types[len_modifier.val].std;
2593   if (wanted_type == 0)
2594     {
2595       format_warning_at_char (format_string_loc, format_string_cst,
2596 			      format_chars - orig_format_chars,
2597 			      OPT_Wformat_,
2598 			      "use of %qs length modifier with %qc type"
2599 			      " character has either no effect"
2600 			      " or undefined behavior",
2601 			      len_modifier.chars, format_char);
2602       /* Heuristic: skip one argument when an invalid length/type
2603 	 combination is encountered.  */
2604       arg_num++;
2605       if (params != 0)
2606 	params = TREE_CHAIN (params);
2607       return false;
2608     }
2609   else if (pedantic
2610 	   /* Warn if non-standard, provided it is more non-standard
2611 	      than the length and type characters that may already
2612 	      have been warned for.  */
2613 	   && ADJ_STD (wanted_type_std) > ADJ_STD (len_modifier.std)
2614 	   && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2615     {
2616       if (ADJ_STD (wanted_type_std) > C_STD_VER)
2617 	format_warning_at_char (format_string_loc, format_string_cst,
2618 				format_chars - orig_format_chars,
2619 				OPT_Wformat_,
2620 				"%s does not support the %<%%%s%c%> %s format",
2621 				C_STD_NAME (wanted_type_std),
2622 				len_modifier.chars,
2623 				format_char, fki->name);
2624     }
2625 
2626   return true;
2627 }
2628 
2629 /* Check type of argument against desired type.
2630 
2631    Return true if format parsing is to continue, false otherwise.  */
2632 
2633 bool
2634 argument_parser::
2635 check_argument_type (const format_char_info *fci,
2636 		     const length_modifier &len_modifier,
2637 		     tree &wanted_type,
2638 		     const char *&wanted_type_name,
2639 		     const bool suppressed,
2640 		     unsigned HOST_WIDE_INT &arg_num,
2641 		     tree &params,
2642 		     const int alloc_flag,
2643 		     const char * const format_start,
2644 		     const char * const type_start,
2645 		     location_t fmt_param_loc,
2646 		     char conversion_char)
2647 {
2648   if (info->first_arg_num == 0)
2649     return true;
2650 
2651   if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2652       || suppressed)
2653     {
2654       if (main_arg_num != 0)
2655 	{
2656 	  if (suppressed)
2657 	    warning_at (format_string_loc, OPT_Wformat_,
2658 			"operand number specified with "
2659 			"suppressed assignment");
2660 	  else
2661 	    warning_at (format_string_loc, OPT_Wformat_,
2662 			"operand number specified for format "
2663 			"taking no argument");
2664 	}
2665     }
2666   else
2667     {
2668       format_wanted_type *wanted_type_ptr;
2669 
2670       if (main_arg_num != 0)
2671 	{
2672 	  arg_num = main_arg_num;
2673 	  params = main_arg_params;
2674 	}
2675       else
2676 	{
2677 	  ++arg_num;
2678 	  if (has_operand_number > 0)
2679 	    {
2680 	      warning_at (format_string_loc, OPT_Wformat_,
2681 			  "missing $ operand number in format");
2682 	      return false;
2683 	    }
2684 	  else
2685 	    has_operand_number = 0;
2686 	}
2687 
2688       wanted_type_ptr = &main_wanted_type;
2689       while (fci)
2690 	{
2691 	  tree cur_param;
2692 	  if (params == 0)
2693 	    cur_param = NULL;
2694 	  else
2695 	    {
2696 	      cur_param = TREE_VALUE (params);
2697 	      params = TREE_CHAIN (params);
2698 	    }
2699 
2700 	  wanted_type_ptr->wanted_type = wanted_type;
2701 	  wanted_type_ptr->wanted_type_name = wanted_type_name;
2702 	  wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2703 	  wanted_type_ptr->char_lenient_flag = 0;
2704 	  if (strchr (fci->flags2, 'c') != 0)
2705 	    wanted_type_ptr->char_lenient_flag = 1;
2706 	  wanted_type_ptr->scalar_identity_flag = 0;
2707 	  if (len_modifier.scalar_identity_flag)
2708 	    wanted_type_ptr->scalar_identity_flag = 1;
2709 	  wanted_type_ptr->writing_in_flag = 0;
2710 	  wanted_type_ptr->reading_from_flag = 0;
2711 	  if (alloc_flag)
2712 	    wanted_type_ptr->writing_in_flag = 1;
2713 	  else
2714 	    {
2715 	      if (strchr (fci->flags2, 'W') != 0)
2716 		wanted_type_ptr->writing_in_flag = 1;
2717 	      if (strchr (fci->flags2, 'R') != 0)
2718 		wanted_type_ptr->reading_from_flag = 1;
2719 	    }
2720 	  wanted_type_ptr->kind = CF_KIND_FORMAT;
2721 	  wanted_type_ptr->param = cur_param;
2722 	  wanted_type_ptr->arg_num = arg_num;
2723 	  wanted_type_ptr->format_start = format_start;
2724 	  wanted_type_ptr->format_length = format_chars - format_start;
2725 	  wanted_type_ptr->offset_loc = format_chars - orig_format_chars;
2726 	  wanted_type_ptr->next = NULL;
2727 	  if (last_wanted_type != 0)
2728 	    last_wanted_type->next = wanted_type_ptr;
2729 	  if (first_wanted_type == 0)
2730 	    first_wanted_type = wanted_type_ptr;
2731 	  last_wanted_type = wanted_type_ptr;
2732 
2733 	  fci = fci->chain;
2734 	  if (fci)
2735 	    {
2736 	      wanted_type_ptr = fwt_pool.allocate ();
2737 	      arg_num++;
2738 	      wanted_type = *fci->types[len_modifier.val].type;
2739 	      wanted_type_name = fci->types[len_modifier.val].name;
2740 	    }
2741 	}
2742     }
2743 
2744   if (first_wanted_type != 0)
2745     {
2746       ptrdiff_t offset_to_format_start = (start_of_this_format - 1) - orig_format_chars;
2747       ptrdiff_t offset_to_format_end = (format_chars - 1) - orig_format_chars;
2748       /* By default, use the end of the range for the caret location.  */
2749       substring_loc fmt_loc (fmt_param_loc, TREE_TYPE (format_string_cst),
2750 			     offset_to_format_end,
2751 			     offset_to_format_start, offset_to_format_end);
2752       ptrdiff_t offset_to_type_start = type_start - orig_format_chars;
2753       check_format_types (fmt_loc, first_wanted_type, fki,
2754 			  offset_to_type_start,
2755 			  conversion_char, arglocs);
2756     }
2757 
2758   return true;
2759 }
2760 
2761 /* Do the main part of checking a call to a format function.  FORMAT_CHARS
2762    is the NUL-terminated format string (which at this point may contain
2763    internal NUL characters); FORMAT_LENGTH is its length (excluding the
2764    terminating NUL character).  ARG_NUM is one less than the number of
2765    the first format argument to check; PARAMS points to that format
2766    argument in the list of arguments.  */
2767 
2768 static void
2769 check_format_info_main (format_check_results *res,
2770 			function_format_info *info, const char *format_chars,
2771 			location_t fmt_param_loc, tree format_string_cst,
2772 			int format_length, tree params,
2773 			unsigned HOST_WIDE_INT arg_num,
2774 			object_allocator <format_wanted_type> &fwt_pool,
2775 			vec<location_t> *arglocs)
2776 {
2777   const char * const orig_format_chars = format_chars;
2778   const tree first_fillin_param = params;
2779 
2780   const format_kind_info * const fki = &format_types[info->format_type];
2781   const format_flag_spec * const flag_specs = fki->flag_specs;
2782   const location_t format_string_loc = res->format_string_loc;
2783 
2784   /* -1 if no conversions taking an operand have been found; 0 if one has
2785      and it didn't use $; 1 if $ formats are in use.  */
2786   int has_operand_number = -1;
2787 
2788   /* Vector of pointers to opening quoting directives (like GCC "%<").  */
2789   auto_vec<const char*> quotdirs;
2790 
2791   /* Pointers to the most recent color directives (like GCC's "%r or %R").
2792      A starting color directive much be terminated before the end of
2793      the format string.  A terminating directive makes no sense without
2794      a prior starting directive.  */
2795   const char *color_begin = NULL;
2796   const char *color_end = NULL;
2797 
2798   init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2799 
2800   while (*format_chars != 0)
2801     {
2802       if (*format_chars++ != '%')
2803 	continue;
2804       if (*format_chars == 0)
2805 	{
2806 	  format_warning_at_char (format_string_loc, format_string_cst,
2807 				  format_chars - orig_format_chars,
2808 				  OPT_Wformat_,
2809 				  "spurious trailing %<%%%> in format");
2810 	  continue;
2811 	}
2812       if (*format_chars == '%')
2813 	{
2814 	  ++format_chars;
2815 	  continue;
2816 	}
2817 
2818       flag_chars_t flag_chars;
2819       argument_parser arg_parser (info, format_chars, format_string_cst,
2820 				  orig_format_chars, format_string_loc,
2821 				  flag_chars, has_operand_number,
2822 				  first_fillin_param, fwt_pool, arglocs);
2823 
2824       if (!arg_parser.read_any_dollar ())
2825 	return;
2826 
2827       if (!arg_parser.read_format_flags ())
2828 	return;
2829 
2830       /* Read any format width, possibly * or *m$.  */
2831       if (!arg_parser.read_any_format_width (params, arg_num))
2832 	return;
2833 
2834       /* Read any format left precision (must be a number, not *).  */
2835       arg_parser.read_any_format_left_precision ();
2836 
2837       /* Read any format precision, possibly * or *m$.  */
2838       if (!arg_parser.read_any_format_precision (params, arg_num))
2839 	return;
2840 
2841       const char *format_start = format_chars;
2842 
2843       arg_parser.handle_alloc_chars ();
2844 
2845       /* The rest of the conversion specification is the length modifier
2846 	 (if any), and the conversion specifier, so this is where the
2847 	 type information starts.  If we need to issue a suggestion
2848 	 about a type mismatch, then we should preserve everything up
2849 	 to here. */
2850       const char *type_start = format_chars;
2851 
2852       /* Read any length modifier, if this kind of format has them.  */
2853       const length_modifier len_modifier
2854 	= arg_parser.read_any_length_modifier ();
2855 
2856       /* Read any modifier (strftime E/O).  */
2857       arg_parser.read_any_other_modifier ();
2858 
2859       char format_char = *format_chars;
2860       if (format_char == 0
2861 	  || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2862 	      && format_char == '%'))
2863 	{
2864 	  format_warning_at_char (format_string_loc, format_string_cst,
2865 			     format_chars - orig_format_chars,
2866 			     OPT_Wformat_,
2867 			     "conversion lacks type at end of format");
2868 	  continue;
2869 	}
2870       format_chars++;
2871 
2872       const format_char_info * const fci
2873 	= arg_parser.find_format_char_info (format_char);
2874       if (!fci)
2875 	continue;
2876 
2877       flag_chars.validate (fki, fci, flag_specs, format_chars,
2878 			   format_string_cst,
2879 			   format_string_loc, orig_format_chars, format_char,
2880 			   quotdirs.length () > 0);
2881 
2882       const int alloc_flag = flag_chars.get_alloc_flag (fki);
2883       const bool suppressed = flag_chars.assignment_suppression_p (fki);
2884 
2885       /* Diagnose nested or unmatched quoting directives such as GCC's
2886 	 "%<...%<" and "%>...%>".  */
2887       bool quot_begin_p = strchr (fci->flags2, '<');
2888       bool quot_end_p = strchr (fci->flags2, '>');
2889 
2890       if (quot_begin_p && !quot_end_p)
2891 	{
2892 	  if (quotdirs.length ())
2893 	    format_warning_at_char (format_string_loc, format_string_cst,
2894 				    format_chars - orig_format_chars,
2895 				    OPT_Wformat_,
2896 				    "nested quoting directive");
2897 	  quotdirs.safe_push (format_chars);
2898 	}
2899       else if (!quot_begin_p && quot_end_p)
2900 	{
2901 	  if (quotdirs.length ())
2902 	    quotdirs.pop ();
2903 	  else
2904 	    format_warning_at_char (format_string_loc, format_string_cst,
2905 				    format_chars - orig_format_chars,
2906 				    OPT_Wformat_,
2907 				    "unmatched quoting directive");
2908 	}
2909 
2910       bool color_begin_p = strchr (fci->flags2, '/');
2911       if (color_begin_p)
2912 	{
2913 	  color_begin = format_chars;
2914 	  color_end = NULL;
2915 	}
2916       else if (strchr (fci->flags2, '\\'))
2917 	{
2918 	  if (color_end)
2919 	    format_warning_at_char (format_string_loc, format_string_cst,
2920 				    format_chars - orig_format_chars,
2921 				    OPT_Wformat_,
2922 				    "%qc directive redundant after prior "
2923 				    "occurence of the same", format_char);
2924 	  else if (!color_begin)
2925 	    format_warning_at_char (format_string_loc, format_string_cst,
2926 				    format_chars - orig_format_chars,
2927 				    OPT_Wformat_,
2928 				    "unmatched color reset directive");
2929 	  color_end = format_chars;
2930 	}
2931 
2932       /* Diagnose directives that shouldn't appear in a quoted sequence.
2933 	 (They are denoted by a double quote in FLAGS2.)  */
2934       if (quotdirs.length ())
2935 	{
2936 	  if (strchr (fci->flags2, '"'))
2937 	    format_warning_at_char (format_string_loc, format_string_cst,
2938 				    format_chars - orig_format_chars,
2939 				    OPT_Wformat_,
2940 				    "%qc conversion used within a quoted "
2941 				    "sequence",
2942 				    format_char);
2943 	}
2944 
2945       /* Validate the pairs of flags used.  */
2946       arg_parser.validate_flag_pairs (fci, format_char);
2947 
2948       arg_parser.give_y2k_warnings (fci, format_char);
2949 
2950       arg_parser.parse_any_scan_set (fci);
2951 
2952       tree wanted_type = NULL;
2953       const char *wanted_type_name = NULL;
2954 
2955       if (!arg_parser.handle_conversions (fci, len_modifier,
2956 					  wanted_type, wanted_type_name,
2957 					  arg_num,
2958 					  params,
2959 					  format_char))
2960 	continue;
2961 
2962       arg_parser.main_wanted_type.next = NULL;
2963 
2964       /* Finally. . .check type of argument against desired type!  */
2965       if (!arg_parser.check_argument_type (fci, len_modifier,
2966 					   wanted_type, wanted_type_name,
2967 					   suppressed,
2968 					   arg_num, params,
2969 					   alloc_flag,
2970 					   format_start, type_start,
2971 					   fmt_param_loc,
2972 					   format_char))
2973 	return;
2974     }
2975 
2976   if (format_chars - orig_format_chars != format_length)
2977     format_warning_at_char (format_string_loc, format_string_cst,
2978 			    format_chars + 1 - orig_format_chars,
2979 			    OPT_Wformat_contains_nul,
2980 			    "embedded %<\\0%> in format");
2981   if (info->first_arg_num != 0 && params != 0
2982       && has_operand_number <= 0)
2983     {
2984       res->number_other--;
2985       res->number_extra_args++;
2986     }
2987   if (has_operand_number > 0)
2988     finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2989 
2990   if (quotdirs.length ())
2991     format_warning_at_char (format_string_loc, format_string_cst,
2992 			    quotdirs.pop () - orig_format_chars,
2993 			    OPT_Wformat_, "unterminated quoting directive");
2994   if (color_begin && !color_end)
2995     format_warning_at_char (format_string_loc, format_string_cst,
2996 			    color_begin - orig_format_chars,
2997 			    OPT_Wformat_, "unterminated color directive");
2998 }
2999 
3000 /* Check the argument types from a single format conversion (possibly
3001    including width and precision arguments).
3002 
3003    FMT_LOC is the location of the format conversion.
3004 
3005    TYPES is a singly-linked list expressing the parts of the format
3006    conversion that expect argument types, and the arguments they
3007    correspond to.
3008 
3009    OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3010    format string to where type information begins for the conversion
3011    (the length modifier and conversion specifier).
3012 
3013    CONVERSION_CHAR is the user-provided conversion specifier.
3014 
3015    For example, given:
3016 
3017      sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3018 
3019    then FMT_LOC covers this range:
3020 
3021      sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3022                          ^^^^^^^^^
3023 
3024    and TYPES in this case is a three-entry singly-linked list consisting of:
3025    (1) the check for the field width here:
3026          sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3027                                 ^              ^^^^
3028        against arg3, and
3029    (2) the check for the field precision here:
3030          sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3031                                  ^^                  ^^^^
3032        against arg4, and
3033    (3) the check for the length modifier and conversion char here:
3034          sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3035                                    ^^^                     ^^^^
3036        against arg5.
3037 
3038    OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3039    STRING_CST:
3040 
3041                   0000000000111111111122
3042                   0123456789012345678901
3043      sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3044                                ^ ^
3045                                | ` CONVERSION_CHAR: 'd'
3046                                type starts here.  */
3047 
3048 static void
3049 check_format_types (const substring_loc &fmt_loc,
3050 		    format_wanted_type *types, const format_kind_info *fki,
3051 		    int offset_to_type_start,
3052 		    char conversion_char,
3053 		    vec<location_t> *arglocs)
3054 {
3055   for (; types != 0; types = types->next)
3056     {
3057       tree cur_param;
3058       tree cur_type;
3059       tree orig_cur_type;
3060       tree wanted_type;
3061       int arg_num;
3062       int i;
3063       int char_type_flag;
3064 
3065       wanted_type = types->wanted_type;
3066       arg_num = types->arg_num;
3067 
3068       /* The following should not occur here.  */
3069       gcc_assert (wanted_type);
3070       gcc_assert (wanted_type != void_type_node || types->pointer_count);
3071 
3072       if (types->pointer_count == 0)
3073 	wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
3074 
3075       wanted_type = TYPE_MAIN_VARIANT (wanted_type);
3076 
3077       cur_param = types->param;
3078       if (!cur_param)
3079         {
3080 	  format_type_warning (fmt_loc, UNKNOWN_LOCATION, types, wanted_type,
3081 			       NULL, fki, offset_to_type_start,
3082 			       conversion_char);
3083           continue;
3084         }
3085 
3086       cur_type = TREE_TYPE (cur_param);
3087       if (cur_type == error_mark_node)
3088 	continue;
3089       orig_cur_type = cur_type;
3090       char_type_flag = 0;
3091 
3092       location_t param_loc = UNKNOWN_LOCATION;
3093       if (EXPR_HAS_LOCATION (cur_param))
3094 	param_loc = EXPR_LOCATION (cur_param);
3095       else if (arglocs)
3096 	{
3097 	  /* arg_num is 1-based.  */
3098 	  gcc_assert (types->arg_num > 0);
3099 	  param_loc = (*arglocs)[types->arg_num - 1];
3100 	}
3101 
3102       STRIP_NOPS (cur_param);
3103 
3104       /* Check the types of any additional pointer arguments
3105 	 that precede the "real" argument.  */
3106       for (i = 0; i < types->pointer_count; ++i)
3107 	{
3108 	  if (TREE_CODE (cur_type) == POINTER_TYPE)
3109 	    {
3110 	      cur_type = TREE_TYPE (cur_type);
3111 	      if (cur_type == error_mark_node)
3112 		break;
3113 
3114 	      /* Check for writing through a NULL pointer.  */
3115 	      if (types->writing_in_flag
3116 		  && i == 0
3117 		  && cur_param != 0
3118 		  && integer_zerop (cur_param))
3119 		warning (OPT_Wformat_, "writing through null pointer "
3120 			 "(argument %d)", arg_num);
3121 
3122 	      /* Check for reading through a NULL pointer.  */
3123 	      if (types->reading_from_flag
3124 		  && i == 0
3125 		  && cur_param != 0
3126 		  && integer_zerop (cur_param))
3127 		warning (OPT_Wformat_, "reading through null pointer "
3128 			 "(argument %d)", arg_num);
3129 
3130 	      if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
3131 		cur_param = TREE_OPERAND (cur_param, 0);
3132 	      else
3133 		cur_param = 0;
3134 
3135 	      /* See if this is an attempt to write into a const type with
3136 		 scanf or with printf "%n".  Note: the writing in happens
3137 		 at the first indirection only, if for example
3138 		 void * const * is passed to scanf %p; passing
3139 		 const void ** is simply passing an incompatible type.  */
3140 	      if (types->writing_in_flag
3141 		  && i == 0
3142 		  && (TYPE_READONLY (cur_type)
3143 		      || (cur_param != 0
3144 			  && (CONSTANT_CLASS_P (cur_param)
3145 			      || (DECL_P (cur_param)
3146 				  && TREE_READONLY (cur_param))))))
3147 		warning (OPT_Wformat_, "writing into constant object "
3148 			 "(argument %d)", arg_num);
3149 
3150 	      /* If there are extra type qualifiers beyond the first
3151 		 indirection, then this makes the types technically
3152 		 incompatible.  */
3153 	      if (i > 0
3154 		  && pedantic
3155 		  && (TYPE_READONLY (cur_type)
3156 		      || TYPE_VOLATILE (cur_type)
3157 		      || TYPE_ATOMIC (cur_type)
3158 		      || TYPE_RESTRICT (cur_type)))
3159 		warning (OPT_Wformat_, "extra type qualifiers in format "
3160 			 "argument (argument %d)",
3161 			 arg_num);
3162 
3163 	    }
3164 	  else
3165 	    {
3166 	      format_type_warning (fmt_loc, param_loc,
3167 				   types, wanted_type, orig_cur_type, fki,
3168 				   offset_to_type_start, conversion_char);
3169 	      break;
3170 	    }
3171 	}
3172 
3173       if (i < types->pointer_count)
3174 	continue;
3175 
3176       cur_type = TYPE_MAIN_VARIANT (cur_type);
3177 
3178       /* Check whether the argument type is a character type.  This leniency
3179 	 only applies to certain formats, flagged with 'c'.  */
3180       if (types->char_lenient_flag)
3181 	char_type_flag = (cur_type == char_type_node
3182 			  || cur_type == signed_char_type_node
3183 			  || cur_type == unsigned_char_type_node);
3184 
3185       /* Check the type of the "real" argument, if there's a type we want.  */
3186       if (lang_hooks.types_compatible_p (wanted_type, cur_type))
3187 	continue;
3188       /* If we want 'void *', allow any pointer type.
3189 	 (Anything else would already have got a warning.)
3190 	 With -Wpedantic, only allow pointers to void and to character
3191 	 types.  */
3192       if (wanted_type == void_type_node
3193 	  && (!pedantic || (i == 1 && char_type_flag)))
3194 	continue;
3195       /* Don't warn about differences merely in signedness, unless
3196 	 -Wpedantic.  With -Wpedantic, warn if the type is a pointer
3197 	 target and not a character type, and for character types at
3198 	 a second level of indirection.  */
3199       if (TREE_CODE (wanted_type) == INTEGER_TYPE
3200 	  && TREE_CODE (cur_type) == INTEGER_TYPE
3201 	  && ((!pedantic && !warn_format_signedness)
3202 	      || (i == 0 && !warn_format_signedness)
3203 	      || (i == 1 && char_type_flag))
3204 	  && (TYPE_UNSIGNED (wanted_type)
3205 	      ? wanted_type == c_common_unsigned_type (cur_type)
3206 	      : wanted_type == c_common_signed_type (cur_type)))
3207 	continue;
3208       /* Don't warn about differences merely in signedness if we know
3209 	 that the current type is integer-promoted and its original type
3210 	 was unsigned such as that it is in the range of WANTED_TYPE.  */
3211       if (TREE_CODE (wanted_type) == INTEGER_TYPE
3212 	  && TREE_CODE (cur_type) == INTEGER_TYPE
3213 	  && warn_format_signedness
3214 	  && TYPE_UNSIGNED (wanted_type)
3215 	  && cur_param != NULL_TREE
3216 	  && TREE_CODE (cur_param) == NOP_EXPR)
3217 	{
3218 	  tree t = TREE_TYPE (TREE_OPERAND (cur_param, 0));
3219 	  if (TYPE_UNSIGNED (t)
3220 	      && cur_type == lang_hooks.types.type_promotes_to (t))
3221 	    continue;
3222 	}
3223       /* Likewise, "signed char", "unsigned char" and "char" are
3224 	 equivalent but the above test won't consider them equivalent.  */
3225       if (wanted_type == char_type_node
3226 	  && (!pedantic || i < 2)
3227 	  && char_type_flag)
3228 	continue;
3229       if (types->scalar_identity_flag
3230 	  && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
3231 	      || (INTEGRAL_TYPE_P (cur_type)
3232 		  && INTEGRAL_TYPE_P (wanted_type)))
3233 	  && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
3234 	continue;
3235       /* Now we have a type mismatch.  */
3236       format_type_warning (fmt_loc, param_loc, types,
3237 			   wanted_type, orig_cur_type, fki,
3238 			   offset_to_type_start, conversion_char);
3239     }
3240 }
3241 
3242 /* Given type TYPE, attempt to dereference the type N times
3243    (e.g. from ("int ***", 2) to "int *")
3244 
3245    Return the derefenced type, with any qualifiers
3246    such as "const" stripped from the result, or
3247    NULL if unsuccessful (e.g. TYPE is not a pointer type).  */
3248 
3249 static tree
3250 deref_n_times (tree type, int n)
3251 {
3252   gcc_assert (type);
3253 
3254   for (int i = n; i > 0; i--)
3255     {
3256       if (TREE_CODE (type) != POINTER_TYPE)
3257 	return NULL_TREE;
3258       type = TREE_TYPE (type);
3259     }
3260   /* Strip off any "const" etc.  */
3261   return build_qualified_type (type, 0);
3262 }
3263 
3264 /* Lookup the format code for FORMAT_LEN within FLI,
3265    returning the string code for expressing it, or NULL
3266    if it is not found.  */
3267 
3268 static const char *
3269 get_modifier_for_format_len (const format_length_info *fli,
3270 			     enum format_lengths format_len)
3271 {
3272   for (; fli->name; fli++)
3273     {
3274       if (fli->index == format_len)
3275 	return fli->name;
3276       if (fli->double_index == format_len)
3277 	return fli->double_name;
3278     }
3279   return NULL;
3280 }
3281 
3282 #if CHECKING_P
3283 
3284 namespace selftest {
3285 
3286 static void
3287 test_get_modifier_for_format_len ()
3288 {
3289   ASSERT_STREQ ("h",
3290 		get_modifier_for_format_len (printf_length_specs, FMT_LEN_h));
3291   ASSERT_STREQ ("hh",
3292 		get_modifier_for_format_len (printf_length_specs, FMT_LEN_hh));
3293   ASSERT_STREQ ("L",
3294 		get_modifier_for_format_len (printf_length_specs, FMT_LEN_L));
3295   ASSERT_EQ (NULL,
3296 	     get_modifier_for_format_len (printf_length_specs, FMT_LEN_none));
3297 }
3298 
3299 } // namespace selftest
3300 
3301 #endif /* CHECKING_P */
3302 
3303 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3304    format_type_detail using SPEC_TYPE to be offered as a suggestion for
3305    Wformat type errors where the argument has type ARG_TYPE.  */
3306 
3307 static bool
3308 matching_type_p (tree spec_type, tree arg_type)
3309 {
3310   gcc_assert (spec_type);
3311   gcc_assert (arg_type);
3312 
3313   /* If any of the types requires structural equality, we can't compare
3314      their canonical types.  */
3315   if (TYPE_STRUCTURAL_EQUALITY_P (spec_type)
3316       || TYPE_STRUCTURAL_EQUALITY_P (arg_type))
3317     return false;
3318 
3319   spec_type = TYPE_CANONICAL (spec_type);
3320   arg_type = TYPE_CANONICAL (arg_type);
3321 
3322   if (TREE_CODE (spec_type) == INTEGER_TYPE
3323       && TREE_CODE (arg_type) == INTEGER_TYPE
3324       && (TYPE_UNSIGNED (spec_type)
3325 	  ? spec_type == c_common_unsigned_type (arg_type)
3326 	  : spec_type == c_common_signed_type (arg_type)))
3327     return true;
3328 
3329   return spec_type == arg_type;
3330 }
3331 
3332 /* Subroutine of get_format_for_type.
3333 
3334    Generate a string containing the length modifier and conversion specifier
3335    that should be used to format arguments of type ARG_TYPE within FKI
3336    (effectively the inverse of the checking code).
3337 
3338    If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3339    is required to use it, for correcting bogus length modifiers.
3340    If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3341    that matches ARG_TYPE.
3342 
3343    If successful, returns a non-NULL string which should be freed
3344    by the caller.
3345    Otherwise, returns NULL.  */
3346 
3347 static char *
3348 get_format_for_type_1 (const format_kind_info *fki, tree arg_type,
3349 		       char conversion_char)
3350 {
3351   gcc_assert (arg_type);
3352 
3353   const format_char_info *spec;
3354   for (spec = &fki->conversion_specs[0];
3355        spec->format_chars;
3356        spec++)
3357     {
3358       if (conversion_char)
3359 	if (!strchr (spec->format_chars, conversion_char))
3360 	  continue;
3361 
3362       tree effective_arg_type = deref_n_times (arg_type,
3363 					       spec->pointer_count);
3364       if (!effective_arg_type)
3365 	continue;
3366       for (int i = 0; i < FMT_LEN_MAX; i++)
3367 	{
3368 	  const format_type_detail *ftd = &spec->types[i];
3369 	  if (!ftd->type)
3370 	    continue;
3371 	  if (matching_type_p (*ftd->type, effective_arg_type))
3372 	    {
3373 	      const char *len_modifier
3374 		= get_modifier_for_format_len (fki->length_char_specs,
3375 					       (enum format_lengths)i);
3376 	      if (!len_modifier)
3377 		len_modifier = "";
3378 
3379 	      if (conversion_char)
3380 		/* We found a match, using the given conversion char - the
3381 		   length modifier was incorrect (or absent).
3382 		   Provide a suggestion using the conversion char with the
3383 		   correct length modifier for the type.  */
3384 		return xasprintf ("%s%c", len_modifier, conversion_char);
3385 	      else
3386 		/* 2nd pass: no match was possible using the user-provided
3387 		   conversion char, but we do have a match without using it.
3388 		   Provide a suggestion using the first conversion char
3389 		   listed for the given type.  */
3390 		return xasprintf ("%s%c", len_modifier, spec->format_chars[0]);
3391 	    }
3392 	}
3393    }
3394 
3395   return NULL;
3396 }
3397 
3398 /* Generate a string containing the length modifier and conversion specifier
3399    that should be used to format arguments of type ARG_TYPE within FKI
3400    (effectively the inverse of the checking code).
3401 
3402    If successful, returns a non-NULL string which should be freed
3403    by the caller.
3404    Otherwise, returns NULL.  */
3405 
3406 static char *
3407 get_format_for_type (const format_kind_info *fki, tree arg_type,
3408 		     char conversion_char)
3409 {
3410   gcc_assert (arg_type);
3411   gcc_assert (conversion_char);
3412 
3413   /* First pass: look for a format_char_info containing CONVERSION_CHAR
3414      If we find one, then presumably the length modifier was incorrect
3415      (or absent).  */
3416   char *result = get_format_for_type_1 (fki, arg_type, conversion_char);
3417   if (result)
3418     return result;
3419 
3420   /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3421      matching just on the type. */
3422   return get_format_for_type_1 (fki, arg_type, '\0');
3423 }
3424 
3425 /* Attempt to get a string for use as a replacement fix-it hint for the
3426    source range in FMT_LOC.
3427 
3428    Preserve all of the text within the range of FMT_LOC up to
3429    OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3430    length modifier and conversion specifier for ARG_TYPE, attempting
3431    to keep the user-provided CONVERSION_CHAR if possible.
3432 
3433    For example, given a long vs long long mismatch for arg5 here:
3434 
3435     000000000111111111122222222223333333333|
3436     123456789012345678901234567890123456789` column numbers
3437                    0000000000111111111122|
3438                    0123456789012345678901` string offsets
3439                           V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3440       sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3441                                 ^ ^
3442                                 | ` CONVERSION_CHAR: 'd'
3443                                 type starts here
3444 
3445    where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3446    STRING_CST), where the user provided:
3447      %-+*.*lld
3448    the result (assuming "long" argument 5) should be:
3449      %-+*.*ld
3450 
3451    If successful, returns a non-NULL string which should be freed
3452    by the caller.
3453    Otherwise, returns NULL.  */
3454 
3455 static char *
3456 get_corrected_substring (const substring_loc &fmt_loc,
3457 			 format_wanted_type *type, tree arg_type,
3458 			 const format_kind_info *fki,
3459 			 int offset_to_type_start, char conversion_char)
3460 {
3461   /* Attempt to provide hints for argument types, but not for field widths
3462      and precisions.  */
3463   if (!arg_type)
3464     return NULL;
3465   if (type->kind != CF_KIND_FORMAT)
3466     return NULL;
3467 
3468   /* Locate the current code within the source range, rejecting
3469      any awkward cases where the format string occupies more than
3470      one line.
3471      Lookup the place where the type starts (including any length
3472      modifiers), getting it as the caret location.  */
3473   substring_loc type_loc (fmt_loc);
3474   type_loc.set_caret_index (offset_to_type_start);
3475 
3476   location_t fmt_substring_loc;
3477   const char *err = type_loc.get_location (&fmt_substring_loc);
3478   if (err)
3479     return NULL;
3480 
3481   source_range fmt_substring_range
3482     = get_range_from_loc (line_table, fmt_substring_loc);
3483 
3484   expanded_location caret
3485     = expand_location_to_spelling_point (fmt_substring_loc);
3486   expanded_location start
3487     = expand_location_to_spelling_point (fmt_substring_range.m_start);
3488   expanded_location finish
3489     = expand_location_to_spelling_point (fmt_substring_range.m_finish);
3490   if (caret.file != start.file)
3491     return NULL;
3492   if (start.file != finish.file)
3493     return NULL;
3494   if (caret.line != start.line)
3495     return NULL;
3496   if (start.line != finish.line)
3497     return NULL;
3498   if (start.column > caret.column)
3499     return NULL;
3500   if (start.column > finish.column)
3501     return NULL;
3502   if (caret.column > finish.column)
3503     return NULL;
3504 
3505   int line_width;
3506   const char *line = location_get_source_line (start.file, start.line,
3507 					       &line_width);
3508   if (line == NULL)
3509     return NULL;
3510 
3511   /* If we got this far, then we have the line containing the
3512      existing conversion specification.
3513 
3514      Generate a trimmed copy, containing the prefix part of the conversion
3515      specification, up to the (but not including) the length modifier.
3516      In the above example, this would be "%-+*.*".  */
3517   const char *current_content = line + start.column - 1;
3518   int length_up_to_type = caret.column - start.column;
3519   char *prefix = xstrndup (current_content, length_up_to_type);
3520 
3521   /* Now attempt to generate a suggestion for the rest of the specification
3522      (length modifier and conversion char), based on ARG_TYPE and
3523      CONVERSION_CHAR.
3524      In the above example, this would be "ld".  */
3525   char *format_for_type = get_format_for_type (fki, arg_type, conversion_char);
3526   if (!format_for_type)
3527     {
3528       free (prefix);
3529       return NULL;
3530     }
3531 
3532   /* Success.  Generate the resulting suggestion for the whole range of
3533      FMT_LOC by concatenating the two strings.
3534      In the above example, this would be "%-+*.*ld".  */
3535   char *result = concat (prefix, format_for_type, NULL);
3536   free (format_for_type);
3537   free (prefix);
3538   return result;
3539 }
3540 
3541 /* Give a warning about a format argument of different type from that expected.
3542    The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3543    is based on the location of the char at TYPE->offset_loc.
3544    PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3545    if this is unavailable.
3546    WANTED_TYPE is the type the argument should have,
3547    possibly stripped of pointer dereferences.  The description (such as "field
3548    precision"), the placement in the format string, a possibly more
3549    friendly name of WANTED_TYPE, and the number of pointer dereferences
3550    are taken from TYPE.  ARG_TYPE is the type of the actual argument,
3551    or NULL if it is missing.
3552 
3553    OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3554    format string to where type information begins for the conversion
3555    (the length modifier and conversion specifier).
3556    CONVERSION_CHAR is the user-provided conversion specifier.
3557 
3558    For example, given a type mismatch for argument 5 here:
3559 
3560     00000000011111111112222222222333333333344444444445555555555|
3561     12345678901234567890123456789012345678901234567890123456789` column numbers
3562                    0000000000111111111122|
3563                    0123456789012345678901` offsets within STRING_CST
3564                           V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3565       sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3566                                 ^ ^                             ^~~~~~~~~
3567                                 | ` CONVERSION_CHAR: 'd'        PARAM_LOC
3568                                 type starts here
3569 
3570    OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3571    STRING_CST.  */
3572 
3573 static void
3574 format_type_warning (const substring_loc &whole_fmt_loc,
3575 		     location_t param_loc,
3576 		     format_wanted_type *type,
3577 		     tree wanted_type, tree arg_type,
3578 		     const format_kind_info *fki,
3579 		     int offset_to_type_start,
3580 		     char conversion_char)
3581 {
3582   enum format_specifier_kind kind = type->kind;
3583   const char *wanted_type_name = type->wanted_type_name;
3584   const char *format_start = type->format_start;
3585   int format_length = type->format_length;
3586   int pointer_count = type->pointer_count;
3587   int arg_num = type->arg_num;
3588 
3589   char *p;
3590   /* If ARG_TYPE is a typedef with a misleading name (for example,
3591      size_t but not the standard size_t expected by printf %zu), avoid
3592      printing the typedef name.  */
3593   if (wanted_type_name
3594       && arg_type
3595       && TYPE_NAME (arg_type)
3596       && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
3597       && DECL_NAME (TYPE_NAME (arg_type))
3598       && !strcmp (wanted_type_name,
3599 		  lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
3600     arg_type = TYPE_MAIN_VARIANT (arg_type);
3601   /* The format type and name exclude any '*' for pointers, so those
3602      must be formatted manually.  For all the types we currently have,
3603      this is adequate, but formats taking pointers to functions or
3604      arrays would require the full type to be built up in order to
3605      print it with %T.  */
3606   p = (char *) alloca (pointer_count + 2);
3607   if (pointer_count == 0)
3608     p[0] = 0;
3609   else if (c_dialect_cxx ())
3610     {
3611       memset (p, '*', pointer_count);
3612       p[pointer_count] = 0;
3613     }
3614   else
3615     {
3616       p[0] = ' ';
3617       memset (p + 1, '*', pointer_count);
3618       p[pointer_count + 1] = 0;
3619     }
3620 
3621   /* WHOLE_FMT_LOC has the caret at the end of the range.
3622      Set the caret to be at the offset from TYPE.  Subtract one
3623      from the offset for the same reason as in format_warning_at_char.  */
3624   substring_loc fmt_loc (whole_fmt_loc);
3625   fmt_loc.set_caret_index (type->offset_loc - 1);
3626 
3627   /* Get a string for use as a replacement fix-it hint for the range in
3628      fmt_loc, or NULL.  */
3629   char *corrected_substring
3630     = get_corrected_substring (fmt_loc, type, arg_type, fki,
3631 			       offset_to_type_start, conversion_char);
3632 
3633   if (wanted_type_name)
3634     {
3635       if (arg_type)
3636 	format_warning_at_substring
3637 	  (fmt_loc, param_loc,
3638 	   corrected_substring, OPT_Wformat_,
3639 	   "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
3640 	   "but argument %d has type %qT",
3641 	   gettext (kind_descriptions[kind]),
3642 	   (kind == CF_KIND_FORMAT ? "%" : ""),
3643 	   format_length, format_start,
3644 	   wanted_type_name, p, arg_num, arg_type);
3645       else
3646 	format_warning_at_substring
3647 	  (fmt_loc, param_loc,
3648 	   corrected_substring, OPT_Wformat_,
3649 	   "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
3650 	   gettext (kind_descriptions[kind]),
3651 	   (kind == CF_KIND_FORMAT ? "%" : ""),
3652 	   format_length, format_start, wanted_type_name, p);
3653     }
3654   else
3655     {
3656       if (arg_type)
3657 	format_warning_at_substring
3658 	  (fmt_loc, param_loc,
3659 	   corrected_substring, OPT_Wformat_,
3660 	   "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
3661 	   "but argument %d has type %qT",
3662 	   gettext (kind_descriptions[kind]),
3663 	   (kind == CF_KIND_FORMAT ? "%" : ""),
3664 	   format_length, format_start,
3665 	   wanted_type, p, arg_num, arg_type);
3666       else
3667 	format_warning_at_substring
3668 	  (fmt_loc, param_loc,
3669 	   corrected_substring, OPT_Wformat_,
3670 	   "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
3671 	   gettext (kind_descriptions[kind]),
3672 	   (kind == CF_KIND_FORMAT ? "%" : ""),
3673 	   format_length, format_start, wanted_type, p);
3674     }
3675 
3676   free (corrected_substring);
3677 }
3678 
3679 
3680 /* Given a format_char_info array FCI, and a character C, this function
3681    returns the index into the conversion_specs where that specifier's
3682    data is located.  The character must exist.  */
3683 static unsigned int
3684 find_char_info_specifier_index (const format_char_info *fci, int c)
3685 {
3686   unsigned i;
3687 
3688   for (i = 0; fci->format_chars; i++, fci++)
3689     if (strchr (fci->format_chars, c))
3690       return i;
3691 
3692   /* We shouldn't be looking for a non-existent specifier.  */
3693   gcc_unreachable ();
3694 }
3695 
3696 /* Given a format_length_info array FLI, and a character C, this
3697    function returns the index into the conversion_specs where that
3698    modifier's data is located.  The character must exist.  */
3699 static unsigned int
3700 find_length_info_modifier_index (const format_length_info *fli, int c)
3701 {
3702   unsigned i;
3703 
3704   for (i = 0; fli->name; i++, fli++)
3705     if (strchr (fli->name, c))
3706       return i;
3707 
3708   /* We shouldn't be looking for a non-existent modifier.  */
3709   gcc_unreachable ();
3710 }
3711 
3712 /* Determine the type of HOST_WIDE_INT in the code being compiled for
3713    use in GCC's __asm_fprintf__ custom format attribute.  You must
3714    have set dynamic_format_types before calling this function.  */
3715 static void
3716 init_dynamic_asm_fprintf_info (void)
3717 {
3718   static tree hwi;
3719 
3720   if (!hwi)
3721     {
3722       format_length_info *new_asm_fprintf_length_specs;
3723       unsigned int i;
3724 
3725       /* Find the underlying type for HOST_WIDE_INT.  For the %w
3726 	 length modifier to work, one must have issued: "typedef
3727 	 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3728 	 prior to using that modifier.  */
3729       hwi = maybe_get_identifier ("__gcc_host_wide_int__");
3730       if (!hwi)
3731 	{
3732 	  error ("%<__gcc_host_wide_int__%> is not defined as a type");
3733 	  return;
3734 	}
3735       hwi = identifier_global_value (hwi);
3736       if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
3737 	{
3738 	  error ("%<__gcc_host_wide_int__%> is not defined as a type");
3739 	  return;
3740 	}
3741       hwi = DECL_ORIGINAL_TYPE (hwi);
3742       gcc_assert (hwi);
3743       if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
3744 	{
3745 	  error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
3746 		 " or %<long long%>");
3747 	  return;
3748 	}
3749 
3750       /* Create a new (writable) copy of asm_fprintf_length_specs.  */
3751       new_asm_fprintf_length_specs = (format_length_info *)
3752 				     xmemdup (asm_fprintf_length_specs,
3753 					      sizeof (asm_fprintf_length_specs),
3754 					      sizeof (asm_fprintf_length_specs));
3755 
3756       /* HOST_WIDE_INT must be one of 'long' or 'long long'.  */
3757       i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
3758       if (hwi == long_integer_type_node)
3759 	new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
3760       else if (hwi == long_long_integer_type_node)
3761 	new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
3762       else
3763 	gcc_unreachable ();
3764 
3765       /* Assign the new data for use.  */
3766       dynamic_format_types[asm_fprintf_format_type].length_char_specs =
3767 	new_asm_fprintf_length_specs;
3768     }
3769 }
3770 
3771 /* Determine the type of a "locus" in the code being compiled for use
3772    in GCC's __gcc_gfc__ custom format attribute.  You must have set
3773    dynamic_format_types before calling this function.  */
3774 static void
3775 init_dynamic_gfc_info (void)
3776 {
3777   if (!locus)
3778     {
3779       static format_char_info *gfc_fci;
3780 
3781       /* For the GCC __gcc_gfc__ custom format specifier to work, one
3782 	 must have declared 'locus' prior to using this attribute.  If
3783 	 we haven't seen this declarations then you shouldn't use the
3784 	 specifier requiring that type.  */
3785       if ((locus = maybe_get_identifier ("locus")))
3786 	{
3787 	  locus = identifier_global_value (locus);
3788 	  if (locus)
3789 	    {
3790 	      if (TREE_CODE (locus) != TYPE_DECL
3791 		  || TREE_TYPE (locus) == error_mark_node)
3792 		{
3793 		  error ("%<locus%> is not defined as a type");
3794 		  locus = 0;
3795 		}
3796 	      else
3797 		locus = TREE_TYPE (locus);
3798 	    }
3799 	}
3800 
3801       /* Assign the new data for use.  */
3802 
3803       /* Handle the __gcc_gfc__ format specifics.  */
3804       if (!gfc_fci)
3805 	dynamic_format_types[gcc_gfc_format_type].conversion_specs =
3806 	  gfc_fci = (format_char_info *)
3807 		     xmemdup (gcc_gfc_char_table,
3808 			      sizeof (gcc_gfc_char_table),
3809 			      sizeof (gcc_gfc_char_table));
3810       if (locus)
3811 	{
3812 	  const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
3813 	  gfc_fci[i].types[0].type = &locus;
3814 	  gfc_fci[i].pointer_count = 1;
3815 	}
3816     }
3817 }
3818 
3819 /* Determine the types of "tree" and "location_t" in the code being
3820    compiled for use in GCC's diagnostic custom format attributes.  You
3821    must have set dynamic_format_types before calling this function.  */
3822 static void
3823 init_dynamic_diag_info (void)
3824 {
3825   /* For the GCC-diagnostics custom format specifiers to work, one
3826      must have declared 'tree' and 'location_t' prior to using those
3827      attributes.  If we haven't seen these declarations then
3828      the specifiers requiring these types shouldn't be used.
3829      However we don't force a hard ICE because we may see only one
3830      or the other type.  */
3831   if (tree loc = maybe_get_identifier ("location_t"))
3832     {
3833       loc = identifier_global_value (loc);
3834       if (loc && TREE_CODE (loc) != TYPE_DECL)
3835 	error ("%<location_t%> is not defined as a type");
3836     }
3837 
3838   /* Initialize the global tree node type local to this file.  */
3839   if (!local_tree_type_node
3840       || local_tree_type_node == void_type_node)
3841     {
3842       /* We need to grab the underlying 'union tree_node' so peek into
3843 	 an extra type level.  */
3844       if ((local_tree_type_node = maybe_get_identifier ("tree")))
3845 	{
3846 	  local_tree_type_node = identifier_global_value (local_tree_type_node);
3847 	  if (local_tree_type_node)
3848 	    {
3849 	      if (TREE_CODE (local_tree_type_node) != TYPE_DECL)
3850 		{
3851 		  error ("%<tree%> is not defined as a type");
3852 		  local_tree_type_node = 0;
3853 		}
3854 	      else if (TREE_CODE (TREE_TYPE (local_tree_type_node))
3855 		       != POINTER_TYPE)
3856 		{
3857 		  error ("%<tree%> is not defined as a pointer type");
3858 		  local_tree_type_node = 0;
3859 		}
3860 	      else
3861 		local_tree_type_node =
3862 		  TREE_TYPE (TREE_TYPE (local_tree_type_node));
3863 	    }
3864 	}
3865       else
3866 	local_tree_type_node = void_type_node;
3867     }
3868 
3869   /* Similar to the above but for gcall*.  */
3870   if (!local_gcall_ptr_node
3871       || local_gcall_ptr_node == void_type_node)
3872     {
3873       if ((local_gcall_ptr_node = maybe_get_identifier ("gcall")))
3874 	{
3875 	  local_gcall_ptr_node
3876 	    = identifier_global_value (local_gcall_ptr_node);
3877 	  if (local_gcall_ptr_node)
3878 	    {
3879 	      if (TREE_CODE (local_gcall_ptr_node) != TYPE_DECL)
3880 		{
3881 		  error ("%<gcall%> is not defined as a type");
3882 		  local_gcall_ptr_node = 0;
3883 		}
3884 	      else
3885 		local_gcall_ptr_node = TREE_TYPE (local_gcall_ptr_node);
3886 	    }
3887 	}
3888       else
3889 	local_gcall_ptr_node = void_type_node;
3890     }
3891 
3892   static tree hwi;
3893 
3894   if (!hwi)
3895     {
3896       static format_length_info *diag_ls;
3897       unsigned int i;
3898 
3899       /* Find the underlying type for HOST_WIDE_INT.  For the 'w'
3900 	 length modifier to work, one must have issued: "typedef
3901 	 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3902 	 prior to using that modifier.  */
3903       if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
3904 	{
3905 	  hwi = identifier_global_value (hwi);
3906 	  if (hwi)
3907 	    {
3908 	      if (TREE_CODE (hwi) != TYPE_DECL)
3909 		{
3910 		  error ("%<__gcc_host_wide_int__%> is not defined as a type");
3911 		  hwi = 0;
3912 		}
3913 	      else
3914 		{
3915 		  hwi = DECL_ORIGINAL_TYPE (hwi);
3916 		  gcc_assert (hwi);
3917 		  if (hwi != long_integer_type_node
3918 		      && hwi != long_long_integer_type_node)
3919 		    {
3920 		      error ("%<__gcc_host_wide_int__%> is not defined"
3921 			     " as %<long%> or %<long long%>");
3922 		      hwi = 0;
3923 		    }
3924 		}
3925 	    }
3926 	}
3927 
3928       /* Assign the new data for use.  */
3929 
3930       /* All the GCC diag formats use the same length specs.  */
3931       if (!diag_ls)
3932 	dynamic_format_types[gcc_diag_format_type].length_char_specs =
3933 	  dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
3934 	  dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
3935 	  dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
3936 	  diag_ls = (format_length_info *)
3937 		    xmemdup (gcc_diag_length_specs,
3938 			     sizeof (gcc_diag_length_specs),
3939 			     sizeof (gcc_diag_length_specs));
3940       if (hwi)
3941 	{
3942 	  /* HOST_WIDE_INT must be one of 'long' or 'long long'.  */
3943 	  i = find_length_info_modifier_index (diag_ls, 'w');
3944 	  if (hwi == long_integer_type_node)
3945 	    diag_ls[i].index = FMT_LEN_l;
3946 	  else if (hwi == long_long_integer_type_node)
3947 	    diag_ls[i].index = FMT_LEN_ll;
3948 	  else
3949 	    gcc_unreachable ();
3950 	}
3951     }
3952 
3953   /* It's safe to "re-initialize these to the same values.  */
3954   dynamic_format_types[gcc_diag_format_type].conversion_specs =
3955     gcc_diag_char_table;
3956   dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
3957     gcc_tdiag_char_table;
3958   dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
3959     gcc_cdiag_char_table;
3960   dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
3961     gcc_cxxdiag_char_table;
3962 }
3963 
3964 #ifdef TARGET_FORMAT_TYPES
3965 extern const format_kind_info TARGET_FORMAT_TYPES[];
3966 #endif
3967 
3968 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3969 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
3970 #endif
3971 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3972   extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3973 #endif
3974 
3975 /* Attributes such as "printf" are equivalent to those such as
3976    "gnu_printf" unless this is overridden by a target.  */
3977 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
3978 {
3979   { "gnu_printf",   "printf" },
3980   { "gnu_scanf",    "scanf" },
3981   { "gnu_strftime", "strftime" },
3982   { "gnu_strfmon",  "strfmon" },
3983   { NULL,           NULL }
3984 };
3985 
3986 /* Translate to unified attribute name. This is used in decode_format_type and
3987    decode_format_attr. In attr_name the user specified argument is passed. It
3988    returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3989    or the attr_name passed to this function, if there is no matching entry.  */
3990 static const char *
3991 convert_format_name_to_system_name (const char *attr_name)
3992 {
3993   int i;
3994 
3995   if (attr_name == NULL || *attr_name == 0
3996       || strncmp (attr_name, "gcc_", 4) == 0)
3997     return attr_name;
3998 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3999   TARGET_OVERRIDES_FORMAT_INIT ();
4000 #endif
4001 
4002 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4003   /* Check if format attribute is overridden by target.  */
4004   if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
4005       && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
4006     {
4007       for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
4008         {
4009           if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
4010 			   attr_name))
4011             return attr_name;
4012           if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
4013 			   attr_name))
4014             return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
4015         }
4016     }
4017 #endif
4018   /* Otherwise default to gnu format.  */
4019   for (i = 0;
4020        gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
4021        ++i)
4022     {
4023       if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
4024 		       attr_name))
4025         return attr_name;
4026       if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
4027 		       attr_name))
4028         return gnu_target_overrides_format_attributes[i].named_attr_src;
4029     }
4030 
4031   return attr_name;
4032 }
4033 
4034 /* Handle a "format" attribute; arguments as in
4035    struct attribute_spec.handler.  */
4036 tree
4037 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
4038 			 int flags, bool *no_add_attrs)
4039 {
4040   tree type = *node;
4041   function_format_info info;
4042 
4043 #ifdef TARGET_FORMAT_TYPES
4044   /* If the target provides additional format types, we need to
4045      add them to FORMAT_TYPES at first use.  */
4046   if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
4047     {
4048       dynamic_format_types = XNEWVEC (format_kind_info,
4049 				      n_format_types + TARGET_N_FORMAT_TYPES);
4050       memcpy (dynamic_format_types, format_types_orig,
4051 	      sizeof (format_types_orig));
4052       memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
4053 	      TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
4054 
4055       format_types = dynamic_format_types;
4056       /* Provide a reference for the first potential external type.  */
4057       first_target_format_type = n_format_types;
4058       n_format_types += TARGET_N_FORMAT_TYPES;
4059     }
4060 #endif
4061 
4062   /* Canonicalize name of format function.  */
4063   if (TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
4064     TREE_VALUE (args) = canonicalize_attr_name (TREE_VALUE (args));
4065 
4066   if (!decode_format_attr (args, &info, 0))
4067     {
4068       *no_add_attrs = true;
4069       return NULL_TREE;
4070     }
4071 
4072   if (prototype_p (type))
4073     {
4074       if (!check_format_string (type, info.format_num, flags,
4075 				no_add_attrs, info.format_type))
4076 	return NULL_TREE;
4077 
4078       if (info.first_arg_num != 0)
4079 	{
4080 	  unsigned HOST_WIDE_INT arg_num = 1;
4081 	  function_args_iterator iter;
4082 	  tree arg_type;
4083 
4084 	  /* Verify that first_arg_num points to the last arg,
4085 	     the ...  */
4086 	  FOREACH_FUNCTION_ARGS (type, arg_type, iter)
4087 	    arg_num++;
4088 
4089 	  if (arg_num != info.first_arg_num)
4090 	    {
4091 	      if (!(flags & (int) ATTR_FLAG_BUILT_IN))
4092 		error ("args to be formatted is not %<...%>");
4093 	      *no_add_attrs = true;
4094 	      return NULL_TREE;
4095 	    }
4096 	}
4097     }
4098 
4099   /* Check if this is a strftime variant. Just for this variant
4100      FMT_FLAG_ARG_CONVERT is not set.  */
4101   if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
4102       && info.first_arg_num != 0)
4103     {
4104       error ("strftime formats cannot format arguments");
4105       *no_add_attrs = true;
4106       return NULL_TREE;
4107     }
4108 
4109   /* If this is a custom GCC-internal format type, we have to
4110      initialize certain bits at runtime.  */
4111   if (info.format_type == asm_fprintf_format_type
4112       || info.format_type == gcc_gfc_format_type
4113       || info.format_type == gcc_diag_format_type
4114       || info.format_type == gcc_tdiag_format_type
4115       || info.format_type == gcc_cdiag_format_type
4116       || info.format_type == gcc_cxxdiag_format_type)
4117     {
4118       /* Our first time through, we have to make sure that our
4119 	 format_type data is allocated dynamically and is modifiable.  */
4120       if (!dynamic_format_types)
4121 	format_types = dynamic_format_types = (format_kind_info *)
4122 	  xmemdup (format_types_orig, sizeof (format_types_orig),
4123 		   sizeof (format_types_orig));
4124 
4125       /* If this is format __asm_fprintf__, we have to initialize
4126 	 GCC's notion of HOST_WIDE_INT for checking %wd.  */
4127       if (info.format_type == asm_fprintf_format_type)
4128 	init_dynamic_asm_fprintf_info ();
4129       /* If this is format __gcc_gfc__, we have to initialize GCC's
4130 	 notion of 'locus' at runtime for %L.  */
4131       else if (info.format_type == gcc_gfc_format_type)
4132 	init_dynamic_gfc_info ();
4133       /* If this is one of the diagnostic attributes, then we have to
4134 	 initialize 'location_t' and 'tree' at runtime.  */
4135       else if (info.format_type == gcc_diag_format_type
4136 	       || info.format_type == gcc_tdiag_format_type
4137 	       || info.format_type == gcc_cdiag_format_type
4138 	       || info.format_type == gcc_cxxdiag_format_type)
4139 	init_dynamic_diag_info ();
4140       else
4141 	gcc_unreachable ();
4142     }
4143 
4144   return NULL_TREE;
4145 }
4146 
4147 #if CHECKING_P
4148 
4149 namespace selftest {
4150 
4151 /* Selftests of location handling.  */
4152 
4153 /* Get the format_kind_info with the given name.  */
4154 
4155 static const format_kind_info *
4156 get_info (const char *name)
4157 {
4158   int idx = decode_format_type (name);
4159   const format_kind_info *fki = &format_types[idx];
4160   ASSERT_STREQ (fki->name, name);
4161   return fki;
4162 }
4163 
4164 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
4165    is EXPECTED_FORMAT.  */
4166 
4167 static void
4168 assert_format_for_type_streq (const location &loc, const format_kind_info *fki,
4169 			      const char *expected_format, tree type,
4170 			      char conversion_char)
4171 {
4172   gcc_assert (fki);
4173   gcc_assert (expected_format);
4174   gcc_assert (type);
4175 
4176   char *actual_format = get_format_for_type (fki, type, conversion_char);
4177   ASSERT_STREQ_AT (loc, expected_format, actual_format);
4178   free (actual_format);
4179 }
4180 
4181 /* Selftests for get_format_for_type.  */
4182 
4183 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
4184   assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
4185 				(TYPE), (CONVERSION_CHAR))
4186 
4187 /* Selftest for get_format_for_type for "printf"-style functions.  */
4188 
4189 static void
4190 test_get_format_for_type_printf ()
4191 {
4192   const format_kind_info *fki = get_info ("gnu_printf");
4193   ASSERT_NE (fki, NULL);
4194 
4195   ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'i');
4196   ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'i');
4197   ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'o');
4198   ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'o');
4199   ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'x');
4200   ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'x');
4201   ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'X');
4202   ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'X');
4203   ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node, 'd');
4204   ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node, 'i');
4205   ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node, 'o');
4206   ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node, 'x');
4207   ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node, 'X');
4208   ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node, 'd');
4209   ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node, 'i');
4210   ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node, 'o');
4211   ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node, 'x');
4212   ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node, 'X');
4213   ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node, 'd');
4214   ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node, 'i');
4215   ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node, 'x');
4216   ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node, 'o');
4217   ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node, 'x');
4218   ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node, 'd');
4219   ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node, 'i');
4220   ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node, 'o');
4221   ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node, 'x');
4222   ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node), 'i');
4223 }
4224 
4225 /* Selftest for get_format_for_type for "scanf"-style functions.  */
4226 
4227 static void
4228 test_get_format_for_type_scanf ()
4229 {
4230   const format_kind_info *fki = get_info ("gnu_scanf");
4231   ASSERT_NE (fki, NULL);
4232   ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node), 'd');
4233   ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node), 'u');
4234   ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
4235 				build_pointer_type (long_integer_type_node), 'd');
4236   ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
4237 				build_pointer_type (long_unsigned_type_node), 'u');
4238   ASSERT_FORMAT_FOR_TYPE_STREQ
4239     ("lld", build_pointer_type (long_long_integer_type_node), 'd');
4240   ASSERT_FORMAT_FOR_TYPE_STREQ
4241     ("llu", build_pointer_type (long_long_unsigned_type_node), 'u');
4242   ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node), 'e');
4243   ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node), 'e');
4244 }
4245 
4246 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4247 
4248 /* Run all of the selftests within this file.  */
4249 
4250 void
4251 c_format_c_tests ()
4252 {
4253   test_get_modifier_for_format_len ();
4254   test_get_format_for_type_printf ();
4255   test_get_format_for_type_scanf ();
4256 }
4257 
4258 } // namespace selftest
4259 
4260 #endif /* CHECKING_P */
4261 
4262 #include "gt-c-family-c-format.h"
4263