1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "alloc-pool.h"
27 #include "stringpool.h"
30 #include "langhooks.h"
32 #include "diagnostic.h"
33 #include "substring-locations.h"
38 /* Handle attributes associated with format checking. */
40 /* This must be in the same order as format_types, except for
41 format_type_error. Target-specific format types do not have
42 matching enum values. */
43 enum format_type
{ printf_format_type
, asm_fprintf_format_type
,
44 gcc_diag_format_type
, gcc_tdiag_format_type
,
45 gcc_cdiag_format_type
,
46 gcc_cxxdiag_format_type
, gcc_gfc_format_type
,
47 gcc_objc_string_format_type
,
48 format_type_error
= -1};
50 struct function_format_info
52 int format_type
; /* type of format (printf, scanf, etc.) */
53 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
54 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
57 /* Initialized in init_dynamic_diag_info. */
58 static GTY(()) tree local_tree_type_node
;
59 static GTY(()) tree local_gcall_ptr_node
;
60 static GTY(()) tree locus
;
62 static bool decode_format_attr (tree
, function_format_info
*, int);
63 static int decode_format_type (const char *);
65 static bool check_format_string (tree argument
,
66 unsigned HOST_WIDE_INT format_num
,
67 int flags
, bool *no_add_attrs
,
68 int expected_format_type
);
69 static bool get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
,
71 static const char *convert_format_name_to_system_name (const char *attr_name
);
73 static int first_target_format_type
;
74 static const char *format_name (int format_num
);
75 static int format_flags (int format_num
);
77 /* Emit a warning as per format_warning_va, but construct the substring_loc
78 for the character at offset (CHAR_IDX - 1) within a string constant
79 FORMAT_STRING_CST at FMT_STRING_LOC. */
81 ATTRIBUTE_GCC_DIAG (5,6)
83 format_warning_at_char (location_t fmt_string_loc
, tree format_string_cst
,
84 int char_idx
, int opt
, const char *gmsgid
, ...)
87 va_start (ap
, gmsgid
);
88 tree string_type
= TREE_TYPE (format_string_cst
);
90 /* The callers are of the form:
91 format_warning (format_string_loc, format_string_cst,
92 format_chars - orig_format_chars,
93 where format_chars has already been incremented, so that
94 CHAR_IDX is one character beyond where the warning should
95 be emitted. Fix it. */
98 substring_loc
fmt_loc (fmt_string_loc
, string_type
, char_idx
, char_idx
,
100 bool warned
= format_warning_va (fmt_loc
, NULL
, NULL
, opt
, gmsgid
, &ap
);
106 /* Check that we have a pointer to a string suitable for use as a format.
107 The default is to check for a char type.
108 For objective-c dialects, this is extended to include references to string
109 objects validated by objc_string_ref_type_p ().
110 Targets may also provide a string object type that can be used within c and
111 c++ and shared with their respective objective-c dialects. In this case the
112 reference to a format string is checked for validity via a hook.
114 The function returns true if strref points to any string type valid for the
115 language dialect and target. */
118 valid_stringptr_type_p (tree strref
)
120 return (strref
!= NULL
121 && TREE_CODE (strref
) == POINTER_TYPE
122 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref
)) == char_type_node
123 || objc_string_ref_type_p (strref
)
124 || (*targetcm
.string_object_ref_type_p
) ((const_tree
) strref
)));
127 /* Handle a "format_arg" attribute; arguments as in
128 struct attribute_spec.handler. */
130 handle_format_arg_attribute (tree
*node
, tree
ARG_UNUSED (name
),
131 tree args
, int flags
, bool *no_add_attrs
)
134 tree format_num_expr
= TREE_VALUE (args
);
135 unsigned HOST_WIDE_INT format_num
= 0;
137 if (!get_constant (format_num_expr
, &format_num
, 0))
139 error ("format string has invalid operand number");
140 *no_add_attrs
= true;
144 if (prototype_p (type
))
146 /* The format arg can be any string reference valid for the language and
147 target. We cannot be more specific in this case. */
148 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
152 if (!valid_stringptr_type_p (TREE_TYPE (type
)))
154 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
155 error ("function does not return string type");
156 *no_add_attrs
= true;
163 /* Verify that the format_num argument is actually a string reference suitable,
164 for the language dialect and target (in case the format attribute is in
165 error). When we know the specific reference type expected, this is also
168 check_format_string (tree fntype
, unsigned HOST_WIDE_INT format_num
,
169 int flags
, bool *no_add_attrs
, int expected_format_type
)
171 unsigned HOST_WIDE_INT i
;
172 bool is_objc_sref
, is_target_sref
, is_char_ref
;
175 function_args_iterator iter
;
178 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
186 || !valid_stringptr_type_p (ref
))
188 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
189 error ("format string argument is not a string type");
190 *no_add_attrs
= true;
194 /* We only know that we want a suitable string reference. */
195 if (expected_format_type
< 0)
198 /* Now check that the arg matches the expected type. */
200 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
202 fmt_flags
= format_flags (expected_format_type
);
203 is_objc_sref
= is_target_sref
= false;
205 is_objc_sref
= objc_string_ref_type_p (ref
);
207 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
210 return true; /* OK, we expected a char and found one. */
213 /* We expected a char but found an extended string type. */
215 error ("found a %qs reference but the format argument should"
216 " be a string", format_name (gcc_objc_string_format_type
));
218 error ("found a %qT but the format argument should be a string",
220 *no_add_attrs
= true;
225 /* We expect a string object type as the format arg. */
228 error ("format argument should be a %qs reference but"
229 " a string was found", format_name (expected_format_type
));
230 *no_add_attrs
= true;
234 /* We will assert that objective-c will support either its own string type
235 or the target-supplied variant. */
237 is_target_sref
= (*targetcm
.string_object_ref_type_p
) ((const_tree
) ref
);
239 if (expected_format_type
== (int) gcc_objc_string_format_type
240 && (is_objc_sref
|| is_target_sref
))
243 /* We will allow a target string ref to match only itself. */
244 if (first_target_format_type
245 && expected_format_type
>= first_target_format_type
250 error ("format argument should be a %qs reference",
251 format_name (expected_format_type
));
252 *no_add_attrs
= true;
259 /* Verify EXPR is a constant, and store its value.
260 If validated_p is true there should be no errors.
261 Returns true on success, false otherwise. */
263 get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
265 if (!tree_fits_uhwi_p (expr
))
267 gcc_assert (!validated_p
);
271 *value
= TREE_INT_CST_LOW (expr
);
276 /* Decode the arguments to a "format" attribute into a
277 function_format_info structure. It is already known that the list
278 is of the right length. If VALIDATED_P is true, then these
279 attributes have already been validated and must not be erroneous;
280 if false, it will give an error message. Returns true if the
281 attributes are successfully decoded, false otherwise. */
284 decode_format_attr (tree args
, function_format_info
*info
, int validated_p
)
286 tree format_type_id
= TREE_VALUE (args
);
287 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
288 tree first_arg_num_expr
289 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
291 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
293 gcc_assert (!validated_p
);
294 error ("unrecognized format specifier");
299 const char *p
= IDENTIFIER_POINTER (format_type_id
);
301 p
= convert_format_name_to_system_name (p
);
303 info
->format_type
= decode_format_type (p
);
305 if (!c_dialect_objc ()
306 && info
->format_type
== gcc_objc_string_format_type
)
308 gcc_assert (!validated_p
);
309 warning (OPT_Wformat_
, "%qE is only allowed in Objective-C dialects",
311 info
->format_type
= format_type_error
;
315 if (info
->format_type
== format_type_error
)
317 gcc_assert (!validated_p
);
318 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
324 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
326 error ("format string has invalid operand number");
330 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
332 error ("%<...%> has invalid operand number");
336 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
338 gcc_assert (!validated_p
);
339 error ("format string argument follows the args to be formatted");
346 /* Check a call to a format function against a parameter list. */
348 /* The C standard version C++ is treated as equivalent to
349 or inheriting from, for the purpose of format features supported. */
350 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
351 /* The C standard version we are checking formats against when pedantic. */
352 #define C_STD_VER ((int) (c_dialect_cxx () \
353 ? CPLUSPLUS_STD_VER \
356 : (flag_isoc94 ? STD_C94 : STD_C89))))
357 /* The name to give to the standard version we are warning about when
358 pedantic. FEATURE_VER is the version in which the feature warned out
359 appeared, which is higher than C_STD_VER. */
360 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
361 ? (cxx_dialect < cxx11 ? "ISO C++98" \
363 : ((FEATURE_VER) == STD_EXT \
366 /* Adjust a C standard version, which may be STD_C9L, to account for
367 -Wno-long-long. Returns other standard versions unchanged. */
368 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
369 ? (warn_long_long ? STD_C99 : STD_C89) \
372 /* Enum describing the kind of specifiers present in the format and
373 requiring an argument. */
374 enum format_specifier_kind
{
377 CF_KIND_FIELD_PRECISION
380 static const char *kind_descriptions
[] = {
382 N_("field width specifier"),
383 N_("field precision specifier")
386 /* Structure describing details of a type expected in format checking,
387 and the type to check against it. */
388 struct format_wanted_type
390 /* The type wanted. */
392 /* The name of this type to use in diagnostics. */
393 const char *wanted_type_name
;
394 /* Should be type checked just for scalar width identity. */
395 int scalar_identity_flag
;
396 /* The level of indirection through pointers at which this type occurs. */
398 /* Whether, when pointer_count is 1, to allow any character type when
399 pedantic, rather than just the character or void type specified. */
400 int char_lenient_flag
;
401 /* Whether the argument, dereferenced once, is written into and so the
402 argument must not be a pointer to a const-qualified type. */
404 /* Whether the argument, dereferenced once, is read from and so
405 must not be a NULL pointer. */
406 int reading_from_flag
;
407 /* The kind of specifier that this type is used for. */
408 enum format_specifier_kind kind
;
409 /* The starting character of the specifier. This never includes the
410 initial percent sign. */
411 const char *format_start
;
412 /* The length of the specifier. */
414 /* The actual parameter to check against the wanted type. */
416 /* The argument number of that parameter. */
418 /* The offset location of this argument with respect to the format
420 unsigned int offset_loc
;
421 /* The next type to check for this format conversion, or NULL if none. */
422 struct format_wanted_type
*next
;
425 /* Convenience macro for format_length_info meaning unused. */
426 #define NO_FMT NULL, FMT_LEN_none, STD_C89
428 static const format_length_info printf_length_specs
[] =
430 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
431 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
432 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
433 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
434 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
435 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
436 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
437 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
438 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
439 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
440 { NO_FMT
, NO_FMT
, 0 }
443 /* Length specifiers valid for asm_fprintf. */
444 static const format_length_info asm_fprintf_length_specs
[] =
446 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
447 { "w", FMT_LEN_none
, STD_C89
, NO_FMT
, 0 },
448 { NO_FMT
, NO_FMT
, 0 }
451 /* Length specifiers valid for GCC diagnostics. */
452 static const format_length_info gcc_diag_length_specs
[] =
454 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
455 { "w", FMT_LEN_none
, STD_C89
, NO_FMT
, 0 },
456 { NO_FMT
, NO_FMT
, 0 }
459 /* The custom diagnostics all accept the same length specifiers. */
460 #define gcc_tdiag_length_specs gcc_diag_length_specs
461 #define gcc_cdiag_length_specs gcc_diag_length_specs
462 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
464 /* This differs from printf_length_specs only in that "Z" is not accepted. */
465 static const format_length_info scanf_length_specs
[] =
467 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
468 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
469 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
470 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
471 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
472 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
473 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
474 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
475 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
476 { NO_FMT
, NO_FMT
, 0 }
480 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
481 make no sense for a format type not part of any C standard version. */
482 static const format_length_info strfmon_length_specs
[] =
484 /* A GNU extension. */
485 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
486 { NO_FMT
, NO_FMT
, 0 }
490 /* For now, the Fortran front-end routines only use l as length modifier. */
491 static const format_length_info gcc_gfc_length_specs
[] =
493 { "l", FMT_LEN_l
, STD_C89
, NO_FMT
, 0 },
494 { NO_FMT
, NO_FMT
, 0 }
498 static const format_flag_spec printf_flag_specs
[] =
500 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
501 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
502 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
503 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
504 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
505 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
506 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
507 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
508 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
509 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
510 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
514 static const format_flag_pair printf_flag_pairs
[] =
518 { '0', 'p', 1, 'i' },
522 static const format_flag_spec asm_fprintf_flag_specs
[] =
524 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
525 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
526 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
527 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
528 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
529 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
530 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
531 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
532 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
535 static const format_flag_pair asm_fprintf_flag_pairs
[] =
539 { '0', 'p', 1, 'i' },
543 static const format_flag_pair gcc_diag_flag_pairs
[] =
548 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
549 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
550 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
551 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
553 static const format_flag_spec gcc_diag_flag_specs
[] =
555 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
556 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
557 { 'q', 0, 0, 1, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89
},
558 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
559 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
560 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
563 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
564 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
565 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
566 #define gcc_gfc_flag_specs gcc_diag_flag_specs
568 static const format_flag_spec scanf_flag_specs
[] =
570 { '*', 0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
571 { 'a', 0, 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT
},
572 { 'm', 0, 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT
},
573 { 'w', 0, 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
574 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
575 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT
},
576 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT
},
577 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
581 static const format_flag_pair scanf_flag_pairs
[] =
589 static const format_flag_spec strftime_flag_specs
[] =
591 { '_', 0, 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT
},
592 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT
},
593 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT
},
594 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT
},
595 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT
},
596 { 'w', 0, 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
597 { 'E', 0, 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99
},
598 { 'O', 0, 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99
},
599 { 'O', 'o', 0, 0, NULL
, N_("the 'O' modifier"), STD_EXT
},
600 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
604 static const format_flag_pair strftime_flag_pairs
[] =
615 static const format_flag_spec strfmon_flag_specs
[] =
617 { '=', 0, 1, 0, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
618 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89
},
619 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89
},
620 { '(', 0, 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89
},
621 { '!', 0, 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89
},
622 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89
},
623 { 'w', 0, 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
624 { '#', 0, 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
625 { 'p', 0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
626 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
627 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
630 static const format_flag_pair strfmon_flag_pairs
[] =
637 static const format_char_info print_char_table
[] =
639 /* C89 conversion specifiers. */
640 { "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
},
641 { "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
},
642 { "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
},
643 { "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
},
644 { "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
},
645 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
646 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
647 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c", NULL
},
648 { "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
},
649 /* C99 conversion specifiers. */
650 { "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
},
651 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "", NULL
},
652 /* X/Open conversion specifiers. */
653 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
654 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
},
655 /* GNU conversion specifiers. */
656 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
},
657 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
660 static const format_char_info asm_fprintf_char_table
[] =
662 /* C89 conversion specifiers. */
663 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i", NULL
},
664 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
665 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i", NULL
},
666 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
667 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
669 /* asm_fprintf conversion specifiers. */
670 { "O", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
671 { "R", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
672 { "I", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
673 { "L", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
674 { "U", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
675 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", NULL
},
676 { "z", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
677 { "@", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
678 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
681 static const format_char_info gcc_diag_char_table
[] =
683 /* C89 conversion specifiers. */
684 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
685 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
686 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
687 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
688 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
689 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
691 /* Custom conversion specifiers. */
693 /* G requires a "gcall*" argument at runtime. */
694 { "G", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
695 /* K requires a "tree" argument at runtime. */
696 { "K", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
698 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "//cR", NULL
},
699 { "<", 0, STD_C89
, NOARGUMENTS
, "", "<", NULL
},
700 { ">", 0, STD_C89
, NOARGUMENTS
, "", ">", NULL
},
701 { "'" , 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
702 { "R", 0, STD_C89
, NOARGUMENTS
, "", "\\", NULL
},
703 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
704 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
707 static const format_char_info gcc_tdiag_char_table
[] =
709 /* C89 conversion specifiers. */
710 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
711 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
712 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
713 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
714 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
715 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
717 /* Custom conversion specifiers. */
719 /* These will require a "tree" at runtime. */
720 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
721 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
722 { "K", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
724 /* G requires a "gcall*" argument at runtime. */
725 { "G", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
727 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
729 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "/cR", NULL
},
730 { "<", 0, STD_C89
, NOARGUMENTS
, "", "<", NULL
},
731 { ">", 0, STD_C89
, NOARGUMENTS
, "", ">", NULL
},
732 { "'", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
733 { "R", 0, STD_C89
, NOARGUMENTS
, "", "\\", NULL
},
734 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
735 { "Z", 1, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", &gcc_tdiag_char_table
[0] },
736 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
739 static const format_char_info gcc_cdiag_char_table
[] =
741 /* C89 conversion specifiers. */
742 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
743 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
744 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
745 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
746 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
747 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
749 /* Custom conversion specifiers. */
751 /* These will require a "tree" at runtime. */
752 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
753 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
754 { "K", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
756 /* G requires a "gcall*" argument at runtime. */
757 { "G", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
759 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
761 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "/cR", NULL
},
762 { "<", 0, STD_C89
, NOARGUMENTS
, "", "<", NULL
},
763 { ">", 0, STD_C89
, NOARGUMENTS
, "", ">", NULL
},
764 { "'", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
765 { "R", 0, STD_C89
, NOARGUMENTS
, "", "\\", NULL
},
766 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
767 { "Z", 1, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", &gcc_tdiag_char_table
[0] },
768 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
771 static const format_char_info gcc_cxxdiag_char_table
[] =
773 /* C89 conversion specifiers. */
774 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
775 { "ox", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
776 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
777 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
778 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "pq", "cR", NULL
},
779 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "c", NULL
},
781 /* Custom conversion specifiers. */
783 /* These will require a "tree" at runtime. */
784 { "ADFHISTVX",1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "'", NULL
},
785 { "E", 1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "", NULL
},
786 { "K", 1, STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
787 { "v", 0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
789 /* G requires a "gcall*" argument at runtime. */
790 { "G", 1, STD_C89
,{ T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
792 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
793 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
795 { "r", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "/cR", NULL
},
796 { "<", 0, STD_C89
, NOARGUMENTS
, "", "<", NULL
},
797 { ">", 0, STD_C89
, NOARGUMENTS
, "", ">", NULL
},
798 { "'", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
799 { "R", 0, STD_C89
, NOARGUMENTS
, "", "\\", NULL
},
800 { "m", 0, STD_C89
, NOARGUMENTS
, "q", "", NULL
},
801 { "Z", 1, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", &gcc_tdiag_char_table
[0] },
802 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
805 static const format_char_info gcc_gfc_char_table
[] =
807 /* C89 conversion specifiers. */
808 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
809 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
810 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
811 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "cR", NULL
},
813 /* gfc conversion specifiers. */
815 { "C", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
817 /* This will require a "locus" at runtime. */
818 { "L", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "R", NULL
},
820 /* These will require nothing. */
821 { "<>",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
822 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
825 static const format_char_info scan_char_table
[] =
827 /* C89 conversion specifiers. */
828 { "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
},
829 { "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
},
830 { "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
},
831 { "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
},
832 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "cW", NULL
},
833 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW", NULL
},
834 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW[", NULL
},
835 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
836 { "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
},
837 /* C99 conversion specifiers. */
838 { "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
},
839 { "aA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W", NULL
},
840 /* X/Open conversion specifiers. */
841 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "W", NULL
},
842 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "W", NULL
},
843 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
846 static const format_char_info time_char_table
[] =
848 /* C89 conversion specifiers. */
849 { "ABZab", 0, STD_C89
, NOLENGTHS
, "^#", "", NULL
},
850 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3", NULL
},
851 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "", NULL
},
852 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o", NULL
},
853 { "p", 0, STD_C89
, NOLENGTHS
, "#", "", NULL
},
854 { "X", 0, STD_C89
, NOLENGTHS
, "E", "", NULL
},
855 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4", NULL
},
856 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o", NULL
},
857 { "%", 0, STD_C89
, NOLENGTHS
, "", "", NULL
},
858 /* C99 conversion specifiers. */
859 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o", NULL
},
860 { "D", 0, STD_C99
, NOLENGTHS
, "", "2", NULL
},
861 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "", NULL
},
862 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "", NULL
},
863 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o", NULL
},
864 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o", NULL
},
865 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "", NULL
},
866 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o", NULL
},
867 /* GNU conversion specifiers. */
868 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "", NULL
},
869 { "P", 0, STD_EXT
, NOLENGTHS
, "", "", NULL
},
870 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
873 static const format_char_info monetary_char_table
[] =
875 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "", NULL
},
876 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
879 /* This must be in the same order as enum format_type. */
880 static const format_kind_info format_types_orig
[] =
882 { "gnu_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
883 printf_flag_specs
, printf_flag_pairs
,
884 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
885 'w', 0, 'p', 0, 'L', 0,
886 &integer_type_node
, &integer_type_node
888 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
889 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
890 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
891 'w', 0, 'p', 0, 'L', 0,
894 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q+#", NULL
,
895 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
896 FMT_FLAG_ARG_CONVERT
,
897 0, 0, 'p', 0, 'L', 0,
898 NULL
, &integer_type_node
900 { "gcc_tdiag", gcc_tdiag_length_specs
, gcc_tdiag_char_table
, "q+#", NULL
,
901 gcc_tdiag_flag_specs
, gcc_tdiag_flag_pairs
,
902 FMT_FLAG_ARG_CONVERT
,
903 0, 0, 'p', 0, 'L', 0,
904 NULL
, &integer_type_node
906 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q+#", NULL
,
907 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
908 FMT_FLAG_ARG_CONVERT
,
909 0, 0, 'p', 0, 'L', 0,
910 NULL
, &integer_type_node
912 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
913 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
914 FMT_FLAG_ARG_CONVERT
,
915 0, 0, 'p', 0, 'L', 0,
916 NULL
, &integer_type_node
918 { "gcc_gfc", gcc_gfc_length_specs
, gcc_gfc_char_table
, "q+#", NULL
,
919 gcc_gfc_flag_specs
, gcc_gfc_flag_pairs
,
920 FMT_FLAG_ARG_CONVERT
,
924 { "NSString", NULL
, NULL
, NULL
, NULL
,
926 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
929 { "gnu_scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
930 scanf_flag_specs
, scanf_flag_pairs
,
931 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
932 'w', 0, 0, '*', 'L', 'm',
935 { "gnu_strftime", NULL
, time_char_table
, "_-0^#", "EO",
936 strftime_flag_specs
, strftime_flag_pairs
,
937 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0, 0,
940 { "gnu_strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
941 strfmon_flag_specs
, strfmon_flag_pairs
,
942 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L', 0,
947 /* This layer of indirection allows GCC to reassign format_types with
948 new data if necessary, while still allowing the original data to be
950 static const format_kind_info
*format_types
= format_types_orig
;
951 /* We can modify this one. We also add target-specific format types
952 to the end of the array. */
953 static format_kind_info
*dynamic_format_types
;
955 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
957 /* Structure detailing the results of checking a format function call
958 where the format expression may be a conditional expression with
959 many leaves resulting from nested conditional expressions. */
960 struct format_check_results
962 /* Number of leaves of the format argument that could not be checked
963 as they were not string literals. */
964 int number_non_literal
;
965 /* Number of leaves of the format argument that were null pointers or
966 string literals, but had extra format arguments. */
967 int number_extra_args
;
968 location_t extra_arg_loc
;
969 /* Number of leaves of the format argument that were null pointers or
970 string literals, but had extra format arguments and used $ operand
972 int number_dollar_extra_args
;
973 /* Number of leaves of the format argument that were wide string
976 /* Number of leaves of the format argument that were empty strings. */
978 /* Number of leaves of the format argument that were unterminated
980 int number_unterminated
;
981 /* Number of leaves of the format argument that were not counted above. */
983 /* Location of the format string. */
984 location_t format_string_loc
;
987 struct format_check_context
989 format_check_results
*res
;
990 function_format_info
*info
;
992 vec
<location_t
> *arglocs
;
995 /* Return the format name (as specified in the original table) for the format
996 type indicated by format_num. */
998 format_name (int format_num
)
1000 if (format_num
>= 0 && format_num
< n_format_types
)
1001 return format_types
[format_num
].name
;
1005 /* Return the format flags (as specified in the original table) for the format
1006 type indicated by format_num. */
1008 format_flags (int format_num
)
1010 if (format_num
>= 0 && format_num
< n_format_types
)
1011 return format_types
[format_num
].flags
;
1015 static void check_format_info (function_format_info
*, tree
,
1017 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
1018 static void check_format_info_main (format_check_results
*,
1019 function_format_info
*, const char *,
1022 unsigned HOST_WIDE_INT
,
1023 object_allocator
<format_wanted_type
> &,
1026 static void init_dollar_format_checking (int, tree
);
1027 static int maybe_read_dollar_number (const char **, int,
1028 tree
, tree
*, const format_kind_info
*);
1029 static bool avoid_dollar_number (const char *);
1030 static void finish_dollar_format_checking (format_check_results
*, int);
1032 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
1035 static void check_format_types (const substring_loc
&fmt_loc
,
1036 format_wanted_type
*,
1037 const format_kind_info
*fki
,
1038 int offset_to_type_start
,
1039 char conversion_char
,
1040 vec
<location_t
> *arglocs
);
1041 static void format_type_warning (const substring_loc
&fmt_loc
,
1042 source_range
*param_range
,
1043 format_wanted_type
*, tree
,
1045 const format_kind_info
*fki
,
1046 int offset_to_type_start
,
1047 char conversion_char
);
1049 /* Decode a format type from a string, returning the type, or
1050 format_type_error if not valid, in which case the caller should print an
1053 decode_format_type (const char *s
)
1058 s
= convert_format_name_to_system_name (s
);
1060 for (i
= 0; i
< n_format_types
; i
++)
1063 if (!strcmp (s
, format_types
[i
].name
))
1065 alen
= strlen (format_types
[i
].name
);
1066 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1067 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1068 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1071 return format_type_error
;
1075 /* Check the argument list of a call to printf, scanf, etc.
1076 ATTRS are the attributes on the function type. There are NARGS argument
1077 values in the array ARGARRAY.
1078 Also, if -Wsuggest-attribute=format,
1079 warn for calls to vprintf or vscanf in functions with no such format
1080 attribute themselves. */
1083 check_function_format (tree attrs
, int nargs
, tree
*argarray
,
1084 vec
<location_t
> *arglocs
)
1088 /* See if this function has any format attributes. */
1089 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
1091 if (is_attribute_p ("format", TREE_PURPOSE (a
)))
1093 /* Yup; check it. */
1094 function_format_info info
;
1095 decode_format_attr (TREE_VALUE (a
), &info
, /*validated=*/true);
1098 /* FIXME: Rewrite all the internal functions in this file
1099 to use the ARGARRAY directly instead of constructing this
1101 tree params
= NULL_TREE
;
1103 for (i
= nargs
- 1; i
>= 0; i
--)
1104 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
1105 check_format_info (&info
, params
, arglocs
);
1108 /* Attempt to detect whether the current function might benefit
1109 from the format attribute if the called function is decorated
1110 with it. Avoid using calls with string literal formats for
1111 guidance since those are unlikely to be viable candidates. */
1112 if (warn_suggest_attribute_format
&& info
.first_arg_num
== 0
1113 && (format_types
[info
.format_type
].flags
1114 & (int) FMT_FLAG_ARG_CONVERT
)
1115 /* c_strlen will fail for a function parameter but succeed
1116 for a literal or constant array. */
1117 && !c_strlen (argarray
[info
.format_num
- 1], 1))
1120 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1123 if (is_attribute_p ("format", TREE_PURPOSE (c
))
1124 && (decode_format_type (IDENTIFIER_POINTER
1125 (TREE_VALUE (TREE_VALUE (c
))))
1126 == info
.format_type
))
1130 /* Check if the current function has a parameter to which
1131 the format attribute could be attached; if not, it
1132 can't be a candidate for a format attribute, despite
1133 the vprintf-like or vscanf-like call. */
1135 for (args
= DECL_ARGUMENTS (current_function_decl
);
1137 args
= DECL_CHAIN (args
))
1139 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1140 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1145 warning (OPT_Wsuggest_attribute_format
, "function %qD "
1146 "might be a candidate for %qs format attribute",
1147 current_function_decl
,
1148 format_types
[info
.format_type
].name
);
1156 /* Variables used by the checking of $ operand number formats. */
1157 static char *dollar_arguments_used
= NULL
;
1158 static char *dollar_arguments_pointer_p
= NULL
;
1159 static int dollar_arguments_alloc
= 0;
1160 static int dollar_arguments_count
;
1161 static int dollar_first_arg_num
;
1162 static int dollar_max_arg_used
;
1163 static int dollar_format_warned
;
1165 /* Initialize the checking for a format string that may contain $
1166 parameter number specifications; we will need to keep track of whether
1167 each parameter has been used. FIRST_ARG_NUM is the number of the first
1168 argument that is a parameter to the format, or 0 for a vprintf-style
1169 function; PARAMS is the list of arguments starting at this argument. */
1172 init_dollar_format_checking (int first_arg_num
, tree params
)
1174 tree oparams
= params
;
1176 dollar_first_arg_num
= first_arg_num
;
1177 dollar_arguments_count
= 0;
1178 dollar_max_arg_used
= 0;
1179 dollar_format_warned
= 0;
1180 if (first_arg_num
> 0)
1184 dollar_arguments_count
++;
1185 params
= TREE_CHAIN (params
);
1188 if (dollar_arguments_alloc
< dollar_arguments_count
)
1190 free (dollar_arguments_used
);
1191 free (dollar_arguments_pointer_p
);
1192 dollar_arguments_alloc
= dollar_arguments_count
;
1193 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
1194 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
1196 if (dollar_arguments_alloc
)
1198 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1199 if (first_arg_num
> 0)
1205 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1207 params
= TREE_CHAIN (params
);
1215 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1216 is set, it is an error if one is not found; otherwise, it is OK. If
1217 such a number is found, check whether it is within range and mark that
1218 numbered operand as being used for later checking. Returns the operand
1219 number if found and within range, zero if no such number was found and
1220 this is OK, or -1 on error. PARAMS points to the first operand of the
1221 format; PARAM_PTR is made to point to the parameter referred to. If
1222 a $ format is found, *FORMAT is updated to point just after it. */
1225 maybe_read_dollar_number (const char **format
,
1226 int dollar_needed
, tree params
, tree
*param_ptr
,
1227 const format_kind_info
*fki
)
1231 const char *fcp
= *format
;
1232 if (!ISDIGIT (*fcp
))
1236 warning (OPT_Wformat_
, "missing $ operand number in format");
1244 while (ISDIGIT (*fcp
))
1247 nargnum
= 10 * argnum
+ (*fcp
- '0');
1248 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
1257 warning (OPT_Wformat_
, "missing $ operand number in format");
1264 if (pedantic
&& !dollar_format_warned
)
1266 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
1267 C_STD_NAME (STD_EXT
));
1268 dollar_format_warned
= 1;
1270 if (overflow_flag
|| argnum
== 0
1271 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1273 warning (OPT_Wformat_
, "operand number out of range in format");
1276 if (argnum
> dollar_max_arg_used
)
1277 dollar_max_arg_used
= argnum
;
1278 /* For vprintf-style functions we may need to allocate more memory to
1279 track which arguments are used. */
1280 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1283 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1284 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
1286 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
1288 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1289 nalloc
- dollar_arguments_alloc
);
1290 dollar_arguments_alloc
= nalloc
;
1292 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1293 && dollar_arguments_used
[argnum
- 1] == 1)
1295 dollar_arguments_used
[argnum
- 1] = 2;
1296 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
1300 dollar_arguments_used
[argnum
- 1] = 1;
1301 if (dollar_first_arg_num
)
1304 *param_ptr
= params
;
1305 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1306 *param_ptr
= TREE_CHAIN (*param_ptr
);
1308 /* This case shouldn't be caught here. */
1309 gcc_assert (*param_ptr
);
1316 /* Ensure that FORMAT does not start with a decimal number followed by
1317 a $; give a diagnostic and return true if it does, false otherwise. */
1320 avoid_dollar_number (const char *format
)
1322 if (!ISDIGIT (*format
))
1324 while (ISDIGIT (*format
))
1328 warning (OPT_Wformat_
, "$ operand number used after format without operand number");
1335 /* Finish the checking for a format string that used $ operand number formats
1336 instead of non-$ formats. We check for unused operands before used ones
1337 (a serious error, since the implementation of the format function
1338 can't know what types to pass to va_arg to find the later arguments).
1339 and for unused operands at the end of the format (if we know how many
1340 arguments the format had, so not for vprintf). If there were operand
1341 numbers out of range on a non-vprintf-style format, we won't have reached
1342 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1346 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1349 bool found_pointer_gap
= false;
1350 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1352 if (!dollar_arguments_used
[i
])
1354 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1355 || dollar_arguments_pointer_p
[i
]))
1356 found_pointer_gap
= true;
1358 warning_at (res
->format_string_loc
, OPT_Wformat_
,
1359 "format argument %d unused before used argument %d in $-style format",
1360 i
+ 1, dollar_max_arg_used
);
1363 if (found_pointer_gap
1364 || (dollar_first_arg_num
1365 && dollar_max_arg_used
< dollar_arguments_count
))
1367 res
->number_other
--;
1368 res
->number_dollar_extra_args
++;
1373 /* Retrieve the specification for a format flag. SPEC contains the
1374 specifications for format flags for the applicable kind of format.
1375 FLAG is the flag in question. If PREDICATES is NULL, the basic
1376 spec for that flag must be retrieved and must exist. If
1377 PREDICATES is not NULL, it is a string listing possible predicates
1378 for the spec entry; if an entry predicated on any of these is
1379 found, it is returned, otherwise NULL is returned. */
1381 static const format_flag_spec
*
1382 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1385 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1387 if (spec
[i
].flag_char
!= flag
)
1389 if (predicates
!= NULL
)
1391 if (spec
[i
].predicate
!= 0
1392 && strchr (predicates
, spec
[i
].predicate
) != 0)
1395 else if (spec
[i
].predicate
== 0)
1398 gcc_assert (predicates
);
1403 /* Check the argument list of a call to printf, scanf, etc.
1404 INFO points to the function_format_info structure.
1405 PARAMS is the list of argument values. */
1408 check_format_info (function_format_info
*info
, tree params
,
1409 vec
<location_t
> *arglocs
)
1411 format_check_context format_ctx
;
1412 unsigned HOST_WIDE_INT arg_num
;
1414 format_check_results res
;
1415 /* Skip to format argument. If the argument isn't available, there's
1416 no work for us to do; prototype checking will catch the problem. */
1417 for (arg_num
= 1; ; ++arg_num
)
1421 if (arg_num
== info
->format_num
)
1423 params
= TREE_CHAIN (params
);
1425 format_tree
= TREE_VALUE (params
);
1426 params
= TREE_CHAIN (params
);
1427 if (format_tree
== 0)
1430 res
.number_non_literal
= 0;
1431 res
.number_extra_args
= 0;
1432 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1433 res
.number_dollar_extra_args
= 0;
1434 res
.number_wide
= 0;
1435 res
.number_empty
= 0;
1436 res
.number_unterminated
= 0;
1437 res
.number_other
= 0;
1438 res
.format_string_loc
= input_location
;
1440 format_ctx
.res
= &res
;
1441 format_ctx
.info
= info
;
1442 format_ctx
.params
= params
;
1443 format_ctx
.arglocs
= arglocs
;
1445 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1446 format_tree
, arg_num
);
1448 location_t loc
= format_ctx
.res
->format_string_loc
;
1450 if (res
.number_non_literal
> 0)
1452 /* Functions taking a va_list normally pass a non-literal format
1453 string. These functions typically are declared with
1454 first_arg_num == 0, so avoid warning in those cases. */
1455 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1457 /* For strftime-like formats, warn for not checking the format
1458 string; but there are no arguments to check. */
1459 warning_at (loc
, OPT_Wformat_nonliteral
,
1460 "format not a string literal, format string not checked");
1462 else if (info
->first_arg_num
!= 0)
1464 /* If there are no arguments for the format at all, we may have
1465 printf (foo) which is likely to be a security hole. */
1466 while (arg_num
+ 1 < info
->first_arg_num
)
1470 params
= TREE_CHAIN (params
);
1473 if (params
== 0 && warn_format_security
)
1474 warning_at (loc
, OPT_Wformat_security
,
1475 "format not a string literal and no format arguments");
1476 else if (params
== 0 && warn_format_nonliteral
)
1477 warning_at (loc
, OPT_Wformat_nonliteral
,
1478 "format not a string literal and no format arguments");
1480 warning_at (loc
, OPT_Wformat_nonliteral
,
1481 "format not a string literal, argument types not checked");
1485 /* If there were extra arguments to the format, normally warn. However,
1486 the standard does say extra arguments are ignored, so in the specific
1487 case where we have multiple leaves (conditional expressions or
1488 ngettext) allow extra arguments if at least one leaf didn't have extra
1489 arguments, but was otherwise OK (either non-literal or checked OK).
1490 If the format is an empty string, this should be counted similarly to the
1491 case of extra format arguments. */
1492 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1493 && res
.number_other
== 0)
1495 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1496 res
.extra_arg_loc
= loc
;
1497 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1498 "too many arguments for format");
1500 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1501 && res
.number_other
== 0)
1502 warning_at (loc
, OPT_Wformat_extra_args
, "unused arguments in $-style format");
1503 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1504 && res
.number_other
== 0)
1505 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1506 format_types
[info
->format_type
].name
);
1508 if (res
.number_wide
> 0)
1509 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1511 if (res
.number_unterminated
> 0)
1512 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1515 /* Callback from check_function_arguments_recurse to check a
1516 format string. FORMAT_TREE is the format parameter. ARG_NUM
1517 is the number of the format argument. CTX points to a
1518 format_check_context. */
1521 check_format_arg (void *ctx
, tree format_tree
,
1522 unsigned HOST_WIDE_INT arg_num
)
1524 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1525 format_check_results
*res
= format_ctx
->res
;
1526 function_format_info
*info
= format_ctx
->info
;
1527 tree params
= format_ctx
->params
;
1528 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1531 HOST_WIDE_INT offset
;
1532 const char *format_chars
;
1533 tree array_size
= 0;
1536 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1538 if (VAR_P (format_tree
))
1540 /* Pull out a constant value if the front end didn't. */
1541 format_tree
= decl_constant_value (format_tree
);
1542 STRIP_NOPS (format_tree
);
1545 if (integer_zerop (format_tree
))
1547 /* Skip to first argument to check, so we can see if this format
1548 has any arguments (it shouldn't). */
1549 while (arg_num
+ 1 < info
->first_arg_num
)
1553 params
= TREE_CHAIN (params
);
1558 res
->number_other
++;
1561 if (res
->number_extra_args
== 0)
1562 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1564 res
->number_extra_args
++;
1570 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1574 arg0
= TREE_OPERAND (format_tree
, 0);
1575 arg1
= TREE_OPERAND (format_tree
, 1);
1578 if (TREE_CODE (arg1
) == INTEGER_CST
)
1582 res
->number_non_literal
++;
1585 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1586 if (!cst_and_fits_in_hwi (arg1
))
1588 res
->number_non_literal
++;
1591 offset
= int_cst_value (arg1
);
1593 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1595 res
->number_non_literal
++;
1598 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1599 format_tree
= TREE_OPERAND (format_tree
, 0);
1600 if (format_types
[info
->format_type
].flags
1601 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1603 bool objc_str
= (info
->format_type
== gcc_objc_string_format_type
);
1604 /* We cannot examine this string here - but we can check that it is
1606 if (TREE_CODE (format_tree
) != CONST_DECL
1607 || !((objc_str
&& objc_string_ref_type_p (TREE_TYPE (format_tree
)))
1608 || (*targetcm
.string_object_ref_type_p
)
1609 ((const_tree
) TREE_TYPE (format_tree
))))
1611 res
->number_non_literal
++;
1614 /* Skip to first argument to check. */
1615 while (arg_num
+ 1 < info
->first_arg_num
)
1619 params
= TREE_CHAIN (params
);
1622 /* So, we have a valid literal string object and one or more params.
1623 We need to use an external helper to parse the string into format
1624 info. For Objective-C variants we provide the resource within the
1625 objc tree, for target variants, via a hook. */
1627 objc_check_format_arg (format_tree
, params
);
1628 else if (targetcm
.check_string_object_format_arg
)
1629 (*targetcm
.check_string_object_format_arg
) (format_tree
, params
);
1630 /* Else we can't handle it and retire quietly. */
1633 if (TREE_CODE (format_tree
) == ARRAY_REF
1634 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1635 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1636 format_tree
= TREE_OPERAND (format_tree
, 0);
1639 res
->number_non_literal
++;
1642 if (VAR_P (format_tree
)
1643 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1644 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1645 && TREE_CODE (array_init
) == STRING_CST
)
1647 /* Extract the string constant initializer. Note that this may include
1648 a trailing NUL character that is not in the array (e.g.
1649 const char a[3] = "foo";). */
1650 array_size
= DECL_SIZE_UNIT (format_tree
);
1651 format_tree
= array_init
;
1653 if (TREE_CODE (format_tree
) != STRING_CST
)
1655 res
->number_non_literal
++;
1658 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
))) != char_type_node
)
1663 format_chars
= TREE_STRING_POINTER (format_tree
);
1664 format_length
= TREE_STRING_LENGTH (format_tree
);
1665 if (array_size
!= 0)
1667 /* Variable length arrays can't be initialized. */
1668 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1670 if (tree_fits_shwi_p (array_size
))
1672 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1673 if (array_size_value
> 0
1674 && array_size_value
== (int) array_size_value
1675 && format_length
> array_size_value
)
1676 format_length
= array_size_value
;
1681 if (offset
>= format_length
)
1683 res
->number_non_literal
++;
1686 format_chars
+= offset
;
1687 format_length
-= offset
;
1689 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1691 res
->number_unterminated
++;
1694 if (format_length
== 0)
1696 res
->number_empty
++;
1700 /* Skip to first argument to check. */
1701 while (arg_num
+ 1 < info
->first_arg_num
)
1705 params
= TREE_CHAIN (params
);
1708 /* Provisionally increment res->number_other; check_format_info_main
1709 will decrement it if it finds there are extra arguments, but this way
1710 need not adjust it for every return. */
1711 res
->number_other
++;
1712 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1713 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1714 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1717 /* Support class for argument_parser and check_format_info_main.
1718 Tracks any flag characters that have been applied to the
1719 current argument. */
1725 bool has_char_p (char ch
) const;
1726 void add_char (char ch
);
1727 void validate (const format_kind_info
*fki
,
1728 const format_char_info
*fci
,
1729 const format_flag_spec
*flag_specs
,
1730 const char * const format_chars
,
1731 tree format_string_cst
,
1732 location_t format_string_loc
,
1733 const char * const orig_format_chars
,
1736 int get_alloc_flag (const format_kind_info
*fki
);
1737 int assignment_suppression_p (const format_kind_info
*fki
);
1740 char m_flag_chars
[256];
1743 /* Support struct for argument_parser and check_format_info_main.
1744 Encapsulates any length modifier applied to the current argument. */
1746 struct length_modifier
1749 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1750 scalar_identity_flag (0)
1754 length_modifier (const char *chars_
,
1755 enum format_lengths val_
,
1756 enum format_std_version std_
,
1757 int scalar_identity_flag_
)
1758 : chars (chars_
), val (val_
), std (std_
),
1759 scalar_identity_flag (scalar_identity_flag_
)
1764 enum format_lengths val
;
1765 enum format_std_version std
;
1766 int scalar_identity_flag
;
1769 /* Parsing one argument within a format string. */
1771 class argument_parser
1774 argument_parser (function_format_info
*info
, const char *&format_chars
,
1775 tree format_string_cst
,
1776 const char * const orig_format_chars
,
1777 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1778 int &has_operand_number
, tree first_fillin_param
,
1779 object_allocator
<format_wanted_type
> &fwt_pool_
,
1780 vec
<location_t
> *arglocs
);
1782 bool read_any_dollar ();
1784 bool read_format_flags ();
1787 read_any_format_width (tree
¶ms
,
1788 unsigned HOST_WIDE_INT
&arg_num
);
1791 read_any_format_left_precision ();
1794 read_any_format_precision (tree
¶ms
,
1795 unsigned HOST_WIDE_INT
&arg_num
);
1797 void handle_alloc_chars ();
1799 length_modifier
read_any_length_modifier ();
1801 void read_any_other_modifier ();
1803 const format_char_info
*find_format_char_info (char format_char
);
1806 validate_flag_pairs (const format_char_info
*fci
,
1810 give_y2k_warnings (const format_char_info
*fci
,
1813 void parse_any_scan_set (const format_char_info
*fci
);
1815 bool handle_conversions (const format_char_info
*fci
,
1816 const length_modifier
&len_modifier
,
1818 const char *&wanted_type_name
,
1819 unsigned HOST_WIDE_INT
&arg_num
,
1824 check_argument_type (const format_char_info
*fci
,
1825 const length_modifier
&len_modifier
,
1827 const char *&wanted_type_name
,
1828 const bool suppressed
,
1829 unsigned HOST_WIDE_INT
&arg_num
,
1831 const int alloc_flag
,
1832 const char * const format_start
,
1833 const char * const type_start
,
1834 location_t fmt_param_loc
,
1835 char conversion_char
);
1838 const function_format_info
*const info
;
1839 const format_kind_info
* const fki
;
1840 const format_flag_spec
* const flag_specs
;
1841 const char *start_of_this_format
;
1842 const char *&format_chars
;
1843 const tree format_string_cst
;
1844 const char * const orig_format_chars
;
1845 const location_t format_string_loc
;
1846 object_allocator
<format_wanted_type
> &fwt_pool
;
1847 flag_chars_t
&flag_chars
;
1849 tree main_arg_params
;
1850 int &has_operand_number
;
1851 const tree first_fillin_param
;
1852 format_wanted_type width_wanted_type
;
1853 format_wanted_type precision_wanted_type
;
1855 format_wanted_type main_wanted_type
;
1857 format_wanted_type
*first_wanted_type
;
1858 format_wanted_type
*last_wanted_type
;
1859 vec
<location_t
> *arglocs
;
1862 /* flag_chars_t's constructor. */
1864 flag_chars_t::flag_chars_t ()
1866 m_flag_chars
[0] = 0;
1869 /* Has CH been seen as a flag within the current argument? */
1872 flag_chars_t::has_char_p (char ch
) const
1874 return strchr (m_flag_chars
, ch
) != 0;
1877 /* Add CH to the flags seen within the current argument. */
1880 flag_chars_t::add_char (char ch
)
1882 int i
= strlen (m_flag_chars
);
1883 m_flag_chars
[i
++] = ch
;
1884 m_flag_chars
[i
] = 0;
1887 /* Validate the individual flags used, removing any that are invalid. */
1890 flag_chars_t::validate (const format_kind_info
*fki
,
1891 const format_char_info
*fci
,
1892 const format_flag_spec
*flag_specs
,
1893 const char * const format_chars
,
1894 tree format_string_cst
,
1895 location_t format_string_loc
,
1896 const char * const orig_format_chars
,
1902 bool quotflag
= false;
1904 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
1906 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1907 m_flag_chars
[i
], NULL
);
1908 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
1909 if (m_flag_chars
[i
] == fki
->length_code_char
)
1912 /* Remember if a quoting flag is seen. */
1913 quotflag
|= s
->quoting
;
1915 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
1917 format_warning_at_char (format_string_loc
, format_string_cst
,
1918 format_chars
- orig_format_chars
,
1920 "%s used with %<%%%c%> %s format",
1921 _(s
->name
), format_char
, fki
->name
);
1927 const format_flag_spec
*t
;
1928 if (ADJ_STD (s
->std
) > C_STD_VER
)
1929 warning_at (format_string_loc
, OPT_Wformat_
,
1930 "%s does not support %s",
1931 C_STD_NAME (s
->std
), _(s
->long_name
));
1932 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
1933 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
1935 const char *long_name
= (t
->long_name
!= NULL
1938 if (ADJ_STD (t
->std
) > C_STD_VER
)
1939 warning_at (format_string_loc
, OPT_Wformat_
,
1940 "%s does not support %s with"
1941 " the %<%%%c%> %s format",
1942 C_STD_NAME (t
->std
), _(long_name
),
1943 format_char
, fki
->name
);
1947 /* Detect quoting directives used within a quoted sequence, such
1948 as GCC's "%<...%qE". */
1949 if (quoted
&& s
->quoting
)
1951 format_warning_at_char (format_string_loc
, format_string_cst
,
1952 format_chars
- orig_format_chars
- 1,
1954 "%s used within a quoted sequence",
1958 m_flag_chars
[i
- d
] = 0;
1962 && strchr (fci
->flags2
, '\''))
1964 format_warning_at_char (format_string_loc
, format_string_cst
,
1965 format_chars
- orig_format_chars
,
1967 "%qc conversion used unquoted",
1972 /* Determine if an assignment-allocation has been set, requiring
1973 an extra char ** for writing back a dynamically-allocated char *.
1974 This is for handling the optional 'm' character in scanf. */
1977 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
1979 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1980 && has_char_p ('a'))
1982 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
1987 /* Determine if an assignment-suppression character was seen.
1988 ('*' in scanf, for discarding the converted input). */
1991 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
1993 if (fki
->suppression_char
1994 && has_char_p (fki
->suppression_char
))
1999 /* Constructor for argument_parser. Initialize for parsing one
2000 argument within a format string. */
2003 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
2004 tree format_string_cst_
,
2005 const char * const orig_format_chars_
,
2006 location_t format_string_loc_
,
2007 flag_chars_t
&flag_chars_
,
2008 int &has_operand_number_
,
2009 tree first_fillin_param_
,
2010 object_allocator
<format_wanted_type
> &fwt_pool_
,
2011 vec
<location_t
> *arglocs_
)
2013 fki (&format_types
[info
->format_type
]),
2014 flag_specs (fki
->flag_specs
),
2015 start_of_this_format (format_chars_
),
2016 format_chars (format_chars_
),
2017 format_string_cst (format_string_cst_
),
2018 orig_format_chars (orig_format_chars_
),
2019 format_string_loc (format_string_loc_
),
2020 fwt_pool (fwt_pool_
),
2021 flag_chars (flag_chars_
),
2023 main_arg_params (NULL
),
2024 has_operand_number (has_operand_number_
),
2025 first_fillin_param (first_fillin_param_
),
2026 first_wanted_type (NULL
),
2027 last_wanted_type (NULL
),
2032 /* Handle dollars at the start of format arguments, setting up main_arg_params
2035 Return true if format parsing is to continue, false otherwise. */
2038 argument_parser::read_any_dollar ()
2040 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
2042 /* Possibly read a $ operand number at the start of the format.
2043 If one was previously used, one is required here. If one
2044 is not used here, we can't immediately conclude this is a
2045 format without them, since it could be printf %m or scanf %*. */
2047 opnum
= maybe_read_dollar_number (&format_chars
, 0,
2049 &main_arg_params
, fki
);
2054 has_operand_number
= 1;
2055 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
2058 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
2060 if (avoid_dollar_number (format_chars
))
2066 /* Read any format flags, but do not yet validate them beyond removing
2067 duplicates, since in general validation depends on the rest of
2070 Return true if format parsing is to continue, false otherwise. */
2073 argument_parser::read_format_flags ()
2075 while (*format_chars
!= 0
2076 && strchr (fki
->flag_chars
, *format_chars
) != 0)
2078 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2079 *format_chars
, NULL
);
2080 if (flag_chars
.has_char_p (*format_chars
))
2082 format_warning_at_char (format_string_loc
, format_string_cst
,
2083 format_chars
+ 1 - orig_format_chars
,
2085 "repeated %s in format", _(s
->name
));
2088 flag_chars
.add_char (*format_chars
);
2090 if (s
->skip_next_char
)
2093 if (*format_chars
== 0)
2095 warning_at (format_string_loc
, OPT_Wformat_
,
2096 "missing fill character at end of strfmon format");
2106 /* Read any format width, possibly * or *m$.
2108 Return true if format parsing is to continue, false otherwise. */
2112 read_any_format_width (tree
¶ms
,
2113 unsigned HOST_WIDE_INT
&arg_num
)
2115 if (!fki
->width_char
)
2118 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
2120 flag_chars
.add_char (fki
->width_char
);
2121 /* "...a field width...may be indicated by an asterisk.
2122 In this case, an int argument supplies the field width..." */
2124 if (has_operand_number
!= 0)
2127 opnum
= maybe_read_dollar_number (&format_chars
,
2128 has_operand_number
== 1,
2135 has_operand_number
= 1;
2136 arg_num
= opnum
+ info
->first_arg_num
- 1;
2139 has_operand_number
= 0;
2143 if (avoid_dollar_number (format_chars
))
2146 if (info
->first_arg_num
!= 0)
2153 cur_param
= TREE_VALUE (params
);
2154 if (has_operand_number
<= 0)
2156 params
= TREE_CHAIN (params
);
2160 width_wanted_type
.wanted_type
= *fki
->width_type
;
2161 width_wanted_type
.wanted_type_name
= NULL
;
2162 width_wanted_type
.pointer_count
= 0;
2163 width_wanted_type
.char_lenient_flag
= 0;
2164 width_wanted_type
.scalar_identity_flag
= 0;
2165 width_wanted_type
.writing_in_flag
= 0;
2166 width_wanted_type
.reading_from_flag
= 0;
2167 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
2168 width_wanted_type
.format_start
= format_chars
- 1;
2169 width_wanted_type
.format_length
= 1;
2170 width_wanted_type
.param
= cur_param
;
2171 width_wanted_type
.arg_num
= arg_num
;
2172 width_wanted_type
.offset_loc
=
2173 format_chars
- orig_format_chars
;
2174 width_wanted_type
.next
= NULL
;
2175 if (last_wanted_type
!= 0)
2176 last_wanted_type
->next
= &width_wanted_type
;
2177 if (first_wanted_type
== 0)
2178 first_wanted_type
= &width_wanted_type
;
2179 last_wanted_type
= &width_wanted_type
;
2184 /* Possibly read a numeric width. If the width is zero,
2185 we complain if appropriate. */
2186 int non_zero_width_char
= FALSE
;
2187 int found_width
= FALSE
;
2188 while (ISDIGIT (*format_chars
))
2191 if (*format_chars
!= '0')
2192 non_zero_width_char
= TRUE
;
2195 if (found_width
&& !non_zero_width_char
&&
2196 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
2197 warning_at (format_string_loc
, OPT_Wformat_
,
2198 "zero width in %s format", fki
->name
);
2200 flag_chars
.add_char (fki
->width_char
);
2206 /* Read any format left precision (must be a number, not *). */
2208 argument_parser::read_any_format_left_precision ()
2210 if (fki
->left_precision_char
== 0)
2212 if (*format_chars
!= '#')
2216 flag_chars
.add_char (fki
->left_precision_char
);
2217 if (!ISDIGIT (*format_chars
))
2218 format_warning_at_char (format_string_loc
, format_string_cst
,
2219 format_chars
- orig_format_chars
,
2221 "empty left precision in %s format", fki
->name
);
2222 while (ISDIGIT (*format_chars
))
2226 /* Read any format precision, possibly * or *m$.
2228 Return true if format parsing is to continue, false otherwise. */
2232 read_any_format_precision (tree
¶ms
,
2233 unsigned HOST_WIDE_INT
&arg_num
)
2235 if (fki
->precision_char
== 0)
2237 if (*format_chars
!= '.')
2241 flag_chars
.add_char (fki
->precision_char
);
2242 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
2244 /* "...a...precision...may be indicated by an asterisk.
2245 In this case, an int argument supplies the...precision." */
2247 if (has_operand_number
!= 0)
2250 opnum
= maybe_read_dollar_number (&format_chars
,
2251 has_operand_number
== 1,
2258 has_operand_number
= 1;
2259 arg_num
= opnum
+ info
->first_arg_num
- 1;
2262 has_operand_number
= 0;
2266 if (avoid_dollar_number (format_chars
))
2269 if (info
->first_arg_num
!= 0)
2276 cur_param
= TREE_VALUE (params
);
2277 if (has_operand_number
<= 0)
2279 params
= TREE_CHAIN (params
);
2283 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
2284 precision_wanted_type
.wanted_type_name
= NULL
;
2285 precision_wanted_type
.pointer_count
= 0;
2286 precision_wanted_type
.char_lenient_flag
= 0;
2287 precision_wanted_type
.scalar_identity_flag
= 0;
2288 precision_wanted_type
.writing_in_flag
= 0;
2289 precision_wanted_type
.reading_from_flag
= 0;
2290 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
2291 precision_wanted_type
.param
= cur_param
;
2292 precision_wanted_type
.format_start
= format_chars
- 2;
2293 precision_wanted_type
.format_length
= 2;
2294 precision_wanted_type
.arg_num
= arg_num
;
2295 precision_wanted_type
.offset_loc
=
2296 format_chars
- orig_format_chars
;
2297 precision_wanted_type
.next
= NULL
;
2298 if (last_wanted_type
!= 0)
2299 last_wanted_type
->next
= &precision_wanted_type
;
2300 if (first_wanted_type
== 0)
2301 first_wanted_type
= &precision_wanted_type
;
2302 last_wanted_type
= &precision_wanted_type
;
2307 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
2308 && !ISDIGIT (*format_chars
))
2309 format_warning_at_char (format_string_loc
, format_string_cst
,
2310 format_chars
- orig_format_chars
,
2312 "empty precision in %s format", fki
->name
);
2313 while (ISDIGIT (*format_chars
))
2320 /* Parse any assignment-allocation flags, which request an extra
2321 char ** for writing back a dynamically-allocated char *.
2322 This is for handling the optional 'm' character in scanf,
2323 and, before C99, 'a' (for compatibility with a non-standard
2324 GNU libc extension). */
2327 argument_parser::handle_alloc_chars ()
2329 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
2331 flag_chars
.add_char (fki
->alloc_char
);
2335 /* Handle the scanf allocation kludge. */
2336 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2338 if (*format_chars
== 'a' && !flag_isoc99
)
2340 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
2341 || format_chars
[1] == '[')
2343 /* 'a' is used as a flag. */
2344 flag_chars
.add_char ('a');
2351 /* Look for length modifiers within the current format argument,
2352 returning a length_modifier instance describing it (or the
2353 default if one is not found).
2355 Issue warnings about non-standard modifiers. */
2358 argument_parser::read_any_length_modifier ()
2360 length_modifier result
;
2362 const format_length_info
*fli
= fki
->length_char_specs
;
2366 while (fli
->name
!= 0
2367 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
2371 format_chars
+= strlen (fli
->name
);
2372 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
2375 result
= length_modifier (fli
->double_name
, fli
->double_index
,
2376 fli
->double_std
, 0);
2380 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
2381 fli
->scalar_identity_flag
);
2383 flag_chars
.add_char (fki
->length_code_char
);
2387 /* Warn if the length modifier is non-standard. */
2388 if (ADJ_STD (result
.std
) > C_STD_VER
)
2389 warning_at (format_string_loc
, OPT_Wformat_
,
2390 "%s does not support the %qs %s length modifier",
2391 C_STD_NAME (result
.std
), result
.chars
,
2398 /* Read any other modifier (strftime E/O). */
2401 argument_parser::read_any_other_modifier ()
2403 if (fki
->modifier_chars
== NULL
)
2406 while (*format_chars
!= 0
2407 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2409 if (flag_chars
.has_char_p (*format_chars
))
2411 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2412 *format_chars
, NULL
);
2413 format_warning_at_char (format_string_loc
, format_string_cst
,
2414 format_chars
- orig_format_chars
,
2416 "repeated %s in format", _(s
->name
));
2419 flag_chars
.add_char (*format_chars
);
2424 /* Return the format_char_info corresponding to FORMAT_CHAR,
2425 potentially issuing a warning if the format char is
2426 not supported in the C standard version we are checking
2429 Issue a warning and return NULL if it is not found.
2431 Issue warnings about non-standard modifiers. */
2433 const format_char_info
*
2434 argument_parser::find_format_char_info (char format_char
)
2436 const format_char_info
*fci
= fki
->conversion_specs
;
2438 while (fci
->format_chars
!= 0
2439 && strchr (fci
->format_chars
, format_char
) == 0)
2441 if (fci
->format_chars
== 0)
2443 format_warning_at_char (format_string_loc
, format_string_cst
,
2444 format_chars
- orig_format_chars
,
2446 "unknown conversion type character"
2454 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2455 format_warning_at_char (format_string_loc
, format_string_cst
,
2456 format_chars
- orig_format_chars
,
2458 "%s does not support the %<%%%c%> %s format",
2459 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2465 /* Validate the pairs of flags used.
2466 Issue warnings about incompatible combinations of flags. */
2469 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2472 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2474 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2476 const format_flag_spec
*s
, *t
;
2477 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2479 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2481 if (bad_flag_pairs
[i
].predicate
!= 0
2482 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2484 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2485 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2486 if (bad_flag_pairs
[i
].ignored
)
2488 if (bad_flag_pairs
[i
].predicate
!= 0)
2489 warning_at (format_string_loc
, OPT_Wformat_
,
2490 "%s ignored with %s and %<%%%c%> %s format",
2491 _(s
->name
), _(t
->name
), format_char
,
2494 warning_at (format_string_loc
, OPT_Wformat_
,
2495 "%s ignored with %s in %s format",
2496 _(s
->name
), _(t
->name
), fki
->name
);
2500 if (bad_flag_pairs
[i
].predicate
!= 0)
2501 warning_at (format_string_loc
, OPT_Wformat_
,
2502 "use of %s and %s together with %<%%%c%> %s format",
2503 _(s
->name
), _(t
->name
), format_char
,
2506 warning_at (format_string_loc
, OPT_Wformat_
,
2507 "use of %s and %s together in %s format",
2508 _(s
->name
), _(t
->name
), fki
->name
);
2513 /* Give Y2K warnings. */
2516 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2519 if (!warn_format_y2k
)
2523 if (strchr (fci
->flags2
, '4') != 0)
2524 if (flag_chars
.has_char_p ('E'))
2528 else if (strchr (fci
->flags2
, '3') != 0)
2530 else if (strchr (fci
->flags2
, '2') != 0)
2533 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2534 "%<%%%c%> yields only last 2 digits of "
2535 "year in some locales", format_char
);
2536 else if (y2k_level
== 2)
2537 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2538 "%<%%%c%> yields only last 2 digits of year",
2542 /* Parse any "scan sets" enclosed in square brackets, e.g.
2543 for scanf-style calls. */
2546 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2548 if (strchr (fci
->flags2
, '[') == NULL
)
2551 /* Skip over scan set, in case it happens to have '%' in it. */
2552 if (*format_chars
== '^')
2554 /* Find closing bracket; if one is hit immediately, then
2555 it's part of the scan set rather than a terminator. */
2556 if (*format_chars
== ']')
2558 while (*format_chars
&& *format_chars
!= ']')
2560 if (*format_chars
!= ']')
2561 /* The end of the format string was reached. */
2562 format_warning_at_char (format_string_loc
, format_string_cst
,
2563 format_chars
- orig_format_chars
,
2565 "no closing %<]%> for %<%%[%> format");
2568 /* Return true if this argument is to be continued to be parsed,
2569 false to skip to next argument. */
2572 argument_parser::handle_conversions (const format_char_info
*fci
,
2573 const length_modifier
&len_modifier
,
2575 const char *&wanted_type_name
,
2576 unsigned HOST_WIDE_INT
&arg_num
,
2580 enum format_std_version wanted_type_std
;
2582 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2585 wanted_type
= (fci
->types
[len_modifier
.val
].type
2586 ? *fci
->types
[len_modifier
.val
].type
: 0);
2587 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2588 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2589 if (wanted_type
== 0)
2591 format_warning_at_char (format_string_loc
, format_string_cst
,
2592 format_chars
- orig_format_chars
,
2594 "use of %qs length modifier with %qc type"
2595 " character has either no effect"
2596 " or undefined behavior",
2597 len_modifier
.chars
, format_char
);
2598 /* Heuristic: skip one argument when an invalid length/type
2599 combination is encountered. */
2602 params
= TREE_CHAIN (params
);
2606 /* Warn if non-standard, provided it is more non-standard
2607 than the length and type characters that may already
2608 have been warned for. */
2609 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2610 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2612 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2613 format_warning_at_char (format_string_loc
, format_string_cst
,
2614 format_chars
- orig_format_chars
,
2616 "%s does not support the %<%%%s%c%> %s format",
2617 C_STD_NAME (wanted_type_std
),
2619 format_char
, fki
->name
);
2625 /* Check type of argument against desired type.
2627 Return true if format parsing is to continue, false otherwise. */
2631 check_argument_type (const format_char_info
*fci
,
2632 const length_modifier
&len_modifier
,
2634 const char *&wanted_type_name
,
2635 const bool suppressed
,
2636 unsigned HOST_WIDE_INT
&arg_num
,
2638 const int alloc_flag
,
2639 const char * const format_start
,
2640 const char * const type_start
,
2641 location_t fmt_param_loc
,
2642 char conversion_char
)
2644 if (info
->first_arg_num
== 0)
2647 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2650 if (main_arg_num
!= 0)
2653 warning_at (format_string_loc
, OPT_Wformat_
,
2654 "operand number specified with "
2655 "suppressed assignment");
2657 warning_at (format_string_loc
, OPT_Wformat_
,
2658 "operand number specified for format "
2659 "taking no argument");
2664 format_wanted_type
*wanted_type_ptr
;
2666 if (main_arg_num
!= 0)
2668 arg_num
= main_arg_num
;
2669 params
= main_arg_params
;
2674 if (has_operand_number
> 0)
2676 warning_at (format_string_loc
, OPT_Wformat_
,
2677 "missing $ operand number in format");
2681 has_operand_number
= 0;
2684 wanted_type_ptr
= &main_wanted_type
;
2692 cur_param
= TREE_VALUE (params
);
2693 params
= TREE_CHAIN (params
);
2696 wanted_type_ptr
->wanted_type
= wanted_type
;
2697 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2698 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2699 wanted_type_ptr
->char_lenient_flag
= 0;
2700 if (strchr (fci
->flags2
, 'c') != 0)
2701 wanted_type_ptr
->char_lenient_flag
= 1;
2702 wanted_type_ptr
->scalar_identity_flag
= 0;
2703 if (len_modifier
.scalar_identity_flag
)
2704 wanted_type_ptr
->scalar_identity_flag
= 1;
2705 wanted_type_ptr
->writing_in_flag
= 0;
2706 wanted_type_ptr
->reading_from_flag
= 0;
2708 wanted_type_ptr
->writing_in_flag
= 1;
2711 if (strchr (fci
->flags2
, 'W') != 0)
2712 wanted_type_ptr
->writing_in_flag
= 1;
2713 if (strchr (fci
->flags2
, 'R') != 0)
2714 wanted_type_ptr
->reading_from_flag
= 1;
2716 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2717 wanted_type_ptr
->param
= cur_param
;
2718 wanted_type_ptr
->arg_num
= arg_num
;
2719 wanted_type_ptr
->format_start
= format_start
;
2720 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2721 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2722 wanted_type_ptr
->next
= NULL
;
2723 if (last_wanted_type
!= 0)
2724 last_wanted_type
->next
= wanted_type_ptr
;
2725 if (first_wanted_type
== 0)
2726 first_wanted_type
= wanted_type_ptr
;
2727 last_wanted_type
= wanted_type_ptr
;
2732 wanted_type_ptr
= fwt_pool
.allocate ();
2734 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2735 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2740 if (first_wanted_type
!= 0)
2742 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2743 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2744 /* By default, use the end of the range for the caret location. */
2745 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2746 offset_to_format_end
,
2747 offset_to_format_start
, offset_to_format_end
);
2748 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2749 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2750 offset_to_type_start
,
2751 conversion_char
, arglocs
);
2757 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2758 is the NUL-terminated format string (which at this point may contain
2759 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2760 terminating NUL character). ARG_NUM is one less than the number of
2761 the first format argument to check; PARAMS points to that format
2762 argument in the list of arguments. */
2765 check_format_info_main (format_check_results
*res
,
2766 function_format_info
*info
, const char *format_chars
,
2767 location_t fmt_param_loc
, tree format_string_cst
,
2768 int format_length
, tree params
,
2769 unsigned HOST_WIDE_INT arg_num
,
2770 object_allocator
<format_wanted_type
> &fwt_pool
,
2771 vec
<location_t
> *arglocs
)
2773 const char * const orig_format_chars
= format_chars
;
2774 const tree first_fillin_param
= params
;
2776 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
2777 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
2778 const location_t format_string_loc
= res
->format_string_loc
;
2780 /* -1 if no conversions taking an operand have been found; 0 if one has
2781 and it didn't use $; 1 if $ formats are in use. */
2782 int has_operand_number
= -1;
2784 /* Vector of pointers to opening quoting directives (like GCC "%<"). */
2785 auto_vec
<const char*> quotdirs
;
2787 /* Pointers to the most recent color directives (like GCC's "%r or %R").
2788 A starting color directive much be terminated before the end of
2789 the format string. A terminating directive makes no sense without
2790 a prior starting directive. */
2791 const char *color_begin
= NULL
;
2792 const char *color_end
= NULL
;
2794 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
2796 while (*format_chars
!= 0)
2798 if (*format_chars
++ != '%')
2800 if (*format_chars
== 0)
2802 format_warning_at_char (format_string_loc
, format_string_cst
,
2803 format_chars
- orig_format_chars
,
2805 "spurious trailing %<%%%> in format");
2808 if (*format_chars
== '%')
2814 flag_chars_t flag_chars
;
2815 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
2816 orig_format_chars
, format_string_loc
,
2817 flag_chars
, has_operand_number
,
2818 first_fillin_param
, fwt_pool
, arglocs
);
2820 if (!arg_parser
.read_any_dollar ())
2823 if (!arg_parser
.read_format_flags ())
2826 /* Read any format width, possibly * or *m$. */
2827 if (!arg_parser
.read_any_format_width (params
, arg_num
))
2830 /* Read any format left precision (must be a number, not *). */
2831 arg_parser
.read_any_format_left_precision ();
2833 /* Read any format precision, possibly * or *m$. */
2834 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
2837 const char *format_start
= format_chars
;
2839 arg_parser
.handle_alloc_chars ();
2841 /* The rest of the conversion specification is the length modifier
2842 (if any), and the conversion specifier, so this is where the
2843 type information starts. If we need to issue a suggestion
2844 about a type mismatch, then we should preserve everything up
2846 const char *type_start
= format_chars
;
2848 /* Read any length modifier, if this kind of format has them. */
2849 const length_modifier len_modifier
2850 = arg_parser
.read_any_length_modifier ();
2852 /* Read any modifier (strftime E/O). */
2853 arg_parser
.read_any_other_modifier ();
2855 char format_char
= *format_chars
;
2856 if (format_char
== 0
2857 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
2858 && format_char
== '%'))
2860 format_warning_at_char (format_string_loc
, format_string_cst
,
2861 format_chars
- orig_format_chars
,
2863 "conversion lacks type at end of format");
2868 const format_char_info
* const fci
2869 = arg_parser
.find_format_char_info (format_char
);
2873 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
2875 format_string_loc
, orig_format_chars
, format_char
,
2876 quotdirs
.length () > 0);
2878 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
2879 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
2881 /* Diagnose nested or unmatched quoting directives such as GCC's
2882 "%<...%<" and "%>...%>". */
2883 bool quot_begin_p
= strchr (fci
->flags2
, '<');
2884 bool quot_end_p
= strchr (fci
->flags2
, '>');
2886 if (quot_begin_p
&& !quot_end_p
)
2888 if (quotdirs
.length ())
2889 format_warning_at_char (format_string_loc
, format_string_cst
,
2890 format_chars
- orig_format_chars
,
2892 "nested quoting directive");
2893 quotdirs
.safe_push (format_chars
);
2895 else if (!quot_begin_p
&& quot_end_p
)
2897 if (quotdirs
.length ())
2900 format_warning_at_char (format_string_loc
, format_string_cst
,
2901 format_chars
- orig_format_chars
,
2903 "unmatched quoting directive");
2906 bool color_begin_p
= strchr (fci
->flags2
, '/');
2909 color_begin
= format_chars
;
2912 else if (strchr (fci
->flags2
, '\\'))
2915 format_warning_at_char (format_string_loc
, format_string_cst
,
2916 format_chars
- orig_format_chars
,
2918 "%qc directive redundant after prior "
2919 "occurence of the same", format_char
);
2920 else if (!color_begin
)
2921 format_warning_at_char (format_string_loc
, format_string_cst
,
2922 format_chars
- orig_format_chars
,
2924 "unmatched color reset directive");
2925 color_end
= format_chars
;
2928 /* Diagnose directives that shouldn't appear in a quoted sequence.
2929 (They are denoted by a double quote in FLAGS2.) */
2930 if (quotdirs
.length ())
2932 if (strchr (fci
->flags2
, '"'))
2933 format_warning_at_char (format_string_loc
, format_string_cst
,
2934 format_chars
- orig_format_chars
,
2936 "%qc conversion used within a quoted "
2941 /* Validate the pairs of flags used. */
2942 arg_parser
.validate_flag_pairs (fci
, format_char
);
2944 arg_parser
.give_y2k_warnings (fci
, format_char
);
2946 arg_parser
.parse_any_scan_set (fci
);
2948 tree wanted_type
= NULL
;
2949 const char *wanted_type_name
= NULL
;
2951 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
2952 wanted_type
, wanted_type_name
,
2958 arg_parser
.main_wanted_type
.next
= NULL
;
2960 /* Finally. . .check type of argument against desired type! */
2961 if (!arg_parser
.check_argument_type (fci
, len_modifier
,
2962 wanted_type
, wanted_type_name
,
2966 format_start
, type_start
,
2972 if (format_chars
- orig_format_chars
!= format_length
)
2973 format_warning_at_char (format_string_loc
, format_string_cst
,
2974 format_chars
+ 1 - orig_format_chars
,
2975 OPT_Wformat_contains_nul
,
2976 "embedded %<\\0%> in format");
2977 if (info
->first_arg_num
!= 0 && params
!= 0
2978 && has_operand_number
<= 0)
2980 res
->number_other
--;
2981 res
->number_extra_args
++;
2983 if (has_operand_number
> 0)
2984 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
2986 if (quotdirs
.length ())
2987 format_warning_at_char (format_string_loc
, format_string_cst
,
2988 quotdirs
.pop () - orig_format_chars
,
2989 OPT_Wformat_
, "unterminated quoting directive");
2990 if (color_begin
&& !color_end
)
2991 format_warning_at_char (format_string_loc
, format_string_cst
,
2992 color_begin
- orig_format_chars
,
2993 OPT_Wformat_
, "unterminated color directive");
2996 /* Check the argument types from a single format conversion (possibly
2997 including width and precision arguments).
2999 FMT_LOC is the location of the format conversion.
3001 TYPES is a singly-linked list expressing the parts of the format
3002 conversion that expect argument types, and the arguments they
3005 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3006 format string to where type information begins for the conversion
3007 (the length modifier and conversion specifier).
3009 CONVERSION_CHAR is the user-provided conversion specifier.
3013 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3015 then FMT_LOC covers this range:
3017 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3020 and TYPES in this case is a three-entry singly-linked list consisting of:
3021 (1) the check for the field width here:
3022 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3025 (2) the check for the field precision here:
3026 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3029 (3) the check for the length modifier and conversion char here:
3030 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3034 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3037 0000000000111111111122
3038 0123456789012345678901
3039 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3041 | ` CONVERSION_CHAR: 'd'
3042 type starts here. */
3045 check_format_types (const substring_loc
&fmt_loc
,
3046 format_wanted_type
*types
, const format_kind_info
*fki
,
3047 int offset_to_type_start
,
3048 char conversion_char
,
3049 vec
<location_t
> *arglocs
)
3051 for (; types
!= 0; types
= types
->next
)
3061 wanted_type
= types
->wanted_type
;
3062 arg_num
= types
->arg_num
;
3064 /* The following should not occur here. */
3065 gcc_assert (wanted_type
);
3066 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
3068 if (types
->pointer_count
== 0)
3069 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
3071 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
3073 cur_param
= types
->param
;
3076 format_type_warning (fmt_loc
, NULL
, types
, wanted_type
, NULL
, fki
,
3077 offset_to_type_start
, conversion_char
);
3081 cur_type
= TREE_TYPE (cur_param
);
3082 if (cur_type
== error_mark_node
)
3084 orig_cur_type
= cur_type
;
3087 source_range param_range
;
3088 source_range
*param_range_ptr
;
3089 if (EXPR_HAS_LOCATION (cur_param
))
3091 param_range
= EXPR_LOCATION_RANGE (cur_param
);
3092 param_range_ptr
= ¶m_range
;
3096 /* arg_num is 1-based. */
3097 gcc_assert (types
->arg_num
> 0);
3098 location_t param_loc
= (*arglocs
)[types
->arg_num
- 1];
3099 param_range
= get_range_from_loc (line_table
, param_loc
);
3100 param_range_ptr
= ¶m_range
;
3103 param_range_ptr
= NULL
;
3105 STRIP_NOPS (cur_param
);
3107 /* Check the types of any additional pointer arguments
3108 that precede the "real" argument. */
3109 for (i
= 0; i
< types
->pointer_count
; ++i
)
3111 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
3113 cur_type
= TREE_TYPE (cur_type
);
3114 if (cur_type
== error_mark_node
)
3117 /* Check for writing through a NULL pointer. */
3118 if (types
->writing_in_flag
3121 && integer_zerop (cur_param
))
3122 warning (OPT_Wformat_
, "writing through null pointer "
3123 "(argument %d)", arg_num
);
3125 /* Check for reading through a NULL pointer. */
3126 if (types
->reading_from_flag
3129 && integer_zerop (cur_param
))
3130 warning (OPT_Wformat_
, "reading through null pointer "
3131 "(argument %d)", arg_num
);
3133 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
3134 cur_param
= TREE_OPERAND (cur_param
, 0);
3138 /* See if this is an attempt to write into a const type with
3139 scanf or with printf "%n". Note: the writing in happens
3140 at the first indirection only, if for example
3141 void * const * is passed to scanf %p; passing
3142 const void ** is simply passing an incompatible type. */
3143 if (types
->writing_in_flag
3145 && (TYPE_READONLY (cur_type
)
3147 && (CONSTANT_CLASS_P (cur_param
)
3148 || (DECL_P (cur_param
)
3149 && TREE_READONLY (cur_param
))))))
3150 warning (OPT_Wformat_
, "writing into constant object "
3151 "(argument %d)", arg_num
);
3153 /* If there are extra type qualifiers beyond the first
3154 indirection, then this makes the types technically
3158 && (TYPE_READONLY (cur_type
)
3159 || TYPE_VOLATILE (cur_type
)
3160 || TYPE_ATOMIC (cur_type
)
3161 || TYPE_RESTRICT (cur_type
)))
3162 warning (OPT_Wformat_
, "extra type qualifiers in format "
3163 "argument (argument %d)",
3169 format_type_warning (fmt_loc
, param_range_ptr
,
3170 types
, wanted_type
, orig_cur_type
, fki
,
3171 offset_to_type_start
, conversion_char
);
3176 if (i
< types
->pointer_count
)
3179 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
3181 /* Check whether the argument type is a character type. This leniency
3182 only applies to certain formats, flagged with 'c'. */
3183 if (types
->char_lenient_flag
)
3184 char_type_flag
= (cur_type
== char_type_node
3185 || cur_type
== signed_char_type_node
3186 || cur_type
== unsigned_char_type_node
);
3188 /* Check the type of the "real" argument, if there's a type we want. */
3189 if (lang_hooks
.types_compatible_p (wanted_type
, cur_type
))
3191 /* If we want 'void *', allow any pointer type.
3192 (Anything else would already have got a warning.)
3193 With -Wpedantic, only allow pointers to void and to character
3195 if (wanted_type
== void_type_node
3196 && (!pedantic
|| (i
== 1 && char_type_flag
)))
3198 /* Don't warn about differences merely in signedness, unless
3199 -Wpedantic. With -Wpedantic, warn if the type is a pointer
3200 target and not a character type, and for character types at
3201 a second level of indirection. */
3202 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
3203 && TREE_CODE (cur_type
) == INTEGER_TYPE
3204 && ((!pedantic
&& !warn_format_signedness
)
3205 || (i
== 0 && !warn_format_signedness
)
3206 || (i
== 1 && char_type_flag
))
3207 && (TYPE_UNSIGNED (wanted_type
)
3208 ? wanted_type
== c_common_unsigned_type (cur_type
)
3209 : wanted_type
== c_common_signed_type (cur_type
)))
3211 /* Don't warn about differences merely in signedness if we know
3212 that the current type is integer-promoted and its original type
3213 was unsigned such as that it is in the range of WANTED_TYPE. */
3214 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
3215 && TREE_CODE (cur_type
) == INTEGER_TYPE
3216 && warn_format_signedness
3217 && TYPE_UNSIGNED (wanted_type
)
3218 && cur_param
!= NULL_TREE
3219 && TREE_CODE (cur_param
) == NOP_EXPR
)
3221 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
3222 if (TYPE_UNSIGNED (t
)
3223 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
3226 /* Likewise, "signed char", "unsigned char" and "char" are
3227 equivalent but the above test won't consider them equivalent. */
3228 if (wanted_type
== char_type_node
3229 && (!pedantic
|| i
< 2)
3232 if (types
->scalar_identity_flag
3233 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
3234 || (INTEGRAL_TYPE_P (cur_type
)
3235 && INTEGRAL_TYPE_P (wanted_type
)))
3236 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
3238 /* Now we have a type mismatch. */
3239 format_type_warning (fmt_loc
, param_range_ptr
, types
,
3240 wanted_type
, orig_cur_type
, fki
,
3241 offset_to_type_start
, conversion_char
);
3245 /* Given type TYPE, attempt to dereference the type N times
3246 (e.g. from ("int ***", 2) to "int *")
3248 Return the derefenced type, with any qualifiers
3249 such as "const" stripped from the result, or
3250 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3253 deref_n_times (tree type
, int n
)
3257 for (int i
= n
; i
> 0; i
--)
3259 if (TREE_CODE (type
) != POINTER_TYPE
)
3261 type
= TREE_TYPE (type
);
3263 /* Strip off any "const" etc. */
3264 return build_qualified_type (type
, 0);
3267 /* Lookup the format code for FORMAT_LEN within FLI,
3268 returning the string code for expressing it, or NULL
3269 if it is not found. */
3272 get_modifier_for_format_len (const format_length_info
*fli
,
3273 enum format_lengths format_len
)
3275 for (; fli
->name
; fli
++)
3277 if (fli
->index
== format_len
)
3279 if (fli
->double_index
== format_len
)
3280 return fli
->double_name
;
3287 namespace selftest
{
3290 test_get_modifier_for_format_len ()
3293 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
3295 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
3297 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
3299 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
3302 } // namespace selftest
3304 #endif /* CHECKING_P */
3306 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3307 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3308 Wformat type errors where the argument has type ARG_TYPE. */
3311 matching_type_p (tree spec_type
, tree arg_type
)
3313 gcc_assert (spec_type
);
3314 gcc_assert (arg_type
);
3316 /* If any of the types requires structural equality, we can't compare
3317 their canonical types. */
3318 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
3319 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
3322 spec_type
= TYPE_CANONICAL (spec_type
);
3323 arg_type
= TYPE_CANONICAL (arg_type
);
3325 if (TREE_CODE (spec_type
) == INTEGER_TYPE
3326 && TREE_CODE (arg_type
) == INTEGER_TYPE
3327 && (TYPE_UNSIGNED (spec_type
)
3328 ? spec_type
== c_common_unsigned_type (arg_type
)
3329 : spec_type
== c_common_signed_type (arg_type
)))
3332 return spec_type
== arg_type
;
3335 /* Subroutine of get_format_for_type.
3337 Generate a string containing the length modifier and conversion specifier
3338 that should be used to format arguments of type ARG_TYPE within FKI
3339 (effectively the inverse of the checking code).
3341 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3342 is required to use it, for correcting bogus length modifiers.
3343 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3344 that matches ARG_TYPE.
3346 If successful, returns a non-NULL string which should be freed
3348 Otherwise, returns NULL. */
3351 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
3352 char conversion_char
)
3354 gcc_assert (arg_type
);
3356 const format_char_info
*spec
;
3357 for (spec
= &fki
->conversion_specs
[0];
3361 if (conversion_char
)
3362 if (!strchr (spec
->format_chars
, conversion_char
))
3365 tree effective_arg_type
= deref_n_times (arg_type
,
3366 spec
->pointer_count
);
3367 if (!effective_arg_type
)
3369 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
3371 const format_type_detail
*ftd
= &spec
->types
[i
];
3374 if (matching_type_p (*ftd
->type
, effective_arg_type
))
3376 const char *len_modifier
3377 = get_modifier_for_format_len (fki
->length_char_specs
,
3378 (enum format_lengths
)i
);
3382 if (conversion_char
)
3383 /* We found a match, using the given conversion char - the
3384 length modifier was incorrect (or absent).
3385 Provide a suggestion using the conversion char with the
3386 correct length modifier for the type. */
3387 return xasprintf ("%s%c", len_modifier
, conversion_char
);
3389 /* 2nd pass: no match was possible using the user-provided
3390 conversion char, but we do have a match without using it.
3391 Provide a suggestion using the first conversion char
3392 listed for the given type. */
3393 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
3401 /* Generate a string containing the length modifier and conversion specifier
3402 that should be used to format arguments of type ARG_TYPE within FKI
3403 (effectively the inverse of the checking code).
3405 If successful, returns a non-NULL string which should be freed
3407 Otherwise, returns NULL. */
3410 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
3411 char conversion_char
)
3413 gcc_assert (arg_type
);
3414 gcc_assert (conversion_char
);
3416 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3417 If we find one, then presumably the length modifier was incorrect
3419 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
3423 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3424 matching just on the type. */
3425 return get_format_for_type_1 (fki
, arg_type
, '\0');
3428 /* Attempt to get a string for use as a replacement fix-it hint for the
3429 source range in FMT_LOC.
3431 Preserve all of the text within the range of FMT_LOC up to
3432 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3433 length modifier and conversion specifier for ARG_TYPE, attempting
3434 to keep the user-provided CONVERSION_CHAR if possible.
3436 For example, given a long vs long long mismatch for arg5 here:
3438 000000000111111111122222222223333333333|
3439 123456789012345678901234567890123456789` column numbers
3440 0000000000111111111122|
3441 0123456789012345678901` string offsets
3442 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3443 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3445 | ` CONVERSION_CHAR: 'd'
3448 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3449 STRING_CST), where the user provided:
3451 the result (assuming "long" argument 5) should be:
3454 If successful, returns a non-NULL string which should be freed
3456 Otherwise, returns NULL. */
3459 get_corrected_substring (const substring_loc
&fmt_loc
,
3460 format_wanted_type
*type
, tree arg_type
,
3461 const format_kind_info
*fki
,
3462 int offset_to_type_start
, char conversion_char
)
3464 /* Attempt to provide hints for argument types, but not for field widths
3468 if (type
->kind
!= CF_KIND_FORMAT
)
3471 /* Locate the current code within the source range, rejecting
3472 any awkward cases where the format string occupies more than
3474 Lookup the place where the type starts (including any length
3475 modifiers), getting it as the caret location. */
3476 substring_loc
type_loc (fmt_loc
);
3477 type_loc
.set_caret_index (offset_to_type_start
);
3479 location_t fmt_substring_loc
;
3480 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
3484 source_range fmt_substring_range
3485 = get_range_from_loc (line_table
, fmt_substring_loc
);
3487 expanded_location caret
3488 = expand_location_to_spelling_point (fmt_substring_loc
);
3489 expanded_location start
3490 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
3491 expanded_location finish
3492 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
3493 if (caret
.file
!= start
.file
)
3495 if (start
.file
!= finish
.file
)
3497 if (caret
.line
!= start
.line
)
3499 if (start
.line
!= finish
.line
)
3501 if (start
.column
> caret
.column
)
3503 if (start
.column
> finish
.column
)
3505 if (caret
.column
> finish
.column
)
3509 const char *line
= location_get_source_line (start
.file
, start
.line
,
3514 /* If we got this far, then we have the line containing the
3515 existing conversion specification.
3517 Generate a trimmed copy, containing the prefix part of the conversion
3518 specification, up to the (but not including) the length modifier.
3519 In the above example, this would be "%-+*.*". */
3520 const char *current_content
= line
+ start
.column
- 1;
3521 int length_up_to_type
= caret
.column
- start
.column
;
3522 char *prefix
= xstrndup (current_content
, length_up_to_type
);
3524 /* Now attempt to generate a suggestion for the rest of the specification
3525 (length modifier and conversion char), based on ARG_TYPE and
3527 In the above example, this would be "ld". */
3528 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
3529 if (!format_for_type
)
3535 /* Success. Generate the resulting suggestion for the whole range of
3536 FMT_LOC by concatenating the two strings.
3537 In the above example, this would be "%-+*.*ld". */
3538 char *result
= concat (prefix
, format_for_type
, NULL
);
3539 free (format_for_type
);
3544 /* Give a warning about a format argument of different type from that expected.
3545 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3546 is based on the location of the char at TYPE->offset_loc.
3547 If non-NULL, PARAM_RANGE is the source range of the
3548 relevant argument. WANTED_TYPE is the type the argument should have,
3549 possibly stripped of pointer dereferences. The description (such as "field
3550 precision"), the placement in the format string, a possibly more
3551 friendly name of WANTED_TYPE, and the number of pointer dereferences
3552 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3553 or NULL if it is missing.
3555 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3556 format string to where type information begins for the conversion
3557 (the length modifier and conversion specifier).
3558 CONVERSION_CHAR is the user-provided conversion specifier.
3560 For example, given a type mismatch for argument 5 here:
3562 00000000011111111112222222222333333333344444444445555555555|
3563 12345678901234567890123456789012345678901234567890123456789` column numbers
3564 0000000000111111111122|
3565 0123456789012345678901` offsets within STRING_CST
3566 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3567 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3569 | ` CONVERSION_CHAR: 'd' *PARAM_RANGE
3572 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3576 format_type_warning (const substring_loc
&whole_fmt_loc
,
3577 source_range
*param_range
,
3578 format_wanted_type
*type
,
3579 tree wanted_type
, tree arg_type
,
3580 const format_kind_info
*fki
,
3581 int offset_to_type_start
,
3582 char conversion_char
)
3584 enum format_specifier_kind kind
= type
->kind
;
3585 const char *wanted_type_name
= type
->wanted_type_name
;
3586 const char *format_start
= type
->format_start
;
3587 int format_length
= type
->format_length
;
3588 int pointer_count
= type
->pointer_count
;
3589 int arg_num
= type
->arg_num
;
3592 /* If ARG_TYPE is a typedef with a misleading name (for example,
3593 size_t but not the standard size_t expected by printf %zu), avoid
3594 printing the typedef name. */
3595 if (wanted_type_name
3597 && TYPE_NAME (arg_type
)
3598 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
3599 && DECL_NAME (TYPE_NAME (arg_type
))
3600 && !strcmp (wanted_type_name
,
3601 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
3602 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
3603 /* The format type and name exclude any '*' for pointers, so those
3604 must be formatted manually. For all the types we currently have,
3605 this is adequate, but formats taking pointers to functions or
3606 arrays would require the full type to be built up in order to
3607 print it with %T. */
3608 p
= (char *) alloca (pointer_count
+ 2);
3609 if (pointer_count
== 0)
3611 else if (c_dialect_cxx ())
3613 memset (p
, '*', pointer_count
);
3614 p
[pointer_count
] = 0;
3619 memset (p
+ 1, '*', pointer_count
);
3620 p
[pointer_count
+ 1] = 0;
3623 /* WHOLE_FMT_LOC has the caret at the end of the range.
3624 Set the caret to be at the offset from TYPE. Subtract one
3625 from the offset for the same reason as in format_warning_at_char. */
3626 substring_loc
fmt_loc (whole_fmt_loc
);
3627 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
3629 /* Get a string for use as a replacement fix-it hint for the range in
3630 fmt_loc, or NULL. */
3631 char *corrected_substring
3632 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3633 offset_to_type_start
, conversion_char
);
3635 if (wanted_type_name
)
3638 format_warning_at_substring
3639 (fmt_loc
, param_range
,
3640 corrected_substring
, OPT_Wformat_
,
3641 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
3642 "but argument %d has type %qT",
3643 gettext (kind_descriptions
[kind
]),
3644 (kind
== CF_KIND_FORMAT
? "%" : ""),
3645 format_length
, format_start
,
3646 wanted_type_name
, p
, arg_num
, arg_type
);
3648 format_warning_at_substring
3649 (fmt_loc
, param_range
,
3650 corrected_substring
, OPT_Wformat_
,
3651 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
3652 gettext (kind_descriptions
[kind
]),
3653 (kind
== CF_KIND_FORMAT
? "%" : ""),
3654 format_length
, format_start
, wanted_type_name
, p
);
3659 format_warning_at_substring
3660 (fmt_loc
, param_range
,
3661 corrected_substring
, OPT_Wformat_
,
3662 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
3663 "but argument %d has type %qT",
3664 gettext (kind_descriptions
[kind
]),
3665 (kind
== CF_KIND_FORMAT
? "%" : ""),
3666 format_length
, format_start
,
3667 wanted_type
, p
, arg_num
, arg_type
);
3669 format_warning_at_substring
3670 (fmt_loc
, param_range
,
3671 corrected_substring
, OPT_Wformat_
,
3672 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
3673 gettext (kind_descriptions
[kind
]),
3674 (kind
== CF_KIND_FORMAT
? "%" : ""),
3675 format_length
, format_start
, wanted_type
, p
);
3678 free (corrected_substring
);
3682 /* Given a format_char_info array FCI, and a character C, this function
3683 returns the index into the conversion_specs where that specifier's
3684 data is located. The character must exist. */
3686 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
3690 for (i
= 0; fci
->format_chars
; i
++, fci
++)
3691 if (strchr (fci
->format_chars
, c
))
3694 /* We shouldn't be looking for a non-existent specifier. */
3698 /* Given a format_length_info array FLI, and a character C, this
3699 function returns the index into the conversion_specs where that
3700 modifier's data is located. The character must exist. */
3702 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
3706 for (i
= 0; fli
->name
; i
++, fli
++)
3707 if (strchr (fli
->name
, c
))
3710 /* We shouldn't be looking for a non-existent modifier. */
3714 /* Determine the type of HOST_WIDE_INT in the code being compiled for
3715 use in GCC's __asm_fprintf__ custom format attribute. You must
3716 have set dynamic_format_types before calling this function. */
3718 init_dynamic_asm_fprintf_info (void)
3724 format_length_info
*new_asm_fprintf_length_specs
;
3727 /* Find the underlying type for HOST_WIDE_INT. For the %w
3728 length modifier to work, one must have issued: "typedef
3729 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3730 prior to using that modifier. */
3731 hwi
= maybe_get_identifier ("__gcc_host_wide_int__");
3734 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3737 hwi
= identifier_global_value (hwi
);
3738 if (!hwi
|| TREE_CODE (hwi
) != TYPE_DECL
)
3740 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3743 hwi
= DECL_ORIGINAL_TYPE (hwi
);
3745 if (hwi
!= long_integer_type_node
&& hwi
!= long_long_integer_type_node
)
3747 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
3748 " or %<long long%>");
3752 /* Create a new (writable) copy of asm_fprintf_length_specs. */
3753 new_asm_fprintf_length_specs
= (format_length_info
*)
3754 xmemdup (asm_fprintf_length_specs
,
3755 sizeof (asm_fprintf_length_specs
),
3756 sizeof (asm_fprintf_length_specs
));
3758 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3759 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
3760 if (hwi
== long_integer_type_node
)
3761 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
3762 else if (hwi
== long_long_integer_type_node
)
3763 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
3767 /* Assign the new data for use. */
3768 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
3769 new_asm_fprintf_length_specs
;
3773 /* Determine the type of a "locus" in the code being compiled for use
3774 in GCC's __gcc_gfc__ custom format attribute. You must have set
3775 dynamic_format_types before calling this function. */
3777 init_dynamic_gfc_info (void)
3781 static format_char_info
*gfc_fci
;
3783 /* For the GCC __gcc_gfc__ custom format specifier to work, one
3784 must have declared 'locus' prior to using this attribute. If
3785 we haven't seen this declarations then you shouldn't use the
3786 specifier requiring that type. */
3787 if ((locus
= maybe_get_identifier ("locus")))
3789 locus
= identifier_global_value (locus
);
3792 if (TREE_CODE (locus
) != TYPE_DECL
3793 || TREE_TYPE (locus
) == error_mark_node
)
3795 error ("%<locus%> is not defined as a type");
3799 locus
= TREE_TYPE (locus
);
3803 /* Assign the new data for use. */
3805 /* Handle the __gcc_gfc__ format specifics. */
3807 dynamic_format_types
[gcc_gfc_format_type
].conversion_specs
=
3808 gfc_fci
= (format_char_info
*)
3809 xmemdup (gcc_gfc_char_table
,
3810 sizeof (gcc_gfc_char_table
),
3811 sizeof (gcc_gfc_char_table
));
3814 const unsigned i
= find_char_info_specifier_index (gfc_fci
, 'L');
3815 gfc_fci
[i
].types
[0].type
= &locus
;
3816 gfc_fci
[i
].pointer_count
= 1;
3821 /* Determine the types of "tree" and "location_t" in the code being
3822 compiled for use in GCC's diagnostic custom format attributes. You
3823 must have set dynamic_format_types before calling this function. */
3825 init_dynamic_diag_info (void)
3827 /* For the GCC-diagnostics custom format specifiers to work, one
3828 must have declared 'tree' and 'location_t' prior to using those
3829 attributes. If we haven't seen these declarations then
3830 the specifiers requiring these types shouldn't be used.
3831 However we don't force a hard ICE because we may see only one
3832 or the other type. */
3833 if (tree loc
= maybe_get_identifier ("location_t"))
3835 loc
= identifier_global_value (loc
);
3836 if (loc
&& TREE_CODE (loc
) != TYPE_DECL
)
3837 error ("%<location_t%> is not defined as a type");
3840 /* Initialize the global tree node type local to this file. */
3841 if (!local_tree_type_node
3842 || local_tree_type_node
== void_type_node
)
3844 /* We need to grab the underlying 'union tree_node' so peek into
3845 an extra type level. */
3846 if ((local_tree_type_node
= maybe_get_identifier ("tree")))
3848 local_tree_type_node
= identifier_global_value (local_tree_type_node
);
3849 if (local_tree_type_node
)
3851 if (TREE_CODE (local_tree_type_node
) != TYPE_DECL
)
3853 error ("%<tree%> is not defined as a type");
3854 local_tree_type_node
= 0;
3856 else if (TREE_CODE (TREE_TYPE (local_tree_type_node
))
3859 error ("%<tree%> is not defined as a pointer type");
3860 local_tree_type_node
= 0;
3863 local_tree_type_node
=
3864 TREE_TYPE (TREE_TYPE (local_tree_type_node
));
3868 local_tree_type_node
= void_type_node
;
3871 /* Similar to the above but for gcall*. */
3872 if (!local_gcall_ptr_node
3873 || local_gcall_ptr_node
== void_type_node
)
3875 if ((local_gcall_ptr_node
= maybe_get_identifier ("gcall")))
3877 local_gcall_ptr_node
3878 = identifier_global_value (local_gcall_ptr_node
);
3879 if (local_gcall_ptr_node
)
3881 if (TREE_CODE (local_gcall_ptr_node
) != TYPE_DECL
)
3883 error ("%<gcall%> is not defined as a type");
3884 local_gcall_ptr_node
= 0;
3887 local_gcall_ptr_node
= TREE_TYPE (local_gcall_ptr_node
);
3891 local_gcall_ptr_node
= void_type_node
;
3898 static format_length_info
*diag_ls
;
3901 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
3902 length modifier to work, one must have issued: "typedef
3903 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3904 prior to using that modifier. */
3905 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
3907 hwi
= identifier_global_value (hwi
);
3910 if (TREE_CODE (hwi
) != TYPE_DECL
)
3912 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3917 hwi
= DECL_ORIGINAL_TYPE (hwi
);
3919 if (hwi
!= long_integer_type_node
3920 && hwi
!= long_long_integer_type_node
)
3922 error ("%<__gcc_host_wide_int__%> is not defined"
3923 " as %<long%> or %<long long%>");
3930 /* Assign the new data for use. */
3932 /* All the GCC diag formats use the same length specs. */
3934 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
3935 dynamic_format_types
[gcc_tdiag_format_type
].length_char_specs
=
3936 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
3937 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
3938 diag_ls
= (format_length_info
*)
3939 xmemdup (gcc_diag_length_specs
,
3940 sizeof (gcc_diag_length_specs
),
3941 sizeof (gcc_diag_length_specs
));
3944 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3945 i
= find_length_info_modifier_index (diag_ls
, 'w');
3946 if (hwi
== long_integer_type_node
)
3947 diag_ls
[i
].index
= FMT_LEN_l
;
3948 else if (hwi
== long_long_integer_type_node
)
3949 diag_ls
[i
].index
= FMT_LEN_ll
;
3955 /* It's safe to "re-initialize these to the same values. */
3956 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
3957 gcc_diag_char_table
;
3958 dynamic_format_types
[gcc_tdiag_format_type
].conversion_specs
=
3959 gcc_tdiag_char_table
;
3960 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
3961 gcc_cdiag_char_table
;
3962 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
3963 gcc_cxxdiag_char_table
;
3966 #ifdef TARGET_FORMAT_TYPES
3967 extern const format_kind_info TARGET_FORMAT_TYPES
[];
3970 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3971 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
3973 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3974 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3977 /* Attributes such as "printf" are equivalent to those such as
3978 "gnu_printf" unless this is overridden by a target. */
3979 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
3981 { "gnu_printf", "printf" },
3982 { "gnu_scanf", "scanf" },
3983 { "gnu_strftime", "strftime" },
3984 { "gnu_strfmon", "strfmon" },
3988 /* Translate to unified attribute name. This is used in decode_format_type and
3989 decode_format_attr. In attr_name the user specified argument is passed. It
3990 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3991 or the attr_name passed to this function, if there is no matching entry. */
3993 convert_format_name_to_system_name (const char *attr_name
)
3997 if (attr_name
== NULL
|| *attr_name
== 0
3998 || strncmp (attr_name
, "gcc_", 4) == 0)
4000 #ifdef TARGET_OVERRIDES_FORMAT_INIT
4001 TARGET_OVERRIDES_FORMAT_INIT ();
4004 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4005 /* Check if format attribute is overridden by target. */
4006 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
4007 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
4009 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
4011 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
4014 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
4016 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
4020 /* Otherwise default to gnu format. */
4022 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
4025 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
4028 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
4030 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
4036 /* Handle a "format" attribute; arguments as in
4037 struct attribute_spec.handler. */
4039 handle_format_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
4040 int flags
, bool *no_add_attrs
)
4043 function_format_info info
;
4045 #ifdef TARGET_FORMAT_TYPES
4046 /* If the target provides additional format types, we need to
4047 add them to FORMAT_TYPES at first use. */
4048 if (TARGET_FORMAT_TYPES
!= NULL
&& !dynamic_format_types
)
4050 dynamic_format_types
= XNEWVEC (format_kind_info
,
4051 n_format_types
+ TARGET_N_FORMAT_TYPES
);
4052 memcpy (dynamic_format_types
, format_types_orig
,
4053 sizeof (format_types_orig
));
4054 memcpy (&dynamic_format_types
[n_format_types
], TARGET_FORMAT_TYPES
,
4055 TARGET_N_FORMAT_TYPES
* sizeof (dynamic_format_types
[0]));
4057 format_types
= dynamic_format_types
;
4058 /* Provide a reference for the first potential external type. */
4059 first_target_format_type
= n_format_types
;
4060 n_format_types
+= TARGET_N_FORMAT_TYPES
;
4064 /* Canonicalize name of format function. */
4065 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
4066 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
4068 if (!decode_format_attr (args
, &info
, 0))
4070 *no_add_attrs
= true;
4074 if (prototype_p (type
))
4076 if (!check_format_string (type
, info
.format_num
, flags
,
4077 no_add_attrs
, info
.format_type
))
4080 if (info
.first_arg_num
!= 0)
4082 unsigned HOST_WIDE_INT arg_num
= 1;
4083 function_args_iterator iter
;
4086 /* Verify that first_arg_num points to the last arg,
4088 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
4091 if (arg_num
!= info
.first_arg_num
)
4093 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
4094 error ("args to be formatted is not %<...%>");
4095 *no_add_attrs
= true;
4101 /* Check if this is a strftime variant. Just for this variant
4102 FMT_FLAG_ARG_CONVERT is not set. */
4103 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
4104 && info
.first_arg_num
!= 0)
4106 error ("strftime formats cannot format arguments");
4107 *no_add_attrs
= true;
4111 /* If this is a custom GCC-internal format type, we have to
4112 initialize certain bits at runtime. */
4113 if (info
.format_type
== asm_fprintf_format_type
4114 || info
.format_type
== gcc_gfc_format_type
4115 || info
.format_type
== gcc_diag_format_type
4116 || info
.format_type
== gcc_tdiag_format_type
4117 || info
.format_type
== gcc_cdiag_format_type
4118 || info
.format_type
== gcc_cxxdiag_format_type
)
4120 /* Our first time through, we have to make sure that our
4121 format_type data is allocated dynamically and is modifiable. */
4122 if (!dynamic_format_types
)
4123 format_types
= dynamic_format_types
= (format_kind_info
*)
4124 xmemdup (format_types_orig
, sizeof (format_types_orig
),
4125 sizeof (format_types_orig
));
4127 /* If this is format __asm_fprintf__, we have to initialize
4128 GCC's notion of HOST_WIDE_INT for checking %wd. */
4129 if (info
.format_type
== asm_fprintf_format_type
)
4130 init_dynamic_asm_fprintf_info ();
4131 /* If this is format __gcc_gfc__, we have to initialize GCC's
4132 notion of 'locus' at runtime for %L. */
4133 else if (info
.format_type
== gcc_gfc_format_type
)
4134 init_dynamic_gfc_info ();
4135 /* If this is one of the diagnostic attributes, then we have to
4136 initialize 'location_t' and 'tree' at runtime. */
4137 else if (info
.format_type
== gcc_diag_format_type
4138 || info
.format_type
== gcc_tdiag_format_type
4139 || info
.format_type
== gcc_cdiag_format_type
4140 || info
.format_type
== gcc_cxxdiag_format_type
)
4141 init_dynamic_diag_info ();
4151 namespace selftest
{
4153 /* Selftests of location handling. */
4155 /* Get the format_kind_info with the given name. */
4157 static const format_kind_info
*
4158 get_info (const char *name
)
4160 int idx
= decode_format_type (name
);
4161 const format_kind_info
*fki
= &format_types
[idx
];
4162 ASSERT_STREQ (fki
->name
, name
);
4166 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
4167 is EXPECTED_FORMAT. */
4170 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
4171 const char *expected_format
, tree type
,
4172 char conversion_char
)
4175 gcc_assert (expected_format
);
4178 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
4179 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
4180 free (actual_format
);
4183 /* Selftests for get_format_for_type. */
4185 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
4186 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
4187 (TYPE), (CONVERSION_CHAR))
4189 /* Selftest for get_format_for_type for "printf"-style functions. */
4192 test_get_format_for_type_printf ()
4194 const format_kind_info
*fki
= get_info ("gnu_printf");
4195 ASSERT_NE (fki
, NULL
);
4197 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
4198 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
4199 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
4200 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
4201 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
4202 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
4203 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
4204 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
4205 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
4206 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
4207 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
4208 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
4209 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
4210 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
4211 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
4212 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
4213 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
4214 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
4215 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
4216 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
4217 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
4218 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
4219 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
4220 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
4221 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
4222 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
4223 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
4224 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
4227 /* Selftest for get_format_for_type for "scanf"-style functions. */
4230 test_get_format_for_type_scanf ()
4232 const format_kind_info
*fki
= get_info ("gnu_scanf");
4233 ASSERT_NE (fki
, NULL
);
4234 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
4235 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
4236 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
4237 build_pointer_type (long_integer_type_node
), 'd');
4238 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
4239 build_pointer_type (long_unsigned_type_node
), 'u');
4240 ASSERT_FORMAT_FOR_TYPE_STREQ
4241 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
4242 ASSERT_FORMAT_FOR_TYPE_STREQ
4243 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
4244 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
4245 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
4248 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4250 /* Run all of the selftests within this file. */
4255 test_get_modifier_for_format_len ();
4256 test_get_format_for_type_printf ();
4257 test_get_format_for_type_scanf ();
4260 } // namespace selftest
4262 #endif /* CHECKING_P */
4264 #include "gt-c-family-c-format.h"