1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2023 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 bool validate_constant (const_tree fn
, 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 the validate_constant call. */
176 tree
*format_num_expr
= &TREE_VALUE (args
);
177 unsigned HOST_WIDE_INT format_num
= 0;
179 if (!validate_constant (type
, atname
, *format_num_expr
, 0, &format_num
, 0,
182 *no_add_attrs
= true;
186 if (prototype_p (type
))
188 /* The format arg can be any string reference valid for the language and
189 target. We cannot be more specific in this case. */
190 if (!check_format_string (type
, format_num
, flags
, no_add_attrs
, -1))
194 if (!valid_format_string_type_p (TREE_TYPE (type
)))
196 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
197 error ("function does not return string type");
198 *no_add_attrs
= true;
205 /* Verify that the format_num argument is actually a string reference suitable,
206 for the language dialect and target (in case the format attribute is in
207 error). When we know the specific reference type expected, this is also
210 check_format_string (const_tree fntype
, unsigned HOST_WIDE_INT format_num
,
211 int flags
, bool *no_add_attrs
, int expected_format_type
)
213 unsigned HOST_WIDE_INT i
;
214 bool is_objc_sref
, is_target_sref
, is_char_ref
;
217 function_args_iterator iter
;
220 FOREACH_FUNCTION_ARGS (fntype
, ref
, iter
)
228 || !valid_format_string_type_p (ref
))
230 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
231 error ("format string argument is not a string type");
232 *no_add_attrs
= true;
236 /* We only know that we want a suitable string reference. */
237 if (expected_format_type
< 0)
240 /* Now check that the arg matches the expected type. */
242 (TYPE_MAIN_VARIANT (TREE_TYPE (ref
)) == char_type_node
);
244 fmt_flags
= format_flags (expected_format_type
);
245 is_objc_sref
= is_target_sref
= false;
247 is_objc_sref
= objc_string_ref_type_p (ref
);
249 if (!(fmt_flags
& FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
))
252 return true; /* OK, we expected a char and found one. */
255 /* We expected a char but found an extended string type. */
257 error ("found a %qs reference but the format argument should"
258 " be a string", format_name (gcc_objc_string_format_type
));
260 error ("found a %qT but the format argument should be a string",
262 *no_add_attrs
= true;
267 /* We expect a string object type as the format arg. */
270 error ("format argument should be a %qs reference but"
271 " a string was found", format_name (expected_format_type
));
272 *no_add_attrs
= true;
276 /* We will assert that objective-c will support either its own string type
277 or the target-supplied variant. */
279 is_target_sref
= (*targetcm
.string_object_ref_type_p
) ((const_tree
) ref
);
281 if (expected_format_type
== (int) gcc_objc_string_format_type
282 && (is_objc_sref
|| is_target_sref
))
285 /* We will allow a target string ref to match only itself. */
286 if (first_target_format_type
287 && expected_format_type
>= first_target_format_type
292 error ("format argument should be a %qs reference",
293 format_name (expected_format_type
));
294 *no_add_attrs
= true;
299 /* Under the control of FLAGS, verify EXPR is a valid constant that
300 refers to a positional argument ARGNO having a string type (char*
301 or, for targets like Darwin, a pointer to struct CFString) to
302 a function FN declared with attribute ATNAME. If valid, store the
303 constant's integer value in *VALUE and return true. If VALIDATED_P
304 is true assert the validation is successful.
306 N.B. This function modifies EXPR. */
309 validate_constant (const_tree fn
, const_tree atname
, tree
&expr
, int argno
,
310 unsigned HOST_WIDE_INT
*value
, int flags
, bool validated_p
)
312 /* Require the referenced argument to have a string type. For targets
313 like Darwin, also accept pointers to struct CFString. */
314 if (tree val
= positional_argument (fn
, atname
, expr
, STRING_CST
,
317 *value
= TREE_INT_CST_LOW (val
);
321 gcc_assert (!validated_p
);
325 /* Decode the arguments to a "format" attribute into a
326 function_format_info structure. It is already known that the list
327 is of the right length. If VALIDATED_P is true, then these
328 attributes have already been validated and must not be erroneous;
329 if false, it will give an error message. FN is either a function
330 declaration or function type. Returns true if the attributes are
331 successfully decoded, false otherwise. */
334 decode_format_attr (const_tree fn
, tree atname
, tree args
,
335 function_format_info
*info
, bool validated_p
)
337 tree format_type_id
= TREE_VALUE (args
);
338 /* Note that TREE_VALUE (args) is changed in place below. Ditto
339 for the value of the next element on the list. */
340 tree
*format_num_expr
= &TREE_VALUE (TREE_CHAIN (args
));
341 tree
*first_arg_num_expr
= &TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
343 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
345 gcc_assert (!validated_p
);
346 error ("unrecognized format specifier");
351 const char *p
= IDENTIFIER_POINTER (format_type_id
);
353 info
->format_type
= decode_format_type (p
, &info
->is_raw
);
355 if (!c_dialect_objc ()
356 && info
->format_type
== gcc_objc_string_format_type
)
358 gcc_assert (!validated_p
);
359 warning (OPT_Wformat_
, "%qE is only allowed in Objective-C dialects",
361 info
->format_type
= format_type_error
;
365 if (info
->format_type
== format_type_error
)
367 gcc_assert (!validated_p
);
368 warning (OPT_Wformat_
, "%qE is an unrecognized format function type",
374 if (!validate_constant (fn
, atname
, *format_num_expr
, 2, &info
->format_num
,
378 if (!validate_constant (fn
, atname
, *first_arg_num_expr
, 3,
379 &info
->first_arg_num
,
380 (POSARG_ZERO
| POSARG_ELLIPSIS
), validated_p
))
383 if (info
->first_arg_num
!= 0 && info
->first_arg_num
<= info
->format_num
)
385 gcc_assert (!validated_p
);
386 error ("format string argument follows the arguments to be formatted");
393 /* Check a call to a format function against a parameter list. */
395 /* The C standard version C++ is treated as equivalent to
396 or inheriting from, for the purpose of format features supported. */
397 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
398 /* The C standard version we are checking formats against when pedantic. */
399 #define C_STD_VER ((int) (c_dialect_cxx () \
400 ? CPLUSPLUS_STD_VER \
405 : (flag_isoc94 ? STD_C94 : STD_C89)))))
406 /* The name to give to the standard version we are warning about when
407 pedantic. FEATURE_VER is the version in which the feature warned out
408 appeared, which is higher than C_STD_VER. */
409 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
410 ? (cxx_dialect < cxx11 ? "ISO C++98" \
412 : ((FEATURE_VER) == STD_EXT \
414 : ((FEATURE_VER) == STD_C2X \
417 /* Adjust a C standard version, which may be STD_C9L, to account for
418 -Wno-long-long. Returns other standard versions unchanged. */
419 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
420 ? (warn_long_long ? STD_C99 : STD_C89) \
423 /* Enum describing the kind of specifiers present in the format and
424 requiring an argument. */
425 enum format_specifier_kind
{
428 CF_KIND_FIELD_PRECISION
431 static const char *kind_descriptions
[] = {
433 N_("field width specifier"),
434 N_("field precision specifier")
437 /* Structure describing details of a type expected in format checking,
438 and the type to check against it. */
439 struct format_wanted_type
441 /* The type wanted. */
443 /* The name of this type to use in diagnostics. */
444 const char *wanted_type_name
;
445 /* Should be type checked just for scalar width identity. */
446 int scalar_identity_flag
;
447 /* The level of indirection through pointers at which this type occurs. */
449 /* Whether, when pointer_count is 1, to allow any character type when
450 pedantic, rather than just the character or void type specified. */
451 int char_lenient_flag
;
452 /* Whether the argument, dereferenced once, is written into and so the
453 argument must not be a pointer to a const-qualified type. */
455 /* Whether the argument, dereferenced once, is read from and so
456 must not be a NULL pointer. */
457 int reading_from_flag
;
458 /* The kind of specifier that this type is used for. */
459 enum format_specifier_kind kind
;
460 /* The starting character of the specifier. This never includes the
461 initial percent sign. */
462 const char *format_start
;
463 /* The length of the specifier. */
465 /* The actual parameter to check against the wanted type. */
467 /* The argument number of that parameter. */
469 /* The offset location of this argument with respect to the format
471 unsigned int offset_loc
;
472 /* The next type to check for this format conversion, or NULL if none. */
473 struct format_wanted_type
*next
;
476 /* Convenience macro for format_length_info meaning unused. */
477 #define NO_FMT NULL, FMT_LEN_none, STD_C89
479 static const format_length_info printf_length_specs
[] =
481 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
482 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
483 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
484 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
485 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
486 { "Z", FMT_LEN_z
, STD_EXT
, NO_FMT
, 0 },
487 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
488 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
489 { "H", FMT_LEN_H
, STD_C2X
, NO_FMT
, 0 },
490 { "D", FMT_LEN_D
, STD_C2X
, "DD", FMT_LEN_DD
, STD_C2X
, 0 },
491 { "w8", FMT_LEN_w8
, STD_C2X
, NO_FMT
, 0 },
492 { "w16", FMT_LEN_w16
, STD_C2X
, NO_FMT
, 0 },
493 { "w32", FMT_LEN_w32
, STD_C2X
, NO_FMT
, 0 },
494 { "w64", FMT_LEN_w64
, STD_C2X
, NO_FMT
, 0 },
495 { "wf8", FMT_LEN_wf8
, STD_C2X
, NO_FMT
, 0 },
496 { "wf16", FMT_LEN_wf16
, STD_C2X
, NO_FMT
, 0 },
497 { "wf32", FMT_LEN_wf32
, STD_C2X
, NO_FMT
, 0 },
498 { "wf64", FMT_LEN_wf64
, STD_C2X
, NO_FMT
, 0 },
499 { NO_FMT
, NO_FMT
, 0 }
502 /* Length specifiers valid for asm_fprintf. */
503 static const format_length_info asm_fprintf_length_specs
[] =
505 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
506 { "w", FMT_LEN_w
, STD_C89
, NO_FMT
, 0 },
507 { NO_FMT
, NO_FMT
, 0 }
510 /* Length specifiers valid for GCC diagnostics. */
511 static const format_length_info gcc_diag_length_specs
[] =
513 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
514 { "w", FMT_LEN_w
, STD_C89
, NO_FMT
, 0 },
515 { NO_FMT
, NO_FMT
, 0 }
518 /* The custom diagnostics all accept the same length specifiers. */
519 #define gcc_tdiag_length_specs gcc_diag_length_specs
520 #define gcc_cdiag_length_specs gcc_diag_length_specs
521 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
522 #define gcc_dump_printf_length_specs gcc_diag_length_specs
524 /* This differs from printf_length_specs only in that "Z" is not accepted. */
525 static const format_length_info scanf_length_specs
[] =
527 { "h", FMT_LEN_h
, STD_C89
, "hh", FMT_LEN_hh
, STD_C99
, 0 },
528 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C9L
, 0 },
529 { "q", FMT_LEN_ll
, STD_EXT
, NO_FMT
, 0 },
530 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
531 { "z", FMT_LEN_z
, STD_C99
, NO_FMT
, 0 },
532 { "t", FMT_LEN_t
, STD_C99
, NO_FMT
, 0 },
533 { "j", FMT_LEN_j
, STD_C99
, NO_FMT
, 0 },
534 { "H", FMT_LEN_H
, STD_C2X
, NO_FMT
, 0 },
535 { "D", FMT_LEN_D
, STD_C2X
, "DD", FMT_LEN_DD
, STD_C2X
, 0 },
536 { "w8", FMT_LEN_w8
, STD_C2X
, NO_FMT
, 0 },
537 { "w16", FMT_LEN_w16
, STD_C2X
, NO_FMT
, 0 },
538 { "w32", FMT_LEN_w32
, STD_C2X
, NO_FMT
, 0 },
539 { "w64", FMT_LEN_w64
, STD_C2X
, NO_FMT
, 0 },
540 { "wf8", FMT_LEN_wf8
, STD_C2X
, NO_FMT
, 0 },
541 { "wf16", FMT_LEN_wf16
, STD_C2X
, NO_FMT
, 0 },
542 { "wf32", FMT_LEN_wf32
, STD_C2X
, NO_FMT
, 0 },
543 { "wf64", FMT_LEN_wf64
, STD_C2X
, NO_FMT
, 0 },
544 { NO_FMT
, NO_FMT
, 0 }
548 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
549 make no sense for a format type not part of any C standard version. */
550 static const format_length_info strfmon_length_specs
[] =
552 /* A GNU extension. */
553 { "L", FMT_LEN_L
, STD_C89
, NO_FMT
, 0 },
554 { NO_FMT
, NO_FMT
, 0 }
558 /* Length modifiers used by the fortran/error.cc routines. */
559 static const format_length_info gcc_gfc_length_specs
[] =
561 { "l", FMT_LEN_l
, STD_C89
, "ll", FMT_LEN_ll
, STD_C89
, 0 },
562 { "w", FMT_LEN_w
, STD_C89
, NO_FMT
, 0 },
563 { NO_FMT
, NO_FMT
, 0 }
567 static const format_flag_spec printf_flag_specs
[] =
569 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
570 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
571 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
572 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
573 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
574 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT
},
575 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT
},
576 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
577 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
578 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
579 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
583 static const format_flag_pair printf_flag_pairs
[] =
587 { '0', 'p', 1, 'i' },
591 static const format_flag_spec asm_fprintf_flag_specs
[] =
593 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89
},
594 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
595 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
596 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89
},
597 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89
},
598 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89
},
599 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
600 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
601 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
604 static const format_flag_pair asm_fprintf_flag_pairs
[] =
608 { '0', 'p', 1, 'i' },
612 static const format_flag_pair gcc_diag_flag_pairs
[] =
617 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
618 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
619 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
620 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
621 #define gcc_dump_printf_flag_pairs gcc_diag_flag_pairs
623 static const format_flag_spec gcc_diag_flag_specs
[] =
625 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89
},
626 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89
},
627 { 'q', 0, 0, 1, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89
},
628 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89
},
629 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89
},
630 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
633 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
634 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
635 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
636 #define gcc_gfc_flag_specs gcc_diag_flag_specs
637 #define gcc_dump_printf_flag_specs gcc_diag_flag_specs
639 static const format_flag_spec scanf_flag_specs
[] =
641 { '*', 0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89
},
642 { 'a', 0, 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT
},
643 { 'm', 0, 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT
},
644 { 'w', 0, 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89
},
645 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89
},
646 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT
},
647 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT
},
648 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
652 static const format_flag_pair scanf_flag_pairs
[] =
660 static const format_flag_spec strftime_flag_specs
[] =
662 { '_', 0, 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT
},
663 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT
},
664 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT
},
665 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT
},
666 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT
},
667 { 'w', 0, 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT
},
668 { 'E', 0, 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99
},
669 { 'O', 0, 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99
},
670 { 'O', 'o', 0, 0, NULL
, N_("the 'O' modifier"), STD_EXT
},
671 { 'O', 'p', 0, 0, NULL
, N_("the 'O' modifier"), STD_C2X
},
672 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
676 static const format_flag_pair strftime_flag_pairs
[] =
687 static const format_flag_spec strfmon_flag_specs
[] =
689 { '=', 0, 1, 0, N_("fill character"), N_("fill character in strfmon format"), STD_C89
},
690 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89
},
691 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89
},
692 { '(', 0, 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89
},
693 { '!', 0, 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89
},
694 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89
},
695 { 'w', 0, 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89
},
696 { '#', 0, 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89
},
697 { 'p', 0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89
},
698 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89
},
699 { 0, 0, 0, 0, NULL
, NULL
, STD_C89
}
702 static const format_flag_pair strfmon_flag_pairs
[] =
709 static const format_char_info print_char_table
[] =
711 /* C89 conversion specifiers. */
712 { "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
, T2X_I8
, T2X_I16
, T2X_I32
, T2X_I64
, T2X_IF8
, T2X_IF16
, T2X_IF32
, T2X_IF64
}, "-wp0 +'I", "i", NULL
},
713 { "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
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "-wp0#", "i", NULL
},
714 { "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
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "-wp0'I", "i", NULL
},
715 { "fgG", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'I", "", NULL
},
716 { "eE", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#I", "", NULL
},
717 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T94_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
718 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
719 { "p", 1, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "c", NULL
},
720 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
, T2X_I8
, T2X_I16
, T2X_I32
, T2X_I64
, T2X_IF8
, T2X_IF16
, T2X_IF32
, T2X_IF64
}, "", "W", NULL
},
721 /* C99 conversion specifiers. */
722 { "F", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#'I", "", NULL
},
723 { "aA", 0, STD_C99
, { T99_D
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +#", "", NULL
},
724 /* C2X conversion specifiers. */
725 { "bB", 0, STD_C2X
, { T2X_UI
, T2X_UC
, T2X_US
, T2X_UL
, T2X_ULL
, TEX_ULL
, T2X_ST
, T2X_UPD
, T2X_UIM
, BADLEN
, BADLEN
, BADLEN
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "-wp0#", "i", NULL
},
726 /* X/Open conversion specifiers. */
727 { "C", 0, STD_EXT
, { TEX_WI
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
728 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "R", NULL
},
729 /* GNU conversion specifiers. */
730 { "m", 0, STD_EXT
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "", NULL
},
731 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
734 static const format_char_info asm_fprintf_char_table
[] =
736 /* C89 conversion specifiers. */
737 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i", NULL
},
738 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
739 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i", NULL
},
740 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
741 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
743 /* asm_fprintf conversion specifiers. */
744 { "O", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
745 { "R", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
746 { "I", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
747 { "L", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
748 { "U", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
749 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", NULL
},
750 { "z", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
751 { "@", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
752 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
755 /* GCC-specific format_char_info arrays. */
757 /* The conversion specifiers implemented within pp_format, and thus supported
758 by all pretty_printer instances within GCC. */
760 #define PP_FORMAT_CHAR_TABLE \
761 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
762 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
763 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
764 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
765 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL }, \
766 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL }, \
767 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "//cR", NULL }, \
768 { "@", 1, STD_C89, { T_EVENT_PTR, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL }, \
769 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL }, \
770 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL }, \
771 { "'" , 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
772 { "{", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL }, \
773 { "}", 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
774 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL }, \
775 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL }, \
776 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_diag_char_table[0] }
778 static const format_char_info gcc_diag_char_table
[] =
780 /* The conversion specifiers implemented within pp_format. */
781 PP_FORMAT_CHAR_TABLE
,
783 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
786 static const format_char_info gcc_tdiag_char_table
[] =
788 /* The conversion specifiers implemented within pp_format. */
789 PP_FORMAT_CHAR_TABLE
,
791 /* Custom conversion specifiers implemented by default_tree_printer. */
793 /* These will require a "tree" at runtime. */
794 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
795 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
797 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
800 static const format_char_info gcc_cdiag_char_table
[] =
802 /* The conversion specifiers implemented within pp_format. */
803 PP_FORMAT_CHAR_TABLE
,
805 /* Custom conversion specifiers implemented by c_tree_printer. */
807 /* These will require a "tree" at runtime. */
808 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
809 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
811 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
813 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
816 static const format_char_info gcc_cxxdiag_char_table
[] =
818 /* The conversion specifiers implemented within pp_format. */
819 PP_FORMAT_CHAR_TABLE
,
821 /* Custom conversion specifiers implemented by cp_printer. */
823 /* These will require a "tree" at runtime. */
824 { "ADFHISTVX",1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "'", NULL
},
825 { "E", 1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "", NULL
},
827 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
828 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
830 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
833 static const format_char_info gcc_gfc_char_table
[] =
835 /* C89 conversion specifiers. */
836 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
837 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
838 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
839 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "cR", NULL
},
841 /* gfc conversion specifiers. */
843 { "C", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
845 /* This will require a "locus" at runtime. */
846 { "L", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "R", NULL
},
848 /* These will require nothing. */
849 { "<>",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
850 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
853 static const format_char_info gcc_dump_printf_char_table
[] =
855 /* The conversion specifiers implemented within pp_format. */
856 PP_FORMAT_CHAR_TABLE
,
858 /* Custom conversion specifiers implemented by dump_pretty_printer. */
860 /* E and G require a "gimple *" argument at runtime. */
861 { "EG", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
863 /* C requires a "cgraph_node *" argument at runtime. */
864 { "C", 1, STD_C89
, { T_CGRAPH_NODE
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
866 /* T requires a "tree" at runtime. */
867 { "T", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
869 /* %f requires a "double"; it doesn't support modifiers. */
870 { "f", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
872 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
875 static const format_char_info scan_char_table
[] =
877 /* C89 conversion specifiers. */
878 { "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
, T2X_I8
, T2X_I16
, T2X_I32
, T2X_I64
, T2X_IF8
, T2X_IF16
, T2X_IF32
, T2X_IF64
}, "*w'I", "W", NULL
},
879 { "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
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "*w'I", "W", NULL
},
880 { "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
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "*w", "W", NULL
},
881 { "efgEG", 1, STD_C89
, { T89_F
, BADLEN
, BADLEN
, T89_D
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W", NULL
},
882 { "c", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "cW", NULL
},
883 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW", NULL
},
884 { "[", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, T94_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "cW[", NULL
},
885 { "p", 2, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w", "W", NULL
},
886 { "n", 1, STD_C89
, { T89_I
, T99_SC
, T89_S
, T89_L
, T9L_LL
, BADLEN
, T99_SST
, T99_PD
, T99_IM
, BADLEN
, BADLEN
, BADLEN
, T2X_I8
, T2X_I16
, T2X_I32
, T2X_I64
, T2X_IF8
, T2X_IF16
, T2X_IF32
, T2X_IF64
}, "", "W", NULL
},
887 /* C99 conversion specifiers. */
888 { "F", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W", NULL
},
889 { "aA", 1, STD_C99
, { T99_F
, BADLEN
, BADLEN
, T99_D
, BADLEN
, T99_LD
, BADLEN
, BADLEN
, BADLEN
, T2X_D32
, T2X_D64
, T2X_D128
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*w'", "W", NULL
},
890 /* C2X conversion specifiers. */
891 { "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
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "*w", "W", NULL
},
892 /* X/Open conversion specifiers. */
893 { "C", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*mw", "W", NULL
},
894 { "S", 1, STD_EXT
, { TEX_W
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "*amw", "W", NULL
},
895 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
898 static const format_char_info time_char_table
[] =
900 /* C89 conversion specifiers. */
901 { "AZa", 0, STD_C89
, NOLENGTHS
, "^#", "", NULL
},
902 { "Bb", 0, STD_C89
, NOLENGTHS
, "O^#", "p", NULL
},
903 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3", NULL
},
904 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "", NULL
},
905 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o", NULL
},
906 { "p", 0, STD_C89
, NOLENGTHS
, "#", "", NULL
},
907 { "X", 0, STD_C89
, NOLENGTHS
, "E", "", NULL
},
908 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4", NULL
},
909 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o", NULL
},
910 { "%", 0, STD_C89
, NOLENGTHS
, "", "", NULL
},
911 /* C99 conversion specifiers. */
912 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o", NULL
},
913 { "D", 0, STD_C99
, NOLENGTHS
, "", "2", NULL
},
914 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "", NULL
},
915 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "", NULL
},
916 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o", NULL
},
917 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o", NULL
},
918 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "", NULL
},
919 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o", NULL
},
920 /* GNU conversion specifiers. */
921 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "", NULL
},
922 { "P", 0, STD_EXT
, NOLENGTHS
, "", "", NULL
},
923 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
926 static const format_char_info monetary_char_table
[] =
928 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "", NULL
},
929 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
932 /* This must be in the same order as enum format_type. */
933 static const format_kind_info format_types_orig
[] =
935 { "gnu_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
936 printf_flag_specs
, printf_flag_pairs
,
937 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
938 'w', 0, 'p', 0, 'L', 0,
939 &integer_type_node
, &integer_type_node
941 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
942 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
943 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
944 'w', 0, 'p', 0, 'L', 0,
947 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q+#", NULL
,
948 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
949 FMT_FLAG_ARG_CONVERT
,
950 0, 0, 'p', 0, 'L', 0,
951 NULL
, &integer_type_node
953 { "gcc_tdiag", gcc_tdiag_length_specs
, gcc_tdiag_char_table
, "q+#", NULL
,
954 gcc_tdiag_flag_specs
, gcc_tdiag_flag_pairs
,
955 FMT_FLAG_ARG_CONVERT
,
956 0, 0, 'p', 0, 'L', 0,
957 NULL
, &integer_type_node
959 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q+#", NULL
,
960 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
961 FMT_FLAG_ARG_CONVERT
,
962 0, 0, 'p', 0, 'L', 0,
963 NULL
, &integer_type_node
965 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
966 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
967 FMT_FLAG_ARG_CONVERT
,
968 0, 0, 'p', 0, 'L', 0,
969 NULL
, &integer_type_node
971 { "gcc_gfc", gcc_gfc_length_specs
, gcc_gfc_char_table
, "q+#", NULL
,
972 gcc_gfc_flag_specs
, gcc_gfc_flag_pairs
,
973 FMT_FLAG_ARG_CONVERT
,
977 { "gcc_dump_printf", gcc_dump_printf_length_specs
,
978 gcc_dump_printf_char_table
, "q+#", NULL
,
979 gcc_dump_printf_flag_specs
, gcc_dump_printf_flag_pairs
,
980 FMT_FLAG_ARG_CONVERT
,
981 0, 0, 'p', 0, 'L', 0,
982 NULL
, &integer_type_node
984 { "NSString", NULL
, NULL
, NULL
, NULL
,
986 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
989 { "gnu_scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
990 scanf_flag_specs
, scanf_flag_pairs
,
991 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
992 'w', 0, 0, '*', 'L', 'm',
995 { "gnu_strftime", NULL
, time_char_table
, "_-0^#", "EO",
996 strftime_flag_specs
, strftime_flag_pairs
,
997 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0, 0,
1000 { "gnu_strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
1001 strfmon_flag_specs
, strfmon_flag_pairs
,
1002 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L', 0,
1007 /* This layer of indirection allows GCC to reassign format_types with
1008 new data if necessary, while still allowing the original data to be
1010 static const format_kind_info
*format_types
= format_types_orig
;
1011 /* We can modify this one. We also add target-specific format types
1012 to the end of the array. */
1013 static format_kind_info
*dynamic_format_types
;
1015 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
1017 /* Structure detailing the results of checking a format function call
1018 where the format expression may be a conditional expression with
1019 many leaves resulting from nested conditional expressions. */
1020 struct format_check_results
1022 /* Number of leaves of the format argument that could not be checked
1023 as they were not string literals. */
1024 int number_non_literal
;
1025 /* Number of leaves of the format argument that were null pointers or
1026 string literals, but had extra format arguments. */
1027 int number_extra_args
;
1028 location_t extra_arg_loc
;
1029 /* Number of leaves of the format argument that were null pointers or
1030 string literals, but had extra format arguments and used $ operand
1032 int number_dollar_extra_args
;
1033 /* Number of leaves of the format argument that were wide string
1036 /* Number of leaves of the format argument that are not array of "char". */
1037 int number_non_char
;
1038 /* Number of leaves of the format argument that were empty strings. */
1040 /* Number of leaves of the format argument that were unterminated
1042 int number_unterminated
;
1043 /* Number of leaves of the format argument that were not counted above. */
1045 /* Location of the format string. */
1046 location_t format_string_loc
;
1049 struct format_check_context
1051 format_check_results
*res
;
1052 function_format_info
*info
;
1054 vec
<location_t
> *arglocs
;
1057 /* Return the format name (as specified in the original table) for the format
1058 type indicated by format_num. */
1060 format_name (int format_num
)
1062 if (format_num
>= 0 && format_num
< n_format_types
)
1063 return format_types
[format_num
].name
;
1067 /* Return the format flags (as specified in the original table) for the format
1068 type indicated by format_num. */
1070 format_flags (int format_num
)
1072 if (format_num
>= 0 && format_num
< n_format_types
)
1073 return format_types
[format_num
].flags
;
1077 static void check_format_info (function_format_info
*, tree
,
1079 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
1080 static void check_format_info_main (format_check_results
*,
1081 function_format_info
*, const char *,
1084 unsigned HOST_WIDE_INT
,
1085 object_allocator
<format_wanted_type
> &,
1088 static void init_dollar_format_checking (int, tree
);
1089 static int maybe_read_dollar_number (const char **, int,
1090 tree
, tree
*, const format_kind_info
*);
1091 static bool avoid_dollar_number (const char *);
1092 static void finish_dollar_format_checking (format_check_results
*, int);
1094 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
1097 static void check_format_types (const substring_loc
&fmt_loc
,
1098 format_wanted_type
*,
1099 const format_kind_info
*fki
,
1100 int offset_to_type_start
,
1101 char conversion_char
,
1102 vec
<location_t
> *arglocs
);
1103 static void format_type_warning (const substring_loc
&fmt_loc
,
1104 location_t param_loc
,
1105 format_wanted_type
*, tree
,
1107 const format_kind_info
*fki
,
1108 int offset_to_type_start
,
1109 char conversion_char
);
1111 /* Decode a format type from a string, returning the type, or
1112 format_type_error if not valid, in which case the caller should
1113 print an error message. On success, when IS_RAW is non-null, set
1114 *IS_RAW when the format type corresponds to a GCC "raw" diagnostic
1115 formatting function and clear it otherwise. */
1117 decode_format_type (const char *s
, bool *is_raw
/* = NULL */)
1122 is_raw
= &is_raw_buf
;
1126 s
= convert_format_name_to_system_name (s
);
1128 size_t slen
= strlen (s
);
1129 for (int i
= 0; i
< n_format_types
; i
++)
1131 /* Check for a match with no underscores. */
1132 if (!strcmp (s
, format_types
[i
].name
))
1133 return static_cast<format_type
> (i
);
1135 /* Check for leading and trailing underscores. */
1136 size_t alen
= strlen (format_types
[i
].name
);
1137 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1138 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1139 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1140 return static_cast<format_type
>(i
);
1142 /* Check for the "_raw" suffix and no leading underscores. */
1143 if (slen
== alen
+ 4
1144 && !strncmp (s
, format_types
[i
].name
, alen
)
1145 && !strcmp (s
+ alen
, "_raw"))
1148 return static_cast<format_type
>(i
);
1151 /* Check for the "_raw__" suffix and leading underscores. */
1152 if (slen
== alen
+ 8 && s
[0] == '_' && s
[1] == '_'
1153 && !strncmp (s
+ 2, format_types
[i
].name
, alen
)
1154 && !strcmp (s
+ 2 + alen
, "_raw__"))
1157 return static_cast<format_type
>(i
);
1161 return format_type_error
;
1165 /* Check the argument list of a call to printf, scanf, etc.
1166 ATTRS are the attributes on the function type. There are NARGS argument
1167 values in the array ARGARRAY. FN is either a function declaration or
1168 function type. Also, if -Wsuggest-attribute=format, warn for calls to
1169 vprintf or vscanf in functions with no such format attribute themselves. */
1172 check_function_format (const_tree fn
, tree attrs
, int nargs
,
1173 tree
*argarray
, vec
<location_t
> *arglocs
)
1177 tree atname
= get_identifier ("format");
1178 bool skipped_default_format
= false;
1180 /* See if this function has any format attributes. */
1181 for (a
= attrs
; a
; a
= TREE_CHAIN (a
))
1183 if (is_attribute_p ("format", get_attribute_name (a
)))
1185 /* Yup; check it. */
1186 function_format_info info
;
1187 decode_format_attr (fn
, atname
, TREE_VALUE (a
), &info
,
1188 /*validated=*/true);
1190 /* Mingw32 targets have traditionally used ms_printf format for the
1191 printf function, and this format is built in GCC. But nowadays,
1192 if mingw-w64 is configured to target UCRT, the printf function
1193 uses the gnu_printf format (specified in the stdio.h header). This
1194 causes GCC to check both formats, which means that GCC would
1195 warn twice about the same issue when both formats are violated,
1196 e.g. for %lu used to print long long unsigned.
1198 Hence, if there is a built-in attribute specifier and at least
1199 one another, we skip the built-in one. See PR 95130 (but note that
1200 GCC ms_printf already supports %llu) and PR 92292. */
1202 if (!skipped_default_format
1204 && TREE_CODE (fn
) == FUNCTION_DECL
1205 && fndecl_built_in_p (fn
, BUILT_IN_NORMAL
)
1206 && (tree_to_uhwi (TREE_PURPOSE (TREE_VALUE (a
)))
1207 & (int) ATTR_FLAG_BUILT_IN
))
1210 for (aa
= attrs
; aa
; aa
= TREE_CHAIN (aa
))
1212 && is_attribute_p ("format", get_attribute_name (aa
)))
1214 skipped_default_format
= true;
1217 if (skipped_default_format
)
1223 /* FIXME: Rewrite all the internal functions in this file
1224 to use the ARGARRAY directly instead of constructing this
1226 tree params
= NULL_TREE
;
1228 for (i
= nargs
- 1; i
>= 0; i
--)
1229 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
1230 check_format_info (&info
, params
, arglocs
);
1233 /* Attempt to detect whether the current function might benefit
1234 from the format attribute if the called function is decorated
1235 with it. Avoid using calls with string literal formats for
1236 guidance since those are unlikely to be viable candidates. */
1237 if (warn_suggest_attribute_format
1238 && current_function_decl
!= NULL_TREE
1239 && info
.first_arg_num
== 0
1240 && (format_types
[info
.format_type
].flags
1241 & (int) FMT_FLAG_ARG_CONVERT
)
1242 /* c_strlen will fail for a function parameter but succeed
1243 for a literal or constant array. */
1244 && !c_strlen (argarray
[info
.format_num
- 1], 1))
1247 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1250 if (is_attribute_p ("format", get_attribute_name (c
))
1251 && (decode_format_type (IDENTIFIER_POINTER
1252 (TREE_VALUE (TREE_VALUE (c
))))
1253 == info
.format_type
))
1257 /* Check if the current function has a parameter to which
1258 the format attribute could be attached; if not, it
1259 can't be a candidate for a format attribute, despite
1260 the vprintf-like or vscanf-like call. */
1262 for (args
= DECL_ARGUMENTS (current_function_decl
);
1264 args
= DECL_CHAIN (args
))
1266 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1267 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1272 warning (OPT_Wsuggest_attribute_format
, "function %qD "
1273 "might be a candidate for %qs format attribute",
1274 current_function_decl
,
1275 format_types
[info
.format_type
].name
);
1283 /* Variables used by the checking of $ operand number formats. */
1284 static char *dollar_arguments_used
= NULL
;
1285 static char *dollar_arguments_pointer_p
= NULL
;
1286 static int dollar_arguments_alloc
= 0;
1287 static int dollar_arguments_count
;
1288 static int dollar_first_arg_num
;
1289 static int dollar_max_arg_used
;
1290 static int dollar_format_warned
;
1292 /* Initialize the checking for a format string that may contain $
1293 parameter number specifications; we will need to keep track of whether
1294 each parameter has been used. FIRST_ARG_NUM is the number of the first
1295 argument that is a parameter to the format, or 0 for a vprintf-style
1296 function; PARAMS is the list of arguments starting at this argument. */
1299 init_dollar_format_checking (int first_arg_num
, tree params
)
1301 tree oparams
= params
;
1303 dollar_first_arg_num
= first_arg_num
;
1304 dollar_arguments_count
= 0;
1305 dollar_max_arg_used
= 0;
1306 dollar_format_warned
= 0;
1307 if (first_arg_num
> 0)
1311 dollar_arguments_count
++;
1312 params
= TREE_CHAIN (params
);
1315 if (dollar_arguments_alloc
< dollar_arguments_count
)
1317 free (dollar_arguments_used
);
1318 free (dollar_arguments_pointer_p
);
1319 dollar_arguments_alloc
= dollar_arguments_count
;
1320 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
1321 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
1323 if (dollar_arguments_alloc
)
1325 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1326 if (first_arg_num
> 0)
1332 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1334 params
= TREE_CHAIN (params
);
1342 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1343 is set, it is an error if one is not found; otherwise, it is OK. If
1344 such a number is found, check whether it is within range and mark that
1345 numbered operand as being used for later checking. Returns the operand
1346 number if found and within range, zero if no such number was found and
1347 this is OK, or -1 on error. PARAMS points to the first operand of the
1348 format; PARAM_PTR is made to point to the parameter referred to. If
1349 a $ format is found, *FORMAT is updated to point just after it. */
1352 maybe_read_dollar_number (const char **format
,
1353 int dollar_needed
, tree params
, tree
*param_ptr
,
1354 const format_kind_info
*fki
)
1358 const char *fcp
= *format
;
1359 if (!ISDIGIT (*fcp
))
1363 warning (OPT_Wformat_
, "missing $ operand number in format");
1371 while (ISDIGIT (*fcp
))
1373 HOST_WIDE_INT nargnum
1374 = HOST_WIDE_INT_UC (10) * argnum
+ (*fcp
- '0');
1375 if ((int) nargnum
!= nargnum
)
1384 warning (OPT_Wformat_
, "missing $ operand number in format");
1391 if (pedantic
&& !dollar_format_warned
)
1393 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
1394 C_STD_NAME (STD_EXT
));
1395 dollar_format_warned
= 1;
1397 if (overflow_flag
|| argnum
== 0
1398 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1400 warning (OPT_Wformat_
, "operand number out of range in format");
1403 if (argnum
> dollar_max_arg_used
)
1404 dollar_max_arg_used
= argnum
;
1405 /* For vprintf-style functions we may need to allocate more memory to
1406 track which arguments are used. */
1407 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1410 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1411 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
1413 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
1415 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1416 nalloc
- dollar_arguments_alloc
);
1417 dollar_arguments_alloc
= nalloc
;
1419 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1420 && dollar_arguments_used
[argnum
- 1] == 1)
1422 dollar_arguments_used
[argnum
- 1] = 2;
1423 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
1427 dollar_arguments_used
[argnum
- 1] = 1;
1428 if (dollar_first_arg_num
)
1431 *param_ptr
= params
;
1432 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1433 *param_ptr
= TREE_CHAIN (*param_ptr
);
1435 /* This case shouldn't be caught here. */
1436 gcc_assert (*param_ptr
);
1443 /* Ensure that FORMAT does not start with a decimal number followed by
1444 a $; give a diagnostic and return true if it does, false otherwise. */
1447 avoid_dollar_number (const char *format
)
1449 if (!ISDIGIT (*format
))
1451 while (ISDIGIT (*format
))
1455 warning (OPT_Wformat_
,
1456 "%<$%>operand number used after format without operand number");
1463 /* Finish the checking for a format string that used $ operand number formats
1464 instead of non-$ formats. We check for unused operands before used ones
1465 (a serious error, since the implementation of the format function
1466 can't know what types to pass to va_arg to find the later arguments).
1467 and for unused operands at the end of the format (if we know how many
1468 arguments the format had, so not for vprintf). If there were operand
1469 numbers out of range on a non-vprintf-style format, we won't have reached
1470 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1474 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1477 bool found_pointer_gap
= false;
1478 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1480 if (!dollar_arguments_used
[i
])
1482 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1483 || dollar_arguments_pointer_p
[i
]))
1484 found_pointer_gap
= true;
1486 warning_at (res
->format_string_loc
, OPT_Wformat_
,
1487 "format argument %d unused before used argument %d "
1488 "in %<$%>-style format",
1489 i
+ 1, dollar_max_arg_used
);
1492 if (found_pointer_gap
1493 || (dollar_first_arg_num
1494 && dollar_max_arg_used
< dollar_arguments_count
))
1496 res
->number_other
--;
1497 res
->number_dollar_extra_args
++;
1502 /* Retrieve the specification for a format flag. SPEC contains the
1503 specifications for format flags for the applicable kind of format.
1504 FLAG is the flag in question. If PREDICATES is NULL, the basic
1505 spec for that flag must be retrieved and must exist. If
1506 PREDICATES is not NULL, it is a string listing possible predicates
1507 for the spec entry; if an entry predicated on any of these is
1508 found, it is returned, otherwise NULL is returned. */
1510 static const format_flag_spec
*
1511 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1514 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1516 if (spec
[i
].flag_char
!= flag
)
1518 if (predicates
!= NULL
)
1520 if (spec
[i
].predicate
!= 0
1521 && strchr (predicates
, spec
[i
].predicate
) != 0)
1524 else if (spec
[i
].predicate
== 0)
1527 gcc_assert (predicates
);
1532 /* Check the argument list of a call to printf, scanf, etc.
1533 INFO points to the function_format_info structure.
1534 PARAMS is the list of argument values. */
1537 check_format_info (function_format_info
*info
, tree params
,
1538 vec
<location_t
> *arglocs
)
1540 format_check_context format_ctx
;
1541 unsigned HOST_WIDE_INT arg_num
;
1543 format_check_results res
;
1544 /* Skip to format argument. If the argument isn't available, there's
1545 no work for us to do; prototype checking will catch the problem. */
1546 for (arg_num
= 1; ; ++arg_num
)
1550 if (arg_num
== info
->format_num
)
1552 params
= TREE_CHAIN (params
);
1554 format_tree
= TREE_VALUE (params
);
1555 params
= TREE_CHAIN (params
);
1556 if (format_tree
== 0)
1559 res
.number_non_literal
= 0;
1560 res
.number_extra_args
= 0;
1561 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1562 res
.number_dollar_extra_args
= 0;
1563 res
.number_wide
= 0;
1564 res
.number_non_char
= 0;
1565 res
.number_empty
= 0;
1566 res
.number_unterminated
= 0;
1567 res
.number_other
= 0;
1568 res
.format_string_loc
= input_location
;
1570 format_ctx
.res
= &res
;
1571 format_ctx
.info
= info
;
1572 format_ctx
.params
= params
;
1573 format_ctx
.arglocs
= arglocs
;
1575 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1576 format_tree
, arg_num
, OPT_Wformat_
);
1578 location_t loc
= format_ctx
.res
->format_string_loc
;
1580 if (res
.number_non_literal
> 0)
1582 /* Functions taking a va_list normally pass a non-literal format
1583 string. These functions typically are declared with
1584 first_arg_num == 0, so avoid warning in those cases. */
1585 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1587 /* For strftime-like formats, warn for not checking the format
1588 string; but there are no arguments to check. */
1589 warning_at (loc
, OPT_Wformat_nonliteral
,
1590 "format not a string literal, format string not checked");
1592 else if (info
->first_arg_num
!= 0)
1594 /* If there are no arguments for the format at all, we may have
1595 printf (foo) which is likely to be a security hole. */
1596 while (arg_num
+ 1 < info
->first_arg_num
)
1600 params
= TREE_CHAIN (params
);
1603 if (params
== 0 && warn_format_security
)
1604 warning_at (loc
, OPT_Wformat_security
,
1605 "format not a string literal and no format arguments");
1606 else if (params
== 0 && warn_format_nonliteral
)
1607 warning_at (loc
, OPT_Wformat_nonliteral
,
1608 "format not a string literal and no format arguments");
1610 warning_at (loc
, OPT_Wformat_nonliteral
,
1611 "format not a string literal, argument types not checked");
1615 /* If there were extra arguments to the format, normally warn. However,
1616 the standard does say extra arguments are ignored, so in the specific
1617 case where we have multiple leaves (conditional expressions or
1618 ngettext) allow extra arguments if at least one leaf didn't have extra
1619 arguments, but was otherwise OK (either non-literal or checked OK).
1620 If the format is an empty string, this should be counted similarly to the
1621 case of extra format arguments. */
1622 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1623 && res
.number_other
== 0)
1625 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1626 res
.extra_arg_loc
= loc
;
1627 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1628 "too many arguments for format");
1630 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1631 && res
.number_other
== 0)
1632 warning_at (loc
, OPT_Wformat_extra_args
,
1633 "unused arguments in %<$%>-style format");
1634 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1635 && res
.number_other
== 0)
1636 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1637 format_types
[info
->format_type
].name
);
1639 if (res
.number_wide
> 0)
1640 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1642 if (res
.number_non_char
> 0)
1643 warning_at (loc
, OPT_Wformat_
,
1644 "format string is not an array of type %qs", "char");
1646 if (res
.number_unterminated
> 0)
1647 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1650 /* Callback from check_function_arguments_recurse to check a
1651 format string. FORMAT_TREE is the format parameter. ARG_NUM
1652 is the number of the format argument. CTX points to a
1653 format_check_context. */
1656 check_format_arg (void *ctx
, tree format_tree
,
1657 unsigned HOST_WIDE_INT arg_num
)
1659 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1660 format_check_results
*res
= format_ctx
->res
;
1661 function_format_info
*info
= format_ctx
->info
;
1662 tree params
= format_ctx
->params
;
1663 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1666 HOST_WIDE_INT offset
;
1667 const char *format_chars
;
1668 tree array_size
= 0;
1671 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1673 /* Pull out a constant value if the front end didn't, and handle location
1675 format_tree
= fold_for_warn (format_tree
);
1676 STRIP_NOPS (format_tree
);
1678 if (integer_zerop (format_tree
))
1680 /* Skip to first argument to check, so we can see if this format
1681 has any arguments (it shouldn't). */
1682 while (arg_num
+ 1 < info
->first_arg_num
)
1686 params
= TREE_CHAIN (params
);
1691 res
->number_other
++;
1694 if (res
->number_extra_args
== 0)
1695 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1697 res
->number_extra_args
++;
1703 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1707 arg0
= TREE_OPERAND (format_tree
, 0);
1708 arg1
= TREE_OPERAND (format_tree
, 1);
1711 if (TREE_CODE (arg1
) == INTEGER_CST
)
1715 res
->number_non_literal
++;
1718 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1719 if (!cst_and_fits_in_hwi (arg1
))
1721 res
->number_non_literal
++;
1724 offset
= int_cst_value (arg1
);
1726 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1728 res
->number_non_literal
++;
1731 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1732 format_tree
= TREE_OPERAND (format_tree
, 0);
1733 if (format_types
[info
->format_type
].flags
1734 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1736 bool objc_str
= (info
->format_type
== gcc_objc_string_format_type
);
1737 /* We cannot examine this string here - but we can check that it is
1739 if (TREE_CODE (format_tree
) != CONST_DECL
1740 || !((objc_str
&& objc_string_ref_type_p (TREE_TYPE (format_tree
)))
1741 || (*targetcm
.string_object_ref_type_p
)
1742 ((const_tree
) TREE_TYPE (format_tree
))))
1744 res
->number_non_literal
++;
1747 /* Skip to first argument to check. */
1748 while (arg_num
+ 1 < info
->first_arg_num
)
1752 params
= TREE_CHAIN (params
);
1755 /* So, we have a valid literal string object and one or more params.
1756 We need to use an external helper to parse the string into format
1757 info. For Objective-C variants we provide the resource within the
1758 objc tree, for target variants, via a hook. */
1760 objc_check_format_arg (format_tree
, params
);
1761 else if (targetcm
.check_string_object_format_arg
)
1762 (*targetcm
.check_string_object_format_arg
) (format_tree
, params
);
1763 /* Else we can't handle it and retire quietly. */
1766 if (TREE_CODE (format_tree
) == ARRAY_REF
1767 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1768 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1769 format_tree
= TREE_OPERAND (format_tree
, 0);
1772 res
->number_non_literal
++;
1775 if (VAR_P (format_tree
)
1776 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1777 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1778 && TREE_CODE (array_init
) == STRING_CST
)
1780 /* Extract the string constant initializer. Note that this may include
1781 a trailing NUL character that is not in the array (e.g.
1782 const char a[3] = "foo";). */
1783 array_size
= DECL_SIZE_UNIT (format_tree
);
1784 format_tree
= array_init
;
1786 if (TREE_CODE (format_tree
) != STRING_CST
)
1788 res
->number_non_literal
++;
1791 tree underlying_type
1792 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
)));
1793 if (underlying_type
!= char_type_node
1794 && !(flag_char8_t
&& underlying_type
== char8_type_node
))
1796 if (underlying_type
== char16_type_node
1797 || underlying_type
== char32_type_node
1798 || underlying_type
== wchar_type_node
)
1801 res
->number_non_char
++;
1804 format_chars
= TREE_STRING_POINTER (format_tree
);
1805 format_length
= TREE_STRING_LENGTH (format_tree
);
1806 if (array_size
!= 0)
1808 /* Variable length arrays can't be initialized. */
1809 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1811 if (tree_fits_shwi_p (array_size
))
1813 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1814 if (array_size_value
> 0
1815 && array_size_value
== (int) array_size_value
1816 && format_length
> array_size_value
)
1817 format_length
= array_size_value
;
1822 if (offset
>= format_length
)
1824 res
->number_non_literal
++;
1827 format_chars
+= offset
;
1828 format_length
-= offset
;
1830 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1832 res
->number_unterminated
++;
1835 if (format_length
== 0)
1837 res
->number_empty
++;
1841 /* Skip to first argument to check. */
1842 while (arg_num
+ 1 < info
->first_arg_num
)
1846 params
= TREE_CHAIN (params
);
1849 /* Provisionally increment res->number_other; check_format_info_main
1850 will decrement it if it finds there are extra arguments, but this way
1851 need not adjust it for every return. */
1852 res
->number_other
++;
1853 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1854 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1855 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1858 /* Support class for argument_parser and check_format_info_main.
1859 Tracks any flag characters that have been applied to the
1860 current argument. */
1866 bool has_char_p (char ch
) const;
1867 void add_char (char ch
);
1868 void validate (const format_kind_info
*fki
,
1869 const format_char_info
*fci
,
1870 const format_flag_spec
*flag_specs
,
1871 const char * const format_chars
,
1872 tree format_string_cst
,
1873 location_t format_string_loc
,
1874 const char * const orig_format_chars
,
1877 int get_alloc_flag (const format_kind_info
*fki
);
1878 int assignment_suppression_p (const format_kind_info
*fki
);
1881 char m_flag_chars
[256];
1884 /* Support struct for argument_parser and check_format_info_main.
1885 Encapsulates any length modifier applied to the current argument. */
1887 class length_modifier
1891 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1892 scalar_identity_flag (0)
1896 length_modifier (const char *chars_
,
1897 enum format_lengths val_
,
1898 enum format_std_version std_
,
1899 int scalar_identity_flag_
)
1900 : chars (chars_
), val (val_
), std (std_
),
1901 scalar_identity_flag (scalar_identity_flag_
)
1906 enum format_lengths val
;
1907 enum format_std_version std
;
1908 int scalar_identity_flag
;
1911 /* Parsing one argument within a format string. */
1913 class argument_parser
1916 argument_parser (function_format_info
*info
, const char *&format_chars
,
1917 tree format_string_cst
,
1918 const char * const orig_format_chars
,
1919 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1920 int &has_operand_number
, tree first_fillin_param
,
1921 object_allocator
<format_wanted_type
> &fwt_pool_
,
1922 vec
<location_t
> *arglocs
);
1924 bool read_any_dollar ();
1926 bool read_format_flags ();
1929 read_any_format_width (tree
¶ms
,
1930 unsigned HOST_WIDE_INT
&arg_num
);
1933 read_any_format_left_precision ();
1936 read_any_format_precision (tree
¶ms
,
1937 unsigned HOST_WIDE_INT
&arg_num
);
1939 void handle_alloc_chars ();
1941 length_modifier
read_any_length_modifier ();
1943 void read_any_other_modifier ();
1945 const format_char_info
*find_format_char_info (char format_char
);
1948 validate_flag_pairs (const format_char_info
*fci
,
1952 give_y2k_warnings (const format_char_info
*fci
,
1955 void parse_any_scan_set (const format_char_info
*fci
);
1957 bool handle_conversions (const format_char_info
*fci
,
1958 const length_modifier
&len_modifier
,
1960 const char *&wanted_type_name
,
1961 unsigned HOST_WIDE_INT
&arg_num
,
1966 check_argument_type (const format_char_info
*fci
,
1967 const length_modifier
&len_modifier
,
1969 const char *&wanted_type_name
,
1970 const bool suppressed
,
1971 unsigned HOST_WIDE_INT
&arg_num
,
1973 const int alloc_flag
,
1974 const char * const format_start
,
1975 const char * const type_start
,
1976 location_t fmt_param_loc
,
1977 char conversion_char
);
1980 const function_format_info
*const info
;
1981 const format_kind_info
* const fki
;
1982 const format_flag_spec
* const flag_specs
;
1983 const char *start_of_this_format
;
1984 const char *&format_chars
;
1985 const tree format_string_cst
;
1986 const char * const orig_format_chars
;
1987 const location_t format_string_loc
;
1988 object_allocator
<format_wanted_type
> &fwt_pool
;
1989 flag_chars_t
&flag_chars
;
1991 tree main_arg_params
;
1992 int &has_operand_number
;
1993 const tree first_fillin_param
;
1994 format_wanted_type width_wanted_type
;
1995 format_wanted_type precision_wanted_type
;
1997 format_wanted_type main_wanted_type
;
1999 format_wanted_type
*first_wanted_type
;
2000 format_wanted_type
*last_wanted_type
;
2001 vec
<location_t
> *arglocs
;
2004 /* flag_chars_t's constructor. */
2006 flag_chars_t::flag_chars_t ()
2008 m_flag_chars
[0] = 0;
2011 /* Has CH been seen as a flag within the current argument? */
2014 flag_chars_t::has_char_p (char ch
) const
2016 return strchr (m_flag_chars
, ch
) != 0;
2019 /* Add CH to the flags seen within the current argument. */
2022 flag_chars_t::add_char (char ch
)
2024 int i
= strlen (m_flag_chars
);
2025 m_flag_chars
[i
++] = ch
;
2026 m_flag_chars
[i
] = 0;
2029 /* Validate the individual flags used, removing any that are invalid. */
2032 flag_chars_t::validate (const format_kind_info
*fki
,
2033 const format_char_info
*fci
,
2034 const format_flag_spec
*flag_specs
,
2035 const char * const format_chars
,
2036 tree format_string_cst
,
2037 location_t format_string_loc
,
2038 const char * const orig_format_chars
,
2044 bool quotflag
= false;
2046 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
2048 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2049 m_flag_chars
[i
], NULL
);
2050 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
2051 if (m_flag_chars
[i
] == fki
->length_code_char
)
2054 /* Remember if a quoting flag is seen. */
2055 quotflag
|= s
->quoting
;
2057 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
2059 format_warning_at_char (format_string_loc
, format_string_cst
,
2060 format_chars
- orig_format_chars
,
2062 "%s used with %<%%%c%> %s format",
2063 _(s
->name
), format_char
, fki
->name
);
2069 const format_flag_spec
*t
;
2070 if (ADJ_STD (s
->std
) > C_STD_VER
)
2071 warning_at (format_string_loc
, OPT_Wformat_
,
2072 "%s does not support %s",
2073 C_STD_NAME (s
->std
), _(s
->long_name
));
2074 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
2075 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
2077 const char *long_name
= (t
->long_name
!= NULL
2080 if (ADJ_STD (t
->std
) > C_STD_VER
)
2081 warning_at (format_string_loc
, OPT_Wformat_
,
2082 "%s does not support %s with"
2083 " the %<%%%c%> %s format",
2084 C_STD_NAME (t
->std
), _(long_name
),
2085 format_char
, fki
->name
);
2089 /* Detect quoting directives used within a quoted sequence, such
2090 as GCC's "%<...%qE". */
2091 if (quoted
&& s
->quoting
)
2093 format_warning_at_char (format_string_loc
, format_string_cst
,
2094 format_chars
- orig_format_chars
- 1,
2096 "%s used within a quoted sequence",
2100 m_flag_chars
[i
- d
] = 0;
2104 && strchr (fci
->flags2
, '\''))
2106 format_warning_at_char (format_string_loc
, format_string_cst
,
2107 format_chars
- orig_format_chars
,
2109 "%qc conversion used unquoted",
2114 /* Determine if an assignment-allocation has been set, requiring
2115 an extra char ** for writing back a dynamically-allocated char *.
2116 This is for handling the optional 'm' character in scanf. */
2119 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
2121 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2122 && has_char_p ('a'))
2124 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
2129 /* Determine if an assignment-suppression character was seen.
2130 ('*' in scanf, for discarding the converted input). */
2133 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
2135 if (fki
->suppression_char
2136 && has_char_p (fki
->suppression_char
))
2141 /* Constructor for argument_parser. Initialize for parsing one
2142 argument within a format string. */
2145 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
2146 tree format_string_cst_
,
2147 const char * const orig_format_chars_
,
2148 location_t format_string_loc_
,
2149 flag_chars_t
&flag_chars_
,
2150 int &has_operand_number_
,
2151 tree first_fillin_param_
,
2152 object_allocator
<format_wanted_type
> &fwt_pool_
,
2153 vec
<location_t
> *arglocs_
)
2155 fki (&format_types
[info
->format_type
]),
2156 flag_specs (fki
->flag_specs
),
2157 start_of_this_format (format_chars_
),
2158 format_chars (format_chars_
),
2159 format_string_cst (format_string_cst_
),
2160 orig_format_chars (orig_format_chars_
),
2161 format_string_loc (format_string_loc_
),
2162 fwt_pool (fwt_pool_
),
2163 flag_chars (flag_chars_
),
2165 main_arg_params (NULL
),
2166 has_operand_number (has_operand_number_
),
2167 first_fillin_param (first_fillin_param_
),
2168 first_wanted_type (NULL
),
2169 last_wanted_type (NULL
),
2174 /* Handle dollars at the start of format arguments, setting up main_arg_params
2177 Return true if format parsing is to continue, false otherwise. */
2180 argument_parser::read_any_dollar ()
2182 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
2184 /* Possibly read a $ operand number at the start of the format.
2185 If one was previously used, one is required here. If one
2186 is not used here, we can't immediately conclude this is a
2187 format without them, since it could be printf %m or scanf %*. */
2189 opnum
= maybe_read_dollar_number (&format_chars
, 0,
2191 &main_arg_params
, fki
);
2196 has_operand_number
= 1;
2197 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
2200 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
2202 if (avoid_dollar_number (format_chars
))
2208 /* Read any format flags, but do not yet validate them beyond removing
2209 duplicates, since in general validation depends on the rest of
2212 Return true if format parsing is to continue, false otherwise. */
2215 argument_parser::read_format_flags ()
2217 while (*format_chars
!= 0
2218 && strchr (fki
->flag_chars
, *format_chars
) != 0)
2220 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2221 *format_chars
, NULL
);
2222 if (flag_chars
.has_char_p (*format_chars
))
2224 format_warning_at_char (format_string_loc
, format_string_cst
,
2225 format_chars
+ 1 - orig_format_chars
,
2227 "repeated %s in format", _(s
->name
));
2230 flag_chars
.add_char (*format_chars
);
2232 if (s
->skip_next_char
)
2235 if (*format_chars
== 0)
2237 warning_at (format_string_loc
, OPT_Wformat_
,
2238 "missing fill character at end of strfmon format");
2248 /* Read any format width, possibly * or *m$.
2250 Return true if format parsing is to continue, false otherwise. */
2254 read_any_format_width (tree
¶ms
,
2255 unsigned HOST_WIDE_INT
&arg_num
)
2257 if (!fki
->width_char
)
2260 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
2262 flag_chars
.add_char (fki
->width_char
);
2263 /* "...a field width...may be indicated by an asterisk.
2264 In this case, an int argument supplies the field width..." */
2266 if (has_operand_number
!= 0)
2269 opnum
= maybe_read_dollar_number (&format_chars
,
2270 has_operand_number
== 1,
2277 has_operand_number
= 1;
2278 arg_num
= opnum
+ info
->first_arg_num
- 1;
2281 has_operand_number
= 0;
2285 if (avoid_dollar_number (format_chars
))
2288 if (info
->first_arg_num
!= 0)
2295 cur_param
= TREE_VALUE (params
);
2296 if (has_operand_number
<= 0)
2298 params
= TREE_CHAIN (params
);
2302 width_wanted_type
.wanted_type
= *fki
->width_type
;
2303 width_wanted_type
.wanted_type_name
= NULL
;
2304 width_wanted_type
.pointer_count
= 0;
2305 width_wanted_type
.char_lenient_flag
= 0;
2306 width_wanted_type
.scalar_identity_flag
= 0;
2307 width_wanted_type
.writing_in_flag
= 0;
2308 width_wanted_type
.reading_from_flag
= 0;
2309 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
2310 width_wanted_type
.format_start
= format_chars
- 1;
2311 width_wanted_type
.format_length
= 1;
2312 width_wanted_type
.param
= cur_param
;
2313 width_wanted_type
.arg_num
= arg_num
;
2314 width_wanted_type
.offset_loc
=
2315 format_chars
- orig_format_chars
;
2316 width_wanted_type
.next
= NULL
;
2317 if (last_wanted_type
!= 0)
2318 last_wanted_type
->next
= &width_wanted_type
;
2319 if (first_wanted_type
== 0)
2320 first_wanted_type
= &width_wanted_type
;
2321 last_wanted_type
= &width_wanted_type
;
2326 /* Possibly read a numeric width. If the width is zero,
2327 we complain if appropriate. */
2328 int non_zero_width_char
= false;
2329 int found_width
= false;
2330 while (ISDIGIT (*format_chars
))
2333 if (*format_chars
!= '0')
2334 non_zero_width_char
= true;
2337 if (found_width
&& !non_zero_width_char
&&
2338 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
2339 warning_at (format_string_loc
, OPT_Wformat_
,
2340 "zero width in %s format", fki
->name
);
2342 flag_chars
.add_char (fki
->width_char
);
2348 /* Read any format left precision (must be a number, not *). */
2350 argument_parser::read_any_format_left_precision ()
2352 if (fki
->left_precision_char
== 0)
2354 if (*format_chars
!= '#')
2358 flag_chars
.add_char (fki
->left_precision_char
);
2359 if (!ISDIGIT (*format_chars
))
2360 format_warning_at_char (format_string_loc
, format_string_cst
,
2361 format_chars
- orig_format_chars
,
2363 "empty left precision in %s format", fki
->name
);
2364 while (ISDIGIT (*format_chars
))
2368 /* Read any format precision, possibly * or *m$.
2370 Return true if format parsing is to continue, false otherwise. */
2374 read_any_format_precision (tree
¶ms
,
2375 unsigned HOST_WIDE_INT
&arg_num
)
2377 if (fki
->precision_char
== 0)
2379 if (*format_chars
!= '.')
2383 flag_chars
.add_char (fki
->precision_char
);
2384 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
2386 /* "...a...precision...may be indicated by an asterisk.
2387 In this case, an int argument supplies the...precision." */
2389 if (has_operand_number
!= 0)
2392 opnum
= maybe_read_dollar_number (&format_chars
,
2393 has_operand_number
== 1,
2400 has_operand_number
= 1;
2401 arg_num
= opnum
+ info
->first_arg_num
- 1;
2404 has_operand_number
= 0;
2408 if (avoid_dollar_number (format_chars
))
2411 if (info
->first_arg_num
!= 0)
2418 cur_param
= TREE_VALUE (params
);
2419 if (has_operand_number
<= 0)
2421 params
= TREE_CHAIN (params
);
2425 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
2426 precision_wanted_type
.wanted_type_name
= NULL
;
2427 precision_wanted_type
.pointer_count
= 0;
2428 precision_wanted_type
.char_lenient_flag
= 0;
2429 precision_wanted_type
.scalar_identity_flag
= 0;
2430 precision_wanted_type
.writing_in_flag
= 0;
2431 precision_wanted_type
.reading_from_flag
= 0;
2432 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
2433 precision_wanted_type
.param
= cur_param
;
2434 precision_wanted_type
.format_start
= format_chars
- 2;
2435 precision_wanted_type
.format_length
= 2;
2436 precision_wanted_type
.arg_num
= arg_num
;
2437 precision_wanted_type
.offset_loc
=
2438 format_chars
- orig_format_chars
;
2439 precision_wanted_type
.next
= NULL
;
2440 if (last_wanted_type
!= 0)
2441 last_wanted_type
->next
= &precision_wanted_type
;
2442 if (first_wanted_type
== 0)
2443 first_wanted_type
= &precision_wanted_type
;
2444 last_wanted_type
= &precision_wanted_type
;
2449 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
2450 && !ISDIGIT (*format_chars
))
2451 format_warning_at_char (format_string_loc
, format_string_cst
,
2452 format_chars
- orig_format_chars
,
2454 "empty precision in %s format", fki
->name
);
2455 while (ISDIGIT (*format_chars
))
2462 /* Parse any assignment-allocation flags, which request an extra
2463 char ** for writing back a dynamically-allocated char *.
2464 This is for handling the optional 'm' character in scanf,
2465 and, before C99, 'a' (for compatibility with a non-standard
2466 GNU libc extension). */
2469 argument_parser::handle_alloc_chars ()
2471 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
2473 flag_chars
.add_char (fki
->alloc_char
);
2477 /* Handle the scanf allocation kludge. */
2478 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2480 if (*format_chars
== 'a' && !flag_isoc99
)
2482 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
2483 || format_chars
[1] == '[')
2485 /* 'a' is used as a flag. */
2486 flag_chars
.add_char ('a');
2493 /* Look for length modifiers within the current format argument,
2494 returning a length_modifier instance describing it (or the
2495 default if one is not found).
2497 Issue warnings about non-standard modifiers. */
2500 argument_parser::read_any_length_modifier ()
2502 length_modifier result
;
2504 const format_length_info
*fli
= fki
->length_char_specs
;
2508 while (fli
->name
!= 0
2509 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
2513 format_chars
+= strlen (fli
->name
);
2514 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
2517 result
= length_modifier (fli
->double_name
, fli
->double_index
,
2518 fli
->double_std
, 0);
2522 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
2523 fli
->scalar_identity_flag
);
2525 flag_chars
.add_char (fki
->length_code_char
);
2529 /* Warn if the length modifier is non-standard. */
2530 if (ADJ_STD (result
.std
) > C_STD_VER
)
2531 warning_at (format_string_loc
, OPT_Wformat_
,
2532 "%s does not support the %qs %s length modifier",
2533 C_STD_NAME (result
.std
), result
.chars
,
2540 /* Read any other modifier (strftime E/O). */
2543 argument_parser::read_any_other_modifier ()
2545 if (fki
->modifier_chars
== NULL
)
2548 while (*format_chars
!= 0
2549 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2551 if (flag_chars
.has_char_p (*format_chars
))
2553 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2554 *format_chars
, NULL
);
2555 format_warning_at_char (format_string_loc
, format_string_cst
,
2556 format_chars
- orig_format_chars
,
2558 "repeated %s in format", _(s
->name
));
2561 flag_chars
.add_char (*format_chars
);
2566 /* Return the format_char_info corresponding to FORMAT_CHAR,
2567 potentially issuing a warning if the format char is
2568 not supported in the C standard version we are checking
2571 Issue a warning and return NULL if it is not found.
2573 Issue warnings about non-standard modifiers. */
2575 const format_char_info
*
2576 argument_parser::find_format_char_info (char format_char
)
2578 const format_char_info
*fci
= fki
->conversion_specs
;
2580 while (fci
->format_chars
!= 0
2581 && strchr (fci
->format_chars
, format_char
) == 0)
2583 if (fci
->format_chars
== 0)
2585 format_warning_at_char (format_string_loc
, format_string_cst
,
2586 format_chars
- orig_format_chars
,
2588 "unknown conversion type character"
2596 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2597 format_warning_at_char (format_string_loc
, format_string_cst
,
2598 format_chars
- orig_format_chars
,
2600 "%s does not support the %<%%%c%> %s format",
2601 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2607 /* Validate the pairs of flags used.
2608 Issue warnings about incompatible combinations of flags. */
2611 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2614 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2616 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2618 const format_flag_spec
*s
, *t
;
2619 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2621 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2623 if (bad_flag_pairs
[i
].predicate
!= 0
2624 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2626 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2627 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2628 if (bad_flag_pairs
[i
].ignored
)
2630 if (bad_flag_pairs
[i
].predicate
!= 0)
2631 warning_at (format_string_loc
, OPT_Wformat_
,
2632 "%s ignored with %s and %<%%%c%> %s format",
2633 _(s
->name
), _(t
->name
), format_char
,
2636 warning_at (format_string_loc
, OPT_Wformat_
,
2637 "%s ignored with %s in %s format",
2638 _(s
->name
), _(t
->name
), fki
->name
);
2642 if (bad_flag_pairs
[i
].predicate
!= 0)
2643 warning_at (format_string_loc
, OPT_Wformat_
,
2644 "use of %s and %s together with %<%%%c%> %s format",
2645 _(s
->name
), _(t
->name
), format_char
,
2648 warning_at (format_string_loc
, OPT_Wformat_
,
2649 "use of %s and %s together in %s format",
2650 _(s
->name
), _(t
->name
), fki
->name
);
2655 /* Give Y2K warnings. */
2658 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2661 if (!warn_format_y2k
)
2665 if (strchr (fci
->flags2
, '4') != 0)
2666 if (flag_chars
.has_char_p ('E'))
2670 else if (strchr (fci
->flags2
, '3') != 0)
2672 else if (strchr (fci
->flags2
, '2') != 0)
2675 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2676 "%<%%%c%> yields only last 2 digits of "
2677 "year in some locales", format_char
);
2678 else if (y2k_level
== 2)
2679 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2680 "%<%%%c%> yields only last 2 digits of year",
2684 /* Parse any "scan sets" enclosed in square brackets, e.g.
2685 for scanf-style calls. */
2688 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2690 if (strchr (fci
->flags2
, '[') == NULL
)
2693 /* Skip over scan set, in case it happens to have '%' in it. */
2694 if (*format_chars
== '^')
2696 /* Find closing bracket; if one is hit immediately, then
2697 it's part of the scan set rather than a terminator. */
2698 if (*format_chars
== ']')
2700 while (*format_chars
&& *format_chars
!= ']')
2702 if (*format_chars
!= ']')
2703 /* The end of the format string was reached. */
2704 format_warning_at_char (format_string_loc
, format_string_cst
,
2705 format_chars
- orig_format_chars
,
2707 "no closing %<]%> for %<%%[%> format");
2710 /* Return true if this argument is to be continued to be parsed,
2711 false to skip to next argument. */
2714 argument_parser::handle_conversions (const format_char_info
*fci
,
2715 const length_modifier
&len_modifier
,
2717 const char *&wanted_type_name
,
2718 unsigned HOST_WIDE_INT
&arg_num
,
2722 enum format_std_version wanted_type_std
;
2724 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2727 wanted_type
= (fci
->types
[len_modifier
.val
].type
2728 ? *fci
->types
[len_modifier
.val
].type
: 0);
2729 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2730 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2731 if (wanted_type
== 0)
2733 format_warning_at_char (format_string_loc
, format_string_cst
,
2734 format_chars
- orig_format_chars
,
2736 "use of %qs length modifier with %qc type"
2737 " character has either no effect"
2738 " or undefined behavior",
2739 len_modifier
.chars
, format_char
);
2740 /* Heuristic: skip one argument when an invalid length/type
2741 combination is encountered. */
2744 params
= TREE_CHAIN (params
);
2748 /* Warn if non-standard, provided it is more non-standard
2749 than the length and type characters that may already
2750 have been warned for. */
2751 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2752 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2754 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2755 format_warning_at_char (format_string_loc
, format_string_cst
,
2756 format_chars
- orig_format_chars
,
2758 "%s does not support the %<%%%s%c%> %s format",
2759 C_STD_NAME (wanted_type_std
),
2761 format_char
, fki
->name
);
2767 /* Check type of argument against desired type.
2769 Return true if format parsing is to continue, false otherwise. */
2773 check_argument_type (const format_char_info
*fci
,
2774 const length_modifier
&len_modifier
,
2776 const char *&wanted_type_name
,
2777 const bool suppressed
,
2778 unsigned HOST_WIDE_INT
&arg_num
,
2780 const int alloc_flag
,
2781 const char * const format_start
,
2782 const char * const type_start
,
2783 location_t fmt_param_loc
,
2784 char conversion_char
)
2786 if (info
->first_arg_num
== 0)
2789 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2792 if (main_arg_num
!= 0)
2795 warning_at (format_string_loc
, OPT_Wformat_
,
2796 "operand number specified with "
2797 "suppressed assignment");
2799 warning_at (format_string_loc
, OPT_Wformat_
,
2800 "operand number specified for format "
2801 "taking no argument");
2806 format_wanted_type
*wanted_type_ptr
;
2808 if (main_arg_num
!= 0)
2810 arg_num
= main_arg_num
;
2811 params
= main_arg_params
;
2816 if (has_operand_number
> 0)
2818 warning_at (format_string_loc
, OPT_Wformat_
,
2819 "missing $ operand number in format");
2823 has_operand_number
= 0;
2826 wanted_type_ptr
= &main_wanted_type
;
2834 cur_param
= TREE_VALUE (params
);
2835 params
= TREE_CHAIN (params
);
2838 wanted_type_ptr
->wanted_type
= wanted_type
;
2839 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2840 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2841 wanted_type_ptr
->char_lenient_flag
= 0;
2842 if (strchr (fci
->flags2
, 'c') != 0)
2843 wanted_type_ptr
->char_lenient_flag
= 1;
2844 wanted_type_ptr
->scalar_identity_flag
= 0;
2845 if (len_modifier
.scalar_identity_flag
)
2846 wanted_type_ptr
->scalar_identity_flag
= 1;
2847 wanted_type_ptr
->writing_in_flag
= 0;
2848 wanted_type_ptr
->reading_from_flag
= 0;
2850 wanted_type_ptr
->writing_in_flag
= 1;
2853 if (strchr (fci
->flags2
, 'W') != 0)
2854 wanted_type_ptr
->writing_in_flag
= 1;
2855 if (strchr (fci
->flags2
, 'R') != 0)
2856 wanted_type_ptr
->reading_from_flag
= 1;
2858 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2859 wanted_type_ptr
->param
= cur_param
;
2860 wanted_type_ptr
->arg_num
= arg_num
;
2861 wanted_type_ptr
->format_start
= format_start
;
2862 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2863 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2864 wanted_type_ptr
->next
= NULL
;
2865 if (last_wanted_type
!= 0)
2866 last_wanted_type
->next
= wanted_type_ptr
;
2867 if (first_wanted_type
== 0)
2868 first_wanted_type
= wanted_type_ptr
;
2869 last_wanted_type
= wanted_type_ptr
;
2874 wanted_type_ptr
= fwt_pool
.allocate ();
2876 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2877 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2882 if (first_wanted_type
!= 0)
2884 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2885 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2886 /* By default, use the end of the range for the caret location. */
2887 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2888 offset_to_format_end
,
2889 offset_to_format_start
, offset_to_format_end
);
2890 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2891 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2892 offset_to_type_start
,
2893 conversion_char
, arglocs
);
2899 /* Describes "paired tokens" within the format string that are
2900 expected to be balanced. */
2905 baltoks_t (): singlequote (), doublequote () { }
2907 typedef auto_vec
<const char *> balanced_tokens_t
;
2908 /* Vectors of pointers to opening brackets ('['), curly brackets ('{'),
2909 quoting directives (like GCC "%<"), parentheses, and angle brackets
2910 ('<'). Used to detect unbalanced tokens. */
2911 balanced_tokens_t brackets
;
2912 balanced_tokens_t curly
;
2913 balanced_tokens_t quotdirs
;
2914 balanced_tokens_t parens
;
2915 balanced_tokens_t pointy
;
2916 /* Pointer to the last opening quote. */
2917 const char *singlequote
;
2918 const char *doublequote
;
2921 /* Describes a keyword, operator, or other name. */
2925 const char *name
; /* Keyword/operator name. */
2926 unsigned char len
; /* Its length. */
2927 const char *alt
; /* Alternate spelling. */
2930 /* Helper for initializing global token_t arrays below. */
2931 #define NAME(name) { name, sizeof name - 1, NULL }
2933 /* C/C++ operators that are expected to be quoted within the format
2936 static const token_t c_opers
[] =
2938 NAME ("!="), NAME ("%="), NAME ("&&"), NAME ("&="), NAME ("*="),
2939 NAME ("++"), NAME ("+="), NAME ("--"), NAME ("-="), NAME ("->"),
2940 NAME ("/="), NAME ("<<"), NAME ("<<="), NAME ("<="), NAME ("=="),
2941 NAME (">="), NAME (">>="), NAME (">>"), NAME ("?:"), NAME ("^="),
2942 NAME ("|="), NAME ("||")
2945 static const token_t cxx_opers
[] =
2947 NAME ("->*"), NAME (".*"), NAME ("::"), NAME ("<=>")
2950 /* Common C/C++ keywords that are expected to be quoted within the format
2951 string. Keywords like auto, inline, or volatile are excluded because
2952 they are sometimes used in common terms like /auto variables/, /inline
2953 function/, or /volatile access/ where they should not be quoted. */
2955 static const token_t c_keywords
[] =
2958 #define NAME(name, alt) { name, sizeof name - 1, alt }
2960 NAME ("alignas", NULL
),
2961 NAME ("alignof", NULL
),
2963 NAME ("bool", NULL
),
2964 NAME ("char", NULL
),
2965 NAME ("const %", NULL
),
2966 NAME ("const-qualified", "%<const%>-qualified"),
2967 NAME ("float", NULL
),
2968 NAME ("ifunc", NULL
),
2970 NAME ("long double", NULL
),
2971 NAME ("long int", NULL
),
2972 NAME ("long long", NULL
),
2973 NAME ("malloc", NULL
),
2974 NAME ("noclone", NULL
),
2975 NAME ("noinline", NULL
),
2976 NAME ("nonnull", NULL
),
2977 NAME ("noreturn", NULL
),
2978 NAME ("offsetof", NULL
),
2979 NAME ("readonly", "read-only"),
2980 NAME ("readwrite", "read-write"),
2981 NAME ("restrict %", NULL
),
2982 NAME ("restrict-qualified", "%<restrict%>-qualified"),
2983 NAME ("short int", NULL
),
2984 NAME ("signed char", NULL
),
2985 NAME ("signed int", NULL
),
2986 NAME ("signed long", NULL
),
2987 NAME ("signed short", NULL
),
2988 NAME ("sizeof", NULL
),
2989 NAME ("typeof", NULL
),
2990 NAME ("unsigned char", NULL
),
2991 NAME ("unsigned int", NULL
),
2992 NAME ("unsigned long", NULL
),
2993 NAME ("unsigned short", NULL
),
2994 NAME ("volatile %", NULL
),
2995 NAME ("volatile-qualified", "%<volatile%>-qualified"),
2996 NAME ("weakref", NULL
),
2999 static const token_t cxx_keywords
[] =
3001 /* C++ only keywords and operators. */
3002 NAME ("catch", NULL
),
3003 NAME ("constexpr if", NULL
),
3004 NAME ("constexpr", NULL
),
3005 NAME ("constinit", NULL
),
3006 NAME ("consteval", NULL
),
3007 NAME ("decltype", NULL
),
3008 NAME ("nullptr", NULL
),
3009 NAME ("operator delete", NULL
),
3010 NAME ("operator new", NULL
),
3011 NAME ("typeid", NULL
),
3012 NAME ("typeinfo", NULL
)
3015 /* Blacklisted words such as misspellings that should be avoided in favor
3016 of the specified alternatives. */
3019 const char *name
; /* Bad word. */
3020 unsigned char len
; /* Its length. */
3021 const char *alt
; /* Preferred alternative. */
3024 NAME ("arg", "argument"),
3025 NAME ("bitfield", "bit-field"),
3026 NAME ("builtin function", "built-in function"),
3027 NAME ("can not", "cannot"),
3028 NAME ("commandline option", "command-line option"),
3029 NAME ("commandline", "command line"),
3030 NAME ("command line option", "command-line option"),
3031 NAME ("decl", "declaration"),
3032 NAME ("enumeral", "enumerated"),
3033 NAME ("floating point", "floating-point"),
3034 NAME ("nonstatic", "non-static"),
3035 NAME ("non-zero", "nonzero"),
3036 NAME ("reg", "register"),
3037 NAME ("stmt", "statement"),
3040 /* Common contractions that should be avoided in favor of the specified
3045 const char *name
; /* Contraction. */
3046 unsigned char len
; /* Its length. */
3047 const char *alt
; /* Preferred alternative. */
3050 NAME ("can't", "cannot"),
3051 NAME ("didn't", "did not"),
3052 /* These are commonly abused. Avoid diagnosing them for now.
3053 NAME ("isn't", "is not"),
3054 NAME ("don't", "is not"),
3056 NAME ("mustn't", "must not"),
3057 NAME ("needn't", "need not"),
3058 NAME ("should't", "should not"),
3059 NAME ("that's", "that is"),
3060 NAME ("there's", "there is"),
3061 NAME ("they're", "they are"),
3062 NAME ("what's", "what is"),
3063 NAME ("won't", "will not")
3066 /* Check for unquoted TOKENS. FORMAT_STRING_LOC is the location of
3067 the format string, FORMAT_STRING_CST the format string itself (as
3068 a tree), ORIG_FORMAT_CHARS and FORMAT_CHARS are pointers to
3069 the beginning of the format string and the character currently
3070 being processed, and BALTOKS describes paired "tokens" within
3071 the format string that are expected to be balanced.
3072 Returns a pointer to the last processed character or null when
3073 nothing was done. */
3076 check_tokens (const token_t
*tokens
, unsigned ntoks
,
3077 location_t format_string_loc
, tree format_string_cst
,
3078 const char *orig_format_chars
, const char *format_chars
,
3082 const int opt
= OPT_Wformat_diag
;
3083 /* Zero-based starting position of a problem sequence. */
3084 int fmtchrpos
= format_chars
- orig_format_chars
;
3086 /* For identifier-like "words," set to the word length. */
3088 /* Set for an operator, clear for an identifier/word. */
3089 bool is_oper
= false;
3090 bool underscore
= false;
3092 if (format_chars
[0] == '_' || ISALPHA (format_chars
[0]))
3094 while (format_chars
[wlen
] == '_' || ISALNUM (format_chars
[wlen
]))
3096 underscore
|= format_chars
[wlen
] == '_';
3103 for (unsigned i
= 0; i
!= ntoks
; ++i
)
3105 unsigned toklen
= tokens
[i
].len
;
3108 || strncmp (format_chars
, tokens
[i
].name
, toklen
))
3112 && format_chars
- orig_format_chars
> 0
3113 && (TOUPPER (format_chars
[-1]) == 'C'
3114 || TOUPPER (format_chars
[-1]) == 'G'))
3115 return format_chars
+ toklen
- 1; /* Reference to C++ or G++. */
3117 if (ISPUNCT (format_chars
[toklen
- 1]))
3119 if (format_chars
[toklen
- 1] == format_chars
[toklen
])
3120 return NULL
; /* Operator followed by another punctuator. */
3122 else if (ISALNUM (format_chars
[toklen
]))
3123 return NULL
; /* Keyword prefix for a longer word. */
3126 && format_chars
[0] == '-'
3127 && format_chars
[1] == '-'
3128 && ISALNUM (format_chars
[2]))
3129 return NULL
; /* Probably option like --help. */
3131 /* Allow this ugly warning for the time being. */
3133 && format_chars
- orig_format_chars
> 6
3134 && startswith (format_chars
- 7, " count >= width of "))
3135 return format_chars
+ 10;
3137 /* The token is a type if it ends in an alphabetic character. */
3138 bool is_type
= (ISALPHA (tokens
[i
].name
[toklen
- 1])
3139 && strchr (tokens
[i
].name
, ' '));
3141 /* Backtrack to the last alphabetic character (for tokens whose
3142 names end in '%'). */
3144 while (!ISALPHA (tokens
[i
].name
[toklen
- 1]))
3147 if (format_warning_substr (format_string_loc
, format_string_cst
,
3148 fmtchrpos
, fmtchrpos
+ toklen
, opt
,
3150 ? G_("unquoted type name %<%.*s%> in format")
3152 ? G_("unquoted operator %<%.*s%> in format")
3153 : G_("unquoted keyword %<%.*s%> in format"))),
3154 toklen
, format_chars
)
3156 inform (format_string_loc
, "use %qs instead", tokens
[i
].alt
);
3158 return format_chars
+ toklen
- 1;
3161 /* Diagnose unquoted __attribute__. Consider any parenthesized
3162 argument to the attribute to avoid redundant warnings for
3163 the double parentheses that might follow. */
3164 if (startswith (format_chars
, "__attribute"))
3166 unsigned nchars
= sizeof "__attribute" - 1;
3167 while ('_' == format_chars
[nchars
])
3170 for (int i
= nchars
; format_chars
[i
]; ++i
)
3171 if (' ' != format_chars
[i
])
3177 if (format_chars
[nchars
] == '(')
3179 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3183 if (format_chars
[nchars
] == '(')
3185 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3189 for (int i
= nchars
; format_chars
[i
]; ++i
)
3190 if (')' == format_chars
[i
])
3192 if (baltoks
.parens
.length () > 0)
3193 baltoks
.parens
.pop ();
3198 if (close
&& format_chars
[nchars
] == ')')
3200 if (baltoks
.parens
.length () > 0)
3201 baltoks
.parens
.pop ();
3206 format_warning_substr (format_string_loc
, format_string_cst
,
3207 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3208 "unquoted attribute in format");
3209 return format_chars
+ nchars
- 1;
3212 /* Diagnose unquoted built-ins. */
3213 if (format_chars
[0] == '_'
3214 && format_chars
[1] == '_'
3215 && (startswith (format_chars
+ 2, "atomic")
3216 || startswith (format_chars
+ 2, "builtin")
3217 || startswith (format_chars
+ 2, "sync")))
3219 format_warning_substr (format_string_loc
, format_string_cst
,
3220 fmtchrpos
, fmtchrpos
+ wlen
, opt
,
3221 "unquoted name of built-in function %<%.*s%> "
3223 wlen
, format_chars
);
3224 return format_chars
+ wlen
- 1;
3227 /* Diagnose unquoted substrings of alphanumeric characters containing
3228 underscores. They most likely refer to identifiers and should be
3231 format_warning_substr (format_string_loc
, format_string_cst
,
3232 format_chars
- orig_format_chars
,
3233 format_chars
+ wlen
- orig_format_chars
,
3235 "unquoted identifier or keyword %<%.*s%> in format",
3236 wlen
, format_chars
);
3239 /* Diagnose some common misspellings. */
3240 for (unsigned i
= 0; i
!= ARRAY_SIZE (badwords
); ++i
)
3242 unsigned badwlen
= strspn (badwords
[i
].name
, " -");
3244 && (wlen
<= badwords
[i
].len
3245 || (wlen
== badwords
[i
].len
+ 1U
3246 && TOUPPER (format_chars
[wlen
- 1]) == 'S'))
3247 && !strncasecmp (format_chars
, badwords
[i
].name
, badwords
[i
].len
))
3249 /* Handle singular as well as plural forms of all bad words
3250 even though the latter don't necessarily make sense for
3251 all of the former (like "can nots"). */
3252 badwlen
= badwords
[i
].len
;
3253 const char *plural
= "";
3254 if (TOUPPER (format_chars
[badwlen
]) == 'S')
3260 /* As an exception, don't warn about "decl-specifier*" since
3261 it's a C++ grammar production. */
3262 if (badwords
[i
].name
[0] == 'd'
3263 && startswith (format_chars
, "decl-specifier"))
3266 format_warning_substr (format_string_loc
, format_string_cst
,
3267 fmtchrpos
, fmtchrpos
+ badwords
[i
].len
,
3269 "misspelled term %<%.*s%> in format; "
3270 "use %<%s%s%> instead",
3271 badwlen
, format_chars
,
3272 badwords
[i
].alt
, plural
);
3274 return format_chars
+ badwords
[i
].len
- 1;
3279 if (!strncasecmp (format_chars
, "c++", 3)
3280 || !strncasecmp (format_chars
, "g++", 3))
3281 return format_chars
+ 2;
3284 return wlen
? format_chars
+ wlen
- 1 : NULL
;
3287 /* Check plain text in a format string of a GCC diagnostic function
3288 for common quoting, punctuation, and spelling mistakes, and issue
3289 -Wformat-diag warnings if they are found. FORMAT_STRING_LOC is
3290 the location of the format string, FORMAT_STRING_CST the format
3291 string itself (as a tree), ORIG_FORMAT_CHARS and FORMAT_CHARS are
3292 pointers to the beginning of the format string and the character
3293 currently being processed, and BALTOKS describes paired "tokens"
3294 within the format string that are expected to be balanced.
3295 Returns a pointer to the last processed character. */
3298 check_plain (location_t format_string_loc
, tree format_string_cst
,
3299 const char *orig_format_chars
, const char *format_chars
,
3303 const int opt
= OPT_Wformat_diag
;
3304 /* Zero-based starting position of a problem sequence. */
3305 int fmtchrpos
= format_chars
- orig_format_chars
;
3307 if (*format_chars
== '%')
3309 /* Diagnose %<%s%> and suggest using %qs instead. */
3310 if (startswith (format_chars
, "%<%s%>"))
3311 format_warning_substr (format_string_loc
, format_string_cst
,
3312 fmtchrpos
, fmtchrpos
+ 6, opt
,
3313 "quoted %qs directive in format; "
3314 "use %qs instead", "%s", "%qs");
3315 else if (format_chars
- orig_format_chars
> 2
3316 && !strncasecmp (format_chars
- 3, "can%'t", 6))
3317 format_warning_substr (format_string_loc
,
3319 fmtchrpos
- 3, fmtchrpos
+ 3, opt
,
3320 "contraction %<%.*s%> in format; "
3322 6, format_chars
- 3, "cannot");
3324 return format_chars
;
3327 if (baltoks
.quotdirs
.length ())
3329 /* Skip over all plain text within a quoting directive until
3330 the next directive. */
3331 while (*format_chars
&& '%' != *format_chars
)
3334 return format_chars
;
3337 /* The length of the problem sequence. */
3340 /* Diagnose any whitespace characters other than <space> but only
3341 leading, trailing, and two or more consecutive <space>s. Do
3342 this before diagnosing control characters because whitespace
3343 is a subset of controls. */
3344 const char *other_than_space
= NULL
;
3345 while (ISSPACE (format_chars
[nchars
]))
3347 if (format_chars
[nchars
] != ' ' && !other_than_space
)
3348 other_than_space
= format_chars
+ nchars
;
3354 /* This is the most common problem: go the extra mile to describe
3355 the problem in as much helpful detail as possible. */
3356 if (other_than_space
)
3358 format_warning_substr (format_string_loc
, format_string_cst
,
3359 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3360 "unquoted whitespace character %qc in format",
3362 return format_chars
+ nchars
- 1;
3366 /* Accept strings of leading spaces with no warning. */
3367 return format_chars
+ nchars
- 1;
3369 if (!format_chars
[nchars
])
3371 format_warning_substr (format_string_loc
, format_string_cst
,
3372 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3373 "spurious trailing space in format");
3374 return format_chars
+ nchars
- 1;
3380 && orig_format_chars
< format_chars
3381 && format_chars
[-1] == '.'
3382 && format_chars
[0] == ' '
3383 && format_chars
[1] == ' ')
3385 /* A period followed by two spaces. */
3386 if (ISUPPER (*orig_format_chars
))
3388 /* If the part before the period is a capitalized
3389 sentence check to make sure that what follows
3390 is also capitalized. */
3391 if (ISLOWER (format_chars
[2]))
3392 format_warning_substr (format_string_loc
, format_string_cst
,
3393 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3394 "inconsistent capitalization in "
3399 format_warning_substr (format_string_loc
, format_string_cst
,
3400 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3401 "unquoted sequence of %i consecutive "
3402 "space characters in format", nchars
);
3403 return format_chars
+ nchars
- 1;
3406 format_chars
+= nchars
;
3410 fmtchrpos
= format_chars
- orig_format_chars
;
3412 /* Diagnose any unquoted control characters other than the terminating
3414 while (format_chars
[nchars
] && ISCNTRL (format_chars
[nchars
]))
3419 format_warning_substr (format_string_loc
, format_string_cst
,
3420 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3421 "unquoted control characters in format");
3422 return format_chars
+ nchars
- 1;
3426 format_warning_substr (format_string_loc
, format_string_cst
,
3427 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3428 "unquoted control character %qc in format",
3430 return format_chars
+ nchars
- 1;
3433 if (ISPUNCT (format_chars
[0]))
3435 size_t nelts
= ARRAY_SIZE (c_opers
);
3436 if (const char *ret
= check_tokens (c_opers
, nelts
,
3437 format_string_loc
, format_string_cst
,
3438 orig_format_chars
, format_chars
,
3442 nelts
= c_dialect_cxx () ? ARRAY_SIZE (cxx_opers
) : 0;
3443 if (const char *ret
= check_tokens (cxx_opers
, nelts
,
3444 format_string_loc
, format_string_cst
,
3445 orig_format_chars
, format_chars
,
3450 if (ISALPHA (format_chars
[0]))
3452 size_t nelts
= ARRAY_SIZE (c_keywords
);
3453 if (const char *ret
= check_tokens (c_keywords
, nelts
,
3454 format_string_loc
, format_string_cst
,
3455 orig_format_chars
, format_chars
,
3459 nelts
= c_dialect_cxx () ? ARRAY_SIZE (cxx_keywords
) : 0;
3460 if (const char *ret
= check_tokens (cxx_keywords
, nelts
,
3461 format_string_loc
, format_string_cst
,
3462 orig_format_chars
, format_chars
,
3469 /* Diagnose unquoted options. */
3470 if ((format_chars
== orig_format_chars
3471 || format_chars
[-1] == ' ')
3472 && format_chars
[0] == '-'
3473 && ((format_chars
[1] == '-'
3474 && ISALPHA (format_chars
[2]))
3475 || ISALPHA (format_chars
[1])))
3478 while (ISALNUM (format_chars
[nchars
])
3479 || '_' == format_chars
[nchars
]
3480 || '-' == format_chars
[nchars
]
3481 || '+' == format_chars
[nchars
])
3484 format_warning_substr (format_string_loc
, format_string_cst
,
3485 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3486 "unquoted option name %<%.*s%> in format",
3487 nchars
, format_chars
);
3488 return format_chars
+ nchars
- 1;
3491 /* Diagnose leading, trailing, and two or more consecutive punctuation
3493 const char *unbalanced
= NULL
;
3494 while ('%' != format_chars
[nchars
]
3495 && ISPUNCT (format_chars
[nchars
])
3498 switch (format_chars
[nchars
])
3501 baltoks
.brackets
.safe_push (format_chars
+ nchars
);
3504 baltoks
.curly
.safe_push (format_chars
+ nchars
);
3507 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3510 baltoks
.pointy
.safe_push (format_chars
+ nchars
);
3514 if (baltoks
.brackets
.length () > 0)
3515 baltoks
.brackets
.pop ();
3517 unbalanced
= format_chars
+ nchars
;
3520 if (baltoks
.curly
.length () > 0)
3521 baltoks
.curly
.pop ();
3523 unbalanced
= format_chars
+ nchars
;
3526 if (baltoks
.parens
.length () > 0)
3527 baltoks
.parens
.pop ();
3529 unbalanced
= format_chars
+ nchars
;
3532 if (baltoks
.pointy
.length () > 0)
3533 baltoks
.pointy
.pop ();
3535 unbalanced
= format_chars
+ nchars
;
3544 format_warning_substr (format_string_loc
, format_string_cst
,
3545 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3546 "unbalanced punctuation character %qc in format",
3548 return format_chars
+ nchars
- 1;
3553 /* Consider any identifier that follows the pound ('#') sign
3554 a preprocessing directive. */
3556 && format_chars
[0] == '#'
3557 && ISALPHA (format_chars
[1]))
3559 while (ISALNUM (format_chars
[nchars
])
3560 || format_chars
[nchars
] == '_')
3563 format_warning_substr (format_string_loc
, format_string_cst
,
3564 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3565 "unquoted preprocessing directive %<%.*s%> "
3566 "in format", nchars
, format_chars
);
3567 return format_chars
+ nchars
- 1;
3570 /* Diagnose a bare single quote. */
3572 && format_chars
[0] == '\''
3573 && format_chars
- orig_format_chars
3574 && ISALPHA (format_chars
[-1])
3575 && ISALPHA (format_chars
[1]))
3577 /* Diagnose a subset of contractions that are best avoided. */
3578 for (unsigned i
= 0; i
!= ARRAY_SIZE (contrs
); ++i
)
3580 const char *apos
= strchr (contrs
[i
].name
, '\'');
3581 gcc_assert (apos
!= NULL
);
3582 int off
= apos
- contrs
[i
].name
;
3584 if (format_chars
- orig_format_chars
>= off
3585 && !strncmp (format_chars
- off
,
3586 contrs
[i
].name
, contrs
[i
].len
))
3588 format_warning_substr (format_string_loc
,
3590 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3591 "contraction %<%.*s%> in format; "
3593 contrs
[i
].len
, contrs
[i
].name
,
3595 return format_chars
+ nchars
- 1;
3599 if (format_warning_substr (format_string_loc
, format_string_cst
,
3600 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3601 "bare apostrophe %<'%> in format"))
3602 inform (format_string_loc
,
3603 "if avoiding the apostrophe is not feasible, enclose "
3604 "it in a pair of %qs and %qs directives instead",
3606 return format_chars
+ nchars
- 1;
3609 /* Diagnose a backtick (grave accent). */
3611 && format_chars
[0] == '`')
3613 if (format_warning_substr (format_string_loc
, format_string_cst
,
3614 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3615 "grave accent %<`%> in format"))
3616 inform (format_string_loc
,
3617 "use the apostrophe directive %qs instead", "%'");
3618 return format_chars
+ nchars
- 1;
3621 /* Diagnose a punctuation character after a space. */
3623 && format_chars
- orig_format_chars
3624 && format_chars
[-1] == ' '
3625 && strspn (format_chars
, "!?:;.,") == 1)
3627 format_warning_substr (format_string_loc
, format_string_cst
,
3628 fmtchrpos
- 1, fmtchrpos
, opt
,
3629 "space followed by punctuation character "
3630 "%<%c%>", format_chars
[0]);
3631 return format_chars
;
3636 if (startswith (format_chars
, "\"%s\""))
3638 if (format_warning_substr (format_string_loc
, format_string_cst
,
3639 fmtchrpos
, fmtchrpos
+ 4, opt
,
3640 "quoted %qs directive in format",
3642 inform (format_string_loc
, "if using %qs is not feasible, "
3643 "use %qs instead", "%qs", "\"%-s\"");
3646 if (format_chars
[0] == '"')
3648 baltoks
.doublequote
= baltoks
.doublequote
? NULL
: format_chars
;
3649 return format_chars
+ nchars
- 1;
3651 if (format_chars
[0] == '\'')
3653 baltoks
.singlequote
= baltoks
.singlequote
? NULL
: format_chars
;
3654 return format_chars
+ nchars
- 1;
3661 && format_chars
[0] == '(')
3662 ; /* Text beginning in an open parenthesis. */
3663 else if (nchars
== 3
3664 && startswith (format_chars
, "...")
3666 ; /* Text beginning in an ellipsis. */
3669 format_warning_substr (format_string_loc
, format_string_cst
,
3670 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3671 "spurious leading punctuation sequence "
3672 "%<%.*s%> in format",
3673 nchars
, format_chars
);
3674 return format_chars
+ nchars
- 1;
3677 else if (!format_chars
[nchars
])
3680 && (format_chars
[nchars
- 1] == ':'
3681 || format_chars
[nchars
- 1] == ')'))
3682 ; /* Text ending in a colon or a closing parenthesis. */
3683 else if (nchars
== 1
3684 && ((ISUPPER (*orig_format_chars
)
3685 && format_chars
[nchars
- 1] == '.')
3686 || strspn (format_chars
+ nchars
- 1, "?])") == 1))
3687 ; /* Capitalized sentence terminated by a single period,
3688 or text ending in a question mark, closing bracket,
3690 else if (nchars
== 2
3691 && format_chars
[0] == '?'
3692 && format_chars
[1] == ')')
3693 ; /* A question mark after a closing parenthetical note. */
3694 else if (nchars
== 2
3695 && format_chars
[0] == ')'
3696 && (format_chars
[1] == '?'
3697 || format_chars
[1] == ';'
3698 || format_chars
[1] == ':'
3699 || (ISUPPER (*orig_format_chars
)
3700 && format_chars
[1] == '.')))
3701 ; /* Closing parenthetical note followed by a question mark,
3702 semicolon, or colon at the end of the string, or by
3703 a period at the end of a capitalized sentence. */
3704 else if (nchars
== 3
3705 && format_chars
- orig_format_chars
> 0
3706 && startswith (format_chars
, "..."))
3707 ; /* Text ending in the ellipsis. */
3709 format_warning_substr (format_string_loc
, format_string_cst
,
3710 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3711 "spurious trailing punctuation sequence "
3712 "%<%.*s%> in format",
3713 nchars
, format_chars
);
3715 return format_chars
+ nchars
- 1;
3717 else if (nchars
== 2
3718 && format_chars
[0] == ')'
3719 && (format_chars
[1] == ':'
3720 || format_chars
[1] == ';'
3721 || format_chars
[1] == ',')
3722 && format_chars
[2] == ' ')
3723 ; /* Closing parenthetical note followed by a colon, semicolon
3724 or a comma followed by a space in the middle of the string. */
3725 else if (nchars
> 1)
3726 format_warning_substr (format_string_loc
, format_string_cst
,
3727 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3728 "unquoted sequence of %i consecutive "
3729 "punctuation characters %q.*s in format",
3730 nchars
, nchars
, format_chars
);
3731 return format_chars
+ nchars
- 1;
3736 /* Finally, diagnose any unquoted non-graph, non-punctuation characters
3737 other than the terminating NUL. */
3738 while (format_chars
[nchars
]
3739 && '%' != format_chars
[nchars
]
3740 && !ISPUNCT (format_chars
[nchars
])
3741 && !ISGRAPH (format_chars
[nchars
]))
3746 format_warning_substr (format_string_loc
, format_string_cst
,
3747 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3748 "unquoted non-graph characters in format");
3749 return format_chars
+ nchars
- 1;
3753 format_warning_substr (format_string_loc
, format_string_cst
,
3754 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3755 "unquoted non-graph character %qc in format",
3757 return format_chars
+ nchars
- 1;
3760 return format_chars
;
3763 /* Diagnose unbalanced tokens described by BALTOKS in format string
3764 ORIG_FORMAT_CHARS and the corresponding FORMAT_STRING_CST. */
3767 maybe_diag_unbalanced_tokens (location_t format_string_loc
,
3768 const char *orig_format_chars
,
3769 tree format_string_cst
,
3772 const char *unbalanced
= NULL
;
3774 if (baltoks
.brackets
.length ())
3775 unbalanced
= baltoks
.brackets
.pop ();
3776 else if (baltoks
.curly
.length ())
3777 unbalanced
= baltoks
.curly
.pop ();
3778 else if (baltoks
.parens
.length ())
3779 unbalanced
= baltoks
.parens
.pop ();
3780 else if (baltoks
.pointy
.length ())
3781 unbalanced
= baltoks
.pointy
.pop ();
3784 format_warning_at_char (format_string_loc
, format_string_cst
,
3785 unbalanced
- orig_format_chars
+ 1,
3787 "unbalanced punctuation character %<%c%> in format",
3790 if (baltoks
.quotdirs
.length ())
3791 format_warning_at_char (format_string_loc
, format_string_cst
,
3792 baltoks
.quotdirs
.pop () - orig_format_chars
,
3794 "unterminated quoting directive");
3797 = baltoks
.singlequote
? baltoks
.singlequote
: baltoks
.doublequote
;
3800 format_warning_at_char (format_string_loc
, format_string_cst
,
3801 quote
- orig_format_chars
+ 1,
3803 "unterminated quote character %<%c%> in format",
3807 /* Do the main part of checking a call to a format function. FORMAT_CHARS
3808 is the NUL-terminated format string (which at this point may contain
3809 internal NUL characters); FORMAT_LENGTH is its length (excluding the
3810 terminating NUL character). ARG_NUM is one less than the number of
3811 the first format argument to check; PARAMS points to that format
3812 argument in the list of arguments. */
3815 check_format_info_main (format_check_results
*res
,
3816 function_format_info
*info
, const char *format_chars
,
3817 location_t fmt_param_loc
, tree format_string_cst
,
3818 int format_length
, tree params
,
3819 unsigned HOST_WIDE_INT arg_num
,
3820 object_allocator
<format_wanted_type
> &fwt_pool
,
3821 vec
<location_t
> *arglocs
)
3823 const char * const orig_format_chars
= format_chars
;
3824 const tree first_fillin_param
= params
;
3826 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
3827 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
3828 const location_t format_string_loc
= res
->format_string_loc
;
3830 /* -1 if no conversions taking an operand have been found; 0 if one has
3831 and it didn't use $; 1 if $ formats are in use. */
3832 int has_operand_number
= -1;
3834 /* Vectors of pointers to opening quoting directives (like GCC "%<"),
3835 opening braces, brackets, and parentheses. Used to detect unbalanced
3839 /* Pointers to the most recent color directives (like GCC's "%r or %R").
3840 A starting color directive much be terminated before the end of
3841 the format string. A terminating directive makes no sense without
3842 a prior starting directive. */
3843 const char *color_begin
= NULL
;
3844 const char *color_end
= NULL
;
3846 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
3848 /* In GCC diagnostic functions check plain directives (substrings within
3849 the format string that don't start with %) for quoting and punctuations
3851 bool ck_plain
= (!info
->is_raw
3852 && (info
->format_type
== gcc_diag_format_type
3853 || info
->format_type
== gcc_tdiag_format_type
3854 || info
->format_type
== gcc_cdiag_format_type
3855 || info
->format_type
== gcc_cxxdiag_format_type
));
3857 while (*format_chars
!= 0)
3860 format_chars
= check_plain (format_string_loc
,
3862 orig_format_chars
, format_chars
,
3865 if (*format_chars
== 0 || *format_chars
++ != '%')
3868 if (*format_chars
== 0)
3870 format_warning_at_char (format_string_loc
, format_string_cst
,
3871 format_chars
- orig_format_chars
,
3873 "spurious trailing %<%%%> in format");
3876 if (*format_chars
== '%')
3882 /* ARGUMENT_PARSER ctor takes FORMAT_CHARS by reference and calls
3883 to ARG_PARSER members may modify the variable. */
3884 flag_chars_t flag_chars
;
3885 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
3886 orig_format_chars
, format_string_loc
,
3887 flag_chars
, has_operand_number
,
3888 first_fillin_param
, fwt_pool
, arglocs
);
3890 if (!arg_parser
.read_any_dollar ())
3893 if (!arg_parser
.read_format_flags ())
3896 /* Read any format width, possibly * or *m$. */
3897 if (!arg_parser
.read_any_format_width (params
, arg_num
))
3900 /* Read any format left precision (must be a number, not *). */
3901 arg_parser
.read_any_format_left_precision ();
3903 /* Read any format precision, possibly * or *m$. */
3904 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
3907 const char *format_start
= format_chars
;
3909 arg_parser
.handle_alloc_chars ();
3911 /* The rest of the conversion specification is the length modifier
3912 (if any), and the conversion specifier, so this is where the
3913 type information starts. If we need to issue a suggestion
3914 about a type mismatch, then we should preserve everything up
3916 const char *type_start
= format_chars
;
3918 /* Read any length modifier, if this kind of format has them. */
3919 const length_modifier len_modifier
3920 = arg_parser
.read_any_length_modifier ();
3922 /* Read any modifier (strftime E/O). */
3923 arg_parser
.read_any_other_modifier ();
3925 char format_char
= *format_chars
;
3926 if (format_char
== 0
3927 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
3928 && format_char
== '%'))
3930 format_warning_at_char (format_string_loc
, format_string_cst
,
3931 format_chars
- orig_format_chars
,
3933 "conversion lacks type at end of format");
3938 const format_char_info
* const fci
3939 = arg_parser
.find_format_char_info (format_char
);
3943 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
3945 format_string_loc
, orig_format_chars
, format_char
,
3946 baltoks
.quotdirs
.length () > 0);
3948 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
3949 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
3951 /* Diagnose nested or unmatched quoting directives such as GCC's
3952 "%<...%<" and "%>...%>". */
3953 bool quot_begin_p
= strchr (fci
->flags2
, '<');
3954 bool quot_end_p
= strchr (fci
->flags2
, '>');
3956 if (quot_begin_p
&& !quot_end_p
)
3958 if (baltoks
.quotdirs
.length ())
3959 format_warning_at_char (format_string_loc
, format_string_cst
,
3960 format_chars
- orig_format_chars
,
3962 "nested quoting directive");
3963 baltoks
.quotdirs
.safe_push (format_chars
);
3965 else if (!quot_begin_p
&& quot_end_p
)
3967 if (baltoks
.quotdirs
.length ())
3968 baltoks
.quotdirs
.pop ();
3970 format_warning_at_char (format_string_loc
, format_string_cst
,
3971 format_chars
- orig_format_chars
,
3973 "unmatched quoting directive");
3976 bool color_begin_p
= strchr (fci
->flags2
, '/');
3979 color_begin
= format_chars
;
3982 else if (strchr (fci
->flags2
, '\\'))
3985 format_warning_at_char (format_string_loc
, format_string_cst
,
3986 format_chars
- orig_format_chars
,
3988 "%qc directive redundant after prior "
3989 "occurence of the same", format_char
);
3990 else if (!color_begin
)
3991 format_warning_at_char (format_string_loc
, format_string_cst
,
3992 format_chars
- orig_format_chars
,
3994 "unmatched color reset directive");
3995 color_end
= format_chars
;
3998 /* Diagnose directives that shouldn't appear in a quoted sequence.
3999 (They are denoted by a double quote in FLAGS2.) */
4000 if (baltoks
.quotdirs
.length ())
4002 if (strchr (fci
->flags2
, '"'))
4003 format_warning_at_char (format_string_loc
, format_string_cst
,
4004 format_chars
- orig_format_chars
,
4006 "%qc conversion used within a quoted "
4011 /* Validate the pairs of flags used. */
4012 arg_parser
.validate_flag_pairs (fci
, format_char
);
4014 arg_parser
.give_y2k_warnings (fci
, format_char
);
4016 arg_parser
.parse_any_scan_set (fci
);
4018 tree wanted_type
= NULL
;
4019 const char *wanted_type_name
= NULL
;
4021 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
4022 wanted_type
, wanted_type_name
,
4028 arg_parser
.main_wanted_type
.next
= NULL
;
4030 /* Finally. . .check type of argument against desired type! */
4031 if (!arg_parser
.check_argument_type (fci
, len_modifier
,
4032 wanted_type
, wanted_type_name
,
4036 format_start
, type_start
,
4042 if (format_chars
- orig_format_chars
!= format_length
)
4043 format_warning_at_char (format_string_loc
, format_string_cst
,
4044 format_chars
+ 1 - orig_format_chars
,
4045 OPT_Wformat_contains_nul
,
4046 "embedded %<\\0%> in format");
4047 if (info
->first_arg_num
!= 0 && params
!= 0
4048 && has_operand_number
<= 0)
4050 res
->number_other
--;
4051 res
->number_extra_args
++;
4053 if (has_operand_number
> 0)
4054 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
4056 maybe_diag_unbalanced_tokens (format_string_loc
, orig_format_chars
,
4057 format_string_cst
, baltoks
);
4059 if (color_begin
&& !color_end
)
4060 format_warning_at_char (format_string_loc
, format_string_cst
,
4061 color_begin
- orig_format_chars
,
4062 OPT_Wformat_
, "unterminated color directive");
4065 /* Check the argument types from a single format conversion (possibly
4066 including width and precision arguments).
4068 FMT_LOC is the location of the format conversion.
4070 TYPES is a singly-linked list expressing the parts of the format
4071 conversion that expect argument types, and the arguments they
4074 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
4075 format string to where type information begins for the conversion
4076 (the length modifier and conversion specifier).
4078 CONVERSION_CHAR is the user-provided conversion specifier.
4082 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4084 then FMT_LOC covers this range:
4086 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4089 and TYPES in this case is a three-entry singly-linked list consisting of:
4090 (1) the check for the field width here:
4091 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4094 (2) the check for the field precision here:
4095 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4098 (3) the check for the length modifier and conversion char here:
4099 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4103 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
4106 0000000000111111111122
4107 0123456789012345678901
4108 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4110 | ` CONVERSION_CHAR: 'd'
4111 type starts here. */
4114 check_format_types (const substring_loc
&fmt_loc
,
4115 format_wanted_type
*types
, const format_kind_info
*fki
,
4116 int offset_to_type_start
,
4117 char conversion_char
,
4118 vec
<location_t
> *arglocs
)
4120 for (; types
!= 0; types
= types
->next
)
4130 wanted_type
= types
->wanted_type
;
4131 arg_num
= types
->arg_num
;
4133 /* The following should not occur here. */
4134 gcc_assert (wanted_type
);
4135 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
4137 if (types
->pointer_count
== 0)
4138 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
4140 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
4142 cur_param
= types
->param
;
4145 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
4146 NULL
, fki
, offset_to_type_start
,
4151 cur_type
= TREE_TYPE (cur_param
);
4152 if (cur_type
== error_mark_node
)
4154 orig_cur_type
= cur_type
;
4157 location_t param_loc
= UNKNOWN_LOCATION
;
4158 if (EXPR_HAS_LOCATION (cur_param
))
4159 param_loc
= EXPR_LOCATION (cur_param
);
4162 /* arg_num is 1-based. */
4163 gcc_assert (types
->arg_num
> 0);
4164 param_loc
= (*arglocs
)[types
->arg_num
- 1];
4167 STRIP_NOPS (cur_param
);
4169 /* Check the types of any additional pointer arguments
4170 that precede the "real" argument. */
4171 for (i
= 0; i
< types
->pointer_count
; ++i
)
4173 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
4175 cur_type
= TREE_TYPE (cur_type
);
4176 if (cur_type
== error_mark_node
)
4179 /* Check for writing through a NULL pointer. */
4180 if (types
->writing_in_flag
4183 && integer_zerop (cur_param
))
4184 warning (OPT_Wformat_
, "writing through null pointer "
4185 "(argument %d)", arg_num
);
4187 /* Check for reading through a NULL pointer. Ignore
4188 printf-family of functions as they are checked for
4189 null arguments by the middle-end. */
4190 if (fki
->conversion_specs
!= print_char_table
4191 && types
->reading_from_flag
4194 && integer_zerop (cur_param
))
4195 warning (OPT_Wformat_
, "reading through null pointer "
4196 "(argument %d)", arg_num
);
4198 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
4199 cur_param
= TREE_OPERAND (cur_param
, 0);
4203 /* See if this is an attempt to write into a const type with
4204 scanf or with printf "%n". Note: the writing in happens
4205 at the first indirection only, if for example
4206 void * const * is passed to scanf %p; passing
4207 const void ** is simply passing an incompatible type. */
4208 if (types
->writing_in_flag
4210 && (TYPE_READONLY (cur_type
)
4212 && (CONSTANT_CLASS_P (cur_param
)
4213 || (DECL_P (cur_param
)
4214 && TREE_READONLY (cur_param
))))))
4215 warning (OPT_Wformat_
, "writing into constant object "
4216 "(argument %d)", arg_num
);
4218 /* If there are extra type qualifiers beyond the first
4219 indirection, then this makes the types technically
4223 && (TYPE_READONLY (cur_type
)
4224 || TYPE_VOLATILE (cur_type
)
4225 || TYPE_ATOMIC (cur_type
)
4226 || TYPE_RESTRICT (cur_type
)))
4227 warning (OPT_Wformat_
, "extra type qualifiers in format "
4228 "argument (argument %d)",
4234 format_type_warning (fmt_loc
, param_loc
,
4235 types
, wanted_type
, orig_cur_type
, fki
,
4236 offset_to_type_start
, conversion_char
);
4241 if (i
< types
->pointer_count
)
4244 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
4246 /* Check whether the argument type is a character type. This leniency
4247 only applies to certain formats, flagged with 'c'. */
4248 if (types
->char_lenient_flag
)
4249 char_type_flag
= (cur_type
== char_type_node
4250 || cur_type
== signed_char_type_node
4251 || cur_type
== unsigned_char_type_node
);
4253 /* Check the type of the "real" argument, if there's a type we want. */
4254 if (lang_hooks
.types_compatible_p (wanted_type
, cur_type
))
4256 /* If we want 'void *', allow any pointer type.
4257 (Anything else would already have got a warning.)
4258 With -Wpedantic, only allow pointers to void and to character
4260 if (wanted_type
== void_type_node
4261 && (!pedantic
|| (i
== 1 && char_type_flag
)))
4263 /* Don't warn about differences merely in signedness, unless
4264 -Wpedantic. With -Wpedantic, warn if the type is a pointer
4265 target and not a character type, and for character types at
4266 a second level of indirection. */
4267 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
4268 && TREE_CODE (cur_type
) == INTEGER_TYPE
4269 && ((!pedantic
&& !warn_format_signedness
)
4270 || (i
== 0 && !warn_format_signedness
)
4271 || (i
== 1 && char_type_flag
))
4272 && (TYPE_UNSIGNED (wanted_type
)
4273 ? wanted_type
== c_common_unsigned_type (cur_type
)
4274 : wanted_type
== c_common_signed_type (cur_type
)))
4276 /* Don't warn about differences merely in signedness if we know
4277 that the current type is integer-promoted and its original type
4278 was unsigned such as that it is in the range of WANTED_TYPE. */
4279 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
4280 && TREE_CODE (cur_type
) == INTEGER_TYPE
4281 && warn_format_signedness
4282 && TYPE_UNSIGNED (wanted_type
)
4283 && cur_param
!= NULL_TREE
4284 && TREE_CODE (cur_param
) == NOP_EXPR
)
4286 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
4287 if (TYPE_UNSIGNED (t
)
4288 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
4291 /* Likewise, "signed char", "unsigned char" and "char" are
4292 equivalent but the above test won't consider them equivalent. */
4293 if (wanted_type
== char_type_node
4294 && (!pedantic
|| i
< 2)
4297 if (types
->scalar_identity_flag
4298 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
4299 || (INTEGRAL_TYPE_P (cur_type
)
4300 && INTEGRAL_TYPE_P (wanted_type
)))
4301 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
4303 /* Now we have a type mismatch. */
4304 format_type_warning (fmt_loc
, param_loc
, types
,
4305 wanted_type
, orig_cur_type
, fki
,
4306 offset_to_type_start
, conversion_char
);
4310 /* Given type TYPE, attempt to dereference the type N times
4311 (e.g. from ("int ***", 2) to "int *")
4313 Return the derefenced type, with any qualifiers
4314 such as "const" stripped from the result, or
4315 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
4318 deref_n_times (tree type
, int n
)
4322 for (int i
= n
; i
> 0; i
--)
4324 if (TREE_CODE (type
) != POINTER_TYPE
)
4326 type
= TREE_TYPE (type
);
4328 /* Strip off any "const" etc. */
4329 return build_qualified_type (type
, 0);
4332 /* Lookup the format code for FORMAT_LEN within FLI,
4333 returning the string code for expressing it, or NULL
4334 if it is not found. */
4337 get_modifier_for_format_len (const format_length_info
*fli
,
4338 enum format_lengths format_len
)
4340 for (; fli
->name
; fli
++)
4342 if (fli
->index
== format_len
)
4344 if (fli
->double_index
== format_len
)
4345 return fli
->double_name
;
4352 namespace selftest
{
4355 test_get_modifier_for_format_len ()
4358 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
4360 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
4362 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
4364 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
4367 } // namespace selftest
4369 #endif /* CHECKING_P */
4371 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
4372 format_type_detail using SPEC_TYPE to be offered as a suggestion for
4373 Wformat type errors where the argument has type ARG_TYPE. */
4376 matching_type_p (tree spec_type
, tree arg_type
)
4378 gcc_assert (spec_type
);
4379 gcc_assert (arg_type
);
4381 /* If any of the types requires structural equality, we can't compare
4382 their canonical types. */
4383 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
4384 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
4387 spec_type
= TYPE_CANONICAL (spec_type
);
4388 arg_type
= TYPE_CANONICAL (arg_type
);
4390 if (TREE_CODE (spec_type
) == INTEGER_TYPE
4391 && TREE_CODE (arg_type
) == INTEGER_TYPE
4392 && (TYPE_UNSIGNED (spec_type
)
4393 ? spec_type
== c_common_unsigned_type (arg_type
)
4394 : spec_type
== c_common_signed_type (arg_type
)))
4397 return spec_type
== arg_type
;
4400 /* Subroutine of get_format_for_type.
4402 Generate a string containing the length modifier and conversion specifier
4403 that should be used to format arguments of type ARG_TYPE within FKI
4404 (effectively the inverse of the checking code).
4406 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
4407 is required to use it, for correcting bogus length modifiers.
4408 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
4409 that matches ARG_TYPE.
4411 If successful, returns a non-NULL string which should be freed
4413 Otherwise, returns NULL. */
4416 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
4417 char conversion_char
)
4419 gcc_assert (arg_type
);
4421 const format_char_info
*spec
;
4422 for (spec
= &fki
->conversion_specs
[0];
4426 if (conversion_char
)
4427 if (!strchr (spec
->format_chars
, conversion_char
))
4430 tree effective_arg_type
= deref_n_times (arg_type
,
4431 spec
->pointer_count
);
4432 if (!effective_arg_type
)
4434 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
4436 const format_type_detail
*ftd
= &spec
->types
[i
];
4437 if (!ftd
->type
|| *ftd
->type
== NULL_TREE
)
4439 if (matching_type_p (*ftd
->type
, effective_arg_type
))
4441 const char *len_modifier
4442 = get_modifier_for_format_len (fki
->length_char_specs
,
4443 (enum format_lengths
)i
);
4447 if (conversion_char
)
4448 /* We found a match, using the given conversion char - the
4449 length modifier was incorrect (or absent).
4450 Provide a suggestion using the conversion char with the
4451 correct length modifier for the type. */
4452 return xasprintf ("%s%c", len_modifier
, conversion_char
);
4454 /* 2nd pass: no match was possible using the user-provided
4455 conversion char, but we do have a match without using it.
4456 Provide a suggestion using the first conversion char
4457 listed for the given type. */
4458 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
4466 /* Generate a string containing the length modifier and conversion specifier
4467 that should be used to format arguments of type ARG_TYPE within FKI
4468 (effectively the inverse of the checking code).
4470 If successful, returns a non-NULL string which should be freed
4472 Otherwise, returns NULL. */
4475 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
4476 char conversion_char
)
4478 gcc_assert (arg_type
);
4479 gcc_assert (conversion_char
);
4481 /* First pass: look for a format_char_info containing CONVERSION_CHAR
4482 If we find one, then presumably the length modifier was incorrect
4484 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
4488 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
4489 matching just on the type. */
4490 return get_format_for_type_1 (fki
, arg_type
, '\0');
4493 /* Attempt to get a string for use as a replacement fix-it hint for the
4494 source range in FMT_LOC.
4496 Preserve all of the text within the range of FMT_LOC up to
4497 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
4498 length modifier and conversion specifier for ARG_TYPE, attempting
4499 to keep the user-provided CONVERSION_CHAR if possible.
4501 For example, given a long vs long long mismatch for arg5 here:
4503 000000000111111111122222222223333333333|
4504 123456789012345678901234567890123456789` column numbers
4505 0000000000111111111122|
4506 0123456789012345678901` string offsets
4507 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
4508 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4510 | ` CONVERSION_CHAR: 'd'
4513 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
4514 STRING_CST), where the user provided:
4516 the result (assuming "long" argument 5) should be:
4519 If successful, returns a non-NULL string which should be freed
4521 Otherwise, returns NULL. */
4524 get_corrected_substring (const substring_loc
&fmt_loc
,
4525 format_wanted_type
*type
, tree arg_type
,
4526 const format_kind_info
*fki
,
4527 int offset_to_type_start
, char conversion_char
)
4529 /* Attempt to provide hints for argument types, but not for field widths
4533 if (type
->kind
!= CF_KIND_FORMAT
)
4536 /* Locate the current code within the source range, rejecting
4537 any awkward cases where the format string occupies more than
4539 Lookup the place where the type starts (including any length
4540 modifiers), getting it as the caret location. */
4541 substring_loc
type_loc (fmt_loc
);
4542 type_loc
.set_caret_index (offset_to_type_start
);
4544 location_t fmt_substring_loc
;
4545 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
4549 source_range fmt_substring_range
4550 = get_range_from_loc (line_table
, fmt_substring_loc
);
4552 expanded_location caret
4553 = expand_location_to_spelling_point (fmt_substring_loc
);
4554 expanded_location start
4555 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
4556 expanded_location finish
4557 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
4558 if (caret
.file
!= start
.file
)
4560 if (start
.file
!= finish
.file
)
4562 if (caret
.line
!= start
.line
)
4564 if (start
.line
!= finish
.line
)
4566 if (start
.column
> caret
.column
)
4568 if (start
.column
> finish
.column
)
4570 if (caret
.column
> finish
.column
)
4573 char_span line
= location_get_source_line (start
.file
, start
.line
);
4577 /* If we got this far, then we have the line containing the
4578 existing conversion specification.
4580 Generate a trimmed copy, containing the prefix part of the conversion
4581 specification, up to the (but not including) the length modifier.
4582 In the above example, this would be "%-+*.*". */
4583 int length_up_to_type
= caret
.column
- start
.column
;
4584 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
4585 char *prefix
= prefix_span
.xstrdup ();
4587 /* Now attempt to generate a suggestion for the rest of the specification
4588 (length modifier and conversion char), based on ARG_TYPE and
4590 In the above example, this would be "ld". */
4591 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
4592 if (!format_for_type
)
4598 /* Success. Generate the resulting suggestion for the whole range of
4599 FMT_LOC by concatenating the two strings.
4600 In the above example, this would be "%-+*.*ld". */
4601 char *result
= concat (prefix
, format_for_type
, NULL
);
4602 free (format_for_type
);
4607 /* Helper class for adding zero or more trailing '*' to types.
4609 The format type and name exclude any '*' for pointers, so those
4610 must be formatted manually. For all the types we currently have,
4611 this is adequate, but formats taking pointers to functions or
4612 arrays would require the full type to be built up in order to
4613 print it with %T. */
4615 class indirection_suffix
4618 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
4620 /* Determine the size of the buffer (including NUL-terminator). */
4622 size_t get_buffer_size () const
4624 return m_pointer_count
+ 2;
4627 /* Write the '*' to DST and add a NUL-terminator. */
4629 void fill_buffer (char *dst
) const
4631 if (m_pointer_count
== 0)
4633 else if (c_dialect_cxx ())
4635 memset (dst
, '*', m_pointer_count
);
4636 dst
[m_pointer_count
] = 0;
4641 memset (dst
+ 1, '*', m_pointer_count
);
4642 dst
[m_pointer_count
+ 1] = 0;
4647 int m_pointer_count
;
4650 /* Subclass of range_label for labelling the range in the format string
4651 with the type in question, adding trailing '*' for pointer_count. */
4653 class range_label_for_format_type_mismatch
4654 : public range_label_for_type_mismatch
4657 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
4659 : range_label_for_type_mismatch (labelled_type
, other_type
),
4660 m_pointer_count (pointer_count
)
4664 label_text
get_text (unsigned range_idx
) const final override
4666 label_text text
= range_label_for_type_mismatch::get_text (range_idx
);
4667 if (text
.get () == NULL
)
4670 indirection_suffix
suffix (m_pointer_count
);
4671 char *p
= (char *) alloca (suffix
.get_buffer_size ());
4672 suffix
.fill_buffer (p
);
4674 char *result
= concat (text
.get (), p
, NULL
);
4675 return label_text::take (result
);
4679 int m_pointer_count
;
4682 /* Give a warning about a format argument of different type from that expected.
4683 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
4684 is based on the location of the char at TYPE->offset_loc.
4685 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
4686 if this is unavailable.
4687 WANTED_TYPE is the type the argument should have,
4688 possibly stripped of pointer dereferences. The description (such as "field
4689 precision"), the placement in the format string, a possibly more
4690 friendly name of WANTED_TYPE, and the number of pointer dereferences
4691 are taken from TYPE. ARG_TYPE is the type of the actual argument,
4692 or NULL if it is missing.
4694 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
4695 format string to where type information begins for the conversion
4696 (the length modifier and conversion specifier).
4697 CONVERSION_CHAR is the user-provided conversion specifier.
4699 For example, given a type mismatch for argument 5 here:
4701 00000000011111111112222222222333333333344444444445555555555|
4702 12345678901234567890123456789012345678901234567890123456789` column numbers
4703 0000000000111111111122|
4704 0123456789012345678901` offsets within STRING_CST
4705 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
4706 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
4708 | ` CONVERSION_CHAR: 'd' PARAM_LOC
4711 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
4715 format_type_warning (const substring_loc
&whole_fmt_loc
,
4716 location_t param_loc
,
4717 format_wanted_type
*type
,
4718 tree wanted_type
, tree arg_type
,
4719 const format_kind_info
*fki
,
4720 int offset_to_type_start
,
4721 char conversion_char
)
4723 enum format_specifier_kind kind
= type
->kind
;
4724 const char *wanted_type_name
= type
->wanted_type_name
;
4725 const char *format_start
= type
->format_start
;
4726 int format_length
= type
->format_length
;
4727 int pointer_count
= type
->pointer_count
;
4728 int arg_num
= type
->arg_num
;
4730 /* If ARG_TYPE is a typedef with a misleading name (for example,
4731 size_t but not the standard size_t expected by printf %zu), avoid
4732 printing the typedef name. */
4733 if (wanted_type_name
4735 && TYPE_NAME (arg_type
)
4736 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
4737 && DECL_NAME (TYPE_NAME (arg_type
))
4738 && !strcmp (wanted_type_name
,
4739 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
4740 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
4742 indirection_suffix
suffix (pointer_count
);
4743 char *p
= (char *) alloca (suffix
.get_buffer_size ());
4744 suffix
.fill_buffer (p
);
4746 /* WHOLE_FMT_LOC has the caret at the end of the range.
4747 Set the caret to be at the offset from TYPE. Subtract one
4748 from the offset for the same reason as in format_warning_at_char. */
4749 substring_loc
fmt_loc (whole_fmt_loc
);
4750 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
4752 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
4754 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
4756 /* Get a string for use as a replacement fix-it hint for the range in
4757 fmt_loc, or NULL. */
4758 char *corrected_substring
4759 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
4760 offset_to_type_start
, conversion_char
);
4761 format_string_diagnostic_t
diag (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
4762 corrected_substring
);
4763 if (wanted_type_name
)
4768 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
4769 "but argument %d has type %qT",
4770 gettext (kind_descriptions
[kind
]),
4771 (kind
== CF_KIND_FORMAT
? "%" : ""),
4772 format_length
, format_start
,
4773 wanted_type_name
, p
, arg_num
, arg_type
);
4777 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
4778 gettext (kind_descriptions
[kind
]),
4779 (kind
== CF_KIND_FORMAT
? "%" : ""),
4780 format_length
, format_start
, wanted_type_name
, p
);
4787 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
4788 "but argument %d has type %qT",
4789 gettext (kind_descriptions
[kind
]),
4790 (kind
== CF_KIND_FORMAT
? "%" : ""),
4791 format_length
, format_start
,
4792 wanted_type
, p
, arg_num
, arg_type
);
4796 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
4797 gettext (kind_descriptions
[kind
]),
4798 (kind
== CF_KIND_FORMAT
? "%" : ""),
4799 format_length
, format_start
, wanted_type
, p
);
4802 free (corrected_substring
);
4806 /* Given a format_char_info array FCI, and a character C, this function
4807 returns the index into the conversion_specs where that specifier's
4808 data is located. The character must exist. */
4810 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
4814 for (i
= 0; fci
->format_chars
; i
++, fci
++)
4815 if (strchr (fci
->format_chars
, c
))
4818 /* We shouldn't be looking for a non-existent specifier. */
4822 /* Given a format_length_info array FLI, and a character C, this
4823 function returns the index into the conversion_specs where that
4824 modifier's data is located. The character must exist. */
4826 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
4830 for (i
= 0; fli
->name
; i
++, fli
++)
4831 if (strchr (fli
->name
, c
))
4834 /* We shouldn't be looking for a non-existent modifier. */
4838 /* Determine the type of HOST_WIDE_INT in the code being compiled for
4839 use in GCC's __asm_fprintf__ custom format attribute. You must
4840 have set dynamic_format_types before calling this function. */
4842 init_dynamic_asm_fprintf_info (void)
4848 format_length_info
*new_asm_fprintf_length_specs
;
4851 /* Find the underlying type for HOST_WIDE_INT. For the %w
4852 length modifier to work, one must have issued: "typedef
4853 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
4854 prior to using that modifier. */
4855 hwi
= maybe_get_identifier ("__gcc_host_wide_int__");
4858 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4861 hwi
= identifier_global_value (hwi
);
4862 if (!hwi
|| TREE_CODE (hwi
) != TYPE_DECL
)
4864 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4867 hwi
= DECL_ORIGINAL_TYPE (hwi
);
4869 if (hwi
!= long_integer_type_node
&& hwi
!= long_long_integer_type_node
)
4871 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
4872 " or %<long long%>");
4876 /* Create a new (writable) copy of asm_fprintf_length_specs. */
4877 new_asm_fprintf_length_specs
= (format_length_info
*)
4878 xmemdup (asm_fprintf_length_specs
,
4879 sizeof (asm_fprintf_length_specs
),
4880 sizeof (asm_fprintf_length_specs
));
4882 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4883 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
4884 if (hwi
== long_integer_type_node
)
4885 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
4886 else if (hwi
== long_long_integer_type_node
)
4887 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
4891 /* Assign the new data for use. */
4892 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
4893 new_asm_fprintf_length_specs
;
4897 static const format_length_info
*
4898 get_init_dynamic_hwi (void)
4901 static format_length_info
*diag_ls
;
4907 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
4908 length modifier to work, one must have issued: "typedef
4909 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
4910 prior to using that modifier. */
4911 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
4913 hwi
= identifier_global_value (hwi
);
4916 if (TREE_CODE (hwi
) != TYPE_DECL
)
4918 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4923 hwi
= DECL_ORIGINAL_TYPE (hwi
);
4925 if (hwi
!= long_integer_type_node
4926 && hwi
!= long_long_integer_type_node
)
4928 error ("%<__gcc_host_wide_int__%> is not defined"
4929 " as %<long%> or %<long long%>");
4936 diag_ls
= (format_length_info
*)
4937 xmemdup (gcc_diag_length_specs
,
4938 sizeof (gcc_diag_length_specs
),
4939 sizeof (gcc_diag_length_specs
));
4942 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4943 i
= find_length_info_modifier_index (diag_ls
, 'w');
4944 if (hwi
== long_integer_type_node
)
4945 diag_ls
[i
].index
= FMT_LEN_l
;
4946 else if (hwi
== long_long_integer_type_node
)
4947 diag_ls
[i
].index
= FMT_LEN_ll
;
4955 /* Determine the type of a "locus" in the code being compiled for use
4956 in GCC's __gcc_gfc__ custom format attribute. You must have set
4957 dynamic_format_types before calling this function. */
4959 init_dynamic_gfc_info (void)
4961 dynamic_format_types
[gcc_gfc_format_type
].length_char_specs
4962 = get_init_dynamic_hwi ();
4966 static format_char_info
*gfc_fci
;
4968 /* For the GCC __gcc_gfc__ custom format specifier to work, one
4969 must have declared 'locus' prior to using this attribute. If
4970 we haven't seen this declarations then you shouldn't use the
4971 specifier requiring that type. */
4972 if ((locus
= maybe_get_identifier ("locus")))
4974 locus
= identifier_global_value (locus
);
4977 if (TREE_CODE (locus
) != TYPE_DECL
4978 || TREE_TYPE (locus
) == error_mark_node
)
4980 error ("%<locus%> is not defined as a type");
4984 locus
= TREE_TYPE (locus
);
4988 /* Assign the new data for use. */
4990 /* Handle the __gcc_gfc__ format specifics. */
4992 dynamic_format_types
[gcc_gfc_format_type
].conversion_specs
=
4993 gfc_fci
= (format_char_info
*)
4994 xmemdup (gcc_gfc_char_table
,
4995 sizeof (gcc_gfc_char_table
),
4996 sizeof (gcc_gfc_char_table
));
4999 const unsigned i
= find_char_info_specifier_index (gfc_fci
, 'L');
5000 gfc_fci
[i
].types
[0].type
= &locus
;
5001 gfc_fci
[i
].pointer_count
= 1;
5006 /* Lookup the type named NAME and return a NAME type if found.
5007 Otherwise, return void_type_node if NAME has not been used yet,
5008 or NULL_TREE if NAME is not a type (issuing an error). */
5011 get_named_type (const char *name
)
5013 if (tree result
= maybe_get_identifier (name
))
5015 result
= identifier_global_tag (result
);
5018 if (TYPE_P (result
))
5020 else if (TREE_CODE (result
) == TYPE_DECL
)
5021 result
= TREE_TYPE (result
);
5024 error ("%qs is not defined as a type", name
);
5031 return void_type_node
;
5034 /* Determine the types of "tree" and "location_t" in the code being
5035 compiled for use in GCC's diagnostic custom format attributes. You
5036 must have set dynamic_format_types before calling this function. */
5038 init_dynamic_diag_info (void)
5040 /* For the GCC-diagnostics custom format specifiers to work, one
5041 must have declared 'tree' and 'location_t' prior to using those
5042 attributes. If we haven't seen these declarations then
5043 the specifiers requiring these types shouldn't be used.
5044 However we don't force a hard ICE because we may see only one
5045 or the other type. */
5046 if (tree loc
= maybe_get_identifier ("location_t"))
5048 loc
= identifier_global_value (loc
);
5049 if (loc
&& TREE_CODE (loc
) != TYPE_DECL
)
5050 error ("%<location_t%> is not defined as a type");
5053 /* Initialize the global tree node type local to this file. */
5054 if (!local_tree_type_node
5055 || local_tree_type_node
== void_type_node
)
5057 /* We need to grab the underlying 'union tree_node' so peek into
5058 an extra type level. */
5059 if ((local_tree_type_node
= maybe_get_identifier ("tree")))
5061 local_tree_type_node
5062 = identifier_global_value (local_tree_type_node
);
5063 if (local_tree_type_node
)
5065 if (TREE_CODE (local_tree_type_node
) != TYPE_DECL
)
5067 error ("%<tree%> is not defined as a type");
5068 local_tree_type_node
= NULL_TREE
;
5070 else if (TREE_CODE (TREE_TYPE (local_tree_type_node
))
5073 error ("%<tree%> is not defined as a pointer type");
5074 local_tree_type_node
= NULL_TREE
;
5077 local_tree_type_node
5078 = TREE_TYPE (TREE_TYPE (local_tree_type_node
));
5082 local_tree_type_node
= void_type_node
;
5085 /* Similar to the above but for gimple*. */
5086 if (!local_gimple_ptr_node
5087 || local_gimple_ptr_node
== void_type_node
)
5088 local_gimple_ptr_node
= get_named_type ("gimple");
5090 /* Similar to the above but for cgraph_node*. */
5091 if (!local_cgraph_node_ptr_node
5092 || local_cgraph_node_ptr_node
== void_type_node
)
5093 local_cgraph_node_ptr_node
= get_named_type ("cgraph_node");
5095 /* Similar to the above but for diagnostic_event_id_t*. */
5096 if (!local_event_ptr_node
5097 || local_event_ptr_node
== void_type_node
)
5098 local_event_ptr_node
= get_named_type ("diagnostic_event_id_t");
5100 /* All the GCC diag formats use the same length specs. */
5101 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
5102 dynamic_format_types
[gcc_tdiag_format_type
].length_char_specs
=
5103 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
5104 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
5105 dynamic_format_types
[gcc_dump_printf_format_type
].length_char_specs
5106 = get_init_dynamic_hwi ();
5108 /* It's safe to "re-initialize these to the same values. */
5109 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
5110 gcc_diag_char_table
;
5111 dynamic_format_types
[gcc_tdiag_format_type
].conversion_specs
=
5112 gcc_tdiag_char_table
;
5113 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
5114 gcc_cdiag_char_table
;
5115 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
5116 gcc_cxxdiag_char_table
;
5117 dynamic_format_types
[gcc_dump_printf_format_type
].conversion_specs
=
5118 gcc_dump_printf_char_table
;
5121 #ifdef TARGET_FORMAT_TYPES
5122 extern const format_kind_info TARGET_FORMAT_TYPES
[];
5125 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5126 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
5128 #ifdef TARGET_OVERRIDES_FORMAT_INIT
5129 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
5132 /* Attributes such as "printf" are equivalent to those such as
5133 "gnu_printf" unless this is overridden by a target. */
5134 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
5136 { "gnu_printf", "printf" },
5137 { "gnu_scanf", "scanf" },
5138 { "gnu_strftime", "strftime" },
5139 { "gnu_strfmon", "strfmon" },
5143 /* Translate to unified attribute name. This is used in decode_format_type and
5144 decode_format_attr. In attr_name the user specified argument is passed. It
5145 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5146 or the attr_name passed to this function, if there is no matching entry. */
5148 convert_format_name_to_system_name (const char *attr_name
)
5152 if (attr_name
== NULL
|| *attr_name
== 0
5153 || startswith (attr_name
, "gcc_"))
5155 #ifdef TARGET_OVERRIDES_FORMAT_INIT
5156 TARGET_OVERRIDES_FORMAT_INIT ();
5159 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5160 /* Check if format attribute is overridden by target. */
5161 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
5163 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
5165 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
5168 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
5170 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
5174 /* Otherwise default to gnu format. */
5176 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
5179 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
5182 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
5184 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
5190 /* Handle a "format" attribute; arguments as in
5191 struct attribute_spec.handler. */
5193 handle_format_attribute (tree node
[3], tree atname
, tree args
,
5194 int flags
, bool *no_add_attrs
)
5196 const_tree type
= *node
;
5197 /* NODE[2] may be NULL, and it also may be a PARM_DECL for function
5199 const_tree fndecl
= ((node
[2] && TREE_CODE (node
[2]) == FUNCTION_DECL
)
5200 ? node
[2] : NULL_TREE
);
5201 function_format_info info
;
5203 #ifdef TARGET_FORMAT_TYPES
5204 /* If the target provides additional format types, we need to
5205 add them to FORMAT_TYPES at first use. */
5206 if (!dynamic_format_types
)
5208 dynamic_format_types
= XNEWVEC (format_kind_info
,
5209 n_format_types
+ TARGET_N_FORMAT_TYPES
);
5210 memcpy (dynamic_format_types
, format_types_orig
,
5211 sizeof (format_types_orig
));
5212 memcpy (&dynamic_format_types
[n_format_types
], TARGET_FORMAT_TYPES
,
5213 TARGET_N_FORMAT_TYPES
* sizeof (dynamic_format_types
[0]));
5215 format_types
= dynamic_format_types
;
5216 /* Provide a reference for the first potential external type. */
5217 first_target_format_type
= n_format_types
;
5218 n_format_types
+= TARGET_N_FORMAT_TYPES
;
5222 /* Canonicalize name of format function. */
5223 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
5224 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
5226 /* record the flags for check_function_format */
5227 TREE_PURPOSE (args
) = build_int_cst (unsigned_type_node
, flags
);
5229 if (!decode_format_attr (fndecl
? fndecl
: type
, atname
, args
, &info
,
5230 /* validated_p = */false))
5232 *no_add_attrs
= true;
5236 if (prototype_p (type
))
5238 if (!check_format_string (type
, info
.format_num
, flags
,
5239 no_add_attrs
, info
.format_type
))
5242 if (info
.first_arg_num
!= 0)
5244 unsigned HOST_WIDE_INT arg_num
= 1;
5245 function_args_iterator iter
;
5248 /* Verify that first_arg_num points to the last arg,
5250 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
5253 if (arg_num
!= info
.first_arg_num
)
5255 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
5256 error ("argument to be formatted is not %<...%>");
5257 *no_add_attrs
= true;
5263 /* Check if this is a strftime variant. Just for this variant
5264 FMT_FLAG_ARG_CONVERT is not set. */
5265 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
5266 && info
.first_arg_num
!= 0)
5268 error ("strftime formats cannot format arguments");
5269 *no_add_attrs
= true;
5273 /* If this is a custom GCC-internal format type, we have to
5274 initialize certain bits at runtime. */
5275 if (info
.format_type
== asm_fprintf_format_type
5276 || info
.format_type
== gcc_gfc_format_type
5277 || info
.format_type
== gcc_diag_format_type
5278 || info
.format_type
== gcc_tdiag_format_type
5279 || info
.format_type
== gcc_cdiag_format_type
5280 || info
.format_type
== gcc_cxxdiag_format_type
5281 || info
.format_type
== gcc_dump_printf_format_type
)
5283 /* Our first time through, we have to make sure that our
5284 format_type data is allocated dynamically and is modifiable. */
5285 if (!dynamic_format_types
)
5286 format_types
= dynamic_format_types
= (format_kind_info
*)
5287 xmemdup (format_types_orig
, sizeof (format_types_orig
),
5288 sizeof (format_types_orig
));
5290 /* If this is format __asm_fprintf__, we have to initialize
5291 GCC's notion of HOST_WIDE_INT for checking %wd. */
5292 if (info
.format_type
== asm_fprintf_format_type
)
5293 init_dynamic_asm_fprintf_info ();
5294 /* If this is format __gcc_gfc__, we have to initialize GCC's
5295 notion of 'locus' at runtime for %L. */
5296 else if (info
.format_type
== gcc_gfc_format_type
)
5297 init_dynamic_gfc_info ();
5298 /* If this is one of the diagnostic attributes, then we have to
5299 initialize 'location_t' and 'tree' at runtime. */
5300 else if (info
.format_type
== gcc_diag_format_type
5301 || info
.format_type
== gcc_tdiag_format_type
5302 || info
.format_type
== gcc_cdiag_format_type
5303 || info
.format_type
== gcc_cxxdiag_format_type
5304 || info
.format_type
== gcc_dump_printf_format_type
)
5305 init_dynamic_diag_info ();
5315 namespace selftest
{
5317 /* Selftests of location handling. */
5319 /* Get the format_kind_info with the given name. */
5321 static const format_kind_info
*
5322 get_info (const char *name
)
5324 int idx
= decode_format_type (name
);
5325 const format_kind_info
*fki
= &format_types
[idx
];
5326 ASSERT_STREQ (fki
->name
, name
);
5330 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
5331 is EXPECTED_FORMAT. */
5334 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
5335 const char *expected_format
, tree type
,
5336 char conversion_char
)
5339 gcc_assert (expected_format
);
5342 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
5343 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
5344 free (actual_format
);
5347 /* Selftests for get_format_for_type. */
5349 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
5350 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
5351 (TYPE), (CONVERSION_CHAR))
5353 /* Selftest for get_format_for_type for "printf"-style functions. */
5356 test_get_format_for_type_printf ()
5358 const format_kind_info
*fki
= get_info ("gnu_printf");
5359 ASSERT_NE (fki
, NULL
);
5361 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
5362 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
5363 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
5364 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
5365 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
5366 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
5367 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
5368 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
5369 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
5370 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
5371 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
5372 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
5373 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
5374 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
5375 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
5376 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
5377 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
5378 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
5379 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
5380 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
5381 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
5382 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
5383 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
5384 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
5385 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
5386 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
5387 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
5388 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
5391 /* Selftest for get_format_for_type for "scanf"-style functions. */
5394 test_get_format_for_type_scanf ()
5396 const format_kind_info
*fki
= get_info ("gnu_scanf");
5397 ASSERT_NE (fki
, NULL
);
5398 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
5399 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
5400 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
5401 build_pointer_type (long_integer_type_node
), 'd');
5402 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
5403 build_pointer_type (long_unsigned_type_node
), 'u');
5404 ASSERT_FORMAT_FOR_TYPE_STREQ
5405 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
5406 ASSERT_FORMAT_FOR_TYPE_STREQ
5407 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
5408 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
5409 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
5412 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
5414 /* Exercise the type-printing label code, to give some coverage
5415 under "make selftest-valgrind" (in particular, to ensure that
5416 the label-printing machinery doesn't leak). */
5419 test_type_mismatch_range_labels ()
5421 /* Create a tempfile and write some text to it.
5422 ....................0000000001 11111111 12 22222222
5423 ....................1234567890 12345678 90 12345678. */
5424 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
5425 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
5426 line_table_test ltt
;
5428 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
5430 location_t c17
= linemap_position_for_column (line_table
, 17);
5431 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
5432 location_t c18
= linemap_position_for_column (line_table
, 18);
5433 location_t c24
= linemap_position_for_column (line_table
, 24);
5434 location_t c26
= linemap_position_for_column (line_table
, 26);
5436 /* Don't attempt to run the tests if column data might be unavailable. */
5437 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
5440 location_t fmt
= make_location (c18
, c17
, c18
);
5441 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
5443 location_t param
= make_location (c24
, c24
, c26
);
5444 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
5446 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
5447 integer_type_node
, 1);
5448 range_label_for_type_mismatch
param_label (integer_type_node
,
5450 gcc_rich_location
richloc (fmt
, &fmt_label
);
5451 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
5453 test_diagnostic_context dc
;
5454 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
5455 if (c_dialect_cxx ())
5456 /* "char*", without a space. */
5457 ASSERT_STREQ (" printf (\"msg: %i\\n\", msg);\n"
5461 pp_formatted_text (dc
.printer
));
5463 /* "char *", with a space. */
5464 ASSERT_STREQ (" printf (\"msg: %i\\n\", msg);\n"
5469 pp_formatted_text (dc
.printer
));
5472 /* Run all of the selftests within this file. */
5475 c_format_cc_tests ()
5477 test_get_modifier_for_format_len ();
5478 test_get_format_for_type_printf ();
5479 test_get_format_for_type_scanf ();
5480 test_type_mismatch_range_labels ();
5483 } // namespace selftest
5485 #endif /* CHECKING_P */
5487 #include "gt-c-family-c-format.h"