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.
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
));
2733 check_format_types (const substring_loc
&fmt_loc
,
2734 format_wanted_type
*types
, const format_kind_info
*fki
,
2735 int offset_to_type_start
,
2736 char conversion_char
,
2737 vec
<location_t
> *arglocs
)
2739 for (; types
!= 0; types
= types
->next
)
2743 tree cur_type_cousin
= NULL
;
2750 wanted_type
= types
->wanted_type
;
2751 arg_num
= types
->arg_num
;
2753 /* The following should not occur here. */
2754 gcc_assert (wanted_type
);
2755 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
2757 if (types
->pointer_count
== 0)
2758 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
2760 switch (TREE_CODE (wanted_type
))
2762 case IDENTIFIER_NODE
:
2765 wanted_type
= TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (wanted_type
));
2768 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
2772 cur_param
= types
->param
;
2775 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
2776 NULL
, fki
, offset_to_type_start
,
2781 cur_type
= TREE_TYPE (cur_param
);
2782 if (cur_type
== error_mark_node
)
2784 orig_cur_type
= cur_type
;
2787 location_t param_loc
= UNKNOWN_LOCATION
;
2788 if (EXPR_HAS_LOCATION (cur_param
))
2789 param_loc
= EXPR_LOCATION (cur_param
);
2792 /* arg_num is 1-based. */
2793 gcc_assert (types
->arg_num
> 0);
2794 param_loc
= (*arglocs
)[types
->arg_num
- 1];
2797 STRIP_NOPS (cur_param
);
2799 /* Check the types of any additional pointer arguments
2800 that precede the "real" argument. */
2801 for (i
= 0; i
< types
->pointer_count
; ++i
)
2803 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
2805 cur_type
= TREE_TYPE (cur_type
);
2806 if (cur_type
== error_mark_node
)
2809 /* Check for writing through a NULL pointer. */
2810 if (types
->writing_in_flag
2813 && integer_zerop (cur_param
))
2814 warning (OPT_Wformat_
, "writing through null pointer (argument %d)", arg_num
);
2816 /* Check for reading through a NULL pointer. */
2817 if (types
->reading_from_flag
2820 && integer_zerop (cur_param
))
2821 warning (OPT_Wformat_
, "reading through null pointer (argument %d)", arg_num
);
2823 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
2824 cur_param
= TREE_OPERAND (cur_param
, 0);
2828 /* See if this is an attempt to write into a const type with
2829 scanf or with printf "%n". Note: the writing in happens
2830 at the first indirection only, if for example
2831 void * const * is passed to scanf %p; passing
2832 const void ** is simply passing an incompatible type. */
2833 if (types
->writing_in_flag
2835 && (TYPE_READONLY (cur_type
)
2837 && (CONSTANT_CLASS_P (cur_param
)
2838 || (DECL_P (cur_param
)
2839 && TREE_READONLY (cur_param
))))))
2840 warning (OPT_Wformat_
, "writing into constant object (argument %d)", arg_num
);
2842 /* If there are extra type qualifiers beyond the first
2843 indirection, then this makes the types technically
2847 && (TYPE_READONLY (cur_type
)
2848 || TYPE_VOLATILE (cur_type
)
2849 || TYPE_ATOMIC (cur_type
)
2850 || TYPE_RESTRICT (cur_type
)))
2851 warning (OPT_Wformat_
, "extra type qualifiers in format argument (argument %d)",
2857 format_type_warning (fmt_loc
, param_loc
,
2858 types
, wanted_type
, orig_cur_type
, fki
,
2859 offset_to_type_start
, conversion_char
);
2864 if (i
< types
->pointer_count
)
2867 cur_type
= type_normalize (cur_type
, &cur_type_cousin
);
2869 /* Check whether the argument type is a character type. This leniency
2870 only applies to certain formats, flagged with 'c'. */
2871 if (types
->char_lenient_flag
)
2872 char_type_flag
= (cur_type
== char_type_node
2873 || cur_type
== signed_char_type_node
2874 || cur_type
== unsigned_char_type_node
);
2876 int compat
= lang_hooks
.types_compatible_p (wanted_type
, cur_type
);
2877 /* Check the type of the "real" argument, if there's a type we want. */
2878 if ((TREE_CODE (wanted_type
) != INTEGER_TYPE
|| types
->pointer_count
)
2881 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
&& !types
->pointer_count
2885 if (TREE_CODE (cur_param
) == INTEGER_CST
)
2888 if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
2889 && TREE_CODE (cur_type
) == TYPE_DECL
)
2891 if (types
->wanted_type
== cur_type
)
2893 format_type_warning (fmt_loc
, param_loc
, types
,
2894 wanted_type
, orig_cur_type
, fki
,
2895 offset_to_type_start
, conversion_char
,
2896 " (strict match required [A])");
2899 else if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
)
2901 if (types
->wanted_type
== TYPE_NAME(cur_type
))
2903 format_type_warning (fmt_loc
, param_loc
, types
,
2904 wanted_type
, orig_cur_type
, fki
,
2905 offset_to_type_start
, conversion_char
,
2906 " (strict match required [B])");
2909 else if (wanted_type
== cur_type
)
2911 else if (cur_type_cousin
)
2913 format_type_warning (fmt_loc
, param_loc
, types
,
2914 wanted_type
, orig_cur_type
, fki
,
2915 offset_to_type_start
, conversion_char
,
2916 " (strict match required [C])");
2920 format_type_warning (fmt_loc, param_loc, types,
2921 wanted_type, orig_cur_type, fki,
2922 offset_to_type_start, conversion_char,
2923 " (ultra-pedantic mode)");
2928 /* If we want 'void *', allow any pointer type.
2929 (Anything else would already have got a warning.)
2930 With -Wpedantic, only allow pointers to void and to character
2932 if (wanted_type
== void_type_node
2933 && (!pedantic
|| (i
== 1 && char_type_flag
)))
2935 /* Don't warn about differences merely in signedness, unless
2936 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2937 target and not a character type, and for character types at
2938 a second level of indirection. */
2939 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2940 && TREE_CODE (cur_type
) == INTEGER_TYPE
2941 && ((!pedantic
&& !warn_format_signedness
)
2942 || (i
== 0 && !warn_format_signedness
)
2943 || (i
== 1 && char_type_flag
))
2944 && (TYPE_UNSIGNED (wanted_type
)
2945 ? wanted_type
== c_common_unsigned_type (cur_type
)
2946 : wanted_type
== c_common_signed_type (cur_type
)))
2948 if (cur_type_cousin
)
2950 if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
2951 && TREE_CODE (cur_type_cousin
) == TYPE_DECL
)
2953 if (types
->wanted_type
== cur_type_cousin
)
2955 format_type_warning (fmt_loc
, param_loc
, types
,
2956 wanted_type
, orig_cur_type
, fki
,
2957 offset_to_type_start
, conversion_char
,
2958 " (strict match required [X])");
2961 else if (TREE_CODE (types
->wanted_type
) == TYPE_DECL
)
2963 if (types
->wanted_type
== TYPE_NAME(cur_type_cousin
))
2965 format_type_warning (fmt_loc
, param_loc
, types
,
2966 wanted_type
, orig_cur_type
, fki
,
2967 offset_to_type_start
, conversion_char
,
2968 " (strict match required [Y])");
2971 else if (wanted_type
== cur_type_cousin
)
2975 format_type_warning (fmt_loc
, param_loc
, types
,
2976 wanted_type
, orig_cur_type
, fki
,
2977 offset_to_type_start
, conversion_char
,
2978 " (strict match required [Z])");
2984 /* Don't warn about differences merely in signedness if we know
2985 that the current type is integer-promoted and its original type
2986 was unsigned such as that it is in the range of WANTED_TYPE. */
2987 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
2988 && TREE_CODE (cur_type
) == INTEGER_TYPE
2989 && warn_format_signedness
2990 && TYPE_UNSIGNED (wanted_type
)
2991 && cur_param
!= NULL_TREE
2992 && TREE_CODE (cur_param
) == NOP_EXPR
)
2994 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
2995 if (TYPE_UNSIGNED (t
)
2996 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
2999 /* Likewise, "signed char", "unsigned char" and "char" are
3000 equivalent but the above test won't consider them equivalent. */
3001 if (wanted_type
== char_type_node
3002 && (!pedantic
|| i
< 2)
3005 if (types
->scalar_identity_flag
3006 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
3007 || (INTEGRAL_TYPE_P (cur_type
)
3008 && INTEGRAL_TYPE_P (wanted_type
)))
3009 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
3011 /* Now we have a type mismatch. */
3012 format_type_warning (fmt_loc
, param_loc
, types
,
3013 wanted_type
, orig_cur_type
, fki
,
3014 offset_to_type_start
, conversion_char
);
3019 check_kef_type (const substring_loc
&fmt_loc
,
3020 const struct kernel_ext_fmt
*kef
,
3024 const format_kind_info
*fki
,
3025 int offset_to_type_start
,
3026 char conversion_char
,
3027 vec
<location_t
> *arglocs
)
3033 /* The following should not occur here. */
3034 gcc_assert (wanted_type
);
3035 gcc_assert (wanted_type
!= void_type_node
|| kef
->ptrlevel
);
3037 if (TREE_CODE (wanted_type
) == TYPE_DECL
)
3038 wanted_type
= DECL_ORIGINAL_TYPE (wanted_type
);
3043 cur_type
= TREE_TYPE (cur_param
);
3044 if (cur_type
== error_mark_node
)
3047 location_t param_loc
= UNKNOWN_LOCATION
;
3048 if (EXPR_HAS_LOCATION (cur_param
))
3049 param_loc
= EXPR_LOCATION (cur_param
);
3052 /* arg_num is 1-based. */
3053 gcc_assert (arg_num
> 0);
3054 param_loc
= (*arglocs
)[arg_num
- 1];
3058 STRIP_NOPS (cur_param
);
3060 /* Check the types of any additional pointer arguments
3061 that precede the "real" argument. */
3062 for (i
= 0; i
< kef
->ptrlevel
; ++i
)
3064 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
3066 cur_type
= TREE_TYPE (cur_type
);
3067 if (cur_type
== error_mark_node
)
3070 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
3071 cur_param
= TREE_OPERAND (cur_param
, 0);
3075 /* If there are extra type qualifiers beyond the first
3076 indirection, then this makes the types technically
3080 && (TYPE_READONLY (cur_type
)
3081 || TYPE_VOLATILE (cur_type
)
3082 || TYPE_ATOMIC (cur_type
)
3083 || TYPE_RESTRICT (cur_type
)))
3084 warning (OPT_Wformat_
, "extra type qualifiers in format argument (argument %d)",
3095 if (i
< kef
->ptrlevel
)
3098 int compat
= lang_hooks
.types_compatible_p (wanted_type
, cur_type
);
3106 normal_type
= type_normalize (cur_type
, &cousin
, wanted_type
);
3108 return normal_type
== wanted_type
;
3112 /* Given type TYPE, attempt to dereference the type N times
3113 (e.g. from ("int ***", 2) to "int *")
3115 Return the derefenced type, with any qualifiers
3116 such as "const" stripped from the result, or
3117 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3120 deref_n_times (tree type
, int n
)
3124 for (int i
= n
; i
> 0; i
--)
3126 if (TREE_CODE (type
) != POINTER_TYPE
)
3128 type
= TREE_TYPE (type
);
3130 /* Strip off any "const" etc. */
3131 return build_qualified_type (type
, 0);
3134 /* Lookup the format code for FORMAT_LEN within FLI,
3135 returning the string code for expressing it, or NULL
3136 if it is not found. */
3139 get_modifier_for_format_len (const format_length_info
*fli
,
3140 enum format_lengths format_len
)
3142 for (; fli
->name
; fli
++)
3144 if (fli
->index
== format_len
)
3146 if (fli
->double_index
== format_len
)
3147 return fli
->double_name
;
3154 namespace selftest
{
3157 test_get_modifier_for_format_len ()
3160 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
3162 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
3164 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
3166 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
3169 } // namespace selftest
3171 #endif /* CHECKING_P */
3173 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3174 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3175 Wformat type errors where the argument has type ARG_TYPE. */
3178 matching_type_p (tree spec_type
, tree arg_type
)
3180 gcc_assert (spec_type
);
3181 gcc_assert (arg_type
);
3183 /* If any of the types requires structural equality, we can't compare
3184 their canonical types. */
3185 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
3186 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
3189 spec_type
= TYPE_CANONICAL (spec_type
);
3190 arg_type
= TYPE_CANONICAL (arg_type
);
3192 if (TREE_CODE (spec_type
) == INTEGER_TYPE
3193 && TREE_CODE (arg_type
) == INTEGER_TYPE
3194 && (TYPE_UNSIGNED (spec_type
)
3195 ? spec_type
== c_common_unsigned_type (arg_type
)
3196 : spec_type
== c_common_signed_type (arg_type
)))
3199 return spec_type
== arg_type
;
3202 /* Subroutine of get_format_for_type.
3204 Generate a string containing the length modifier and conversion specifier
3205 that should be used to format arguments of type ARG_TYPE within FKI
3206 (effectively the inverse of the checking code).
3208 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3209 is required to use it, for correcting bogus length modifiers.
3210 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3211 that matches ARG_TYPE.
3213 If successful, returns a non-NULL string which should be freed
3215 Otherwise, returns NULL. */
3218 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
3219 char conversion_char
)
3221 gcc_assert (arg_type
);
3223 const format_char_info
*spec
;
3224 for (spec
= &fki
->conversion_specs
[0];
3228 if (conversion_char
)
3229 if (!strchr (spec
->format_chars
, conversion_char
))
3232 tree effective_arg_type
= deref_n_times (arg_type
,
3233 spec
->pointer_count
);
3234 if (!effective_arg_type
)
3236 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
3238 const format_type_detail
*ftd
= &spec
->types
[i
];
3241 if (matching_type_p (*ftd
->type
, effective_arg_type
))
3243 const char *len_modifier
3244 = get_modifier_for_format_len (fki
->length_char_specs
,
3245 (enum format_lengths
)i
);
3249 if (conversion_char
)
3250 /* We found a match, using the given conversion char - the
3251 length modifier was incorrect (or absent).
3252 Provide a suggestion using the conversion char with the
3253 correct length modifier for the type. */
3254 return xasprintf ("%s%c", len_modifier
, conversion_char
);
3256 /* 2nd pass: no match was possible using the user-provided
3257 conversion char, but we do have a match without using it.
3258 Provide a suggestion using the first conversion char
3259 listed for the given type. */
3260 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
3268 /* Generate a string containing the length modifier and conversion specifier
3269 that should be used to format arguments of type ARG_TYPE within FKI
3270 (effectively the inverse of the checking code).
3272 If successful, returns a non-NULL string which should be freed
3274 Otherwise, returns NULL. */
3277 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
3278 char conversion_char
)
3280 gcc_assert (arg_type
);
3281 gcc_assert (conversion_char
);
3283 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3284 If we find one, then presumably the length modifier was incorrect
3286 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
3290 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3291 matching just on the type. */
3292 return get_format_for_type_1 (fki
, arg_type
, '\0');
3295 /* Attempt to get a string for use as a replacement fix-it hint for the
3296 source range in FMT_LOC.
3298 Preserve all of the text within the range of FMT_LOC up to
3299 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3300 length modifier and conversion specifier for ARG_TYPE, attempting
3301 to keep the user-provided CONVERSION_CHAR if possible.
3303 For example, given a long vs long long mismatch for arg5 here:
3305 000000000111111111122222222223333333333|
3306 123456789012345678901234567890123456789` column numbers
3307 0000000000111111111122|
3308 0123456789012345678901` string offsets
3309 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3310 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3312 | ` CONVERSION_CHAR: 'd'
3315 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3316 STRING_CST), where the user provided:
3318 the result (assuming "long" argument 5) should be:
3321 If successful, returns a non-NULL string which should be freed
3323 Otherwise, returns NULL. */
3326 get_corrected_substring (const substring_loc
&fmt_loc
,
3327 format_wanted_type
*type
, tree arg_type
,
3328 const format_kind_info
*fki
,
3329 int offset_to_type_start
, char conversion_char
)
3331 /* Attempt to provide hints for argument types, but not for field widths
3335 if (type
->kind
!= CF_KIND_FORMAT
)
3338 /* Locate the current code within the source range, rejecting
3339 any awkward cases where the format string occupies more than
3341 Lookup the place where the type starts (including any length
3342 modifiers), getting it as the caret location. */
3343 substring_loc
type_loc (fmt_loc
);
3344 type_loc
.set_caret_index (offset_to_type_start
);
3346 location_t fmt_substring_loc
;
3347 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
3351 source_range fmt_substring_range
3352 = get_range_from_loc (line_table
, fmt_substring_loc
);
3354 expanded_location caret
3355 = expand_location_to_spelling_point (fmt_substring_loc
);
3356 expanded_location start
3357 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
3358 expanded_location finish
3359 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
3360 if (caret
.file
!= start
.file
)
3362 if (start
.file
!= finish
.file
)
3364 if (caret
.line
!= start
.line
)
3366 if (start
.line
!= finish
.line
)
3368 if (start
.column
> caret
.column
)
3370 if (start
.column
> finish
.column
)
3372 if (caret
.column
> finish
.column
)
3375 #if BUILDING_GCC_VERSION >= 9000
3376 char_span line
= location_get_source_line (start
.file
, start
.line
);
3380 /* If we got this far, then we have the line containing the
3381 existing conversion specification.
3383 Generate a trimmed copy, containing the prefix part of the conversion
3384 specification, up to the (but not including) the length modifier.
3385 In the above example, this would be "%-+*.*". */
3386 int length_up_to_type
= caret
.column
- start
.column
;
3387 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
3388 char *prefix
= prefix_span
.xstrdup ();
3390 char *prefix
= NULL
;
3393 /* Now attempt to generate a suggestion for the rest of the specification
3394 (length modifier and conversion char), based on ARG_TYPE and
3396 In the above example, this would be "ld". */
3397 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
3398 if (!format_for_type
)
3404 /* Success. Generate the resulting suggestion for the whole range of
3405 FMT_LOC by concatenating the two strings.
3406 In the above example, this would be "%-+*.*ld". */
3407 char *result
= concat (prefix
, format_for_type
, NULL
);
3408 free (format_for_type
);
3413 /* Helper class for adding zero or more trailing '*' to types.
3415 The format type and name exclude any '*' for pointers, so those
3416 must be formatted manually. For all the types we currently have,
3417 this is adequate, but formats taking pointers to functions or
3418 arrays would require the full type to be built up in order to
3419 print it with %T. */
3421 class indirection_suffix
3424 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
3426 /* Determine the size of the buffer (including NUL-terminator). */
3428 size_t get_buffer_size () const
3430 return m_pointer_count
+ 2;
3433 /* Write the '*' to DST and add a NUL-terminator. */
3435 void fill_buffer (char *dst
) const
3437 if (m_pointer_count
== 0)
3439 else if (c_dialect_cxx ())
3441 memset (dst
, '*', m_pointer_count
);
3442 dst
[m_pointer_count
] = 0;
3447 memset (dst
+ 1, '*', m_pointer_count
);
3448 dst
[m_pointer_count
+ 1] = 0;
3453 int m_pointer_count
;
3456 #if BUILDING_GCC_VERSION >= 9000
3457 /* not exported by GCC... need a local copy :( */
3458 class frr_range_label_for_type_mismatch
: public range_label
3461 frr_range_label_for_type_mismatch (tree labelled_type
, tree other_type
)
3462 : m_labelled_type (labelled_type
), m_other_type (other_type
)
3466 label_text
get_text (unsigned range_idx
) const OVERRIDE
;
3469 tree m_labelled_type
;
3473 /* Print T to CPP. */
3476 print_type (c_pretty_printer
*cpp
, tree t
, bool *quoted
)
3478 gcc_assert (TYPE_P (t
));
3479 struct obstack
*ob
= pp_buffer (cpp
)->obstack
;
3480 char *p
= (char *) obstack_base (ob
);
3481 /* Remember the end of the initial dump. */
3482 int len
= obstack_object_size (ob
);
3484 tree name
= TYPE_NAME (t
);
3485 if (name
&& TREE_CODE (name
) == TYPE_DECL
&& DECL_NAME (name
))
3486 pp_identifier (cpp
, lang_hooks
.decl_printable_name (name
, 2));
3490 /* If we're printing a type that involves typedefs, also print the
3491 stripped version. But sometimes the stripped version looks
3492 exactly the same, so we don't want it after all. To avoid
3493 printing it in that case, we play ugly obstack games. */
3494 if (TYPE_CANONICAL (t
) && t
!= TYPE_CANONICAL (t
))
3496 c_pretty_printer cpp2
;
3497 /* Print the stripped version into a temporary printer. */
3498 cpp2
.type_id (TYPE_CANONICAL (t
));
3499 struct obstack
*ob2
= cpp2
.buffer
->obstack
;
3500 /* Get the stripped version from the temporary printer. */
3501 const char *aka
= (char *) obstack_base (ob2
);
3502 int aka_len
= obstack_object_size (ob2
);
3503 int type1_len
= obstack_object_size (ob
) - len
;
3505 /* If they are identical, bail out. */
3506 if (aka_len
== type1_len
&& memcmp (p
+ len
, aka
, aka_len
) == 0)
3509 /* They're not, print the stripped version now. */
3511 pp_end_quote (cpp
, pp_show_color (cpp
));
3512 pp_c_whitespace (cpp
);
3513 pp_left_brace (cpp
);
3514 pp_c_ws_string (cpp
, _("aka"));
3515 pp_c_whitespace (cpp
);
3517 pp_begin_quote (cpp
, pp_show_color (cpp
));
3518 cpp
->type_id (TYPE_CANONICAL (t
));
3520 pp_end_quote (cpp
, pp_show_color (cpp
));
3521 pp_right_brace (cpp
);
3522 /* No further closing quotes are needed. */
3527 /* C-specific implementation of range_label::get_text () vfunc for
3528 range_label_for_type_mismatch. */
3529 #if BUILDING_GCC_VERSION >= 10000
3530 #define label_borrow(text) label_text::borrow(text)
3531 #define label_take(text) label_text::take(text)
3533 #define label_borrow(text) label_text((char *)text, false)
3534 #define label_take(text) label_text(text, true)
3538 frr_range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
3540 if (m_labelled_type
== NULL_TREE
)
3541 return label_borrow("(null tree)");
3543 c_pretty_printer cpp
;
3544 bool quoted
= false;
3545 print_type (&cpp
, m_labelled_type
, "ed
);
3546 return label_take(xstrdup (pp_formatted_text (&cpp
)));
3549 #define range_label_for_type_mismatch frr_range_label_for_type_mismatch
3552 /* Subclass of range_label for labelling the range in the format string
3553 with the type in question, adding trailing '*' for pointer_count. */
3555 class range_label_for_format_type_mismatch
3556 : public range_label_for_type_mismatch
3559 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
3561 : range_label_for_type_mismatch (labelled_type
, other_type
),
3562 m_pointer_count (pointer_count
)
3566 label_text
get_text (unsigned range_idx
) const FINAL OVERRIDE
3568 label_text text
= range_label_for_type_mismatch::get_text (range_idx
);
3569 if (text
.m_buffer
== NULL
)
3572 indirection_suffix
suffix (m_pointer_count
);
3573 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3574 suffix
.fill_buffer (p
);
3576 char *result
= concat (text
.m_buffer
, p
, NULL
);
3578 return label_take(result
);
3582 int m_pointer_count
;
3585 /* Give a warning about a format argument of different type from that expected.
3586 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3587 is based on the location of the char at TYPE->offset_loc.
3588 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3589 if this is unavailable.
3590 WANTED_TYPE is the type the argument should have,
3591 possibly stripped of pointer dereferences. The description (such as "field
3592 precision"), the placement in the format string, a possibly more
3593 friendly name of WANTED_TYPE, and the number of pointer dereferences
3594 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3595 or NULL if it is missing.
3597 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3598 format string to where type information begins for the conversion
3599 (the length modifier and conversion specifier).
3600 CONVERSION_CHAR is the user-provided conversion specifier.
3602 For example, given a type mismatch for argument 5 here:
3604 00000000011111111112222222222333333333344444444445555555555|
3605 12345678901234567890123456789012345678901234567890123456789` column numbers
3606 0000000000111111111122|
3607 0123456789012345678901` offsets within STRING_CST
3608 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3609 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3611 | ` CONVERSION_CHAR: 'd' PARAM_LOC
3614 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3618 format_type_warning (const substring_loc
&whole_fmt_loc
,
3619 location_t param_loc
,
3620 format_wanted_type
*type
,
3621 tree wanted_type
, tree arg_type
,
3622 const format_kind_info
*fki
,
3623 int offset_to_type_start
,
3624 char conversion_char
,
3627 enum format_specifier_kind kind
= type
->kind
;
3628 const char *wanted_type_name
= type
->wanted_type_name
;
3629 const char *format_start
= type
->format_start
;
3630 int format_length
= type
->format_length
;
3631 int pointer_count
= type
->pointer_count
;
3632 int arg_num
= type
->arg_num
;
3637 /* If ARG_TYPE is a typedef with a misleading name (for example,
3638 size_t but not the standard size_t expected by printf %zu), avoid
3639 printing the typedef name. */
3640 if (wanted_type_name
3642 && TYPE_NAME (arg_type
)
3643 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
3644 && DECL_NAME (TYPE_NAME (arg_type
))
3645 && !strcmp (wanted_type_name
,
3646 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
3647 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
3649 indirection_suffix
suffix (pointer_count
);
3650 char *p
= (char *) alloca (suffix
.get_buffer_size ());
3651 suffix
.fill_buffer (p
);
3653 /* WHOLE_FMT_LOC has the caret at the end of the range.
3654 Set the caret to be at the offset from TYPE. Subtract one
3655 from the offset for the same reason as in format_warning_at_char. */
3656 substring_loc
fmt_loc (whole_fmt_loc
);
3657 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
3659 #if BUILDING_GCC_VERSION >= 9000
3660 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
3662 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
3664 /* Get a string for use as a replacement fix-it hint for the range in
3665 fmt_loc, or NULL. */
3666 char *corrected_substring
3667 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3668 offset_to_type_start
, conversion_char
);
3669 format_string_diagnostic_t
diag (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3670 corrected_substring
);
3671 # define format_warning_at_substring(a,b,c,d,e,...) \
3672 diag.emit_warning(__VA_ARGS__)
3674 # define format_warning_at_substring(a,b,c,d,...) \
3675 format_warning_at_substring(a,c,__VA_ARGS__)
3676 /* Get a string for use as a replacement fix-it hint for the range in
3677 fmt_loc, or NULL. */
3678 char *corrected_substring
3679 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
3680 offset_to_type_start
, conversion_char
);
3684 if (wanted_type_name
)
3687 format_warning_at_substring
3688 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3689 corrected_substring
, OPT_Wformat_
,
3690 "%s %<%s%.*s%> expects argument of type %<%s%s%>, but argument %d has type %qT%s",
3691 gettext (kind_descriptions
[kind
]),
3692 (kind
== CF_KIND_FORMAT
? "%" : ""),
3693 format_length
, format_start
,
3694 wanted_type_name
, p
, arg_num
, arg_type
, extra
);
3696 format_warning_at_substring
3697 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3698 corrected_substring
, OPT_Wformat_
,
3699 "%s %<%s%.*s%> expects a matching %<%s%s%> argument%s",
3700 gettext (kind_descriptions
[kind
]),
3701 (kind
== CF_KIND_FORMAT
? "%" : ""),
3702 format_length
, format_start
, wanted_type_name
, p
, extra
);
3707 format_warning_at_substring
3708 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3709 corrected_substring
, OPT_Wformat_
,
3710 "%s %<%s%.*s%> expects argument of type %<%T%s%>, but argument %d has type %qT%s",
3711 gettext (kind_descriptions
[kind
]),
3712 (kind
== CF_KIND_FORMAT
? "%" : ""),
3713 format_length
, format_start
,
3714 wanted_type
, p
, arg_num
, arg_type
, extra
);
3716 format_warning_at_substring
3717 (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
3718 corrected_substring
, OPT_Wformat_
,
3719 "%s %<%s%.*s%> expects a matching %<%T%s%> argument%s",
3720 gettext (kind_descriptions
[kind
]),
3721 (kind
== CF_KIND_FORMAT
? "%" : ""),
3722 format_length
, format_start
, wanted_type
, p
, extra
);
3725 free (corrected_substring
);
3730 /* Given a format_char_info array FCI, and a character C, this function
3731 returns the index into the conversion_specs where that specifier's
3732 data is located. The character must exist. */
3734 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
3738 for (i
= 0; fci
->format_chars
; i
++, fci
++)
3739 if (strchr (fci
->format_chars
, c
))
3742 /* We shouldn't be looking for a non-existent specifier. */
3746 /* Given a format_length_info array FLI, and a character C, this
3747 function returns the index into the conversion_specs where that
3748 modifier's data is located. The character must exist. */
3750 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
3754 for (i
= 0; fli
->name
; i
++, fli
++)
3755 if (strchr (fli
->name
, c
))
3758 /* We shouldn't be looking for a non-existent modifier. */
3763 #ifdef TARGET_FORMAT_TYPES
3764 extern const format_kind_info TARGET_FORMAT_TYPES
[];
3767 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3768 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
3770 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3771 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3774 /* Attributes such as "printf" are equivalent to those such as
3775 "gnu_printf" unless this is overridden by a target. */
3776 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
3781 /* Translate to unified attribute name. This is used in decode_format_type and
3782 decode_format_attr. In attr_name the user specified argument is passed. It
3783 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3784 or the attr_name passed to this function, if there is no matching entry. */
3786 convert_format_name_to_system_name (const char *attr_name
)
3790 if (attr_name
== NULL
|| *attr_name
== 0
3791 || strncmp (attr_name
, "gcc_", 4) == 0)
3793 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3794 TARGET_OVERRIDES_FORMAT_INIT ();
3797 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3798 /* Check if format attribute is overridden by target. */
3799 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
3800 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
3802 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
3804 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
3807 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
3809 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
3813 /* Otherwise default to gnu format. */
3815 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
3818 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
3821 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
3823 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
3829 /* Handle a "format" attribute; arguments as in
3830 struct attribute_spec.handler. */
3832 handle_frr_format_attribute (tree
*node
, tree
ARG_UNUSED (name
), tree args
,
3833 int flags
, bool *no_add_attrs
)
3836 function_format_info info
;
3838 /* Canonicalize name of format function. */
3839 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
3840 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
3842 if (!decode_format_attr (args
, &info
, 0))
3844 *no_add_attrs
= true;
3848 if (prototype_p (type
))
3850 if (!check_format_string (type
, info
.format_num
, flags
,
3851 no_add_attrs
, info
.format_type
))
3854 if (info
.first_arg_num
!= 0)
3856 unsigned HOST_WIDE_INT arg_num
= 1;
3857 function_args_iterator iter
;
3860 /* Verify that first_arg_num points to the last arg,
3862 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
3865 if (arg_num
!= info
.first_arg_num
)
3867 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
3868 error ("arguments to be formatted is not %<...%>");
3869 *no_add_attrs
= true;
3875 /* Check if this is a strftime variant. Just for this variant
3876 FMT_FLAG_ARG_CONVERT is not set. */
3877 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
3878 && info
.first_arg_num
!= 0)
3880 error ("strftime formats cannot format arguments");
3881 *no_add_attrs
= true;
3890 namespace selftest
{
3892 /* Selftests of location handling. */
3894 /* Get the format_kind_info with the given name. */
3896 static const format_kind_info
*
3897 get_info (const char *name
)
3899 int idx
= decode_format_type (name
);
3900 const format_kind_info
*fki
= &format_types
[idx
];
3901 ASSERT_STREQ (fki
->name
, name
);
3905 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
3906 is EXPECTED_FORMAT. */
3909 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
3910 const char *expected_format
, tree type
,
3911 char conversion_char
)
3914 gcc_assert (expected_format
);
3917 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
3918 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
3919 free (actual_format
);
3922 /* Selftests for get_format_for_type. */
3924 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
3925 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
3926 (TYPE), (CONVERSION_CHAR))
3928 /* Selftest for get_format_for_type for "printf"-style functions. */
3931 test_get_format_for_type_printf ()
3933 const format_kind_info
*fki
= get_info ("gnu_printf");
3934 ASSERT_NE (fki
, NULL
);
3936 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
3937 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
3938 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
3939 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
3940 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
3941 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
3942 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
3943 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
3944 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
3945 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
3946 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
3947 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
3948 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
3949 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
3950 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
3951 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
3952 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
3953 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
3954 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
3955 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
3956 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
3957 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
3958 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
3959 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
3960 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
3961 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
3962 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
3963 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
3966 /* Selftest for get_format_for_type for "scanf"-style functions. */
3969 test_get_format_for_type_scanf ()
3971 const format_kind_info
*fki
= get_info ("gnu_scanf");
3972 ASSERT_NE (fki
, NULL
);
3973 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
3974 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
3975 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
3976 build_pointer_type (long_integer_type_node
), 'd');
3977 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
3978 build_pointer_type (long_unsigned_type_node
), 'u');
3979 ASSERT_FORMAT_FOR_TYPE_STREQ
3980 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
3981 ASSERT_FORMAT_FOR_TYPE_STREQ
3982 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
3983 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
3984 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
3987 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
3989 /* Exercise the type-printing label code, to give some coverage
3990 under "make selftest-valgrind" (in particular, to ensure that
3991 the label-printing machinery doesn't leak). */
3994 test_type_mismatch_range_labels ()
3996 /* Create a tempfile and write some text to it.
3997 ....................0000000001 11111111 12 22222222
3998 ....................1234567890 12345678 90 12345678. */
3999 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
4000 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
4001 line_table_test ltt
;
4003 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
4005 location_t c17
= linemap_position_for_column (line_table
, 17);
4006 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
4007 location_t c18
= linemap_position_for_column (line_table
, 18);
4008 location_t c24
= linemap_position_for_column (line_table
, 24);
4009 location_t c26
= linemap_position_for_column (line_table
, 26);
4011 /* Don't attempt to run the tests if column data might be unavailable. */
4012 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
4015 location_t fmt
= make_location (c18
, c17
, c18
);
4016 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
4018 location_t param
= make_location (c24
, c24
, c26
);
4019 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
4021 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
4022 integer_type_node
, 1);
4023 range_label_for_type_mismatch
param_label (integer_type_node
,
4025 gcc_rich_location
richloc (fmt
, &fmt_label
);
4026 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
4028 test_diagnostic_context dc
;
4029 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
4030 if (c_dialect_cxx ())
4031 /* "char*", without a space. */
4033 " printf (\"msg: %i\\n\", msg);\n"
4037 pp_formatted_text (dc
.printer
));
4039 /* "char *", with a space. */
4041 " printf (\"msg: %i\\n\", msg);\n"
4046 pp_formatted_text (dc
.printer
));
4049 /* Run all of the selftests within this file. */
4054 test_get_modifier_for_format_len ();
4055 test_get_format_for_type_printf ();
4056 test_get_format_for_type_scanf ();
4057 test_type_mismatch_range_labels ();
4060 } // namespace selftest
4062 #endif /* CHECKING_P */
4064 // include "gt-c-family-c-format.h"
4066 static const struct attribute_spec frr_format_attribute_table
[] =
4068 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4069 affects_type_identity, handler, exclude } */
4070 { "frr_format", 3, 3, false, true, true, false,
4071 handle_frr_format_attribute
, NULL
},
4072 { "frr_format_arg", 1, 1, false, true, true, false,
4073 handle_frr_format_arg_attribute
, NULL
},
4074 { NULL
, 0, 0, false, false, false, false, NULL
, NULL
}
4078 register_attributes (void *event_data
, void *data
)
4080 // warning (0, G_("Callback to register attributes"));
4081 register_attribute (frr_format_attribute_table
);
4085 cb_walk_tree_fn (tree
* tp
, int * walk_subtrees
, void * data ATTRIBUTE_UNUSED
)
4087 if (TREE_CODE (*tp
) != CALL_EXPR
)
4090 tree call_expr
= *tp
;
4092 int nargs
= call_expr_nargs(call_expr
);
4093 tree fn
= CALL_EXPR_FN(call_expr
);
4095 if (!fn
|| TREE_CODE (fn
) != ADDR_EXPR
)
4098 tree fndecl
= TREE_OPERAND (fn
, 0);
4099 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
4103 warning (0, G_("function call to %s, %d args"),
4104 IDENTIFIER_POINTER (DECL_NAME (fndecl
)),
4108 tree
*fargs
= (tree
*) alloca (nargs
* sizeof (tree
));
4110 for (int j
= 0; j
< nargs
; j
++)
4112 tree arg
= CALL_EXPR_ARG(call_expr
, j
);
4114 /* For -Wformat undo the implicit passing by hidden reference
4115 done by convert_arg_to_ellipsis. */
4116 if (TREE_CODE (arg
) == ADDR_EXPR
4117 && TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
4118 fargs
[j
] = TREE_OPERAND (arg
, 0);
4123 check_function_format (TYPE_ATTRIBUTES (TREE_TYPE (fndecl
)), nargs
, fargs
, NULL
);
4128 setup_type (const char *name
, tree
*dst
)
4132 if (*dst
&& *dst
!= void_type_node
)
4135 *dst
= maybe_get_identifier (name
);
4139 tmp
= identifier_global_value (*dst
);
4140 if (tmp
&& TREE_CODE (tmp
) != TYPE_DECL
)
4142 warning (0, "%qs is not defined as a type", name
);
4146 if (tmp
&& TREE_CODE (tmp
) == TYPE_DECL
)
4153 handle_finish_parse (void *event_data
, void *data
)
4155 tree fndecl
= (tree
) event_data
;
4156 gcc_assert (TREE_CODE (fndecl
) == FUNCTION_DECL
);
4158 setup_type ("uint64_t", &local_uint64_t_node
);
4159 setup_type ("int64_t", &local_int64_t_node
);
4161 setup_type ("size_t", &local_size_t_node
);
4162 setup_type ("ssize_t", &local_ssize_t_node
);
4163 setup_type ("atomic_size_t", &local_atomic_size_t_node
);
4164 setup_type ("atomic_ssize_t", &local_atomic_ssize_t_node
);
4165 setup_type ("ptrdiff_t", &local_ptrdiff_t_node
);
4167 setup_type ("pid_t", &local_pid_t_node
);
4168 setup_type ("uid_t", &local_uid_t_node
);
4169 setup_type ("gid_t", &local_gid_t_node
);
4170 setup_type ("time_t", &local_time_t_node
);
4172 setup_type ("socklen_t", &local_socklen_t_node
);
4173 setup_type ("in_addr_t", &local_in_addr_t_node
);
4175 const format_char_info
*fci
;
4177 for (fci
= print_char_table
; fci
->format_chars
; fci
++)
4179 if (!fci
->kernel_ext
)
4182 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
4183 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
4185 for (; etab
->suffix
&& etab
< etab_end
; etab
++)
4187 tree identifier
, node
;
4189 if (etab
->type
&& etab
->type
!= void_type_node
)
4192 identifier
= maybe_get_identifier (etab
->type_str
);
4194 if (!identifier
|| identifier
== error_mark_node
)
4197 if (etab
->type_code
)
4199 node
= identifier_global_tag (identifier
);
4203 if (node
->base
.code
!= etab
->type_code
)
4207 warning (0, "%qs tag category (struct/union/enum) mismatch", etab
->type_str
);
4208 etab
->warned
= true;
4215 node
= identifier_global_value (identifier
);
4219 if (TREE_CODE (node
) != TYPE_DECL
)
4223 warning (0, "%qs is defined as a non-type", etab
->type_str
);
4224 etab
->warned
= true;
4228 node
= TREE_TYPE (node
);
4235 walk_tree (&DECL_SAVED_TREE (fndecl
), cb_walk_tree_fn
, NULL
, NULL
);
4239 handle_pragma_printfrr_ext (cpp_reader
*dummy
)
4243 enum cpp_ttype ttype
;
4245 ttype
= pragma_lex (&token
, &loc
);
4246 if (ttype
!= CPP_STRING
)
4248 error_at (loc
, "%<#pragma FRR printfrr_ext%> requires string argument");
4252 const char *s
= TREE_STRING_POINTER (token
);
4256 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, needs to start with '%%'");
4267 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, needs to be %%p, %%d or %%i");
4271 const format_char_info
*fci
;
4273 for (fci
= print_char_table
; fci
->format_chars
; fci
++)
4274 if (strchr (fci
->format_chars
, s
[1]))
4277 gcc_assert (fci
->format_chars
);
4278 gcc_assert (fci
->kernel_ext
);
4280 struct kernel_ext_fmt
*etab
= fci
->kernel_ext
;
4281 struct kernel_ext_fmt
*etab_end
= etab
+ ETAB_SZ
;
4289 error_at (loc
, "%<#pragma FRR printfrr_ext%>: invalid format string, suffix must start with an uppercase letter");
4293 /* -2 -- need to keep the sentinel at the end */
4294 if (etab
[ETAB_SZ
- 2].suffix
)
4296 error_at (loc
, "%<#pragma FRR printfrr_ext%>: out of space for format suffixes");
4300 for (; etab
->suffix
&& etab
< etab_end
; etab
++)
4302 if (!strcmp(s
+ 2, etab
->suffix
))
4304 memmove (etab
+ 1, etab
, (etab_end
- etab
- 1) * sizeof (*etab
));
4308 warning_at (loc
, OPT_Wformat_
,
4309 "%<#pragma FRR printfrr_ext%>: duplicate printf format suffix %qs", s
);
4310 warning_at (etab
->origin_loc
, OPT_Wformat_
,
4311 "%<#pragma FRR printfrr_ext%>: previous definition was here");
4318 if (!strncmp(s
+ 2, etab
->suffix
, MIN(strlen(s
+ 2), strlen(etab
->suffix
))))
4320 warning_at (loc
, OPT_Wformat_
,
4321 "%<#pragma FRR printfrr_ext%>: overlapping printf format suffix %qs", s
);
4322 warning_at (etab
->origin_loc
, OPT_Wformat_
,
4323 "%<#pragma FRR printfrr_ext%>: previous definition for %<%%%c%s%> was here", s
[1], etab
->suffix
);
4328 gcc_assert (etab
< etab_end
);
4330 memset (etab
, 0, sizeof (*etab
));
4331 etab
->suffix
= xstrdup(s
+ 2);
4332 etab
->origin_loc
= loc
;
4333 etab
->type
= void_type_node
;
4335 ttype
= pragma_lex (&token
, &loc
);
4336 if (ttype
!= CPP_OPEN_PAREN
)
4338 error_at (loc
, "%<#pragma FRR printfrr_ext%> expected %<(%>");
4342 ttype
= pragma_lex (&token
, &loc
);
4345 if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "const"))
4347 etab
->t_const
= true;
4348 ttype
= pragma_lex (&token
, &loc
);
4352 if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "struct"))
4354 etab
->type_code
= RECORD_TYPE
;
4355 ttype
= pragma_lex (&token
, &loc
);
4357 else if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "union"))
4359 etab
->type_code
= UNION_TYPE
;
4360 ttype
= pragma_lex (&token
, &loc
);
4362 else if (ttype
== CPP_NAME
&& !strcmp (IDENTIFIER_POINTER (token
), "enum"))
4364 etab
->type_code
= ENUMERAL_TYPE
;
4365 ttype
= pragma_lex (&token
, &loc
);
4369 if (ttype
!= CPP_NAME
)
4371 error_at (loc
, "%<#pragma FRR printfrr_ext%>: expected typename identifier");
4375 etab
->type_str
= xstrdup (IDENTIFIER_POINTER (token
));
4377 while ((ttype
= pragma_lex (&token
, &loc
)) != CPP_CLOSE_PAREN
)
4381 error_at (loc
, "%<#pragma FRR printfrr_ext%>: unexpected identifier. Note the only supported qualifier is %<const%>");
4389 error_at (loc
, "%<#pragma FRR printfrr_ext%>: premature end of line, missing %<)%>");
4393 error_at (loc
, "%<#pragma FRR printfrr_ext%>: unsupported token");
4398 ttype
= pragma_lex (&token
, &loc
);
4399 if (ttype
!= CPP_EOF
)
4400 warning_at (loc
, OPT_Wformat_
,
4401 "%<#pragma FRR printfrr_ext%>: garbage at end of line");
4406 memset (etab
, 0, sizeof (*etab
));
4410 register_pragma_printfrr_ext (void *event_data
, void *data
)
4412 c_register_pragma_with_expansion ("FRR", "printfrr_ext", handle_pragma_printfrr_ext
);
4416 define_vars (void *gcc_data
, void *user_data
)
4418 cpp_define (parse_in
, "_FRR_ATTRIBUTE_PRINTFRR=0x10000");
4422 #define __visible __attribute__((visibility("default")))
4425 __visible
int plugin_is_GPL_compatible
;
4428 plugin_init (struct plugin_name_args
*plugin_info
,
4429 struct plugin_gcc_version
*version
)
4431 const char *plugin_name
= plugin_info
->base_name
;
4433 if (!plugin_default_version_check(version
, &gcc_version
))
4435 error(G_("incompatible gcc/plugin versions"));
4439 memset (ext_p
, 0, sizeof (ext_p
));
4440 memset (ext_d
, 0, sizeof (ext_d
));
4442 register_callback (plugin_name
, PLUGIN_FINISH_PARSE_FUNCTION
, handle_finish_parse
, NULL
);
4443 register_callback (plugin_name
, PLUGIN_ATTRIBUTES
, register_attributes
, NULL
);
4444 register_callback (plugin_name
, PLUGIN_START_UNIT
, define_vars
, NULL
);
4445 register_callback (plugin_name
, PLUGIN_PRAGMAS
, register_pragma_printfrr_ext
, NULL
);