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