1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2018 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"
35 #include "selftest-diagnostic.h"
38 #include "gcc-rich-location.h"
40 /* Handle attributes associated with format checking. */
42 /* This must be in the same order as format_types, except for
43 format_type_error. Target-specific format types do not have
44 matching enum values. */
45 enum format_type
{ printf_format_type
, asm_fprintf_format_type
,
46 gcc_diag_format_type
, gcc_tdiag_format_type
,
47 gcc_cdiag_format_type
,
48 gcc_cxxdiag_format_type
, gcc_gfc_format_type
,
49 gcc_dump_printf_format_type
,
50 gcc_objc_string_format_type
,
51 format_type_error
= -1};
53 struct function_format_info
55 int format_type
; /* type of format (printf, scanf, etc.) */
56 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
57 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
60 /* Initialized in init_dynamic_diag_info. */
61 static GTY(()) tree local_tree_type_node
;
62 static GTY(()) tree local_gimple_ptr_node
;
63 static GTY(()) tree locus
;
65 static bool decode_format_attr (tree
, function_format_info
*, int);
66 static int decode_format_type (const char *);
68 static bool check_format_string (tree argument
,
69 unsigned HOST_WIDE_INT format_num
,
70 int flags
, bool *no_add_attrs
,
71 int expected_format_type
);
72 static bool get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
,
74 static const char *convert_format_name_to_system_name (const char *attr_name
);
76 static int first_target_format_type
;
77 static const char *format_name (int format_num
);
78 static int format_flags (int format_num
);
80 /* Emit a warning as per format_warning_va, but construct the substring_loc
81 for the character at offset (CHAR_IDX - 1) within a string constant
82 FORMAT_STRING_CST at FMT_STRING_LOC. */
84 ATTRIBUTE_GCC_DIAG (5,6)
86 format_warning_at_char (location_t fmt_string_loc
, tree format_string_cst
,
87 int char_idx
, int opt
, const char *gmsgid
, ...)
90 va_start (ap
, gmsgid
);
91 tree string_type
= TREE_TYPE (format_string_cst
);
93 /* The callers are of the form:
94 format_warning (format_string_loc, format_string_cst,
95 format_chars - orig_format_chars,
96 where format_chars has already been incremented, so that
97 CHAR_IDX is one character beyond where the warning should
98 be emitted. Fix it. */
101 substring_loc
fmt_loc (fmt_string_loc
, string_type
, char_idx
, char_idx
,
103 bool warned
= format_warning_va (fmt_loc
, NULL
, UNKNOWN_LOCATION
, NULL
,
104 NULL
, opt
, gmsgid
, &ap
);
110 /* Check that we have a pointer to a string suitable for use as a format.
111 The default is to check for a char type.
112 For objective-c dialects, this is extended to include references to string
113 objects validated by objc_string_ref_type_p ().
114 Targets may also provide a string object type that can be used within c and
115 c++ and shared with their respective objective-c dialects. In this case the
116 reference to a format string is checked for validity via a hook.
118 The function returns true if strref points to any string type valid for the
119 language dialect and target. */
122 valid_stringptr_type_p (tree strref
)
124 return (strref
!= NULL
125 && TREE_CODE (strref
) == POINTER_TYPE
126 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref
)) == char_type_node
127 || objc_string_ref_type_p (strref
)
128 || (*targetcm
.string_object_ref_type_p
) ((const_tree
) strref
)));
131 /* Handle a "format_arg" attribute; arguments as in
132 struct attribute_spec.handler. */
134 handle_format_arg_attribute (tree
*node
, tree
ARG_UNUSED (name
),
135 tree args
, int flags
, bool *no_add_attrs
)
138 tree format_num_expr
= TREE_VALUE (args
);
139 unsigned HOST_WIDE_INT format_num
= 0;
141 if (!get_constant (format_num_expr
, &format_num
, 0))
143 error ("format string has invalid operand number");
144 *no_add_attrs
= true;
148 if (prototype_p (type
))
150 /* The format arg can be any string reference valid for the language and
151 target. We cannot be more specific in this case. */
152 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
156 if (!valid_stringptr_type_p (TREE_TYPE (type
)))
158 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
159 error ("function does not return string type");
160 *no_add_attrs
= true;
167 /* Verify that the format_num argument is actually a string reference suitable,
168 for the language dialect and target (in case the format attribute is in
169 error). When we know the specific reference type expected, this is also
172 check_format_string (tree fntype
, unsigned HOST_WIDE_INT format_num
,
173 int flags
, bool *no_add_attrs
, int expected_format_type
)
175 unsigned HOST_WIDE_INT i
;
176 bool is_objc_sref
, is_target_sref
, is_char_ref
;
179 function_args_iterator iter
;
182 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
190 || !valid_stringptr_type_p (ref
))
192 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
193 error ("format string argument is not a string type");
194 *no_add_attrs
= true;
198 /* We only know that we want a suitable string reference. */
199 if (expected_format_type
< 0)
202 /* Now check that the arg matches the expected type. */
204 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
206 fmt_flags
= format_flags (expected_format_type
);
207 is_objc_sref
= is_target_sref
= false;
209 is_objc_sref
= objc_string_ref_type_p (ref
);
211 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
214 return true; /* OK, we expected a char and found one. */
217 /* We expected a char but found an extended string type. */
219 error ("found a %qs reference but the format argument should"
220 " be a string", format_name (gcc_objc_string_format_type
));
222 error ("found a %qT but the format argument should be a string",
224 *no_add_attrs
= true;
229 /* We expect a string object type as the format arg. */
232 error ("format argument should be a %qs reference but"
233 " a string was found", format_name (expected_format_type
));
234 *no_add_attrs
= true;
238 /* We will assert that objective-c will support either its own string type
239 or the target-supplied variant. */
241 is_target_sref
= (*targetcm
.string_object_ref_type_p
) ((const_tree
) ref
);
243 if (expected_format_type
== (int) gcc_objc_string_format_type
244 && (is_objc_sref
|| is_target_sref
))
247 /* We will allow a target string ref to match only itself. */
248 if (first_target_format_type
249 && expected_format_type
>= first_target_format_type
254 error ("format argument should be a %qs reference",
255 format_name (expected_format_type
));
256 *no_add_attrs
= true;
263 /* Verify EXPR is a constant, and store its value.
264 If validated_p is true there should be no errors.
265 Returns true on success, false otherwise. */
267 get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
269 if (!tree_fits_uhwi_p (expr
))
271 gcc_assert (!validated_p
);
275 *value
= TREE_INT_CST_LOW (expr
);
280 /* Decode the arguments to a "format" attribute into a
281 function_format_info structure. It is already known that the list
282 is of the right length. If VALIDATED_P is true, then these
283 attributes have already been validated and must not be erroneous;
284 if false, it will give an error message. Returns true if the
285 attributes are successfully decoded, false otherwise. */
288 decode_format_attr (tree args
, function_format_info
*info
, int validated_p
)
290 tree format_type_id
= TREE_VALUE (args
);
291 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
292 tree first_arg_num_expr
293 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
295 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
297 gcc_assert (!validated_p
);
298 error ("unrecognized format specifier");
303 const char *p
= IDENTIFIER_POINTER (format_type_id
);
305 p
= convert_format_name_to_system_name (p
);
307 info
->format_type
= decode_format_type (p
);
309 if (!c_dialect_objc ()
310 && info
->format_type
== gcc_objc_string_format_type
)
312 gcc_assert (!validated_p
);
313 warning (OPT_Wformat_
, "%qE is only allowed in Objective-C dialects",
315 info
->format_type
= format_type_error
;
319 if (info
->format_type
== format_type_error
)
321 gcc_assert (!validated_p
);
322 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
328 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
330 error ("format string has invalid operand number");
334 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
336 error ("%<...%> has invalid operand number");
340 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
342 gcc_assert (!validated_p
);
343 error ("format string argument follows the args to be formatted");
350 /* Check a call to a format function against a parameter list. */
352 /* The C standard version C++ is treated as equivalent to
353 or inheriting from, for the purpose of format features supported. */
354 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
355 /* The C standard version we are checking formats against when pedantic. */
356 #define C_STD_VER ((int) (c_dialect_cxx () \
357 ? CPLUSPLUS_STD_VER \
360 : (flag_isoc94 ? STD_C94 : STD_C89))))
361 /* The name to give to the standard version we are warning about when
362 pedantic. FEATURE_VER is the version in which the feature warned out
363 appeared, which is higher than C_STD_VER. */
364 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
365 ? (cxx_dialect < cxx11 ? "ISO C++98" \
367 : ((FEATURE_VER) == STD_EXT \
370 /* Adjust a C standard version, which may be STD_C9L, to account for
371 -Wno-long-long. Returns other standard versions unchanged. */
372 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
373 ? (warn_long_long ? STD_C99 : STD_C89) \
376 /* Enum describing the kind of specifiers present in the format and
377 requiring an argument. */
378 enum format_specifier_kind
{
381 CF_KIND_FIELD_PRECISION
384 static const char *kind_descriptions
[] = {
386 N_("field width specifier"),
387 N_("field precision specifier")
390 /* Structure describing details of a type expected in format checking,
391 and the type to check against it. */
392 struct format_wanted_type
394 /* The type wanted. */
396 /* The name of this type to use in diagnostics. */
397 const char *wanted_type_name
;
398 /* Should be type checked just for scalar width identity. */
399 int scalar_identity_flag
;
400 /* The level of indirection through pointers at which this type occurs. */
402 /* Whether, when pointer_count is 1, to allow any character type when
403 pedantic, rather than just the character or void type specified. */
404 int char_lenient_flag
;
405 /* Whether the argument, dereferenced once, is written into and so the
406 argument must not be a pointer to a const-qualified type. */
408 /* Whether the argument, dereferenced once, is read from and so
409 must not be a NULL pointer. */
410 int reading_from_flag
;
411 /* The kind of specifier that this type is used for. */
412 enum format_specifier_kind kind
;
413 /* The starting character of the specifier. This never includes the
414 initial percent sign. */
415 const char *format_start
;
416 /* The length of the specifier. */
418 /* The actual parameter to check against the wanted type. */
420 /* The argument number of that parameter. */
422 /* The offset location of this argument with respect to the format
424 unsigned int offset_loc
;
425 /* The next type to check for this format conversion, or NULL if none. */
426 struct format_wanted_type
*next
;
429 /* Convenience macro for format_length_info meaning unused. */
430 #define NO_FMT NULL, FMT_LEN_none, STD_C89
432 static const format_length_info printf_length_specs
[] =
434 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
435 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
436 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
437 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
438 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
439 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
440 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
441 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
442 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
443 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
444 { NO_FMT
, NO_FMT
, 0 }
447 /* Length specifiers valid for asm_fprintf. */
448 static const format_length_info asm_fprintf_length_specs
[] =
450 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
451 { "w", FMT_LEN_none
, STD_C89
, NO_FMT
, 0 },
452 { NO_FMT
, NO_FMT
, 0 }
455 /* Length specifiers valid for GCC diagnostics. */
456 static const format_length_info gcc_diag_length_specs
[] =
458 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
459 { "w", FMT_LEN_none
, STD_C89
, NO_FMT
, 0 },
460 { NO_FMT
, NO_FMT
, 0 }
463 /* The custom diagnostics all accept the same length specifiers. */
464 #define gcc_tdiag_length_specs gcc_diag_length_specs
465 #define gcc_cdiag_length_specs gcc_diag_length_specs
466 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
467 #define gcc_dump_printf_length_specs gcc_diag_length_specs
469 /* This differs from printf_length_specs only in that "Z" is not accepted. */
470 static const format_length_info scanf_length_specs
[] =
472 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
473 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
474 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
475 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
476 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
477 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
478 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
479 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
480 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
481 { NO_FMT
, NO_FMT
, 0 }
485 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
486 make no sense for a format type not part of any C standard version. */
487 static const format_length_info strfmon_length_specs
[] =
489 /* A GNU extension. */
490 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
491 { NO_FMT
, NO_FMT
, 0 }
495 /* For now, the Fortran front-end routines only use l as length modifier. */
496 static const format_length_info gcc_gfc_length_specs
[] =
498 { "l", FMT_LEN_l
, STD_C89
, NO_FMT
, 0 },
499 { NO_FMT
, NO_FMT
, 0 }
503 static const format_flag_spec printf_flag_specs
[] =
505 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
506 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
507 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
508 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
509 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
510 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
511 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
512 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
513 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
514 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
515 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
519 static const format_flag_pair printf_flag_pairs
[] =
523 { '0', 'p', 1, 'i' },
527 static const format_flag_spec asm_fprintf_flag_specs
[] =
529 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
530 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
531 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
532 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
533 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
534 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
535 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
536 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
537 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
540 static const format_flag_pair asm_fprintf_flag_pairs
[] =
544 { '0', 'p', 1, 'i' },
548 static const format_flag_pair gcc_diag_flag_pairs
[] =
553 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
554 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
555 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
556 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
557 #define gcc_dump_printf_flag_pairs gcc_diag_flag_pairs
559 static const format_flag_spec gcc_diag_flag_specs
[] =
561 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
562 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
563 { 'q', 0, 0, 1, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89
},
564 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
565 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
566 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
569 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
570 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
571 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
572 #define gcc_gfc_flag_specs gcc_diag_flag_specs
573 #define gcc_dump_printf_flag_specs gcc_diag_flag_specs
575 static const format_flag_spec scanf_flag_specs
[] =
577 { '*', 0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
578 { 'a', 0, 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT
},
579 { 'm', 0, 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT
},
580 { 'w', 0, 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
581 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
582 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT
},
583 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT
},
584 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
588 static const format_flag_pair scanf_flag_pairs
[] =
596 static const format_flag_spec strftime_flag_specs
[] =
598 { '_', 0, 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT
},
599 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT
},
600 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT
},
601 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT
},
602 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT
},
603 { 'w', 0, 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
604 { 'E', 0, 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99
},
605 { 'O', 0, 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99
},
606 { 'O', 'o', 0, 0, NULL
, N_("the 'O' modifier"), STD_EXT
},
607 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
611 static const format_flag_pair strftime_flag_pairs
[] =
622 static const format_flag_spec strfmon_flag_specs
[] =
624 { '=', 0, 1, 0, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
625 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89
},
626 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89
},
627 { '(', 0, 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89
},
628 { '!', 0, 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89
},
629 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89
},
630 { 'w', 0, 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
631 { '#', 0, 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
632 { 'p', 0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
633 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
634 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
637 static const format_flag_pair strfmon_flag_pairs
[] =
644 static const format_char_info print_char_table
[] =
646 /* C89 conversion specifiers. */
647 { "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
},
648 { "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
},
649 { "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
},
650 { "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
},
651 { "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
},
652 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
653 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
654 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c", NULL
},
655 { "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
},
656 /* C99 conversion specifiers. */
657 { "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
},
658 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "", NULL
},
659 /* X/Open conversion specifiers. */
660 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
661 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
},
662 /* GNU conversion specifiers. */
663 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
},
664 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
667 static const format_char_info asm_fprintf_char_table
[] =
669 /* C89 conversion specifiers. */
670 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i", NULL
},
671 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
672 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i", NULL
},
673 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
674 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
676 /* asm_fprintf conversion specifiers. */
677 { "O", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
678 { "R", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
679 { "I", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
680 { "L", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
681 { "U", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
682 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", NULL
},
683 { "z", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
684 { "@", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
685 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
688 /* GCC-specific format_char_info arrays. */
690 /* The conversion specifiers implemented within pp_format, and thus supported
691 by all pretty_printer instances within GCC. */
693 #define PP_FORMAT_CHAR_TABLE \
694 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
695 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
696 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
697 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
698 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL }, \
699 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL }, \
700 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "//cR", NULL }, \
701 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL }, \
702 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL }, \
703 { "'" , 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
704 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL }, \
705 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL }, \
706 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_diag_char_table[0] }
708 static const format_char_info gcc_diag_char_table
[] =
710 /* The conversion specifiers implemented within pp_format. */
711 PP_FORMAT_CHAR_TABLE
,
713 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
716 static const format_char_info gcc_tdiag_char_table
[] =
718 /* The conversion specifiers implemented within pp_format. */
719 PP_FORMAT_CHAR_TABLE
,
721 /* Custom conversion specifiers implemented by default_tree_printer. */
723 /* These will require a "tree" at runtime. */
724 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
725 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
726 { "K", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
728 /* G requires a "gimple*" argument at runtime. */
729 { "G", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
731 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
734 static const format_char_info gcc_cdiag_char_table
[] =
736 /* The conversion specifiers implemented within pp_format. */
737 PP_FORMAT_CHAR_TABLE
,
739 /* Custom conversion specifiers implemented by c_tree_printer. */
741 /* These will require a "tree" at runtime. */
742 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
743 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
744 { "K", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
746 /* G requires a "gimple*" argument at runtime. */
747 { "G", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
749 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
751 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
754 static const format_char_info gcc_cxxdiag_char_table
[] =
756 /* The conversion specifiers implemented within pp_format. */
757 PP_FORMAT_CHAR_TABLE
,
759 /* Custom conversion specifiers implemented by cp_printer. */
761 /* These will require a "tree" at runtime. */
762 { "ADFHISTVX",1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "'", NULL
},
763 { "E", 1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "", NULL
},
764 { "K", 1, STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
766 /* G requires a "gimple*" argument at runtime. */
767 { "G", 1, STD_C89
,{ T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
769 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
770 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
772 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
775 static const format_char_info gcc_gfc_char_table
[] =
777 /* C89 conversion specifiers. */
778 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
779 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
780 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
781 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "cR", NULL
},
783 /* gfc conversion specifiers. */
785 { "C", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
787 /* This will require a "locus" at runtime. */
788 { "L", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "R", NULL
},
790 /* These will require nothing. */
791 { "<>",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
792 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
795 static const format_char_info gcc_dump_printf_char_table
[] =
797 /* The conversion specifiers implemented within pp_format. */
798 PP_FORMAT_CHAR_TABLE
,
800 /* Custom conversion specifiers implemented by dump_pretty_printer. */
802 /* E and G require a "gimple *" argument at runtime. */
803 { "EG", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
805 /* T requires a "tree" at runtime. */
806 { "T", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
808 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
811 static const format_char_info scan_char_table
[] =
813 /* C89 conversion specifiers. */
814 { "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
},
815 { "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
},
816 { "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
},
817 { "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
},
818 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "cW", NULL
},
819 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW", NULL
},
820 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW[", NULL
},
821 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
822 { "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
},
823 /* C99 conversion specifiers. */
824 { "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
},
825 { "aA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W", NULL
},
826 /* X/Open conversion specifiers. */
827 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "W", NULL
},
828 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "W", NULL
},
829 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
832 static const format_char_info time_char_table
[] =
834 /* C89 conversion specifiers. */
835 { "ABZab", 0, STD_C89
, NOLENGTHS
, "^#", "", NULL
},
836 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3", NULL
},
837 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "", NULL
},
838 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o", NULL
},
839 { "p", 0, STD_C89
, NOLENGTHS
, "#", "", NULL
},
840 { "X", 0, STD_C89
, NOLENGTHS
, "E", "", NULL
},
841 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4", NULL
},
842 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o", NULL
},
843 { "%", 0, STD_C89
, NOLENGTHS
, "", "", NULL
},
844 /* C99 conversion specifiers. */
845 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o", NULL
},
846 { "D", 0, STD_C99
, NOLENGTHS
, "", "2", NULL
},
847 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "", NULL
},
848 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "", NULL
},
849 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o", NULL
},
850 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o", NULL
},
851 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "", NULL
},
852 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o", NULL
},
853 /* GNU conversion specifiers. */
854 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "", NULL
},
855 { "P", 0, STD_EXT
, NOLENGTHS
, "", "", NULL
},
856 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
859 static const format_char_info monetary_char_table
[] =
861 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "", NULL
},
862 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
865 /* This must be in the same order as enum format_type. */
866 static const format_kind_info format_types_orig
[] =
868 { "gnu_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
869 printf_flag_specs
, printf_flag_pairs
,
870 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
871 'w', 0, 'p', 0, 'L', 0,
872 &integer_type_node
, &integer_type_node
874 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
875 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
876 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
877 'w', 0, 'p', 0, 'L', 0,
880 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q+#", NULL
,
881 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
882 FMT_FLAG_ARG_CONVERT
,
883 0, 0, 'p', 0, 'L', 0,
884 NULL
, &integer_type_node
886 { "gcc_tdiag", gcc_tdiag_length_specs
, gcc_tdiag_char_table
, "q+#", NULL
,
887 gcc_tdiag_flag_specs
, gcc_tdiag_flag_pairs
,
888 FMT_FLAG_ARG_CONVERT
,
889 0, 0, 'p', 0, 'L', 0,
890 NULL
, &integer_type_node
892 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q+#", NULL
,
893 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
894 FMT_FLAG_ARG_CONVERT
,
895 0, 0, 'p', 0, 'L', 0,
896 NULL
, &integer_type_node
898 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
899 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
900 FMT_FLAG_ARG_CONVERT
,
901 0, 0, 'p', 0, 'L', 0,
902 NULL
, &integer_type_node
904 { "gcc_gfc", gcc_gfc_length_specs
, gcc_gfc_char_table
, "q+#", NULL
,
905 gcc_gfc_flag_specs
, gcc_gfc_flag_pairs
,
906 FMT_FLAG_ARG_CONVERT
,
910 { "gcc_dump_printf", gcc_dump_printf_length_specs
,
911 gcc_dump_printf_char_table
, "q+#", NULL
,
912 gcc_dump_printf_flag_specs
, gcc_dump_printf_flag_pairs
,
913 FMT_FLAG_ARG_CONVERT
,
914 0, 0, 'p', 0, 'L', 0,
915 NULL
, &integer_type_node
917 { "NSString", NULL
, NULL
, NULL
, NULL
,
919 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
922 { "gnu_scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
923 scanf_flag_specs
, scanf_flag_pairs
,
924 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
925 'w', 0, 0, '*', 'L', 'm',
928 { "gnu_strftime", NULL
, time_char_table
, "_-0^#", "EO",
929 strftime_flag_specs
, strftime_flag_pairs
,
930 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0, 0,
933 { "gnu_strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
934 strfmon_flag_specs
, strfmon_flag_pairs
,
935 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L', 0,
940 /* This layer of indirection allows GCC to reassign format_types with
941 new data if necessary, while still allowing the original data to be
943 static const format_kind_info
*format_types
= format_types_orig
;
944 /* We can modify this one. We also add target-specific format types
945 to the end of the array. */
946 static format_kind_info
*dynamic_format_types
;
948 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
950 /* Structure detailing the results of checking a format function call
951 where the format expression may be a conditional expression with
952 many leaves resulting from nested conditional expressions. */
953 struct format_check_results
955 /* Number of leaves of the format argument that could not be checked
956 as they were not string literals. */
957 int number_non_literal
;
958 /* Number of leaves of the format argument that were null pointers or
959 string literals, but had extra format arguments. */
960 int number_extra_args
;
961 location_t extra_arg_loc
;
962 /* Number of leaves of the format argument that were null pointers or
963 string literals, but had extra format arguments and used $ operand
965 int number_dollar_extra_args
;
966 /* Number of leaves of the format argument that were wide string
969 /* Number of leaves of the format argument that are not array of "char". */
971 /* Number of leaves of the format argument that were empty strings. */
973 /* Number of leaves of the format argument that were unterminated
975 int number_unterminated
;
976 /* Number of leaves of the format argument that were not counted above. */
978 /* Location of the format string. */
979 location_t format_string_loc
;
982 struct format_check_context
984 format_check_results
*res
;
985 function_format_info
*info
;
987 vec
<location_t
> *arglocs
;
990 /* Return the format name (as specified in the original table) for the format
991 type indicated by format_num. */
993 format_name (int format_num
)
995 if (format_num
>= 0 && format_num
< n_format_types
)
996 return format_types
[format_num
].name
;
1000 /* Return the format flags (as specified in the original table) for the format
1001 type indicated by format_num. */
1003 format_flags (int format_num
)
1005 if (format_num
>= 0 && format_num
< n_format_types
)
1006 return format_types
[format_num
].flags
;
1010 static void check_format_info (function_format_info
*, tree
,
1012 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
1013 static void check_format_info_main (format_check_results
*,
1014 function_format_info
*, const char *,
1017 unsigned HOST_WIDE_INT
,
1018 object_allocator
<format_wanted_type
> &,
1021 static void init_dollar_format_checking (int, tree
);
1022 static int maybe_read_dollar_number (const char **, int,
1023 tree
, tree
*, const format_kind_info
*);
1024 static bool avoid_dollar_number (const char *);
1025 static void finish_dollar_format_checking (format_check_results
*, int);
1027 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
1030 static void check_format_types (const substring_loc
&fmt_loc
,
1031 format_wanted_type
*,
1032 const format_kind_info
*fki
,
1033 int offset_to_type_start
,
1034 char conversion_char
,
1035 vec
<location_t
> *arglocs
);
1036 static void format_type_warning (const substring_loc
&fmt_loc
,
1037 location_t param_loc
,
1038 format_wanted_type
*, tree
,
1040 const format_kind_info
*fki
,
1041 int offset_to_type_start
,
1042 char conversion_char
);
1044 /* Decode a format type from a string, returning the type, or
1045 format_type_error if not valid, in which case the caller should print an
1048 decode_format_type (const char *s
)
1053 s
= convert_format_name_to_system_name (s
);
1055 for (i
= 0; i
< n_format_types
; i
++)
1058 if (!strcmp (s
, format_types
[i
].name
))
1060 alen
= strlen (format_types
[i
].name
);
1061 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1062 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1063 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1066 return format_type_error
;
1070 /* Check the argument list of a call to printf, scanf, etc.
1071 ATTRS are the attributes on the function type. There are NARGS argument
1072 values in the array ARGARRAY.
1073 Also, if -Wsuggest-attribute=format,
1074 warn for calls to vprintf or vscanf in functions with no such format
1075 attribute themselves. */
1078 check_function_format (tree attrs
, int nargs
, tree
*argarray
,
1079 vec
<location_t
> *arglocs
)
1083 /* See if this function has any format attributes. */
1084 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
1086 if (is_attribute_p ("format", TREE_PURPOSE (a
)))
1088 /* Yup; check it. */
1089 function_format_info info
;
1090 decode_format_attr (TREE_VALUE (a
), &info
, /*validated=*/true);
1093 /* FIXME: Rewrite all the internal functions in this file
1094 to use the ARGARRAY directly instead of constructing this
1096 tree params
= NULL_TREE
;
1098 for (i
= nargs
- 1; i
>= 0; i
--)
1099 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
1100 check_format_info (&info
, params
, arglocs
);
1103 /* Attempt to detect whether the current function might benefit
1104 from the format attribute if the called function is decorated
1105 with it. Avoid using calls with string literal formats for
1106 guidance since those are unlikely to be viable candidates. */
1107 if (warn_suggest_attribute_format
1108 && current_function_decl
!= NULL_TREE
1109 && info
.first_arg_num
== 0
1110 && (format_types
[info
.format_type
].flags
1111 & (int) FMT_FLAG_ARG_CONVERT
)
1112 /* c_strlen will fail for a function parameter but succeed
1113 for a literal or constant array. */
1114 && !c_strlen (argarray
[info
.format_num
- 1], 1))
1117 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1120 if (is_attribute_p ("format", TREE_PURPOSE (c
))
1121 && (decode_format_type (IDENTIFIER_POINTER
1122 (TREE_VALUE (TREE_VALUE (c
))))
1123 == info
.format_type
))
1127 /* Check if the current function has a parameter to which
1128 the format attribute could be attached; if not, it
1129 can't be a candidate for a format attribute, despite
1130 the vprintf-like or vscanf-like call. */
1132 for (args
= DECL_ARGUMENTS (current_function_decl
);
1134 args
= DECL_CHAIN (args
))
1136 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1137 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1142 warning (OPT_Wsuggest_attribute_format
, "function %qD "
1143 "might be a candidate for %qs format attribute",
1144 current_function_decl
,
1145 format_types
[info
.format_type
].name
);
1153 /* Variables used by the checking of $ operand number formats. */
1154 static char *dollar_arguments_used
= NULL
;
1155 static char *dollar_arguments_pointer_p
= NULL
;
1156 static int dollar_arguments_alloc
= 0;
1157 static int dollar_arguments_count
;
1158 static int dollar_first_arg_num
;
1159 static int dollar_max_arg_used
;
1160 static int dollar_format_warned
;
1162 /* Initialize the checking for a format string that may contain $
1163 parameter number specifications; we will need to keep track of whether
1164 each parameter has been used. FIRST_ARG_NUM is the number of the first
1165 argument that is a parameter to the format, or 0 for a vprintf-style
1166 function; PARAMS is the list of arguments starting at this argument. */
1169 init_dollar_format_checking (int first_arg_num
, tree params
)
1171 tree oparams
= params
;
1173 dollar_first_arg_num
= first_arg_num
;
1174 dollar_arguments_count
= 0;
1175 dollar_max_arg_used
= 0;
1176 dollar_format_warned
= 0;
1177 if (first_arg_num
> 0)
1181 dollar_arguments_count
++;
1182 params
= TREE_CHAIN (params
);
1185 if (dollar_arguments_alloc
< dollar_arguments_count
)
1187 free (dollar_arguments_used
);
1188 free (dollar_arguments_pointer_p
);
1189 dollar_arguments_alloc
= dollar_arguments_count
;
1190 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
1191 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
1193 if (dollar_arguments_alloc
)
1195 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1196 if (first_arg_num
> 0)
1202 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1204 params
= TREE_CHAIN (params
);
1212 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1213 is set, it is an error if one is not found; otherwise, it is OK. If
1214 such a number is found, check whether it is within range and mark that
1215 numbered operand as being used for later checking. Returns the operand
1216 number if found and within range, zero if no such number was found and
1217 this is OK, or -1 on error. PARAMS points to the first operand of the
1218 format; PARAM_PTR is made to point to the parameter referred to. If
1219 a $ format is found, *FORMAT is updated to point just after it. */
1222 maybe_read_dollar_number (const char **format
,
1223 int dollar_needed
, tree params
, tree
*param_ptr
,
1224 const format_kind_info
*fki
)
1228 const char *fcp
= *format
;
1229 if (!ISDIGIT (*fcp
))
1233 warning (OPT_Wformat_
, "missing $ operand number in format");
1241 while (ISDIGIT (*fcp
))
1244 nargnum
= 10 * argnum
+ (*fcp
- '0');
1245 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
1254 warning (OPT_Wformat_
, "missing $ operand number in format");
1261 if (pedantic
&& !dollar_format_warned
)
1263 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
1264 C_STD_NAME (STD_EXT
));
1265 dollar_format_warned
= 1;
1267 if (overflow_flag
|| argnum
== 0
1268 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1270 warning (OPT_Wformat_
, "operand number out of range in format");
1273 if (argnum
> dollar_max_arg_used
)
1274 dollar_max_arg_used
= argnum
;
1275 /* For vprintf-style functions we may need to allocate more memory to
1276 track which arguments are used. */
1277 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1280 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1281 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
1283 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
1285 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1286 nalloc
- dollar_arguments_alloc
);
1287 dollar_arguments_alloc
= nalloc
;
1289 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1290 && dollar_arguments_used
[argnum
- 1] == 1)
1292 dollar_arguments_used
[argnum
- 1] = 2;
1293 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
1297 dollar_arguments_used
[argnum
- 1] = 1;
1298 if (dollar_first_arg_num
)
1301 *param_ptr
= params
;
1302 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1303 *param_ptr
= TREE_CHAIN (*param_ptr
);
1305 /* This case shouldn't be caught here. */
1306 gcc_assert (*param_ptr
);
1313 /* Ensure that FORMAT does not start with a decimal number followed by
1314 a $; give a diagnostic and return true if it does, false otherwise. */
1317 avoid_dollar_number (const char *format
)
1319 if (!ISDIGIT (*format
))
1321 while (ISDIGIT (*format
))
1325 warning (OPT_Wformat_
, "$ operand number used after format without operand number");
1332 /* Finish the checking for a format string that used $ operand number formats
1333 instead of non-$ formats. We check for unused operands before used ones
1334 (a serious error, since the implementation of the format function
1335 can't know what types to pass to va_arg to find the later arguments).
1336 and for unused operands at the end of the format (if we know how many
1337 arguments the format had, so not for vprintf). If there were operand
1338 numbers out of range on a non-vprintf-style format, we won't have reached
1339 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1343 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1346 bool found_pointer_gap
= false;
1347 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1349 if (!dollar_arguments_used
[i
])
1351 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1352 || dollar_arguments_pointer_p
[i
]))
1353 found_pointer_gap
= true;
1355 warning_at (res
->format_string_loc
, OPT_Wformat_
,
1356 "format argument %d unused before used argument %d in $-style format",
1357 i
+ 1, dollar_max_arg_used
);
1360 if (found_pointer_gap
1361 || (dollar_first_arg_num
1362 && dollar_max_arg_used
< dollar_arguments_count
))
1364 res
->number_other
--;
1365 res
->number_dollar_extra_args
++;
1370 /* Retrieve the specification for a format flag. SPEC contains the
1371 specifications for format flags for the applicable kind of format.
1372 FLAG is the flag in question. If PREDICATES is NULL, the basic
1373 spec for that flag must be retrieved and must exist. If
1374 PREDICATES is not NULL, it is a string listing possible predicates
1375 for the spec entry; if an entry predicated on any of these is
1376 found, it is returned, otherwise NULL is returned. */
1378 static const format_flag_spec
*
1379 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1382 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1384 if (spec
[i
].flag_char
!= flag
)
1386 if (predicates
!= NULL
)
1388 if (spec
[i
].predicate
!= 0
1389 && strchr (predicates
, spec
[i
].predicate
) != 0)
1392 else if (spec
[i
].predicate
== 0)
1395 gcc_assert (predicates
);
1400 /* Check the argument list of a call to printf, scanf, etc.
1401 INFO points to the function_format_info structure.
1402 PARAMS is the list of argument values. */
1405 check_format_info (function_format_info
*info
, tree params
,
1406 vec
<location_t
> *arglocs
)
1408 format_check_context format_ctx
;
1409 unsigned HOST_WIDE_INT arg_num
;
1411 format_check_results res
;
1412 /* Skip to format argument. If the argument isn't available, there's
1413 no work for us to do; prototype checking will catch the problem. */
1414 for (arg_num
= 1; ; ++arg_num
)
1418 if (arg_num
== info
->format_num
)
1420 params
= TREE_CHAIN (params
);
1422 format_tree
= TREE_VALUE (params
);
1423 params
= TREE_CHAIN (params
);
1424 if (format_tree
== 0)
1427 res
.number_non_literal
= 0;
1428 res
.number_extra_args
= 0;
1429 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1430 res
.number_dollar_extra_args
= 0;
1431 res
.number_wide
= 0;
1432 res
.number_non_char
= 0;
1433 res
.number_empty
= 0;
1434 res
.number_unterminated
= 0;
1435 res
.number_other
= 0;
1436 res
.format_string_loc
= input_location
;
1438 format_ctx
.res
= &res
;
1439 format_ctx
.info
= info
;
1440 format_ctx
.params
= params
;
1441 format_ctx
.arglocs
= arglocs
;
1443 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1444 format_tree
, arg_num
);
1446 location_t loc
= format_ctx
.res
->format_string_loc
;
1448 if (res
.number_non_literal
> 0)
1450 /* Functions taking a va_list normally pass a non-literal format
1451 string. These functions typically are declared with
1452 first_arg_num == 0, so avoid warning in those cases. */
1453 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1455 /* For strftime-like formats, warn for not checking the format
1456 string; but there are no arguments to check. */
1457 warning_at (loc
, OPT_Wformat_nonliteral
,
1458 "format not a string literal, format string not checked");
1460 else if (info
->first_arg_num
!= 0)
1462 /* If there are no arguments for the format at all, we may have
1463 printf (foo) which is likely to be a security hole. */
1464 while (arg_num
+ 1 < info
->first_arg_num
)
1468 params
= TREE_CHAIN (params
);
1471 if (params
== 0 && warn_format_security
)
1472 warning_at (loc
, OPT_Wformat_security
,
1473 "format not a string literal and no format arguments");
1474 else if (params
== 0 && warn_format_nonliteral
)
1475 warning_at (loc
, OPT_Wformat_nonliteral
,
1476 "format not a string literal and no format arguments");
1478 warning_at (loc
, OPT_Wformat_nonliteral
,
1479 "format not a string literal, argument types not checked");
1483 /* If there were extra arguments to the format, normally warn. However,
1484 the standard does say extra arguments are ignored, so in the specific
1485 case where we have multiple leaves (conditional expressions or
1486 ngettext) allow extra arguments if at least one leaf didn't have extra
1487 arguments, but was otherwise OK (either non-literal or checked OK).
1488 If the format is an empty string, this should be counted similarly to the
1489 case of extra format arguments. */
1490 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1491 && res
.number_other
== 0)
1493 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1494 res
.extra_arg_loc
= loc
;
1495 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1496 "too many arguments for format");
1498 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1499 && res
.number_other
== 0)
1500 warning_at (loc
, OPT_Wformat_extra_args
, "unused arguments in $-style format");
1501 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1502 && res
.number_other
== 0)
1503 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1504 format_types
[info
->format_type
].name
);
1506 if (res
.number_wide
> 0)
1507 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1509 if (res
.number_non_char
> 0)
1510 warning_at (loc
, OPT_Wformat_
,
1511 "format string is not an array of type %qs", "char");
1513 if (res
.number_unterminated
> 0)
1514 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1517 /* Callback from check_function_arguments_recurse to check a
1518 format string. FORMAT_TREE is the format parameter. ARG_NUM
1519 is the number of the format argument. CTX points to a
1520 format_check_context. */
1523 check_format_arg (void *ctx
, tree format_tree
,
1524 unsigned HOST_WIDE_INT arg_num
)
1526 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1527 format_check_results
*res
= format_ctx
->res
;
1528 function_format_info
*info
= format_ctx
->info
;
1529 tree params
= format_ctx
->params
;
1530 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1533 HOST_WIDE_INT offset
;
1534 const char *format_chars
;
1535 tree array_size
= 0;
1538 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1540 /* Pull out a constant value if the front end didn't, and handle location
1542 format_tree
= fold_for_warn (format_tree
);
1543 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 tree underlying_type
1659 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
)));
1660 if (underlying_type
!= char_type_node
)
1662 if (underlying_type
== char16_type_node
1663 || underlying_type
== char32_type_node
1664 || underlying_type
== wchar_type_node
)
1667 res
->number_non_char
++;
1670 format_chars
= TREE_STRING_POINTER (format_tree
);
1671 format_length
= TREE_STRING_LENGTH (format_tree
);
1672 if (array_size
!= 0)
1674 /* Variable length arrays can't be initialized. */
1675 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1677 if (tree_fits_shwi_p (array_size
))
1679 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1680 if (array_size_value
> 0
1681 && array_size_value
== (int) array_size_value
1682 && format_length
> array_size_value
)
1683 format_length
= array_size_value
;
1688 if (offset
>= format_length
)
1690 res
->number_non_literal
++;
1693 format_chars
+= offset
;
1694 format_length
-= offset
;
1696 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1698 res
->number_unterminated
++;
1701 if (format_length
== 0)
1703 res
->number_empty
++;
1707 /* Skip to first argument to check. */
1708 while (arg_num
+ 1 < info
->first_arg_num
)
1712 params
= TREE_CHAIN (params
);
1715 /* Provisionally increment res->number_other; check_format_info_main
1716 will decrement it if it finds there are extra arguments, but this way
1717 need not adjust it for every return. */
1718 res
->number_other
++;
1719 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1720 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1721 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1724 /* Support class for argument_parser and check_format_info_main.
1725 Tracks any flag characters that have been applied to the
1726 current argument. */
1732 bool has_char_p (char ch
) const;
1733 void add_char (char ch
);
1734 void validate (const format_kind_info
*fki
,
1735 const format_char_info
*fci
,
1736 const format_flag_spec
*flag_specs
,
1737 const char * const format_chars
,
1738 tree format_string_cst
,
1739 location_t format_string_loc
,
1740 const char * const orig_format_chars
,
1743 int get_alloc_flag (const format_kind_info
*fki
);
1744 int assignment_suppression_p (const format_kind_info
*fki
);
1747 char m_flag_chars
[256];
1750 /* Support struct for argument_parser and check_format_info_main.
1751 Encapsulates any length modifier applied to the current argument. */
1753 struct length_modifier
1756 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1757 scalar_identity_flag (0)
1761 length_modifier (const char *chars_
,
1762 enum format_lengths val_
,
1763 enum format_std_version std_
,
1764 int scalar_identity_flag_
)
1765 : chars (chars_
), val (val_
), std (std_
),
1766 scalar_identity_flag (scalar_identity_flag_
)
1771 enum format_lengths val
;
1772 enum format_std_version std
;
1773 int scalar_identity_flag
;
1776 /* Parsing one argument within a format string. */
1778 class argument_parser
1781 argument_parser (function_format_info
*info
, const char *&format_chars
,
1782 tree format_string_cst
,
1783 const char * const orig_format_chars
,
1784 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1785 int &has_operand_number
, tree first_fillin_param
,
1786 object_allocator
<format_wanted_type
> &fwt_pool_
,
1787 vec
<location_t
> *arglocs
);
1789 bool read_any_dollar ();
1791 bool read_format_flags ();
1794 read_any_format_width (tree
¶ms
,
1795 unsigned HOST_WIDE_INT
&arg_num
);
1798 read_any_format_left_precision ();
1801 read_any_format_precision (tree
¶ms
,
1802 unsigned HOST_WIDE_INT
&arg_num
);
1804 void handle_alloc_chars ();
1806 length_modifier
read_any_length_modifier ();
1808 void read_any_other_modifier ();
1810 const format_char_info
*find_format_char_info (char format_char
);
1813 validate_flag_pairs (const format_char_info
*fci
,
1817 give_y2k_warnings (const format_char_info
*fci
,
1820 void parse_any_scan_set (const format_char_info
*fci
);
1822 bool handle_conversions (const format_char_info
*fci
,
1823 const length_modifier
&len_modifier
,
1825 const char *&wanted_type_name
,
1826 unsigned HOST_WIDE_INT
&arg_num
,
1831 check_argument_type (const format_char_info
*fci
,
1832 const length_modifier
&len_modifier
,
1834 const char *&wanted_type_name
,
1835 const bool suppressed
,
1836 unsigned HOST_WIDE_INT
&arg_num
,
1838 const int alloc_flag
,
1839 const char * const format_start
,
1840 const char * const type_start
,
1841 location_t fmt_param_loc
,
1842 char conversion_char
);
1845 const function_format_info
*const info
;
1846 const format_kind_info
* const fki
;
1847 const format_flag_spec
* const flag_specs
;
1848 const char *start_of_this_format
;
1849 const char *&format_chars
;
1850 const tree format_string_cst
;
1851 const char * const orig_format_chars
;
1852 const location_t format_string_loc
;
1853 object_allocator
<format_wanted_type
> &fwt_pool
;
1854 flag_chars_t
&flag_chars
;
1856 tree main_arg_params
;
1857 int &has_operand_number
;
1858 const tree first_fillin_param
;
1859 format_wanted_type width_wanted_type
;
1860 format_wanted_type precision_wanted_type
;
1862 format_wanted_type main_wanted_type
;
1864 format_wanted_type
*first_wanted_type
;
1865 format_wanted_type
*last_wanted_type
;
1866 vec
<location_t
> *arglocs
;
1869 /* flag_chars_t's constructor. */
1871 flag_chars_t::flag_chars_t ()
1873 m_flag_chars
[0] = 0;
1876 /* Has CH been seen as a flag within the current argument? */
1879 flag_chars_t::has_char_p (char ch
) const
1881 return strchr (m_flag_chars
, ch
) != 0;
1884 /* Add CH to the flags seen within the current argument. */
1887 flag_chars_t::add_char (char ch
)
1889 int i
= strlen (m_flag_chars
);
1890 m_flag_chars
[i
++] = ch
;
1891 m_flag_chars
[i
] = 0;
1894 /* Validate the individual flags used, removing any that are invalid. */
1897 flag_chars_t::validate (const format_kind_info
*fki
,
1898 const format_char_info
*fci
,
1899 const format_flag_spec
*flag_specs
,
1900 const char * const format_chars
,
1901 tree format_string_cst
,
1902 location_t format_string_loc
,
1903 const char * const orig_format_chars
,
1909 bool quotflag
= false;
1911 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
1913 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1914 m_flag_chars
[i
], NULL
);
1915 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
1916 if (m_flag_chars
[i
] == fki
->length_code_char
)
1919 /* Remember if a quoting flag is seen. */
1920 quotflag
|= s
->quoting
;
1922 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
1924 format_warning_at_char (format_string_loc
, format_string_cst
,
1925 format_chars
- orig_format_chars
,
1927 "%s used with %<%%%c%> %s format",
1928 _(s
->name
), format_char
, fki
->name
);
1934 const format_flag_spec
*t
;
1935 if (ADJ_STD (s
->std
) > C_STD_VER
)
1936 warning_at (format_string_loc
, OPT_Wformat_
,
1937 "%s does not support %s",
1938 C_STD_NAME (s
->std
), _(s
->long_name
));
1939 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
1940 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
1942 const char *long_name
= (t
->long_name
!= NULL
1945 if (ADJ_STD (t
->std
) > C_STD_VER
)
1946 warning_at (format_string_loc
, OPT_Wformat_
,
1947 "%s does not support %s with"
1948 " the %<%%%c%> %s format",
1949 C_STD_NAME (t
->std
), _(long_name
),
1950 format_char
, fki
->name
);
1954 /* Detect quoting directives used within a quoted sequence, such
1955 as GCC's "%<...%qE". */
1956 if (quoted
&& s
->quoting
)
1958 format_warning_at_char (format_string_loc
, format_string_cst
,
1959 format_chars
- orig_format_chars
- 1,
1961 "%s used within a quoted sequence",
1965 m_flag_chars
[i
- d
] = 0;
1969 && strchr (fci
->flags2
, '\''))
1971 format_warning_at_char (format_string_loc
, format_string_cst
,
1972 format_chars
- orig_format_chars
,
1974 "%qc conversion used unquoted",
1979 /* Determine if an assignment-allocation has been set, requiring
1980 an extra char ** for writing back a dynamically-allocated char *.
1981 This is for handling the optional 'm' character in scanf. */
1984 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
1986 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1987 && has_char_p ('a'))
1989 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
1994 /* Determine if an assignment-suppression character was seen.
1995 ('*' in scanf, for discarding the converted input). */
1998 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
2000 if (fki
->suppression_char
2001 && has_char_p (fki
->suppression_char
))
2006 /* Constructor for argument_parser. Initialize for parsing one
2007 argument within a format string. */
2010 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
2011 tree format_string_cst_
,
2012 const char * const orig_format_chars_
,
2013 location_t format_string_loc_
,
2014 flag_chars_t
&flag_chars_
,
2015 int &has_operand_number_
,
2016 tree first_fillin_param_
,
2017 object_allocator
<format_wanted_type
> &fwt_pool_
,
2018 vec
<location_t
> *arglocs_
)
2020 fki (&format_types
[info
->format_type
]),
2021 flag_specs (fki
->flag_specs
),
2022 start_of_this_format (format_chars_
),
2023 format_chars (format_chars_
),
2024 format_string_cst (format_string_cst_
),
2025 orig_format_chars (orig_format_chars_
),
2026 format_string_loc (format_string_loc_
),
2027 fwt_pool (fwt_pool_
),
2028 flag_chars (flag_chars_
),
2030 main_arg_params (NULL
),
2031 has_operand_number (has_operand_number_
),
2032 first_fillin_param (first_fillin_param_
),
2033 first_wanted_type (NULL
),
2034 last_wanted_type (NULL
),
2039 /* Handle dollars at the start of format arguments, setting up main_arg_params
2042 Return true if format parsing is to continue, false otherwise. */
2045 argument_parser::read_any_dollar ()
2047 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
2049 /* Possibly read a $ operand number at the start of the format.
2050 If one was previously used, one is required here. If one
2051 is not used here, we can't immediately conclude this is a
2052 format without them, since it could be printf %m or scanf %*. */
2054 opnum
= maybe_read_dollar_number (&format_chars
, 0,
2056 &main_arg_params
, fki
);
2061 has_operand_number
= 1;
2062 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
2065 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
2067 if (avoid_dollar_number (format_chars
))
2073 /* Read any format flags, but do not yet validate them beyond removing
2074 duplicates, since in general validation depends on the rest of
2077 Return true if format parsing is to continue, false otherwise. */
2080 argument_parser::read_format_flags ()
2082 while (*format_chars
!= 0
2083 && strchr (fki
->flag_chars
, *format_chars
) != 0)
2085 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2086 *format_chars
, NULL
);
2087 if (flag_chars
.has_char_p (*format_chars
))
2089 format_warning_at_char (format_string_loc
, format_string_cst
,
2090 format_chars
+ 1 - orig_format_chars
,
2092 "repeated %s in format", _(s
->name
));
2095 flag_chars
.add_char (*format_chars
);
2097 if (s
->skip_next_char
)
2100 if (*format_chars
== 0)
2102 warning_at (format_string_loc
, OPT_Wformat_
,
2103 "missing fill character at end of strfmon format");
2113 /* Read any format width, possibly * or *m$.
2115 Return true if format parsing is to continue, false otherwise. */
2119 read_any_format_width (tree
¶ms
,
2120 unsigned HOST_WIDE_INT
&arg_num
)
2122 if (!fki
->width_char
)
2125 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
2127 flag_chars
.add_char (fki
->width_char
);
2128 /* "...a field width...may be indicated by an asterisk.
2129 In this case, an int argument supplies the field width..." */
2131 if (has_operand_number
!= 0)
2134 opnum
= maybe_read_dollar_number (&format_chars
,
2135 has_operand_number
== 1,
2142 has_operand_number
= 1;
2143 arg_num
= opnum
+ info
->first_arg_num
- 1;
2146 has_operand_number
= 0;
2150 if (avoid_dollar_number (format_chars
))
2153 if (info
->first_arg_num
!= 0)
2160 cur_param
= TREE_VALUE (params
);
2161 if (has_operand_number
<= 0)
2163 params
= TREE_CHAIN (params
);
2167 width_wanted_type
.wanted_type
= *fki
->width_type
;
2168 width_wanted_type
.wanted_type_name
= NULL
;
2169 width_wanted_type
.pointer_count
= 0;
2170 width_wanted_type
.char_lenient_flag
= 0;
2171 width_wanted_type
.scalar_identity_flag
= 0;
2172 width_wanted_type
.writing_in_flag
= 0;
2173 width_wanted_type
.reading_from_flag
= 0;
2174 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
2175 width_wanted_type
.format_start
= format_chars
- 1;
2176 width_wanted_type
.format_length
= 1;
2177 width_wanted_type
.param
= cur_param
;
2178 width_wanted_type
.arg_num
= arg_num
;
2179 width_wanted_type
.offset_loc
=
2180 format_chars
- orig_format_chars
;
2181 width_wanted_type
.next
= NULL
;
2182 if (last_wanted_type
!= 0)
2183 last_wanted_type
->next
= &width_wanted_type
;
2184 if (first_wanted_type
== 0)
2185 first_wanted_type
= &width_wanted_type
;
2186 last_wanted_type
= &width_wanted_type
;
2191 /* Possibly read a numeric width. If the width is zero,
2192 we complain if appropriate. */
2193 int non_zero_width_char
= FALSE
;
2194 int found_width
= FALSE
;
2195 while (ISDIGIT (*format_chars
))
2198 if (*format_chars
!= '0')
2199 non_zero_width_char
= TRUE
;
2202 if (found_width
&& !non_zero_width_char
&&
2203 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
2204 warning_at (format_string_loc
, OPT_Wformat_
,
2205 "zero width in %s format", fki
->name
);
2207 flag_chars
.add_char (fki
->width_char
);
2213 /* Read any format left precision (must be a number, not *). */
2215 argument_parser::read_any_format_left_precision ()
2217 if (fki
->left_precision_char
== 0)
2219 if (*format_chars
!= '#')
2223 flag_chars
.add_char (fki
->left_precision_char
);
2224 if (!ISDIGIT (*format_chars
))
2225 format_warning_at_char (format_string_loc
, format_string_cst
,
2226 format_chars
- orig_format_chars
,
2228 "empty left precision in %s format", fki
->name
);
2229 while (ISDIGIT (*format_chars
))
2233 /* Read any format precision, possibly * or *m$.
2235 Return true if format parsing is to continue, false otherwise. */
2239 read_any_format_precision (tree
¶ms
,
2240 unsigned HOST_WIDE_INT
&arg_num
)
2242 if (fki
->precision_char
== 0)
2244 if (*format_chars
!= '.')
2248 flag_chars
.add_char (fki
->precision_char
);
2249 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
2251 /* "...a...precision...may be indicated by an asterisk.
2252 In this case, an int argument supplies the...precision." */
2254 if (has_operand_number
!= 0)
2257 opnum
= maybe_read_dollar_number (&format_chars
,
2258 has_operand_number
== 1,
2265 has_operand_number
= 1;
2266 arg_num
= opnum
+ info
->first_arg_num
- 1;
2269 has_operand_number
= 0;
2273 if (avoid_dollar_number (format_chars
))
2276 if (info
->first_arg_num
!= 0)
2283 cur_param
= TREE_VALUE (params
);
2284 if (has_operand_number
<= 0)
2286 params
= TREE_CHAIN (params
);
2290 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
2291 precision_wanted_type
.wanted_type_name
= NULL
;
2292 precision_wanted_type
.pointer_count
= 0;
2293 precision_wanted_type
.char_lenient_flag
= 0;
2294 precision_wanted_type
.scalar_identity_flag
= 0;
2295 precision_wanted_type
.writing_in_flag
= 0;
2296 precision_wanted_type
.reading_from_flag
= 0;
2297 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
2298 precision_wanted_type
.param
= cur_param
;
2299 precision_wanted_type
.format_start
= format_chars
- 2;
2300 precision_wanted_type
.format_length
= 2;
2301 precision_wanted_type
.arg_num
= arg_num
;
2302 precision_wanted_type
.offset_loc
=
2303 format_chars
- orig_format_chars
;
2304 precision_wanted_type
.next
= NULL
;
2305 if (last_wanted_type
!= 0)
2306 last_wanted_type
->next
= &precision_wanted_type
;
2307 if (first_wanted_type
== 0)
2308 first_wanted_type
= &precision_wanted_type
;
2309 last_wanted_type
= &precision_wanted_type
;
2314 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
2315 && !ISDIGIT (*format_chars
))
2316 format_warning_at_char (format_string_loc
, format_string_cst
,
2317 format_chars
- orig_format_chars
,
2319 "empty precision in %s format", fki
->name
);
2320 while (ISDIGIT (*format_chars
))
2327 /* Parse any assignment-allocation flags, which request an extra
2328 char ** for writing back a dynamically-allocated char *.
2329 This is for handling the optional 'm' character in scanf,
2330 and, before C99, 'a' (for compatibility with a non-standard
2331 GNU libc extension). */
2334 argument_parser::handle_alloc_chars ()
2336 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
2338 flag_chars
.add_char (fki
->alloc_char
);
2342 /* Handle the scanf allocation kludge. */
2343 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2345 if (*format_chars
== 'a' && !flag_isoc99
)
2347 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
2348 || format_chars
[1] == '[')
2350 /* 'a' is used as a flag. */
2351 flag_chars
.add_char ('a');
2358 /* Look for length modifiers within the current format argument,
2359 returning a length_modifier instance describing it (or the
2360 default if one is not found).
2362 Issue warnings about non-standard modifiers. */
2365 argument_parser::read_any_length_modifier ()
2367 length_modifier result
;
2369 const format_length_info
*fli
= fki
->length_char_specs
;
2373 while (fli
->name
!= 0
2374 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
2378 format_chars
+= strlen (fli
->name
);
2379 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
2382 result
= length_modifier (fli
->double_name
, fli
->double_index
,
2383 fli
->double_std
, 0);
2387 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
2388 fli
->scalar_identity_flag
);
2390 flag_chars
.add_char (fki
->length_code_char
);
2394 /* Warn if the length modifier is non-standard. */
2395 if (ADJ_STD (result
.std
) > C_STD_VER
)
2396 warning_at (format_string_loc
, OPT_Wformat_
,
2397 "%s does not support the %qs %s length modifier",
2398 C_STD_NAME (result
.std
), result
.chars
,
2405 /* Read any other modifier (strftime E/O). */
2408 argument_parser::read_any_other_modifier ()
2410 if (fki
->modifier_chars
== NULL
)
2413 while (*format_chars
!= 0
2414 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2416 if (flag_chars
.has_char_p (*format_chars
))
2418 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2419 *format_chars
, NULL
);
2420 format_warning_at_char (format_string_loc
, format_string_cst
,
2421 format_chars
- orig_format_chars
,
2423 "repeated %s in format", _(s
->name
));
2426 flag_chars
.add_char (*format_chars
);
2431 /* Return the format_char_info corresponding to FORMAT_CHAR,
2432 potentially issuing a warning if the format char is
2433 not supported in the C standard version we are checking
2436 Issue a warning and return NULL if it is not found.
2438 Issue warnings about non-standard modifiers. */
2440 const format_char_info
*
2441 argument_parser::find_format_char_info (char format_char
)
2443 const format_char_info
*fci
= fki
->conversion_specs
;
2445 while (fci
->format_chars
!= 0
2446 && strchr (fci
->format_chars
, format_char
) == 0)
2448 if (fci
->format_chars
== 0)
2450 format_warning_at_char (format_string_loc
, format_string_cst
,
2451 format_chars
- orig_format_chars
,
2453 "unknown conversion type character"
2461 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2462 format_warning_at_char (format_string_loc
, format_string_cst
,
2463 format_chars
- orig_format_chars
,
2465 "%s does not support the %<%%%c%> %s format",
2466 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2472 /* Validate the pairs of flags used.
2473 Issue warnings about incompatible combinations of flags. */
2476 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2479 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2481 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2483 const format_flag_spec
*s
, *t
;
2484 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2486 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2488 if (bad_flag_pairs
[i
].predicate
!= 0
2489 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2491 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2492 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2493 if (bad_flag_pairs
[i
].ignored
)
2495 if (bad_flag_pairs
[i
].predicate
!= 0)
2496 warning_at (format_string_loc
, OPT_Wformat_
,
2497 "%s ignored with %s and %<%%%c%> %s format",
2498 _(s
->name
), _(t
->name
), format_char
,
2501 warning_at (format_string_loc
, OPT_Wformat_
,
2502 "%s ignored with %s in %s format",
2503 _(s
->name
), _(t
->name
), fki
->name
);
2507 if (bad_flag_pairs
[i
].predicate
!= 0)
2508 warning_at (format_string_loc
, OPT_Wformat_
,
2509 "use of %s and %s together with %<%%%c%> %s format",
2510 _(s
->name
), _(t
->name
), format_char
,
2513 warning_at (format_string_loc
, OPT_Wformat_
,
2514 "use of %s and %s together in %s format",
2515 _(s
->name
), _(t
->name
), fki
->name
);
2520 /* Give Y2K warnings. */
2523 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2526 if (!warn_format_y2k
)
2530 if (strchr (fci
->flags2
, '4') != 0)
2531 if (flag_chars
.has_char_p ('E'))
2535 else if (strchr (fci
->flags2
, '3') != 0)
2537 else if (strchr (fci
->flags2
, '2') != 0)
2540 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2541 "%<%%%c%> yields only last 2 digits of "
2542 "year in some locales", format_char
);
2543 else if (y2k_level
== 2)
2544 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2545 "%<%%%c%> yields only last 2 digits of year",
2549 /* Parse any "scan sets" enclosed in square brackets, e.g.
2550 for scanf-style calls. */
2553 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2555 if (strchr (fci
->flags2
, '[') == NULL
)
2558 /* Skip over scan set, in case it happens to have '%' in it. */
2559 if (*format_chars
== '^')
2561 /* Find closing bracket; if one is hit immediately, then
2562 it's part of the scan set rather than a terminator. */
2563 if (*format_chars
== ']')
2565 while (*format_chars
&& *format_chars
!= ']')
2567 if (*format_chars
!= ']')
2568 /* The end of the format string was reached. */
2569 format_warning_at_char (format_string_loc
, format_string_cst
,
2570 format_chars
- orig_format_chars
,
2572 "no closing %<]%> for %<%%[%> format");
2575 /* Return true if this argument is to be continued to be parsed,
2576 false to skip to next argument. */
2579 argument_parser::handle_conversions (const format_char_info
*fci
,
2580 const length_modifier
&len_modifier
,
2582 const char *&wanted_type_name
,
2583 unsigned HOST_WIDE_INT
&arg_num
,
2587 enum format_std_version wanted_type_std
;
2589 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2592 wanted_type
= (fci
->types
[len_modifier
.val
].type
2593 ? *fci
->types
[len_modifier
.val
].type
: 0);
2594 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2595 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2596 if (wanted_type
== 0)
2598 format_warning_at_char (format_string_loc
, format_string_cst
,
2599 format_chars
- orig_format_chars
,
2601 "use of %qs length modifier with %qc type"
2602 " character has either no effect"
2603 " or undefined behavior",
2604 len_modifier
.chars
, format_char
);
2605 /* Heuristic: skip one argument when an invalid length/type
2606 combination is encountered. */
2609 params
= TREE_CHAIN (params
);
2613 /* Warn if non-standard, provided it is more non-standard
2614 than the length and type characters that may already
2615 have been warned for. */
2616 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2617 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2619 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2620 format_warning_at_char (format_string_loc
, format_string_cst
,
2621 format_chars
- orig_format_chars
,
2623 "%s does not support the %<%%%s%c%> %s format",
2624 C_STD_NAME (wanted_type_std
),
2626 format_char
, fki
->name
);
2632 /* Check type of argument against desired type.
2634 Return true if format parsing is to continue, false otherwise. */
2638 check_argument_type (const format_char_info
*fci
,
2639 const length_modifier
&len_modifier
,
2641 const char *&wanted_type_name
,
2642 const bool suppressed
,
2643 unsigned HOST_WIDE_INT
&arg_num
,
2645 const int alloc_flag
,
2646 const char * const format_start
,
2647 const char * const type_start
,
2648 location_t fmt_param_loc
,
2649 char conversion_char
)
2651 if (info
->first_arg_num
== 0)
2654 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2657 if (main_arg_num
!= 0)
2660 warning_at (format_string_loc
, OPT_Wformat_
,
2661 "operand number specified with "
2662 "suppressed assignment");
2664 warning_at (format_string_loc
, OPT_Wformat_
,
2665 "operand number specified for format "
2666 "taking no argument");
2671 format_wanted_type
*wanted_type_ptr
;
2673 if (main_arg_num
!= 0)
2675 arg_num
= main_arg_num
;
2676 params
= main_arg_params
;
2681 if (has_operand_number
> 0)
2683 warning_at (format_string_loc
, OPT_Wformat_
,
2684 "missing $ operand number in format");
2688 has_operand_number
= 0;
2691 wanted_type_ptr
= &main_wanted_type
;
2699 cur_param
= TREE_VALUE (params
);
2700 params
= TREE_CHAIN (params
);
2703 wanted_type_ptr
->wanted_type
= wanted_type
;
2704 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2705 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2706 wanted_type_ptr
->char_lenient_flag
= 0;
2707 if (strchr (fci
->flags2
, 'c') != 0)
2708 wanted_type_ptr
->char_lenient_flag
= 1;
2709 wanted_type_ptr
->scalar_identity_flag
= 0;
2710 if (len_modifier
.scalar_identity_flag
)
2711 wanted_type_ptr
->scalar_identity_flag
= 1;
2712 wanted_type_ptr
->writing_in_flag
= 0;
2713 wanted_type_ptr
->reading_from_flag
= 0;
2715 wanted_type_ptr
->writing_in_flag
= 1;
2718 if (strchr (fci
->flags2
, 'W') != 0)
2719 wanted_type_ptr
->writing_in_flag
= 1;
2720 if (strchr (fci
->flags2
, 'R') != 0)
2721 wanted_type_ptr
->reading_from_flag
= 1;
2723 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2724 wanted_type_ptr
->param
= cur_param
;
2725 wanted_type_ptr
->arg_num
= arg_num
;
2726 wanted_type_ptr
->format_start
= format_start
;
2727 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2728 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2729 wanted_type_ptr
->next
= NULL
;
2730 if (last_wanted_type
!= 0)
2731 last_wanted_type
->next
= wanted_type_ptr
;
2732 if (first_wanted_type
== 0)
2733 first_wanted_type
= wanted_type_ptr
;
2734 last_wanted_type
= wanted_type_ptr
;
2739 wanted_type_ptr
= fwt_pool
.allocate ();
2741 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2742 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2747 if (first_wanted_type
!= 0)
2749 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2750 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2751 /* By default, use the end of the range for the caret location. */
2752 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2753 offset_to_format_end
,
2754 offset_to_format_start
, offset_to_format_end
);
2755 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2756 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2757 offset_to_type_start
,
2758 conversion_char
, arglocs
);
2764 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2765 is the NUL-terminated format string (which at this point may contain
2766 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2767 terminating NUL character). ARG_NUM is one less than the number of
2768 the first format argument to check; PARAMS points to that format
2769 argument in the list of arguments. */
2772 check_format_info_main (format_check_results
*res
,
2773 function_format_info
*info
, const char *format_chars
,
2774 location_t fmt_param_loc
, tree format_string_cst
,
2775 int format_length
, tree params
,
2776 unsigned HOST_WIDE_INT arg_num
,
2777 object_allocator
<format_wanted_type
> &fwt_pool
,
2778 vec
<location_t
> *arglocs
)
2780 const char * const orig_format_chars
= format_chars
;
2781 const tree first_fillin_param
= params
;
2783 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
2784 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
2785 const location_t format_string_loc
= res
->format_string_loc
;
2787 /* -1 if no conversions taking an operand have been found; 0 if one has
2788 and it didn't use $; 1 if $ formats are in use. */
2789 int has_operand_number
= -1;
2791 /* Vector of pointers to opening quoting directives (like GCC "%<"). */
2792 auto_vec
<const char*> quotdirs
;
2794 /* Pointers to the most recent color directives (like GCC's "%r or %R").
2795 A starting color directive much be terminated before the end of
2796 the format string. A terminating directive makes no sense without
2797 a prior starting directive. */
2798 const char *color_begin
= NULL
;
2799 const char *color_end
= NULL
;
2801 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
2803 while (*format_chars
!= 0)
2805 if (*format_chars
++ != '%')
2807 if (*format_chars
== 0)
2809 format_warning_at_char (format_string_loc
, format_string_cst
,
2810 format_chars
- orig_format_chars
,
2812 "spurious trailing %<%%%> in format");
2815 if (*format_chars
== '%')
2821 flag_chars_t flag_chars
;
2822 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
2823 orig_format_chars
, format_string_loc
,
2824 flag_chars
, has_operand_number
,
2825 first_fillin_param
, fwt_pool
, arglocs
);
2827 if (!arg_parser
.read_any_dollar ())
2830 if (!arg_parser
.read_format_flags ())
2833 /* Read any format width, possibly * or *m$. */
2834 if (!arg_parser
.read_any_format_width (params
, arg_num
))
2837 /* Read any format left precision (must be a number, not *). */
2838 arg_parser
.read_any_format_left_precision ();
2840 /* Read any format precision, possibly * or *m$. */
2841 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
2844 const char *format_start
= format_chars
;
2846 arg_parser
.handle_alloc_chars ();
2848 /* The rest of the conversion specification is the length modifier
2849 (if any), and the conversion specifier, so this is where the
2850 type information starts. If we need to issue a suggestion
2851 about a type mismatch, then we should preserve everything up
2853 const char *type_start
= format_chars
;
2855 /* Read any length modifier, if this kind of format has them. */
2856 const length_modifier len_modifier
2857 = arg_parser
.read_any_length_modifier ();
2859 /* Read any modifier (strftime E/O). */
2860 arg_parser
.read_any_other_modifier ();
2862 char format_char
= *format_chars
;
2863 if (format_char
== 0
2864 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
2865 && format_char
== '%'))
2867 format_warning_at_char (format_string_loc
, format_string_cst
,
2868 format_chars
- orig_format_chars
,
2870 "conversion lacks type at end of format");
2875 const format_char_info
* const fci
2876 = arg_parser
.find_format_char_info (format_char
);
2880 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
2882 format_string_loc
, orig_format_chars
, format_char
,
2883 quotdirs
.length () > 0);
2885 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
2886 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
2888 /* Diagnose nested or unmatched quoting directives such as GCC's
2889 "%<...%<" and "%>...%>". */
2890 bool quot_begin_p
= strchr (fci
->flags2
, '<');
2891 bool quot_end_p
= strchr (fci
->flags2
, '>');
2893 if (quot_begin_p
&& !quot_end_p
)
2895 if (quotdirs
.length ())
2896 format_warning_at_char (format_string_loc
, format_string_cst
,
2897 format_chars
- orig_format_chars
,
2899 "nested quoting directive");
2900 quotdirs
.safe_push (format_chars
);
2902 else if (!quot_begin_p
&& quot_end_p
)
2904 if (quotdirs
.length ())
2907 format_warning_at_char (format_string_loc
, format_string_cst
,
2908 format_chars
- orig_format_chars
,
2910 "unmatched quoting directive");
2913 bool color_begin_p
= strchr (fci
->flags2
, '/');
2916 color_begin
= format_chars
;
2919 else if (strchr (fci
->flags2
, '\\'))
2922 format_warning_at_char (format_string_loc
, format_string_cst
,
2923 format_chars
- orig_format_chars
,
2925 "%qc directive redundant after prior "
2926 "occurence of the same", format_char
);
2927 else if (!color_begin
)
2928 format_warning_at_char (format_string_loc
, format_string_cst
,
2929 format_chars
- orig_format_chars
,
2931 "unmatched color reset directive");
2932 color_end
= format_chars
;
2935 /* Diagnose directives that shouldn't appear in a quoted sequence.
2936 (They are denoted by a double quote in FLAGS2.) */
2937 if (quotdirs
.length ())
2939 if (strchr (fci
->flags2
, '"'))
2940 format_warning_at_char (format_string_loc
, format_string_cst
,
2941 format_chars
- orig_format_chars
,
2943 "%qc conversion used within a quoted "
2948 /* Validate the pairs of flags used. */
2949 arg_parser
.validate_flag_pairs (fci
, format_char
);
2951 arg_parser
.give_y2k_warnings (fci
, format_char
);
2953 arg_parser
.parse_any_scan_set (fci
);
2955 tree wanted_type
= NULL
;
2956 const char *wanted_type_name
= NULL
;
2958 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
2959 wanted_type
, wanted_type_name
,
2965 arg_parser
.main_wanted_type
.next
= NULL
;
2967 /* Finally. . .check type of argument against desired type! */
2968 if (!arg_parser
.check_argument_type (fci
, len_modifier
,
2969 wanted_type
, wanted_type_name
,
2973 format_start
, type_start
,
2979 if (format_chars
- orig_format_chars
!= format_length
)
2980 format_warning_at_char (format_string_loc
, format_string_cst
,
2981 format_chars
+ 1 - orig_format_chars
,
2982 OPT_Wformat_contains_nul
,
2983 "embedded %<\\0%> in format");
2984 if (info
->first_arg_num
!= 0 && params
!= 0
2985 && has_operand_number
<= 0)
2987 res
->number_other
--;
2988 res
->number_extra_args
++;
2990 if (has_operand_number
> 0)
2991 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
2993 if (quotdirs
.length ())
2994 format_warning_at_char (format_string_loc
, format_string_cst
,
2995 quotdirs
.pop () - orig_format_chars
,
2996 OPT_Wformat_
, "unterminated quoting directive");
2997 if (color_begin
&& !color_end
)
2998 format_warning_at_char (format_string_loc
, format_string_cst
,
2999 color_begin
- orig_format_chars
,
3000 OPT_Wformat_
, "unterminated color directive");
3003 /* Check the argument types from a single format conversion (possibly
3004 including width and precision arguments).
3006 FMT_LOC is the location of the format conversion.
3008 TYPES is a singly-linked list expressing the parts of the format
3009 conversion that expect argument types, and the arguments they
3012 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3013 format string to where type information begins for the conversion
3014 (the length modifier and conversion specifier).
3016 CONVERSION_CHAR is the user-provided conversion specifier.
3020 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3022 then FMT_LOC covers this range:
3024 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3027 and TYPES in this case is a three-entry singly-linked list consisting of:
3028 (1) the check for the field width here:
3029 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3032 (2) the check for the field precision here:
3033 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3036 (3) the check for the length modifier and conversion char here:
3037 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3041 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3044 0000000000111111111122
3045 0123456789012345678901
3046 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3048 | ` CONVERSION_CHAR: 'd'
3049 type starts here. */
3052 check_format_types (const substring_loc
&fmt_loc
,
3053 format_wanted_type
*types
, const format_kind_info
*fki
,
3054 int offset_to_type_start
,
3055 char conversion_char
,
3056 vec
<location_t
> *arglocs
)
3058 for (; types
!= 0; types
= types
->next
)
3068 wanted_type
= types
->wanted_type
;
3069 arg_num
= types
->arg_num
;
3071 /* The following should not occur here. */
3072 gcc_assert (wanted_type
);
3073 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
3075 if (types
->pointer_count
== 0)
3076 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
3078 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
3080 cur_param
= types
->param
;
3083 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
3084 NULL
, fki
, offset_to_type_start
,
3089 cur_type
= TREE_TYPE (cur_param
);
3090 if (cur_type
== error_mark_node
)
3092 orig_cur_type
= cur_type
;
3095 location_t param_loc
= UNKNOWN_LOCATION
;
3096 if (EXPR_HAS_LOCATION (cur_param
))
3097 param_loc
= EXPR_LOCATION (cur_param
);
3100 /* arg_num is 1-based. */
3101 gcc_assert (types
->arg_num
> 0);
3102 param_loc
= (*arglocs
)[types
->arg_num
- 1];
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_loc
,
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_loc
, 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
)
3508 char_span line
= location_get_source_line (start
.file
, start
.line
);
3512 /* If we got this far, then we have the line containing the
3513 existing conversion specification.
3515 Generate a trimmed copy, containing the prefix part of the conversion
3516 specification, up to the (but not including) the length modifier.
3517 In the above example, this would be "%-+*.*". */
3518 int length_up_to_type
= caret
.column
- start
.column
;
3519 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
3520 char *prefix
= prefix_span
.xstrdup ();
3522 /* Now attempt to generate a suggestion for the rest of the specification
3523 (length modifier and conversion char), based on ARG_TYPE and
3525 In the above example, this would be "ld". */
3526 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
3527 if (!format_for_type
)
3533 /* Success. Generate the resulting suggestion for the whole range of
3534 FMT_LOC by concatenating the two strings.
3535 In the above example, this would be "%-+*.*ld". */
3536 char *result
= concat (prefix
, format_for_type
, NULL
);
3537 free (format_for_type
);
3542 /* Helper class for adding zero or more trailing '*' to types.
3544 The format type and name exclude any '*' for pointers, so those
3545 must be formatted manually. For all the types we currently have,
3546 this is adequate, but formats taking pointers to functions or
3547 arrays would require the full type to be built up in order to
3548 print it with %T. */
3550 class indirection_suffix
3553 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
3555 /* Determine the size of the buffer (including NUL-terminator). */
3557 size_t get_buffer_size () const
3559 return m_pointer_count
+ 2;
3562 /* Write the '*' to DST and add a NUL-terminator. */
3564 void fill_buffer (char *dst
) const
3566 if (m_pointer_count
== 0)
3568 else if (c_dialect_cxx ())
3570 memset (dst
, '*', m_pointer_count
);
3571 dst
[m_pointer_count
] = 0;
3576 memset (dst
+ 1, '*', m_pointer_count
);
3577 dst
[m_pointer_count
+ 1] = 0;
3582 int m_pointer_count
;
3585 /* Subclass of range_label for labelling the range in the format string
3586 with the type in question, adding trailing '*' for pointer_count. */
3588 class range_label_for_format_type_mismatch
3589 : public range_label_for_type_mismatch
3592 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
3594 : range_label_for_type_mismatch (labelled_type
, other_type
),
3595 m_pointer_count (pointer_count
)
3599 label_text
get_text () const FINAL OVERRIDE
3601 label_text text
= range_label_for_type_mismatch::get_text ();
3602 if (text
.m_buffer
== NULL
)
3605 indirection_suffix
suffix (m_pointer_count
);
3606 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3607 suffix
.fill_buffer (p
);
3609 char *result
= concat (text
.m_buffer
, p
, NULL
);
3611 return label_text (result
, true);
3615 int m_pointer_count
;
3618 /* Give a warning about a format argument of different type from that expected.
3619 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3620 is based on the location of the char at TYPE->offset_loc.
3621 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3622 if this is unavailable.
3623 WANTED_TYPE is the type the argument should have,
3624 possibly stripped of pointer dereferences. The description (such as "field
3625 precision"), the placement in the format string, a possibly more
3626 friendly name of WANTED_TYPE, and the number of pointer dereferences
3627 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3628 or NULL if it is missing.
3630 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3631 format string to where type information begins for the conversion
3632 (the length modifier and conversion specifier).
3633 CONVERSION_CHAR is the user-provided conversion specifier.
3635 For example, given a type mismatch for argument 5 here:
3637 00000000011111111112222222222333333333344444444445555555555|
3638 12345678901234567890123456789012345678901234567890123456789` column numbers
3639 0000000000111111111122|
3640 0123456789012345678901` offsets within STRING_CST
3641 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3642 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3644 | ` CONVERSION_CHAR: 'd' PARAM_LOC
3647 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3651 format_type_warning (const substring_loc
&whole_fmt_loc
,
3652 location_t param_loc
,
3653 format_wanted_type
*type
,
3654 tree wanted_type
, tree arg_type
,
3655 const format_kind_info
*fki
,
3656 int offset_to_type_start
,
3657 char conversion_char
)
3659 enum format_specifier_kind kind
= type
->kind
;
3660 const char *wanted_type_name
= type
->wanted_type_name
;
3661 const char *format_start
= type
->format_start
;
3662 int format_length
= type
->format_length
;
3663 int pointer_count
= type
->pointer_count
;
3664 int arg_num
= type
->arg_num
;
3666 /* If ARG_TYPE is a typedef with a misleading name (for example,
3667 size_t but not the standard size_t expected by printf %zu), avoid
3668 printing the typedef name. */
3669 if (wanted_type_name
3671 && TYPE_NAME (arg_type
)
3672 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
3673 && DECL_NAME (TYPE_NAME (arg_type
))
3674 && !strcmp (wanted_type_name
,
3675 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
3676 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
3678 indirection_suffix
suffix (pointer_count
);
3679 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3680 suffix
.fill_buffer (p
);
3682 /* WHOLE_FMT_LOC has the caret at the end of the range.
3683 Set the caret to be at the offset from TYPE. Subtract one
3684 from the offset for the same reason as in format_warning_at_char. */
3685 substring_loc
fmt_loc (whole_fmt_loc
);
3686 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
3688 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
3690 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
3692 /* Get a string for use as a replacement fix-it hint for the range in
3693 fmt_loc, or NULL. */
3694 char *corrected_substring
3695 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3696 offset_to_type_start
, conversion_char
);
3698 if (wanted_type_name
)
3701 format_warning_at_substring
3702 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3703 corrected_substring
, OPT_Wformat_
,
3704 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
3705 "but argument %d has type %qT",
3706 gettext (kind_descriptions
[kind
]),
3707 (kind
== CF_KIND_FORMAT
? "%" : ""),
3708 format_length
, format_start
,
3709 wanted_type_name
, p
, arg_num
, arg_type
);
3711 format_warning_at_substring
3712 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3713 corrected_substring
, OPT_Wformat_
,
3714 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
3715 gettext (kind_descriptions
[kind
]),
3716 (kind
== CF_KIND_FORMAT
? "%" : ""),
3717 format_length
, format_start
, wanted_type_name
, p
);
3722 format_warning_at_substring
3723 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3724 corrected_substring
, OPT_Wformat_
,
3725 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
3726 "but argument %d has type %qT",
3727 gettext (kind_descriptions
[kind
]),
3728 (kind
== CF_KIND_FORMAT
? "%" : ""),
3729 format_length
, format_start
,
3730 wanted_type
, p
, arg_num
, arg_type
);
3732 format_warning_at_substring
3733 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3734 corrected_substring
, OPT_Wformat_
,
3735 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
3736 gettext (kind_descriptions
[kind
]),
3737 (kind
== CF_KIND_FORMAT
? "%" : ""),
3738 format_length
, format_start
, wanted_type
, p
);
3741 free (corrected_substring
);
3745 /* Given a format_char_info array FCI, and a character C, this function
3746 returns the index into the conversion_specs where that specifier's
3747 data is located. The character must exist. */
3749 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
3753 for (i
= 0; fci
->format_chars
; i
++, fci
++)
3754 if (strchr (fci
->format_chars
, c
))
3757 /* We shouldn't be looking for a non-existent specifier. */
3761 /* Given a format_length_info array FLI, and a character C, this
3762 function returns the index into the conversion_specs where that
3763 modifier's data is located. The character must exist. */
3765 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
3769 for (i
= 0; fli
->name
; i
++, fli
++)
3770 if (strchr (fli
->name
, c
))
3773 /* We shouldn't be looking for a non-existent modifier. */
3777 /* Determine the type of HOST_WIDE_INT in the code being compiled for
3778 use in GCC's __asm_fprintf__ custom format attribute. You must
3779 have set dynamic_format_types before calling this function. */
3781 init_dynamic_asm_fprintf_info (void)
3787 format_length_info
*new_asm_fprintf_length_specs
;
3790 /* Find the underlying type for HOST_WIDE_INT. For the %w
3791 length modifier to work, one must have issued: "typedef
3792 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3793 prior to using that modifier. */
3794 hwi
= maybe_get_identifier ("__gcc_host_wide_int__");
3797 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3800 hwi
= identifier_global_value (hwi
);
3801 if (!hwi
|| TREE_CODE (hwi
) != TYPE_DECL
)
3803 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3806 hwi
= DECL_ORIGINAL_TYPE (hwi
);
3808 if (hwi
!= long_integer_type_node
&& hwi
!= long_long_integer_type_node
)
3810 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
3811 " or %<long long%>");
3815 /* Create a new (writable) copy of asm_fprintf_length_specs. */
3816 new_asm_fprintf_length_specs
= (format_length_info
*)
3817 xmemdup (asm_fprintf_length_specs
,
3818 sizeof (asm_fprintf_length_specs
),
3819 sizeof (asm_fprintf_length_specs
));
3821 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3822 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
3823 if (hwi
== long_integer_type_node
)
3824 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
3825 else if (hwi
== long_long_integer_type_node
)
3826 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
3830 /* Assign the new data for use. */
3831 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
3832 new_asm_fprintf_length_specs
;
3836 /* Determine the type of a "locus" in the code being compiled for use
3837 in GCC's __gcc_gfc__ custom format attribute. You must have set
3838 dynamic_format_types before calling this function. */
3840 init_dynamic_gfc_info (void)
3844 static format_char_info
*gfc_fci
;
3846 /* For the GCC __gcc_gfc__ custom format specifier to work, one
3847 must have declared 'locus' prior to using this attribute. If
3848 we haven't seen this declarations then you shouldn't use the
3849 specifier requiring that type. */
3850 if ((locus
= maybe_get_identifier ("locus")))
3852 locus
= identifier_global_value (locus
);
3855 if (TREE_CODE (locus
) != TYPE_DECL
3856 || TREE_TYPE (locus
) == error_mark_node
)
3858 error ("%<locus%> is not defined as a type");
3862 locus
= TREE_TYPE (locus
);
3866 /* Assign the new data for use. */
3868 /* Handle the __gcc_gfc__ format specifics. */
3870 dynamic_format_types
[gcc_gfc_format_type
].conversion_specs
=
3871 gfc_fci
= (format_char_info
*)
3872 xmemdup (gcc_gfc_char_table
,
3873 sizeof (gcc_gfc_char_table
),
3874 sizeof (gcc_gfc_char_table
));
3877 const unsigned i
= find_char_info_specifier_index (gfc_fci
, 'L');
3878 gfc_fci
[i
].types
[0].type
= &locus
;
3879 gfc_fci
[i
].pointer_count
= 1;
3884 /* Determine the types of "tree" and "location_t" in the code being
3885 compiled for use in GCC's diagnostic custom format attributes. You
3886 must have set dynamic_format_types before calling this function. */
3888 init_dynamic_diag_info (void)
3890 /* For the GCC-diagnostics custom format specifiers to work, one
3891 must have declared 'tree' and 'location_t' prior to using those
3892 attributes. If we haven't seen these declarations then
3893 the specifiers requiring these types shouldn't be used.
3894 However we don't force a hard ICE because we may see only one
3895 or the other type. */
3896 if (tree loc
= maybe_get_identifier ("location_t"))
3898 loc
= identifier_global_value (loc
);
3899 if (loc
&& TREE_CODE (loc
) != TYPE_DECL
)
3900 error ("%<location_t%> is not defined as a type");
3903 /* Initialize the global tree node type local to this file. */
3904 if (!local_tree_type_node
3905 || local_tree_type_node
== void_type_node
)
3907 /* We need to grab the underlying 'union tree_node' so peek into
3908 an extra type level. */
3909 if ((local_tree_type_node
= maybe_get_identifier ("tree")))
3911 local_tree_type_node
= identifier_global_value (local_tree_type_node
);
3912 if (local_tree_type_node
)
3914 if (TREE_CODE (local_tree_type_node
) != TYPE_DECL
)
3916 error ("%<tree%> is not defined as a type");
3917 local_tree_type_node
= 0;
3919 else if (TREE_CODE (TREE_TYPE (local_tree_type_node
))
3922 error ("%<tree%> is not defined as a pointer type");
3923 local_tree_type_node
= 0;
3926 local_tree_type_node
=
3927 TREE_TYPE (TREE_TYPE (local_tree_type_node
));
3931 local_tree_type_node
= void_type_node
;
3934 /* Similar to the above but for gimple*. */
3935 if (!local_gimple_ptr_node
3936 || local_gimple_ptr_node
== void_type_node
)
3938 if ((local_gimple_ptr_node
= maybe_get_identifier ("gimple")))
3940 local_gimple_ptr_node
3941 = identifier_global_value (local_gimple_ptr_node
);
3942 if (local_gimple_ptr_node
)
3944 if (TREE_CODE (local_gimple_ptr_node
) != TYPE_DECL
)
3946 error ("%<gimple%> is not defined as a type");
3947 local_gimple_ptr_node
= 0;
3950 local_gimple_ptr_node
= TREE_TYPE (local_gimple_ptr_node
);
3954 local_gimple_ptr_node
= void_type_node
;
3961 static format_length_info
*diag_ls
;
3964 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
3965 length modifier to work, one must have issued: "typedef
3966 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3967 prior to using that modifier. */
3968 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
3970 hwi
= identifier_global_value (hwi
);
3973 if (TREE_CODE (hwi
) != TYPE_DECL
)
3975 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3980 hwi
= DECL_ORIGINAL_TYPE (hwi
);
3982 if (hwi
!= long_integer_type_node
3983 && hwi
!= long_long_integer_type_node
)
3985 error ("%<__gcc_host_wide_int__%> is not defined"
3986 " as %<long%> or %<long long%>");
3993 /* Assign the new data for use. */
3995 /* All the GCC diag formats use the same length specs. */
3997 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
3998 dynamic_format_types
[gcc_tdiag_format_type
].length_char_specs
=
3999 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
4000 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
4001 dynamic_format_types
[gcc_dump_printf_format_type
].length_char_specs
=
4002 diag_ls
= (format_length_info
*)
4003 xmemdup (gcc_diag_length_specs
,
4004 sizeof (gcc_diag_length_specs
),
4005 sizeof (gcc_diag_length_specs
));
4008 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4009 i
= find_length_info_modifier_index (diag_ls
, 'w');
4010 if (hwi
== long_integer_type_node
)
4011 diag_ls
[i
].index
= FMT_LEN_l
;
4012 else if (hwi
== long_long_integer_type_node
)
4013 diag_ls
[i
].index
= FMT_LEN_ll
;
4019 /* It's safe to "re-initialize these to the same values. */
4020 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
4021 gcc_diag_char_table
;
4022 dynamic_format_types
[gcc_tdiag_format_type
].conversion_specs
=
4023 gcc_tdiag_char_table
;
4024 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
4025 gcc_cdiag_char_table
;
4026 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
4027 gcc_cxxdiag_char_table
;
4028 dynamic_format_types
[gcc_dump_printf_format_type
].conversion_specs
=
4029 gcc_dump_printf_char_table
;
4032 #ifdef TARGET_FORMAT_TYPES
4033 extern const format_kind_info TARGET_FORMAT_TYPES
[];
4036 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4037 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
4039 #ifdef TARGET_OVERRIDES_FORMAT_INIT
4040 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
4043 /* Attributes such as "printf" are equivalent to those such as
4044 "gnu_printf" unless this is overridden by a target. */
4045 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
4047 { "gnu_printf", "printf" },
4048 { "gnu_scanf", "scanf" },
4049 { "gnu_strftime", "strftime" },
4050 { "gnu_strfmon", "strfmon" },
4054 /* Translate to unified attribute name. This is used in decode_format_type and
4055 decode_format_attr. In attr_name the user specified argument is passed. It
4056 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4057 or the attr_name passed to this function, if there is no matching entry. */
4059 convert_format_name_to_system_name (const char *attr_name
)
4063 if (attr_name
== NULL
|| *attr_name
== 0
4064 || strncmp (attr_name
, "gcc_", 4) == 0)
4066 #ifdef TARGET_OVERRIDES_FORMAT_INIT
4067 TARGET_OVERRIDES_FORMAT_INIT ();
4070 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
4071 /* Check if format attribute is overridden by target. */
4072 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
4073 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
4075 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
4077 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
4080 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
4082 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
4086 /* Otherwise default to gnu format. */
4088 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
4091 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
4094 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
4096 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
4102 /* Handle a "format" attribute; arguments as in
4103 struct attribute_spec.handler. */
4105 handle_format_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
4106 int flags
, bool *no_add_attrs
)
4109 function_format_info info
;
4111 #ifdef TARGET_FORMAT_TYPES
4112 /* If the target provides additional format types, we need to
4113 add them to FORMAT_TYPES at first use. */
4114 if (TARGET_FORMAT_TYPES
!= NULL
&& !dynamic_format_types
)
4116 dynamic_format_types
= XNEWVEC (format_kind_info
,
4117 n_format_types
+ TARGET_N_FORMAT_TYPES
);
4118 memcpy (dynamic_format_types
, format_types_orig
,
4119 sizeof (format_types_orig
));
4120 memcpy (&dynamic_format_types
[n_format_types
], TARGET_FORMAT_TYPES
,
4121 TARGET_N_FORMAT_TYPES
* sizeof (dynamic_format_types
[0]));
4123 format_types
= dynamic_format_types
;
4124 /* Provide a reference for the first potential external type. */
4125 first_target_format_type
= n_format_types
;
4126 n_format_types
+= TARGET_N_FORMAT_TYPES
;
4130 /* Canonicalize name of format function. */
4131 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
4132 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
4134 if (!decode_format_attr (args
, &info
, 0))
4136 *no_add_attrs
= true;
4140 if (prototype_p (type
))
4142 if (!check_format_string (type
, info
.format_num
, flags
,
4143 no_add_attrs
, info
.format_type
))
4146 if (info
.first_arg_num
!= 0)
4148 unsigned HOST_WIDE_INT arg_num
= 1;
4149 function_args_iterator iter
;
4152 /* Verify that first_arg_num points to the last arg,
4154 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
4157 if (arg_num
!= info
.first_arg_num
)
4159 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
4160 error ("args to be formatted is not %<...%>");
4161 *no_add_attrs
= true;
4167 /* Check if this is a strftime variant. Just for this variant
4168 FMT_FLAG_ARG_CONVERT is not set. */
4169 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
4170 && info
.first_arg_num
!= 0)
4172 error ("strftime formats cannot format arguments");
4173 *no_add_attrs
= true;
4177 /* If this is a custom GCC-internal format type, we have to
4178 initialize certain bits at runtime. */
4179 if (info
.format_type
== asm_fprintf_format_type
4180 || info
.format_type
== gcc_gfc_format_type
4181 || info
.format_type
== gcc_diag_format_type
4182 || info
.format_type
== gcc_tdiag_format_type
4183 || info
.format_type
== gcc_cdiag_format_type
4184 || info
.format_type
== gcc_cxxdiag_format_type
4185 || info
.format_type
== gcc_dump_printf_format_type
)
4187 /* Our first time through, we have to make sure that our
4188 format_type data is allocated dynamically and is modifiable. */
4189 if (!dynamic_format_types
)
4190 format_types
= dynamic_format_types
= (format_kind_info
*)
4191 xmemdup (format_types_orig
, sizeof (format_types_orig
),
4192 sizeof (format_types_orig
));
4194 /* If this is format __asm_fprintf__, we have to initialize
4195 GCC's notion of HOST_WIDE_INT for checking %wd. */
4196 if (info
.format_type
== asm_fprintf_format_type
)
4197 init_dynamic_asm_fprintf_info ();
4198 /* If this is format __gcc_gfc__, we have to initialize GCC's
4199 notion of 'locus' at runtime for %L. */
4200 else if (info
.format_type
== gcc_gfc_format_type
)
4201 init_dynamic_gfc_info ();
4202 /* If this is one of the diagnostic attributes, then we have to
4203 initialize 'location_t' and 'tree' at runtime. */
4204 else if (info
.format_type
== gcc_diag_format_type
4205 || info
.format_type
== gcc_tdiag_format_type
4206 || info
.format_type
== gcc_cdiag_format_type
4207 || info
.format_type
== gcc_cxxdiag_format_type
4208 || info
.format_type
== gcc_dump_printf_format_type
)
4209 init_dynamic_diag_info ();
4219 namespace selftest
{
4221 /* Selftests of location handling. */
4223 /* Get the format_kind_info with the given name. */
4225 static const format_kind_info
*
4226 get_info (const char *name
)
4228 int idx
= decode_format_type (name
);
4229 const format_kind_info
*fki
= &format_types
[idx
];
4230 ASSERT_STREQ (fki
->name
, name
);
4234 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
4235 is EXPECTED_FORMAT. */
4238 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
4239 const char *expected_format
, tree type
,
4240 char conversion_char
)
4243 gcc_assert (expected_format
);
4246 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
4247 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
4248 free (actual_format
);
4251 /* Selftests for get_format_for_type. */
4253 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
4254 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
4255 (TYPE), (CONVERSION_CHAR))
4257 /* Selftest for get_format_for_type for "printf"-style functions. */
4260 test_get_format_for_type_printf ()
4262 const format_kind_info
*fki
= get_info ("gnu_printf");
4263 ASSERT_NE (fki
, NULL
);
4265 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
4266 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
4267 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
4268 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
4269 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
4270 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
4271 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
4272 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
4273 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
4274 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
4275 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
4276 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
4277 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
4278 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
4279 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
4280 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
4281 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
4282 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
4283 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
4284 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
4285 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
4286 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
4287 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
4288 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
4289 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
4290 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
4291 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
4292 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
4295 /* Selftest for get_format_for_type for "scanf"-style functions. */
4298 test_get_format_for_type_scanf ()
4300 const format_kind_info
*fki
= get_info ("gnu_scanf");
4301 ASSERT_NE (fki
, NULL
);
4302 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
4303 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
4304 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
4305 build_pointer_type (long_integer_type_node
), 'd');
4306 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
4307 build_pointer_type (long_unsigned_type_node
), 'u');
4308 ASSERT_FORMAT_FOR_TYPE_STREQ
4309 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
4310 ASSERT_FORMAT_FOR_TYPE_STREQ
4311 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
4312 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
4313 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
4316 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4318 /* Exercise the type-printing label code, to give some coverage
4319 under "make selftest-valgrind" (in particular, to ensure that
4320 the label-printing machinery doesn't leak). */
4323 test_type_mismatch_range_labels ()
4325 /* Create a tempfile and write some text to it.
4326 ....................0000000001 11111111 12 22222222
4327 ....................1234567890 12345678 90 12345678. */
4328 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
4329 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
4330 line_table_test ltt
;
4332 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
4334 location_t c17
= linemap_position_for_column (line_table
, 17);
4335 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
4336 location_t c18
= linemap_position_for_column (line_table
, 18);
4337 location_t c24
= linemap_position_for_column (line_table
, 24);
4338 location_t c26
= linemap_position_for_column (line_table
, 26);
4340 /* Don't attempt to run the tests if column data might be unavailable. */
4341 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
4344 location_t fmt
= make_location (c18
, c17
, c18
);
4345 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
4347 location_t param
= make_location (c24
, c24
, c26
);
4348 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
4350 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
4351 integer_type_node
, 1);
4352 range_label_for_type_mismatch
param_label (integer_type_node
,
4354 gcc_rich_location
richloc (fmt
, &fmt_label
);
4355 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
4357 test_diagnostic_context dc
;
4358 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
4359 if (c_dialect_cxx ())
4360 /* "char*", without a space. */
4362 " printf (\"msg: %i\\n\", msg);\n"
4366 pp_formatted_text (dc
.printer
));
4368 /* "char *", with a space. */
4370 " printf (\"msg: %i\\n\", msg);\n"
4375 pp_formatted_text (dc
.printer
));
4378 /* Run all of the selftests within this file. */
4383 test_get_modifier_for_format_len ();
4384 test_get_format_for_type_printf ();
4385 test_get_format_for_type_scanf ();
4386 test_type_mismatch_range_labels ();
4389 } // namespace selftest
4391 #endif /* CHECKING_P */
4393 #include "gt-c-family-c-format.h"