1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "alloc-pool.h"
27 #include "stringpool.h"
30 #include "langhooks.h"
32 #include "diagnostic.h"
33 #include "substring-locations.h"
35 #include "selftest-diagnostic.h"
38 #include "gcc-rich-location.h"
40 /* Handle attributes associated with format checking. */
42 /* This must be in the same order as format_types, except for
43 format_type_error. Target-specific format types do not have
44 matching enum values. */
45 enum format_type
{ printf_format_type
, asm_fprintf_format_type
,
46 gcc_diag_format_type
, gcc_tdiag_format_type
,
47 gcc_cdiag_format_type
,
48 gcc_cxxdiag_format_type
, gcc_gfc_format_type
,
49 gcc_dump_printf_format_type
,
50 gcc_objc_string_format_type
,
51 format_type_error
= -1};
53 struct function_format_info
55 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
56 /* IS_RAW is relevant only for GCC diagnostic format functions.
57 It is set for "raw" formatting functions like pp_printf that
58 are not intended to produce complete diagnostics according to
59 GCC guidelines, and clear for others like error and warning
60 whose format string is checked for proper quoting and spelling. */
62 unsigned HOST_WIDE_INT format_num
; /* number of format argument */
63 unsigned HOST_WIDE_INT first_arg_num
; /* number of first arg (zero for varargs) */
66 /* Initialized in init_dynamic_diag_info. */
67 static GTY(()) tree local_tree_type_node
;
68 static GTY(()) tree local_event_ptr_node
;
69 static GTY(()) tree local_gimple_ptr_node
;
70 static GTY(()) tree local_cgraph_node_ptr_node
;
71 static GTY(()) tree locus
;
73 static bool decode_format_attr (const_tree
, tree
, tree
, function_format_info
*,
75 static format_type
decode_format_type (const char *, bool * = NULL
);
77 static bool check_format_string (const_tree argument
,
78 unsigned HOST_WIDE_INT format_num
,
79 int flags
, bool *no_add_attrs
,
80 int expected_format_type
);
81 static tree
get_constant (const_tree fntype
, const_tree atname
, tree expr
,
82 int argno
, unsigned HOST_WIDE_INT
*value
,
83 int flags
, bool validated_p
);
84 static const char *convert_format_name_to_system_name (const char *attr_name
);
86 static int first_target_format_type
;
87 static const char *format_name (int format_num
);
88 static int format_flags (int format_num
);
90 /* Emit a warning as per format_warning_va, but construct the substring_loc
91 for the character at offset (CHAR_IDX - 1) within a string constant
92 FORMAT_STRING_CST at FMT_STRING_LOC. */
94 ATTRIBUTE_GCC_DIAG (5,6)
96 format_warning_at_char (location_t fmt_string_loc
, tree format_string_cst
,
97 int char_idx
, int opt
, const char *gmsgid
, ...)
100 va_start (ap
, gmsgid
);
101 tree string_type
= TREE_TYPE (format_string_cst
);
103 /* The callers are of the form:
104 format_warning (format_string_loc, format_string_cst,
105 format_chars - orig_format_chars,
106 where format_chars has already been incremented, so that
107 CHAR_IDX is one character beyond where the warning should
108 be emitted. Fix it. */
111 substring_loc
fmt_loc (fmt_string_loc
, string_type
, char_idx
, char_idx
,
113 format_string_diagnostic_t
diag (fmt_loc
, NULL
, UNKNOWN_LOCATION
, NULL
,
115 bool warned
= diag
.emit_warning_va (opt
, gmsgid
, &ap
);
122 /* Emit a warning as per format_warning_va, but construct the substring_loc
123 for the substring at offset (POS1, POS2 - 1) within a string constant
124 FORMAT_STRING_CST at FMT_STRING_LOC. */
126 ATTRIBUTE_GCC_DIAG (6,7)
128 format_warning_substr (location_t fmt_string_loc
, tree format_string_cst
,
129 int pos1
, int pos2
, int opt
, const char *gmsgid
, ...)
132 va_start (ap
, gmsgid
);
133 tree string_type
= TREE_TYPE (format_string_cst
);
137 substring_loc
fmt_loc (fmt_string_loc
, string_type
, pos1
, pos1
, pos2
);
138 format_string_diagnostic_t
diag (fmt_loc
, NULL
, UNKNOWN_LOCATION
, NULL
,
140 bool warned
= diag
.emit_warning_va (opt
, gmsgid
, &ap
);
147 /* Check that we have a pointer to a string suitable for use as a format.
148 The default is to check for a char type.
149 For objective-c dialects, this is extended to include references to string
150 objects validated by objc_string_ref_type_p ().
151 Targets may also provide a string object type that can be used within c and
152 c++ and shared with their respective objective-c dialects. In this case the
153 reference to a format string is checked for validity via a hook.
155 The function returns true if strref points to any string type valid for the
156 language dialect and target. */
159 valid_format_string_type_p (tree strref
)
161 return (strref
!= NULL
162 && TREE_CODE (strref
) == POINTER_TYPE
163 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref
)) == char_type_node
164 || objc_string_ref_type_p (strref
)
165 || (*targetcm
.string_object_ref_type_p
) ((const_tree
) strref
)));
168 /* Handle a "format_arg" attribute; arguments as in
169 struct attribute_spec.handler. */
171 handle_format_arg_attribute (tree
*node
, tree atname
,
172 tree args
, int flags
, bool *no_add_attrs
)
175 /* Note that TREE_VALUE (args) is changed in place below. */
176 tree
*format_num_expr
= &TREE_VALUE (args
);
177 unsigned HOST_WIDE_INT format_num
= 0;
179 if (tree val
= get_constant (type
, atname
, *format_num_expr
, 0, &format_num
,
181 *format_num_expr
= val
;
184 *no_add_attrs
= true;
188 if (prototype_p (type
))
190 /* The format arg can be any string reference valid for the language and
191 target. We cannot be more specific in this case. */
192 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
196 if (!valid_format_string_type_p (TREE_TYPE (type
)))
198 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
199 error ("function does not return string type");
200 *no_add_attrs
= true;
207 /* Verify that the format_num argument is actually a string reference suitable,
208 for the language dialect and target (in case the format attribute is in
209 error). When we know the specific reference type expected, this is also
212 check_format_string (const_tree fntype
, unsigned HOST_WIDE_INT format_num
,
213 int flags
, bool *no_add_attrs
, int expected_format_type
)
215 unsigned HOST_WIDE_INT i
;
216 bool is_objc_sref
, is_target_sref
, is_char_ref
;
219 function_args_iterator iter
;
222 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
230 || !valid_format_string_type_p (ref
))
232 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
233 error ("format string argument is not a string type");
234 *no_add_attrs
= true;
238 /* We only know that we want a suitable string reference. */
239 if (expected_format_type
< 0)
242 /* Now check that the arg matches the expected type. */
244 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
246 fmt_flags
= format_flags (expected_format_type
);
247 is_objc_sref
= is_target_sref
= false;
249 is_objc_sref
= objc_string_ref_type_p (ref
);
251 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
254 return true; /* OK, we expected a char and found one. */
257 /* We expected a char but found an extended string type. */
259 error ("found a %qs reference but the format argument should"
260 " be a string", format_name (gcc_objc_string_format_type
));
262 error ("found a %qT but the format argument should be a string",
264 *no_add_attrs
= true;
269 /* We expect a string object type as the format arg. */
272 error ("format argument should be a %qs reference but"
273 " a string was found", format_name (expected_format_type
));
274 *no_add_attrs
= true;
278 /* We will assert that objective-c will support either its own string type
279 or the target-supplied variant. */
281 is_target_sref
= (*targetcm
.string_object_ref_type_p
) ((const_tree
) ref
);
283 if (expected_format_type
== (int) gcc_objc_string_format_type
284 && (is_objc_sref
|| is_target_sref
))
287 /* We will allow a target string ref to match only itself. */
288 if (first_target_format_type
289 && expected_format_type
>= first_target_format_type
294 error ("format argument should be a %qs reference",
295 format_name (expected_format_type
));
296 *no_add_attrs
= true;
303 /* Under the control of FLAGS, verify EXPR is a valid constant that
304 refers to a positional argument ARGNO having a string type (char*
305 or, for targets like Darwin, a pointer to struct CFString) to
306 a function type FNTYPE declared with attribute ATNAME.
307 If valid, store the constant's integer value in *VALUE and return
309 If VALIDATED_P is true assert the validation is successful.
310 Returns the converted constant value on success, null otherwise. */
313 get_constant (const_tree fntype
, const_tree atname
, tree expr
, int argno
,
314 unsigned HOST_WIDE_INT
*value
, int flags
, bool validated_p
)
316 /* Require the referenced argument to have a string type. For targets
317 like Darwin, also accept pointers to struct CFString. */
318 if (tree val
= positional_argument (fntype
, atname
, expr
, STRING_CST
,
321 *value
= TREE_INT_CST_LOW (val
);
325 gcc_assert (!validated_p
);
329 /* Decode the arguments to a "format" attribute into a
330 function_format_info structure. It is already known that the list
331 is of the right length. If VALIDATED_P is true, then these
332 attributes have already been validated and must not be erroneous;
333 if false, it will give an error message. Returns true if the
334 attributes are successfully decoded, false otherwise. */
337 decode_format_attr (const_tree fntype
, tree atname
, tree args
,
338 function_format_info
*info
, bool validated_p
)
340 tree format_type_id
= TREE_VALUE (args
);
341 /* Note that TREE_VALUE (args) is changed in place below. Ditto
342 for the value of the next element on the list. */
343 tree
*format_num_expr
= &TREE_VALUE (TREE_CHAIN (args
));
344 tree
*first_arg_num_expr
= &TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
346 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
348 gcc_assert (!validated_p
);
349 error ("unrecognized format specifier");
354 const char *p
= IDENTIFIER_POINTER (format_type_id
);
356 info
->format_type
= decode_format_type (p
, &info
->is_raw
);
358 if (!c_dialect_objc ()
359 && info
->format_type
== gcc_objc_string_format_type
)
361 gcc_assert (!validated_p
);
362 warning (OPT_Wformat_
, "%qE is only allowed in Objective-C dialects",
364 info
->format_type
= format_type_error
;
368 if (info
->format_type
== format_type_error
)
370 gcc_assert (!validated_p
);
371 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
377 if (tree val
= get_constant (fntype
, atname
, *format_num_expr
,
378 2, &info
->format_num
, 0, validated_p
))
379 *format_num_expr
= val
;
383 if (tree val
= get_constant (fntype
, atname
, *first_arg_num_expr
,
384 3, &info
->first_arg_num
,
385 (POSARG_ZERO
| POSARG_ELLIPSIS
), validated_p
))
386 *first_arg_num_expr
= val
;
390 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
392 gcc_assert (!validated_p
);
393 error ("format string argument follows the arguments to be formatted");
400 /* Check a call to a format function against a parameter list. */
402 /* The C standard version C++ is treated as equivalent to
403 or inheriting from, for the purpose of format features supported. */
404 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
405 /* The C standard version we are checking formats against when pedantic. */
406 #define C_STD_VER ((int) (c_dialect_cxx () \
407 ? CPLUSPLUS_STD_VER \
412 : (flag_isoc94 ? STD_C94 : STD_C89)))))
413 /* The name to give to the standard version we are warning about when
414 pedantic. FEATURE_VER is the version in which the feature warned out
415 appeared, which is higher than C_STD_VER. */
416 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
417 ? (cxx_dialect < cxx11 ? "ISO C++98" \
419 : ((FEATURE_VER) == STD_EXT \
421 : ((FEATURE_VER) == STD_C2X \
424 /* Adjust a C standard version, which may be STD_C9L, to account for
425 -Wno-long-long. Returns other standard versions unchanged. */
426 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
427 ? (warn_long_long ? STD_C99 : STD_C89) \
430 /* Enum describing the kind of specifiers present in the format and
431 requiring an argument. */
432 enum format_specifier_kind
{
435 CF_KIND_FIELD_PRECISION
438 static const char *kind_descriptions
[] = {
440 N_("field width specifier"),
441 N_("field precision specifier")
444 /* Structure describing details of a type expected in format checking,
445 and the type to check against it. */
446 struct format_wanted_type
448 /* The type wanted. */
450 /* The name of this type to use in diagnostics. */
451 const char *wanted_type_name
;
452 /* Should be type checked just for scalar width identity. */
453 int scalar_identity_flag
;
454 /* The level of indirection through pointers at which this type occurs. */
456 /* Whether, when pointer_count is 1, to allow any character type when
457 pedantic, rather than just the character or void type specified. */
458 int char_lenient_flag
;
459 /* Whether the argument, dereferenced once, is written into and so the
460 argument must not be a pointer to a const-qualified type. */
462 /* Whether the argument, dereferenced once, is read from and so
463 must not be a NULL pointer. */
464 int reading_from_flag
;
465 /* The kind of specifier that this type is used for. */
466 enum format_specifier_kind kind
;
467 /* The starting character of the specifier. This never includes the
468 initial percent sign. */
469 const char *format_start
;
470 /* The length of the specifier. */
472 /* The actual parameter to check against the wanted type. */
474 /* The argument number of that parameter. */
476 /* The offset location of this argument with respect to the format
478 unsigned int offset_loc
;
479 /* The next type to check for this format conversion, or NULL if none. */
480 struct format_wanted_type
*next
;
483 /* Convenience macro for format_length_info meaning unused. */
484 #define NO_FMT NULL, FMT_LEN_none, STD_C89
486 static const format_length_info printf_length_specs
[] =
488 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
489 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
490 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
491 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
492 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
493 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
494 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
495 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
496 { "H", FMT_LEN_H
, STD_C2X
, NO_FMT
, 0 },
497 { "D", FMT_LEN_D
, STD_C2X
, "DD", FMT_LEN_DD
, STD_C2X
, 0 },
498 { NO_FMT
, NO_FMT
, 0 }
501 /* Length specifiers valid for asm_fprintf. */
502 static const format_length_info asm_fprintf_length_specs
[] =
504 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
505 { "w", FMT_LEN_w
, STD_C89
, NO_FMT
, 0 },
506 { NO_FMT
, NO_FMT
, 0 }
509 /* Length specifiers valid for GCC diagnostics. */
510 static const format_length_info gcc_diag_length_specs
[] =
512 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
513 { "w", FMT_LEN_w
, STD_C89
, NO_FMT
, 0 },
514 { NO_FMT
, NO_FMT
, 0 }
517 /* The custom diagnostics all accept the same length specifiers. */
518 #define gcc_tdiag_length_specs gcc_diag_length_specs
519 #define gcc_cdiag_length_specs gcc_diag_length_specs
520 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
521 #define gcc_dump_printf_length_specs gcc_diag_length_specs
523 /* This differs from printf_length_specs only in that "Z" is not accepted. */
524 static const format_length_info scanf_length_specs
[] =
526 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
527 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
528 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
529 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
530 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
531 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
532 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
533 { "H", FMT_LEN_H
, STD_C2X
, NO_FMT
, 0 },
534 { "D", FMT_LEN_D
, STD_C2X
, "DD", FMT_LEN_DD
, STD_C2X
, 0 },
535 { NO_FMT
, NO_FMT
, 0 }
539 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
540 make no sense for a format type not part of any C standard version. */
541 static const format_length_info strfmon_length_specs
[] =
543 /* A GNU extension. */
544 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
545 { NO_FMT
, NO_FMT
, 0 }
549 /* Length modifiers used by the fortran/error.c routines. */
550 static const format_length_info gcc_gfc_length_specs
[] =
552 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
553 { "w", FMT_LEN_w
, STD_C89
, NO_FMT
, 0 },
554 { NO_FMT
, NO_FMT
, 0 }
558 static const format_flag_spec printf_flag_specs
[] =
560 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
561 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
562 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
563 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
564 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
565 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
566 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
567 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
568 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
569 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
570 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
574 static const format_flag_pair printf_flag_pairs
[] =
578 { '0', 'p', 1, 'i' },
582 static const format_flag_spec asm_fprintf_flag_specs
[] =
584 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
585 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
586 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
587 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
588 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
589 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
590 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
591 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
592 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
595 static const format_flag_pair asm_fprintf_flag_pairs
[] =
599 { '0', 'p', 1, 'i' },
603 static const format_flag_pair gcc_diag_flag_pairs
[] =
608 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
609 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
610 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
611 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
612 #define gcc_dump_printf_flag_pairs gcc_diag_flag_pairs
614 static const format_flag_spec gcc_diag_flag_specs
[] =
616 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
617 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
618 { 'q', 0, 0, 1, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89
},
619 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
620 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
621 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
624 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
625 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
626 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
627 #define gcc_gfc_flag_specs gcc_diag_flag_specs
628 #define gcc_dump_printf_flag_specs gcc_diag_flag_specs
630 static const format_flag_spec scanf_flag_specs
[] =
632 { '*', 0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
633 { 'a', 0, 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT
},
634 { 'm', 0, 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT
},
635 { 'w', 0, 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
636 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
637 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT
},
638 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT
},
639 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
643 static const format_flag_pair scanf_flag_pairs
[] =
651 static const format_flag_spec strftime_flag_specs
[] =
653 { '_', 0, 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT
},
654 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT
},
655 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT
},
656 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT
},
657 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT
},
658 { 'w', 0, 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
659 { 'E', 0, 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99
},
660 { 'O', 0, 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99
},
661 { 'O', 'o', 0, 0, NULL
, N_("the 'O' modifier"), STD_EXT
},
662 { 'O', 'p', 0, 0, NULL
, N_("the 'O' modifier"), STD_C2X
},
663 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
667 static const format_flag_pair strftime_flag_pairs
[] =
678 static const format_flag_spec strfmon_flag_specs
[] =
680 { '=', 0, 1, 0, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
681 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89
},
682 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89
},
683 { '(', 0, 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89
},
684 { '!', 0, 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89
},
685 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89
},
686 { 'w', 0, 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
687 { '#', 0, 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
688 { 'p', 0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
689 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
690 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
693 static const format_flag_pair strfmon_flag_pairs
[] =
700 static const format_char_info print_char_table
[] =
702 /* C89 conversion specifiers. */
703 { "di", 0, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_LL
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +'I", "i", NULL
},
704 { "oxX", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
705 { "u", 0, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0'I", "i", NULL
},
706 { "fgG", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "-wp0 +#'I", "", NULL
},
707 { "eE", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "-wp0 +#I", "", NULL
},
708 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
709 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
710 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c", NULL
},
711 { "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
},
712 /* C99 conversion specifiers. */
713 { "F", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "-wp0 +#'I", "", NULL
},
714 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "-wp0 +#", "", NULL
},
715 /* C2X conversion specifiers. */
716 { "b", 0, STD_C2X
, { T2X_UI
, T2X_UC
, T2X_US
, T2X_UL
, T2X_ULL
, TEX_ULL
, T2X_ST
, T2X_UPD
, T2X_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
717 /* X/Open conversion specifiers. */
718 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
719 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
},
720 /* GNU conversion specifiers. */
721 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
},
722 { "B", 0, STD_EXT
, { T2X_UI
, T2X_UC
, T2X_US
, T2X_UL
, T2X_ULL
, TEX_ULL
, T2X_ST
, T2X_UPD
, T2X_UIM
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
723 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
726 static const format_char_info asm_fprintf_char_table
[] =
728 /* C89 conversion specifiers. */
729 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i", NULL
},
730 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
731 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i", NULL
},
732 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
733 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
735 /* asm_fprintf conversion specifiers. */
736 { "O", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
737 { "R", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
738 { "I", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
739 { "L", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
740 { "U", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
741 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", NULL
},
742 { "z", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
743 { "@", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
744 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
747 /* GCC-specific format_char_info arrays. */
749 /* The conversion specifiers implemented within pp_format, and thus supported
750 by all pretty_printer instances within GCC. */
752 #define PP_FORMAT_CHAR_TABLE \
753 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
754 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
755 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
756 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
757 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL }, \
758 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL }, \
759 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "//cR", NULL }, \
760 { "@", 1, STD_C89, { T_EVENT_PTR, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL }, \
761 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL }, \
762 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL }, \
763 { "'" , 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
764 { "{", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL }, \
765 { "}", 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
766 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL }, \
767 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL }, \
768 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_diag_char_table[0] }
770 static const format_char_info gcc_diag_char_table
[] =
772 /* The conversion specifiers implemented within pp_format. */
773 PP_FORMAT_CHAR_TABLE
,
775 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
778 static const format_char_info gcc_tdiag_char_table
[] =
780 /* The conversion specifiers implemented within pp_format. */
781 PP_FORMAT_CHAR_TABLE
,
783 /* Custom conversion specifiers implemented by default_tree_printer. */
785 /* These will require a "tree" at runtime. */
786 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
787 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
789 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
792 static const format_char_info gcc_cdiag_char_table
[] =
794 /* The conversion specifiers implemented within pp_format. */
795 PP_FORMAT_CHAR_TABLE
,
797 /* Custom conversion specifiers implemented by c_tree_printer. */
799 /* These will require a "tree" at runtime. */
800 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
801 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
803 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
805 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
808 static const format_char_info gcc_cxxdiag_char_table
[] =
810 /* The conversion specifiers implemented within pp_format. */
811 PP_FORMAT_CHAR_TABLE
,
813 /* Custom conversion specifiers implemented by cp_printer. */
815 /* These will require a "tree" at runtime. */
816 { "ADFHISTVX",1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "'", NULL
},
817 { "E", 1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "", NULL
},
819 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
820 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
822 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
825 static const format_char_info gcc_gfc_char_table
[] =
827 /* C89 conversion specifiers. */
828 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
829 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
830 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
831 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "cR", NULL
},
833 /* gfc conversion specifiers. */
835 { "C", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
837 /* This will require a "locus" at runtime. */
838 { "L", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "R", NULL
},
840 /* These will require nothing. */
841 { "<>",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
842 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
845 static const format_char_info gcc_dump_printf_char_table
[] =
847 /* The conversion specifiers implemented within pp_format. */
848 PP_FORMAT_CHAR_TABLE
,
850 /* Custom conversion specifiers implemented by dump_pretty_printer. */
852 /* E and G require a "gimple *" argument at runtime. */
853 { "EG", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
855 /* C requires a "cgraph_node *" argument at runtime. */
856 { "C", 1, STD_C89
, { T_CGRAPH_NODE
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
858 /* T requires a "tree" at runtime. */
859 { "T", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
861 /* %f requires a "double"; it doesn't support modifiers. */
862 { "f", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
864 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
867 static const format_char_info scan_char_table
[] =
869 /* C89 conversion specifiers. */
870 { "di", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, TEX_LL
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
}, "*w'I", "W", NULL
},
871 { "u", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "*w'I", "W", NULL
},
872 { "oxX", 1, STD_C89
, { T89_UI
, T99_UC
, T89_US
, T89_UL
, T9L_ULL
, TEX_ULL
, T99_ST
, T99_UPD
, T99_UIM
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
873 { "efgEG", 1, STD_C89
, { T89_F
, BADLEN
, BADLEN
, T89_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "*w'", "W", NULL
},
874 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "cW", NULL
},
875 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW", NULL
},
876 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW[", NULL
},
877 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
878 { "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
},
879 /* C99 conversion specifiers. */
880 { "F", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "*w'", "W", NULL
},
881 { "aA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
}, "*w'", "W", NULL
},
882 /* C2X conversion specifiers. */
883 { "b", 1, STD_C2X
, { T2X_UI
, T2X_UC
, T2X_US
, T2X_UL
, T2X_ULL
, TEX_ULL
, T2X_ST
, T2X_UPD
, T2X_UIM
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
884 /* X/Open conversion specifiers. */
885 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "W", NULL
},
886 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "W", NULL
},
887 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
890 static const format_char_info time_char_table
[] =
892 /* C89 conversion specifiers. */
893 { "AZa", 0, STD_C89
, NOLENGTHS
, "^#", "", NULL
},
894 { "Bb", 0, STD_C89
, NOLENGTHS
, "O^#", "p", NULL
},
895 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3", NULL
},
896 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "", NULL
},
897 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o", NULL
},
898 { "p", 0, STD_C89
, NOLENGTHS
, "#", "", NULL
},
899 { "X", 0, STD_C89
, NOLENGTHS
, "E", "", NULL
},
900 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4", NULL
},
901 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o", NULL
},
902 { "%", 0, STD_C89
, NOLENGTHS
, "", "", NULL
},
903 /* C99 conversion specifiers. */
904 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o", NULL
},
905 { "D", 0, STD_C99
, NOLENGTHS
, "", "2", NULL
},
906 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "", NULL
},
907 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "", NULL
},
908 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o", NULL
},
909 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o", NULL
},
910 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "", NULL
},
911 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o", NULL
},
912 /* GNU conversion specifiers. */
913 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "", NULL
},
914 { "P", 0, STD_EXT
, NOLENGTHS
, "", "", NULL
},
915 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
918 static const format_char_info monetary_char_table
[] =
920 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "", NULL
},
921 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
924 /* This must be in the same order as enum format_type. */
925 static const format_kind_info format_types_orig
[] =
927 { "gnu_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
928 printf_flag_specs
, printf_flag_pairs
,
929 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
930 'w', 0, 'p', 0, 'L', 0,
931 &integer_type_node
, &integer_type_node
933 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
934 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
935 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
936 'w', 0, 'p', 0, 'L', 0,
939 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q+#", NULL
,
940 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
941 FMT_FLAG_ARG_CONVERT
,
942 0, 0, 'p', 0, 'L', 0,
943 NULL
, &integer_type_node
945 { "gcc_tdiag", gcc_tdiag_length_specs
, gcc_tdiag_char_table
, "q+#", NULL
,
946 gcc_tdiag_flag_specs
, gcc_tdiag_flag_pairs
,
947 FMT_FLAG_ARG_CONVERT
,
948 0, 0, 'p', 0, 'L', 0,
949 NULL
, &integer_type_node
951 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q+#", NULL
,
952 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
953 FMT_FLAG_ARG_CONVERT
,
954 0, 0, 'p', 0, 'L', 0,
955 NULL
, &integer_type_node
957 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
958 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
959 FMT_FLAG_ARG_CONVERT
,
960 0, 0, 'p', 0, 'L', 0,
961 NULL
, &integer_type_node
963 { "gcc_gfc", gcc_gfc_length_specs
, gcc_gfc_char_table
, "q+#", NULL
,
964 gcc_gfc_flag_specs
, gcc_gfc_flag_pairs
,
965 FMT_FLAG_ARG_CONVERT
,
969 { "gcc_dump_printf", gcc_dump_printf_length_specs
,
970 gcc_dump_printf_char_table
, "q+#", NULL
,
971 gcc_dump_printf_flag_specs
, gcc_dump_printf_flag_pairs
,
972 FMT_FLAG_ARG_CONVERT
,
973 0, 0, 'p', 0, 'L', 0,
974 NULL
, &integer_type_node
976 { "NSString", NULL
, NULL
, NULL
, NULL
,
978 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
981 { "gnu_scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
982 scanf_flag_specs
, scanf_flag_pairs
,
983 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
984 'w', 0, 0, '*', 'L', 'm',
987 { "gnu_strftime", NULL
, time_char_table
, "_-0^#", "EO",
988 strftime_flag_specs
, strftime_flag_pairs
,
989 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0, 0,
992 { "gnu_strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
993 strfmon_flag_specs
, strfmon_flag_pairs
,
994 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L', 0,
999 /* This layer of indirection allows GCC to reassign format_types with
1000 new data if necessary, while still allowing the original data to be
1002 static const format_kind_info
*format_types
= format_types_orig
;
1003 /* We can modify this one. We also add target-specific format types
1004 to the end of the array. */
1005 static format_kind_info
*dynamic_format_types
;
1007 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
1009 /* Structure detailing the results of checking a format function call
1010 where the format expression may be a conditional expression with
1011 many leaves resulting from nested conditional expressions. */
1012 struct format_check_results
1014 /* Number of leaves of the format argument that could not be checked
1015 as they were not string literals. */
1016 int number_non_literal
;
1017 /* Number of leaves of the format argument that were null pointers or
1018 string literals, but had extra format arguments. */
1019 int number_extra_args
;
1020 location_t extra_arg_loc
;
1021 /* Number of leaves of the format argument that were null pointers or
1022 string literals, but had extra format arguments and used $ operand
1024 int number_dollar_extra_args
;
1025 /* Number of leaves of the format argument that were wide string
1028 /* Number of leaves of the format argument that are not array of "char". */
1029 int number_non_char
;
1030 /* Number of leaves of the format argument that were empty strings. */
1032 /* Number of leaves of the format argument that were unterminated
1034 int number_unterminated
;
1035 /* Number of leaves of the format argument that were not counted above. */
1037 /* Location of the format string. */
1038 location_t format_string_loc
;
1041 struct format_check_context
1043 format_check_results
*res
;
1044 function_format_info
*info
;
1046 vec
<location_t
> *arglocs
;
1049 /* Return the format name (as specified in the original table) for the format
1050 type indicated by format_num. */
1052 format_name (int format_num
)
1054 if (format_num
>= 0 && format_num
< n_format_types
)
1055 return format_types
[format_num
].name
;
1059 /* Return the format flags (as specified in the original table) for the format
1060 type indicated by format_num. */
1062 format_flags (int format_num
)
1064 if (format_num
>= 0 && format_num
< n_format_types
)
1065 return format_types
[format_num
].flags
;
1069 static void check_format_info (function_format_info
*, tree
,
1071 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
1072 static void check_format_info_main (format_check_results
*,
1073 function_format_info
*, const char *,
1076 unsigned HOST_WIDE_INT
,
1077 object_allocator
<format_wanted_type
> &,
1080 static void init_dollar_format_checking (int, tree
);
1081 static int maybe_read_dollar_number (const char **, int,
1082 tree
, tree
*, const format_kind_info
*);
1083 static bool avoid_dollar_number (const char *);
1084 static void finish_dollar_format_checking (format_check_results
*, int);
1086 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
1089 static void check_format_types (const substring_loc
&fmt_loc
,
1090 format_wanted_type
*,
1091 const format_kind_info
*fki
,
1092 int offset_to_type_start
,
1093 char conversion_char
,
1094 vec
<location_t
> *arglocs
);
1095 static void format_type_warning (const substring_loc
&fmt_loc
,
1096 location_t param_loc
,
1097 format_wanted_type
*, tree
,
1099 const format_kind_info
*fki
,
1100 int offset_to_type_start
,
1101 char conversion_char
);
1103 /* Decode a format type from a string, returning the type, or
1104 format_type_error if not valid, in which case the caller should
1105 print an error message. On success, when IS_RAW is non-null, set
1106 *IS_RAW when the format type corresponds to a GCC "raw" diagnostic
1107 formatting function and clear it otherwise. */
1109 decode_format_type (const char *s
, bool *is_raw
/* = NULL */)
1114 is_raw
= &is_raw_buf
;
1118 s
= convert_format_name_to_system_name (s
);
1120 size_t slen
= strlen (s
);
1121 for (int i
= 0; i
< n_format_types
; i
++)
1123 /* Check for a match with no underscores. */
1124 if (!strcmp (s
, format_types
[i
].name
))
1125 return static_cast<format_type
> (i
);
1127 /* Check for leading and trailing underscores. */
1128 size_t alen
= strlen (format_types
[i
].name
);
1129 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1130 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1131 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1132 return static_cast<format_type
>(i
);
1134 /* Check for the "_raw" suffix and no leading underscores. */
1135 if (slen
== alen
+ 4
1136 && !strncmp (s
, format_types
[i
].name
, alen
)
1137 && !strcmp (s
+ alen
, "_raw"))
1140 return static_cast<format_type
>(i
);
1143 /* Check for the "_raw__" suffix and leading underscores. */
1144 if (slen
== alen
+ 8 && s
[0] == '_' && s
[1] == '_'
1145 && !strncmp (s
+ 2, format_types
[i
].name
, alen
)
1146 && !strcmp (s
+ 2 + alen
, "_raw__"))
1149 return static_cast<format_type
>(i
);
1153 return format_type_error
;
1157 /* Check the argument list of a call to printf, scanf, etc.
1158 ATTRS are the attributes on the function type. There are NARGS argument
1159 values in the array ARGARRAY.
1160 Also, if -Wsuggest-attribute=format,
1161 warn for calls to vprintf or vscanf in functions with no such format
1162 attribute themselves. */
1165 check_function_format (const_tree fntype
, tree attrs
, int nargs
,
1166 tree
*argarray
, vec
<location_t
> *arglocs
)
1170 tree atname
= get_identifier ("format");
1172 /* See if this function has any format attributes. */
1173 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
1175 if (is_attribute_p ("format", get_attribute_name (a
)))
1177 /* Yup; check it. */
1178 function_format_info info
;
1179 decode_format_attr (fntype
, atname
, TREE_VALUE (a
), &info
,
1180 /*validated=*/true);
1183 /* FIXME: Rewrite all the internal functions in this file
1184 to use the ARGARRAY directly instead of constructing this
1186 tree params
= NULL_TREE
;
1188 for (i
= nargs
- 1; i
>= 0; i
--)
1189 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
1190 check_format_info (&info
, params
, arglocs
);
1193 /* Attempt to detect whether the current function might benefit
1194 from the format attribute if the called function is decorated
1195 with it. Avoid using calls with string literal formats for
1196 guidance since those are unlikely to be viable candidates. */
1197 if (warn_suggest_attribute_format
1198 && current_function_decl
!= NULL_TREE
1199 && info
.first_arg_num
== 0
1200 && (format_types
[info
.format_type
].flags
1201 & (int) FMT_FLAG_ARG_CONVERT
)
1202 /* c_strlen will fail for a function parameter but succeed
1203 for a literal or constant array. */
1204 && !c_strlen (argarray
[info
.format_num
- 1], 1))
1207 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1210 if (is_attribute_p ("format", TREE_PURPOSE (c
))
1211 && (decode_format_type (IDENTIFIER_POINTER
1212 (TREE_VALUE (TREE_VALUE (c
))))
1213 == info
.format_type
))
1217 /* Check if the current function has a parameter to which
1218 the format attribute could be attached; if not, it
1219 can't be a candidate for a format attribute, despite
1220 the vprintf-like or vscanf-like call. */
1222 for (args
= DECL_ARGUMENTS (current_function_decl
);
1224 args
= DECL_CHAIN (args
))
1226 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1227 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1232 warning (OPT_Wsuggest_attribute_format
, "function %qD "
1233 "might be a candidate for %qs format attribute",
1234 current_function_decl
,
1235 format_types
[info
.format_type
].name
);
1243 /* Variables used by the checking of $ operand number formats. */
1244 static char *dollar_arguments_used
= NULL
;
1245 static char *dollar_arguments_pointer_p
= NULL
;
1246 static int dollar_arguments_alloc
= 0;
1247 static int dollar_arguments_count
;
1248 static int dollar_first_arg_num
;
1249 static int dollar_max_arg_used
;
1250 static int dollar_format_warned
;
1252 /* Initialize the checking for a format string that may contain $
1253 parameter number specifications; we will need to keep track of whether
1254 each parameter has been used. FIRST_ARG_NUM is the number of the first
1255 argument that is a parameter to the format, or 0 for a vprintf-style
1256 function; PARAMS is the list of arguments starting at this argument. */
1259 init_dollar_format_checking (int first_arg_num
, tree params
)
1261 tree oparams
= params
;
1263 dollar_first_arg_num
= first_arg_num
;
1264 dollar_arguments_count
= 0;
1265 dollar_max_arg_used
= 0;
1266 dollar_format_warned
= 0;
1267 if (first_arg_num
> 0)
1271 dollar_arguments_count
++;
1272 params
= TREE_CHAIN (params
);
1275 if (dollar_arguments_alloc
< dollar_arguments_count
)
1277 free (dollar_arguments_used
);
1278 free (dollar_arguments_pointer_p
);
1279 dollar_arguments_alloc
= dollar_arguments_count
;
1280 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
1281 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
1283 if (dollar_arguments_alloc
)
1285 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1286 if (first_arg_num
> 0)
1292 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1294 params
= TREE_CHAIN (params
);
1302 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1303 is set, it is an error if one is not found; otherwise, it is OK. If
1304 such a number is found, check whether it is within range and mark that
1305 numbered operand as being used for later checking. Returns the operand
1306 number if found and within range, zero if no such number was found and
1307 this is OK, or -1 on error. PARAMS points to the first operand of the
1308 format; PARAM_PTR is made to point to the parameter referred to. If
1309 a $ format is found, *FORMAT is updated to point just after it. */
1312 maybe_read_dollar_number (const char **format
,
1313 int dollar_needed
, tree params
, tree
*param_ptr
,
1314 const format_kind_info
*fki
)
1318 const char *fcp
= *format
;
1319 if (!ISDIGIT (*fcp
))
1323 warning (OPT_Wformat_
, "missing $ operand number in format");
1331 while (ISDIGIT (*fcp
))
1333 HOST_WIDE_INT nargnum
1334 = HOST_WIDE_INT_UC (10) * argnum
+ (*fcp
- '0');
1335 if ((int) nargnum
!= nargnum
)
1344 warning (OPT_Wformat_
, "missing $ operand number in format");
1351 if (pedantic
&& !dollar_format_warned
)
1353 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
1354 C_STD_NAME (STD_EXT
));
1355 dollar_format_warned
= 1;
1357 if (overflow_flag
|| argnum
== 0
1358 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1360 warning (OPT_Wformat_
, "operand number out of range in format");
1363 if (argnum
> dollar_max_arg_used
)
1364 dollar_max_arg_used
= argnum
;
1365 /* For vprintf-style functions we may need to allocate more memory to
1366 track which arguments are used. */
1367 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1370 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1371 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
1373 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
1375 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1376 nalloc
- dollar_arguments_alloc
);
1377 dollar_arguments_alloc
= nalloc
;
1379 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1380 && dollar_arguments_used
[argnum
- 1] == 1)
1382 dollar_arguments_used
[argnum
- 1] = 2;
1383 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
1387 dollar_arguments_used
[argnum
- 1] = 1;
1388 if (dollar_first_arg_num
)
1391 *param_ptr
= params
;
1392 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1393 *param_ptr
= TREE_CHAIN (*param_ptr
);
1395 /* This case shouldn't be caught here. */
1396 gcc_assert (*param_ptr
);
1403 /* Ensure that FORMAT does not start with a decimal number followed by
1404 a $; give a diagnostic and return true if it does, false otherwise. */
1407 avoid_dollar_number (const char *format
)
1409 if (!ISDIGIT (*format
))
1411 while (ISDIGIT (*format
))
1415 warning (OPT_Wformat_
,
1416 "%<$%>operand number used after format without operand number");
1423 /* Finish the checking for a format string that used $ operand number formats
1424 instead of non-$ formats. We check for unused operands before used ones
1425 (a serious error, since the implementation of the format function
1426 can't know what types to pass to va_arg to find the later arguments).
1427 and for unused operands at the end of the format (if we know how many
1428 arguments the format had, so not for vprintf). If there were operand
1429 numbers out of range on a non-vprintf-style format, we won't have reached
1430 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1434 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1437 bool found_pointer_gap
= false;
1438 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1440 if (!dollar_arguments_used
[i
])
1442 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1443 || dollar_arguments_pointer_p
[i
]))
1444 found_pointer_gap
= true;
1446 warning_at (res
->format_string_loc
, OPT_Wformat_
,
1447 "format argument %d unused before used argument %d "
1448 "in %<$%>-style format",
1449 i
+ 1, dollar_max_arg_used
);
1452 if (found_pointer_gap
1453 || (dollar_first_arg_num
1454 && dollar_max_arg_used
< dollar_arguments_count
))
1456 res
->number_other
--;
1457 res
->number_dollar_extra_args
++;
1462 /* Retrieve the specification for a format flag. SPEC contains the
1463 specifications for format flags for the applicable kind of format.
1464 FLAG is the flag in question. If PREDICATES is NULL, the basic
1465 spec for that flag must be retrieved and must exist. If
1466 PREDICATES is not NULL, it is a string listing possible predicates
1467 for the spec entry; if an entry predicated on any of these is
1468 found, it is returned, otherwise NULL is returned. */
1470 static const format_flag_spec
*
1471 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1474 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1476 if (spec
[i
].flag_char
!= flag
)
1478 if (predicates
!= NULL
)
1480 if (spec
[i
].predicate
!= 0
1481 && strchr (predicates
, spec
[i
].predicate
) != 0)
1484 else if (spec
[i
].predicate
== 0)
1487 gcc_assert (predicates
);
1492 /* Check the argument list of a call to printf, scanf, etc.
1493 INFO points to the function_format_info structure.
1494 PARAMS is the list of argument values. */
1497 check_format_info (function_format_info
*info
, tree params
,
1498 vec
<location_t
> *arglocs
)
1500 format_check_context format_ctx
;
1501 unsigned HOST_WIDE_INT arg_num
;
1503 format_check_results res
;
1504 /* Skip to format argument. If the argument isn't available, there's
1505 no work for us to do; prototype checking will catch the problem. */
1506 for (arg_num
= 1; ; ++arg_num
)
1510 if (arg_num
== info
->format_num
)
1512 params
= TREE_CHAIN (params
);
1514 format_tree
= TREE_VALUE (params
);
1515 params
= TREE_CHAIN (params
);
1516 if (format_tree
== 0)
1519 res
.number_non_literal
= 0;
1520 res
.number_extra_args
= 0;
1521 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1522 res
.number_dollar_extra_args
= 0;
1523 res
.number_wide
= 0;
1524 res
.number_non_char
= 0;
1525 res
.number_empty
= 0;
1526 res
.number_unterminated
= 0;
1527 res
.number_other
= 0;
1528 res
.format_string_loc
= input_location
;
1530 format_ctx
.res
= &res
;
1531 format_ctx
.info
= info
;
1532 format_ctx
.params
= params
;
1533 format_ctx
.arglocs
= arglocs
;
1535 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1536 format_tree
, arg_num
);
1538 location_t loc
= format_ctx
.res
->format_string_loc
;
1540 if (res
.number_non_literal
> 0)
1542 /* Functions taking a va_list normally pass a non-literal format
1543 string. These functions typically are declared with
1544 first_arg_num == 0, so avoid warning in those cases. */
1545 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1547 /* For strftime-like formats, warn for not checking the format
1548 string; but there are no arguments to check. */
1549 warning_at (loc
, OPT_Wformat_nonliteral
,
1550 "format not a string literal, format string not checked");
1552 else if (info
->first_arg_num
!= 0)
1554 /* If there are no arguments for the format at all, we may have
1555 printf (foo) which is likely to be a security hole. */
1556 while (arg_num
+ 1 < info
->first_arg_num
)
1560 params
= TREE_CHAIN (params
);
1563 if (params
== 0 && warn_format_security
)
1564 warning_at (loc
, OPT_Wformat_security
,
1565 "format not a string literal and no format arguments");
1566 else if (params
== 0 && warn_format_nonliteral
)
1567 warning_at (loc
, OPT_Wformat_nonliteral
,
1568 "format not a string literal and no format arguments");
1570 warning_at (loc
, OPT_Wformat_nonliteral
,
1571 "format not a string literal, argument types not checked");
1575 /* If there were extra arguments to the format, normally warn. However,
1576 the standard does say extra arguments are ignored, so in the specific
1577 case where we have multiple leaves (conditional expressions or
1578 ngettext) allow extra arguments if at least one leaf didn't have extra
1579 arguments, but was otherwise OK (either non-literal or checked OK).
1580 If the format is an empty string, this should be counted similarly to the
1581 case of extra format arguments. */
1582 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1583 && res
.number_other
== 0)
1585 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1586 res
.extra_arg_loc
= loc
;
1587 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1588 "too many arguments for format");
1590 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1591 && res
.number_other
== 0)
1592 warning_at (loc
, OPT_Wformat_extra_args
,
1593 "unused arguments in %<$%>-style format");
1594 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1595 && res
.number_other
== 0)
1596 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1597 format_types
[info
->format_type
].name
);
1599 if (res
.number_wide
> 0)
1600 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1602 if (res
.number_non_char
> 0)
1603 warning_at (loc
, OPT_Wformat_
,
1604 "format string is not an array of type %qs", "char");
1606 if (res
.number_unterminated
> 0)
1607 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1610 /* Callback from check_function_arguments_recurse to check a
1611 format string. FORMAT_TREE is the format parameter. ARG_NUM
1612 is the number of the format argument. CTX points to a
1613 format_check_context. */
1616 check_format_arg (void *ctx
, tree format_tree
,
1617 unsigned HOST_WIDE_INT arg_num
)
1619 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1620 format_check_results
*res
= format_ctx
->res
;
1621 function_format_info
*info
= format_ctx
->info
;
1622 tree params
= format_ctx
->params
;
1623 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1626 HOST_WIDE_INT offset
;
1627 const char *format_chars
;
1628 tree array_size
= 0;
1631 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1633 /* Pull out a constant value if the front end didn't, and handle location
1635 format_tree
= fold_for_warn (format_tree
);
1636 STRIP_NOPS (format_tree
);
1638 if (integer_zerop (format_tree
))
1640 /* Skip to first argument to check, so we can see if this format
1641 has any arguments (it shouldn't). */
1642 while (arg_num
+ 1 < info
->first_arg_num
)
1646 params
= TREE_CHAIN (params
);
1651 res
->number_other
++;
1654 if (res
->number_extra_args
== 0)
1655 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1657 res
->number_extra_args
++;
1663 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1667 arg0
= TREE_OPERAND (format_tree
, 0);
1668 arg1
= TREE_OPERAND (format_tree
, 1);
1671 if (TREE_CODE (arg1
) == INTEGER_CST
)
1675 res
->number_non_literal
++;
1678 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1679 if (!cst_and_fits_in_hwi (arg1
))
1681 res
->number_non_literal
++;
1684 offset
= int_cst_value (arg1
);
1686 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1688 res
->number_non_literal
++;
1691 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1692 format_tree
= TREE_OPERAND (format_tree
, 0);
1693 if (format_types
[info
->format_type
].flags
1694 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1696 bool objc_str
= (info
->format_type
== gcc_objc_string_format_type
);
1697 /* We cannot examine this string here - but we can check that it is
1699 if (TREE_CODE (format_tree
) != CONST_DECL
1700 || !((objc_str
&& objc_string_ref_type_p (TREE_TYPE (format_tree
)))
1701 || (*targetcm
.string_object_ref_type_p
)
1702 ((const_tree
) TREE_TYPE (format_tree
))))
1704 res
->number_non_literal
++;
1707 /* Skip to first argument to check. */
1708 while (arg_num
+ 1 < info
->first_arg_num
)
1712 params
= TREE_CHAIN (params
);
1715 /* So, we have a valid literal string object and one or more params.
1716 We need to use an external helper to parse the string into format
1717 info. For Objective-C variants we provide the resource within the
1718 objc tree, for target variants, via a hook. */
1720 objc_check_format_arg (format_tree
, params
);
1721 else if (targetcm
.check_string_object_format_arg
)
1722 (*targetcm
.check_string_object_format_arg
) (format_tree
, params
);
1723 /* Else we can't handle it and retire quietly. */
1726 if (TREE_CODE (format_tree
) == ARRAY_REF
1727 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1728 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1729 format_tree
= TREE_OPERAND (format_tree
, 0);
1732 res
->number_non_literal
++;
1735 if (VAR_P (format_tree
)
1736 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1737 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1738 && TREE_CODE (array_init
) == STRING_CST
)
1740 /* Extract the string constant initializer. Note that this may include
1741 a trailing NUL character that is not in the array (e.g.
1742 const char a[3] = "foo";). */
1743 array_size
= DECL_SIZE_UNIT (format_tree
);
1744 format_tree
= array_init
;
1746 if (TREE_CODE (format_tree
) != STRING_CST
)
1748 res
->number_non_literal
++;
1751 tree underlying_type
1752 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
)));
1753 if (underlying_type
!= char_type_node
)
1755 if (underlying_type
== char16_type_node
1756 || underlying_type
== char32_type_node
1757 || underlying_type
== wchar_type_node
)
1760 res
->number_non_char
++;
1763 format_chars
= TREE_STRING_POINTER (format_tree
);
1764 format_length
= TREE_STRING_LENGTH (format_tree
);
1765 if (array_size
!= 0)
1767 /* Variable length arrays can't be initialized. */
1768 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1770 if (tree_fits_shwi_p (array_size
))
1772 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1773 if (array_size_value
> 0
1774 && array_size_value
== (int) array_size_value
1775 && format_length
> array_size_value
)
1776 format_length
= array_size_value
;
1781 if (offset
>= format_length
)
1783 res
->number_non_literal
++;
1786 format_chars
+= offset
;
1787 format_length
-= offset
;
1789 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1791 res
->number_unterminated
++;
1794 if (format_length
== 0)
1796 res
->number_empty
++;
1800 /* Skip to first argument to check. */
1801 while (arg_num
+ 1 < info
->first_arg_num
)
1805 params
= TREE_CHAIN (params
);
1808 /* Provisionally increment res->number_other; check_format_info_main
1809 will decrement it if it finds there are extra arguments, but this way
1810 need not adjust it for every return. */
1811 res
->number_other
++;
1812 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1813 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1814 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1817 /* Support class for argument_parser and check_format_info_main.
1818 Tracks any flag characters that have been applied to the
1819 current argument. */
1825 bool has_char_p (char ch
) const;
1826 void add_char (char ch
);
1827 void validate (const format_kind_info
*fki
,
1828 const format_char_info
*fci
,
1829 const format_flag_spec
*flag_specs
,
1830 const char * const format_chars
,
1831 tree format_string_cst
,
1832 location_t format_string_loc
,
1833 const char * const orig_format_chars
,
1836 int get_alloc_flag (const format_kind_info
*fki
);
1837 int assignment_suppression_p (const format_kind_info
*fki
);
1840 char m_flag_chars
[256];
1843 /* Support struct for argument_parser and check_format_info_main.
1844 Encapsulates any length modifier applied to the current argument. */
1846 class length_modifier
1850 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1851 scalar_identity_flag (0)
1855 length_modifier (const char *chars_
,
1856 enum format_lengths val_
,
1857 enum format_std_version std_
,
1858 int scalar_identity_flag_
)
1859 : chars (chars_
), val (val_
), std (std_
),
1860 scalar_identity_flag (scalar_identity_flag_
)
1865 enum format_lengths val
;
1866 enum format_std_version std
;
1867 int scalar_identity_flag
;
1870 /* Parsing one argument within a format string. */
1872 class argument_parser
1875 argument_parser (function_format_info
*info
, const char *&format_chars
,
1876 tree format_string_cst
,
1877 const char * const orig_format_chars
,
1878 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1879 int &has_operand_number
, tree first_fillin_param
,
1880 object_allocator
<format_wanted_type
> &fwt_pool_
,
1881 vec
<location_t
> *arglocs
);
1883 bool read_any_dollar ();
1885 bool read_format_flags ();
1888 read_any_format_width (tree
¶ms
,
1889 unsigned HOST_WIDE_INT
&arg_num
);
1892 read_any_format_left_precision ();
1895 read_any_format_precision (tree
¶ms
,
1896 unsigned HOST_WIDE_INT
&arg_num
);
1898 void handle_alloc_chars ();
1900 length_modifier
read_any_length_modifier ();
1902 void read_any_other_modifier ();
1904 const format_char_info
*find_format_char_info (char format_char
);
1907 validate_flag_pairs (const format_char_info
*fci
,
1911 give_y2k_warnings (const format_char_info
*fci
,
1914 void parse_any_scan_set (const format_char_info
*fci
);
1916 bool handle_conversions (const format_char_info
*fci
,
1917 const length_modifier
&len_modifier
,
1919 const char *&wanted_type_name
,
1920 unsigned HOST_WIDE_INT
&arg_num
,
1925 check_argument_type (const format_char_info
*fci
,
1926 const length_modifier
&len_modifier
,
1928 const char *&wanted_type_name
,
1929 const bool suppressed
,
1930 unsigned HOST_WIDE_INT
&arg_num
,
1932 const int alloc_flag
,
1933 const char * const format_start
,
1934 const char * const type_start
,
1935 location_t fmt_param_loc
,
1936 char conversion_char
);
1939 const function_format_info
*const info
;
1940 const format_kind_info
* const fki
;
1941 const format_flag_spec
* const flag_specs
;
1942 const char *start_of_this_format
;
1943 const char *&format_chars
;
1944 const tree format_string_cst
;
1945 const char * const orig_format_chars
;
1946 const location_t format_string_loc
;
1947 object_allocator
<format_wanted_type
> &fwt_pool
;
1948 flag_chars_t
&flag_chars
;
1950 tree main_arg_params
;
1951 int &has_operand_number
;
1952 const tree first_fillin_param
;
1953 format_wanted_type width_wanted_type
;
1954 format_wanted_type precision_wanted_type
;
1956 format_wanted_type main_wanted_type
;
1958 format_wanted_type
*first_wanted_type
;
1959 format_wanted_type
*last_wanted_type
;
1960 vec
<location_t
> *arglocs
;
1963 /* flag_chars_t's constructor. */
1965 flag_chars_t::flag_chars_t ()
1967 m_flag_chars
[0] = 0;
1970 /* Has CH been seen as a flag within the current argument? */
1973 flag_chars_t::has_char_p (char ch
) const
1975 return strchr (m_flag_chars
, ch
) != 0;
1978 /* Add CH to the flags seen within the current argument. */
1981 flag_chars_t::add_char (char ch
)
1983 int i
= strlen (m_flag_chars
);
1984 m_flag_chars
[i
++] = ch
;
1985 m_flag_chars
[i
] = 0;
1988 /* Validate the individual flags used, removing any that are invalid. */
1991 flag_chars_t::validate (const format_kind_info
*fki
,
1992 const format_char_info
*fci
,
1993 const format_flag_spec
*flag_specs
,
1994 const char * const format_chars
,
1995 tree format_string_cst
,
1996 location_t format_string_loc
,
1997 const char * const orig_format_chars
,
2003 bool quotflag
= false;
2005 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
2007 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2008 m_flag_chars
[i
], NULL
);
2009 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
2010 if (m_flag_chars
[i
] == fki
->length_code_char
)
2013 /* Remember if a quoting flag is seen. */
2014 quotflag
|= s
->quoting
;
2016 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
2018 format_warning_at_char (format_string_loc
, format_string_cst
,
2019 format_chars
- orig_format_chars
,
2021 "%s used with %<%%%c%> %s format",
2022 _(s
->name
), format_char
, fki
->name
);
2028 const format_flag_spec
*t
;
2029 if (ADJ_STD (s
->std
) > C_STD_VER
)
2030 warning_at (format_string_loc
, OPT_Wformat_
,
2031 "%s does not support %s",
2032 C_STD_NAME (s
->std
), _(s
->long_name
));
2033 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
2034 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
2036 const char *long_name
= (t
->long_name
!= NULL
2039 if (ADJ_STD (t
->std
) > C_STD_VER
)
2040 warning_at (format_string_loc
, OPT_Wformat_
,
2041 "%s does not support %s with"
2042 " the %<%%%c%> %s format",
2043 C_STD_NAME (t
->std
), _(long_name
),
2044 format_char
, fki
->name
);
2048 /* Detect quoting directives used within a quoted sequence, such
2049 as GCC's "%<...%qE". */
2050 if (quoted
&& s
->quoting
)
2052 format_warning_at_char (format_string_loc
, format_string_cst
,
2053 format_chars
- orig_format_chars
- 1,
2055 "%s used within a quoted sequence",
2059 m_flag_chars
[i
- d
] = 0;
2063 && strchr (fci
->flags2
, '\''))
2065 format_warning_at_char (format_string_loc
, format_string_cst
,
2066 format_chars
- orig_format_chars
,
2068 "%qc conversion used unquoted",
2073 /* Determine if an assignment-allocation has been set, requiring
2074 an extra char ** for writing back a dynamically-allocated char *.
2075 This is for handling the optional 'm' character in scanf. */
2078 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
2080 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2081 && has_char_p ('a'))
2083 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
2088 /* Determine if an assignment-suppression character was seen.
2089 ('*' in scanf, for discarding the converted input). */
2092 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
2094 if (fki
->suppression_char
2095 && has_char_p (fki
->suppression_char
))
2100 /* Constructor for argument_parser. Initialize for parsing one
2101 argument within a format string. */
2104 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
2105 tree format_string_cst_
,
2106 const char * const orig_format_chars_
,
2107 location_t format_string_loc_
,
2108 flag_chars_t
&flag_chars_
,
2109 int &has_operand_number_
,
2110 tree first_fillin_param_
,
2111 object_allocator
<format_wanted_type
> &fwt_pool_
,
2112 vec
<location_t
> *arglocs_
)
2114 fki (&format_types
[info
->format_type
]),
2115 flag_specs (fki
->flag_specs
),
2116 start_of_this_format (format_chars_
),
2117 format_chars (format_chars_
),
2118 format_string_cst (format_string_cst_
),
2119 orig_format_chars (orig_format_chars_
),
2120 format_string_loc (format_string_loc_
),
2121 fwt_pool (fwt_pool_
),
2122 flag_chars (flag_chars_
),
2124 main_arg_params (NULL
),
2125 has_operand_number (has_operand_number_
),
2126 first_fillin_param (first_fillin_param_
),
2127 first_wanted_type (NULL
),
2128 last_wanted_type (NULL
),
2133 /* Handle dollars at the start of format arguments, setting up main_arg_params
2136 Return true if format parsing is to continue, false otherwise. */
2139 argument_parser::read_any_dollar ()
2141 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
2143 /* Possibly read a $ operand number at the start of the format.
2144 If one was previously used, one is required here. If one
2145 is not used here, we can't immediately conclude this is a
2146 format without them, since it could be printf %m or scanf %*. */
2148 opnum
= maybe_read_dollar_number (&format_chars
, 0,
2150 &main_arg_params
, fki
);
2155 has_operand_number
= 1;
2156 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
2159 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
2161 if (avoid_dollar_number (format_chars
))
2167 /* Read any format flags, but do not yet validate them beyond removing
2168 duplicates, since in general validation depends on the rest of
2171 Return true if format parsing is to continue, false otherwise. */
2174 argument_parser::read_format_flags ()
2176 while (*format_chars
!= 0
2177 && strchr (fki
->flag_chars
, *format_chars
) != 0)
2179 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2180 *format_chars
, NULL
);
2181 if (flag_chars
.has_char_p (*format_chars
))
2183 format_warning_at_char (format_string_loc
, format_string_cst
,
2184 format_chars
+ 1 - orig_format_chars
,
2186 "repeated %s in format", _(s
->name
));
2189 flag_chars
.add_char (*format_chars
);
2191 if (s
->skip_next_char
)
2194 if (*format_chars
== 0)
2196 warning_at (format_string_loc
, OPT_Wformat_
,
2197 "missing fill character at end of strfmon format");
2207 /* Read any format width, possibly * or *m$.
2209 Return true if format parsing is to continue, false otherwise. */
2213 read_any_format_width (tree
¶ms
,
2214 unsigned HOST_WIDE_INT
&arg_num
)
2216 if (!fki
->width_char
)
2219 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
2221 flag_chars
.add_char (fki
->width_char
);
2222 /* "...a field width...may be indicated by an asterisk.
2223 In this case, an int argument supplies the field width..." */
2225 if (has_operand_number
!= 0)
2228 opnum
= maybe_read_dollar_number (&format_chars
,
2229 has_operand_number
== 1,
2236 has_operand_number
= 1;
2237 arg_num
= opnum
+ info
->first_arg_num
- 1;
2240 has_operand_number
= 0;
2244 if (avoid_dollar_number (format_chars
))
2247 if (info
->first_arg_num
!= 0)
2254 cur_param
= TREE_VALUE (params
);
2255 if (has_operand_number
<= 0)
2257 params
= TREE_CHAIN (params
);
2261 width_wanted_type
.wanted_type
= *fki
->width_type
;
2262 width_wanted_type
.wanted_type_name
= NULL
;
2263 width_wanted_type
.pointer_count
= 0;
2264 width_wanted_type
.char_lenient_flag
= 0;
2265 width_wanted_type
.scalar_identity_flag
= 0;
2266 width_wanted_type
.writing_in_flag
= 0;
2267 width_wanted_type
.reading_from_flag
= 0;
2268 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
2269 width_wanted_type
.format_start
= format_chars
- 1;
2270 width_wanted_type
.format_length
= 1;
2271 width_wanted_type
.param
= cur_param
;
2272 width_wanted_type
.arg_num
= arg_num
;
2273 width_wanted_type
.offset_loc
=
2274 format_chars
- orig_format_chars
;
2275 width_wanted_type
.next
= NULL
;
2276 if (last_wanted_type
!= 0)
2277 last_wanted_type
->next
= &width_wanted_type
;
2278 if (first_wanted_type
== 0)
2279 first_wanted_type
= &width_wanted_type
;
2280 last_wanted_type
= &width_wanted_type
;
2285 /* Possibly read a numeric width. If the width is zero,
2286 we complain if appropriate. */
2287 int non_zero_width_char
= FALSE
;
2288 int found_width
= FALSE
;
2289 while (ISDIGIT (*format_chars
))
2292 if (*format_chars
!= '0')
2293 non_zero_width_char
= TRUE
;
2296 if (found_width
&& !non_zero_width_char
&&
2297 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
2298 warning_at (format_string_loc
, OPT_Wformat_
,
2299 "zero width in %s format", fki
->name
);
2301 flag_chars
.add_char (fki
->width_char
);
2307 /* Read any format left precision (must be a number, not *). */
2309 argument_parser::read_any_format_left_precision ()
2311 if (fki
->left_precision_char
== 0)
2313 if (*format_chars
!= '#')
2317 flag_chars
.add_char (fki
->left_precision_char
);
2318 if (!ISDIGIT (*format_chars
))
2319 format_warning_at_char (format_string_loc
, format_string_cst
,
2320 format_chars
- orig_format_chars
,
2322 "empty left precision in %s format", fki
->name
);
2323 while (ISDIGIT (*format_chars
))
2327 /* Read any format precision, possibly * or *m$.
2329 Return true if format parsing is to continue, false otherwise. */
2333 read_any_format_precision (tree
¶ms
,
2334 unsigned HOST_WIDE_INT
&arg_num
)
2336 if (fki
->precision_char
== 0)
2338 if (*format_chars
!= '.')
2342 flag_chars
.add_char (fki
->precision_char
);
2343 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
2345 /* "...a...precision...may be indicated by an asterisk.
2346 In this case, an int argument supplies the...precision." */
2348 if (has_operand_number
!= 0)
2351 opnum
= maybe_read_dollar_number (&format_chars
,
2352 has_operand_number
== 1,
2359 has_operand_number
= 1;
2360 arg_num
= opnum
+ info
->first_arg_num
- 1;
2363 has_operand_number
= 0;
2367 if (avoid_dollar_number (format_chars
))
2370 if (info
->first_arg_num
!= 0)
2377 cur_param
= TREE_VALUE (params
);
2378 if (has_operand_number
<= 0)
2380 params
= TREE_CHAIN (params
);
2384 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
2385 precision_wanted_type
.wanted_type_name
= NULL
;
2386 precision_wanted_type
.pointer_count
= 0;
2387 precision_wanted_type
.char_lenient_flag
= 0;
2388 precision_wanted_type
.scalar_identity_flag
= 0;
2389 precision_wanted_type
.writing_in_flag
= 0;
2390 precision_wanted_type
.reading_from_flag
= 0;
2391 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
2392 precision_wanted_type
.param
= cur_param
;
2393 precision_wanted_type
.format_start
= format_chars
- 2;
2394 precision_wanted_type
.format_length
= 2;
2395 precision_wanted_type
.arg_num
= arg_num
;
2396 precision_wanted_type
.offset_loc
=
2397 format_chars
- orig_format_chars
;
2398 precision_wanted_type
.next
= NULL
;
2399 if (last_wanted_type
!= 0)
2400 last_wanted_type
->next
= &precision_wanted_type
;
2401 if (first_wanted_type
== 0)
2402 first_wanted_type
= &precision_wanted_type
;
2403 last_wanted_type
= &precision_wanted_type
;
2408 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
2409 && !ISDIGIT (*format_chars
))
2410 format_warning_at_char (format_string_loc
, format_string_cst
,
2411 format_chars
- orig_format_chars
,
2413 "empty precision in %s format", fki
->name
);
2414 while (ISDIGIT (*format_chars
))
2421 /* Parse any assignment-allocation flags, which request an extra
2422 char ** for writing back a dynamically-allocated char *.
2423 This is for handling the optional 'm' character in scanf,
2424 and, before C99, 'a' (for compatibility with a non-standard
2425 GNU libc extension). */
2428 argument_parser::handle_alloc_chars ()
2430 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
2432 flag_chars
.add_char (fki
->alloc_char
);
2436 /* Handle the scanf allocation kludge. */
2437 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2439 if (*format_chars
== 'a' && !flag_isoc99
)
2441 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
2442 || format_chars
[1] == '[')
2444 /* 'a' is used as a flag. */
2445 flag_chars
.add_char ('a');
2452 /* Look for length modifiers within the current format argument,
2453 returning a length_modifier instance describing it (or the
2454 default if one is not found).
2456 Issue warnings about non-standard modifiers. */
2459 argument_parser::read_any_length_modifier ()
2461 length_modifier result
;
2463 const format_length_info
*fli
= fki
->length_char_specs
;
2467 while (fli
->name
!= 0
2468 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
2472 format_chars
+= strlen (fli
->name
);
2473 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
2476 result
= length_modifier (fli
->double_name
, fli
->double_index
,
2477 fli
->double_std
, 0);
2481 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
2482 fli
->scalar_identity_flag
);
2484 flag_chars
.add_char (fki
->length_code_char
);
2488 /* Warn if the length modifier is non-standard. */
2489 if (ADJ_STD (result
.std
) > C_STD_VER
)
2490 warning_at (format_string_loc
, OPT_Wformat_
,
2491 "%s does not support the %qs %s length modifier",
2492 C_STD_NAME (result
.std
), result
.chars
,
2499 /* Read any other modifier (strftime E/O). */
2502 argument_parser::read_any_other_modifier ()
2504 if (fki
->modifier_chars
== NULL
)
2507 while (*format_chars
!= 0
2508 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2510 if (flag_chars
.has_char_p (*format_chars
))
2512 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2513 *format_chars
, NULL
);
2514 format_warning_at_char (format_string_loc
, format_string_cst
,
2515 format_chars
- orig_format_chars
,
2517 "repeated %s in format", _(s
->name
));
2520 flag_chars
.add_char (*format_chars
);
2525 /* Return the format_char_info corresponding to FORMAT_CHAR,
2526 potentially issuing a warning if the format char is
2527 not supported in the C standard version we are checking
2530 Issue a warning and return NULL if it is not found.
2532 Issue warnings about non-standard modifiers. */
2534 const format_char_info
*
2535 argument_parser::find_format_char_info (char format_char
)
2537 const format_char_info
*fci
= fki
->conversion_specs
;
2539 while (fci
->format_chars
!= 0
2540 && strchr (fci
->format_chars
, format_char
) == 0)
2542 if (fci
->format_chars
== 0)
2544 format_warning_at_char (format_string_loc
, format_string_cst
,
2545 format_chars
- orig_format_chars
,
2547 "unknown conversion type character"
2555 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2556 format_warning_at_char (format_string_loc
, format_string_cst
,
2557 format_chars
- orig_format_chars
,
2559 "%s does not support the %<%%%c%> %s format",
2560 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2566 /* Validate the pairs of flags used.
2567 Issue warnings about incompatible combinations of flags. */
2570 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2573 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2575 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2577 const format_flag_spec
*s
, *t
;
2578 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2580 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2582 if (bad_flag_pairs
[i
].predicate
!= 0
2583 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2585 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2586 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2587 if (bad_flag_pairs
[i
].ignored
)
2589 if (bad_flag_pairs
[i
].predicate
!= 0)
2590 warning_at (format_string_loc
, OPT_Wformat_
,
2591 "%s ignored with %s and %<%%%c%> %s format",
2592 _(s
->name
), _(t
->name
), format_char
,
2595 warning_at (format_string_loc
, OPT_Wformat_
,
2596 "%s ignored with %s in %s format",
2597 _(s
->name
), _(t
->name
), fki
->name
);
2601 if (bad_flag_pairs
[i
].predicate
!= 0)
2602 warning_at (format_string_loc
, OPT_Wformat_
,
2603 "use of %s and %s together with %<%%%c%> %s format",
2604 _(s
->name
), _(t
->name
), format_char
,
2607 warning_at (format_string_loc
, OPT_Wformat_
,
2608 "use of %s and %s together in %s format",
2609 _(s
->name
), _(t
->name
), fki
->name
);
2614 /* Give Y2K warnings. */
2617 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2620 if (!warn_format_y2k
)
2624 if (strchr (fci
->flags2
, '4') != 0)
2625 if (flag_chars
.has_char_p ('E'))
2629 else if (strchr (fci
->flags2
, '3') != 0)
2631 else if (strchr (fci
->flags2
, '2') != 0)
2634 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2635 "%<%%%c%> yields only last 2 digits of "
2636 "year in some locales", format_char
);
2637 else if (y2k_level
== 2)
2638 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2639 "%<%%%c%> yields only last 2 digits of year",
2643 /* Parse any "scan sets" enclosed in square brackets, e.g.
2644 for scanf-style calls. */
2647 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2649 if (strchr (fci
->flags2
, '[') == NULL
)
2652 /* Skip over scan set, in case it happens to have '%' in it. */
2653 if (*format_chars
== '^')
2655 /* Find closing bracket; if one is hit immediately, then
2656 it's part of the scan set rather than a terminator. */
2657 if (*format_chars
== ']')
2659 while (*format_chars
&& *format_chars
!= ']')
2661 if (*format_chars
!= ']')
2662 /* The end of the format string was reached. */
2663 format_warning_at_char (format_string_loc
, format_string_cst
,
2664 format_chars
- orig_format_chars
,
2666 "no closing %<]%> for %<%%[%> format");
2669 /* Return true if this argument is to be continued to be parsed,
2670 false to skip to next argument. */
2673 argument_parser::handle_conversions (const format_char_info
*fci
,
2674 const length_modifier
&len_modifier
,
2676 const char *&wanted_type_name
,
2677 unsigned HOST_WIDE_INT
&arg_num
,
2681 enum format_std_version wanted_type_std
;
2683 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2686 wanted_type
= (fci
->types
[len_modifier
.val
].type
2687 ? *fci
->types
[len_modifier
.val
].type
: 0);
2688 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2689 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2690 if (wanted_type
== 0)
2692 format_warning_at_char (format_string_loc
, format_string_cst
,
2693 format_chars
- orig_format_chars
,
2695 "use of %qs length modifier with %qc type"
2696 " character has either no effect"
2697 " or undefined behavior",
2698 len_modifier
.chars
, format_char
);
2699 /* Heuristic: skip one argument when an invalid length/type
2700 combination is encountered. */
2703 params
= TREE_CHAIN (params
);
2707 /* Warn if non-standard, provided it is more non-standard
2708 than the length and type characters that may already
2709 have been warned for. */
2710 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2711 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2713 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2714 format_warning_at_char (format_string_loc
, format_string_cst
,
2715 format_chars
- orig_format_chars
,
2717 "%s does not support the %<%%%s%c%> %s format",
2718 C_STD_NAME (wanted_type_std
),
2720 format_char
, fki
->name
);
2726 /* Check type of argument against desired type.
2728 Return true if format parsing is to continue, false otherwise. */
2732 check_argument_type (const format_char_info
*fci
,
2733 const length_modifier
&len_modifier
,
2735 const char *&wanted_type_name
,
2736 const bool suppressed
,
2737 unsigned HOST_WIDE_INT
&arg_num
,
2739 const int alloc_flag
,
2740 const char * const format_start
,
2741 const char * const type_start
,
2742 location_t fmt_param_loc
,
2743 char conversion_char
)
2745 if (info
->first_arg_num
== 0)
2748 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2751 if (main_arg_num
!= 0)
2754 warning_at (format_string_loc
, OPT_Wformat_
,
2755 "operand number specified with "
2756 "suppressed assignment");
2758 warning_at (format_string_loc
, OPT_Wformat_
,
2759 "operand number specified for format "
2760 "taking no argument");
2765 format_wanted_type
*wanted_type_ptr
;
2767 if (main_arg_num
!= 0)
2769 arg_num
= main_arg_num
;
2770 params
= main_arg_params
;
2775 if (has_operand_number
> 0)
2777 warning_at (format_string_loc
, OPT_Wformat_
,
2778 "missing $ operand number in format");
2782 has_operand_number
= 0;
2785 wanted_type_ptr
= &main_wanted_type
;
2793 cur_param
= TREE_VALUE (params
);
2794 params
= TREE_CHAIN (params
);
2797 wanted_type_ptr
->wanted_type
= wanted_type
;
2798 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2799 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2800 wanted_type_ptr
->char_lenient_flag
= 0;
2801 if (strchr (fci
->flags2
, 'c') != 0)
2802 wanted_type_ptr
->char_lenient_flag
= 1;
2803 wanted_type_ptr
->scalar_identity_flag
= 0;
2804 if (len_modifier
.scalar_identity_flag
)
2805 wanted_type_ptr
->scalar_identity_flag
= 1;
2806 wanted_type_ptr
->writing_in_flag
= 0;
2807 wanted_type_ptr
->reading_from_flag
= 0;
2809 wanted_type_ptr
->writing_in_flag
= 1;
2812 if (strchr (fci
->flags2
, 'W') != 0)
2813 wanted_type_ptr
->writing_in_flag
= 1;
2814 if (strchr (fci
->flags2
, 'R') != 0)
2815 wanted_type_ptr
->reading_from_flag
= 1;
2817 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2818 wanted_type_ptr
->param
= cur_param
;
2819 wanted_type_ptr
->arg_num
= arg_num
;
2820 wanted_type_ptr
->format_start
= format_start
;
2821 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2822 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2823 wanted_type_ptr
->next
= NULL
;
2824 if (last_wanted_type
!= 0)
2825 last_wanted_type
->next
= wanted_type_ptr
;
2826 if (first_wanted_type
== 0)
2827 first_wanted_type
= wanted_type_ptr
;
2828 last_wanted_type
= wanted_type_ptr
;
2833 wanted_type_ptr
= fwt_pool
.allocate ();
2835 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2836 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2841 if (first_wanted_type
!= 0)
2843 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2844 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2845 /* By default, use the end of the range for the caret location. */
2846 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2847 offset_to_format_end
,
2848 offset_to_format_start
, offset_to_format_end
);
2849 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2850 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2851 offset_to_type_start
,
2852 conversion_char
, arglocs
);
2858 /* Describes "paired tokens" within the format string that are
2859 expected to be balanced. */
2864 baltoks_t (): singlequote (), doublequote () { }
2866 typedef auto_vec
<const char *> balanced_tokens_t
;
2867 /* Vectors of pointers to opening brackets ('['), curly brackets ('{'),
2868 quoting directives (like GCC "%<"), parentheses, and angle brackets
2869 ('<'). Used to detect unbalanced tokens. */
2870 balanced_tokens_t brackets
;
2871 balanced_tokens_t curly
;
2872 balanced_tokens_t quotdirs
;
2873 balanced_tokens_t parens
;
2874 balanced_tokens_t pointy
;
2875 /* Pointer to the last opening quote. */
2876 const char *singlequote
;
2877 const char *doublequote
;
2880 /* Describes a keyword, operator, or other name. */
2884 const char *name
; /* Keyword/operator name. */
2885 unsigned char len
; /* Its length. */
2886 const char *alt
; /* Alternate spelling. */
2889 /* Helper for initializing global token_t arrays below. */
2890 #define NAME(name) { name, sizeof name - 1, NULL }
2892 /* C/C++ operators that are expected to be quoted within the format
2895 static const token_t c_opers
[] =
2897 NAME ("!="), NAME ("%="), NAME ("&&"), NAME ("&="), NAME ("*="),
2898 NAME ("++"), NAME ("+="), NAME ("--"), NAME ("-="), NAME ("->"),
2899 NAME ("/="), NAME ("<<"), NAME ("<<="), NAME ("<="), NAME ("=="),
2900 NAME (">="), NAME (">>="), NAME (">>"), NAME ("?:"), NAME ("^="),
2901 NAME ("|="), NAME ("||")
2904 static const token_t cxx_opers
[] =
2906 NAME ("->*"), NAME (".*"), NAME ("::"), NAME ("<=>")
2909 /* Common C/C++ keywords that are expected to be quoted within the format
2910 string. Keywords like auto, inline, or volatile are exccluded because
2911 they are sometimes used in common terms like /auto variables/, /inline
2912 function/, or /volatile access/ where they should not be quoted. */
2914 static const token_t c_keywords
[] =
2917 #define NAME(name, alt) { name, sizeof name - 1, alt }
2919 NAME ("alignas", NULL
),
2920 NAME ("alignof", NULL
),
2922 NAME ("bool", NULL
),
2923 NAME ("char", NULL
),
2924 NAME ("const %", NULL
),
2925 NAME ("const-qualified", "%<const%>-qualified"),
2926 NAME ("float", NULL
),
2927 NAME ("ifunc", NULL
),
2929 NAME ("long double", NULL
),
2930 NAME ("long int", NULL
),
2931 NAME ("long long", NULL
),
2932 NAME ("malloc", NULL
),
2933 NAME ("noclone", NULL
),
2934 NAME ("noinline", NULL
),
2935 NAME ("nonnull", NULL
),
2936 NAME ("noreturn", NULL
),
2937 NAME ("nothrow", NULL
),
2938 NAME ("offsetof", NULL
),
2939 NAME ("readonly", "read-only"),
2940 NAME ("readwrite", "read-write"),
2941 NAME ("restrict %", NULL
),
2942 NAME ("restrict-qualified", "%<restrict%>-qualified"),
2943 NAME ("short int", NULL
),
2944 NAME ("signed char", NULL
),
2945 NAME ("signed int", NULL
),
2946 NAME ("signed long", NULL
),
2947 NAME ("signed short", NULL
),
2948 NAME ("sizeof", NULL
),
2949 NAME ("typeof", NULL
),
2950 NAME ("unsigned char", NULL
),
2951 NAME ("unsigned int", NULL
),
2952 NAME ("unsigned long", NULL
),
2953 NAME ("unsigned short", NULL
),
2954 NAME ("volatile %", NULL
),
2955 NAME ("volatile-qualified", "%<volatile%>-qualified"),
2956 NAME ("weakref", NULL
),
2959 static const token_t cxx_keywords
[] =
2961 /* C++ only keywords and operators. */
2962 NAME ("catch", NULL
),
2963 NAME ("constexpr if", NULL
),
2964 NAME ("constexpr", NULL
),
2965 NAME ("constinit", NULL
),
2966 NAME ("consteval", NULL
),
2967 NAME ("decltype", NULL
),
2968 NAME ("nullptr", NULL
),
2969 NAME ("operator delete", NULL
),
2970 NAME ("operator new", NULL
),
2971 NAME ("typeid", NULL
),
2972 NAME ("typeinfo", NULL
)
2975 /* Blacklisted words such as misspellings that should be avoided in favor
2976 of the specified alternatives. */
2979 const char *name
; /* Bad word. */
2980 unsigned char len
; /* Its length. */
2981 const char *alt
; /* Preferred alternative. */
2984 NAME ("arg", "argument"),
2985 NAME ("bitfield", "bit-field"),
2986 NAME ("builtin function", "built-in function"),
2987 NAME ("can not", "cannot"),
2988 NAME ("commandline option", "command-line option"),
2989 NAME ("commandline", "command line"),
2990 NAME ("command line option", "command-line option"),
2991 NAME ("decl", "declaration"),
2992 NAME ("enumeral", "enumerated"),
2993 NAME ("floating point", "floating-point"),
2994 NAME ("nonstatic", "non-static"),
2995 NAME ("non-zero", "nonzero"),
2996 NAME ("reg", "register"),
2997 NAME ("stmt", "statement"),
3000 /* Common contractions that should be avoided in favor of the specified
3005 const char *name
; /* Contraction. */
3006 unsigned char len
; /* Its length. */
3007 const char *alt
; /* Preferred alternative. */
3010 NAME ("can't", "cannot"),
3011 NAME ("didn't", "did not"),
3012 /* These are commonly abused. Avoid diagnosing them for now.
3013 NAME ("isn't", "is not"),
3014 NAME ("don't", "is not"),
3016 NAME ("mustn't", "must not"),
3017 NAME ("needn't", "need not"),
3018 NAME ("should't", "should not"),
3019 NAME ("that's", "that is"),
3020 NAME ("there's", "there is"),
3021 NAME ("they're", "they are"),
3022 NAME ("what's", "what is"),
3023 NAME ("won't", "will not")
3026 /* Check for unquoted TOKENS. FORMAT_STRING_LOC is the location of
3027 the format string, FORMAT_STRING_CST the format string itself (as
3028 a tree), ORIG_FORMAT_CHARS and FORMAT_CHARS are pointers to
3029 the beginning of the format string and the character currently
3030 being processed, and BALTOKS describes paired "tokens" within
3031 the format string that are expected to be balanced.
3032 Returns a pointer to the last processed character or null when
3033 nothing was done. */
3036 check_tokens (const token_t
*tokens
, unsigned ntoks
,
3037 location_t format_string_loc
, tree format_string_cst
,
3038 const char *orig_format_chars
, const char *format_chars
,
3042 const int opt
= OPT_Wformat_diag
;
3043 /* Zero-based starting position of a problem sequence. */
3044 int fmtchrpos
= format_chars
- orig_format_chars
;
3046 /* For identifier-like "words," set to the word length. */
3048 /* Set for an operator, clear for an identifier/word. */
3049 bool is_oper
= false;
3050 bool underscore
= false;
3052 if (format_chars
[0] == '_' || ISALPHA (format_chars
[0]))
3054 while (format_chars
[wlen
] == '_' || ISALNUM (format_chars
[wlen
]))
3056 underscore
|= format_chars
[wlen
] == '_';
3063 for (unsigned i
= 0; i
!= ntoks
; ++i
)
3065 unsigned toklen
= tokens
[i
].len
;
3068 || strncmp (format_chars
, tokens
[i
].name
, toklen
))
3072 && format_chars
- orig_format_chars
> 0
3073 && (TOUPPER (format_chars
[-1]) == 'C'
3074 || TOUPPER (format_chars
[-1]) == 'G'))
3075 return format_chars
+ toklen
- 1; /* Reference to C++ or G++. */
3077 if (ISPUNCT (format_chars
[toklen
- 1]))
3079 if (format_chars
[toklen
- 1] == format_chars
[toklen
])
3080 return NULL
; /* Operator followed by another punctuator. */
3082 else if (ISALNUM (format_chars
[toklen
]))
3083 return NULL
; /* Keyword prefix for a longer word. */
3086 && format_chars
[0] == '-'
3087 && format_chars
[1] == '-'
3088 && ISALNUM (format_chars
[2]))
3089 return NULL
; /* Probably option like --help. */
3091 /* Allow this ugly warning for the time being. */
3093 && format_chars
- orig_format_chars
> 6
3094 && startswith (format_chars
- 7, " count >= width of "))
3095 return format_chars
+ 10;
3097 /* The token is a type if it ends in an alphabetic character. */
3098 bool is_type
= (ISALPHA (tokens
[i
].name
[toklen
- 1])
3099 && strchr (tokens
[i
].name
, ' '));
3101 /* Backtrack to the last alphabetic character (for tokens whose
3102 names end in '%'). */
3104 while (!ISALPHA (tokens
[i
].name
[toklen
- 1]))
3107 if (format_warning_substr (format_string_loc
, format_string_cst
,
3108 fmtchrpos
, fmtchrpos
+ toklen
, opt
,
3110 ? G_("unquoted type name %<%.*s%> in format")
3112 ? G_("unquoted operator %<%.*s%> in format")
3113 : G_("unquoted keyword %<%.*s%> in format"))),
3114 toklen
, format_chars
)
3116 inform (format_string_loc
, "use %qs instead", tokens
[i
].alt
);
3118 return format_chars
+ toklen
- 1;
3121 /* Diagnose unquoted __attribute__. Consider any parenthesized
3122 argument to the attribute to avoid redundant warnings for
3123 the double parentheses that might follow. */
3124 if (startswith (format_chars
, "__attribute"))
3126 unsigned nchars
= sizeof "__attribute" - 1;
3127 while ('_' == format_chars
[nchars
])
3130 for (int i
= nchars
; format_chars
[i
]; ++i
)
3131 if (' ' != format_chars
[i
])
3137 if (format_chars
[nchars
] == '(')
3139 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3143 if (format_chars
[nchars
] == '(')
3145 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3149 for (int i
= nchars
; format_chars
[i
]; ++i
)
3150 if (')' == format_chars
[i
])
3152 if (baltoks
.parens
.length () > 0)
3153 baltoks
.parens
.pop ();
3158 if (close
&& format_chars
[nchars
] == ')')
3160 if (baltoks
.parens
.length () > 0)
3161 baltoks
.parens
.pop ();
3166 format_warning_substr (format_string_loc
, format_string_cst
,
3167 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3168 "unquoted attribute in format");
3169 return format_chars
+ nchars
- 1;
3172 /* Diagnose unquoted built-ins. */
3173 if (format_chars
[0] == '_'
3174 && format_chars
[1] == '_'
3175 && (startswith (format_chars
+ 2, "atomic")
3176 || startswith (format_chars
+ 2, "builtin")
3177 || startswith (format_chars
+ 2, "sync")))
3179 format_warning_substr (format_string_loc
, format_string_cst
,
3180 fmtchrpos
, fmtchrpos
+ wlen
, opt
,
3181 "unquoted name of built-in function %<%.*s%> "
3183 wlen
, format_chars
);
3184 return format_chars
+ wlen
- 1;
3187 /* Diagnose unquoted substrings of alphanumeric characters containing
3188 underscores. They most likely refer to identifiers and should be
3191 format_warning_substr (format_string_loc
, format_string_cst
,
3192 format_chars
- orig_format_chars
,
3193 format_chars
+ wlen
- orig_format_chars
,
3195 "unquoted identifier or keyword %<%.*s%> in format",
3196 wlen
, format_chars
);
3199 /* Diagnose some common missspellings. */
3200 for (unsigned i
= 0; i
!= sizeof badwords
/ sizeof *badwords
; ++i
)
3202 unsigned badwlen
= strspn (badwords
[i
].name
, " -");
3204 && (wlen
<= badwords
[i
].len
3205 || (wlen
== badwords
[i
].len
+ 1U
3206 && TOUPPER (format_chars
[wlen
- 1]) == 'S'))
3207 && !strncasecmp (format_chars
, badwords
[i
].name
, badwords
[i
].len
))
3209 /* Handle singular as well as plural forms of all bad words
3210 even though the latter don't necessarily make sense for
3211 all of the former (like "can nots"). */
3212 badwlen
= badwords
[i
].len
;
3213 const char *plural
= "";
3214 if (TOUPPER (format_chars
[badwlen
]) == 'S')
3220 format_warning_substr (format_string_loc
, format_string_cst
,
3221 fmtchrpos
, fmtchrpos
+ badwords
[i
].len
,
3223 "misspelled term %<%.*s%> in format; "
3224 "use %<%s%s%> instead",
3225 badwlen
, format_chars
,
3226 badwords
[i
].alt
, plural
);
3228 return format_chars
+ badwords
[i
].len
- 1;
3233 if (!strncasecmp (format_chars
, "c++", 3)
3234 || !strncasecmp (format_chars
, "g++", 3))
3235 return format_chars
+ 2;
3238 return wlen
? format_chars
+ wlen
- 1 : NULL
;
3241 /* Check plain text in a format string of a GCC diagnostic function
3242 for common quoting, punctuation, and spelling mistakes, and issue
3243 -Wformat-diag warnings if they are found. FORMAT_STRING_LOC is
3244 the location of the format string, FORMAT_STRING_CST the format
3245 string itself (as a tree), ORIG_FORMAT_CHARS and FORMAT_CHARS are
3246 pointers to the beginning of the format string and the character
3247 currently being processed, and BALTOKS describes paired "tokens"
3248 within the format string that are expected to be balanced.
3249 Returns a pointer to the last processed character. */
3252 check_plain (location_t format_string_loc
, tree format_string_cst
,
3253 const char *orig_format_chars
, const char *format_chars
,
3257 const int opt
= OPT_Wformat_diag
;
3258 /* Zero-based starting position of a problem sequence. */
3259 int fmtchrpos
= format_chars
- orig_format_chars
;
3261 if (*format_chars
== '%')
3263 /* Diagnose %<%s%> and suggest using %qs instead. */
3264 if (startswith (format_chars
, "%<%s%>"))
3265 format_warning_substr (format_string_loc
, format_string_cst
,
3266 fmtchrpos
, fmtchrpos
+ 6, opt
,
3267 "quoted %qs directive in format; "
3268 "use %qs instead", "%s", "%qs");
3269 else if (format_chars
- orig_format_chars
> 2
3270 && !strncasecmp (format_chars
- 3, "can%'t", 6))
3271 format_warning_substr (format_string_loc
,
3273 fmtchrpos
- 3, fmtchrpos
+ 3, opt
,
3274 "contraction %<%.*s%> in format; "
3276 6, format_chars
- 3, "cannot");
3278 return format_chars
;
3281 if (baltoks
.quotdirs
.length ())
3283 /* Skip over all plain text within a quoting directive until
3284 the next directive. */
3285 while (*format_chars
&& '%' != *format_chars
)
3288 return format_chars
;
3291 /* The length of the problem sequence. */
3294 /* Diagnose any whitespace characters other than <space> but only
3295 leading, trailing, and two or more consecutive <space>s. Do
3296 this before diagnosing control characters because whitespace
3297 is a subset of controls. */
3298 const char *other_than_space
= NULL
;
3299 while (ISSPACE (format_chars
[nchars
]))
3301 if (format_chars
[nchars
] != ' ' && !other_than_space
)
3302 other_than_space
= format_chars
+ nchars
;
3308 /* This is the most common problem: go the extra mile to describe
3309 the problem in as much helpful detail as possible. */
3310 if (other_than_space
)
3312 format_warning_substr (format_string_loc
, format_string_cst
,
3313 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3314 "unquoted whitespace character %qc in format",
3316 return format_chars
+ nchars
- 1;
3320 /* Accept strings of leading spaces with no warning. */
3321 return format_chars
+ nchars
- 1;
3323 if (!format_chars
[nchars
])
3325 format_warning_substr (format_string_loc
, format_string_cst
,
3326 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3327 "spurious trailing space in format");
3328 return format_chars
+ nchars
- 1;
3334 && orig_format_chars
< format_chars
3335 && format_chars
[-1] == '.'
3336 && format_chars
[0] == ' '
3337 && format_chars
[1] == ' ')
3339 /* A period followed by two spaces. */
3340 if (ISUPPER (*orig_format_chars
))
3342 /* If the part before the period is a capitalized
3343 sentence check to make sure that what follows
3344 is also capitalized. */
3345 if (ISLOWER (format_chars
[2]))
3346 format_warning_substr (format_string_loc
, format_string_cst
,
3347 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3348 "inconsistent capitalization in "
3353 format_warning_substr (format_string_loc
, format_string_cst
,
3354 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3355 "unquoted sequence of %i consecutive "
3356 "space characters in format", nchars
);
3357 return format_chars
+ nchars
- 1;
3360 format_chars
+= nchars
;
3364 fmtchrpos
= format_chars
- orig_format_chars
;
3366 /* Diagnose any unquoted control characters other than the terminating
3368 while (format_chars
[nchars
] && ISCNTRL (format_chars
[nchars
]))
3373 format_warning_substr (format_string_loc
, format_string_cst
,
3374 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3375 "unquoted control characters in format");
3376 return format_chars
+ nchars
- 1;
3380 format_warning_substr (format_string_loc
, format_string_cst
,
3381 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3382 "unquoted control character %qc in format",
3384 return format_chars
+ nchars
- 1;
3387 if (ISPUNCT (format_chars
[0]))
3389 size_t nelts
= sizeof c_opers
/ sizeof *c_opers
;
3390 if (const char *ret
= check_tokens (c_opers
, nelts
,
3391 format_string_loc
, format_string_cst
,
3392 orig_format_chars
, format_chars
,
3396 nelts
= c_dialect_cxx () ? sizeof cxx_opers
/ sizeof *cxx_opers
: 0;
3397 if (const char *ret
= check_tokens (cxx_opers
, nelts
,
3398 format_string_loc
, format_string_cst
,
3399 orig_format_chars
, format_chars
,
3404 if (ISALPHA (format_chars
[0]))
3406 size_t nelts
= sizeof c_keywords
/ sizeof *c_keywords
;
3407 if (const char *ret
= check_tokens (c_keywords
, nelts
,
3408 format_string_loc
, format_string_cst
,
3409 orig_format_chars
, format_chars
,
3413 nelts
= c_dialect_cxx () ? sizeof cxx_keywords
/ sizeof *cxx_keywords
: 0;
3414 if (const char *ret
= check_tokens (cxx_keywords
, nelts
,
3415 format_string_loc
, format_string_cst
,
3416 orig_format_chars
, format_chars
,
3423 /* Diagnose unquoted options. */
3424 if ((format_chars
== orig_format_chars
3425 || format_chars
[-1] == ' ')
3426 && format_chars
[0] == '-'
3427 && ((format_chars
[1] == '-'
3428 && ISALPHA (format_chars
[2]))
3429 || ISALPHA (format_chars
[1])))
3432 while (ISALNUM (format_chars
[nchars
])
3433 || '_' == format_chars
[nchars
]
3434 || '-' == format_chars
[nchars
]
3435 || '+' == format_chars
[nchars
])
3438 format_warning_substr (format_string_loc
, format_string_cst
,
3439 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3440 "unquoted option name %<%.*s%> in format",
3441 nchars
, format_chars
);
3442 return format_chars
+ nchars
- 1;
3445 /* Diagnose leading, trailing, and two or more consecutive punctuation
3447 const char *unbalanced
= NULL
;
3448 while ('%' != format_chars
[nchars
]
3449 && ISPUNCT (format_chars
[nchars
])
3452 switch (format_chars
[nchars
])
3455 baltoks
.brackets
.safe_push (format_chars
+ nchars
);
3458 baltoks
.curly
.safe_push (format_chars
+ nchars
);
3461 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3464 baltoks
.pointy
.safe_push (format_chars
+ nchars
);
3468 if (baltoks
.brackets
.length () > 0)
3469 baltoks
.brackets
.pop ();
3471 unbalanced
= format_chars
+ nchars
;
3474 if (baltoks
.curly
.length () > 0)
3475 baltoks
.curly
.pop ();
3477 unbalanced
= format_chars
+ nchars
;
3480 if (baltoks
.parens
.length () > 0)
3481 baltoks
.parens
.pop ();
3483 unbalanced
= format_chars
+ nchars
;
3486 if (baltoks
.pointy
.length () > 0)
3487 baltoks
.pointy
.pop ();
3489 unbalanced
= format_chars
+ nchars
;
3498 format_warning_substr (format_string_loc
, format_string_cst
,
3499 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3500 "unbalanced punctuation character %qc in format",
3502 return format_chars
+ nchars
- 1;
3507 /* Consider any identifier that follows the pound ('#') sign
3508 a preprocessing directive. */
3510 && format_chars
[0] == '#'
3511 && ISALPHA (format_chars
[1]))
3513 while (ISALNUM (format_chars
[nchars
])
3514 || format_chars
[nchars
] == '_')
3517 format_warning_substr (format_string_loc
, format_string_cst
,
3518 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3519 "unquoted preprocessing directive %<%.*s%> "
3520 "in format", nchars
, format_chars
);
3521 return format_chars
+ nchars
- 1;
3524 /* Diagnose a bare single quote. */
3526 && format_chars
[0] == '\''
3527 && format_chars
- orig_format_chars
3528 && ISALPHA (format_chars
[-1])
3529 && ISALPHA (format_chars
[1]))
3531 /* Diagnose a subset of contractions that are best avoided. */
3532 for (unsigned i
= 0; i
!= sizeof contrs
/ sizeof *contrs
; ++i
)
3534 const char *apos
= strchr (contrs
[i
].name
, '\'');
3535 gcc_assert (apos
!= NULL
);
3536 int off
= apos
- contrs
[i
].name
;
3538 if (format_chars
- orig_format_chars
>= off
3539 && !strncmp (format_chars
- off
,
3540 contrs
[i
].name
, contrs
[i
].len
))
3542 format_warning_substr (format_string_loc
,
3544 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3545 "contraction %<%.*s%> in format; "
3547 contrs
[i
].len
, contrs
[i
].name
,
3549 return format_chars
+ nchars
- 1;
3553 if (format_warning_substr (format_string_loc
, format_string_cst
,
3554 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3555 "bare apostrophe %<'%> in format"))
3556 inform (format_string_loc
,
3557 "if avoiding the apostrophe is not feasible, enclose "
3558 "it in a pair of %qs and %qs directives instead",
3560 return format_chars
+ nchars
- 1;
3563 /* Diagnose a backtick (grave accent). */
3565 && format_chars
[0] == '`')
3567 if (format_warning_substr (format_string_loc
, format_string_cst
,
3568 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3569 "grave accent %<`%> in format"))
3570 inform (format_string_loc
,
3571 "use the apostrophe directive %qs instead", "%'");
3572 return format_chars
+ nchars
- 1;
3575 /* Diagnose a punctuation character after a space. */
3577 && format_chars
- orig_format_chars
3578 && format_chars
[-1] == ' '
3579 && strspn (format_chars
, "!?:;.,") == 1)
3581 format_warning_substr (format_string_loc
, format_string_cst
,
3582 fmtchrpos
- 1, fmtchrpos
, opt
,
3583 "space followed by punctuation character "
3584 "%<%c%>", format_chars
[0]);
3585 return format_chars
;
3590 if (startswith (format_chars
, "\"%s\""))
3592 if (format_warning_substr (format_string_loc
, format_string_cst
,
3593 fmtchrpos
, fmtchrpos
+ 4, opt
,
3594 "quoted %qs directive in format",
3596 inform (format_string_loc
, "if using %qs is not feasible, "
3597 "use %qs instead", "%qs", "\"%-s\"");
3600 if (format_chars
[0] == '"')
3602 baltoks
.doublequote
= baltoks
.doublequote
? NULL
: format_chars
;
3603 return format_chars
+ nchars
- 1;
3605 if (format_chars
[0] == '\'')
3607 baltoks
.singlequote
= baltoks
.singlequote
? NULL
: format_chars
;
3608 return format_chars
+ nchars
- 1;
3615 && format_chars
[0] == '(')
3616 ; /* Text beginning in an open parenthesis. */
3617 else if (nchars
== 3
3618 && startswith (format_chars
, "...")
3620 ; /* Text beginning in an ellipsis. */
3623 format_warning_substr (format_string_loc
, format_string_cst
,
3624 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3625 "spurious leading punctuation sequence "
3626 "%<%.*s%> in format",
3627 nchars
, format_chars
);
3628 return format_chars
+ nchars
- 1;
3631 else if (!format_chars
[nchars
])
3634 && (format_chars
[nchars
- 1] == ':'
3635 || format_chars
[nchars
- 1] == ')'))
3636 ; /* Text ending in a colon or a closing parenthesis. */
3637 else if (nchars
== 1
3638 && ((ISUPPER (*orig_format_chars
)
3639 && format_chars
[nchars
- 1] == '.')
3640 || strspn (format_chars
+ nchars
- 1, "?])") == 1))
3641 ; /* Capitalized sentence terminated by a single period,
3642 or text ending in a question mark, closing bracket,
3644 else if (nchars
== 2
3645 && format_chars
[0] == '?'
3646 && format_chars
[1] == ')')
3647 ; /* A question mark after a closing parenthetical note. */
3648 else if (nchars
== 2
3649 && format_chars
[0] == ')'
3650 && (format_chars
[1] == '?'
3651 || format_chars
[1] == ';'
3652 || format_chars
[1] == ':'
3653 || (ISUPPER (*orig_format_chars
)
3654 && format_chars
[1] == '.')))
3655 ; /* Closing parenthetical note followed by a question mark,
3656 semicolon, or colon at the end of the string, or by
3657 a period at the end of a capitalized sentence. */
3658 else if (nchars
== 3
3659 && format_chars
- orig_format_chars
> 0
3660 && startswith (format_chars
, "..."))
3661 ; /* Text ending in the ellipsis. */
3663 format_warning_substr (format_string_loc
, format_string_cst
,
3664 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3665 "spurious trailing punctuation sequence "
3666 "%<%.*s%> in format",
3667 nchars
, format_chars
);
3669 return format_chars
+ nchars
- 1;
3671 else if (nchars
== 2
3672 && format_chars
[0] == ')'
3673 && (format_chars
[1] == ':'
3674 || format_chars
[1] == ';'
3675 || format_chars
[1] == ',')
3676 && format_chars
[2] == ' ')
3677 ; /* Closing parenthetical note followed by a colon, semicolon
3678 or a comma followed by a space in the middle of the string. */
3679 else if (nchars
> 1)
3680 format_warning_substr (format_string_loc
, format_string_cst
,
3681 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3682 "unquoted sequence of %i consecutive "
3683 "punctuation characters %q.*s in format",
3684 nchars
, nchars
, format_chars
);
3685 return format_chars
+ nchars
- 1;
3690 /* Finally, diagnose any unquoted non-graph, non-punctuation characters
3691 other than the terminating NUL. */
3692 while (format_chars
[nchars
]
3693 && '%' != format_chars
[nchars
]
3694 && !ISPUNCT (format_chars
[nchars
])
3695 && !ISGRAPH (format_chars
[nchars
]))
3700 format_warning_substr (format_string_loc
, format_string_cst
,
3701 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3702 "unquoted non-graph characters in format");
3703 return format_chars
+ nchars
- 1;
3707 format_warning_substr (format_string_loc
, format_string_cst
,
3708 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3709 "unquoted non-graph character %qc in format",
3711 return format_chars
+ nchars
- 1;
3714 return format_chars
;
3717 /* Diagnose unbalanced tokens described by BALTOKS in format string
3718 ORIG_FORMAT_CHARS and the corresponding FORMAT_STRING_CST. */
3721 maybe_diag_unbalanced_tokens (location_t format_string_loc
,
3722 const char *orig_format_chars
,
3723 tree format_string_cst
,
3726 const char *unbalanced
= NULL
;
3728 if (baltoks
.brackets
.length ())
3729 unbalanced
= baltoks
.brackets
.pop ();
3730 else if (baltoks
.curly
.length ())
3731 unbalanced
= baltoks
.curly
.pop ();
3732 else if (baltoks
.parens
.length ())
3733 unbalanced
= baltoks
.parens
.pop ();
3734 else if (baltoks
.pointy
.length ())
3735 unbalanced
= baltoks
.pointy
.pop ();
3738 format_warning_at_char (format_string_loc
, format_string_cst
,
3739 unbalanced
- orig_format_chars
+ 1,
3741 "unbalanced punctuation character %<%c%> in format",
3744 if (baltoks
.quotdirs
.length ())
3745 format_warning_at_char (format_string_loc
, format_string_cst
,
3746 baltoks
.quotdirs
.pop () - orig_format_chars
,
3748 "unterminated quoting directive");
3751 = baltoks
.singlequote
? baltoks
.singlequote
: baltoks
.doublequote
;
3754 format_warning_at_char (format_string_loc
, format_string_cst
,
3755 quote
- orig_format_chars
+ 1,
3757 "unterminated quote character %<%c%> in format",
3761 /* Do the main part of checking a call to a format function. FORMAT_CHARS
3762 is the NUL-terminated format string (which at this point may contain
3763 internal NUL characters); FORMAT_LENGTH is its length (excluding the
3764 terminating NUL character). ARG_NUM is one less than the number of
3765 the first format argument to check; PARAMS points to that format
3766 argument in the list of arguments. */
3769 check_format_info_main (format_check_results
*res
,
3770 function_format_info
*info
, const char *format_chars
,
3771 location_t fmt_param_loc
, tree format_string_cst
,
3772 int format_length
, tree params
,
3773 unsigned HOST_WIDE_INT arg_num
,
3774 object_allocator
<format_wanted_type
> &fwt_pool
,
3775 vec
<location_t
> *arglocs
)
3777 const char * const orig_format_chars
= format_chars
;
3778 const tree first_fillin_param
= params
;
3780 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
3781 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
3782 const location_t format_string_loc
= res
->format_string_loc
;
3784 /* -1 if no conversions taking an operand have been found; 0 if one has
3785 and it didn't use $; 1 if $ formats are in use. */
3786 int has_operand_number
= -1;
3788 /* Vectors of pointers to opening quoting directives (like GCC "%<"),
3789 opening braces, brackets, and parentheses. Used to detect unbalanced
3793 /* Pointers to the most recent color directives (like GCC's "%r or %R").
3794 A starting color directive much be terminated before the end of
3795 the format string. A terminating directive makes no sense without
3796 a prior starting directive. */
3797 const char *color_begin
= NULL
;
3798 const char *color_end
= NULL
;
3800 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
3802 /* In GCC diagnostic functions check plain directives (substrings within
3803 the format string that don't start with %) for quoting and punctuations
3805 bool ck_plain
= (!info
->is_raw
3806 && (info
->format_type
== gcc_diag_format_type
3807 || info
->format_type
== gcc_tdiag_format_type
3808 || info
->format_type
== gcc_cdiag_format_type
3809 || info
->format_type
== gcc_cxxdiag_format_type
));
3811 while (*format_chars
!= 0)
3814 format_chars
= check_plain (format_string_loc
,
3816 orig_format_chars
, format_chars
,
3819 if (*format_chars
== 0 || *format_chars
++ != '%')
3822 if (*format_chars
== 0)
3824 format_warning_at_char (format_string_loc
, format_string_cst
,
3825 format_chars
- orig_format_chars
,
3827 "spurious trailing %<%%%> in format");
3830 if (*format_chars
== '%')
3836 /* ARGUMENT_PARSER ctor takes FORMAT_CHARS by reference and calls
3837 to ARG_PARSER members may modify the variable. */
3838 flag_chars_t flag_chars
;
3839 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
3840 orig_format_chars
, format_string_loc
,
3841 flag_chars
, has_operand_number
,
3842 first_fillin_param
, fwt_pool
, arglocs
);
3844 if (!arg_parser
.read_any_dollar ())
3847 if (!arg_parser
.read_format_flags ())
3850 /* Read any format width, possibly * or *m$. */
3851 if (!arg_parser
.read_any_format_width (params
, arg_num
))
3854 /* Read any format left precision (must be a number, not *). */
3855 arg_parser
.read_any_format_left_precision ();
3857 /* Read any format precision, possibly * or *m$. */
3858 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
3861 const char *format_start
= format_chars
;
3863 arg_parser
.handle_alloc_chars ();
3865 /* The rest of the conversion specification is the length modifier
3866 (if any), and the conversion specifier, so this is where the
3867 type information starts. If we need to issue a suggestion
3868 about a type mismatch, then we should preserve everything up
3870 const char *type_start
= format_chars
;
3872 /* Read any length modifier, if this kind of format has them. */
3873 const length_modifier len_modifier
3874 = arg_parser
.read_any_length_modifier ();
3876 /* Read any modifier (strftime E/O). */
3877 arg_parser
.read_any_other_modifier ();
3879 char format_char
= *format_chars
;
3880 if (format_char
== 0
3881 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
3882 && format_char
== '%'))
3884 format_warning_at_char (format_string_loc
, format_string_cst
,
3885 format_chars
- orig_format_chars
,
3887 "conversion lacks type at end of format");
3892 const format_char_info
* const fci
3893 = arg_parser
.find_format_char_info (format_char
);
3897 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
3899 format_string_loc
, orig_format_chars
, format_char
,
3900 baltoks
.quotdirs
.length () > 0);
3902 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
3903 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
3905 /* Diagnose nested or unmatched quoting directives such as GCC's
3906 "%<...%<" and "%>...%>". */
3907 bool quot_begin_p
= strchr (fci
->flags2
, '<');
3908 bool quot_end_p
= strchr (fci
->flags2
, '>');
3910 if (quot_begin_p
&& !quot_end_p
)
3912 if (baltoks
.quotdirs
.length ())
3913 format_warning_at_char (format_string_loc
, format_string_cst
,
3914 format_chars
- orig_format_chars
,
3916 "nested quoting directive");
3917 baltoks
.quotdirs
.safe_push (format_chars
);
3919 else if (!quot_begin_p
&& quot_end_p
)
3921 if (baltoks
.quotdirs
.length ())
3922 baltoks
.quotdirs
.pop ();
3924 format_warning_at_char (format_string_loc
, format_string_cst
,
3925 format_chars
- orig_format_chars
,
3927 "unmatched quoting directive");
3930 bool color_begin_p
= strchr (fci
->flags2
, '/');
3933 color_begin
= format_chars
;
3936 else if (strchr (fci
->flags2
, '\\'))
3939 format_warning_at_char (format_string_loc
, format_string_cst
,
3940 format_chars
- orig_format_chars
,
3942 "%qc directive redundant after prior "
3943 "occurence of the same", format_char
);
3944 else if (!color_begin
)
3945 format_warning_at_char (format_string_loc
, format_string_cst
,
3946 format_chars
- orig_format_chars
,
3948 "unmatched color reset directive");
3949 color_end
= format_chars
;
3952 /* Diagnose directives that shouldn't appear in a quoted sequence.
3953 (They are denoted by a double quote in FLAGS2.) */
3954 if (baltoks
.quotdirs
.length ())
3956 if (strchr (fci
->flags2
, '"'))
3957 format_warning_at_char (format_string_loc
, format_string_cst
,
3958 format_chars
- orig_format_chars
,
3960 "%qc conversion used within a quoted "
3965 /* Validate the pairs of flags used. */
3966 arg_parser
.validate_flag_pairs (fci
, format_char
);
3968 arg_parser
.give_y2k_warnings (fci
, format_char
);
3970 arg_parser
.parse_any_scan_set (fci
);
3972 tree wanted_type
= NULL
;
3973 const char *wanted_type_name
= NULL
;
3975 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
3976 wanted_type
, wanted_type_name
,
3982 arg_parser
.main_wanted_type
.next
= NULL
;
3984 /* Finally. . .check type of argument against desired type! */
3985 if (!arg_parser
.check_argument_type (fci
, len_modifier
,
3986 wanted_type
, wanted_type_name
,
3990 format_start
, type_start
,
3996 if (format_chars
- orig_format_chars
!= format_length
)
3997 format_warning_at_char (format_string_loc
, format_string_cst
,
3998 format_chars
+ 1 - orig_format_chars
,
3999 OPT_Wformat_contains_nul
,
4000 "embedded %<\\0%> in format");
4001 if (info
->first_arg_num
!= 0 && params
!= 0
4002 && has_operand_number
<= 0)
4004 res
->number_other
--;
4005 res
->number_extra_args
++;
4007 if (has_operand_number
> 0)
4008 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
4010 maybe_diag_unbalanced_tokens (format_string_loc
, orig_format_chars
,
4011 format_string_cst
, baltoks
);
4013 if (color_begin
&& !color_end
)
4014 format_warning_at_char (format_string_loc
, format_string_cst
,
4015 color_begin
- orig_format_chars
,
4016 OPT_Wformat_
, "unterminated color directive");
4019 /* Check the argument types from a single format conversion (possibly
4020 including width and precision arguments).
4022 FMT_LOC is the location of the format conversion.
4024 TYPES is a singly-linked list expressing the parts of the format
4025 conversion that expect argument types, and the arguments they
4028 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
4029 format string to where type information begins for the conversion
4030 (the length modifier and conversion specifier).
4032 CONVERSION_CHAR is the user-provided conversion specifier.
4036 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4038 then FMT_LOC covers this range:
4040 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4043 and TYPES in this case is a three-entry singly-linked list consisting of:
4044 (1) the check for the field width here:
4045 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4048 (2) the check for the field precision here:
4049 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4052 (3) the check for the length modifier and conversion char here:
4053 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4057 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
4060 0000000000111111111122
4061 0123456789012345678901
4062 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4064 | ` CONVERSION_CHAR: 'd'
4065 type starts here. */
4068 check_format_types (const substring_loc
&fmt_loc
,
4069 format_wanted_type
*types
, const format_kind_info
*fki
,
4070 int offset_to_type_start
,
4071 char conversion_char
,
4072 vec
<location_t
> *arglocs
)
4074 for (; types
!= 0; types
= types
->next
)
4084 wanted_type
= types
->wanted_type
;
4085 arg_num
= types
->arg_num
;
4087 /* The following should not occur here. */
4088 gcc_assert (wanted_type
);
4089 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
4091 if (types
->pointer_count
== 0)
4092 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
4094 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
4096 cur_param
= types
->param
;
4099 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
4100 NULL
, fki
, offset_to_type_start
,
4105 cur_type
= TREE_TYPE (cur_param
);
4106 if (cur_type
== error_mark_node
)
4108 orig_cur_type
= cur_type
;
4111 location_t param_loc
= UNKNOWN_LOCATION
;
4112 if (EXPR_HAS_LOCATION (cur_param
))
4113 param_loc
= EXPR_LOCATION (cur_param
);
4116 /* arg_num is 1-based. */
4117 gcc_assert (types
->arg_num
> 0);
4118 param_loc
= (*arglocs
)[types
->arg_num
- 1];
4121 STRIP_NOPS (cur_param
);
4123 /* Check the types of any additional pointer arguments
4124 that precede the "real" argument. */
4125 for (i
= 0; i
< types
->pointer_count
; ++i
)
4127 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
4129 cur_type
= TREE_TYPE (cur_type
);
4130 if (cur_type
== error_mark_node
)
4133 /* Check for writing through a NULL pointer. */
4134 if (types
->writing_in_flag
4137 && integer_zerop (cur_param
))
4138 warning (OPT_Wformat_
, "writing through null pointer "
4139 "(argument %d)", arg_num
);
4141 /* Check for reading through a NULL pointer. Ignore
4142 printf-family of functions as they are checked for
4143 null arguments by the middle-end. */
4144 if (fki
->conversion_specs
!= print_char_table
4145 && types
->reading_from_flag
4148 && integer_zerop (cur_param
))
4149 warning (OPT_Wformat_
, "reading through null pointer "
4150 "(argument %d)", arg_num
);
4152 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
4153 cur_param
= TREE_OPERAND (cur_param
, 0);
4157 /* See if this is an attempt to write into a const type with
4158 scanf or with printf "%n". Note: the writing in happens
4159 at the first indirection only, if for example
4160 void * const * is passed to scanf %p; passing
4161 const void ** is simply passing an incompatible type. */
4162 if (types
->writing_in_flag
4164 && (TYPE_READONLY (cur_type
)
4166 && (CONSTANT_CLASS_P (cur_param
)
4167 || (DECL_P (cur_param
)
4168 && TREE_READONLY (cur_param
))))))
4169 warning (OPT_Wformat_
, "writing into constant object "
4170 "(argument %d)", arg_num
);
4172 /* If there are extra type qualifiers beyond the first
4173 indirection, then this makes the types technically
4177 && (TYPE_READONLY (cur_type
)
4178 || TYPE_VOLATILE (cur_type
)
4179 || TYPE_ATOMIC (cur_type
)
4180 || TYPE_RESTRICT (cur_type
)))
4181 warning (OPT_Wformat_
, "extra type qualifiers in format "
4182 "argument (argument %d)",
4188 format_type_warning (fmt_loc
, param_loc
,
4189 types
, wanted_type
, orig_cur_type
, fki
,
4190 offset_to_type_start
, conversion_char
);
4195 if (i
< types
->pointer_count
)
4198 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
4200 /* Check whether the argument type is a character type. This leniency
4201 only applies to certain formats, flagged with 'c'. */
4202 if (types
->char_lenient_flag
)
4203 char_type_flag
= (cur_type
== char_type_node
4204 || cur_type
== signed_char_type_node
4205 || cur_type
== unsigned_char_type_node
);
4207 /* Check the type of the "real" argument, if there's a type we want. */
4208 if (lang_hooks
.types_compatible_p (wanted_type
, cur_type
))
4210 /* If we want 'void *', allow any pointer type.
4211 (Anything else would already have got a warning.)
4212 With -Wpedantic, only allow pointers to void and to character
4214 if (wanted_type
== void_type_node
4215 && (!pedantic
|| (i
== 1 && char_type_flag
)))
4217 /* Don't warn about differences merely in signedness, unless
4218 -Wpedantic. With -Wpedantic, warn if the type is a pointer
4219 target and not a character type, and for character types at
4220 a second level of indirection. */
4221 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
4222 && TREE_CODE (cur_type
) == INTEGER_TYPE
4223 && ((!pedantic
&& !warn_format_signedness
)
4224 || (i
== 0 && !warn_format_signedness
)
4225 || (i
== 1 && char_type_flag
))
4226 && (TYPE_UNSIGNED (wanted_type
)
4227 ? wanted_type
== c_common_unsigned_type (cur_type
)
4228 : wanted_type
== c_common_signed_type (cur_type
)))
4230 /* Don't warn about differences merely in signedness if we know
4231 that the current type is integer-promoted and its original type
4232 was unsigned such as that it is in the range of WANTED_TYPE. */
4233 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
4234 && TREE_CODE (cur_type
) == INTEGER_TYPE
4235 && warn_format_signedness
4236 && TYPE_UNSIGNED (wanted_type
)
4237 && cur_param
!= NULL_TREE
4238 && TREE_CODE (cur_param
) == NOP_EXPR
)
4240 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
4241 if (TYPE_UNSIGNED (t
)
4242 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
4245 /* Likewise, "signed char", "unsigned char" and "char" are
4246 equivalent but the above test won't consider them equivalent. */
4247 if (wanted_type
== char_type_node
4248 && (!pedantic
|| i
< 2)
4251 if (types
->scalar_identity_flag
4252 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
4253 || (INTEGRAL_TYPE_P (cur_type
)
4254 && INTEGRAL_TYPE_P (wanted_type
)))
4255 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
4257 /* Now we have a type mismatch. */
4258 format_type_warning (fmt_loc
, param_loc
, types
,
4259 wanted_type
, orig_cur_type
, fki
,
4260 offset_to_type_start
, conversion_char
);
4264 /* Given type TYPE, attempt to dereference the type N times
4265 (e.g. from ("int ***", 2) to "int *")
4267 Return the derefenced type, with any qualifiers
4268 such as "const" stripped from the result, or
4269 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
4272 deref_n_times (tree type
, int n
)
4276 for (int i
= n
; i
> 0; i
--)
4278 if (TREE_CODE (type
) != POINTER_TYPE
)
4280 type
= TREE_TYPE (type
);
4282 /* Strip off any "const" etc. */
4283 return build_qualified_type (type
, 0);
4286 /* Lookup the format code for FORMAT_LEN within FLI,
4287 returning the string code for expressing it, or NULL
4288 if it is not found. */
4291 get_modifier_for_format_len (const format_length_info
*fli
,
4292 enum format_lengths format_len
)
4294 for (; fli
->name
; fli
++)
4296 if (fli
->index
== format_len
)
4298 if (fli
->double_index
== format_len
)
4299 return fli
->double_name
;
4306 namespace selftest
{
4309 test_get_modifier_for_format_len ()
4312 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
4314 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
4316 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
4318 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
4321 } // namespace selftest
4323 #endif /* CHECKING_P */
4325 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
4326 format_type_detail using SPEC_TYPE to be offered as a suggestion for
4327 Wformat type errors where the argument has type ARG_TYPE. */
4330 matching_type_p (tree spec_type
, tree arg_type
)
4332 gcc_assert (spec_type
);
4333 gcc_assert (arg_type
);
4335 /* If any of the types requires structural equality, we can't compare
4336 their canonical types. */
4337 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
4338 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
4341 spec_type
= TYPE_CANONICAL (spec_type
);
4342 arg_type
= TYPE_CANONICAL (arg_type
);
4344 if (TREE_CODE (spec_type
) == INTEGER_TYPE
4345 && TREE_CODE (arg_type
) == INTEGER_TYPE
4346 && (TYPE_UNSIGNED (spec_type
)
4347 ? spec_type
== c_common_unsigned_type (arg_type
)
4348 : spec_type
== c_common_signed_type (arg_type
)))
4351 return spec_type
== arg_type
;
4354 /* Subroutine of get_format_for_type.
4356 Generate a string containing the length modifier and conversion specifier
4357 that should be used to format arguments of type ARG_TYPE within FKI
4358 (effectively the inverse of the checking code).
4360 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
4361 is required to use it, for correcting bogus length modifiers.
4362 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
4363 that matches ARG_TYPE.
4365 If successful, returns a non-NULL string which should be freed
4367 Otherwise, returns NULL. */
4370 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
4371 char conversion_char
)
4373 gcc_assert (arg_type
);
4375 const format_char_info
*spec
;
4376 for (spec
= &fki
->conversion_specs
[0];
4380 if (conversion_char
)
4381 if (!strchr (spec
->format_chars
, conversion_char
))
4384 tree effective_arg_type
= deref_n_times (arg_type
,
4385 spec
->pointer_count
);
4386 if (!effective_arg_type
)
4388 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
4390 const format_type_detail
*ftd
= &spec
->types
[i
];
4391 if (!ftd
->type
|| *ftd
->type
== NULL_TREE
)
4393 if (matching_type_p (*ftd
->type
, effective_arg_type
))
4395 const char *len_modifier
4396 = get_modifier_for_format_len (fki
->length_char_specs
,
4397 (enum format_lengths
)i
);
4401 if (conversion_char
)
4402 /* We found a match, using the given conversion char - the
4403 length modifier was incorrect (or absent).
4404 Provide a suggestion using the conversion char with the
4405 correct length modifier for the type. */
4406 return xasprintf ("%s%c", len_modifier
, conversion_char
);
4408 /* 2nd pass: no match was possible using the user-provided
4409 conversion char, but we do have a match without using it.
4410 Provide a suggestion using the first conversion char
4411 listed for the given type. */
4412 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
4420 /* Generate a string containing the length modifier and conversion specifier
4421 that should be used to format arguments of type ARG_TYPE within FKI
4422 (effectively the inverse of the checking code).
4424 If successful, returns a non-NULL string which should be freed
4426 Otherwise, returns NULL. */
4429 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
4430 char conversion_char
)
4432 gcc_assert (arg_type
);
4433 gcc_assert (conversion_char
);
4435 /* First pass: look for a format_char_info containing CONVERSION_CHAR
4436 If we find one, then presumably the length modifier was incorrect
4438 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
4442 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
4443 matching just on the type. */
4444 return get_format_for_type_1 (fki
, arg_type
, '\0');
4447 /* Attempt to get a string for use as a replacement fix-it hint for the
4448 source range in FMT_LOC.
4450 Preserve all of the text within the range of FMT_LOC up to
4451 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
4452 length modifier and conversion specifier for ARG_TYPE, attempting
4453 to keep the user-provided CONVERSION_CHAR if possible.
4455 For example, given a long vs long long mismatch for arg5 here:
4457 000000000111111111122222222223333333333|
4458 123456789012345678901234567890123456789` column numbers
4459 0000000000111111111122|
4460 0123456789012345678901` string offsets
4461 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
4462 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4464 | ` CONVERSION_CHAR: 'd'
4467 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
4468 STRING_CST), where the user provided:
4470 the result (assuming "long" argument 5) should be:
4473 If successful, returns a non-NULL string which should be freed
4475 Otherwise, returns NULL. */
4478 get_corrected_substring (const substring_loc
&fmt_loc
,
4479 format_wanted_type
*type
, tree arg_type
,
4480 const format_kind_info
*fki
,
4481 int offset_to_type_start
, char conversion_char
)
4483 /* Attempt to provide hints for argument types, but not for field widths
4487 if (type
->kind
!= CF_KIND_FORMAT
)
4490 /* Locate the current code within the source range, rejecting
4491 any awkward cases where the format string occupies more than
4493 Lookup the place where the type starts (including any length
4494 modifiers), getting it as the caret location. */
4495 substring_loc
type_loc (fmt_loc
);
4496 type_loc
.set_caret_index (offset_to_type_start
);
4498 location_t fmt_substring_loc
;
4499 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
4503 source_range fmt_substring_range
4504 = get_range_from_loc (line_table
, fmt_substring_loc
);
4506 expanded_location caret
4507 = expand_location_to_spelling_point (fmt_substring_loc
);
4508 expanded_location start
4509 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
4510 expanded_location finish
4511 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
4512 if (caret
.file
!= start
.file
)
4514 if (start
.file
!= finish
.file
)
4516 if (caret
.line
!= start
.line
)
4518 if (start
.line
!= finish
.line
)
4520 if (start
.column
> caret
.column
)
4522 if (start
.column
> finish
.column
)
4524 if (caret
.column
> finish
.column
)
4527 char_span line
= location_get_source_line (start
.file
, start
.line
);
4531 /* If we got this far, then we have the line containing the
4532 existing conversion specification.
4534 Generate a trimmed copy, containing the prefix part of the conversion
4535 specification, up to the (but not including) the length modifier.
4536 In the above example, this would be "%-+*.*". */
4537 int length_up_to_type
= caret
.column
- start
.column
;
4538 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
4539 char *prefix
= prefix_span
.xstrdup ();
4541 /* Now attempt to generate a suggestion for the rest of the specification
4542 (length modifier and conversion char), based on ARG_TYPE and
4544 In the above example, this would be "ld". */
4545 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
4546 if (!format_for_type
)
4552 /* Success. Generate the resulting suggestion for the whole range of
4553 FMT_LOC by concatenating the two strings.
4554 In the above example, this would be "%-+*.*ld". */
4555 char *result
= concat (prefix
, format_for_type
, NULL
);
4556 free (format_for_type
);
4561 /* Helper class for adding zero or more trailing '*' to types.
4563 The format type and name exclude any '*' for pointers, so those
4564 must be formatted manually. For all the types we currently have,
4565 this is adequate, but formats taking pointers to functions or
4566 arrays would require the full type to be built up in order to
4567 print it with %T. */
4569 class indirection_suffix
4572 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
4574 /* Determine the size of the buffer (including NUL-terminator). */
4576 size_t get_buffer_size () const
4578 return m_pointer_count
+ 2;
4581 /* Write the '*' to DST and add a NUL-terminator. */
4583 void fill_buffer (char *dst
) const
4585 if (m_pointer_count
== 0)
4587 else if (c_dialect_cxx ())
4589 memset (dst
, '*', m_pointer_count
);
4590 dst
[m_pointer_count
] = 0;
4595 memset (dst
+ 1, '*', m_pointer_count
);
4596 dst
[m_pointer_count
+ 1] = 0;
4601 int m_pointer_count
;
4604 /* Subclass of range_label for labelling the range in the format string
4605 with the type in question, adding trailing '*' for pointer_count. */
4607 class range_label_for_format_type_mismatch
4608 : public range_label_for_type_mismatch
4611 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
4613 : range_label_for_type_mismatch (labelled_type
, other_type
),
4614 m_pointer_count (pointer_count
)
4618 label_text
get_text (unsigned range_idx
) const FINAL OVERRIDE
4620 label_text text
= range_label_for_type_mismatch::get_text (range_idx
);
4621 if (text
.m_buffer
== NULL
)
4624 indirection_suffix
suffix (m_pointer_count
);
4625 char *p
= (char *) alloca (suffix
.get_buffer_size ());
4626 suffix
.fill_buffer (p
);
4628 char *result
= concat (text
.m_buffer
, p
, NULL
);
4630 return label_text::take (result
);
4634 int m_pointer_count
;
4637 /* Give a warning about a format argument of different type from that expected.
4638 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
4639 is based on the location of the char at TYPE->offset_loc.
4640 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
4641 if this is unavailable.
4642 WANTED_TYPE is the type the argument should have,
4643 possibly stripped of pointer dereferences. The description (such as "field
4644 precision"), the placement in the format string, a possibly more
4645 friendly name of WANTED_TYPE, and the number of pointer dereferences
4646 are taken from TYPE. ARG_TYPE is the type of the actual argument,
4647 or NULL if it is missing.
4649 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
4650 format string to where type information begins for the conversion
4651 (the length modifier and conversion specifier).
4652 CONVERSION_CHAR is the user-provided conversion specifier.
4654 For example, given a type mismatch for argument 5 here:
4656 00000000011111111112222222222333333333344444444445555555555|
4657 12345678901234567890123456789012345678901234567890123456789` column numbers
4658 0000000000111111111122|
4659 0123456789012345678901` offsets within STRING_CST
4660 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
4661 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
4663 | ` CONVERSION_CHAR: 'd' PARAM_LOC
4666 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
4670 format_type_warning (const substring_loc
&whole_fmt_loc
,
4671 location_t param_loc
,
4672 format_wanted_type
*type
,
4673 tree wanted_type
, tree arg_type
,
4674 const format_kind_info
*fki
,
4675 int offset_to_type_start
,
4676 char conversion_char
)
4678 enum format_specifier_kind kind
= type
->kind
;
4679 const char *wanted_type_name
= type
->wanted_type_name
;
4680 const char *format_start
= type
->format_start
;
4681 int format_length
= type
->format_length
;
4682 int pointer_count
= type
->pointer_count
;
4683 int arg_num
= type
->arg_num
;
4685 /* If ARG_TYPE is a typedef with a misleading name (for example,
4686 size_t but not the standard size_t expected by printf %zu), avoid
4687 printing the typedef name. */
4688 if (wanted_type_name
4690 && TYPE_NAME (arg_type
)
4691 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
4692 && DECL_NAME (TYPE_NAME (arg_type
))
4693 && !strcmp (wanted_type_name
,
4694 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
4695 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
4697 indirection_suffix
suffix (pointer_count
);
4698 char *p
= (char *) alloca (suffix
.get_buffer_size ());
4699 suffix
.fill_buffer (p
);
4701 /* WHOLE_FMT_LOC has the caret at the end of the range.
4702 Set the caret to be at the offset from TYPE. Subtract one
4703 from the offset for the same reason as in format_warning_at_char. */
4704 substring_loc
fmt_loc (whole_fmt_loc
);
4705 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
4707 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
4709 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
4711 /* Get a string for use as a replacement fix-it hint for the range in
4712 fmt_loc, or NULL. */
4713 char *corrected_substring
4714 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
4715 offset_to_type_start
, conversion_char
);
4716 format_string_diagnostic_t
diag (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
4717 corrected_substring
);
4718 if (wanted_type_name
)
4723 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
4724 "but argument %d has type %qT",
4725 gettext (kind_descriptions
[kind
]),
4726 (kind
== CF_KIND_FORMAT
? "%" : ""),
4727 format_length
, format_start
,
4728 wanted_type_name
, p
, arg_num
, arg_type
);
4732 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
4733 gettext (kind_descriptions
[kind
]),
4734 (kind
== CF_KIND_FORMAT
? "%" : ""),
4735 format_length
, format_start
, wanted_type_name
, p
);
4742 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
4743 "but argument %d has type %qT",
4744 gettext (kind_descriptions
[kind
]),
4745 (kind
== CF_KIND_FORMAT
? "%" : ""),
4746 format_length
, format_start
,
4747 wanted_type
, p
, arg_num
, arg_type
);
4751 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
4752 gettext (kind_descriptions
[kind
]),
4753 (kind
== CF_KIND_FORMAT
? "%" : ""),
4754 format_length
, format_start
, wanted_type
, p
);
4757 free (corrected_substring
);
4761 /* Given a format_char_info array FCI, and a character C, this function
4762 returns the index into the conversion_specs where that specifier's
4763 data is located. The character must exist. */
4765 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
4769 for (i
= 0; fci
->format_chars
; i
++, fci
++)
4770 if (strchr (fci
->format_chars
, c
))
4773 /* We shouldn't be looking for a non-existent specifier. */
4777 /* Given a format_length_info array FLI, and a character C, this
4778 function returns the index into the conversion_specs where that
4779 modifier's data is located. The character must exist. */
4781 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
4785 for (i
= 0; fli
->name
; i
++, fli
++)
4786 if (strchr (fli
->name
, c
))
4789 /* We shouldn't be looking for a non-existent modifier. */
4793 /* Determine the type of HOST_WIDE_INT in the code being compiled for
4794 use in GCC's __asm_fprintf__ custom format attribute. You must
4795 have set dynamic_format_types before calling this function. */
4797 init_dynamic_asm_fprintf_info (void)
4803 format_length_info
*new_asm_fprintf_length_specs
;
4806 /* Find the underlying type for HOST_WIDE_INT. For the %w
4807 length modifier to work, one must have issued: "typedef
4808 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
4809 prior to using that modifier. */
4810 hwi
= maybe_get_identifier ("__gcc_host_wide_int__");
4813 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4816 hwi
= identifier_global_value (hwi
);
4817 if (!hwi
|| TREE_CODE (hwi
) != TYPE_DECL
)
4819 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4822 hwi
= DECL_ORIGINAL_TYPE (hwi
);
4824 if (hwi
!= long_integer_type_node
&& hwi
!= long_long_integer_type_node
)
4826 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
4827 " or %<long long%>");
4831 /* Create a new (writable) copy of asm_fprintf_length_specs. */
4832 new_asm_fprintf_length_specs
= (format_length_info
*)
4833 xmemdup (asm_fprintf_length_specs
,
4834 sizeof (asm_fprintf_length_specs
),
4835 sizeof (asm_fprintf_length_specs
));
4837 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4838 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
4839 if (hwi
== long_integer_type_node
)
4840 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
4841 else if (hwi
== long_long_integer_type_node
)
4842 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
4846 /* Assign the new data for use. */
4847 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
4848 new_asm_fprintf_length_specs
;
4852 static const format_length_info
*
4853 get_init_dynamic_hwi (void)
4856 static format_length_info
*diag_ls
;
4862 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
4863 length modifier to work, one must have issued: "typedef
4864 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
4865 prior to using that modifier. */
4866 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
4868 hwi
= identifier_global_value (hwi
);
4871 if (TREE_CODE (hwi
) != TYPE_DECL
)
4873 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4878 hwi
= DECL_ORIGINAL_TYPE (hwi
);
4880 if (hwi
!= long_integer_type_node
4881 && hwi
!= long_long_integer_type_node
)
4883 error ("%<__gcc_host_wide_int__%> is not defined"
4884 " as %<long%> or %<long long%>");
4891 diag_ls
= (format_length_info
*)
4892 xmemdup (gcc_diag_length_specs
,
4893 sizeof (gcc_diag_length_specs
),
4894 sizeof (gcc_diag_length_specs
));
4897 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4898 i
= find_length_info_modifier_index (diag_ls
, 'w');
4899 if (hwi
== long_integer_type_node
)
4900 diag_ls
[i
].index
= FMT_LEN_l
;
4901 else if (hwi
== long_long_integer_type_node
)
4902 diag_ls
[i
].index
= FMT_LEN_ll
;
4910 /* Determine the type of a "locus" in the code being compiled for use
4911 in GCC's __gcc_gfc__ custom format attribute. You must have set
4912 dynamic_format_types before calling this function. */
4914 init_dynamic_gfc_info (void)
4916 dynamic_format_types
[gcc_gfc_format_type
].length_char_specs
4917 = get_init_dynamic_hwi ();
4921 static format_char_info
*gfc_fci
;
4923 /* For the GCC __gcc_gfc__ custom format specifier to work, one
4924 must have declared 'locus' prior to using this attribute. If
4925 we haven't seen this declarations then you shouldn't use the
4926 specifier requiring that type. */
4927 if ((locus
= maybe_get_identifier ("locus")))
4929 locus
= identifier_global_value (locus
);
4932 if (TREE_CODE (locus
) != TYPE_DECL
4933 || TREE_TYPE (locus
) == error_mark_node
)
4935 error ("%<locus%> is not defined as a type");
4939 locus
= TREE_TYPE (locus
);
4943 /* Assign the new data for use. */
4945 /* Handle the __gcc_gfc__ format specifics. */
4947 dynamic_format_types
[gcc_gfc_format_type
].conversion_specs
=
4948 gfc_fci
= (format_char_info
*)
4949 xmemdup (gcc_gfc_char_table
,
4950 sizeof (gcc_gfc_char_table
),
4951 sizeof (gcc_gfc_char_table
));
4954 const unsigned i
= find_char_info_specifier_index (gfc_fci
, 'L');
4955 gfc_fci
[i
].types
[0].type
= &locus
;
4956 gfc_fci
[i
].pointer_count
= 1;
4961 /* Lookup the type named NAME and return a NAME type if found.
4962 Otherwise, return void_type_node if NAME has not been used yet,
4963 or NULL_TREE if NAME is not a type (issuing an error). */
4966 get_named_type (const char *name
)
4968 if (tree result
= maybe_get_identifier (name
))
4970 result
= identifier_global_tag (result
);
4973 if (TYPE_P (result
))
4975 else if (TREE_CODE (result
) == TYPE_DECL
)
4976 result
= TREE_TYPE (result
);
4979 error ("%qs is not defined as a type", name
);
4986 return void_type_node
;
4989 /* Determine the types of "tree" and "location_t" in the code being
4990 compiled for use in GCC's diagnostic custom format attributes. You
4991 must have set dynamic_format_types before calling this function. */
4993 init_dynamic_diag_info (void)
4995 /* For the GCC-diagnostics custom format specifiers to work, one
4996 must have declared 'tree' and 'location_t' prior to using those
4997 attributes. If we haven't seen these declarations then
4998 the specifiers requiring these types shouldn't be used.
4999 However we don't force a hard ICE because we may see only one
5000 or the other type. */
5001 if (tree loc
= maybe_get_identifier ("location_t"))
5003 loc
= identifier_global_value (loc
);
5004 if (loc
&& TREE_CODE (loc
) != TYPE_DECL
)
5005 error ("%<location_t%> is not defined as a type");
5008 /* Initialize the global tree node type local to this file. */
5009 if (!local_tree_type_node
5010 || local_tree_type_node
== void_type_node
)
5012 /* We need to grab the underlying 'union tree_node' so peek into
5013 an extra type level. */
5014 if ((local_tree_type_node
= maybe_get_identifier ("tree")))
5016 local_tree_type_node
5017 = identifier_global_value (local_tree_type_node
);
5018 if (local_tree_type_node
)
5020 if (TREE_CODE (local_tree_type_node
) != TYPE_DECL
)
5022 error ("%<tree%> is not defined as a type");
5023 local_tree_type_node
= NULL_TREE
;
5025 else if (TREE_CODE (TREE_TYPE (local_tree_type_node
))
5028 error ("%<tree%> is not defined as a pointer type");
5029 local_tree_type_node
= NULL_TREE
;
5032 local_tree_type_node
5033 = TREE_TYPE (TREE_TYPE (local_tree_type_node
));
5037 local_tree_type_node
= void_type_node
;
5040 /* Similar to the above but for gimple*. */
5041 if (!local_gimple_ptr_node
5042 || local_gimple_ptr_node
== void_type_node
)
5043 local_gimple_ptr_node
= get_named_type ("gimple");
5045 /* Similar to the above but for cgraph_node*. */
5046 if (!local_cgraph_node_ptr_node
5047 || local_cgraph_node_ptr_node
== void_type_node
)
5048 local_cgraph_node_ptr_node
= get_named_type ("cgraph_node");
5050 /* Similar to the above but for diagnostic_event_id_t*. */
5051 if (!local_event_ptr_node
5052 || local_event_ptr_node
== void_type_node
)
5053 local_event_ptr_node
= get_named_type ("diagnostic_event_id_t");
5055 /* All the GCC diag formats use the same length specs. */
5056 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
5057 dynamic_format_types
[gcc_tdiag_format_type
].length_char_specs
=
5058 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
5059 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
5060 dynamic_format_types
[gcc_dump_printf_format_type
].length_char_specs
5061 = get_init_dynamic_hwi ();
5063 /* It's safe to "re-initialize these to the same values. */
5064 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
5065 gcc_diag_char_table
;
5066 dynamic_format_types
[gcc_tdiag_format_type
].conversion_specs
=
5067 gcc_tdiag_char_table
;
5068 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
5069 gcc_cdiag_char_table
;
5070 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
5071 gcc_cxxdiag_char_table
;
5072 dynamic_format_types
[gcc_dump_printf_format_type
].conversion_specs
=
5073 gcc_dump_printf_char_table
;
5076 #ifdef TARGET_FORMAT_TYPES
5077 extern const format_kind_info TARGET_FORMAT_TYPES
[];
5080 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5081 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
5083 #ifdef TARGET_OVERRIDES_FORMAT_INIT
5084 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
5087 /* Attributes such as "printf" are equivalent to those such as
5088 "gnu_printf" unless this is overridden by a target. */
5089 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
5091 { "gnu_printf", "printf" },
5092 { "gnu_scanf", "scanf" },
5093 { "gnu_strftime", "strftime" },
5094 { "gnu_strfmon", "strfmon" },
5098 /* Translate to unified attribute name. This is used in decode_format_type and
5099 decode_format_attr. In attr_name the user specified argument is passed. It
5100 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5101 or the attr_name passed to this function, if there is no matching entry. */
5103 convert_format_name_to_system_name (const char *attr_name
)
5107 if (attr_name
== NULL
|| *attr_name
== 0
5108 || startswith (attr_name
, "gcc_"))
5110 #ifdef TARGET_OVERRIDES_FORMAT_INIT
5111 TARGET_OVERRIDES_FORMAT_INIT ();
5114 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5115 /* Check if format attribute is overridden by target. */
5116 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
!= NULL
5117 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
5119 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
5121 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
5124 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
5126 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
5130 /* Otherwise default to gnu format. */
5132 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
5135 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
5138 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
5140 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
5146 /* Handle a "format" attribute; arguments as in
5147 struct attribute_spec.handler. */
5149 handle_format_attribute (tree
*node
, tree atname
, tree args
,
5150 int flags
, bool *no_add_attrs
)
5152 const_tree type
= *node
;
5153 function_format_info info
;
5155 #ifdef TARGET_FORMAT_TYPES
5156 /* If the target provides additional format types, we need to
5157 add them to FORMAT_TYPES at first use. */
5158 if (!dynamic_format_types
)
5160 dynamic_format_types
= XNEWVEC (format_kind_info
,
5161 n_format_types
+ TARGET_N_FORMAT_TYPES
);
5162 memcpy (dynamic_format_types
, format_types_orig
,
5163 sizeof (format_types_orig
));
5164 memcpy (&dynamic_format_types
[n_format_types
], TARGET_FORMAT_TYPES
,
5165 TARGET_N_FORMAT_TYPES
* sizeof (dynamic_format_types
[0]));
5167 format_types
= dynamic_format_types
;
5168 /* Provide a reference for the first potential external type. */
5169 first_target_format_type
= n_format_types
;
5170 n_format_types
+= TARGET_N_FORMAT_TYPES
;
5174 /* Canonicalize name of format function. */
5175 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
5176 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
5178 if (!decode_format_attr (type
, atname
, args
, &info
, /* validated_p = */false))
5180 *no_add_attrs
= true;
5184 if (prototype_p (type
))
5186 if (!check_format_string (type
, info
.format_num
, flags
,
5187 no_add_attrs
, info
.format_type
))
5190 if (info
.first_arg_num
!= 0)
5192 unsigned HOST_WIDE_INT arg_num
= 1;
5193 function_args_iterator iter
;
5196 /* Verify that first_arg_num points to the last arg,
5198 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
5201 if (arg_num
!= info
.first_arg_num
)
5203 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
5204 error ("argument to be formatted is not %<...%>");
5205 *no_add_attrs
= true;
5211 /* Check if this is a strftime variant. Just for this variant
5212 FMT_FLAG_ARG_CONVERT is not set. */
5213 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
5214 && info
.first_arg_num
!= 0)
5216 error ("strftime formats cannot format arguments");
5217 *no_add_attrs
= true;
5221 /* If this is a custom GCC-internal format type, we have to
5222 initialize certain bits at runtime. */
5223 if (info
.format_type
== asm_fprintf_format_type
5224 || info
.format_type
== gcc_gfc_format_type
5225 || info
.format_type
== gcc_diag_format_type
5226 || info
.format_type
== gcc_tdiag_format_type
5227 || info
.format_type
== gcc_cdiag_format_type
5228 || info
.format_type
== gcc_cxxdiag_format_type
5229 || info
.format_type
== gcc_dump_printf_format_type
)
5231 /* Our first time through, we have to make sure that our
5232 format_type data is allocated dynamically and is modifiable. */
5233 if (!dynamic_format_types
)
5234 format_types
= dynamic_format_types
= (format_kind_info
*)
5235 xmemdup (format_types_orig
, sizeof (format_types_orig
),
5236 sizeof (format_types_orig
));
5238 /* If this is format __asm_fprintf__, we have to initialize
5239 GCC's notion of HOST_WIDE_INT for checking %wd. */
5240 if (info
.format_type
== asm_fprintf_format_type
)
5241 init_dynamic_asm_fprintf_info ();
5242 /* If this is format __gcc_gfc__, we have to initialize GCC's
5243 notion of 'locus' at runtime for %L. */
5244 else if (info
.format_type
== gcc_gfc_format_type
)
5245 init_dynamic_gfc_info ();
5246 /* If this is one of the diagnostic attributes, then we have to
5247 initialize 'location_t' and 'tree' at runtime. */
5248 else if (info
.format_type
== gcc_diag_format_type
5249 || info
.format_type
== gcc_tdiag_format_type
5250 || info
.format_type
== gcc_cdiag_format_type
5251 || info
.format_type
== gcc_cxxdiag_format_type
5252 || info
.format_type
== gcc_dump_printf_format_type
)
5253 init_dynamic_diag_info ();
5263 namespace selftest
{
5265 /* Selftests of location handling. */
5267 /* Get the format_kind_info with the given name. */
5269 static const format_kind_info
*
5270 get_info (const char *name
)
5272 int idx
= decode_format_type (name
);
5273 const format_kind_info
*fki
= &format_types
[idx
];
5274 ASSERT_STREQ (fki
->name
, name
);
5278 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
5279 is EXPECTED_FORMAT. */
5282 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
5283 const char *expected_format
, tree type
,
5284 char conversion_char
)
5287 gcc_assert (expected_format
);
5290 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
5291 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
5292 free (actual_format
);
5295 /* Selftests for get_format_for_type. */
5297 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
5298 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
5299 (TYPE), (CONVERSION_CHAR))
5301 /* Selftest for get_format_for_type for "printf"-style functions. */
5304 test_get_format_for_type_printf ()
5306 const format_kind_info
*fki
= get_info ("gnu_printf");
5307 ASSERT_NE (fki
, NULL
);
5309 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
5310 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
5311 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
5312 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
5313 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
5314 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
5315 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
5316 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
5317 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
5318 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
5319 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
5320 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
5321 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
5322 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
5323 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
5324 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
5325 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
5326 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
5327 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
5328 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
5329 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
5330 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
5331 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
5332 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
5333 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
5334 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
5335 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
5336 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
5339 /* Selftest for get_format_for_type for "scanf"-style functions. */
5342 test_get_format_for_type_scanf ()
5344 const format_kind_info
*fki
= get_info ("gnu_scanf");
5345 ASSERT_NE (fki
, NULL
);
5346 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
5347 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
5348 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
5349 build_pointer_type (long_integer_type_node
), 'd');
5350 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
5351 build_pointer_type (long_unsigned_type_node
), 'u');
5352 ASSERT_FORMAT_FOR_TYPE_STREQ
5353 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
5354 ASSERT_FORMAT_FOR_TYPE_STREQ
5355 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
5356 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
5357 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
5360 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
5362 /* Exercise the type-printing label code, to give some coverage
5363 under "make selftest-valgrind" (in particular, to ensure that
5364 the label-printing machinery doesn't leak). */
5367 test_type_mismatch_range_labels ()
5369 /* Create a tempfile and write some text to it.
5370 ....................0000000001 11111111 12 22222222
5371 ....................1234567890 12345678 90 12345678. */
5372 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
5373 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
5374 line_table_test ltt
;
5376 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
5378 location_t c17
= linemap_position_for_column (line_table
, 17);
5379 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
5380 location_t c18
= linemap_position_for_column (line_table
, 18);
5381 location_t c24
= linemap_position_for_column (line_table
, 24);
5382 location_t c26
= linemap_position_for_column (line_table
, 26);
5384 /* Don't attempt to run the tests if column data might be unavailable. */
5385 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
5388 location_t fmt
= make_location (c18
, c17
, c18
);
5389 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
5391 location_t param
= make_location (c24
, c24
, c26
);
5392 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
5394 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
5395 integer_type_node
, 1);
5396 range_label_for_type_mismatch
param_label (integer_type_node
,
5398 gcc_rich_location
richloc (fmt
, &fmt_label
);
5399 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
5401 test_diagnostic_context dc
;
5402 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
5403 if (c_dialect_cxx ())
5404 /* "char*", without a space. */
5405 ASSERT_STREQ (" printf (\"msg: %i\\n\", msg);\n"
5409 pp_formatted_text (dc
.printer
));
5411 /* "char *", with a space. */
5412 ASSERT_STREQ (" printf (\"msg: %i\\n\", msg);\n"
5417 pp_formatted_text (dc
.printer
));
5420 /* Run all of the selftests within this file. */
5425 test_get_modifier_for_format_len ();
5426 test_get_format_for_type_printf ();
5427 test_get_format_for_type_scanf ();
5428 test_type_mismatch_range_labels ();
5431 } // namespace selftest
5433 #endif /* CHECKING_P */
5435 #include "gt-c-family-c-format.h"