1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
4 Extended for FRR's printfrr() with Linux kernel style extensions
5 Copyright (C) 2019-2020 David Lamparter, for NetDEF, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.GPLv3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "gcc-common.h"
27 #include "coretypes.h"
29 //include "c-target.h"
31 #include "alloc-pool.h"
32 #include "stringpool.h"
36 #include "langhooks.h"
37 #include "frr-format.h"
38 #include "diagnostic.h"
39 #include "substring-locations.h"
41 #include "selftest-diagnostic.h"
42 #ifndef FIRST_PSEUDO_REGISTER
43 #define FIRST_PSEUDO_REGISTER 0
47 #include "gcc-rich-location.h"
48 #include "c-pretty-print.h"
51 extern struct cpp_reader
*parse_in
;
53 #pragma GCC visibility push(hidden)
55 /* Handle attributes associated with format checking. */
57 /* This must be in the same order as format_types, except for
58 format_type_error. Target-specific format types do not have
59 matching enum values. */
60 enum format_type
{ frr_printf_format_type
,
61 format_type_error
= -1};
63 struct function_format_info
65 int format_type
; /* type of format (printf, scanf, etc.) */
66 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
67 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
70 static GTY(()) tree local_uint64_t_node
;
71 static GTY(()) tree local_int64_t_node
;
73 static GTY(()) tree local_size_t_node
;
74 static GTY(()) tree local_ssize_t_node
;
75 static GTY(()) tree local_atomic_size_t_node
;
76 static GTY(()) tree local_atomic_ssize_t_node
;
77 static GTY(()) tree local_ptrdiff_t_node
;
79 static GTY(()) tree local_pid_t_node
;
80 static GTY(()) tree local_uid_t_node
;
81 static GTY(()) tree local_gid_t_node
;
82 static GTY(()) tree local_time_t_node
;
84 static GTY(()) tree local_socklen_t_node
;
85 static GTY(()) tree local_in_addr_t_node
;
87 static struct type_special
{
92 { &local_atomic_size_t_node
, &local_size_t_node
, &local_ssize_t_node
, },
93 { &local_atomic_ssize_t_node
, &local_ssize_t_node
, &local_size_t_node
, },
94 { &local_size_t_node
, NULL
, &local_ssize_t_node
, },
95 { &local_ssize_t_node
, NULL
, &local_size_t_node
, },
96 { &local_uint64_t_node
, NULL
, &local_int64_t_node
, },
97 { &local_int64_t_node
, NULL
, &local_uint64_t_node
, },
98 { &local_pid_t_node
, NULL
, &local_pid_t_node
, },
99 { &local_uid_t_node
, NULL
, &local_uid_t_node
, },
100 { &local_gid_t_node
, NULL
, &local_gid_t_node
, },
101 { &local_time_t_node
, NULL
, &local_time_t_node
, },
102 { NULL
, NULL
, NULL
, }
105 static bool decode_format_attr (tree
, function_format_info
*, int);
106 static int decode_format_type (const char *);
108 static bool check_format_string (tree argument
,
109 unsigned HOST_WIDE_INT format_num
,
110 int flags
, bool *no_add_attrs
,
111 int expected_format_type
);
112 static bool get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
,
114 static const char *convert_format_name_to_system_name (const char *attr_name
);
116 static int first_target_format_type
;
117 static const char *format_name (int format_num
);
118 static int format_flags (int format_num
);
120 /* Emit a warning as per format_warning_va, but construct the substring_loc
121 for the character at offset (CHAR_IDX - 1) within a string constant
122 FORMAT_STRING_CST at FMT_STRING_LOC. */
124 ATTRIBUTE_GCC_DIAG (5,6)
126 format_warning_at_char (location_t fmt_string_loc
, tree format_string_cst
,
127 int char_idx
, int opt
, const char *gmsgid
, ...)
130 va_start (ap
, gmsgid
);
131 tree string_type
= TREE_TYPE (format_string_cst
);
133 /* The callers are of the form:
134 format_warning (format_string_loc, format_string_cst,
135 format_chars - orig_format_chars,
136 where format_chars has already been incremented, so that
137 CHAR_IDX is one character beyond where the warning should
138 be emitted. Fix it. */
141 substring_loc
fmt_loc (fmt_string_loc
, string_type
, char_idx
, char_idx
,
143 #if BUILDING_GCC_VERSION >= 9000
144 format_string_diagnostic_t
diag (fmt_loc
, NULL
, UNKNOWN_LOCATION
, NULL
,
146 bool warned
= diag
.emit_warning_va (opt
, gmsgid
, &ap
);
148 bool warned
= format_warning_va (fmt_loc
, UNKNOWN_LOCATION
, NULL
,
156 /* Check that we have a pointer to a string suitable for use as a format.
157 The default is to check for a char type.
158 For objective-c dialects, this is extended to include references to string
159 objects validated by objc_string_ref_type_p ().
160 Targets may also provide a string object type that can be used within c and
161 c++ and shared with their respective objective-c dialects. In this case the
162 reference to a format string is checked for validity via a hook.
164 The function returns true if strref points to any string type valid for the
165 language dialect and target. */
168 valid_stringptr_type_p (tree strref
)
170 return (strref
!= NULL
171 && TREE_CODE (strref
) == POINTER_TYPE
172 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref
)) == char_type_node
173 || objc_string_ref_type_p (strref
)));
174 // || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
177 /* Handle a "format_arg" attribute; arguments as in
178 struct attribute_spec.handler. */
180 handle_frr_format_arg_attribute (tree
*node
, tree
ARG_UNUSED (name
),
181 tree args
, int flags
, bool *no_add_attrs
)
184 tree format_num_expr
= TREE_VALUE (args
);
185 unsigned HOST_WIDE_INT format_num
= 0;
187 if (!get_constant (format_num_expr
, &format_num
, 0))
189 error ("format string has invalid operand number");
190 *no_add_attrs
= true;
194 if (prototype_p (type
))
196 /* The format arg can be any string reference valid for the language and
197 target. We cannot be more specific in this case. */
198 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
202 if (!valid_stringptr_type_p (TREE_TYPE (type
)))
204 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
205 error ("function does not return string type");
206 *no_add_attrs
= true;
213 /* Verify that the format_num argument is actually a string reference suitable,
214 for the language dialect and target (in case the format attribute is in
215 error). When we know the specific reference type expected, this is also
218 check_format_string (tree fntype
, unsigned HOST_WIDE_INT format_num
,
219 int flags
, bool *no_add_attrs
, int expected_format_type
)
221 unsigned HOST_WIDE_INT i
;
222 bool is_target_sref
, is_char_ref
;
225 function_args_iterator iter
;
228 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
236 || !valid_stringptr_type_p (ref
))
238 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
239 error ("format string argument is not a string type");
240 *no_add_attrs
= true;
244 /* We only know that we want a suitable string reference. */
245 if (expected_format_type
< 0)
248 /* Now check that the arg matches the expected type. */
250 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
252 fmt_flags
= format_flags (expected_format_type
);
253 is_target_sref
= false;
255 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
258 return true; /* OK, we expected a char and found one. */
261 error ("found a %qT but the format argument should be a string",
263 *no_add_attrs
= true;
268 /* We expect a string object type as the format arg. */
271 error ("format argument should be a %qs reference but a string was found", format_name (expected_format_type
));
272 *no_add_attrs
= true;
276 /* We will allow a target string ref to match only itself. */
277 if (first_target_format_type
278 && expected_format_type
>= first_target_format_type
283 error ("format argument should be a %qs reference",
284 format_name (expected_format_type
));
285 *no_add_attrs
= true;
292 /* Verify EXPR is a constant, and store its value.
293 If validated_p is true there should be no errors.
294 Returns true on success, false otherwise. */
296 get_constant (tree expr
, unsigned HOST_WIDE_INT
*value
, int validated_p
)
298 if (!tree_fits_uhwi_p (expr
))
300 gcc_assert (!validated_p
);
304 *value
= TREE_INT_CST_LOW (expr
);
309 /* Decode the arguments to a "format" attribute into a
310 function_format_info structure. It is already known that the list
311 is of the right length. If VALIDATED_P is true, then these
312 attributes have already been validated and must not be erroneous;
313 if false, it will give an error message. Returns true if the
314 attributes are successfully decoded, false otherwise. */
317 decode_format_attr (tree args
, function_format_info
*info
, int validated_p
)
319 tree format_type_id
= TREE_VALUE (args
);
320 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
321 tree first_arg_num_expr
322 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
324 if (TREE_CODE (format_type_id
) != STRING_CST
)
326 gcc_assert (!validated_p
);
327 error ("unrecognized format specifier");
332 const char *p
= TREE_STRING_POINTER (format_type_id
);
334 p
= convert_format_name_to_system_name (p
);
336 info
->format_type
= decode_format_type (p
);
338 if (info
->format_type
== format_type_error
)
340 gcc_assert (!validated_p
);
341 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
347 if (!get_constant (format_num_expr
, &info
->format_num
, validated_p
))
349 error ("format string has invalid operand number");
353 if (!get_constant (first_arg_num_expr
, &info
->first_arg_num
, validated_p
))
355 error ("%<...%> has invalid operand number");
359 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
361 gcc_assert (!validated_p
);
362 error ("format string argument follows the arguments to be formatted");
369 /* Check a call to a format function against a parameter list. */
371 /* The C standard version C++ is treated as equivalent to
372 or inheriting from, for the purpose of format features supported. */
373 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
374 /* The C standard version we are checking formats against when pedantic. */
375 #define C_STD_VER ((int) (c_dialect_cxx () \
376 ? CPLUSPLUS_STD_VER \
379 : (flag_isoc94 ? STD_C94 : STD_C89))))
380 /* The name to give to the standard version we are warning about when
381 pedantic. FEATURE_VER is the version in which the feature warned out
382 appeared, which is higher than C_STD_VER. */
383 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
384 ? (cxx_dialect < cxx11 ? "ISO C++98" \
386 : ((FEATURE_VER) == STD_EXT \
389 /* Adjust a C standard version, which may be STD_C9L, to account for
390 -Wno-long-long. Returns other standard versions unchanged. */
391 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
392 ? (warn_long_long ? STD_C99 : STD_C89) \
395 /* Enum describing the kind of specifiers present in the format and
396 requiring an argument. */
397 enum format_specifier_kind
{
400 CF_KIND_FIELD_PRECISION
403 static const char *kind_descriptions
[] = {
405 N_("field width specifier"),
406 N_("field precision specifier")
409 /* Structure describing details of a type expected in format checking,
410 and the type to check against it. */
411 struct format_wanted_type
413 /* The type wanted. */
415 /* The name of this type to use in diagnostics. */
416 const char *wanted_type_name
;
417 /* Should be type checked just for scalar width identity. */
418 int scalar_identity_flag
;
419 /* The level of indirection through pointers at which this type occurs. */
421 /* Whether, when pointer_count is 1, to allow any character type when
422 pedantic, rather than just the character or void type specified. */
423 int char_lenient_flag
;
424 /* Whether the argument, dereferenced once, is written into and so the
425 argument must not be a pointer to a const-qualified type. */
427 /* Whether the argument, dereferenced once, is read from and so
428 must not be a NULL pointer. */
429 int reading_from_flag
;
430 /* The kind of specifier that this type is used for. */
431 enum format_specifier_kind kind
;
432 /* The starting character of the specifier. This never includes the
433 initial percent sign. */
434 const char *format_start
;
435 /* The length of the specifier. */
437 /* The actual parameter to check against the wanted type. */
439 /* The argument number of that parameter. */
441 /* The offset location of this argument with respect to the format
443 unsigned int offset_loc
;
444 /* The next type to check for this format conversion, or NULL if none. */
445 struct format_wanted_type
*next
;
448 /* Convenience macro for format_length_info meaning unused. */
449 #define NO_FMT NULL, FMT_LEN_none, STD_C89
451 static const format_length_info printf_length_specs
[] =
453 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
454 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
455 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
456 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
457 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
458 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
459 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
460 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
461 { "H", FMT_LEN_H
, STD_EXT
, NO_FMT
, 0 },
462 { "D", FMT_LEN_D
, STD_EXT
, "DD", FMT_LEN_DD
, STD_EXT
, 0 },
463 { NO_FMT
, NO_FMT
, 0 }
466 static const format_flag_spec printf_flag_specs
[] =
468 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
469 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
470 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
471 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
472 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
473 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
474 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
475 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
476 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
477 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
478 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
482 static const format_flag_pair printf_flag_pairs
[] =
486 { '0', 'p', 1, 'i' },
491 static kernel_ext_fmt ext_p
[ETAB_SZ
] = {
494 static kernel_ext_fmt ext_d
[ETAB_SZ
] = {
498 static const format_char_info print_char_table
[] =
500 /* C89 conversion specifiers. */
501 /* none, hh, h, l, ll, L, z, t, j, H, D, DD */
502 { "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
},
503 { "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
},
504 { "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
},
505 { "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
},
506 { "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
},
507 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
, NULL
},
508 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
, NULL
},
509 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "c", NULL
, ext_p
},
510 { "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
},
511 /* C99 conversion specifiers. */
512 { "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
},
513 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "", NULL
, NULL
},
514 /* X/Open conversion specifiers. */
515 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
, NULL
},
516 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
, NULL
},
517 /* GNU conversion specifiers. */
518 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
, NULL
},
519 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
, NULL
}
522 /* This must be in the same order as enum format_type. */
523 static const format_kind_info format_types_orig
[] =
525 { "frr_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
526 printf_flag_specs
, printf_flag_pairs
,
527 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
528 'w', 0, 'p', 0, 'L', 0,
529 &integer_type_node
, &integer_type_node
533 /* This layer of indirection allows GCC to reassign format_types with
534 new data if necessary, while still allowing the original data to be
536 static const format_kind_info
*format_types
= format_types_orig
;
538 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
540 /* Structure detailing the results of checking a format function call
541 where the format expression may be a conditional expression with
542 many leaves resulting from nested conditional expressions. */
543 struct format_check_results
545 /* Number of leaves of the format argument that could not be checked
546 as they were not string literals. */
547 int number_non_literal
;
548 /* Number of leaves of the format argument that were null pointers or
549 string literals, but had extra format arguments. */
550 int number_extra_args
;
551 location_t extra_arg_loc
;
552 /* Number of leaves of the format argument that were null pointers or
553 string literals, but had extra format arguments and used $ operand
555 int number_dollar_extra_args
;
556 /* Number of leaves of the format argument that were wide string
559 /* Number of leaves of the format argument that are not array of "char". */
561 /* Number of leaves of the format argument that were empty strings. */
563 /* Number of leaves of the format argument that were unterminated
565 int number_unterminated
;
566 /* Number of leaves of the format argument that were not counted above. */
568 /* Location of the format string. */
569 location_t format_string_loc
;
572 struct format_check_context
574 format_check_results
*res
;
575 function_format_info
*info
;
577 vec
<location_t
> *arglocs
;
580 /* Return the format name (as specified in the original table) for the format
581 type indicated by format_num. */
583 format_name (int format_num
)
585 if (format_num
>= 0 && format_num
< n_format_types
)
586 return format_types
[format_num
].name
;
590 /* Return the format flags (as specified in the original table) for the format
591 type indicated by format_num. */
593 format_flags (int format_num
)
595 if (format_num
>= 0 && format_num
< n_format_types
)
596 return format_types
[format_num
].flags
;
600 static void check_format_info (function_format_info
*, tree
,
602 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
603 static void check_format_info_main (format_check_results
*,
604 function_format_info
*, const char *,
607 unsigned HOST_WIDE_INT
,
608 object_allocator
<format_wanted_type
> &,
611 static void init_dollar_format_checking (int, tree
);
612 static int maybe_read_dollar_number (const char **, int,
613 tree
, tree
*, const format_kind_info
*);
614 static bool avoid_dollar_number (const char *);
615 static void finish_dollar_format_checking (format_check_results
*, int);
617 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
620 static void check_format_types (const substring_loc
&fmt_loc
,
621 format_wanted_type
*,
622 const format_kind_info
*fki
,
623 int offset_to_type_start
,
624 char conversion_char
,
625 vec
<location_t
> *arglocs
);
626 static void format_type_warning (const substring_loc
&fmt_loc
,
627 location_t param_loc
,
628 format_wanted_type
*, tree
,
630 const format_kind_info
*fki
,
631 int offset_to_type_start
,
632 char conversion_char
,
633 const char *extra
= NULL
);
635 static bool check_kef_type (const substring_loc
&fmt_loc
,
636 const struct kernel_ext_fmt
*kef
,
640 const format_kind_info
*fki
,
641 int offset_to_type_start
,
642 char conversion_char
,
643 vec
<location_t
> *arglocs
);
645 /* Decode a format type from a string, returning the type, or
646 format_type_error if not valid, in which case the caller should print an
649 decode_format_type (const char *s
)
654 s
= convert_format_name_to_system_name (s
);
656 for (i
= 0; i
< n_format_types
; i
++)
659 if (!strcmp (s
, format_types
[i
].name
))
661 alen
= strlen (format_types
[i
].name
);
662 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
663 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
664 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
667 return format_type_error
;
671 /* Check the argument list of a call to printf, scanf, etc.
672 ATTRS are the attributes on the function type. There are NARGS argument
673 values in the array ARGARRAY.
674 Also, if -Wsuggest-attribute=format,
675 warn for calls to vprintf or vscanf in functions with no such format
676 attribute themselves. */
679 check_function_format (tree attrs
, int nargs
, tree
*argarray
,
680 vec
<location_t
> *arglocs
)
684 /* See if this function has any format attributes. */
685 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
687 if (is_attribute_p ("frr_format", TREE_PURPOSE (a
)))
690 function_format_info info
;
691 decode_format_attr (TREE_VALUE (a
), &info
, /*validated=*/true);
694 /* FIXME: Rewrite all the internal functions in this file
695 to use the ARGARRAY directly instead of constructing this
697 tree params
= NULL_TREE
;
699 for (i
= nargs
- 1; i
>= 0; i
--)
700 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
701 check_format_info (&info
, params
, arglocs
);
704 /* Attempt to detect whether the current function might benefit
705 from the format attribute if the called function is decorated
706 with it. Avoid using calls with string literal formats for
707 guidance since those are unlikely to be viable candidates. */
708 if (warn_suggest_attribute_format
709 && current_function_decl
!= NULL_TREE
710 && info
.first_arg_num
== 0
711 && (format_types
[info
.format_type
].flags
712 & (int) FMT_FLAG_ARG_CONVERT
)
713 /* c_strlen will fail for a function parameter but succeed
714 for a literal or constant array. */
715 && !c_strlen (argarray
[info
.format_num
- 1], 1))
718 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
721 if (is_attribute_p ("frr_format", TREE_PURPOSE (c
))
722 && (decode_format_type (IDENTIFIER_POINTER
723 (TREE_VALUE (TREE_VALUE (c
))))
724 == info
.format_type
))
728 /* Check if the current function has a parameter to which
729 the format attribute could be attached; if not, it
730 can't be a candidate for a format attribute, despite
731 the vprintf-like or vscanf-like call. */
733 for (args
= DECL_ARGUMENTS (current_function_decl
);
735 args
= DECL_CHAIN (args
))
737 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
738 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
743 warning (OPT_Wsuggest_attribute_format
,
744 "function %qD might be a candidate for %qs %<frr_format%> attribute",
745 current_function_decl
,
746 format_types
[info
.format_type
].name
);
754 /* Variables used by the checking of $ operand number formats. */
755 static char *dollar_arguments_used
= NULL
;
756 static char *dollar_arguments_pointer_p
= NULL
;
757 static int dollar_arguments_alloc
= 0;
758 static int dollar_arguments_count
;
759 static int dollar_first_arg_num
;
760 static int dollar_max_arg_used
;
761 static int dollar_format_warned
;
763 /* Initialize the checking for a format string that may contain $
764 parameter number specifications; we will need to keep track of whether
765 each parameter has been used. FIRST_ARG_NUM is the number of the first
766 argument that is a parameter to the format, or 0 for a vprintf-style
767 function; PARAMS is the list of arguments starting at this argument. */
770 init_dollar_format_checking (int first_arg_num
, tree params
)
772 tree oparams
= params
;
774 dollar_first_arg_num
= first_arg_num
;
775 dollar_arguments_count
= 0;
776 dollar_max_arg_used
= 0;
777 dollar_format_warned
= 0;
778 if (first_arg_num
> 0)
782 dollar_arguments_count
++;
783 params
= TREE_CHAIN (params
);
786 if (dollar_arguments_alloc
< dollar_arguments_count
)
788 free (dollar_arguments_used
);
789 free (dollar_arguments_pointer_p
);
790 dollar_arguments_alloc
= dollar_arguments_count
;
791 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
792 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
794 if (dollar_arguments_alloc
)
796 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
797 if (first_arg_num
> 0)
803 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
805 params
= TREE_CHAIN (params
);
813 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
814 is set, it is an error if one is not found; otherwise, it is OK. If
815 such a number is found, check whether it is within range and mark that
816 numbered operand as being used for later checking. Returns the operand
817 number if found and within range, zero if no such number was found and
818 this is OK, or -1 on error. PARAMS points to the first operand of the
819 format; PARAM_PTR is made to point to the parameter referred to. If
820 a $ format is found, *FORMAT is updated to point just after it. */
823 maybe_read_dollar_number (const char **format
,
824 int dollar_needed
, tree params
, tree
*param_ptr
,
825 const format_kind_info
*fki
)
829 const char *fcp
= *format
;
834 warning (OPT_Wformat_
, "missing $ operand number in format");
842 while (ISDIGIT (*fcp
))
845 nargnum
= 10 * argnum
+ (*fcp
- '0');
846 if (nargnum
< 0 || nargnum
/ 10 != argnum
)
855 warning (OPT_Wformat_
, "missing $ operand number in format");
862 if (pedantic
&& !dollar_format_warned
)
864 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
865 C_STD_NAME (STD_EXT
));
866 dollar_format_warned
= 1;
868 if (overflow_flag
|| argnum
== 0
869 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
871 warning (OPT_Wformat_
, "operand number out of range in format");
874 if (argnum
> dollar_max_arg_used
)
875 dollar_max_arg_used
= argnum
;
876 /* For vprintf-style functions we may need to allocate more memory to
877 track which arguments are used. */
878 while (dollar_arguments_alloc
< dollar_max_arg_used
)
881 nalloc
= 2 * dollar_arguments_alloc
+ 16;
882 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
884 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
886 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
887 nalloc
- dollar_arguments_alloc
);
888 dollar_arguments_alloc
= nalloc
;
890 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
891 && dollar_arguments_used
[argnum
- 1] == 1)
893 dollar_arguments_used
[argnum
- 1] = 2;
894 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
898 dollar_arguments_used
[argnum
- 1] = 1;
899 if (dollar_first_arg_num
)
903 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
904 *param_ptr
= TREE_CHAIN (*param_ptr
);
906 /* This case shouldn't be caught here. */
907 gcc_assert (*param_ptr
);
914 /* Ensure that FORMAT does not start with a decimal number followed by
915 a $; give a diagnostic and return true if it does, false otherwise. */
918 avoid_dollar_number (const char *format
)
920 if (!ISDIGIT (*format
))
922 while (ISDIGIT (*format
))
926 warning (OPT_Wformat_
, "%<$%> operand number used after format without operand number");
933 /* Finish the checking for a format string that used $ operand number formats
934 instead of non-$ formats. We check for unused operands before used ones
935 (a serious error, since the implementation of the format function
936 can't know what types to pass to va_arg to find the later arguments).
937 and for unused operands at the end of the format (if we know how many
938 arguments the format had, so not for vprintf). If there were operand
939 numbers out of range on a non-vprintf-style format, we won't have reached
940 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
944 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
947 bool found_pointer_gap
= false;
948 for (i
= 0; i
< dollar_max_arg_used
; i
++)
950 if (!dollar_arguments_used
[i
])
952 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
953 || dollar_arguments_pointer_p
[i
]))
954 found_pointer_gap
= true;
956 warning_at (res
->format_string_loc
, OPT_Wformat_
,
957 "format argument %d unused before used argument %d in %<$%>-style format",
958 i
+ 1, dollar_max_arg_used
);
961 if (found_pointer_gap
962 || (dollar_first_arg_num
963 && dollar_max_arg_used
< dollar_arguments_count
))
966 res
->number_dollar_extra_args
++;
971 /* Retrieve the specification for a format flag. SPEC contains the
972 specifications for format flags for the applicable kind of format.
973 FLAG is the flag in question. If PREDICATES is NULL, the basic
974 spec for that flag must be retrieved and must exist. If
975 PREDICATES is not NULL, it is a string listing possible predicates
976 for the spec entry; if an entry predicated on any of these is
977 found, it is returned, otherwise NULL is returned. */
979 static const format_flag_spec
*
980 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
983 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
985 if (spec
[i
].flag_char
!= flag
)
987 if (predicates
!= NULL
)
989 if (spec
[i
].predicate
!= 0
990 && strchr (predicates
, spec
[i
].predicate
) != 0)
993 else if (spec
[i
].predicate
== 0)
996 gcc_assert (predicates
);
1001 /* Check the argument list of a call to printf, scanf, etc.
1002 INFO points to the function_format_info structure.
1003 PARAMS is the list of argument values. */
1006 check_format_info (function_format_info
*info
, tree params
,
1007 vec
<location_t
> *arglocs
)
1009 format_check_context format_ctx
;
1010 unsigned HOST_WIDE_INT arg_num
;
1012 format_check_results res
;
1013 /* Skip to format argument. If the argument isn't available, there's
1014 no work for us to do; prototype checking will catch the problem. */
1015 for (arg_num
= 1; ; ++arg_num
)
1019 if (arg_num
== info
->format_num
)
1021 params
= TREE_CHAIN (params
);
1023 format_tree
= TREE_VALUE (params
);
1024 params
= TREE_CHAIN (params
);
1025 if (format_tree
== 0)
1028 res
.number_non_literal
= 0;
1029 res
.number_extra_args
= 0;
1030 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1031 res
.number_dollar_extra_args
= 0;
1032 res
.number_wide
= 0;
1033 res
.number_non_char
= 0;
1034 res
.number_empty
= 0;
1035 res
.number_unterminated
= 0;
1036 res
.number_other
= 0;
1037 res
.format_string_loc
= input_location
;
1039 format_ctx
.res
= &res
;
1040 format_ctx
.info
= info
;
1041 format_ctx
.params
= params
;
1042 format_ctx
.arglocs
= arglocs
;
1044 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1045 format_tree
, arg_num
);
1047 location_t loc
= format_ctx
.res
->format_string_loc
;
1049 if (res
.number_non_literal
> 0)
1051 /* Functions taking a va_list normally pass a non-literal format
1052 string. These functions typically are declared with
1053 first_arg_num == 0, so avoid warning in those cases. */
1054 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1056 /* For strftime-like formats, warn for not checking the format
1057 string; but there are no arguments to check. */
1058 warning_at (loc
, OPT_Wformat_nonliteral
,
1059 "format not a string literal, format string not checked");
1061 else if (info
->first_arg_num
!= 0)
1063 /* If there are no arguments for the format at all, we may have
1064 printf (foo) which is likely to be a security hole. */
1065 while (arg_num
+ 1 < info
->first_arg_num
)
1069 params
= TREE_CHAIN (params
);
1072 if (params
== 0 && warn_format_security
)
1073 warning_at (loc
, OPT_Wformat_security
,
1074 "format not a string literal and no format arguments");
1075 else if (params
== 0 && warn_format_nonliteral
)
1076 warning_at (loc
, OPT_Wformat_nonliteral
,
1077 "format not a string literal and no format arguments");
1079 warning_at (loc
, OPT_Wformat_nonliteral
,
1080 "format not a string literal, argument types not checked");
1084 /* If there were extra arguments to the format, normally warn. However,
1085 the standard does say extra arguments are ignored, so in the specific
1086 case where we have multiple leaves (conditional expressions or
1087 ngettext) allow extra arguments if at least one leaf didn't have extra
1088 arguments, but was otherwise OK (either non-literal or checked OK).
1089 If the format is an empty string, this should be counted similarly to the
1090 case of extra format arguments. */
1091 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1092 && res
.number_other
== 0)
1094 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1095 res
.extra_arg_loc
= loc
;
1096 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1097 "too many arguments for format");
1099 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1100 && res
.number_other
== 0)
1101 warning_at (loc
, OPT_Wformat_extra_args
, "unused arguments in %<$%>-style format");
1102 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1103 && res
.number_other
== 0)
1104 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1105 format_types
[info
->format_type
].name
);
1107 if (res
.number_wide
> 0)
1108 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1110 if (res
.number_non_char
> 0)
1111 warning_at (loc
, OPT_Wformat_
,
1112 "format string is not an array of type %qs", "char");
1114 if (res
.number_unterminated
> 0)
1115 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1118 /* Callback from check_function_arguments_recurse to check a
1119 format string. FORMAT_TREE is the format parameter. ARG_NUM
1120 is the number of the format argument. CTX points to a
1121 format_check_context. */
1124 check_format_arg (void *ctx
, tree format_tree
,
1125 unsigned HOST_WIDE_INT arg_num
)
1127 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1128 format_check_results
*res
= format_ctx
->res
;
1129 function_format_info
*info
= format_ctx
->info
;
1130 tree params
= format_ctx
->params
;
1131 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1134 HOST_WIDE_INT offset
;
1135 const char *format_chars
;
1136 tree array_size
= 0;
1139 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1141 /* Pull out a constant value if the front end didn't, and handle location
1143 format_tree
= fold_for_warn (format_tree
);
1144 STRIP_NOPS (format_tree
);
1146 if (integer_zerop (format_tree
))
1148 /* Skip to first argument to check, so we can see if this format
1149 has any arguments (it shouldn't). */
1150 while (arg_num
+ 1 < info
->first_arg_num
)
1154 params
= TREE_CHAIN (params
);
1159 res
->number_other
++;
1162 if (res
->number_extra_args
== 0)
1163 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1165 res
->number_extra_args
++;
1171 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1175 arg0
= TREE_OPERAND (format_tree
, 0);
1176 arg1
= TREE_OPERAND (format_tree
, 1);
1179 if (TREE_CODE (arg1
) == INTEGER_CST
)
1183 res
->number_non_literal
++;
1186 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1187 if (!cst_and_fits_in_hwi (arg1
))
1189 res
->number_non_literal
++;
1192 offset
= int_cst_value (arg1
);
1194 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1196 res
->number_non_literal
++;
1199 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1200 format_tree
= TREE_OPERAND (format_tree
, 0);
1201 if (format_types
[info
->format_type
].flags
1202 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1204 /* We cannot examine this string here - but we can check that it is
1206 if (TREE_CODE (format_tree
) != CONST_DECL
)
1208 res
->number_non_literal
++;
1211 /* Skip to first argument to check. */
1212 while (arg_num
+ 1 < info
->first_arg_num
)
1216 params
= TREE_CHAIN (params
);
1221 if (TREE_CODE (format_tree
) == ARRAY_REF
1222 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1223 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1224 format_tree
= TREE_OPERAND (format_tree
, 0);
1227 res
->number_non_literal
++;
1230 if (VAR_P (format_tree
)
1231 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1232 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1233 && TREE_CODE (array_init
) == STRING_CST
)
1235 /* Extract the string constant initializer. Note that this may include
1236 a trailing NUL character that is not in the array (e.g.
1237 const char a[3] = "foo";). */
1238 array_size
= DECL_SIZE_UNIT (format_tree
);
1239 format_tree
= array_init
;
1241 if (TREE_CODE (format_tree
) != STRING_CST
)
1243 res
->number_non_literal
++;
1246 tree underlying_type
1247 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
)));
1248 if (underlying_type
!= char_type_node
)
1250 if (underlying_type
== char16_type_node
1251 || underlying_type
== char32_type_node
1252 || underlying_type
== wchar_type_node
)
1255 res
->number_non_char
++;
1258 format_chars
= TREE_STRING_POINTER (format_tree
);
1259 format_length
= TREE_STRING_LENGTH (format_tree
);
1260 if (array_size
!= 0)
1262 /* Variable length arrays can't be initialized. */
1263 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1265 if (tree_fits_shwi_p (array_size
))
1267 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1268 if (array_size_value
> 0
1269 && array_size_value
== (int) array_size_value
1270 && format_length
> array_size_value
)
1271 format_length
= array_size_value
;
1276 if (offset
>= format_length
)
1278 res
->number_non_literal
++;
1281 format_chars
+= offset
;
1282 format_length
-= offset
;
1284 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1286 res
->number_unterminated
++;
1289 if (format_length
== 0)
1291 res
->number_empty
++;
1295 /* Skip to first argument to check. */
1296 while (arg_num
+ 1 < info
->first_arg_num
)
1300 params
= TREE_CHAIN (params
);
1303 /* Provisionally increment res->number_other; check_format_info_main
1304 will decrement it if it finds there are extra arguments, but this way
1305 need not adjust it for every return. */
1306 res
->number_other
++;
1307 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1308 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1309 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1312 /* Support class for argument_parser and check_format_info_main.
1313 Tracks any flag characters that have been applied to the
1314 current argument. */
1320 bool has_char_p (char ch
) const;
1321 void add_char (char ch
);
1322 void validate (const format_kind_info
*fki
,
1323 const format_char_info
*fci
,
1324 const format_flag_spec
*flag_specs
,
1325 const char * const format_chars
,
1326 tree format_string_cst
,
1327 location_t format_string_loc
,
1328 const char * const orig_format_chars
,
1331 int get_alloc_flag (const format_kind_info
*fki
);
1332 int assignment_suppression_p (const format_kind_info
*fki
);
1335 char m_flag_chars
[256];
1338 /* Support struct for argument_parser and check_format_info_main.
1339 Encapsulates any length modifier applied to the current argument. */
1341 struct length_modifier
1344 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1345 scalar_identity_flag (0)
1349 length_modifier (const char *chars_
,
1350 enum format_lengths val_
,
1351 enum format_std_version std_
,
1352 int scalar_identity_flag_
)
1353 : chars (chars_
), val (val_
), std (std_
),
1354 scalar_identity_flag (scalar_identity_flag_
)
1359 enum format_lengths val
;
1360 enum format_std_version std
;
1361 int scalar_identity_flag
;
1364 /* Parsing one argument within a format string. */
1366 class argument_parser
1369 argument_parser (function_format_info
*info
, const char *&format_chars
,
1370 tree format_string_cst
,
1371 const char * const orig_format_chars
,
1372 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1373 int &has_operand_number
, tree first_fillin_param
,
1374 object_allocator
<format_wanted_type
> &fwt_pool_
,
1375 vec
<location_t
> *arglocs
);
1377 bool read_any_dollar ();
1379 bool read_format_flags ();
1382 read_any_format_width (tree
¶ms
,
1383 unsigned HOST_WIDE_INT
&arg_num
);
1386 read_any_format_left_precision ();
1389 read_any_format_precision (tree
¶ms
,
1390 unsigned HOST_WIDE_INT
&arg_num
);
1392 void handle_alloc_chars ();
1394 length_modifier
read_any_length_modifier ();
1396 void read_any_other_modifier ();
1398 const format_char_info
*find_format_char_info (char format_char
);
1401 validate_flag_pairs (const format_char_info
*fci
,
1405 give_y2k_warnings (const format_char_info
*fci
,
1408 void parse_any_scan_set (const format_char_info
*fci
);
1410 bool handle_conversions (const format_char_info
*fci
,
1411 const length_modifier
&len_modifier
,
1413 const char *&wanted_type_name
,
1414 unsigned HOST_WIDE_INT
&arg_num
,
1419 check_argument_type (const format_char_info
*fci
,
1420 const struct kernel_ext_fmt
*kef
,
1421 const length_modifier
&len_modifier
,
1423 const char *&wanted_type_name
,
1424 const bool suppressed
,
1425 unsigned HOST_WIDE_INT
&arg_num
,
1427 const int alloc_flag
,
1428 const char * const format_start
,
1429 const char * const type_start
,
1430 location_t fmt_param_loc
,
1431 char conversion_char
);
1434 const function_format_info
*const info
;
1435 const format_kind_info
* const fki
;
1436 const format_flag_spec
* const flag_specs
;
1437 const char *start_of_this_format
;
1438 const char *&format_chars
;
1439 const tree format_string_cst
;
1440 const char * const orig_format_chars
;
1441 const location_t format_string_loc
;
1442 object_allocator
<format_wanted_type
> &fwt_pool
;
1443 flag_chars_t
&flag_chars
;
1445 tree main_arg_params
;
1446 int &has_operand_number
;
1447 const tree first_fillin_param
;
1448 format_wanted_type width_wanted_type
;
1449 format_wanted_type precision_wanted_type
;
1451 format_wanted_type main_wanted_type
;
1453 format_wanted_type
*first_wanted_type
;
1454 format_wanted_type
*last_wanted_type
;
1455 vec
<location_t
> *arglocs
;
1458 /* flag_chars_t's constructor. */
1460 flag_chars_t::flag_chars_t ()
1462 m_flag_chars
[0] = 0;
1465 /* Has CH been seen as a flag within the current argument? */
1468 flag_chars_t::has_char_p (char ch
) const
1470 return strchr (m_flag_chars
, ch
) != 0;
1473 /* Add CH to the flags seen within the current argument. */
1476 flag_chars_t::add_char (char ch
)
1478 int i
= strlen (m_flag_chars
);
1479 m_flag_chars
[i
++] = ch
;
1480 m_flag_chars
[i
] = 0;
1483 /* Validate the individual flags used, removing any that are invalid. */
1486 flag_chars_t::validate (const format_kind_info
*fki
,
1487 const format_char_info
*fci
,
1488 const format_flag_spec
*flag_specs
,
1489 const char * const format_chars
,
1490 tree format_string_cst
,
1491 location_t format_string_loc
,
1492 const char * const orig_format_chars
,
1498 bool quotflag
= false;
1500 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
1502 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1503 m_flag_chars
[i
], NULL
);
1504 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
1505 if (m_flag_chars
[i
] == fki
->length_code_char
)
1508 /* Remember if a quoting flag is seen. */
1509 quotflag
|= s
->quoting
;
1511 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
1513 format_warning_at_char (format_string_loc
, format_string_cst
,
1514 format_chars
- orig_format_chars
,
1516 "%s used with %<%%%c%> %s format",
1517 _(s
->name
), format_char
, fki
->name
);
1523 const format_flag_spec
*t
;
1524 if (ADJ_STD (s
->std
) > C_STD_VER
)
1525 warning_at (format_string_loc
, OPT_Wformat_
,
1526 "%s does not support %s",
1527 C_STD_NAME (s
->std
), _(s
->long_name
));
1528 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
1529 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
1531 const char *long_name
= (t
->long_name
!= NULL
1534 if (ADJ_STD (t
->std
) > C_STD_VER
)
1535 warning_at (format_string_loc
, OPT_Wformat_
,
1536 "%s does not support %s with the %<%%%c%> %s format",
1537 C_STD_NAME (t
->std
), _(long_name
),
1538 format_char
, fki
->name
);
1542 /* Detect quoting directives used within a quoted sequence, such
1543 as GCC's "%<...%qE". */
1544 if (quoted
&& s
->quoting
)
1546 format_warning_at_char (format_string_loc
, format_string_cst
,
1547 format_chars
- orig_format_chars
- 1,
1549 "%s used within a quoted sequence",
1553 m_flag_chars
[i
- d
] = 0;
1557 && strchr (fci
->flags2
, '\''))
1559 format_warning_at_char (format_string_loc
, format_string_cst
,
1560 format_chars
- orig_format_chars
,
1562 "%qc conversion used unquoted",
1567 /* Determine if an assignment-allocation has been set, requiring
1568 an extra char ** for writing back a dynamically-allocated char *.
1569 This is for handling the optional 'm' character in scanf. */
1572 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
1574 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1575 && has_char_p ('a'))
1577 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
1582 /* Determine if an assignment-suppression character was seen.
1583 ('*' in scanf, for discarding the converted input). */
1586 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
1588 if (fki
->suppression_char
1589 && has_char_p (fki
->suppression_char
))
1594 /* Constructor for argument_parser. Initialize for parsing one
1595 argument within a format string. */
1598 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
1599 tree format_string_cst_
,
1600 const char * const orig_format_chars_
,
1601 location_t format_string_loc_
,
1602 flag_chars_t
&flag_chars_
,
1603 int &has_operand_number_
,
1604 tree first_fillin_param_
,
1605 object_allocator
<format_wanted_type
> &fwt_pool_
,
1606 vec
<location_t
> *arglocs_
)
1608 fki (&format_types
[info
->format_type
]),
1609 flag_specs (fki
->flag_specs
),
1610 start_of_this_format (format_chars_
),
1611 format_chars (format_chars_
),
1612 format_string_cst (format_string_cst_
),
1613 orig_format_chars (orig_format_chars_
),
1614 format_string_loc (format_string_loc_
),
1615 fwt_pool (fwt_pool_
),
1616 flag_chars (flag_chars_
),
1618 main_arg_params (NULL
),
1619 has_operand_number (has_operand_number_
),
1620 first_fillin_param (first_fillin_param_
),
1621 first_wanted_type (NULL
),
1622 last_wanted_type (NULL
),
1627 /* Handle dollars at the start of format arguments, setting up main_arg_params
1630 Return true if format parsing is to continue, false otherwise. */
1633 argument_parser::read_any_dollar ()
1635 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
1637 /* Possibly read a $ operand number at the start of the format.
1638 If one was previously used, one is required here. If one
1639 is not used here, we can't immediately conclude this is a
1640 format without them, since it could be printf %m or scanf %*. */
1642 opnum
= maybe_read_dollar_number (&format_chars
, 0,
1644 &main_arg_params
, fki
);
1649 has_operand_number
= 1;
1650 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
1653 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
1655 if (avoid_dollar_number (format_chars
))
1661 /* Read any format flags, but do not yet validate them beyond removing
1662 duplicates, since in general validation depends on the rest of
1665 Return true if format parsing is to continue, false otherwise. */
1668 argument_parser::read_format_flags ()
1670 while (*format_chars
!= 0
1671 && strchr (fki
->flag_chars
, *format_chars
) != 0)
1673 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
1674 *format_chars
, NULL
);
1675 if (flag_chars
.has_char_p (*format_chars
))
1677 format_warning_at_char (format_string_loc
, format_string_cst
,
1678 format_chars
+ 1 - orig_format_chars
,
1680 "repeated %s in format", _(s
->name
));
1683 flag_chars
.add_char (*format_chars
);
1685 if (s
->skip_next_char
)
1688 if (*format_chars
== 0)
1690 warning_at (format_string_loc
, OPT_Wformat_
,
1691 "missing fill character at end of strfmon format");
1701 /* Read any format width, possibly * or *m$.
1703 Return true if format parsing is to continue, false otherwise. */
1707 read_any_format_width (tree
¶ms
,
1708 unsigned HOST_WIDE_INT
&arg_num
)
1710 if (!fki
->width_char
)
1713 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
1715 flag_chars
.add_char (fki
->width_char
);
1716 /* "...a field width...may be indicated by an asterisk.
1717 In this case, an int argument supplies the field width..." */
1719 if (has_operand_number
!= 0)
1722 opnum
= maybe_read_dollar_number (&format_chars
,
1723 has_operand_number
== 1,
1730 has_operand_number
= 1;
1731 arg_num
= opnum
+ info
->first_arg_num
- 1;
1734 has_operand_number
= 0;
1738 if (avoid_dollar_number (format_chars
))
1741 if (info
->first_arg_num
!= 0)
1748 cur_param
= TREE_VALUE (params
);
1749 if (has_operand_number
<= 0)
1751 params
= TREE_CHAIN (params
);
1755 width_wanted_type
.wanted_type
= *fki
->width_type
;
1756 width_wanted_type
.wanted_type_name
= NULL
;
1757 width_wanted_type
.pointer_count
= 0;
1758 width_wanted_type
.char_lenient_flag
= 0;
1759 width_wanted_type
.scalar_identity_flag
= 0;
1760 width_wanted_type
.writing_in_flag
= 0;
1761 width_wanted_type
.reading_from_flag
= 0;
1762 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
1763 width_wanted_type
.format_start
= format_chars
- 1;
1764 width_wanted_type
.format_length
= 1;
1765 width_wanted_type
.param
= cur_param
;
1766 width_wanted_type
.arg_num
= arg_num
;
1767 width_wanted_type
.offset_loc
=
1768 format_chars
- orig_format_chars
;
1769 width_wanted_type
.next
= NULL
;
1770 if (last_wanted_type
!= 0)
1771 last_wanted_type
->next
= &width_wanted_type
;
1772 if (first_wanted_type
== 0)
1773 first_wanted_type
= &width_wanted_type
;
1774 last_wanted_type
= &width_wanted_type
;
1779 /* Possibly read a numeric width. If the width is zero,
1780 we complain if appropriate. */
1781 int non_zero_width_char
= FALSE
;
1782 int found_width
= FALSE
;
1783 while (ISDIGIT (*format_chars
))
1786 if (*format_chars
!= '0')
1787 non_zero_width_char
= TRUE
;
1790 if (found_width
&& !non_zero_width_char
&&
1791 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
1792 warning_at (format_string_loc
, OPT_Wformat_
,
1793 "zero width in %s format", fki
->name
);
1795 flag_chars
.add_char (fki
->width_char
);
1801 /* Read any format left precision (must be a number, not *). */
1803 argument_parser::read_any_format_left_precision ()
1805 if (fki
->left_precision_char
== 0)
1807 if (*format_chars
!= '#')
1811 flag_chars
.add_char (fki
->left_precision_char
);
1812 if (!ISDIGIT (*format_chars
))
1813 format_warning_at_char (format_string_loc
, format_string_cst
,
1814 format_chars
- orig_format_chars
,
1816 "empty left precision in %s format", fki
->name
);
1817 while (ISDIGIT (*format_chars
))
1821 /* Read any format precision, possibly * or *m$.
1823 Return true if format parsing is to continue, false otherwise. */
1827 read_any_format_precision (tree
¶ms
,
1828 unsigned HOST_WIDE_INT
&arg_num
)
1830 if (fki
->precision_char
== 0)
1832 if (*format_chars
!= '.')
1836 flag_chars
.add_char (fki
->precision_char
);
1837 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
1839 /* "...a...precision...may be indicated by an asterisk.
1840 In this case, an int argument supplies the...precision." */
1842 if (has_operand_number
!= 0)
1845 opnum
= maybe_read_dollar_number (&format_chars
,
1846 has_operand_number
== 1,
1853 has_operand_number
= 1;
1854 arg_num
= opnum
+ info
->first_arg_num
- 1;
1857 has_operand_number
= 0;
1861 if (avoid_dollar_number (format_chars
))
1864 if (info
->first_arg_num
!= 0)
1871 cur_param
= TREE_VALUE (params
);
1872 if (has_operand_number
<= 0)
1874 params
= TREE_CHAIN (params
);
1878 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
1879 precision_wanted_type
.wanted_type_name
= NULL
;
1880 precision_wanted_type
.pointer_count
= 0;
1881 precision_wanted_type
.char_lenient_flag
= 0;
1882 precision_wanted_type
.scalar_identity_flag
= 0;
1883 precision_wanted_type
.writing_in_flag
= 0;
1884 precision_wanted_type
.reading_from_flag
= 0;
1885 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
1886 precision_wanted_type
.param
= cur_param
;
1887 precision_wanted_type
.format_start
= format_chars
- 2;
1888 precision_wanted_type
.format_length
= 2;
1889 precision_wanted_type
.arg_num
= arg_num
;
1890 precision_wanted_type
.offset_loc
=
1891 format_chars
- orig_format_chars
;
1892 precision_wanted_type
.next
= NULL
;
1893 if (last_wanted_type
!= 0)
1894 last_wanted_type
->next
= &precision_wanted_type
;
1895 if (first_wanted_type
== 0)
1896 first_wanted_type
= &precision_wanted_type
;
1897 last_wanted_type
= &precision_wanted_type
;
1902 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
1903 && !ISDIGIT (*format_chars
))
1904 format_warning_at_char (format_string_loc
, format_string_cst
,
1905 format_chars
- orig_format_chars
,
1907 "empty precision in %s format", fki
->name
);
1908 while (ISDIGIT (*format_chars
))
1915 /* Parse any assignment-allocation flags, which request an extra
1916 char ** for writing back a dynamically-allocated char *.
1917 This is for handling the optional 'm' character in scanf,
1918 and, before C99, 'a' (for compatibility with a non-standard
1919 GNU libc extension). */
1922 argument_parser::handle_alloc_chars ()
1924 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
1926 flag_chars
.add_char (fki
->alloc_char
);
1930 /* Handle the scanf allocation kludge. */
1931 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
1933 if (*format_chars
== 'a' && !flag_isoc99
)
1935 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1936 || format_chars
[1] == '[')
1938 /* 'a' is used as a flag. */
1939 flag_chars
.add_char ('a');
1946 /* Look for length modifiers within the current format argument,
1947 returning a length_modifier instance describing it (or the
1948 default if one is not found).
1950 Issue warnings about non-standard modifiers. */
1953 argument_parser::read_any_length_modifier ()
1955 length_modifier result
;
1957 const format_length_info
*fli
= fki
->length_char_specs
;
1961 while (fli
->name
!= 0
1962 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
1966 format_chars
+= strlen (fli
->name
);
1967 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
1970 result
= length_modifier (fli
->double_name
, fli
->double_index
,
1971 fli
->double_std
, 0);
1975 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
1976 fli
->scalar_identity_flag
);
1978 flag_chars
.add_char (fki
->length_code_char
);
1982 /* Warn if the length modifier is non-standard. */
1983 if (ADJ_STD (result
.std
) > C_STD_VER
)
1984 warning_at (format_string_loc
, OPT_Wformat_
,
1985 "%s does not support the %qs %s length modifier",
1986 C_STD_NAME (result
.std
), result
.chars
,
1993 /* Read any other modifier (strftime E/O). */
1996 argument_parser::read_any_other_modifier ()
1998 if (fki
->modifier_chars
== NULL
)
2001 while (*format_chars
!= 0
2002 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2004 if (flag_chars
.has_char_p (*format_chars
))
2006 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2007 *format_chars
, NULL
);
2008 format_warning_at_char (format_string_loc
, format_string_cst
,
2009 format_chars
- orig_format_chars
,
2011 "repeated %s in format", _(s
->name
));
2014 flag_chars
.add_char (*format_chars
);
2019 /* Return the format_char_info corresponding to FORMAT_CHAR,
2020 potentially issuing a warning if the format char is
2021 not supported in the C standard version we are checking
2024 Issue a warning and return NULL if it is not found.
2026 Issue warnings about non-standard modifiers. */
2028 const format_char_info
*
2029 argument_parser::find_format_char_info (char format_char
)
2031 const format_char_info
*fci
= fki
->conversion_specs
;
2033 while (fci
->format_chars
!= 0
2034 && strchr (fci
->format_chars
, format_char
) == 0)
2036 if (fci
->format_chars
== 0)
2038 format_warning_at_char (format_string_loc
, format_string_cst
,
2039 format_chars
- orig_format_chars
,
2041 "unknown conversion type character %qc in format",
2048 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2049 format_warning_at_char (format_string_loc
, format_string_cst
,
2050 format_chars
- orig_format_chars
,
2052 "%s does not support the %<%%%c%> %s format",
2053 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2059 /* Validate the pairs of flags used.
2060 Issue warnings about incompatible combinations of flags. */
2063 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2066 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2068 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2070 const format_flag_spec
*s
, *t
;
2071 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2073 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2075 if (bad_flag_pairs
[i
].predicate
!= 0
2076 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2078 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2079 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2080 if (bad_flag_pairs
[i
].ignored
)
2082 if (bad_flag_pairs
[i
].predicate
!= 0)
2083 warning_at (format_string_loc
, OPT_Wformat_
,
2084 "%s ignored with %s and %<%%%c%> %s format",
2085 _(s
->name
), _(t
->name
), format_char
,
2088 warning_at (format_string_loc
, OPT_Wformat_
,
2089 "%s ignored with %s in %s format",
2090 _(s
->name
), _(t
->name
), fki
->name
);
2094 if (bad_flag_pairs
[i
].predicate
!= 0)
2095 warning_at (format_string_loc
, OPT_Wformat_
,
2096 "use of %s and %s together with %<%%%c%> %s format",
2097 _(s
->name
), _(t
->name
), format_char
,
2100 warning_at (format_string_loc
, OPT_Wformat_
,
2101 "use of %s and %s together in %s format",
2102 _(s
->name
), _(t
->name
), fki
->name
);
2107 /* Give Y2K warnings. */
2110 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2113 if (!warn_format_y2k
)
2117 if (strchr (fci
->flags2
, '4') != 0)
2118 if (flag_chars
.has_char_p ('E'))
2122 else if (strchr (fci
->flags2
, '3') != 0)
2124 else if (strchr (fci
->flags2
, '2') != 0)
2127 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2128 "%<%%%c%> yields only last 2 digits of year in some locales", format_char
);
2129 else if (y2k_level
== 2)
2130 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2131 "%<%%%c%> yields only last 2 digits of year",
2135 /* Parse any "scan sets" enclosed in square brackets, e.g.
2136 for scanf-style calls. */
2139 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2141 if (strchr (fci
->flags2
, '[') == NULL
)
2144 /* Skip over scan set, in case it happens to have '%' in it. */
2145 if (*format_chars
== '^')
2147 /* Find closing bracket; if one is hit immediately, then
2148 it's part of the scan set rather than a terminator. */
2149 if (*format_chars
== ']')
2151 while (*format_chars
&& *format_chars
!= ']')
2153 if (*format_chars
!= ']')
2154 /* The end of the format string was reached. */
2155 format_warning_at_char (format_string_loc
, format_string_cst
,
2156 format_chars
- orig_format_chars
,
2158 "no closing %<]%> for %<%%[%> format");
2161 /* Return true if this argument is to be continued to be parsed,
2162 false to skip to next argument. */
2165 argument_parser::handle_conversions (const format_char_info
*fci
,
2166 const length_modifier
&len_modifier
,
2168 const char *&wanted_type_name
,
2169 unsigned HOST_WIDE_INT
&arg_num
,
2173 enum format_std_version wanted_type_std
;
2175 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2178 wanted_type
= (fci
->types
[len_modifier
.val
].type
2179 ? *fci
->types
[len_modifier
.val
].type
: 0);
2180 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2181 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2182 if (wanted_type
== 0)
2184 format_warning_at_char (format_string_loc
, format_string_cst
,
2185 format_chars
- orig_format_chars
,
2187 "use of %qs length modifier with %qc type character has either no effect or undefined behavior",
2188 len_modifier
.chars
, format_char
);
2189 /* Heuristic: skip one argument when an invalid length/type
2190 combination is encountered. */
2193 params
= TREE_CHAIN (params
);
2197 /* Warn if non-standard, provided it is more non-standard
2198 than the length and type characters that may already
2199 have been warned for. */
2200 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2201 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2203 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2204 format_warning_at_char (format_string_loc
, format_string_cst
,
2205 format_chars
- orig_format_chars
,
2207 "%s does not support the %<%%%s%c%> %s format",
2208 C_STD_NAME (wanted_type_std
),
2210 format_char
, fki
->name
);
2216 /* Check type of argument against desired type.
2218 Return true if format parsing is to continue, false otherwise. */
2222 check_argument_type (const format_char_info
*fci
,
2223 const struct kernel_ext_fmt
*kef
,
2224 const length_modifier
&len_modifier
,
2226 const char *&wanted_type_name
,
2227 const bool suppressed
,
2228 unsigned HOST_WIDE_INT
&arg_num
,
2230 const int alloc_flag
,
2231 const char * const format_start
,
2232 const char * const type_start
,
2233 location_t fmt_param_loc
,
2234 char conversion_char
)
2236 if (info
->first_arg_num
== 0)
2239 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2242 if (main_arg_num
!= 0)
2245 warning_at (format_string_loc
, OPT_Wformat_
,
2246 "operand number specified with suppressed assignment");
2248 warning_at (format_string_loc
, OPT_Wformat_
,
2249 "operand number specified for format taking no argument");
2254 format_wanted_type
*wanted_type_ptr
;
2256 if (main_arg_num
!= 0)
2258 arg_num
= main_arg_num
;
2259 params
= main_arg_params
;
2264 if (has_operand_number
> 0)
2266 warning_at (format_string_loc
, OPT_Wformat_
,
2267 "missing $ operand number in format");
2271 has_operand_number
= 0;
2274 wanted_type_ptr
= &main_wanted_type
;
2282 cur_param
= TREE_VALUE (params
);
2283 params
= TREE_CHAIN (params
);
2286 wanted_type_ptr
->wanted_type
= wanted_type
;
2287 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2288 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2289 wanted_type_ptr
->char_lenient_flag
= 0;
2290 if (strchr (fci
->flags2
, 'c') != 0)
2291 wanted_type_ptr
->char_lenient_flag
= 1;
2292 wanted_type_ptr
->scalar_identity_flag
= 0;
2293 if (len_modifier
.scalar_identity_flag
)
2294 wanted_type_ptr
->scalar_identity_flag
= 1;
2295 wanted_type_ptr
->writing_in_flag
= 0;
2296 wanted_type_ptr
->reading_from_flag
= 0;
2298 wanted_type_ptr
->writing_in_flag
= 1;
2301 if (strchr (fci
->flags2
, 'W') != 0)
2302 wanted_type_ptr
->writing_in_flag
= 1;
2303 if (strchr (fci
->flags2
, 'R') != 0)
2304 wanted_type_ptr
->reading_from_flag
= 1;
2306 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2307 wanted_type_ptr
->param
= cur_param
;
2308 wanted_type_ptr
->arg_num
= arg_num
;
2309 wanted_type_ptr
->format_start
= format_start
;
2310 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2311 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2312 wanted_type_ptr
->next
= NULL
;
2313 if (last_wanted_type
!= 0)
2314 last_wanted_type
->next
= wanted_type_ptr
;
2315 if (first_wanted_type
== 0)
2316 first_wanted_type
= wanted_type_ptr
;
2317 last_wanted_type
= wanted_type_ptr
;
2322 wanted_type_ptr
= fwt_pool
.allocate ();
2324 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2325 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2330 if (first_wanted_type
!= 0)
2332 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2333 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2334 /* By default, use the end of the range for the caret location. */
2335 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2336 offset_to_format_end
,
2337 offset_to_format_start
, offset_to_format_end
);
2338 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2339 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2340 offset_to_type_start
,
2341 conversion_char
, arglocs
);
2343 /* note printf extension type checks are *additional* - %p must always
2344 * be pointer compatible, %d always int compatible.
2346 if (first_wanted_type
->kind
!= CF_KIND_FORMAT
|| !kef
)
2349 const struct kernel_ext_fmt
*kef_now
;
2352 for (kef_now
= kef
; kef_now
->suffix
&& !strcmp (kef
->suffix
, kef_now
->suffix
); kef_now
++)
2354 success
= check_kef_type (fmt_loc
, kef_now
,
2355 first_wanted_type
->arg_num
,
2356 first_wanted_type
->param
,
2357 kef_now
->type
, fki
, offset_to_type_start
, conversion_char
, arglocs
);
2363 location_t param_loc
;
2365 if (EXPR_HAS_LOCATION (first_wanted_type
->param
))
2366 param_loc
= EXPR_LOCATION (first_wanted_type
->param
);
2369 /* arg_num is 1-based. */
2370 gcc_assert (first_wanted_type
->arg_num
> 0);
2371 param_loc
= (*arglocs
)[first_wanted_type
->arg_num
- 1];
2374 format_type_warning (fmt_loc
, param_loc
, first_wanted_type
,
2375 kef
->type
, TREE_TYPE (first_wanted_type
->param
),
2376 fki
, offset_to_type_start
, conversion_char
);
2382 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2383 is the NUL-terminated format string (which at this point may contain
2384 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2385 terminating NUL character). ARG_NUM is one less than the number of
2386 the first format argument to check; PARAMS points to that format
2387 argument in the list of arguments. */
2390 check_format_info_main (format_check_results
*res
,
2391 function_format_info
*info
, const char *format_chars
,
2392 location_t fmt_param_loc
, tree format_string_cst
,
2393 int format_length
, tree params
,
2394 unsigned HOST_WIDE_INT arg_num
,
2395 object_allocator
<format_wanted_type
> &fwt_pool
,
2396 vec
<location_t
> *arglocs
)
2398 const char * const orig_format_chars
= format_chars
;
2399 const tree first_fillin_param
= params
;
2401 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
2402 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
2403 const location_t format_string_loc
= res
->format_string_loc
;
2405 /* -1 if no conversions taking an operand have been found; 0 if one has
2406 and it didn't use $; 1 if $ formats are in use. */
2407 int has_operand_number
= -1;
2409 /* Vector of pointers to opening quoting directives (like GCC "%<"). */
2410 auto_vec
<const char*> quotdirs
;
2412 /* Pointers to the most recent color directives (like GCC's "%r or %R").
2413 A starting color directive much be terminated before the end of
2414 the format string. A terminating directive makes no sense without
2415 a prior starting directive. */
2416 const char *color_begin
= NULL
;
2417 const char *color_end
= NULL
;
2419 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
2421 while (*format_chars
!= 0)
2423 if (*format_chars
++ != '%')
2425 if (*format_chars
== 0)
2427 format_warning_at_char (format_string_loc
, format_string_cst
,
2428 format_chars
- orig_format_chars
,
2430 "spurious trailing %<%%%> in format");
2433 if (*format_chars
== '%')
2439 flag_chars_t flag_chars
;
2440 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
2441 orig_format_chars
, format_string_loc
,
2442 flag_chars
, has_operand_number
,
2443 first_fillin_param
, fwt_pool
, arglocs
);
2445 if (!arg_parser
.read_any_dollar ())
2448 if (!arg_parser
.read_format_flags ())
2451 /* Read any format width, possibly * or *m$. */
2452 if (!arg_parser
.read_any_format_width (params
, arg_num
))
2455 /* Read any format left precision (must be a number, not *). */
2456 arg_parser
.read_any_format_left_precision ();
2458 /* Read any format precision, possibly * or *m$. */
2459 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
2462 const char *format_start
= format_chars
;
2464 arg_parser
.handle_alloc_chars ();
2466 /* The rest of the conversion specification is the length modifier
2467 (if any), and the conversion specifier, so this is where the
2468 type information starts. If we need to issue a suggestion
2469 about a type mismatch, then we should preserve everything up
2471 const char *type_start
= format_chars
;
2473 /* Read any length modifier, if this kind of format has them. */
2474 const length_modifier len_modifier
2475 = arg_parser
.read_any_length_modifier ();
2477 /* Read any modifier (strftime E/O). */
2478 arg_parser
.read_any_other_modifier ();
2480 char format_char
= *format_chars
;
2481 if (format_char
== 0
2482 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
2483 && format_char
== '%'))
2485 format_warning_at_char (format_string_loc
, format_string_cst
,
2486 format_chars
- orig_format_chars
,
2488 "conversion lacks type at end of format");
2493 const format_char_info
* const fci
2494 = arg_parser
.find_format_char_info (format_char
);
2498 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
2500 if (etab
&& format_chars
[0] >= 'A' && format_chars
[0] <= 'Z')
2502 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
2504 for (; etab
< etab_end
&& etab
->suffix
; etab
++)
2506 if (!strncmp (etab
->suffix
, format_chars
, strlen (etab
->suffix
)))
2510 if (!etab
->suffix
|| etab
== etab_end
)
2512 format_warning_at_char (format_string_loc
, format_string_cst
,
2513 format_chars
- orig_format_chars
+ 1,
2515 "unrecognized printf extension suffix");
2520 format_chars
+= strlen (etab
->suffix
);
2526 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
2528 format_string_loc
, orig_format_chars
, format_char
,
2529 quotdirs
.length () > 0);
2531 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
2532 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
2534 /* Diagnose nested or unmatched quoting directives such as GCC's
2535 "%<...%<" and "%>...%>". */
2536 bool quot_begin_p
= strchr (fci
->flags2
, '<');
2537 bool quot_end_p
= strchr (fci
->flags2
, '>');
2539 if (quot_begin_p
&& !quot_end_p
)
2541 if (quotdirs
.length ())
2542 format_warning_at_char (format_string_loc
, format_string_cst
,
2543 format_chars
- orig_format_chars
,
2545 "nested quoting directive");
2546 quotdirs
.safe_push (format_chars
);
2548 else if (!quot_begin_p
&& quot_end_p
)
2550 if (quotdirs
.length ())
2553 format_warning_at_char (format_string_loc
, format_string_cst
,
2554 format_chars
- orig_format_chars
,
2556 "unmatched quoting directive");
2559 bool color_begin_p
= strchr (fci
->flags2
, '/');
2562 color_begin
= format_chars
;
2565 else if (strchr (fci
->flags2
, '\\'))
2568 format_warning_at_char (format_string_loc
, format_string_cst
,
2569 format_chars
- orig_format_chars
,
2571 "%qc directive redundant after prior occurence of the same", format_char
);
2572 else if (!color_begin
)
2573 format_warning_at_char (format_string_loc
, format_string_cst
,
2574 format_chars
- orig_format_chars
,
2576 "unmatched color reset directive");
2577 color_end
= format_chars
;
2580 /* Diagnose directives that shouldn't appear in a quoted sequence.
2581 (They are denoted by a double quote in FLAGS2.) */
2582 if (quotdirs
.length ())
2584 if (strchr (fci
->flags2
, '"'))
2585 format_warning_at_char (format_string_loc
, format_string_cst
,
2586 format_chars
- orig_format_chars
,
2588 "%qc conversion used within a quoted sequence",
2592 /* Validate the pairs of flags used. */
2593 arg_parser
.validate_flag_pairs (fci
, format_char
);
2595 arg_parser
.give_y2k_warnings (fci
, format_char
);
2597 arg_parser
.parse_any_scan_set (fci
);
2599 tree wanted_type
= NULL
;
2600 const char *wanted_type_name
= NULL
;
2602 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
2603 wanted_type
, wanted_type_name
,
2609 arg_parser
.main_wanted_type
.next
= NULL
;
2611 /* Finally. . .check type of argument against desired type! */
2612 if (!arg_parser
.check_argument_type (fci
, etab
, len_modifier
,
2613 wanted_type
, wanted_type_name
,
2617 format_start
, type_start
,
2623 if (format_chars
- orig_format_chars
!= format_length
)
2624 format_warning_at_char (format_string_loc
, format_string_cst
,
2625 format_chars
+ 1 - orig_format_chars
,
2626 OPT_Wformat_contains_nul
,
2627 "embedded %<\\0%> in format");
2628 if (info
->first_arg_num
!= 0 && params
!= 0
2629 && has_operand_number
<= 0)
2631 res
->number_other
--;
2632 res
->number_extra_args
++;
2634 if (has_operand_number
> 0)
2635 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
2637 if (quotdirs
.length ())
2638 format_warning_at_char (format_string_loc
, format_string_cst
,
2639 quotdirs
.pop () - orig_format_chars
,
2640 OPT_Wformat_
, "unterminated quoting directive");
2641 if (color_begin
&& !color_end
)
2642 format_warning_at_char (format_string_loc
, format_string_cst
,
2643 color_begin
- orig_format_chars
,
2644 OPT_Wformat_
, "unterminated color directive");
2647 /* Check the argument types from a single format conversion (possibly
2648 including width and precision arguments).
2650 FMT_LOC is the location of the format conversion.
2652 TYPES is a singly-linked list expressing the parts of the format
2653 conversion that expect argument types, and the arguments they
2656 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
2657 format string to where type information begins for the conversion
2658 (the length modifier and conversion specifier).
2660 CONVERSION_CHAR is the user-provided conversion specifier.
2664 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2666 then FMT_LOC covers this range:
2668 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2671 and TYPES in this case is a three-entry singly-linked list consisting of:
2672 (1) the check for the field width here:
2673 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2676 (2) the check for the field precision here:
2677 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2680 (3) the check for the length modifier and conversion char here:
2681 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2685 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
2688 0000000000111111111122
2689 0123456789012345678901
2690 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
2692 | ` CONVERSION_CHAR: 'd'
2693 type starts here. */
2694 tree
type_normalize (tree type
, tree
*cousin
, tree target
= NULL
)
2698 if (TREE_CODE (type
) == FUNCTION_TYPE
|| TREE_CODE (type
) == POINTER_TYPE
)
2701 /* Strip off any "const" etc. */
2702 type
= build_qualified_type (type
, 0);
2703 if (TREE_CODE (TYPE_NAME (type
)) != TYPE_DECL
)
2706 if (target
&& (type
== target
|| TYPE_NAME (type
) == target
))
2709 struct type_special
*t
;
2710 for (t
= special_types
; t
->match
; t
++)
2714 if (TYPE_NAME (type
) != *t
->match
)
2716 if (t
->cousin
&& *t
->cousin
)
2717 *cousin
= *t
->cousin
;
2719 return *t
->replace
? *t
->replace
: type
;
2723 tree orig
= DECL_ORIGINAL_TYPE (TYPE_NAME (type
));
2732 /* gcc-10 asserts when you give a TYPE_DECL instead of the actual TYPE */
2734 decl_deref(tree typ
)
2736 while (TREE_CODE (typ
) == TYPE_DECL
)
2737 typ
= DECL_ORIGINAL_TYPE (typ
);
2743 check_format_types (const substring_loc
&fmt_loc
,
2744 format_wanted_type
*types
, const format_kind_info
*fki
,
2745 int offset_to_type_start
,
2746 char conversion_char
,
2747 vec
<location_t
> *arglocs
)
2749 for (; types
!= 0; types
= types
->next
)
2753 tree cur_type_cousin
= NULL
;
2760 wanted_type
= types
->wanted_type
;
2761 arg_num
= types
->arg_num
;
2763 wanted_type
= decl_deref(wanted_type
);
2765 /* The following should not occur here. */
2766 gcc_assert (wanted_type
);
2767 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
2769 if (types
->pointer_count
== 0)
2770 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
2772 switch (TREE_CODE (wanted_type
))
2774 case IDENTIFIER_NODE
:
2777 wanted_type
= TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (wanted_type
));
2780 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
2784 cur_param
= types
->param
;
2787 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
2788 NULL
, fki
, offset_to_type_start
,
2793 cur_type
= TREE_TYPE (cur_param
);
2794 if (cur_type
== error_mark_node
)
2796 orig_cur_type
= cur_type
;
2799 location_t param_loc
= UNKNOWN_LOCATION
;
2800 if (EXPR_HAS_LOCATION (cur_param
))
2801 param_loc
= EXPR_LOCATION (cur_param
);
2804 /* arg_num is 1-based. */
2805 gcc_assert (types
->arg_num
> 0);
2806 param_loc
= (*arglocs
)[types
->arg_num
- 1];
2809 STRIP_NOPS (cur_param
);
2811 /* Check the types of any additional pointer arguments
2812 that precede the "real" argument. */
2813 for (i
= 0; i
< types
->pointer_count
; ++i
)
2815 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2817 cur_type
= TREE_TYPE (cur_type
);
2818 if (cur_type
== error_mark_node
)
2821 /* Check for writing through a NULL pointer. */
2822 if (types
->writing_in_flag
2825 && integer_zerop (cur_param
))
2826 warning (OPT_Wformat_
, "writing through null pointer (argument %d)", arg_num
);
2828 /* Check for reading through a NULL pointer. */
2829 if (types
->reading_from_flag
2832 && integer_zerop (cur_param
))
2833 warning (OPT_Wformat_
, "reading through null pointer (argument %d)", arg_num
);
2835 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2836 cur_param
= TREE_OPERAND (cur_param
, 0);
2840 /* See if this is an attempt to write into a const type with
2841 scanf or with printf "%n". Note: the writing in happens
2842 at the first indirection only, if for example
2843 void * const * is passed to scanf %p; passing
2844 const void ** is simply passing an incompatible type. */
2845 if (types
->writing_in_flag
2847 && (TYPE_READONLY (cur_type
)
2849 && (CONSTANT_CLASS_P (cur_param
)
2850 || (DECL_P (cur_param
)
2851 && TREE_READONLY (cur_param
))))))
2852 warning (OPT_Wformat_
, "writing into constant object (argument %d)", arg_num
);
2854 /* If there are extra type qualifiers beyond the first
2855 indirection, then this makes the types technically
2859 && (TYPE_READONLY (cur_type
)
2860 || TYPE_VOLATILE (cur_type
)
2861 || TYPE_ATOMIC (cur_type
)
2862 || TYPE_RESTRICT (cur_type
)))
2863 warning (OPT_Wformat_
, "extra type qualifiers in format argument (argument %d)",
2869 format_type_warning (fmt_loc
, param_loc
,
2870 types
, wanted_type
, orig_cur_type
, fki
,
2871 offset_to_type_start
, conversion_char
);
2876 if (i
< types
->pointer_count
)
2879 cur_type
= type_normalize (cur_type
, &cur_type_cousin
);
2881 /* Check whether the argument type is a character type. This leniency
2882 only applies to certain formats, flagged with 'c'. */
2883 if (types
->char_lenient_flag
)
2884 char_type_flag
= (cur_type
== char_type_node
2885 || cur_type
== signed_char_type_node
2886 || cur_type
== unsigned_char_type_node
);
2888 int compat
= lang_hooks
.types_compatible_p (decl_deref (wanted_type
), decl_deref (cur_type
));
2889 /* Check the type of the "real" argument, if there's a type we want. */
2890 if ((TREE_CODE (wanted_type
) != INTEGER_TYPE
|| types
->pointer_count
)
2893 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
&& !types
->pointer_count
2897 if (TREE_CODE (cur_param
) == INTEGER_CST
)
2900 if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
2901 && TREE_CODE (cur_type
) == TYPE_DECL
)
2903 if (types
->wanted_type
== cur_type
)
2905 format_type_warning (fmt_loc
, param_loc
, types
,
2906 wanted_type
, orig_cur_type
, fki
,
2907 offset_to_type_start
, conversion_char
,
2908 " (strict match required [A])");
2911 else if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
)
2913 if (types
->wanted_type
== TYPE_NAME(cur_type
))
2915 format_type_warning (fmt_loc
, param_loc
, types
,
2916 wanted_type
, orig_cur_type
, fki
,
2917 offset_to_type_start
, conversion_char
,
2918 " (strict match required [B])");
2921 else if (wanted_type
== cur_type
)
2923 else if (cur_type_cousin
)
2925 format_type_warning (fmt_loc
, param_loc
, types
,
2926 wanted_type
, orig_cur_type
, fki
,
2927 offset_to_type_start
, conversion_char
,
2928 " (strict match required [C])");
2932 format_type_warning (fmt_loc, param_loc, types,
2933 wanted_type, orig_cur_type, fki,
2934 offset_to_type_start, conversion_char,
2935 " (ultra-pedantic mode)");
2940 /* If we want 'void *', allow any pointer type.
2941 (Anything else would already have got a warning.)
2942 With -Wpedantic, only allow pointers to void and to character
2944 if (wanted_type
== void_type_node
2945 && (!pedantic
|| (i
== 1 && char_type_flag
)))
2947 /* Don't warn about differences merely in signedness, unless
2948 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2949 target and not a character type, and for character types at
2950 a second level of indirection. */
2951 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2952 && TREE_CODE (cur_type
) == INTEGER_TYPE
2953 && ((!pedantic
&& !warn_format_signedness
)
2954 || (i
== 0 && !warn_format_signedness
)
2955 || (i
== 1 && char_type_flag
))
2956 && (TYPE_UNSIGNED (wanted_type
)
2957 ? wanted_type
== c_common_unsigned_type (cur_type
)
2958 : wanted_type
== c_common_signed_type (cur_type
)))
2960 if (cur_type_cousin
)
2962 if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
2963 && TREE_CODE (cur_type_cousin
) == TYPE_DECL
)
2965 if (types
->wanted_type
== cur_type_cousin
)
2967 format_type_warning (fmt_loc
, param_loc
, types
,
2968 wanted_type
, orig_cur_type
, fki
,
2969 offset_to_type_start
, conversion_char
,
2970 " (strict match required [X])");
2973 else if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
)
2975 if (types
->wanted_type
== TYPE_NAME(cur_type_cousin
))
2977 format_type_warning (fmt_loc
, param_loc
, types
,
2978 wanted_type
, orig_cur_type
, fki
,
2979 offset_to_type_start
, conversion_char
,
2980 " (strict match required [Y])");
2983 else if (wanted_type
== cur_type_cousin
)
2987 format_type_warning (fmt_loc
, param_loc
, types
,
2988 wanted_type
, orig_cur_type
, fki
,
2989 offset_to_type_start
, conversion_char
,
2990 " (strict match required [Z])");
2996 /* Don't warn about differences merely in signedness if we know
2997 that the current type is integer-promoted and its original type
2998 was unsigned such as that it is in the range of WANTED_TYPE. */
2999 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
3000 && TREE_CODE (cur_type
) == INTEGER_TYPE
3001 && warn_format_signedness
3002 && TYPE_UNSIGNED (wanted_type
)
3003 && cur_param
!= NULL_TREE
3004 && TREE_CODE (cur_param
) == NOP_EXPR
)
3006 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
3007 if (TYPE_UNSIGNED (t
)
3008 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
3011 /* Likewise, "signed char", "unsigned char" and "char" are
3012 equivalent but the above test won't consider them equivalent. */
3013 if (wanted_type
== char_type_node
3014 && (!pedantic
|| i
< 2)
3017 if (types
->scalar_identity_flag
3018 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
3019 || (INTEGRAL_TYPE_P (cur_type
)
3020 && INTEGRAL_TYPE_P (wanted_type
)))
3021 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
3023 /* Now we have a type mismatch. */
3024 format_type_warning (fmt_loc
, param_loc
, types
,
3025 wanted_type
, orig_cur_type
, fki
,
3026 offset_to_type_start
, conversion_char
);
3031 check_kef_type (const substring_loc
&fmt_loc
,
3032 const struct kernel_ext_fmt
*kef
,
3036 const format_kind_info
*fki
,
3037 int offset_to_type_start
,
3038 char conversion_char
,
3039 vec
<location_t
> *arglocs
)
3045 /* The following should not occur here. */
3046 gcc_assert (wanted_type
);
3047 gcc_assert (wanted_type
!= void_type_node
|| kef
->ptrlevel
);
3049 if (TREE_CODE (wanted_type
) == TYPE_DECL
)
3050 wanted_type
= DECL_ORIGINAL_TYPE (wanted_type
);
3055 cur_type
= TREE_TYPE (cur_param
);
3056 if (cur_type
== error_mark_node
)
3059 location_t param_loc
= UNKNOWN_LOCATION
;
3060 if (EXPR_HAS_LOCATION (cur_param
))
3061 param_loc
= EXPR_LOCATION (cur_param
);
3064 /* arg_num is 1-based. */
3065 gcc_assert (arg_num
> 0);
3066 param_loc
= (*arglocs
)[arg_num
- 1];
3070 STRIP_NOPS (cur_param
);
3072 /* Check the types of any additional pointer arguments
3073 that precede the "real" argument. */
3074 for (i
= 0; i
< kef
->ptrlevel
; ++i
)
3076 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
3078 cur_type
= TREE_TYPE (cur_type
);
3079 if (cur_type
== error_mark_node
)
3082 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
3083 cur_param
= TREE_OPERAND (cur_param
, 0);
3087 /* If there are extra type qualifiers beyond the first
3088 indirection, then this makes the types technically
3092 && (TYPE_READONLY (cur_type
)
3093 || TYPE_VOLATILE (cur_type
)
3094 || TYPE_ATOMIC (cur_type
)
3095 || TYPE_RESTRICT (cur_type
)))
3096 warning (OPT_Wformat_
, "extra type qualifiers in format argument (argument %d)",
3107 if (i
< kef
->ptrlevel
)
3110 int compat
= lang_hooks
.types_compatible_p (wanted_type
, cur_type
);
3118 normal_type
= type_normalize (cur_type
, &cousin
, wanted_type
);
3120 return normal_type
== wanted_type
;
3124 /* Given type TYPE, attempt to dereference the type N times
3125 (e.g. from ("int ***", 2) to "int *")
3127 Return the derefenced type, with any qualifiers
3128 such as "const" stripped from the result, or
3129 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3132 deref_n_times (tree type
, int n
)
3136 for (int i
= n
; i
> 0; i
--)
3138 if (TREE_CODE (type
) != POINTER_TYPE
)
3140 type
= TREE_TYPE (type
);
3142 /* Strip off any "const" etc. */
3143 return build_qualified_type (type
, 0);
3146 /* Lookup the format code for FORMAT_LEN within FLI,
3147 returning the string code for expressing it, or NULL
3148 if it is not found. */
3151 get_modifier_for_format_len (const format_length_info
*fli
,
3152 enum format_lengths format_len
)
3154 for (; fli
->name
; fli
++)
3156 if (fli
->index
== format_len
)
3158 if (fli
->double_index
== format_len
)
3159 return fli
->double_name
;
3166 namespace selftest
{
3169 test_get_modifier_for_format_len ()
3172 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
3174 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
3176 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
3178 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
3181 } // namespace selftest
3183 #endif /* CHECKING_P */
3185 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3186 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3187 Wformat type errors where the argument has type ARG_TYPE. */
3190 matching_type_p (tree spec_type
, tree arg_type
)
3192 gcc_assert (spec_type
);
3193 gcc_assert (arg_type
);
3195 spec_type
= decl_deref (spec_type
);
3196 arg_type
= decl_deref (arg_type
);
3198 /* If any of the types requires structural equality, we can't compare
3199 their canonical types. */
3200 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
3201 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
3204 spec_type
= TYPE_CANONICAL (spec_type
);
3205 arg_type
= TYPE_CANONICAL (arg_type
);
3207 if (TREE_CODE (spec_type
) == INTEGER_TYPE
3208 && TREE_CODE (arg_type
) == INTEGER_TYPE
3209 && (TYPE_UNSIGNED (spec_type
)
3210 ? spec_type
== c_common_unsigned_type (arg_type
)
3211 : spec_type
== c_common_signed_type (arg_type
)))
3214 return spec_type
== arg_type
;
3217 /* Subroutine of get_format_for_type.
3219 Generate a string containing the length modifier and conversion specifier
3220 that should be used to format arguments of type ARG_TYPE within FKI
3221 (effectively the inverse of the checking code).
3223 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3224 is required to use it, for correcting bogus length modifiers.
3225 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3226 that matches ARG_TYPE.
3228 If successful, returns a non-NULL string which should be freed
3230 Otherwise, returns NULL. */
3233 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
3234 char conversion_char
)
3236 gcc_assert (arg_type
);
3238 const format_char_info
*spec
;
3239 for (spec
= &fki
->conversion_specs
[0];
3243 if (conversion_char
)
3244 if (!strchr (spec
->format_chars
, conversion_char
))
3247 tree effective_arg_type
= deref_n_times (arg_type
,
3248 spec
->pointer_count
);
3249 if (!effective_arg_type
)
3251 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
3253 const format_type_detail
*ftd
= &spec
->types
[i
];
3256 if (matching_type_p (*ftd
->type
, effective_arg_type
))
3258 const char *len_modifier
3259 = get_modifier_for_format_len (fki
->length_char_specs
,
3260 (enum format_lengths
)i
);
3264 if (conversion_char
)
3265 /* We found a match, using the given conversion char - the
3266 length modifier was incorrect (or absent).
3267 Provide a suggestion using the conversion char with the
3268 correct length modifier for the type. */
3269 return xasprintf ("%s%c", len_modifier
, conversion_char
);
3271 /* 2nd pass: no match was possible using the user-provided
3272 conversion char, but we do have a match without using it.
3273 Provide a suggestion using the first conversion char
3274 listed for the given type. */
3275 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
3283 /* Generate a string containing the length modifier and conversion specifier
3284 that should be used to format arguments of type ARG_TYPE within FKI
3285 (effectively the inverse of the checking code).
3287 If successful, returns a non-NULL string which should be freed
3289 Otherwise, returns NULL. */
3292 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
3293 char conversion_char
)
3295 gcc_assert (arg_type
);
3296 gcc_assert (conversion_char
);
3298 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3299 If we find one, then presumably the length modifier was incorrect
3301 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
3305 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3306 matching just on the type. */
3307 return get_format_for_type_1 (fki
, arg_type
, '\0');
3310 /* Attempt to get a string for use as a replacement fix-it hint for the
3311 source range in FMT_LOC.
3313 Preserve all of the text within the range of FMT_LOC up to
3314 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3315 length modifier and conversion specifier for ARG_TYPE, attempting
3316 to keep the user-provided CONVERSION_CHAR if possible.
3318 For example, given a long vs long long mismatch for arg5 here:
3320 000000000111111111122222222223333333333|
3321 123456789012345678901234567890123456789` column numbers
3322 0000000000111111111122|
3323 0123456789012345678901` string offsets
3324 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3325 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3327 | ` CONVERSION_CHAR: 'd'
3330 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3331 STRING_CST), where the user provided:
3333 the result (assuming "long" argument 5) should be:
3336 If successful, returns a non-NULL string which should be freed
3338 Otherwise, returns NULL. */
3341 get_corrected_substring (const substring_loc
&fmt_loc
,
3342 format_wanted_type
*type
, tree arg_type
,
3343 const format_kind_info
*fki
,
3344 int offset_to_type_start
, char conversion_char
)
3346 /* Attempt to provide hints for argument types, but not for field widths
3350 if (type
->kind
!= CF_KIND_FORMAT
)
3353 /* Locate the current code within the source range, rejecting
3354 any awkward cases where the format string occupies more than
3356 Lookup the place where the type starts (including any length
3357 modifiers), getting it as the caret location. */
3358 substring_loc
type_loc (fmt_loc
);
3359 type_loc
.set_caret_index (offset_to_type_start
);
3361 location_t fmt_substring_loc
;
3362 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
3366 source_range fmt_substring_range
3367 = get_range_from_loc (line_table
, fmt_substring_loc
);
3369 expanded_location caret
3370 = expand_location_to_spelling_point (fmt_substring_loc
);
3371 expanded_location start
3372 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
3373 expanded_location finish
3374 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
3375 if (caret
.file
!= start
.file
)
3377 if (start
.file
!= finish
.file
)
3379 if (caret
.line
!= start
.line
)
3381 if (start
.line
!= finish
.line
)
3383 if (start
.column
> caret
.column
)
3385 if (start
.column
> finish
.column
)
3387 if (caret
.column
> finish
.column
)
3390 #if BUILDING_GCC_VERSION >= 9000
3391 char_span line
= location_get_source_line (start
.file
, start
.line
);
3395 /* If we got this far, then we have the line containing the
3396 existing conversion specification.
3398 Generate a trimmed copy, containing the prefix part of the conversion
3399 specification, up to the (but not including) the length modifier.
3400 In the above example, this would be "%-+*.*". */
3401 int length_up_to_type
= caret
.column
- start
.column
;
3402 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
3403 char *prefix
= prefix_span
.xstrdup ();
3405 char *prefix
= NULL
;
3408 /* Now attempt to generate a suggestion for the rest of the specification
3409 (length modifier and conversion char), based on ARG_TYPE and
3411 In the above example, this would be "ld". */
3412 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
3413 if (!format_for_type
)
3419 /* Success. Generate the resulting suggestion for the whole range of
3420 FMT_LOC by concatenating the two strings.
3421 In the above example, this would be "%-+*.*ld". */
3422 char *result
= concat (prefix
, format_for_type
, NULL
);
3423 free (format_for_type
);
3428 /* Helper class for adding zero or more trailing '*' to types.
3430 The format type and name exclude any '*' for pointers, so those
3431 must be formatted manually. For all the types we currently have,
3432 this is adequate, but formats taking pointers to functions or
3433 arrays would require the full type to be built up in order to
3434 print it with %T. */
3436 class indirection_suffix
3439 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
3441 /* Determine the size of the buffer (including NUL-terminator). */
3443 size_t get_buffer_size () const
3445 return m_pointer_count
+ 2;
3448 /* Write the '*' to DST and add a NUL-terminator. */
3450 void fill_buffer (char *dst
) const
3452 if (m_pointer_count
== 0)
3454 else if (c_dialect_cxx ())
3456 memset (dst
, '*', m_pointer_count
);
3457 dst
[m_pointer_count
] = 0;
3462 memset (dst
+ 1, '*', m_pointer_count
);
3463 dst
[m_pointer_count
+ 1] = 0;
3468 int m_pointer_count
;
3471 #if BUILDING_GCC_VERSION >= 9000
3472 /* not exported by GCC... need a local copy :( */
3473 class frr_range_label_for_type_mismatch
: public range_label
3476 frr_range_label_for_type_mismatch (tree labelled_type
, tree other_type
)
3477 : m_labelled_type (labelled_type
), m_other_type (other_type
)
3481 label_text
get_text (unsigned range_idx
) const OVERRIDE
;
3484 tree m_labelled_type
;
3488 /* Print T to CPP. */
3491 print_type (c_pretty_printer
*cpp
, tree t
, bool *quoted
)
3493 gcc_assert (TYPE_P (t
));
3494 struct obstack
*ob
= pp_buffer (cpp
)->obstack
;
3495 char *p
= (char *) obstack_base (ob
);
3496 /* Remember the end of the initial dump. */
3497 int len
= obstack_object_size (ob
);
3499 tree name
= TYPE_NAME (t
);
3500 if (name
&& TREE_CODE (name
) == TYPE_DECL
&& DECL_NAME (name
))
3501 pp_identifier (cpp
, lang_hooks
.decl_printable_name (name
, 2));
3505 /* If we're printing a type that involves typedefs, also print the
3506 stripped version. But sometimes the stripped version looks
3507 exactly the same, so we don't want it after all. To avoid
3508 printing it in that case, we play ugly obstack games. */
3509 if (TYPE_CANONICAL (t
) && t
!= TYPE_CANONICAL (t
))
3511 c_pretty_printer cpp2
;
3512 /* Print the stripped version into a temporary printer. */
3513 cpp2
.type_id (TYPE_CANONICAL (t
));
3514 struct obstack
*ob2
= cpp2
.buffer
->obstack
;
3515 /* Get the stripped version from the temporary printer. */
3516 const char *aka
= (char *) obstack_base (ob2
);
3517 int aka_len
= obstack_object_size (ob2
);
3518 int type1_len
= obstack_object_size (ob
) - len
;
3520 /* If they are identical, bail out. */
3521 if (aka_len
== type1_len
&& memcmp (p
+ len
, aka
, aka_len
) == 0)
3524 /* They're not, print the stripped version now. */
3526 pp_end_quote (cpp
, pp_show_color (cpp
));
3527 pp_c_whitespace (cpp
);
3528 pp_left_brace (cpp
);
3529 pp_c_ws_string (cpp
, _("aka"));
3530 pp_c_whitespace (cpp
);
3532 pp_begin_quote (cpp
, pp_show_color (cpp
));
3533 cpp
->type_id (TYPE_CANONICAL (t
));
3535 pp_end_quote (cpp
, pp_show_color (cpp
));
3536 pp_right_brace (cpp
);
3537 /* No further closing quotes are needed. */
3542 /* C-specific implementation of range_label::get_text () vfunc for
3543 range_label_for_type_mismatch. */
3544 #if BUILDING_GCC_VERSION >= 10000
3545 #define label_borrow(text) label_text::borrow(text)
3546 #define label_take(text) label_text::take(text)
3548 #define label_borrow(text) label_text((char *)text, false)
3549 #define label_take(text) label_text(text, true)
3553 frr_range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
3555 if (m_labelled_type
== NULL_TREE
)
3556 return label_borrow("(null tree)");
3558 c_pretty_printer cpp
;
3559 bool quoted
= false;
3560 print_type (&cpp
, m_labelled_type
, "ed
);
3561 return label_take(xstrdup (pp_formatted_text (&cpp
)));
3564 #define range_label_for_type_mismatch frr_range_label_for_type_mismatch
3567 /* Subclass of range_label for labelling the range in the format string
3568 with the type in question, adding trailing '*' for pointer_count. */
3570 class range_label_for_format_type_mismatch
3571 : public range_label_for_type_mismatch
3574 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
3576 : range_label_for_type_mismatch (labelled_type
, other_type
),
3577 m_pointer_count (pointer_count
)
3581 label_text
get_text (unsigned range_idx
) const FINAL OVERRIDE
3583 label_text text
= range_label_for_type_mismatch::get_text (range_idx
);
3584 if (text
.m_buffer
== NULL
)
3587 indirection_suffix
suffix (m_pointer_count
);
3588 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3589 suffix
.fill_buffer (p
);
3591 char *result
= concat (text
.m_buffer
, p
, NULL
);
3593 return label_take(result
);
3597 int m_pointer_count
;
3600 /* Give a warning about a format argument of different type from that expected.
3601 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3602 is based on the location of the char at TYPE->offset_loc.
3603 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3604 if this is unavailable.
3605 WANTED_TYPE is the type the argument should have,
3606 possibly stripped of pointer dereferences. The description (such as "field
3607 precision"), the placement in the format string, a possibly more
3608 friendly name of WANTED_TYPE, and the number of pointer dereferences
3609 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3610 or NULL if it is missing.
3612 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3613 format string to where type information begins for the conversion
3614 (the length modifier and conversion specifier).
3615 CONVERSION_CHAR is the user-provided conversion specifier.
3617 For example, given a type mismatch for argument 5 here:
3619 00000000011111111112222222222333333333344444444445555555555|
3620 12345678901234567890123456789012345678901234567890123456789` column numbers
3621 0000000000111111111122|
3622 0123456789012345678901` offsets within STRING_CST
3623 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3624 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3626 | ` CONVERSION_CHAR: 'd' PARAM_LOC
3629 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3633 format_type_warning (const substring_loc
&whole_fmt_loc
,
3634 location_t param_loc
,
3635 format_wanted_type
*type
,
3636 tree wanted_type
, tree arg_type
,
3637 const format_kind_info
*fki
,
3638 int offset_to_type_start
,
3639 char conversion_char
,
3642 enum format_specifier_kind kind
= type
->kind
;
3643 const char *wanted_type_name
= type
->wanted_type_name
;
3644 const char *format_start
= type
->format_start
;
3645 int format_length
= type
->format_length
;
3646 int pointer_count
= type
->pointer_count
;
3647 int arg_num
= type
->arg_num
;
3652 /* If ARG_TYPE is a typedef with a misleading name (for example,
3653 size_t but not the standard size_t expected by printf %zu), avoid
3654 printing the typedef name. */
3655 if (wanted_type_name
3657 && TYPE_NAME (arg_type
)
3658 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
3659 && DECL_NAME (TYPE_NAME (arg_type
))
3660 && !strcmp (wanted_type_name
,
3661 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
3662 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
3664 indirection_suffix
suffix (pointer_count
);
3665 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3666 suffix
.fill_buffer (p
);
3668 /* WHOLE_FMT_LOC has the caret at the end of the range.
3669 Set the caret to be at the offset from TYPE. Subtract one
3670 from the offset for the same reason as in format_warning_at_char. */
3671 substring_loc
fmt_loc (whole_fmt_loc
);
3672 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
3674 #if BUILDING_GCC_VERSION >= 9000
3675 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
3677 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
3679 /* Get a string for use as a replacement fix-it hint for the range in
3680 fmt_loc, or NULL. */
3681 char *corrected_substring
3682 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3683 offset_to_type_start
, conversion_char
);
3684 format_string_diagnostic_t
diag (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3685 corrected_substring
);
3686 # define format_warning_at_substring(a,b,c,d,e,...) \
3687 diag.emit_warning(__VA_ARGS__)
3689 # define format_warning_at_substring(a,b,c,d,...) \
3690 format_warning_at_substring(a,c,__VA_ARGS__)
3691 /* Get a string for use as a replacement fix-it hint for the range in
3692 fmt_loc, or NULL. */
3693 char *corrected_substring
3694 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3695 offset_to_type_start
, conversion_char
);
3699 if (wanted_type_name
)
3702 format_warning_at_substring
3703 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3704 corrected_substring
, OPT_Wformat_
,
3705 "%s %<%s%.*s%> expects argument of type %<%s%s%>, but argument %d has type %qT%s",
3706 gettext (kind_descriptions
[kind
]),
3707 (kind
== CF_KIND_FORMAT
? "%" : ""),
3708 format_length
, format_start
,
3709 wanted_type_name
, p
, arg_num
, arg_type
, extra
);
3711 format_warning_at_substring
3712 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3713 corrected_substring
, OPT_Wformat_
,
3714 "%s %<%s%.*s%> expects a matching %<%s%s%> argument%s",
3715 gettext (kind_descriptions
[kind
]),
3716 (kind
== CF_KIND_FORMAT
? "%" : ""),
3717 format_length
, format_start
, wanted_type_name
, p
, extra
);
3722 format_warning_at_substring
3723 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3724 corrected_substring
, OPT_Wformat_
,
3725 "%s %<%s%.*s%> expects argument of type %<%T%s%>, but argument %d has type %qT%s",
3726 gettext (kind_descriptions
[kind
]),
3727 (kind
== CF_KIND_FORMAT
? "%" : ""),
3728 format_length
, format_start
,
3729 wanted_type
, p
, arg_num
, arg_type
, extra
);
3731 format_warning_at_substring
3732 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3733 corrected_substring
, OPT_Wformat_
,
3734 "%s %<%s%.*s%> expects a matching %<%T%s%> argument%s",
3735 gettext (kind_descriptions
[kind
]),
3736 (kind
== CF_KIND_FORMAT
? "%" : ""),
3737 format_length
, format_start
, wanted_type
, p
, extra
);
3740 free (corrected_substring
);
3745 /* Given a format_char_info array FCI, and a character C, this function
3746 returns the index into the conversion_specs where that specifier's
3747 data is located. The character must exist. */
3749 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
3753 for (i
= 0; fci
->format_chars
; i
++, fci
++)
3754 if (strchr (fci
->format_chars
, c
))
3757 /* We shouldn't be looking for a non-existent specifier. */
3761 /* Given a format_length_info array FLI, and a character C, this
3762 function returns the index into the conversion_specs where that
3763 modifier's data is located. The character must exist. */
3765 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
3769 for (i
= 0; fli
->name
; i
++, fli
++)
3770 if (strchr (fli
->name
, c
))
3773 /* We shouldn't be looking for a non-existent modifier. */
3778 #ifdef TARGET_FORMAT_TYPES
3779 extern const format_kind_info TARGET_FORMAT_TYPES
[];
3782 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3783 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
3785 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3786 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3789 /* Attributes such as "printf" are equivalent to those such as
3790 "gnu_printf" unless this is overridden by a target. */
3791 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
3796 /* Translate to unified attribute name. This is used in decode_format_type and
3797 decode_format_attr. In attr_name the user specified argument is passed. It
3798 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3799 or the attr_name passed to this function, if there is no matching entry. */
3801 convert_format_name_to_system_name (const char *attr_name
)
3805 if (attr_name
== NULL
|| *attr_name
== 0
3806 || strncmp (attr_name
, "gcc_", 4) == 0)
3808 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3809 TARGET_OVERRIDES_FORMAT_INIT ();
3812 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3813 /* Check if format attribute is overridden by target. */
3814 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
3815 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
3817 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
3819 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
3822 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
3824 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
3828 /* Otherwise default to gnu format. */
3830 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
3833 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
3836 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
3838 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
3844 /* Handle a "format" attribute; arguments as in
3845 struct attribute_spec.handler. */
3847 handle_frr_format_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
3848 int flags
, bool *no_add_attrs
)
3851 function_format_info info
;
3853 /* Canonicalize name of format function. */
3854 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
3855 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
3857 if (!decode_format_attr (args
, &info
, 0))
3859 *no_add_attrs
= true;
3863 if (prototype_p (type
))
3865 if (!check_format_string (type
, info
.format_num
, flags
,
3866 no_add_attrs
, info
.format_type
))
3869 if (info
.first_arg_num
!= 0)
3871 unsigned HOST_WIDE_INT arg_num
= 1;
3872 function_args_iterator iter
;
3875 /* Verify that first_arg_num points to the last arg,
3877 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
3880 if (arg_num
!= info
.first_arg_num
)
3882 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
3883 error ("arguments to be formatted is not %<...%>");
3884 *no_add_attrs
= true;
3890 /* Check if this is a strftime variant. Just for this variant
3891 FMT_FLAG_ARG_CONVERT is not set. */
3892 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
3893 && info
.first_arg_num
!= 0)
3895 error ("strftime formats cannot format arguments");
3896 *no_add_attrs
= true;
3905 namespace selftest
{
3907 /* Selftests of location handling. */
3909 /* Get the format_kind_info with the given name. */
3911 static const format_kind_info
*
3912 get_info (const char *name
)
3914 int idx
= decode_format_type (name
);
3915 const format_kind_info
*fki
= &format_types
[idx
];
3916 ASSERT_STREQ (fki
->name
, name
);
3920 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
3921 is EXPECTED_FORMAT. */
3924 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
3925 const char *expected_format
, tree type
,
3926 char conversion_char
)
3929 gcc_assert (expected_format
);
3932 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
3933 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
3934 free (actual_format
);
3937 /* Selftests for get_format_for_type. */
3939 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
3940 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
3941 (TYPE), (CONVERSION_CHAR))
3943 /* Selftest for get_format_for_type for "printf"-style functions. */
3946 test_get_format_for_type_printf ()
3948 const format_kind_info
*fki
= get_info ("gnu_printf");
3949 ASSERT_NE (fki
, NULL
);
3951 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
3952 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
3953 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
3954 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
3955 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
3956 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
3957 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
3958 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
3959 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
3960 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
3961 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
3962 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
3963 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
3964 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
3965 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
3966 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
3967 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
3968 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
3969 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
3970 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
3971 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
3972 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
3973 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
3974 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
3975 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
3976 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
3977 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
3978 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
3981 /* Selftest for get_format_for_type for "scanf"-style functions. */
3984 test_get_format_for_type_scanf ()
3986 const format_kind_info
*fki
= get_info ("gnu_scanf");
3987 ASSERT_NE (fki
, NULL
);
3988 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
3989 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
3990 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
3991 build_pointer_type (long_integer_type_node
), 'd');
3992 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
3993 build_pointer_type (long_unsigned_type_node
), 'u');
3994 ASSERT_FORMAT_FOR_TYPE_STREQ
3995 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
3996 ASSERT_FORMAT_FOR_TYPE_STREQ
3997 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
3998 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
3999 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
4002 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4004 /* Exercise the type-printing label code, to give some coverage
4005 under "make selftest-valgrind" (in particular, to ensure that
4006 the label-printing machinery doesn't leak). */
4009 test_type_mismatch_range_labels ()
4011 /* Create a tempfile and write some text to it.
4012 ....................0000000001 11111111 12 22222222
4013 ....................1234567890 12345678 90 12345678. */
4014 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
4015 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
4016 line_table_test ltt
;
4018 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
4020 location_t c17
= linemap_position_for_column (line_table
, 17);
4021 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
4022 location_t c18
= linemap_position_for_column (line_table
, 18);
4023 location_t c24
= linemap_position_for_column (line_table
, 24);
4024 location_t c26
= linemap_position_for_column (line_table
, 26);
4026 /* Don't attempt to run the tests if column data might be unavailable. */
4027 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
4030 location_t fmt
= make_location (c18
, c17
, c18
);
4031 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
4033 location_t param
= make_location (c24
, c24
, c26
);
4034 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
4036 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
4037 integer_type_node
, 1);
4038 range_label_for_type_mismatch
param_label (integer_type_node
,
4040 gcc_rich_location
richloc (fmt
, &fmt_label
);
4041 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
4043 test_diagnostic_context dc
;
4044 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
4045 if (c_dialect_cxx ())
4046 /* "char*", without a space. */
4048 " printf (\"msg: %i\\n\", msg);\n"
4052 pp_formatted_text (dc
.printer
));
4054 /* "char *", with a space. */
4056 " printf (\"msg: %i\\n\", msg);\n"
4061 pp_formatted_text (dc
.printer
));
4064 /* Run all of the selftests within this file. */
4069 test_get_modifier_for_format_len ();
4070 test_get_format_for_type_printf ();
4071 test_get_format_for_type_scanf ();
4072 test_type_mismatch_range_labels ();
4075 } // namespace selftest
4077 #endif /* CHECKING_P */
4079 // include "gt-c-family-c-format.h"
4081 static const struct attribute_spec frr_format_attribute_table
[] =
4083 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4084 affects_type_identity, handler, exclude } */
4085 { "frr_format", 3, 3, false, true, true, false,
4086 handle_frr_format_attribute
, NULL
},
4087 { "frr_format_arg", 1, 1, false, true, true, false,
4088 handle_frr_format_arg_attribute
, NULL
},
4089 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
4093 register_attributes (void *event_data
, void *data
)
4095 // warning (0, G_("Callback to register attributes"));
4096 register_attribute (frr_format_attribute_table
);
4100 cb_walk_tree_fn (tree
* tp
, int * walk_subtrees
, void * data ATTRIBUTE_UNUSED
)
4102 if (TREE_CODE (*tp
) != CALL_EXPR
)
4105 tree call_expr
= *tp
;
4107 int nargs
= call_expr_nargs(call_expr
);
4108 tree fn
= CALL_EXPR_FN(call_expr
);
4110 if (!fn
|| TREE_CODE (fn
) != ADDR_EXPR
)
4113 tree fndecl
= TREE_OPERAND (fn
, 0);
4114 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
4118 warning (0, G_("function call to %s, %d args"),
4119 IDENTIFIER_POINTER (DECL_NAME (fndecl
)),
4123 tree
*fargs
= (tree
*) alloca (nargs
* sizeof (tree
));
4125 for (int j
= 0; j
< nargs
; j
++)
4127 tree arg
= CALL_EXPR_ARG(call_expr
, j
);
4129 /* For -Wformat undo the implicit passing by hidden reference
4130 done by convert_arg_to_ellipsis. */
4131 if (TREE_CODE (arg
) == ADDR_EXPR
4132 && TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4133 fargs
[j
] = TREE_OPERAND (arg
, 0);
4138 check_function_format (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)), nargs
, fargs
, NULL
);
4143 setup_type (const char *name
, tree
*dst
)
4147 if (*dst
&& *dst
!= void_type_node
)
4150 *dst
= maybe_get_identifier (name
);
4154 tmp
= identifier_global_value (*dst
);
4155 if (tmp
&& TREE_CODE (tmp
) != TYPE_DECL
)
4157 warning (0, "%qs is not defined as a type", name
);
4161 if (tmp
&& TREE_CODE (tmp
) == TYPE_DECL
)
4168 handle_finish_parse (void *event_data
, void *data
)
4170 tree fndecl
= (tree
) event_data
;
4171 gcc_assert (TREE_CODE (fndecl
) == FUNCTION_DECL
);
4173 setup_type ("uint64_t", &local_uint64_t_node
);
4174 setup_type ("int64_t", &local_int64_t_node
);
4176 setup_type ("size_t", &local_size_t_node
);
4177 setup_type ("ssize_t", &local_ssize_t_node
);
4178 setup_type ("atomic_size_t", &local_atomic_size_t_node
);
4179 setup_type ("atomic_ssize_t", &local_atomic_ssize_t_node
);
4180 setup_type ("ptrdiff_t", &local_ptrdiff_t_node
);
4182 setup_type ("pid_t", &local_pid_t_node
);
4183 setup_type ("uid_t", &local_uid_t_node
);
4184 setup_type ("gid_t", &local_gid_t_node
);
4185 setup_type ("time_t", &local_time_t_node
);
4187 setup_type ("socklen_t", &local_socklen_t_node
);
4188 setup_type ("in_addr_t", &local_in_addr_t_node
);
4190 const format_char_info
*fci
;
4192 for (fci
= print_char_table
; fci
->format_chars
; fci
++)
4194 if (!fci
->kernel_ext
)
4197 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
4198 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
4200 for (; etab
->suffix
&& etab
< etab_end
; etab
++)
4202 tree identifier
, node
;
4204 if (etab
->type
&& etab
->type
!= void_type_node
)
4207 identifier
= maybe_get_identifier (etab
->type_str
);
4209 if (!identifier
|| identifier
== error_mark_node
)
4212 if (etab
->type_code
)
4214 node
= identifier_global_tag (identifier
);
4218 if (node
->base
.code
!= etab
->type_code
)
4222 warning (0, "%qs tag category (struct/union/enum) mismatch", etab
->type_str
);
4223 etab
->warned
= true;
4230 node
= identifier_global_value (identifier
);
4234 if (TREE_CODE (node
) != TYPE_DECL
)
4238 warning (0, "%qs is defined as a non-type", etab
->type_str
);
4239 etab
->warned
= true;
4243 node
= TREE_TYPE (node
);
4245 if (etab
->t_unsigned
)
4246 node
= c_common_unsigned_type (node
);
4247 else if (etab
->t_signed
)
4248 node
= c_common_signed_type (node
);
4255 walk_tree (&DECL_SAVED_TREE (fndecl
), cb_walk_tree_fn
, NULL
, NULL
);
4259 handle_pragma_printfrr_ext (cpp_reader
*dummy
)
4263 enum cpp_ttype ttype
;
4265 ttype
= pragma_lex (&token
, &loc
);
4266 if (ttype
!= CPP_STRING
)
4268 error_at (loc
, "%<#pragma FRR printfrr_ext%> requires string argument");
4272 const char *s
= TREE_STRING_POINTER (token
);
4276 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, needs to start with '%%'");
4287 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, needs to be %%p, %%d or %%i");
4291 const format_char_info
*fci
;
4293 for (fci
= print_char_table
; fci
->format_chars
; fci
++)
4294 if (strchr (fci
->format_chars
, s
[1]))
4297 gcc_assert (fci
->format_chars
);
4298 gcc_assert (fci
->kernel_ext
);
4300 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
4301 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
4309 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, suffix must start with an uppercase letter");
4313 /* -2 -- need to keep the sentinel at the end */
4314 if (etab
[ETAB_SZ
- 2].suffix
)
4316 error_at (loc
, "%<#pragma FRR printfrr_ext%>: out of space for format suffixes");
4320 for (; etab
->suffix
&& etab
< etab_end
; etab
++)
4322 if (!strcmp(s
+ 2, etab
->suffix
))
4324 memmove (etab
+ 1, etab
, (etab_end
- etab
- 1) * sizeof (*etab
));
4328 warning_at (loc
, OPT_Wformat_
,
4329 "%<#pragma FRR printfrr_ext%>: duplicate printf format suffix %qs", s
);
4330 warning_at (etab
->origin_loc
, OPT_Wformat_
,
4331 "%<#pragma FRR printfrr_ext%>: previous definition was here");
4338 if (!strncmp(s
+ 2, etab
->suffix
, MIN(strlen(s
+ 2), strlen(etab
->suffix
))))
4340 warning_at (loc
, OPT_Wformat_
,
4341 "%<#pragma FRR printfrr_ext%>: overlapping printf format suffix %qs", s
);
4342 warning_at (etab
->origin_loc
, OPT_Wformat_
,
4343 "%<#pragma FRR printfrr_ext%>: previous definition for %<%%%c%s%> was here", s
[1], etab
->suffix
);
4348 gcc_assert (etab
< etab_end
);
4350 memset (etab
, 0, sizeof (*etab
));
4351 etab
->suffix
= xstrdup(s
+ 2);
4352 etab
->origin_loc
= loc
;
4353 etab
->type
= void_type_node
;
4355 ttype
= pragma_lex (&token
, &loc
);
4356 if (ttype
!= CPP_OPEN_PAREN
)
4358 error_at (loc
, "%<#pragma FRR printfrr_ext%> expected %<(%>");
4362 ttype
= pragma_lex (&token
, &loc
);
4365 while (ttype
== CPP_NAME
)
4367 if (!strcmp (IDENTIFIER_POINTER (token
), "const"))
4368 etab
->t_const
= true;
4369 else if (!strcmp (IDENTIFIER_POINTER (token
), "signed"))
4370 etab
->t_signed
= true;
4371 else if (!strcmp (IDENTIFIER_POINTER (token
), "unsigned"))
4372 etab
->t_unsigned
= true;
4376 ttype
= pragma_lex (&token
, &loc
);
4380 if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "struct"))
4382 etab
->type_code
= RECORD_TYPE
;
4383 ttype
= pragma_lex (&token
, &loc
);
4385 else if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "union"))
4387 etab
->type_code
= UNION_TYPE
;
4388 ttype
= pragma_lex (&token
, &loc
);
4390 else if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "enum"))
4392 etab
->type_code
= ENUMERAL_TYPE
;
4393 ttype
= pragma_lex (&token
, &loc
);
4397 if (ttype
!= CPP_NAME
)
4399 error_at (loc
, "%<#pragma FRR printfrr_ext%>: expected typename identifier");
4403 etab
->type_str
= xstrdup (IDENTIFIER_POINTER (token
));
4405 while ((ttype
= pragma_lex (&token
, &loc
)) != CPP_CLOSE_PAREN
)
4409 error_at (loc
, "%<#pragma FRR printfrr_ext%>: unexpected identifier. Note the only supported qualifier is %<const%>");
4417 error_at (loc
, "%<#pragma FRR printfrr_ext%>: premature end of line, missing %<)%>");
4421 error_at (loc
, "%<#pragma FRR printfrr_ext%>: unsupported token");
4426 ttype
= pragma_lex (&token
, &loc
);
4427 if (ttype
!= CPP_EOF
)
4428 warning_at (loc
, OPT_Wformat_
,
4429 "%<#pragma FRR printfrr_ext%>: garbage at end of line");
4434 memset (etab
, 0, sizeof (*etab
));
4438 register_pragma_printfrr_ext (void *event_data
, void *data
)
4440 c_register_pragma_with_expansion ("FRR", "printfrr_ext", handle_pragma_printfrr_ext
);
4444 define_vars (void *gcc_data
, void *user_data
)
4446 cpp_define (parse_in
, "_FRR_ATTRIBUTE_PRINTFRR=0x10000");
4450 #define __visible __attribute__((visibility("default")))
4453 __visible
int plugin_is_GPL_compatible
;
4456 plugin_init (struct plugin_name_args
*plugin_info
,
4457 struct plugin_gcc_version
*version
)
4459 const char *plugin_name
= plugin_info
->base_name
;
4461 if (!plugin_default_version_check(version
, &gcc_version
))
4463 error(G_("incompatible gcc/plugin versions"));
4467 memset (ext_p
, 0, sizeof (ext_p
));
4468 memset (ext_d
, 0, sizeof (ext_d
));
4470 register_callback (plugin_name
, PLUGIN_FINISH_PARSE_FUNCTION
, handle_finish_parse
, NULL
);
4471 register_callback (plugin_name
, PLUGIN_ATTRIBUTES
, register_attributes
, NULL
);
4472 register_callback (plugin_name
, PLUGIN_START_UNIT
, define_vars
, NULL
);
4473 register_callback (plugin_name
, PLUGIN_PRAGMAS
, register_pragma_printfrr_ext
, NULL
);