1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /* Check calls to formatted I/O functions (-Wformat).
3 Copyright (C) 1992-2019 Free Software Foundation, Inc.
5 Extended for FRR's printfrr() with Linux kernel style extensions
6 Copyright (C) 2019-2020 David Lamparter, for NetDEF, Inc.
9 #include "gcc-common.h"
13 #include "coretypes.h"
15 //include "c-target.h"
17 #include "alloc-pool.h"
18 #include "stringpool.h"
22 #include "langhooks.h"
23 #include "frr-format.h"
24 #include "diagnostic.h"
25 #include "substring-locations.h"
27 #include "selftest-diagnostic.h"
28 #ifndef FIRST_PSEUDO_REGISTER
29 #define FIRST_PSEUDO_REGISTER 0
33 #include "gcc-rich-location.h"
34 #include "c-pretty-print.h"
37 extern struct cpp_reader
*parse_in
;
39 #pragma GCC visibility push(hidden)
41 /* Handle attributes associated with format checking. */
43 /* This must be in the same order as format_types, except for
44 format_type_error. Target-specific format types do not have
45 matching enum values. */
46 enum format_type
{ frr_printf_format_type
,
47 format_type_error
= -1};
49 struct function_format_info
51 int format_type
; /* type of format (printf, scanf, etc.) */
52 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
53 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
56 static GTY(()) tree local_uint64_t_node
;
57 static GTY(()) tree local_int64_t_node
;
59 static GTY(()) tree local_size_t_node
;
60 static GTY(()) tree local_ssize_t_node
;
61 static GTY(()) tree local_atomic_size_t_node
;
62 static GTY(()) tree local_atomic_ssize_t_node
;
63 static GTY(()) tree local_ptrdiff_t_node
;
65 static GTY(()) tree local_pid_t_node
;
66 static GTY(()) tree local_uid_t_node
;
67 static GTY(()) tree local_gid_t_node
;
68 static GTY(()) tree local_time_t_node
;
70 static GTY(()) tree local_socklen_t_node
;
71 static GTY(()) tree local_in_addr_t_node
;
73 static struct type_special
{
78 { &local_atomic_size_t_node
, &local_size_t_node
, &local_ssize_t_node
, },
79 { &local_atomic_ssize_t_node
, &local_ssize_t_node
, &local_size_t_node
, },
80 { &local_size_t_node
, NULL
, &local_ssize_t_node
, },
81 { &local_ssize_t_node
, NULL
, &local_size_t_node
, },
82 { &local_uint64_t_node
, NULL
, &local_int64_t_node
, },
83 { &local_int64_t_node
, NULL
, &local_uint64_t_node
, },
84 { &local_pid_t_node
, NULL
, &local_pid_t_node
, },
85 { &local_uid_t_node
, NULL
, &local_uid_t_node
, },
86 { &local_gid_t_node
, NULL
, &local_gid_t_node
, },
87 { &local_time_t_node
, NULL
, &local_time_t_node
, },
91 static bool decode_format_attr (tree
, function_format_info
*, int);
92 static int decode_format_type (const char *);
94 static bool check_format_string (tree argument
,
95 unsigned HOST_WIDE_INT format_num
,
96 int flags
, bool *no_add_attrs
,
97 int expected_format_type
);
98 static bool get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
,
100 static const char *convert_format_name_to_system_name (const char *attr_name
);
102 static int first_target_format_type
;
103 static const char *format_name (int format_num
);
104 static int format_flags (int format_num
);
106 /* Emit a warning as per format_warning_va, but construct the substring_loc
107 for the character at offset (CHAR_IDX - 1) within a string constant
108 FORMAT_STRING_CST at FMT_STRING_LOC. */
110 ATTRIBUTE_GCC_DIAG (5,6)
112 format_warning_at_char (location_t fmt_string_loc
, tree format_string_cst
,
113 int char_idx
, int opt
, const char *gmsgid
, ...)
116 va_start (ap
, gmsgid
);
117 tree string_type
= TREE_TYPE (format_string_cst
);
119 /* The callers are of the form:
120 format_warning (format_string_loc, format_string_cst,
121 format_chars - orig_format_chars,
122 where format_chars has already been incremented, so that
123 CHAR_IDX is one character beyond where the warning should
124 be emitted. Fix it. */
127 substring_loc
fmt_loc (fmt_string_loc
, string_type
, char_idx
, char_idx
,
129 #if BUILDING_GCC_VERSION >= 9000
130 format_string_diagnostic_t
diag (fmt_loc
, NULL
, UNKNOWN_LOCATION
, NULL
,
132 bool warned
= diag
.emit_warning_va (opt
, gmsgid
, &ap
);
134 bool warned
= format_warning_va (fmt_loc
, UNKNOWN_LOCATION
, NULL
,
142 /* Check that we have a pointer to a string suitable for use as a format.
143 The default is to check for a char type.
144 For objective-c dialects, this is extended to include references to string
145 objects validated by objc_string_ref_type_p ().
146 Targets may also provide a string object type that can be used within c and
147 c++ and shared with their respective objective-c dialects. In this case the
148 reference to a format string is checked for validity via a hook.
150 The function returns true if strref points to any string type valid for the
151 language dialect and target. */
154 valid_stringptr_type_p (tree strref
)
156 return (strref
!= NULL
157 && TREE_CODE (strref
) == POINTER_TYPE
158 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref
)) == char_type_node
159 || objc_string_ref_type_p (strref
)));
160 // || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
163 /* Handle a "format_arg" attribute; arguments as in
164 struct attribute_spec.handler. */
166 handle_frr_format_arg_attribute (tree
*node
, tree
ARG_UNUSED (name
),
167 tree args
, int flags
, bool *no_add_attrs
)
170 tree format_num_expr
= TREE_VALUE (args
);
171 unsigned HOST_WIDE_INT format_num
= 0;
173 if (!get_constant (format_num_expr
, &format_num
, 0))
175 error ("format string has invalid operand number");
176 *no_add_attrs
= true;
180 if (prototype_p (type
))
182 /* The format arg can be any string reference valid for the language and
183 target. We cannot be more specific in this case. */
184 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
188 if (!valid_stringptr_type_p (TREE_TYPE (type
)))
190 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
191 error ("function does not return string type");
192 *no_add_attrs
= true;
199 /* Verify that the format_num argument is actually a string reference suitable,
200 for the language dialect and target (in case the format attribute is in
201 error). When we know the specific reference type expected, this is also
204 check_format_string (tree fntype
, unsigned HOST_WIDE_INT format_num
,
205 int flags
, bool *no_add_attrs
, int expected_format_type
)
207 unsigned HOST_WIDE_INT i
;
208 bool is_target_sref
, is_char_ref
;
211 function_args_iterator iter
;
214 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
222 || !valid_stringptr_type_p (ref
))
224 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
225 error ("format string argument is not a string type");
226 *no_add_attrs
= true;
230 /* We only know that we want a suitable string reference. */
231 if (expected_format_type
< 0)
234 /* Now check that the arg matches the expected type. */
236 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
238 fmt_flags
= format_flags (expected_format_type
);
239 is_target_sref
= false;
241 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
244 return true; /* OK, we expected a char and found one. */
247 error ("found a %qT but the format argument should be a string",
249 *no_add_attrs
= true;
254 /* We expect a string object type as the format arg. */
257 error ("format argument should be a %qs reference but a string was found", format_name (expected_format_type
));
258 *no_add_attrs
= true;
262 /* We will allow a target string ref to match only itself. */
263 if (first_target_format_type
264 && expected_format_type
>= first_target_format_type
269 error ("format argument should be a %qs reference",
270 format_name (expected_format_type
));
271 *no_add_attrs
= true;
278 /* Verify EXPR is a constant, and store its value.
279 If validated_p is true there should be no errors.
280 Returns true on success, false otherwise. */
282 get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
284 if (!tree_fits_uhwi_p (expr
))
286 gcc_assert (!validated_p
);
290 *value
= TREE_INT_CST_LOW (expr
);
295 /* Decode the arguments to a "format" attribute into a
296 function_format_info structure. It is already known that the list
297 is of the right length. If VALIDATED_P is true, then these
298 attributes have already been validated and must not be erroneous;
299 if false, it will give an error message. Returns true if the
300 attributes are successfully decoded, false otherwise. */
303 decode_format_attr (tree args
, function_format_info
*info
, int validated_p
)
305 tree format_type_id
= TREE_VALUE (args
);
306 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
307 tree first_arg_num_expr
308 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
310 if (TREE_CODE (format_type_id
) != STRING_CST
)
312 gcc_assert (!validated_p
);
313 error ("unrecognized format specifier");
318 const char *p
= TREE_STRING_POINTER (format_type_id
);
320 p
= convert_format_name_to_system_name (p
);
322 info
->format_type
= decode_format_type (p
);
324 if (info
->format_type
== format_type_error
)
326 gcc_assert (!validated_p
);
327 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
333 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
335 error ("format string has invalid operand number");
339 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
341 error ("%<...%> has invalid operand number");
345 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
347 gcc_assert (!validated_p
);
348 error ("format string argument follows the arguments to be formatted");
355 /* Check a call to a format function against a parameter list. */
357 /* The C standard version C++ is treated as equivalent to
358 or inheriting from, for the purpose of format features supported. */
359 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
360 /* The C standard version we are checking formats against when pedantic. */
361 #define C_STD_VER ((int) (c_dialect_cxx () \
362 ? CPLUSPLUS_STD_VER \
365 : (flag_isoc94 ? STD_C94 : STD_C89))))
366 /* The name to give to the standard version we are warning about when
367 pedantic. FEATURE_VER is the version in which the feature warned out
368 appeared, which is higher than C_STD_VER. */
369 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
370 ? (cxx_dialect < cxx11 ? "ISO C++98" \
372 : ((FEATURE_VER) == STD_EXT \
375 /* Adjust a C standard version, which may be STD_C9L, to account for
376 -Wno-long-long. Returns other standard versions unchanged. */
377 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
378 ? (warn_long_long ? STD_C99 : STD_C89) \
381 /* Enum describing the kind of specifiers present in the format and
382 requiring an argument. */
383 enum format_specifier_kind
{
386 CF_KIND_FIELD_PRECISION
389 static const char *kind_descriptions
[] = {
391 N_("field width specifier"),
392 N_("field precision specifier")
395 /* Structure describing details of a type expected in format checking,
396 and the type to check against it. */
397 struct format_wanted_type
399 /* The type wanted. */
401 /* The name of this type to use in diagnostics. */
402 const char *wanted_type_name
;
403 /* Should be type checked just for scalar width identity. */
404 int scalar_identity_flag
;
405 /* The level of indirection through pointers at which this type occurs. */
407 /* Whether, when pointer_count is 1, to allow any character type when
408 pedantic, rather than just the character or void type specified. */
409 int char_lenient_flag
;
410 /* Whether the argument, dereferenced once, is written into and so the
411 argument must not be a pointer to a const-qualified type. */
413 /* Whether the argument, dereferenced once, is read from and so
414 must not be a NULL pointer. */
415 int reading_from_flag
;
416 /* The kind of specifier that this type is used for. */
417 enum format_specifier_kind kind
;
418 /* The starting character of the specifier. This never includes the
419 initial percent sign. */
420 const char *format_start
;
421 /* The length of the specifier. */
423 /* The actual parameter to check against the wanted type. */
425 /* The argument number of that parameter. */
427 /* The offset location of this argument with respect to the format
429 unsigned int offset_loc
;
430 /* The next type to check for this format conversion, or NULL if none. */
431 struct format_wanted_type
*next
;
434 /* Convenience macro for format_length_info meaning unused. */
435 #define NO_FMT NULL, FMT_LEN_none, STD_C89
437 static const format_length_info printf_length_specs
[] =
439 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
440 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
441 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
442 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
443 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
444 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
445 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
446 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
447 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
448 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
449 { NO_FMT
, NO_FMT
, 0 }
452 static const format_flag_spec printf_flag_specs
[] =
454 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
455 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
456 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
457 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
458 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
459 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
460 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
461 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
462 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
463 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
464 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
468 static const format_flag_pair printf_flag_pairs
[] =
472 { '0', 'p', 1, 'i' },
477 static kernel_ext_fmt ext_p
[ETAB_SZ
] = {
480 static kernel_ext_fmt ext_d
[ETAB_SZ
] = {
484 static const format_char_info print_char_table
[] =
486 /* C89 conversion specifiers. */
487 /* none, hh, h, l, ll, L, z, t, j, H, D, DD */
488 { "di", 0, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_S64
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +'I", "i", NULL
, ext_d
},
489 { "oxX", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_U64
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
, NULL
},
490 { "u", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_U64
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0'I", "i", NULL
, NULL
},
491 { "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
, NULL
},
492 { "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
, NULL
},
493 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
, NULL
},
494 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
, NULL
},
495 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "c", NULL
, ext_p
},
496 { "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
, NULL
},
497 /* C99 conversion specifiers. */
498 { "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
, NULL
},
499 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "", NULL
, NULL
},
500 /* X/Open conversion specifiers. */
501 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
, NULL
},
502 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
, NULL
},
503 /* GNU conversion specifiers. */
504 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
, NULL
},
505 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
, NULL
}
508 /* This must be in the same order as enum format_type. */
509 static const format_kind_info format_types_orig
[] =
511 { "frr_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
512 printf_flag_specs
, printf_flag_pairs
,
513 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
514 'w', 0, 'p', 0, 'L', 0,
515 &integer_type_node
, &integer_type_node
519 /* This layer of indirection allows GCC to reassign format_types with
520 new data if necessary, while still allowing the original data to be
522 static const format_kind_info
*format_types
= format_types_orig
;
524 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
526 /* Structure detailing the results of checking a format function call
527 where the format expression may be a conditional expression with
528 many leaves resulting from nested conditional expressions. */
529 struct format_check_results
531 /* Number of leaves of the format argument that could not be checked
532 as they were not string literals. */
533 int number_non_literal
;
534 /* Number of leaves of the format argument that were null pointers or
535 string literals, but had extra format arguments. */
536 int number_extra_args
;
537 location_t extra_arg_loc
;
538 /* Number of leaves of the format argument that were null pointers or
539 string literals, but had extra format arguments and used $ operand
541 int number_dollar_extra_args
;
542 /* Number of leaves of the format argument that were wide string
545 /* Number of leaves of the format argument that are not array of "char". */
547 /* Number of leaves of the format argument that were empty strings. */
549 /* Number of leaves of the format argument that were unterminated
551 int number_unterminated
;
552 /* Number of leaves of the format argument that were not counted above. */
554 /* Location of the format string. */
555 location_t format_string_loc
;
558 struct format_check_context
560 format_check_results
*res
;
561 function_format_info
*info
;
563 vec
<location_t
> *arglocs
;
566 /* Return the format name (as specified in the original table) for the format
567 type indicated by format_num. */
569 format_name (int format_num
)
571 if (format_num
>= 0 && format_num
< n_format_types
)
572 return format_types
[format_num
].name
;
576 /* Return the format flags (as specified in the original table) for the format
577 type indicated by format_num. */
579 format_flags (int format_num
)
581 if (format_num
>= 0 && format_num
< n_format_types
)
582 return format_types
[format_num
].flags
;
586 static void check_format_info (function_format_info
*, tree
,
588 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
589 static void check_format_info_main (format_check_results
*,
590 function_format_info
*, const char *,
593 unsigned HOST_WIDE_INT
,
594 object_allocator
<format_wanted_type
> &,
597 static void init_dollar_format_checking (int, tree
);
598 static int maybe_read_dollar_number (const char **, int,
599 tree
, tree
*, const format_kind_info
*);
600 static bool avoid_dollar_number (const char *);
601 static void finish_dollar_format_checking (format_check_results
*, int);
603 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
606 static void check_format_types (const substring_loc
&fmt_loc
,
607 format_wanted_type
*,
608 const format_kind_info
*fki
,
609 int offset_to_type_start
,
610 char conversion_char
,
611 vec
<location_t
> *arglocs
);
612 static void format_type_warning (const substring_loc
&fmt_loc
,
613 location_t param_loc
,
614 format_wanted_type
*, tree
,
616 const format_kind_info
*fki
,
617 int offset_to_type_start
,
618 char conversion_char
,
619 const char *extra
= NULL
);
621 static bool check_kef_type (const substring_loc
&fmt_loc
,
622 const struct kernel_ext_fmt
*kef
,
626 const format_kind_info
*fki
,
627 int offset_to_type_start
,
628 char conversion_char
,
629 vec
<location_t
> *arglocs
);
631 /* Decode a format type from a string, returning the type, or
632 format_type_error if not valid, in which case the caller should print an
635 decode_format_type (const char *s
)
640 s
= convert_format_name_to_system_name (s
);
642 for (i
= 0; i
< n_format_types
; i
++)
645 if (!strcmp (s
, format_types
[i
].name
))
647 alen
= strlen (format_types
[i
].name
);
648 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
649 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
650 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
653 return format_type_error
;
657 /* Check the argument list of a call to printf, scanf, etc.
658 ATTRS are the attributes on the function type. There are NARGS argument
659 values in the array ARGARRAY.
660 Also, if -Wsuggest-attribute=format,
661 warn for calls to vprintf or vscanf in functions with no such format
662 attribute themselves. */
665 check_function_format (tree attrs
, int nargs
, tree
*argarray
,
666 vec
<location_t
> *arglocs
)
670 /* See if this function has any format attributes. */
671 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
673 if (is_attribute_p ("frr_format", TREE_PURPOSE (a
)))
676 function_format_info info
;
677 decode_format_attr (TREE_VALUE (a
), &info
, /*validated=*/true);
680 /* FIXME: Rewrite all the internal functions in this file
681 to use the ARGARRAY directly instead of constructing this
683 tree params
= NULL_TREE
;
685 for (i
= nargs
- 1; i
>= 0; i
--)
686 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
687 check_format_info (&info
, params
, arglocs
);
690 /* Attempt to detect whether the current function might benefit
691 from the format attribute if the called function is decorated
692 with it. Avoid using calls with string literal formats for
693 guidance since those are unlikely to be viable candidates. */
694 if (warn_suggest_attribute_format
695 && current_function_decl
!= NULL_TREE
696 && info
.first_arg_num
== 0
697 && (format_types
[info
.format_type
].flags
698 & (int) FMT_FLAG_ARG_CONVERT
)
699 /* c_strlen will fail for a function parameter but succeed
700 for a literal or constant array. */
701 && !c_strlen (argarray
[info
.format_num
- 1], 1))
704 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
707 if (is_attribute_p ("frr_format", TREE_PURPOSE (c
))
708 && (decode_format_type (IDENTIFIER_POINTER
709 (TREE_VALUE (TREE_VALUE (c
))))
710 == info
.format_type
))
714 /* Check if the current function has a parameter to which
715 the format attribute could be attached; if not, it
716 can't be a candidate for a format attribute, despite
717 the vprintf-like or vscanf-like call. */
719 for (args
= DECL_ARGUMENTS (current_function_decl
);
721 args
= DECL_CHAIN (args
))
723 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
724 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
729 warning (OPT_Wsuggest_attribute_format
,
730 "function %qD might be a candidate for %qs %<frr_format%> attribute",
731 current_function_decl
,
732 format_types
[info
.format_type
].name
);
740 /* Variables used by the checking of $ operand number formats. */
741 static char *dollar_arguments_used
= NULL
;
742 static char *dollar_arguments_pointer_p
= NULL
;
743 static int dollar_arguments_alloc
= 0;
744 static int dollar_arguments_count
;
745 static int dollar_first_arg_num
;
746 static int dollar_max_arg_used
;
747 static int dollar_format_warned
;
749 /* Initialize the checking for a format string that may contain $
750 parameter number specifications; we will need to keep track of whether
751 each parameter has been used. FIRST_ARG_NUM is the number of the first
752 argument that is a parameter to the format, or 0 for a vprintf-style
753 function; PARAMS is the list of arguments starting at this argument. */
756 init_dollar_format_checking (int first_arg_num
, tree params
)
758 tree oparams
= params
;
760 dollar_first_arg_num
= first_arg_num
;
761 dollar_arguments_count
= 0;
762 dollar_max_arg_used
= 0;
763 dollar_format_warned
= 0;
764 if (first_arg_num
> 0)
768 dollar_arguments_count
++;
769 params
= TREE_CHAIN (params
);
772 if (dollar_arguments_alloc
< dollar_arguments_count
)
774 free (dollar_arguments_used
);
775 free (dollar_arguments_pointer_p
);
776 dollar_arguments_alloc
= dollar_arguments_count
;
777 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
778 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
780 if (dollar_arguments_alloc
)
782 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
783 if (first_arg_num
> 0)
789 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
791 params
= TREE_CHAIN (params
);
799 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
800 is set, it is an error if one is not found; otherwise, it is OK. If
801 such a number is found, check whether it is within range and mark that
802 numbered operand as being used for later checking. Returns the operand
803 number if found and within range, zero if no such number was found and
804 this is OK, or -1 on error. PARAMS points to the first operand of the
805 format; PARAM_PTR is made to point to the parameter referred to. If
806 a $ format is found, *FORMAT is updated to point just after it. */
809 maybe_read_dollar_number (const char **format
,
810 int dollar_needed
, tree params
, tree
*param_ptr
,
811 const format_kind_info
*fki
)
815 const char *fcp
= *format
;
820 warning (OPT_Wformat_
, "missing $ operand number in format");
828 while (ISDIGIT (*fcp
))
831 nargnum
= 10 * argnum
+ (*fcp
- '0');
832 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
841 warning (OPT_Wformat_
, "missing $ operand number in format");
848 if (pedantic
&& !dollar_format_warned
)
850 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
851 C_STD_NAME (STD_EXT
));
852 dollar_format_warned
= 1;
854 if (overflow_flag
|| argnum
== 0
855 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
857 warning (OPT_Wformat_
, "operand number out of range in format");
860 if (argnum
> dollar_max_arg_used
)
861 dollar_max_arg_used
= argnum
;
862 /* For vprintf-style functions we may need to allocate more memory to
863 track which arguments are used. */
864 while (dollar_arguments_alloc
< dollar_max_arg_used
)
867 nalloc
= 2 * dollar_arguments_alloc
+ 16;
868 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
870 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
872 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
873 nalloc
- dollar_arguments_alloc
);
874 dollar_arguments_alloc
= nalloc
;
876 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
877 && dollar_arguments_used
[argnum
- 1] == 1)
879 dollar_arguments_used
[argnum
- 1] = 2;
880 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
884 dollar_arguments_used
[argnum
- 1] = 1;
885 if (dollar_first_arg_num
)
889 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
890 *param_ptr
= TREE_CHAIN (*param_ptr
);
892 /* This case shouldn't be caught here. */
893 gcc_assert (*param_ptr
);
900 /* Ensure that FORMAT does not start with a decimal number followed by
901 a $; give a diagnostic and return true if it does, false otherwise. */
904 avoid_dollar_number (const char *format
)
906 if (!ISDIGIT (*format
))
908 while (ISDIGIT (*format
))
912 warning (OPT_Wformat_
, "%<$%> operand number used after format without operand number");
919 /* Finish the checking for a format string that used $ operand number formats
920 instead of non-$ formats. We check for unused operands before used ones
921 (a serious error, since the implementation of the format function
922 can't know what types to pass to va_arg to find the later arguments).
923 and for unused operands at the end of the format (if we know how many
924 arguments the format had, so not for vprintf). If there were operand
925 numbers out of range on a non-vprintf-style format, we won't have reached
926 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
930 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
933 bool found_pointer_gap
= false;
934 for (i
= 0; i
< dollar_max_arg_used
; i
++)
936 if (!dollar_arguments_used
[i
])
938 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
939 || dollar_arguments_pointer_p
[i
]))
940 found_pointer_gap
= true;
942 warning_at (res
->format_string_loc
, OPT_Wformat_
,
943 "format argument %d unused before used argument %d in %<$%>-style format",
944 i
+ 1, dollar_max_arg_used
);
947 if (found_pointer_gap
948 || (dollar_first_arg_num
949 && dollar_max_arg_used
< dollar_arguments_count
))
952 res
->number_dollar_extra_args
++;
957 /* Retrieve the specification for a format flag. SPEC contains the
958 specifications for format flags for the applicable kind of format.
959 FLAG is the flag in question. If PREDICATES is NULL, the basic
960 spec for that flag must be retrieved and must exist. If
961 PREDICATES is not NULL, it is a string listing possible predicates
962 for the spec entry; if an entry predicated on any of these is
963 found, it is returned, otherwise NULL is returned. */
965 static const format_flag_spec
*
966 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
969 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
971 if (spec
[i
].flag_char
!= flag
)
973 if (predicates
!= NULL
)
975 if (spec
[i
].predicate
!= 0
976 && strchr (predicates
, spec
[i
].predicate
) != 0)
979 else if (spec
[i
].predicate
== 0)
982 gcc_assert (predicates
);
987 /* Check the argument list of a call to printf, scanf, etc.
988 INFO points to the function_format_info structure.
989 PARAMS is the list of argument values. */
992 check_format_info (function_format_info
*info
, tree params
,
993 vec
<location_t
> *arglocs
)
995 format_check_context format_ctx
;
996 unsigned HOST_WIDE_INT arg_num
;
998 format_check_results res
;
999 /* Skip to format argument. If the argument isn't available, there's
1000 no work for us to do; prototype checking will catch the problem. */
1001 for (arg_num
= 1; ; ++arg_num
)
1005 if (arg_num
== info
->format_num
)
1007 params
= TREE_CHAIN (params
);
1009 format_tree
= TREE_VALUE (params
);
1010 params
= TREE_CHAIN (params
);
1011 if (format_tree
== 0)
1014 res
.number_non_literal
= 0;
1015 res
.number_extra_args
= 0;
1016 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1017 res
.number_dollar_extra_args
= 0;
1018 res
.number_wide
= 0;
1019 res
.number_non_char
= 0;
1020 res
.number_empty
= 0;
1021 res
.number_unterminated
= 0;
1022 res
.number_other
= 0;
1023 res
.format_string_loc
= input_location
;
1025 format_ctx
.res
= &res
;
1026 format_ctx
.info
= info
;
1027 format_ctx
.params
= params
;
1028 format_ctx
.arglocs
= arglocs
;
1030 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1031 format_tree
, arg_num
, OPT_Wformat_
);
1033 location_t loc
= format_ctx
.res
->format_string_loc
;
1035 if (res
.number_non_literal
> 0)
1037 /* Functions taking a va_list normally pass a non-literal format
1038 string. These functions typically are declared with
1039 first_arg_num == 0, so avoid warning in those cases. */
1040 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1042 /* For strftime-like formats, warn for not checking the format
1043 string; but there are no arguments to check. */
1044 warning_at (loc
, OPT_Wformat_nonliteral
,
1045 "format not a string literal, format string not checked");
1047 else if (info
->first_arg_num
!= 0)
1049 /* If there are no arguments for the format at all, we may have
1050 printf (foo) which is likely to be a security hole. */
1051 while (arg_num
+ 1 < info
->first_arg_num
)
1055 params
= TREE_CHAIN (params
);
1058 if (params
== 0 && warn_format_security
)
1059 warning_at (loc
, OPT_Wformat_security
,
1060 "format not a string literal and no format arguments");
1061 else if (params
== 0 && warn_format_nonliteral
)
1062 warning_at (loc
, OPT_Wformat_nonliteral
,
1063 "format not a string literal and no format arguments");
1065 warning_at (loc
, OPT_Wformat_nonliteral
,
1066 "format not a string literal, argument types not checked");
1070 /* If there were extra arguments to the format, normally warn. However,
1071 the standard does say extra arguments are ignored, so in the specific
1072 case where we have multiple leaves (conditional expressions or
1073 ngettext) allow extra arguments if at least one leaf didn't have extra
1074 arguments, but was otherwise OK (either non-literal or checked OK).
1075 If the format is an empty string, this should be counted similarly to the
1076 case of extra format arguments. */
1077 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1078 && res
.number_other
== 0)
1080 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1081 res
.extra_arg_loc
= loc
;
1082 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1083 "too many arguments for format");
1085 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1086 && res
.number_other
== 0)
1087 warning_at (loc
, OPT_Wformat_extra_args
, "unused arguments in %<$%>-style format");
1088 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1089 && res
.number_other
== 0)
1090 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1091 format_types
[info
->format_type
].name
);
1093 if (res
.number_wide
> 0)
1094 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1096 if (res
.number_non_char
> 0)
1097 warning_at (loc
, OPT_Wformat_
,
1098 "format string is not an array of type %qs", "char");
1100 if (res
.number_unterminated
> 0)
1101 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1104 /* Callback from check_function_arguments_recurse to check a
1105 format string. FORMAT_TREE is the format parameter. ARG_NUM
1106 is the number of the format argument. CTX points to a
1107 format_check_context. */
1110 check_format_arg (void *ctx
, tree format_tree
,
1111 unsigned HOST_WIDE_INT arg_num
)
1113 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1114 format_check_results
*res
= format_ctx
->res
;
1115 function_format_info
*info
= format_ctx
->info
;
1116 tree params
= format_ctx
->params
;
1117 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1120 HOST_WIDE_INT offset
;
1121 const char *format_chars
;
1122 tree array_size
= 0;
1125 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1127 /* Pull out a constant value if the front end didn't, and handle location
1129 format_tree
= fold_for_warn (format_tree
);
1130 STRIP_NOPS (format_tree
);
1132 if (integer_zerop (format_tree
))
1134 /* Skip to first argument to check, so we can see if this format
1135 has any arguments (it shouldn't). */
1136 while (arg_num
+ 1 < info
->first_arg_num
)
1140 params
= TREE_CHAIN (params
);
1145 res
->number_other
++;
1148 if (res
->number_extra_args
== 0)
1149 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1151 res
->number_extra_args
++;
1157 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1161 arg0
= TREE_OPERAND (format_tree
, 0);
1162 arg1
= TREE_OPERAND (format_tree
, 1);
1165 if (TREE_CODE (arg1
) == INTEGER_CST
)
1169 res
->number_non_literal
++;
1172 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1173 if (!cst_and_fits_in_hwi (arg1
))
1175 res
->number_non_literal
++;
1178 offset
= int_cst_value (arg1
);
1180 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1182 res
->number_non_literal
++;
1185 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1186 format_tree
= TREE_OPERAND (format_tree
, 0);
1187 if (format_types
[info
->format_type
].flags
1188 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1190 /* We cannot examine this string here - but we can check that it is
1192 if (TREE_CODE (format_tree
) != CONST_DECL
)
1194 res
->number_non_literal
++;
1197 /* Skip to first argument to check. */
1198 while (arg_num
+ 1 < info
->first_arg_num
)
1202 params
= TREE_CHAIN (params
);
1207 if (TREE_CODE (format_tree
) == ARRAY_REF
1208 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1209 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1210 format_tree
= TREE_OPERAND (format_tree
, 0);
1213 res
->number_non_literal
++;
1216 if (VAR_P (format_tree
)
1217 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1218 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1219 && TREE_CODE (array_init
) == STRING_CST
)
1221 /* Extract the string constant initializer. Note that this may include
1222 a trailing NUL character that is not in the array (e.g.
1223 const char a[3] = "foo";). */
1224 array_size
= DECL_SIZE_UNIT (format_tree
);
1225 format_tree
= array_init
;
1227 if (TREE_CODE (format_tree
) != STRING_CST
)
1229 res
->number_non_literal
++;
1232 tree underlying_type
1233 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
)));
1234 if (underlying_type
!= char_type_node
)
1236 if (underlying_type
== char16_type_node
1237 || underlying_type
== char32_type_node
1238 || underlying_type
== wchar_type_node
)
1241 res
->number_non_char
++;
1244 format_chars
= TREE_STRING_POINTER (format_tree
);
1245 format_length
= TREE_STRING_LENGTH (format_tree
);
1246 if (array_size
!= 0)
1248 /* Variable length arrays can't be initialized. */
1249 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1251 if (tree_fits_shwi_p (array_size
))
1253 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1254 if (array_size_value
> 0
1255 && array_size_value
== (int) array_size_value
1256 && format_length
> array_size_value
)
1257 format_length
= array_size_value
;
1262 if (offset
>= format_length
)
1264 res
->number_non_literal
++;
1267 format_chars
+= offset
;
1268 format_length
-= offset
;
1270 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1272 res
->number_unterminated
++;
1275 if (format_length
== 0)
1277 res
->number_empty
++;
1281 /* Skip to first argument to check. */
1282 while (arg_num
+ 1 < info
->first_arg_num
)
1286 params
= TREE_CHAIN (params
);
1289 /* Provisionally increment res->number_other; check_format_info_main
1290 will decrement it if it finds there are extra arguments, but this way
1291 need not adjust it for every return. */
1292 res
->number_other
++;
1293 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1294 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1295 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1298 /* Support class for argument_parser and check_format_info_main.
1299 Tracks any flag characters that have been applied to the
1300 current argument. */
1306 bool has_char_p (char ch
) const;
1307 void add_char (char ch
);
1308 void validate (const format_kind_info
*fki
,
1309 const format_char_info
*fci
,
1310 const format_flag_spec
*flag_specs
,
1311 const char * const format_chars
,
1312 tree format_string_cst
,
1313 location_t format_string_loc
,
1314 const char * const orig_format_chars
,
1317 int get_alloc_flag (const format_kind_info
*fki
);
1318 int assignment_suppression_p (const format_kind_info
*fki
);
1321 char m_flag_chars
[256];
1324 /* Support struct for argument_parser and check_format_info_main.
1325 Encapsulates any length modifier applied to the current argument. */
1327 struct length_modifier
1330 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1331 scalar_identity_flag (0)
1335 length_modifier (const char *chars_
,
1336 enum format_lengths val_
,
1337 enum format_std_version std_
,
1338 int scalar_identity_flag_
)
1339 : chars (chars_
), val (val_
), std (std_
),
1340 scalar_identity_flag (scalar_identity_flag_
)
1345 enum format_lengths val
;
1346 enum format_std_version std
;
1347 int scalar_identity_flag
;
1350 /* Parsing one argument within a format string. */
1352 class argument_parser
1355 argument_parser (function_format_info
*info
, const char *&format_chars
,
1356 tree format_string_cst
,
1357 const char * const orig_format_chars
,
1358 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1359 int &has_operand_number
, tree first_fillin_param
,
1360 object_allocator
<format_wanted_type
> &fwt_pool_
,
1361 vec
<location_t
> *arglocs
);
1363 bool read_any_dollar ();
1365 bool read_format_flags ();
1368 read_any_format_width (tree
¶ms
,
1369 unsigned HOST_WIDE_INT
&arg_num
);
1372 read_any_format_left_precision ();
1375 read_any_format_precision (tree
¶ms
,
1376 unsigned HOST_WIDE_INT
&arg_num
);
1378 void handle_alloc_chars ();
1380 length_modifier
read_any_length_modifier ();
1382 void read_any_other_modifier ();
1384 const format_char_info
*find_format_char_info (char format_char
);
1387 validate_flag_pairs (const format_char_info
*fci
,
1391 give_y2k_warnings (const format_char_info
*fci
,
1394 void parse_any_scan_set (const format_char_info
*fci
);
1396 bool handle_conversions (const format_char_info
*fci
,
1397 const length_modifier
&len_modifier
,
1399 const char *&wanted_type_name
,
1400 unsigned HOST_WIDE_INT
&arg_num
,
1405 check_argument_type (const format_char_info
*fci
,
1406 const struct kernel_ext_fmt
*kef
,
1407 const length_modifier
&len_modifier
,
1409 const char *&wanted_type_name
,
1410 const bool suppressed
,
1411 unsigned HOST_WIDE_INT
&arg_num
,
1413 const int alloc_flag
,
1414 const char * const format_start
,
1415 const char * const type_start
,
1416 location_t fmt_param_loc
,
1417 char conversion_char
);
1420 const function_format_info
*const info
;
1421 const format_kind_info
* const fki
;
1422 const format_flag_spec
* const flag_specs
;
1423 const char *start_of_this_format
;
1424 const char *&format_chars
;
1425 const tree format_string_cst
;
1426 const char * const orig_format_chars
;
1427 const location_t format_string_loc
;
1428 object_allocator
<format_wanted_type
> &fwt_pool
;
1429 flag_chars_t
&flag_chars
;
1431 tree main_arg_params
;
1432 int &has_operand_number
;
1433 const tree first_fillin_param
;
1434 format_wanted_type width_wanted_type
;
1435 format_wanted_type precision_wanted_type
;
1437 format_wanted_type main_wanted_type
;
1439 format_wanted_type
*first_wanted_type
;
1440 format_wanted_type
*last_wanted_type
;
1441 vec
<location_t
> *arglocs
;
1444 /* flag_chars_t's constructor. */
1446 flag_chars_t::flag_chars_t ()
1448 m_flag_chars
[0] = 0;
1451 /* Has CH been seen as a flag within the current argument? */
1454 flag_chars_t::has_char_p (char ch
) const
1456 return strchr (m_flag_chars
, ch
) != 0;
1459 /* Add CH to the flags seen within the current argument. */
1462 flag_chars_t::add_char (char ch
)
1464 int i
= strlen (m_flag_chars
);
1465 m_flag_chars
[i
++] = ch
;
1466 m_flag_chars
[i
] = 0;
1469 /* Validate the individual flags used, removing any that are invalid. */
1472 flag_chars_t::validate (const format_kind_info
*fki
,
1473 const format_char_info
*fci
,
1474 const format_flag_spec
*flag_specs
,
1475 const char * const format_chars
,
1476 tree format_string_cst
,
1477 location_t format_string_loc
,
1478 const char * const orig_format_chars
,
1484 bool quotflag
= false;
1486 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
1488 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1489 m_flag_chars
[i
], NULL
);
1490 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
1491 if (m_flag_chars
[i
] == fki
->length_code_char
)
1494 /* Remember if a quoting flag is seen. */
1495 quotflag
|= s
->quoting
;
1497 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
1499 format_warning_at_char (format_string_loc
, format_string_cst
,
1500 format_chars
- orig_format_chars
,
1502 "%s used with %<%%%c%> %s format",
1503 _(s
->name
), format_char
, fki
->name
);
1509 const format_flag_spec
*t
;
1510 if (ADJ_STD (s
->std
) > C_STD_VER
)
1511 warning_at (format_string_loc
, OPT_Wformat_
,
1512 "%s does not support %s",
1513 C_STD_NAME (s
->std
), _(s
->long_name
));
1514 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
1515 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
1517 const char *long_name
= (t
->long_name
!= NULL
1520 if (ADJ_STD (t
->std
) > C_STD_VER
)
1521 warning_at (format_string_loc
, OPT_Wformat_
,
1522 "%s does not support %s with the %<%%%c%> %s format",
1523 C_STD_NAME (t
->std
), _(long_name
),
1524 format_char
, fki
->name
);
1528 /* Detect quoting directives used within a quoted sequence, such
1529 as GCC's "%<...%qE". */
1530 if (quoted
&& s
->quoting
)
1532 format_warning_at_char (format_string_loc
, format_string_cst
,
1533 format_chars
- orig_format_chars
- 1,
1535 "%s used within a quoted sequence",
1539 m_flag_chars
[i
- d
] = 0;
1543 && strchr (fci
->flags2
, '\''))
1545 format_warning_at_char (format_string_loc
, format_string_cst
,
1546 format_chars
- orig_format_chars
,
1548 "%qc conversion used unquoted",
1553 /* Determine if an assignment-allocation has been set, requiring
1554 an extra char ** for writing back a dynamically-allocated char *.
1555 This is for handling the optional 'm' character in scanf. */
1558 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
1560 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1561 && has_char_p ('a'))
1563 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
1568 /* Determine if an assignment-suppression character was seen.
1569 ('*' in scanf, for discarding the converted input). */
1572 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
1574 if (fki
->suppression_char
1575 && has_char_p (fki
->suppression_char
))
1580 /* Constructor for argument_parser. Initialize for parsing one
1581 argument within a format string. */
1584 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
1585 tree format_string_cst_
,
1586 const char * const orig_format_chars_
,
1587 location_t format_string_loc_
,
1588 flag_chars_t
&flag_chars_
,
1589 int &has_operand_number_
,
1590 tree first_fillin_param_
,
1591 object_allocator
<format_wanted_type
> &fwt_pool_
,
1592 vec
<location_t
> *arglocs_
)
1594 fki (&format_types
[info
->format_type
]),
1595 flag_specs (fki
->flag_specs
),
1596 start_of_this_format (format_chars_
),
1597 format_chars (format_chars_
),
1598 format_string_cst (format_string_cst_
),
1599 orig_format_chars (orig_format_chars_
),
1600 format_string_loc (format_string_loc_
),
1601 fwt_pool (fwt_pool_
),
1602 flag_chars (flag_chars_
),
1604 main_arg_params (NULL
),
1605 has_operand_number (has_operand_number_
),
1606 first_fillin_param (first_fillin_param_
),
1607 first_wanted_type (NULL
),
1608 last_wanted_type (NULL
),
1613 /* Handle dollars at the start of format arguments, setting up main_arg_params
1616 Return true if format parsing is to continue, false otherwise. */
1619 argument_parser::read_any_dollar ()
1621 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
1623 /* Possibly read a $ operand number at the start of the format.
1624 If one was previously used, one is required here. If one
1625 is not used here, we can't immediately conclude this is a
1626 format without them, since it could be printf %m or scanf %*. */
1628 opnum
= maybe_read_dollar_number (&format_chars
, 0,
1630 &main_arg_params
, fki
);
1635 has_operand_number
= 1;
1636 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
1639 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
1641 if (avoid_dollar_number (format_chars
))
1647 /* Read any format flags, but do not yet validate them beyond removing
1648 duplicates, since in general validation depends on the rest of
1651 Return true if format parsing is to continue, false otherwise. */
1654 argument_parser::read_format_flags ()
1656 while (*format_chars
!= 0
1657 && strchr (fki
->flag_chars
, *format_chars
) != 0)
1659 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1660 *format_chars
, NULL
);
1661 if (flag_chars
.has_char_p (*format_chars
))
1663 format_warning_at_char (format_string_loc
, format_string_cst
,
1664 format_chars
+ 1 - orig_format_chars
,
1666 "repeated %s in format", _(s
->name
));
1669 flag_chars
.add_char (*format_chars
);
1671 if (s
->skip_next_char
)
1674 if (*format_chars
== 0)
1676 warning_at (format_string_loc
, OPT_Wformat_
,
1677 "missing fill character at end of strfmon format");
1687 /* Read any format width, possibly * or *m$.
1689 Return true if format parsing is to continue, false otherwise. */
1693 read_any_format_width (tree
¶ms
,
1694 unsigned HOST_WIDE_INT
&arg_num
)
1696 if (!fki
->width_char
)
1699 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
1701 flag_chars
.add_char (fki
->width_char
);
1702 /* "...a field width...may be indicated by an asterisk.
1703 In this case, an int argument supplies the field width..." */
1705 if (has_operand_number
!= 0)
1708 opnum
= maybe_read_dollar_number (&format_chars
,
1709 has_operand_number
== 1,
1716 has_operand_number
= 1;
1717 arg_num
= opnum
+ info
->first_arg_num
- 1;
1720 has_operand_number
= 0;
1724 if (avoid_dollar_number (format_chars
))
1727 if (info
->first_arg_num
!= 0)
1734 cur_param
= TREE_VALUE (params
);
1735 if (has_operand_number
<= 0)
1737 params
= TREE_CHAIN (params
);
1741 width_wanted_type
.wanted_type
= *fki
->width_type
;
1742 width_wanted_type
.wanted_type_name
= NULL
;
1743 width_wanted_type
.pointer_count
= 0;
1744 width_wanted_type
.char_lenient_flag
= 0;
1745 width_wanted_type
.scalar_identity_flag
= 0;
1746 width_wanted_type
.writing_in_flag
= 0;
1747 width_wanted_type
.reading_from_flag
= 0;
1748 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
1749 width_wanted_type
.format_start
= format_chars
- 1;
1750 width_wanted_type
.format_length
= 1;
1751 width_wanted_type
.param
= cur_param
;
1752 width_wanted_type
.arg_num
= arg_num
;
1753 width_wanted_type
.offset_loc
=
1754 format_chars
- orig_format_chars
;
1755 width_wanted_type
.next
= NULL
;
1756 if (last_wanted_type
!= 0)
1757 last_wanted_type
->next
= &width_wanted_type
;
1758 if (first_wanted_type
== 0)
1759 first_wanted_type
= &width_wanted_type
;
1760 last_wanted_type
= &width_wanted_type
;
1765 /* Possibly read a numeric width. If the width is zero,
1766 we complain if appropriate. */
1767 int non_zero_width_char
= FALSE
;
1768 int found_width
= FALSE
;
1769 while (ISDIGIT (*format_chars
))
1772 if (*format_chars
!= '0')
1773 non_zero_width_char
= TRUE
;
1776 if (found_width
&& !non_zero_width_char
&&
1777 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
1778 warning_at (format_string_loc
, OPT_Wformat_
,
1779 "zero width in %s format", fki
->name
);
1781 flag_chars
.add_char (fki
->width_char
);
1787 /* Read any format left precision (must be a number, not *). */
1789 argument_parser::read_any_format_left_precision ()
1791 if (fki
->left_precision_char
== 0)
1793 if (*format_chars
!= '#')
1797 flag_chars
.add_char (fki
->left_precision_char
);
1798 if (!ISDIGIT (*format_chars
))
1799 format_warning_at_char (format_string_loc
, format_string_cst
,
1800 format_chars
- orig_format_chars
,
1802 "empty left precision in %s format", fki
->name
);
1803 while (ISDIGIT (*format_chars
))
1807 /* Read any format precision, possibly * or *m$.
1809 Return true if format parsing is to continue, false otherwise. */
1813 read_any_format_precision (tree
¶ms
,
1814 unsigned HOST_WIDE_INT
&arg_num
)
1816 if (fki
->precision_char
== 0)
1818 if (*format_chars
!= '.')
1822 flag_chars
.add_char (fki
->precision_char
);
1823 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
1825 /* "...a...precision...may be indicated by an asterisk.
1826 In this case, an int argument supplies the...precision." */
1828 if (has_operand_number
!= 0)
1831 opnum
= maybe_read_dollar_number (&format_chars
,
1832 has_operand_number
== 1,
1839 has_operand_number
= 1;
1840 arg_num
= opnum
+ info
->first_arg_num
- 1;
1843 has_operand_number
= 0;
1847 if (avoid_dollar_number (format_chars
))
1850 if (info
->first_arg_num
!= 0)
1857 cur_param
= TREE_VALUE (params
);
1858 if (has_operand_number
<= 0)
1860 params
= TREE_CHAIN (params
);
1864 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
1865 precision_wanted_type
.wanted_type_name
= NULL
;
1866 precision_wanted_type
.pointer_count
= 0;
1867 precision_wanted_type
.char_lenient_flag
= 0;
1868 precision_wanted_type
.scalar_identity_flag
= 0;
1869 precision_wanted_type
.writing_in_flag
= 0;
1870 precision_wanted_type
.reading_from_flag
= 0;
1871 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
1872 precision_wanted_type
.param
= cur_param
;
1873 precision_wanted_type
.format_start
= format_chars
- 2;
1874 precision_wanted_type
.format_length
= 2;
1875 precision_wanted_type
.arg_num
= arg_num
;
1876 precision_wanted_type
.offset_loc
=
1877 format_chars
- orig_format_chars
;
1878 precision_wanted_type
.next
= NULL
;
1879 if (last_wanted_type
!= 0)
1880 last_wanted_type
->next
= &precision_wanted_type
;
1881 if (first_wanted_type
== 0)
1882 first_wanted_type
= &precision_wanted_type
;
1883 last_wanted_type
= &precision_wanted_type
;
1888 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
1889 && !ISDIGIT (*format_chars
))
1890 format_warning_at_char (format_string_loc
, format_string_cst
,
1891 format_chars
- orig_format_chars
,
1893 "empty precision in %s format", fki
->name
);
1894 while (ISDIGIT (*format_chars
))
1901 /* Parse any assignment-allocation flags, which request an extra
1902 char ** for writing back a dynamically-allocated char *.
1903 This is for handling the optional 'm' character in scanf,
1904 and, before C99, 'a' (for compatibility with a non-standard
1905 GNU libc extension). */
1908 argument_parser::handle_alloc_chars ()
1910 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
1912 flag_chars
.add_char (fki
->alloc_char
);
1916 /* Handle the scanf allocation kludge. */
1917 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1919 if (*format_chars
== 'a' && !flag_isoc99
)
1921 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1922 || format_chars
[1] == '[')
1924 /* 'a' is used as a flag. */
1925 flag_chars
.add_char ('a');
1932 /* Look for length modifiers within the current format argument,
1933 returning a length_modifier instance describing it (or the
1934 default if one is not found).
1936 Issue warnings about non-standard modifiers. */
1939 argument_parser::read_any_length_modifier ()
1941 length_modifier result
;
1943 const format_length_info
*fli
= fki
->length_char_specs
;
1947 while (fli
->name
!= 0
1948 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
1952 format_chars
+= strlen (fli
->name
);
1953 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
1956 result
= length_modifier (fli
->double_name
, fli
->double_index
,
1957 fli
->double_std
, 0);
1961 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
1962 fli
->scalar_identity_flag
);
1964 flag_chars
.add_char (fki
->length_code_char
);
1968 /* Warn if the length modifier is non-standard. */
1969 if (ADJ_STD (result
.std
) > C_STD_VER
)
1970 warning_at (format_string_loc
, OPT_Wformat_
,
1971 "%s does not support the %qs %s length modifier",
1972 C_STD_NAME (result
.std
), result
.chars
,
1979 /* Read any other modifier (strftime E/O). */
1982 argument_parser::read_any_other_modifier ()
1984 if (fki
->modifier_chars
== NULL
)
1987 while (*format_chars
!= 0
1988 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
1990 if (flag_chars
.has_char_p (*format_chars
))
1992 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1993 *format_chars
, NULL
);
1994 format_warning_at_char (format_string_loc
, format_string_cst
,
1995 format_chars
- orig_format_chars
,
1997 "repeated %s in format", _(s
->name
));
2000 flag_chars
.add_char (*format_chars
);
2005 /* Return the format_char_info corresponding to FORMAT_CHAR,
2006 potentially issuing a warning if the format char is
2007 not supported in the C standard version we are checking
2010 Issue a warning and return NULL if it is not found.
2012 Issue warnings about non-standard modifiers. */
2014 const format_char_info
*
2015 argument_parser::find_format_char_info (char format_char
)
2017 const format_char_info
*fci
= fki
->conversion_specs
;
2019 while (fci
->format_chars
!= 0
2020 && strchr (fci
->format_chars
, format_char
) == 0)
2022 if (fci
->format_chars
== 0)
2024 format_warning_at_char (format_string_loc
, format_string_cst
,
2025 format_chars
- orig_format_chars
,
2027 "unknown conversion type character %qc in format",
2034 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2035 format_warning_at_char (format_string_loc
, format_string_cst
,
2036 format_chars
- orig_format_chars
,
2038 "%s does not support the %<%%%c%> %s format",
2039 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2045 /* Validate the pairs of flags used.
2046 Issue warnings about incompatible combinations of flags. */
2049 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2052 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2054 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2056 const format_flag_spec
*s
, *t
;
2057 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2059 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2061 if (bad_flag_pairs
[i
].predicate
!= 0
2062 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2064 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2065 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2066 if (bad_flag_pairs
[i
].ignored
)
2068 if (bad_flag_pairs
[i
].predicate
!= 0)
2069 warning_at (format_string_loc
, OPT_Wformat_
,
2070 "%s ignored with %s and %<%%%c%> %s format",
2071 _(s
->name
), _(t
->name
), format_char
,
2074 warning_at (format_string_loc
, OPT_Wformat_
,
2075 "%s ignored with %s in %s format",
2076 _(s
->name
), _(t
->name
), fki
->name
);
2080 if (bad_flag_pairs
[i
].predicate
!= 0)
2081 warning_at (format_string_loc
, OPT_Wformat_
,
2082 "use of %s and %s together with %<%%%c%> %s format",
2083 _(s
->name
), _(t
->name
), format_char
,
2086 warning_at (format_string_loc
, OPT_Wformat_
,
2087 "use of %s and %s together in %s format",
2088 _(s
->name
), _(t
->name
), fki
->name
);
2093 /* Give Y2K warnings. */
2096 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2099 if (!warn_format_y2k
)
2103 if (strchr (fci
->flags2
, '4') != 0)
2104 if (flag_chars
.has_char_p ('E'))
2108 else if (strchr (fci
->flags2
, '3') != 0)
2110 else if (strchr (fci
->flags2
, '2') != 0)
2113 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2114 "%<%%%c%> yields only last 2 digits of year in some locales", format_char
);
2115 else if (y2k_level
== 2)
2116 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2117 "%<%%%c%> yields only last 2 digits of year",
2121 /* Parse any "scan sets" enclosed in square brackets, e.g.
2122 for scanf-style calls. */
2125 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2127 if (strchr (fci
->flags2
, '[') == NULL
)
2130 /* Skip over scan set, in case it happens to have '%' in it. */
2131 if (*format_chars
== '^')
2133 /* Find closing bracket; if one is hit immediately, then
2134 it's part of the scan set rather than a terminator. */
2135 if (*format_chars
== ']')
2137 while (*format_chars
&& *format_chars
!= ']')
2139 if (*format_chars
!= ']')
2140 /* The end of the format string was reached. */
2141 format_warning_at_char (format_string_loc
, format_string_cst
,
2142 format_chars
- orig_format_chars
,
2144 "no closing %<]%> for %<%%[%> format");
2147 /* Return true if this argument is to be continued to be parsed,
2148 false to skip to next argument. */
2151 argument_parser::handle_conversions (const format_char_info
*fci
,
2152 const length_modifier
&len_modifier
,
2154 const char *&wanted_type_name
,
2155 unsigned HOST_WIDE_INT
&arg_num
,
2159 enum format_std_version wanted_type_std
;
2161 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2164 wanted_type
= (fci
->types
[len_modifier
.val
].type
2165 ? *fci
->types
[len_modifier
.val
].type
: 0);
2166 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2167 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2168 if (wanted_type
== 0)
2170 format_warning_at_char (format_string_loc
, format_string_cst
,
2171 format_chars
- orig_format_chars
,
2173 "use of %qs length modifier with %qc type character has either no effect or undefined behavior",
2174 len_modifier
.chars
, format_char
);
2175 /* Heuristic: skip one argument when an invalid length/type
2176 combination is encountered. */
2179 params
= TREE_CHAIN (params
);
2183 /* Warn if non-standard, provided it is more non-standard
2184 than the length and type characters that may already
2185 have been warned for. */
2186 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2187 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2189 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2190 format_warning_at_char (format_string_loc
, format_string_cst
,
2191 format_chars
- orig_format_chars
,
2193 "%s does not support the %<%%%s%c%> %s format",
2194 C_STD_NAME (wanted_type_std
),
2196 format_char
, fki
->name
);
2202 /* Check type of argument against desired type.
2204 Return true if format parsing is to continue, false otherwise. */
2208 check_argument_type (const format_char_info
*fci
,
2209 const struct kernel_ext_fmt
*kef
,
2210 const length_modifier
&len_modifier
,
2212 const char *&wanted_type_name
,
2213 const bool suppressed
,
2214 unsigned HOST_WIDE_INT
&arg_num
,
2216 const int alloc_flag
,
2217 const char * const format_start
,
2218 const char * const type_start
,
2219 location_t fmt_param_loc
,
2220 char conversion_char
)
2222 if (info
->first_arg_num
== 0)
2225 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2228 if (main_arg_num
!= 0)
2231 warning_at (format_string_loc
, OPT_Wformat_
,
2232 "operand number specified with suppressed assignment");
2234 warning_at (format_string_loc
, OPT_Wformat_
,
2235 "operand number specified for format taking no argument");
2240 format_wanted_type
*wanted_type_ptr
;
2242 if (main_arg_num
!= 0)
2244 arg_num
= main_arg_num
;
2245 params
= main_arg_params
;
2250 if (has_operand_number
> 0)
2252 warning_at (format_string_loc
, OPT_Wformat_
,
2253 "missing $ operand number in format");
2257 has_operand_number
= 0;
2260 wanted_type_ptr
= &main_wanted_type
;
2268 cur_param
= TREE_VALUE (params
);
2269 params
= TREE_CHAIN (params
);
2272 wanted_type_ptr
->wanted_type
= wanted_type
;
2273 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2274 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2275 wanted_type_ptr
->char_lenient_flag
= 0;
2276 if (strchr (fci
->flags2
, 'c') != 0)
2277 wanted_type_ptr
->char_lenient_flag
= 1;
2278 wanted_type_ptr
->scalar_identity_flag
= 0;
2279 if (len_modifier
.scalar_identity_flag
)
2280 wanted_type_ptr
->scalar_identity_flag
= 1;
2281 wanted_type_ptr
->writing_in_flag
= 0;
2282 wanted_type_ptr
->reading_from_flag
= 0;
2284 wanted_type_ptr
->writing_in_flag
= 1;
2287 if (strchr (fci
->flags2
, 'W') != 0)
2288 wanted_type_ptr
->writing_in_flag
= 1;
2289 if (strchr (fci
->flags2
, 'R') != 0)
2290 wanted_type_ptr
->reading_from_flag
= 1;
2292 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2293 wanted_type_ptr
->param
= cur_param
;
2294 wanted_type_ptr
->arg_num
= arg_num
;
2295 wanted_type_ptr
->format_start
= format_start
;
2296 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2297 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2298 wanted_type_ptr
->next
= NULL
;
2299 if (last_wanted_type
!= 0)
2300 last_wanted_type
->next
= wanted_type_ptr
;
2301 if (first_wanted_type
== 0)
2302 first_wanted_type
= wanted_type_ptr
;
2303 last_wanted_type
= wanted_type_ptr
;
2308 wanted_type_ptr
= fwt_pool
.allocate ();
2310 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2311 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2316 if (first_wanted_type
!= 0)
2318 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2319 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2320 /* By default, use the end of the range for the caret location. */
2321 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2322 offset_to_format_end
,
2323 offset_to_format_start
, offset_to_format_end
);
2324 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2325 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2326 offset_to_type_start
,
2327 conversion_char
, arglocs
);
2329 /* note printf extension type checks are *additional* - %p must always
2330 * be pointer compatible, %d always int compatible.
2332 if (first_wanted_type
->kind
!= CF_KIND_FORMAT
|| !kef
)
2335 const struct kernel_ext_fmt
*kef_now
;
2338 for (kef_now
= kef
; kef_now
->suffix
&& !strcmp (kef
->suffix
, kef_now
->suffix
); kef_now
++)
2340 success
= check_kef_type (fmt_loc
, kef_now
,
2341 first_wanted_type
->arg_num
,
2342 first_wanted_type
->param
,
2343 kef_now
->type
, fki
, offset_to_type_start
, conversion_char
, arglocs
);
2349 location_t param_loc
;
2351 if (EXPR_HAS_LOCATION (first_wanted_type
->param
))
2352 param_loc
= EXPR_LOCATION (first_wanted_type
->param
);
2355 /* arg_num is 1-based. */
2356 gcc_assert (first_wanted_type
->arg_num
> 0);
2357 param_loc
= (*arglocs
)[first_wanted_type
->arg_num
- 1];
2360 format_type_warning (fmt_loc
, param_loc
, first_wanted_type
,
2361 kef
->type
, TREE_TYPE (first_wanted_type
->param
),
2362 fki
, offset_to_type_start
, conversion_char
);
2368 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2369 is the NUL-terminated format string (which at this point may contain
2370 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2371 terminating NUL character). ARG_NUM is one less than the number of
2372 the first format argument to check; PARAMS points to that format
2373 argument in the list of arguments. */
2376 check_format_info_main (format_check_results
*res
,
2377 function_format_info
*info
, const char *format_chars
,
2378 location_t fmt_param_loc
, tree format_string_cst
,
2379 int format_length
, tree params
,
2380 unsigned HOST_WIDE_INT arg_num
,
2381 object_allocator
<format_wanted_type
> &fwt_pool
,
2382 vec
<location_t
> *arglocs
)
2384 const char * const orig_format_chars
= format_chars
;
2385 const tree first_fillin_param
= params
;
2387 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
2388 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
2389 const location_t format_string_loc
= res
->format_string_loc
;
2391 /* -1 if no conversions taking an operand have been found; 0 if one has
2392 and it didn't use $; 1 if $ formats are in use. */
2393 int has_operand_number
= -1;
2395 /* Vector of pointers to opening quoting directives (like GCC "%<"). */
2396 auto_vec
<const char*> quotdirs
;
2398 /* Pointers to the most recent color directives (like GCC's "%r or %R").
2399 A starting color directive much be terminated before the end of
2400 the format string. A terminating directive makes no sense without
2401 a prior starting directive. */
2402 const char *color_begin
= NULL
;
2403 const char *color_end
= NULL
;
2405 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
2407 while (*format_chars
!= 0)
2409 if (*format_chars
++ != '%')
2411 if (*format_chars
== 0)
2413 format_warning_at_char (format_string_loc
, format_string_cst
,
2414 format_chars
- orig_format_chars
,
2416 "spurious trailing %<%%%> in format");
2419 if (*format_chars
== '%')
2425 flag_chars_t flag_chars
;
2426 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
2427 orig_format_chars
, format_string_loc
,
2428 flag_chars
, has_operand_number
,
2429 first_fillin_param
, fwt_pool
, arglocs
);
2431 if (!arg_parser
.read_any_dollar ())
2434 if (!arg_parser
.read_format_flags ())
2437 /* Read any format width, possibly * or *m$. */
2438 if (!arg_parser
.read_any_format_width (params
, arg_num
))
2441 /* Read any format left precision (must be a number, not *). */
2442 arg_parser
.read_any_format_left_precision ();
2444 /* Read any format precision, possibly * or *m$. */
2445 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
2448 const char *format_start
= format_chars
;
2450 arg_parser
.handle_alloc_chars ();
2452 /* The rest of the conversion specification is the length modifier
2453 (if any), and the conversion specifier, so this is where the
2454 type information starts. If we need to issue a suggestion
2455 about a type mismatch, then we should preserve everything up
2457 const char *type_start
= format_chars
;
2459 /* Read any length modifier, if this kind of format has them. */
2460 const length_modifier len_modifier
2461 = arg_parser
.read_any_length_modifier ();
2463 /* Read any modifier (strftime E/O). */
2464 arg_parser
.read_any_other_modifier ();
2466 char format_char
= *format_chars
;
2467 if (format_char
== 0
2468 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
2469 && format_char
== '%'))
2471 format_warning_at_char (format_string_loc
, format_string_cst
,
2472 format_chars
- orig_format_chars
,
2474 "conversion lacks type at end of format");
2479 const format_char_info
* const fci
2480 = arg_parser
.find_format_char_info (format_char
);
2484 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
2486 if (etab
&& format_chars
[0] >= 'A' && format_chars
[0] <= 'Z')
2488 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
2490 for (; etab
< etab_end
&& etab
->suffix
; etab
++)
2492 if (!strncmp (etab
->suffix
, format_chars
, strlen (etab
->suffix
)))
2496 if (!etab
->suffix
|| etab
== etab_end
)
2498 format_warning_at_char (format_string_loc
, format_string_cst
,
2499 format_chars
- orig_format_chars
+ 1,
2501 "unrecognized printf extension suffix");
2506 format_chars
+= strlen (etab
->suffix
);
2512 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
2514 format_string_loc
, orig_format_chars
, format_char
,
2515 quotdirs
.length () > 0);
2517 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
2518 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
2520 /* Diagnose nested or unmatched quoting directives such as GCC's
2521 "%<...%<" and "%>...%>". */
2522 bool quot_begin_p
= strchr (fci
->flags2
, '<');
2523 bool quot_end_p
= strchr (fci
->flags2
, '>');
2525 if (quot_begin_p
&& !quot_end_p
)
2527 if (quotdirs
.length ())
2528 format_warning_at_char (format_string_loc
, format_string_cst
,
2529 format_chars
- orig_format_chars
,
2531 "nested quoting directive");
2532 quotdirs
.safe_push (format_chars
);
2534 else if (!quot_begin_p
&& quot_end_p
)
2536 if (quotdirs
.length ())
2539 format_warning_at_char (format_string_loc
, format_string_cst
,
2540 format_chars
- orig_format_chars
,
2542 "unmatched quoting directive");
2545 bool color_begin_p
= strchr (fci
->flags2
, '/');
2548 color_begin
= format_chars
;
2551 else if (strchr (fci
->flags2
, '\\'))
2554 format_warning_at_char (format_string_loc
, format_string_cst
,
2555 format_chars
- orig_format_chars
,
2557 "%qc directive redundant after prior occurence of the same", format_char
);
2558 else if (!color_begin
)
2559 format_warning_at_char (format_string_loc
, format_string_cst
,
2560 format_chars
- orig_format_chars
,
2562 "unmatched color reset directive");
2563 color_end
= format_chars
;
2566 /* Diagnose directives that shouldn't appear in a quoted sequence.
2567 (They are denoted by a double quote in FLAGS2.) */
2568 if (quotdirs
.length ())
2570 if (strchr (fci
->flags2
, '"'))
2571 format_warning_at_char (format_string_loc
, format_string_cst
,
2572 format_chars
- orig_format_chars
,
2574 "%qc conversion used within a quoted sequence",
2578 /* Validate the pairs of flags used. */
2579 arg_parser
.validate_flag_pairs (fci
, format_char
);
2581 arg_parser
.give_y2k_warnings (fci
, format_char
);
2583 arg_parser
.parse_any_scan_set (fci
);
2585 tree wanted_type
= NULL
;
2586 const char *wanted_type_name
= NULL
;
2588 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
2589 wanted_type
, wanted_type_name
,
2595 arg_parser
.main_wanted_type
.next
= NULL
;
2597 /* Finally. . .check type of argument against desired type! */
2598 if (!arg_parser
.check_argument_type (fci
, etab
, len_modifier
,
2599 wanted_type
, wanted_type_name
,
2603 format_start
, type_start
,
2609 if (format_chars
- orig_format_chars
!= format_length
)
2610 format_warning_at_char (format_string_loc
, format_string_cst
,
2611 format_chars
+ 1 - orig_format_chars
,
2612 OPT_Wformat_contains_nul
,
2613 "embedded %<\\0%> in format");
2614 if (info
->first_arg_num
!= 0 && params
!= 0
2615 && has_operand_number
<= 0)
2617 res
->number_other
--;
2618 res
->number_extra_args
++;
2620 if (has_operand_number
> 0)
2621 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
2623 if (quotdirs
.length ())
2624 format_warning_at_char (format_string_loc
, format_string_cst
,
2625 quotdirs
.pop () - orig_format_chars
,
2626 OPT_Wformat_
, "unterminated quoting directive");
2627 if (color_begin
&& !color_end
)
2628 format_warning_at_char (format_string_loc
, format_string_cst
,
2629 color_begin
- orig_format_chars
,
2630 OPT_Wformat_
, "unterminated color directive");
2633 /* Check the argument types from a single format conversion (possibly
2634 including width and precision arguments).
2636 FMT_LOC is the location of the format conversion.
2638 TYPES is a singly-linked list expressing the parts of the format
2639 conversion that expect argument types, and the arguments they
2642 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
2643 format string to where type information begins for the conversion
2644 (the length modifier and conversion specifier).
2646 CONVERSION_CHAR is the user-provided conversion specifier.
2650 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2652 then FMT_LOC covers this range:
2654 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2657 and TYPES in this case is a three-entry singly-linked list consisting of:
2658 (1) the check for the field width here:
2659 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2662 (2) the check for the field precision here:
2663 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2666 (3) the check for the length modifier and conversion char here:
2667 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2671 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
2674 0000000000111111111122
2675 0123456789012345678901
2676 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2678 | ` CONVERSION_CHAR: 'd'
2679 type starts here. */
2680 tree
type_normalize (tree type
, tree
*cousin
, tree target
= NULL
)
2684 if (TREE_CODE (type
) == FUNCTION_TYPE
|| TREE_CODE (type
) == POINTER_TYPE
)
2687 /* Strip off any "const" etc. */
2688 type
= build_qualified_type (type
, 0);
2689 if (TREE_CODE (TYPE_NAME (type
)) != TYPE_DECL
)
2692 if (target
&& (type
== target
|| TYPE_NAME (type
) == target
))
2695 struct type_special
*t
;
2696 for (t
= special_types
; t
->match
; t
++)
2700 if (TYPE_NAME (type
) != *t
->match
)
2702 if (t
->cousin
&& *t
->cousin
)
2703 *cousin
= *t
->cousin
;
2705 return *t
->replace
? *t
->replace
: type
;
2709 tree orig
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
2718 /* gcc-10 asserts when you give a TYPE_DECL instead of the actual TYPE */
2720 decl_deref(tree typ
)
2722 while (TREE_CODE (typ
) == TYPE_DECL
)
2723 typ
= DECL_ORIGINAL_TYPE (typ
);
2729 check_format_types (const substring_loc
&fmt_loc
,
2730 format_wanted_type
*types
, const format_kind_info
*fki
,
2731 int offset_to_type_start
,
2732 char conversion_char
,
2733 vec
<location_t
> *arglocs
)
2735 for (; types
!= 0; types
= types
->next
)
2739 tree cur_type_cousin
= NULL
;
2746 wanted_type
= types
->wanted_type
;
2747 arg_num
= types
->arg_num
;
2749 wanted_type
= decl_deref(wanted_type
);
2751 /* The following should not occur here. */
2752 gcc_assert (wanted_type
);
2753 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
2755 if (types
->pointer_count
== 0)
2756 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
2758 switch (TREE_CODE (wanted_type
))
2760 case IDENTIFIER_NODE
:
2763 wanted_type
= TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (wanted_type
));
2766 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
2770 cur_param
= types
->param
;
2773 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
2774 NULL
, fki
, offset_to_type_start
,
2779 cur_type
= TREE_TYPE (cur_param
);
2780 if (cur_type
== error_mark_node
)
2782 orig_cur_type
= cur_type
;
2785 location_t param_loc
= UNKNOWN_LOCATION
;
2786 if (EXPR_HAS_LOCATION (cur_param
))
2787 param_loc
= EXPR_LOCATION (cur_param
);
2790 /* arg_num is 1-based. */
2791 gcc_assert (types
->arg_num
> 0);
2792 param_loc
= (*arglocs
)[types
->arg_num
- 1];
2795 STRIP_NOPS (cur_param
);
2797 /* Check the types of any additional pointer arguments
2798 that precede the "real" argument. */
2799 for (i
= 0; i
< types
->pointer_count
; ++i
)
2801 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2803 cur_type
= TREE_TYPE (cur_type
);
2804 if (cur_type
== error_mark_node
)
2807 /* Check for writing through a NULL pointer. */
2808 if (types
->writing_in_flag
2811 && integer_zerop (cur_param
))
2812 warning (OPT_Wformat_
, "writing through null pointer (argument %d)", arg_num
);
2814 /* Check for reading through a NULL pointer. */
2815 if (types
->reading_from_flag
2818 && integer_zerop (cur_param
))
2819 warning (OPT_Wformat_
, "reading through null pointer (argument %d)", arg_num
);
2821 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2822 cur_param
= TREE_OPERAND (cur_param
, 0);
2826 /* See if this is an attempt to write into a const type with
2827 scanf or with printf "%n". Note: the writing in happens
2828 at the first indirection only, if for example
2829 void * const * is passed to scanf %p; passing
2830 const void ** is simply passing an incompatible type. */
2831 if (types
->writing_in_flag
2833 && (TYPE_READONLY (cur_type
)
2835 && (CONSTANT_CLASS_P (cur_param
)
2836 || (DECL_P (cur_param
)
2837 && TREE_READONLY (cur_param
))))))
2838 warning (OPT_Wformat_
, "writing into constant object (argument %d)", arg_num
);
2840 /* If there are extra type qualifiers beyond the first
2841 indirection, then this makes the types technically
2845 && (TYPE_READONLY (cur_type
)
2846 || TYPE_VOLATILE (cur_type
)
2847 || TYPE_ATOMIC (cur_type
)
2848 || TYPE_RESTRICT (cur_type
)))
2849 warning (OPT_Wformat_
, "extra type qualifiers in format argument (argument %d)",
2855 format_type_warning (fmt_loc
, param_loc
,
2856 types
, wanted_type
, orig_cur_type
, fki
,
2857 offset_to_type_start
, conversion_char
);
2862 if (i
< types
->pointer_count
)
2865 cur_type
= type_normalize (cur_type
, &cur_type_cousin
);
2867 /* Check whether the argument type is a character type. This leniency
2868 only applies to certain formats, flagged with 'c'. */
2869 if (types
->char_lenient_flag
)
2870 char_type_flag
= (cur_type
== char_type_node
2871 || cur_type
== signed_char_type_node
2872 || cur_type
== unsigned_char_type_node
);
2874 int compat
= lang_hooks
.types_compatible_p (decl_deref (wanted_type
), decl_deref (cur_type
));
2875 /* Check the type of the "real" argument, if there's a type we want. */
2876 if ((TREE_CODE (wanted_type
) != INTEGER_TYPE
|| types
->pointer_count
)
2879 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
&& !types
->pointer_count
2883 if (TREE_CODE (cur_param
) == INTEGER_CST
)
2886 if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
2887 && TREE_CODE (cur_type
) == TYPE_DECL
)
2889 if (types
->wanted_type
== cur_type
)
2891 format_type_warning (fmt_loc
, param_loc
, types
,
2892 wanted_type
, orig_cur_type
, fki
,
2893 offset_to_type_start
, conversion_char
,
2894 " (strict match required [A])");
2897 else if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
)
2899 if (types
->wanted_type
== TYPE_NAME(cur_type
))
2901 format_type_warning (fmt_loc
, param_loc
, types
,
2902 wanted_type
, orig_cur_type
, fki
,
2903 offset_to_type_start
, conversion_char
,
2904 " (strict match required [B])");
2907 else if (wanted_type
== cur_type
)
2909 else if (cur_type_cousin
)
2911 format_type_warning (fmt_loc
, param_loc
, types
,
2912 wanted_type
, orig_cur_type
, fki
,
2913 offset_to_type_start
, conversion_char
,
2914 " (strict match required [C])");
2918 format_type_warning (fmt_loc, param_loc, types,
2919 wanted_type, orig_cur_type, fki,
2920 offset_to_type_start, conversion_char,
2921 " (ultra-pedantic mode)");
2926 /* If we want 'void *', allow any pointer type.
2927 (Anything else would already have got a warning.)
2928 With -Wpedantic, only allow pointers to void and to character
2930 if (wanted_type
== void_type_node
2931 && (!pedantic
|| (i
== 1 && char_type_flag
)))
2933 /* Don't warn about differences merely in signedness, unless
2934 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2935 target and not a character type, and for character types at
2936 a second level of indirection. */
2937 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2938 && TREE_CODE (cur_type
) == INTEGER_TYPE
2939 && ((!pedantic
&& !warn_format_signedness
)
2940 || (i
== 0 && !warn_format_signedness
)
2941 || (i
== 1 && char_type_flag
))
2942 && (TYPE_UNSIGNED (wanted_type
)
2943 ? wanted_type
== c_common_unsigned_type (cur_type
)
2944 : wanted_type
== c_common_signed_type (cur_type
)))
2946 if (cur_type_cousin
)
2948 if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
2949 && TREE_CODE (cur_type_cousin
) == TYPE_DECL
)
2951 if (types
->wanted_type
== cur_type_cousin
)
2953 format_type_warning (fmt_loc
, param_loc
, types
,
2954 wanted_type
, orig_cur_type
, fki
,
2955 offset_to_type_start
, conversion_char
,
2956 " (strict match required [X])");
2959 else if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
)
2961 if (types
->wanted_type
== TYPE_NAME(cur_type_cousin
))
2963 format_type_warning (fmt_loc
, param_loc
, types
,
2964 wanted_type
, orig_cur_type
, fki
,
2965 offset_to_type_start
, conversion_char
,
2966 " (strict match required [Y])");
2969 else if (wanted_type
== cur_type_cousin
)
2973 format_type_warning (fmt_loc
, param_loc
, types
,
2974 wanted_type
, orig_cur_type
, fki
,
2975 offset_to_type_start
, conversion_char
,
2976 " (strict match required [Z])");
2982 /* Don't warn about differences merely in signedness if we know
2983 that the current type is integer-promoted and its original type
2984 was unsigned such as that it is in the range of WANTED_TYPE. */
2985 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2986 && TREE_CODE (cur_type
) == INTEGER_TYPE
2987 && warn_format_signedness
2988 && TYPE_UNSIGNED (wanted_type
)
2989 && cur_param
!= NULL_TREE
2990 && TREE_CODE (cur_param
) == NOP_EXPR
)
2992 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
2993 if (TYPE_UNSIGNED (t
)
2994 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
2997 /* Likewise, "signed char", "unsigned char" and "char" are
2998 equivalent but the above test won't consider them equivalent. */
2999 if (wanted_type
== char_type_node
3000 && (!pedantic
|| i
< 2)
3003 if (types
->scalar_identity_flag
3004 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
3005 || (INTEGRAL_TYPE_P (cur_type
)
3006 && INTEGRAL_TYPE_P (wanted_type
)))
3007 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
3009 /* Now we have a type mismatch. */
3010 format_type_warning (fmt_loc
, param_loc
, types
,
3011 wanted_type
, orig_cur_type
, fki
,
3012 offset_to_type_start
, conversion_char
);
3017 check_kef_type (const substring_loc
&fmt_loc
,
3018 const struct kernel_ext_fmt
*kef
,
3022 const format_kind_info
*fki
,
3023 int offset_to_type_start
,
3024 char conversion_char
,
3025 vec
<location_t
> *arglocs
)
3031 /* The following should not occur here. */
3032 gcc_assert (wanted_type
);
3033 gcc_assert (wanted_type
!= void_type_node
|| kef
->ptrlevel
);
3035 if (TREE_CODE (wanted_type
) == TYPE_DECL
)
3036 wanted_type
= DECL_ORIGINAL_TYPE (wanted_type
);
3041 cur_type
= TREE_TYPE (cur_param
);
3042 if (cur_type
== error_mark_node
)
3045 location_t param_loc
= UNKNOWN_LOCATION
;
3046 if (EXPR_HAS_LOCATION (cur_param
))
3047 param_loc
= EXPR_LOCATION (cur_param
);
3050 /* arg_num is 1-based. */
3051 gcc_assert (arg_num
> 0);
3052 param_loc
= (*arglocs
)[arg_num
- 1];
3056 STRIP_NOPS (cur_param
);
3058 /* Check the types of any additional pointer arguments
3059 that precede the "real" argument. */
3060 for (i
= 0; i
< kef
->ptrlevel
; ++i
)
3062 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
3064 cur_type
= TREE_TYPE (cur_type
);
3065 if (cur_type
== error_mark_node
)
3068 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
3069 cur_param
= TREE_OPERAND (cur_param
, 0);
3073 /* If there are extra type qualifiers beyond the first
3074 indirection, then this makes the types technically
3078 && (TYPE_READONLY (cur_type
)
3079 || TYPE_VOLATILE (cur_type
)
3080 || TYPE_ATOMIC (cur_type
)
3081 || TYPE_RESTRICT (cur_type
)))
3082 warning (OPT_Wformat_
, "extra type qualifiers in format argument (argument %d)",
3093 if (i
< kef
->ptrlevel
)
3096 int compat
= lang_hooks
.types_compatible_p (wanted_type
, cur_type
);
3104 normal_type
= type_normalize (cur_type
, &cousin
, wanted_type
);
3106 return normal_type
== wanted_type
;
3110 /* Given type TYPE, attempt to dereference the type N times
3111 (e.g. from ("int ***", 2) to "int *")
3113 Return the derefenced type, with any qualifiers
3114 such as "const" stripped from the result, or
3115 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3118 deref_n_times (tree type
, int n
)
3122 for (int i
= n
; i
> 0; i
--)
3124 if (TREE_CODE (type
) != POINTER_TYPE
)
3126 type
= TREE_TYPE (type
);
3128 /* Strip off any "const" etc. */
3129 return build_qualified_type (type
, 0);
3132 /* Lookup the format code for FORMAT_LEN within FLI,
3133 returning the string code for expressing it, or NULL
3134 if it is not found. */
3137 get_modifier_for_format_len (const format_length_info
*fli
,
3138 enum format_lengths format_len
)
3140 for (; fli
->name
; fli
++)
3142 if (fli
->index
== format_len
)
3144 if (fli
->double_index
== format_len
)
3145 return fli
->double_name
;
3152 namespace selftest
{
3155 test_get_modifier_for_format_len ()
3158 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
3160 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
3162 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
3164 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
3167 } // namespace selftest
3169 #endif /* CHECKING_P */
3171 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3172 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3173 Wformat type errors where the argument has type ARG_TYPE. */
3176 matching_type_p (tree spec_type
, tree arg_type
)
3178 gcc_assert (spec_type
);
3179 gcc_assert (arg_type
);
3181 spec_type
= decl_deref (spec_type
);
3182 arg_type
= decl_deref (arg_type
);
3184 /* If any of the types requires structural equality, we can't compare
3185 their canonical types. */
3186 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
3187 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
3190 spec_type
= TYPE_CANONICAL (spec_type
);
3191 arg_type
= TYPE_CANONICAL (arg_type
);
3193 if (TREE_CODE (spec_type
) == INTEGER_TYPE
3194 && TREE_CODE (arg_type
) == INTEGER_TYPE
3195 && (TYPE_UNSIGNED (spec_type
)
3196 ? spec_type
== c_common_unsigned_type (arg_type
)
3197 : spec_type
== c_common_signed_type (arg_type
)))
3200 return spec_type
== arg_type
;
3203 /* Subroutine of get_format_for_type.
3205 Generate a string containing the length modifier and conversion specifier
3206 that should be used to format arguments of type ARG_TYPE within FKI
3207 (effectively the inverse of the checking code).
3209 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3210 is required to use it, for correcting bogus length modifiers.
3211 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3212 that matches ARG_TYPE.
3214 If successful, returns a non-NULL string which should be freed
3216 Otherwise, returns NULL. */
3219 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
3220 char conversion_char
)
3222 gcc_assert (arg_type
);
3224 const format_char_info
*spec
;
3225 for (spec
= &fki
->conversion_specs
[0];
3229 if (conversion_char
)
3230 if (!strchr (spec
->format_chars
, conversion_char
))
3233 tree effective_arg_type
= deref_n_times (arg_type
,
3234 spec
->pointer_count
);
3235 if (!effective_arg_type
)
3237 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
3239 const format_type_detail
*ftd
= &spec
->types
[i
];
3242 if (matching_type_p (*ftd
->type
, effective_arg_type
))
3244 const char *len_modifier
3245 = get_modifier_for_format_len (fki
->length_char_specs
,
3246 (enum format_lengths
)i
);
3250 if (conversion_char
)
3251 /* We found a match, using the given conversion char - the
3252 length modifier was incorrect (or absent).
3253 Provide a suggestion using the conversion char with the
3254 correct length modifier for the type. */
3255 return xasprintf ("%s%c", len_modifier
, conversion_char
);
3257 /* 2nd pass: no match was possible using the user-provided
3258 conversion char, but we do have a match without using it.
3259 Provide a suggestion using the first conversion char
3260 listed for the given type. */
3261 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
3269 /* Generate a string containing the length modifier and conversion specifier
3270 that should be used to format arguments of type ARG_TYPE within FKI
3271 (effectively the inverse of the checking code).
3273 If successful, returns a non-NULL string which should be freed
3275 Otherwise, returns NULL. */
3278 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
3279 char conversion_char
)
3281 gcc_assert (arg_type
);
3282 gcc_assert (conversion_char
);
3284 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3285 If we find one, then presumably the length modifier was incorrect
3287 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
3291 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3292 matching just on the type. */
3293 return get_format_for_type_1 (fki
, arg_type
, '\0');
3296 /* Attempt to get a string for use as a replacement fix-it hint for the
3297 source range in FMT_LOC.
3299 Preserve all of the text within the range of FMT_LOC up to
3300 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3301 length modifier and conversion specifier for ARG_TYPE, attempting
3302 to keep the user-provided CONVERSION_CHAR if possible.
3304 For example, given a long vs long long mismatch for arg5 here:
3306 000000000111111111122222222223333333333|
3307 123456789012345678901234567890123456789` column numbers
3308 0000000000111111111122|
3309 0123456789012345678901` string offsets
3310 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3311 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3313 | ` CONVERSION_CHAR: 'd'
3316 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3317 STRING_CST), where the user provided:
3319 the result (assuming "long" argument 5) should be:
3322 If successful, returns a non-NULL string which should be freed
3324 Otherwise, returns NULL. */
3327 get_corrected_substring (const substring_loc
&fmt_loc
,
3328 format_wanted_type
*type
, tree arg_type
,
3329 const format_kind_info
*fki
,
3330 int offset_to_type_start
, char conversion_char
)
3332 /* Attempt to provide hints for argument types, but not for field widths
3336 if (type
->kind
!= CF_KIND_FORMAT
)
3339 /* Locate the current code within the source range, rejecting
3340 any awkward cases where the format string occupies more than
3342 Lookup the place where the type starts (including any length
3343 modifiers), getting it as the caret location. */
3344 substring_loc
type_loc (fmt_loc
);
3345 type_loc
.set_caret_index (offset_to_type_start
);
3347 location_t fmt_substring_loc
;
3348 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
3352 source_range fmt_substring_range
3353 = get_range_from_loc (line_table
, fmt_substring_loc
);
3355 expanded_location caret
3356 = expand_location_to_spelling_point (fmt_substring_loc
);
3357 expanded_location start
3358 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
3359 expanded_location finish
3360 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
3361 if (caret
.file
!= start
.file
)
3363 if (start
.file
!= finish
.file
)
3365 if (caret
.line
!= start
.line
)
3367 if (start
.line
!= finish
.line
)
3369 if (start
.column
> caret
.column
)
3371 if (start
.column
> finish
.column
)
3373 if (caret
.column
> finish
.column
)
3376 #if BUILDING_GCC_VERSION >= 9000
3377 char_span line
= location_get_source_line (start
.file
, start
.line
);
3381 /* If we got this far, then we have the line containing the
3382 existing conversion specification.
3384 Generate a trimmed copy, containing the prefix part of the conversion
3385 specification, up to the (but not including) the length modifier.
3386 In the above example, this would be "%-+*.*". */
3387 int length_up_to_type
= caret
.column
- start
.column
;
3388 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
3389 char *prefix
= prefix_span
.xstrdup ();
3391 char *prefix
= NULL
;
3394 /* Now attempt to generate a suggestion for the rest of the specification
3395 (length modifier and conversion char), based on ARG_TYPE and
3397 In the above example, this would be "ld". */
3398 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
3399 if (!format_for_type
)
3405 /* Success. Generate the resulting suggestion for the whole range of
3406 FMT_LOC by concatenating the two strings.
3407 In the above example, this would be "%-+*.*ld". */
3408 char *result
= concat (prefix
, format_for_type
, NULL
);
3409 free (format_for_type
);
3414 /* Helper class for adding zero or more trailing '*' to types.
3416 The format type and name exclude any '*' for pointers, so those
3417 must be formatted manually. For all the types we currently have,
3418 this is adequate, but formats taking pointers to functions or
3419 arrays would require the full type to be built up in order to
3420 print it with %T. */
3422 class indirection_suffix
3425 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
3427 /* Determine the size of the buffer (including NUL-terminator). */
3429 size_t get_buffer_size () const
3431 return m_pointer_count
+ 2;
3434 /* Write the '*' to DST and add a NUL-terminator. */
3436 void fill_buffer (char *dst
) const
3438 if (m_pointer_count
== 0)
3440 else if (c_dialect_cxx ())
3442 memset (dst
, '*', m_pointer_count
);
3443 dst
[m_pointer_count
] = 0;
3448 memset (dst
+ 1, '*', m_pointer_count
);
3449 dst
[m_pointer_count
+ 1] = 0;
3454 int m_pointer_count
;
3457 #if BUILDING_GCC_VERSION >= 9000
3458 /* not exported by GCC... need a local copy :( */
3459 class frr_range_label_for_type_mismatch
: public range_label
3462 frr_range_label_for_type_mismatch (tree labelled_type
, tree other_type
)
3463 : m_labelled_type (labelled_type
), m_other_type (other_type
)
3467 label_text
get_text (unsigned range_idx
) const OVERRIDE
;
3470 tree m_labelled_type
;
3474 /* Print T to CPP. */
3477 print_type (c_pretty_printer
*cpp
, tree t
, bool *quoted
)
3479 gcc_assert (TYPE_P (t
));
3480 struct obstack
*ob
= pp_buffer (cpp
)->obstack
;
3481 char *p
= (char *) obstack_base (ob
);
3482 /* Remember the end of the initial dump. */
3483 int len
= obstack_object_size (ob
);
3485 tree name
= TYPE_NAME (t
);
3486 if (name
&& TREE_CODE (name
) == TYPE_DECL
&& DECL_NAME (name
))
3487 pp_identifier (cpp
, lang_hooks
.decl_printable_name (name
, 2));
3491 /* If we're printing a type that involves typedefs, also print the
3492 stripped version. But sometimes the stripped version looks
3493 exactly the same, so we don't want it after all. To avoid
3494 printing it in that case, we play ugly obstack games. */
3495 if (TYPE_CANONICAL (t
) && t
!= TYPE_CANONICAL (t
))
3497 c_pretty_printer cpp2
;
3498 /* Print the stripped version into a temporary printer. */
3499 cpp2
.type_id (TYPE_CANONICAL (t
));
3500 struct obstack
*ob2
= cpp2
.buffer
->obstack
;
3501 /* Get the stripped version from the temporary printer. */
3502 const char *aka
= (char *) obstack_base (ob2
);
3503 int aka_len
= obstack_object_size (ob2
);
3504 int type1_len
= obstack_object_size (ob
) - len
;
3506 /* If they are identical, bail out. */
3507 if (aka_len
== type1_len
&& memcmp (p
+ len
, aka
, aka_len
) == 0)
3510 /* They're not, print the stripped version now. */
3512 pp_end_quote (cpp
, pp_show_color (cpp
));
3513 pp_c_whitespace (cpp
);
3514 pp_left_brace (cpp
);
3515 pp_c_ws_string (cpp
, _("aka"));
3516 pp_c_whitespace (cpp
);
3518 pp_begin_quote (cpp
, pp_show_color (cpp
));
3519 cpp
->type_id (TYPE_CANONICAL (t
));
3521 pp_end_quote (cpp
, pp_show_color (cpp
));
3522 pp_right_brace (cpp
);
3523 /* No further closing quotes are needed. */
3528 /* C-specific implementation of range_label::get_text () vfunc for
3529 range_label_for_type_mismatch. */
3530 #if BUILDING_GCC_VERSION >= 10000
3531 #define label_borrow(text) label_text::borrow(text)
3532 #define label_take(text) label_text::take(text)
3534 #define label_borrow(text) label_text((char *)text, false)
3535 #define label_take(text) label_text(text, true)
3539 frr_range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
3541 if (m_labelled_type
== NULL_TREE
)
3542 return label_borrow("(null tree)");
3544 c_pretty_printer cpp
;
3545 bool quoted
= false;
3546 print_type (&cpp
, m_labelled_type
, "ed
);
3547 return label_take(xstrdup (pp_formatted_text (&cpp
)));
3550 #define range_label_for_type_mismatch frr_range_label_for_type_mismatch
3553 /* Subclass of range_label for labelling the range in the format string
3554 with the type in question, adding trailing '*' for pointer_count. */
3556 class range_label_for_format_type_mismatch
3557 : public range_label_for_type_mismatch
3560 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
3562 : range_label_for_type_mismatch (labelled_type
, other_type
),
3563 m_pointer_count (pointer_count
)
3567 label_text
get_text (unsigned range_idx
) const FINAL OVERRIDE
3569 label_text text
= range_label_for_type_mismatch::get_text (range_idx
);
3570 if (text
.m_buffer
== NULL
)
3573 indirection_suffix
suffix (m_pointer_count
);
3574 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3575 suffix
.fill_buffer (p
);
3577 char *result
= concat (text
.m_buffer
, p
, NULL
);
3579 return label_take(result
);
3583 int m_pointer_count
;
3586 /* Give a warning about a format argument of different type from that expected.
3587 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3588 is based on the location of the char at TYPE->offset_loc.
3589 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3590 if this is unavailable.
3591 WANTED_TYPE is the type the argument should have,
3592 possibly stripped of pointer dereferences. The description (such as "field
3593 precision"), the placement in the format string, a possibly more
3594 friendly name of WANTED_TYPE, and the number of pointer dereferences
3595 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3596 or NULL if it is missing.
3598 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3599 format string to where type information begins for the conversion
3600 (the length modifier and conversion specifier).
3601 CONVERSION_CHAR is the user-provided conversion specifier.
3603 For example, given a type mismatch for argument 5 here:
3605 00000000011111111112222222222333333333344444444445555555555|
3606 12345678901234567890123456789012345678901234567890123456789` column numbers
3607 0000000000111111111122|
3608 0123456789012345678901` offsets within STRING_CST
3609 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3610 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3612 | ` CONVERSION_CHAR: 'd' PARAM_LOC
3615 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3619 format_type_warning (const substring_loc
&whole_fmt_loc
,
3620 location_t param_loc
,
3621 format_wanted_type
*type
,
3622 tree wanted_type
, tree arg_type
,
3623 const format_kind_info
*fki
,
3624 int offset_to_type_start
,
3625 char conversion_char
,
3628 enum format_specifier_kind kind
= type
->kind
;
3629 const char *wanted_type_name
= type
->wanted_type_name
;
3630 const char *format_start
= type
->format_start
;
3631 int format_length
= type
->format_length
;
3632 int pointer_count
= type
->pointer_count
;
3633 int arg_num
= type
->arg_num
;
3638 /* If ARG_TYPE is a typedef with a misleading name (for example,
3639 size_t but not the standard size_t expected by printf %zu), avoid
3640 printing the typedef name. */
3641 if (wanted_type_name
3643 && TYPE_NAME (arg_type
)
3644 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
3645 && DECL_NAME (TYPE_NAME (arg_type
))
3646 && !strcmp (wanted_type_name
,
3647 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
3648 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
3650 indirection_suffix
suffix (pointer_count
);
3651 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3652 suffix
.fill_buffer (p
);
3654 /* WHOLE_FMT_LOC has the caret at the end of the range.
3655 Set the caret to be at the offset from TYPE. Subtract one
3656 from the offset for the same reason as in format_warning_at_char. */
3657 substring_loc
fmt_loc (whole_fmt_loc
);
3658 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
3660 #if BUILDING_GCC_VERSION >= 9000
3661 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
3663 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
3665 /* Get a string for use as a replacement fix-it hint for the range in
3666 fmt_loc, or NULL. */
3667 char *corrected_substring
3668 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3669 offset_to_type_start
, conversion_char
);
3670 format_string_diagnostic_t
diag (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3671 corrected_substring
);
3672 # define format_warning_at_substring(a,b,c,d,e,...) \
3673 diag.emit_warning(__VA_ARGS__)
3675 # define format_warning_at_substring(a,b,c,d,...) \
3676 format_warning_at_substring(a,c,__VA_ARGS__)
3677 /* Get a string for use as a replacement fix-it hint for the range in
3678 fmt_loc, or NULL. */
3679 char *corrected_substring
3680 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3681 offset_to_type_start
, conversion_char
);
3685 if (wanted_type_name
)
3688 format_warning_at_substring
3689 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3690 corrected_substring
, OPT_Wformat_
,
3691 "%s %<%s%.*s%> expects argument of type %<%s%s%>, but argument %d has type %qT%s",
3692 gettext (kind_descriptions
[kind
]),
3693 (kind
== CF_KIND_FORMAT
? "%" : ""),
3694 format_length
, format_start
,
3695 wanted_type_name
, p
, arg_num
, arg_type
, extra
);
3697 format_warning_at_substring
3698 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3699 corrected_substring
, OPT_Wformat_
,
3700 "%s %<%s%.*s%> expects a matching %<%s%s%> argument%s",
3701 gettext (kind_descriptions
[kind
]),
3702 (kind
== CF_KIND_FORMAT
? "%" : ""),
3703 format_length
, format_start
, wanted_type_name
, p
, extra
);
3708 format_warning_at_substring
3709 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3710 corrected_substring
, OPT_Wformat_
,
3711 "%s %<%s%.*s%> expects argument of type %<%T%s%>, but argument %d has type %qT%s",
3712 gettext (kind_descriptions
[kind
]),
3713 (kind
== CF_KIND_FORMAT
? "%" : ""),
3714 format_length
, format_start
,
3715 wanted_type
, p
, arg_num
, arg_type
, extra
);
3717 format_warning_at_substring
3718 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3719 corrected_substring
, OPT_Wformat_
,
3720 "%s %<%s%.*s%> expects a matching %<%T%s%> argument%s",
3721 gettext (kind_descriptions
[kind
]),
3722 (kind
== CF_KIND_FORMAT
? "%" : ""),
3723 format_length
, format_start
, wanted_type
, p
, extra
);
3726 free (corrected_substring
);
3731 /* Given a format_char_info array FCI, and a character C, this function
3732 returns the index into the conversion_specs where that specifier's
3733 data is located. The character must exist. */
3735 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
3739 for (i
= 0; fci
->format_chars
; i
++, fci
++)
3740 if (strchr (fci
->format_chars
, c
))
3743 /* We shouldn't be looking for a non-existent specifier. */
3747 /* Given a format_length_info array FLI, and a character C, this
3748 function returns the index into the conversion_specs where that
3749 modifier's data is located. The character must exist. */
3751 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
3755 for (i
= 0; fli
->name
; i
++, fli
++)
3756 if (strchr (fli
->name
, c
))
3759 /* We shouldn't be looking for a non-existent modifier. */
3764 #ifdef TARGET_FORMAT_TYPES
3765 extern const format_kind_info TARGET_FORMAT_TYPES
[];
3768 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3769 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
3771 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3772 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3775 /* Attributes such as "printf" are equivalent to those such as
3776 "gnu_printf" unless this is overridden by a target. */
3777 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
3782 /* Translate to unified attribute name. This is used in decode_format_type and
3783 decode_format_attr. In attr_name the user specified argument is passed. It
3784 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3785 or the attr_name passed to this function, if there is no matching entry. */
3787 convert_format_name_to_system_name (const char *attr_name
)
3791 if (attr_name
== NULL
|| *attr_name
== 0
3792 || strncmp (attr_name
, "gcc_", 4) == 0)
3794 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3795 TARGET_OVERRIDES_FORMAT_INIT ();
3798 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3799 /* Check if format attribute is overridden by target. */
3800 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
3801 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
3803 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
3805 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
3808 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
3810 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
3814 /* Otherwise default to gnu format. */
3816 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
3819 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
3822 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
3824 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
3830 /* Handle a "format" attribute; arguments as in
3831 struct attribute_spec.handler. */
3833 handle_frr_format_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
3834 int flags
, bool *no_add_attrs
)
3837 function_format_info info
;
3839 /* Canonicalize name of format function. */
3840 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
3841 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
3843 if (!decode_format_attr (args
, &info
, 0))
3845 *no_add_attrs
= true;
3849 if (prototype_p (type
))
3851 if (!check_format_string (type
, info
.format_num
, flags
,
3852 no_add_attrs
, info
.format_type
))
3855 if (info
.first_arg_num
!= 0)
3857 unsigned HOST_WIDE_INT arg_num
= 1;
3858 function_args_iterator iter
;
3861 /* Verify that first_arg_num points to the last arg,
3863 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
3866 if (arg_num
!= info
.first_arg_num
)
3868 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
3869 error ("arguments to be formatted is not %<...%>");
3870 *no_add_attrs
= true;
3876 /* Check if this is a strftime variant. Just for this variant
3877 FMT_FLAG_ARG_CONVERT is not set. */
3878 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
3879 && info
.first_arg_num
!= 0)
3881 error ("strftime formats cannot format arguments");
3882 *no_add_attrs
= true;
3891 namespace selftest
{
3893 /* Selftests of location handling. */
3895 /* Get the format_kind_info with the given name. */
3897 static const format_kind_info
*
3898 get_info (const char *name
)
3900 int idx
= decode_format_type (name
);
3901 const format_kind_info
*fki
= &format_types
[idx
];
3902 ASSERT_STREQ (fki
->name
, name
);
3906 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
3907 is EXPECTED_FORMAT. */
3910 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
3911 const char *expected_format
, tree type
,
3912 char conversion_char
)
3915 gcc_assert (expected_format
);
3918 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
3919 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
3920 free (actual_format
);
3923 /* Selftests for get_format_for_type. */
3925 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
3926 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
3927 (TYPE), (CONVERSION_CHAR))
3929 /* Selftest for get_format_for_type for "printf"-style functions. */
3932 test_get_format_for_type_printf ()
3934 const format_kind_info
*fki
= get_info ("gnu_printf");
3935 ASSERT_NE (fki
, NULL
);
3937 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
3938 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
3939 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
3940 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
3941 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
3942 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
3943 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
3944 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
3945 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
3946 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
3947 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
3948 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
3949 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
3950 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
3951 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
3952 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
3953 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
3954 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
3955 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
3956 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
3957 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
3958 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
3959 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
3960 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
3961 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
3962 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
3963 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
3964 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
3967 /* Selftest for get_format_for_type for "scanf"-style functions. */
3970 test_get_format_for_type_scanf ()
3972 const format_kind_info
*fki
= get_info ("gnu_scanf");
3973 ASSERT_NE (fki
, NULL
);
3974 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
3975 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
3976 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
3977 build_pointer_type (long_integer_type_node
), 'd');
3978 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
3979 build_pointer_type (long_unsigned_type_node
), 'u');
3980 ASSERT_FORMAT_FOR_TYPE_STREQ
3981 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
3982 ASSERT_FORMAT_FOR_TYPE_STREQ
3983 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
3984 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
3985 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
3988 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
3990 /* Exercise the type-printing label code, to give some coverage
3991 under "make selftest-valgrind" (in particular, to ensure that
3992 the label-printing machinery doesn't leak). */
3995 test_type_mismatch_range_labels ()
3997 /* Create a tempfile and write some text to it.
3998 ....................0000000001 11111111 12 22222222
3999 ....................1234567890 12345678 90 12345678. */
4000 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
4001 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
4002 line_table_test ltt
;
4004 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
4006 location_t c17
= linemap_position_for_column (line_table
, 17);
4007 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
4008 location_t c18
= linemap_position_for_column (line_table
, 18);
4009 location_t c24
= linemap_position_for_column (line_table
, 24);
4010 location_t c26
= linemap_position_for_column (line_table
, 26);
4012 /* Don't attempt to run the tests if column data might be unavailable. */
4013 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
4016 location_t fmt
= make_location (c18
, c17
, c18
);
4017 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
4019 location_t param
= make_location (c24
, c24
, c26
);
4020 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
4022 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
4023 integer_type_node
, 1);
4024 range_label_for_type_mismatch
param_label (integer_type_node
,
4026 gcc_rich_location
richloc (fmt
, &fmt_label
);
4027 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
4029 test_diagnostic_context dc
;
4030 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
4031 if (c_dialect_cxx ())
4032 /* "char*", without a space. */
4034 " printf (\"msg: %i\\n\", msg);\n"
4038 pp_formatted_text (dc
.printer
));
4040 /* "char *", with a space. */
4042 " printf (\"msg: %i\\n\", msg);\n"
4047 pp_formatted_text (dc
.printer
));
4050 /* Run all of the selftests within this file. */
4055 test_get_modifier_for_format_len ();
4056 test_get_format_for_type_printf ();
4057 test_get_format_for_type_scanf ();
4058 test_type_mismatch_range_labels ();
4061 } // namespace selftest
4063 #endif /* CHECKING_P */
4065 // include "gt-c-family-c-format.h"
4067 static const struct attribute_spec frr_format_attribute_table
[] =
4069 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4070 affects_type_identity, handler, exclude } */
4071 { "frr_format", 3, 3, false, true, true, false,
4072 handle_frr_format_attribute
, NULL
},
4073 { "frr_format_arg", 1, 1, false, true, true, false,
4074 handle_frr_format_arg_attribute
, NULL
},
4075 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
4079 register_attributes (void *event_data
, void *data
)
4081 // warning (0, G_("Callback to register attributes"));
4082 register_attribute (frr_format_attribute_table
);
4086 cb_walk_tree_fn (tree
* tp
, int * walk_subtrees
, void * data ATTRIBUTE_UNUSED
)
4088 if (TREE_CODE (*tp
) != CALL_EXPR
)
4091 tree call_expr
= *tp
;
4093 int nargs
= call_expr_nargs(call_expr
);
4094 tree fn
= CALL_EXPR_FN(call_expr
);
4096 if (!fn
|| TREE_CODE (fn
) != ADDR_EXPR
)
4099 tree fndecl
= TREE_OPERAND (fn
, 0);
4100 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
4104 warning (0, G_("function call to %s, %d args"),
4105 IDENTIFIER_POINTER (DECL_NAME (fndecl
)),
4109 tree
*fargs
= (tree
*) alloca (nargs
* sizeof (tree
));
4111 for (int j
= 0; j
< nargs
; j
++)
4113 tree arg
= CALL_EXPR_ARG(call_expr
, j
);
4115 /* For -Wformat undo the implicit passing by hidden reference
4116 done by convert_arg_to_ellipsis. */
4117 if (TREE_CODE (arg
) == ADDR_EXPR
4118 && TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4119 fargs
[j
] = TREE_OPERAND (arg
, 0);
4124 check_function_format (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)), nargs
, fargs
, NULL
);
4129 setup_type (const char *name
, tree
*dst
)
4133 if (*dst
&& *dst
!= void_type_node
)
4136 *dst
= maybe_get_identifier (name
);
4140 tmp
= identifier_global_value (*dst
);
4141 if (tmp
&& TREE_CODE (tmp
) != TYPE_DECL
)
4143 warning (0, "%qs is not defined as a type", name
);
4147 if (tmp
&& TREE_CODE (tmp
) == TYPE_DECL
)
4154 handle_finish_parse (void *event_data
, void *data
)
4156 tree fndecl
= (tree
) event_data
;
4157 gcc_assert (TREE_CODE (fndecl
) == FUNCTION_DECL
);
4159 setup_type ("uint64_t", &local_uint64_t_node
);
4160 setup_type ("int64_t", &local_int64_t_node
);
4162 setup_type ("size_t", &local_size_t_node
);
4163 setup_type ("ssize_t", &local_ssize_t_node
);
4164 setup_type ("atomic_size_t", &local_atomic_size_t_node
);
4165 setup_type ("atomic_ssize_t", &local_atomic_ssize_t_node
);
4166 setup_type ("ptrdiff_t", &local_ptrdiff_t_node
);
4168 setup_type ("pid_t", &local_pid_t_node
);
4169 setup_type ("uid_t", &local_uid_t_node
);
4170 setup_type ("gid_t", &local_gid_t_node
);
4171 setup_type ("time_t", &local_time_t_node
);
4173 setup_type ("socklen_t", &local_socklen_t_node
);
4174 setup_type ("in_addr_t", &local_in_addr_t_node
);
4176 const format_char_info
*fci
;
4178 for (fci
= print_char_table
; fci
->format_chars
; fci
++)
4180 if (!fci
->kernel_ext
)
4183 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
4184 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
4186 for (; etab
->suffix
&& etab
< etab_end
; etab
++)
4188 tree identifier
, node
;
4190 if (etab
->type
&& etab
->type
!= void_type_node
)
4193 identifier
= maybe_get_identifier (etab
->type_str
);
4195 if (!identifier
|| identifier
== error_mark_node
)
4198 if (etab
->type_code
)
4200 node
= identifier_global_tag (identifier
);
4204 if (node
->base
.code
!= etab
->type_code
)
4208 warning (0, "%qs tag category (struct/union/enum) mismatch", etab
->type_str
);
4209 etab
->warned
= true;
4216 node
= identifier_global_value (identifier
);
4220 if (TREE_CODE (node
) != TYPE_DECL
)
4224 warning (0, "%qs is defined as a non-type", etab
->type_str
);
4225 etab
->warned
= true;
4229 node
= TREE_TYPE (node
);
4231 if (etab
->t_unsigned
)
4232 node
= c_common_unsigned_type (node
);
4233 else if (etab
->t_signed
)
4234 node
= c_common_signed_type (node
);
4241 walk_tree (&DECL_SAVED_TREE (fndecl
), cb_walk_tree_fn
, NULL
, NULL
);
4245 handle_pragma_printfrr_ext (cpp_reader
*dummy
)
4249 enum cpp_ttype ttype
;
4251 ttype
= pragma_lex (&token
, &loc
);
4252 if (ttype
!= CPP_STRING
)
4254 error_at (loc
, "%<#pragma FRR printfrr_ext%> requires string argument");
4258 const char *s
= TREE_STRING_POINTER (token
);
4262 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, needs to start with '%%'");
4273 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, needs to be %%p, %%d or %%i");
4277 const format_char_info
*fci
;
4279 for (fci
= print_char_table
; fci
->format_chars
; fci
++)
4280 if (strchr (fci
->format_chars
, s
[1]))
4283 gcc_assert (fci
->format_chars
);
4284 gcc_assert (fci
->kernel_ext
);
4286 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
4287 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
4295 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, suffix must start with an uppercase letter");
4299 /* -2 -- need to keep the sentinel at the end */
4300 if (etab
[ETAB_SZ
- 2].suffix
)
4302 error_at (loc
, "%<#pragma FRR printfrr_ext%>: out of space for format suffixes");
4306 for (; etab
->suffix
&& etab
< etab_end
; etab
++)
4308 if (!strcmp(s
+ 2, etab
->suffix
))
4310 memmove (etab
+ 1, etab
, (etab_end
- etab
- 1) * sizeof (*etab
));
4314 warning_at (loc
, OPT_Wformat_
,
4315 "%<#pragma FRR printfrr_ext%>: duplicate printf format suffix %qs", s
);
4316 warning_at (etab
->origin_loc
, OPT_Wformat_
,
4317 "%<#pragma FRR printfrr_ext%>: previous definition was here");
4324 if (!strncmp(s
+ 2, etab
->suffix
, MIN(strlen(s
+ 2), strlen(etab
->suffix
))))
4326 warning_at (loc
, OPT_Wformat_
,
4327 "%<#pragma FRR printfrr_ext%>: overlapping printf format suffix %qs", s
);
4328 warning_at (etab
->origin_loc
, OPT_Wformat_
,
4329 "%<#pragma FRR printfrr_ext%>: previous definition for %<%%%c%s%> was here", s
[1], etab
->suffix
);
4334 gcc_assert (etab
< etab_end
);
4336 memset (etab
, 0, sizeof (*etab
));
4337 etab
->suffix
= xstrdup(s
+ 2);
4338 etab
->origin_loc
= loc
;
4339 etab
->type
= void_type_node
;
4341 ttype
= pragma_lex (&token
, &loc
);
4342 if (ttype
!= CPP_OPEN_PAREN
)
4344 error_at (loc
, "%<#pragma FRR printfrr_ext%> expected %<(%>");
4348 ttype
= pragma_lex (&token
, &loc
);
4351 while (ttype
== CPP_NAME
)
4353 if (!strcmp (IDENTIFIER_POINTER (token
), "const"))
4354 etab
->t_const
= true;
4355 else if (!strcmp (IDENTIFIER_POINTER (token
), "signed"))
4356 etab
->t_signed
= true;
4357 else if (!strcmp (IDENTIFIER_POINTER (token
), "unsigned"))
4358 etab
->t_unsigned
= true;
4362 ttype
= pragma_lex (&token
, &loc
);
4366 if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "struct"))
4368 etab
->type_code
= RECORD_TYPE
;
4369 ttype
= pragma_lex (&token
, &loc
);
4371 else if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "union"))
4373 etab
->type_code
= UNION_TYPE
;
4374 ttype
= pragma_lex (&token
, &loc
);
4376 else if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "enum"))
4378 etab
->type_code
= ENUMERAL_TYPE
;
4379 ttype
= pragma_lex (&token
, &loc
);
4383 if (ttype
!= CPP_NAME
)
4385 error_at (loc
, "%<#pragma FRR printfrr_ext%>: expected typename identifier");
4389 etab
->type_str
= xstrdup (IDENTIFIER_POINTER (token
));
4391 while ((ttype
= pragma_lex (&token
, &loc
)) != CPP_CLOSE_PAREN
)
4395 error_at (loc
, "%<#pragma FRR printfrr_ext%>: unexpected identifier. Note the only supported qualifier is %<const%>");
4403 error_at (loc
, "%<#pragma FRR printfrr_ext%>: premature end of line, missing %<)%>");
4407 error_at (loc
, "%<#pragma FRR printfrr_ext%>: unsupported token");
4412 ttype
= pragma_lex (&token
, &loc
);
4413 if (ttype
!= CPP_EOF
)
4414 warning_at (loc
, OPT_Wformat_
,
4415 "%<#pragma FRR printfrr_ext%>: garbage at end of line");
4420 memset (etab
, 0, sizeof (*etab
));
4424 register_pragma_printfrr_ext (void *event_data
, void *data
)
4426 c_register_pragma_with_expansion ("FRR", "printfrr_ext", handle_pragma_printfrr_ext
);
4430 define_vars (void *gcc_data
, void *user_data
)
4432 cpp_define (parse_in
, "_FRR_ATTRIBUTE_PRINTFRR=0x10000");
4436 #define __visible __attribute__((visibility("default")))
4439 __visible
int plugin_is_GPL_compatible
;
4442 plugin_init (struct plugin_name_args
*plugin_info
,
4443 struct plugin_gcc_version
*version
)
4445 const char *plugin_name
= plugin_info
->base_name
;
4447 if (!plugin_default_version_check(version
, &gcc_version
))
4449 error(G_("incompatible gcc/plugin versions"));
4453 memset (ext_p
, 0, sizeof (ext_p
));
4454 memset (ext_d
, 0, sizeof (ext_d
));
4456 register_callback (plugin_name
, PLUGIN_FINISH_PARSE_FUNCTION
, handle_finish_parse
, NULL
);
4457 register_callback (plugin_name
, PLUGIN_ATTRIBUTES
, register_attributes
, NULL
);
4458 register_callback (plugin_name
, PLUGIN_START_UNIT
, define_vars
, NULL
);
4459 register_callback (plugin_name
, PLUGIN_PRAGMAS
, register_pragma_printfrr_ext
, NULL
);