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 { "b", 0, STD_C2X
, { T2X_UI
, T2X_UC
, T2X_US
, T2X_UL
, T2X_ULL
, TEX_ULL
, T2X_ST
, T2X_UPD
, T2X_UIM
, BADLEN
, BADLEN
, BADLEN
, 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 { "B", 0, STD_EXT
, { T2X_UI
, T2X_UC
, T2X_US
, T2X_UL
, T2X_ULL
, TEX_ULL
, T2X_ST
, T2X_UPD
, T2X_UIM
, BADLEN
, BADLEN
, BADLEN
, T2X_U8
, T2X_U16
, T2X_U32
, T2X_U64
, T2X_UF8
, T2X_UF16
, T2X_UF32
, T2X_UF64
}, "-wp0#", "i", NULL
},
732 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
735 static const format_char_info asm_fprintf_char_table
[] =
737 /* C89 conversion specifiers. */
738 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0 +", "i", NULL
},
739 { "oxX", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0#", "i", NULL
},
740 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp0", "i", NULL
},
741 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-w", "", NULL
},
742 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "-wp", "cR", NULL
},
744 /* asm_fprintf conversion specifiers. */
745 { "O", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
746 { "R", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
747 { "I", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
748 { "L", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
749 { "U", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
750 { "r", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "", NULL
},
751 { "z", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
752 { "@", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
753 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
756 /* GCC-specific format_char_info arrays. */
758 /* The conversion specifiers implemented within pp_format, and thus supported
759 by all pretty_printer instances within GCC. */
761 #define PP_FORMAT_CHAR_TABLE \
762 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
763 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
764 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
765 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL }, \
766 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL }, \
767 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL }, \
768 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "//cR", NULL }, \
769 { "@", 1, STD_C89, { T_EVENT_PTR, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL }, \
770 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL }, \
771 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL }, \
772 { "'" , 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
773 { "{", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL }, \
774 { "}", 0, STD_C89, NOARGUMENTS, "", "", NULL }, \
775 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL }, \
776 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL }, \
777 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_diag_char_table[0] }
779 static const format_char_info gcc_diag_char_table
[] =
781 /* The conversion specifiers implemented within pp_format. */
782 PP_FORMAT_CHAR_TABLE
,
784 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
787 static const format_char_info gcc_tdiag_char_table
[] =
789 /* The conversion specifiers implemented within pp_format. */
790 PP_FORMAT_CHAR_TABLE
,
792 /* Custom conversion specifiers implemented by default_tree_printer. */
794 /* These will require a "tree" at runtime. */
795 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
796 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
798 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
801 static const format_char_info gcc_cdiag_char_table
[] =
803 /* The conversion specifiers implemented within pp_format. */
804 PP_FORMAT_CHAR_TABLE
,
806 /* Custom conversion specifiers implemented by c_tree_printer. */
808 /* These will require a "tree" at runtime. */
809 { "DFTV", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "'", NULL
},
810 { "E", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+", "", NULL
},
812 { "v", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q#", "", NULL
},
814 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
817 static const format_char_info gcc_cxxdiag_char_table
[] =
819 /* The conversion specifiers implemented within pp_format. */
820 PP_FORMAT_CHAR_TABLE
,
822 /* Custom conversion specifiers implemented by cp_printer. */
824 /* These will require a "tree" at runtime. */
825 { "ADFHISTVX",1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "'", NULL
},
826 { "E", 1,STD_C89
,{ T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q+#", "", NULL
},
828 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
829 { "CLOPQ",0,STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
831 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
834 static const format_char_info gcc_gfc_char_table
[] =
836 /* C89 conversion specifiers. */
837 { "di", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, T89_L
, T9L_LL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
838 { "u", 0, STD_C89
, { T89_UI
, BADLEN
, BADLEN
, T89_UL
, T9L_ULL
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
839 { "c", 0, STD_C89
, { T89_I
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "", NULL
},
840 { "s", 1, STD_C89
, { T89_C
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "q", "cR", NULL
},
842 /* gfc conversion specifiers. */
844 { "C", 0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
846 /* This will require a "locus" at runtime. */
847 { "L", 0, STD_C89
, { T89_V
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "R", NULL
},
849 /* These will require nothing. */
850 { "<>",0, STD_C89
, NOARGUMENTS
, "", "", NULL
},
851 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
854 static const format_char_info gcc_dump_printf_char_table
[] =
856 /* The conversion specifiers implemented within pp_format. */
857 PP_FORMAT_CHAR_TABLE
,
859 /* Custom conversion specifiers implemented by dump_pretty_printer. */
861 /* E and G require a "gimple *" argument at runtime. */
862 { "EG", 1, STD_C89
, { T89_G
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
864 /* C requires a "cgraph_node *" argument at runtime. */
865 { "C", 1, STD_C89
, { T_CGRAPH_NODE
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
867 /* T requires a "tree" at runtime. */
868 { "T", 1, STD_C89
, { T89_T
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
870 /* %f requires a "double"; it doesn't support modifiers. */
871 { "f", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "", "\"", NULL
},
873 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
876 static const format_char_info scan_char_table
[] =
878 /* C89 conversion specifiers. */
879 { "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
},
880 { "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
},
881 { "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
},
882 { "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
},
883 { "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
},
884 { "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
},
885 { "[", 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
},
886 { "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
},
887 { "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
},
888 /* C99 conversion specifiers. */
889 { "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
},
890 { "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
},
891 /* C2X conversion specifiers. */
892 { "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
},
893 /* X/Open conversion specifiers. */
894 { "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
},
895 { "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
},
896 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
899 static const format_char_info time_char_table
[] =
901 /* C89 conversion specifiers. */
902 { "AZa", 0, STD_C89
, NOLENGTHS
, "^#", "", NULL
},
903 { "Bb", 0, STD_C89
, NOLENGTHS
, "O^#", "p", NULL
},
904 { "cx", 0, STD_C89
, NOLENGTHS
, "E", "3", NULL
},
905 { "HIMSUWdmw", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "", NULL
},
906 { "j", 0, STD_C89
, NOLENGTHS
, "-_0Ow", "o", NULL
},
907 { "p", 0, STD_C89
, NOLENGTHS
, "#", "", NULL
},
908 { "X", 0, STD_C89
, NOLENGTHS
, "E", "", NULL
},
909 { "y", 0, STD_C89
, NOLENGTHS
, "EO-_0w", "4", NULL
},
910 { "Y", 0, STD_C89
, NOLENGTHS
, "-_0EOw", "o", NULL
},
911 { "%", 0, STD_C89
, NOLENGTHS
, "", "", NULL
},
912 /* C99 conversion specifiers. */
913 { "C", 0, STD_C99
, NOLENGTHS
, "-_0EOw", "o", NULL
},
914 { "D", 0, STD_C99
, NOLENGTHS
, "", "2", NULL
},
915 { "eVu", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "", NULL
},
916 { "FRTnrt", 0, STD_C99
, NOLENGTHS
, "", "", NULL
},
917 { "g", 0, STD_C99
, NOLENGTHS
, "O-_0w", "2o", NULL
},
918 { "G", 0, STD_C99
, NOLENGTHS
, "-_0Ow", "o", NULL
},
919 { "h", 0, STD_C99
, NOLENGTHS
, "^#", "", NULL
},
920 { "z", 0, STD_C99
, NOLENGTHS
, "O", "o", NULL
},
921 /* GNU conversion specifiers. */
922 { "kls", 0, STD_EXT
, NOLENGTHS
, "-_0Ow", "", NULL
},
923 { "P", 0, STD_EXT
, NOLENGTHS
, "", "", NULL
},
924 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
927 static const format_char_info monetary_char_table
[] =
929 { "in", 0, STD_C89
, { T89_D
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, T89_LD
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
, BADLEN
}, "=^+(!-w#p", "", NULL
},
930 { NULL
, 0, STD_C89
, NOLENGTHS
, NULL
, NULL
, NULL
}
933 /* This must be in the same order as enum format_type. */
934 static const format_kind_info format_types_orig
[] =
936 { "gnu_printf", printf_length_specs
, print_char_table
, " +#0-'I", NULL
,
937 printf_flag_specs
, printf_flag_pairs
,
938 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_DOLLAR_MULTIPLE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_EMPTY_PREC_OK
,
939 'w', 0, 'p', 0, 'L', 0,
940 &integer_type_node
, &integer_type_node
942 { "asm_fprintf", asm_fprintf_length_specs
, asm_fprintf_char_table
, " +#0-", NULL
,
943 asm_fprintf_flag_specs
, asm_fprintf_flag_pairs
,
944 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_EMPTY_PREC_OK
,
945 'w', 0, 'p', 0, 'L', 0,
948 { "gcc_diag", gcc_diag_length_specs
, gcc_diag_char_table
, "q+#", NULL
,
949 gcc_diag_flag_specs
, gcc_diag_flag_pairs
,
950 FMT_FLAG_ARG_CONVERT
,
951 0, 0, 'p', 0, 'L', 0,
952 NULL
, &integer_type_node
954 { "gcc_tdiag", gcc_tdiag_length_specs
, gcc_tdiag_char_table
, "q+#", NULL
,
955 gcc_tdiag_flag_specs
, gcc_tdiag_flag_pairs
,
956 FMT_FLAG_ARG_CONVERT
,
957 0, 0, 'p', 0, 'L', 0,
958 NULL
, &integer_type_node
960 { "gcc_cdiag", gcc_cdiag_length_specs
, gcc_cdiag_char_table
, "q+#", NULL
,
961 gcc_cdiag_flag_specs
, gcc_cdiag_flag_pairs
,
962 FMT_FLAG_ARG_CONVERT
,
963 0, 0, 'p', 0, 'L', 0,
964 NULL
, &integer_type_node
966 { "gcc_cxxdiag", gcc_cxxdiag_length_specs
, gcc_cxxdiag_char_table
, "q+#", NULL
,
967 gcc_cxxdiag_flag_specs
, gcc_cxxdiag_flag_pairs
,
968 FMT_FLAG_ARG_CONVERT
,
969 0, 0, 'p', 0, 'L', 0,
970 NULL
, &integer_type_node
972 { "gcc_gfc", gcc_gfc_length_specs
, gcc_gfc_char_table
, "q+#", NULL
,
973 gcc_gfc_flag_specs
, gcc_gfc_flag_pairs
,
974 FMT_FLAG_ARG_CONVERT
,
978 { "gcc_dump_printf", gcc_dump_printf_length_specs
,
979 gcc_dump_printf_char_table
, "q+#", NULL
,
980 gcc_dump_printf_flag_specs
, gcc_dump_printf_flag_pairs
,
981 FMT_FLAG_ARG_CONVERT
,
982 0, 0, 'p', 0, 'L', 0,
983 NULL
, &integer_type_node
985 { "NSString", NULL
, NULL
, NULL
, NULL
,
987 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
, 0, 0, 0, 0, 0, 0,
990 { "gnu_scanf", scanf_length_specs
, scan_char_table
, "*'I", NULL
,
991 scanf_flag_specs
, scanf_flag_pairs
,
992 FMT_FLAG_ARG_CONVERT
|FMT_FLAG_SCANF_A_KLUDGE
|FMT_FLAG_USE_DOLLAR
|FMT_FLAG_ZERO_WIDTH_BAD
|FMT_FLAG_DOLLAR_GAP_POINTER_OK
,
993 'w', 0, 0, '*', 'L', 'm',
996 { "gnu_strftime", NULL
, time_char_table
, "_-0^#", "EO",
997 strftime_flag_specs
, strftime_flag_pairs
,
998 FMT_FLAG_FANCY_PERCENT_OK
, 'w', 0, 0, 0, 0, 0,
1001 { "gnu_strfmon", strfmon_length_specs
, monetary_char_table
, "=^+(!-", NULL
,
1002 strfmon_flag_specs
, strfmon_flag_pairs
,
1003 FMT_FLAG_ARG_CONVERT
, 'w', '#', 'p', 0, 'L', 0,
1008 /* This layer of indirection allows GCC to reassign format_types with
1009 new data if necessary, while still allowing the original data to be
1011 static const format_kind_info
*format_types
= format_types_orig
;
1012 /* We can modify this one. We also add target-specific format types
1013 to the end of the array. */
1014 static format_kind_info
*dynamic_format_types
;
1016 static int n_format_types
= ARRAY_SIZE (format_types_orig
);
1018 /* Structure detailing the results of checking a format function call
1019 where the format expression may be a conditional expression with
1020 many leaves resulting from nested conditional expressions. */
1021 struct format_check_results
1023 /* Number of leaves of the format argument that could not be checked
1024 as they were not string literals. */
1025 int number_non_literal
;
1026 /* Number of leaves of the format argument that were null pointers or
1027 string literals, but had extra format arguments. */
1028 int number_extra_args
;
1029 location_t extra_arg_loc
;
1030 /* Number of leaves of the format argument that were null pointers or
1031 string literals, but had extra format arguments and used $ operand
1033 int number_dollar_extra_args
;
1034 /* Number of leaves of the format argument that were wide string
1037 /* Number of leaves of the format argument that are not array of "char". */
1038 int number_non_char
;
1039 /* Number of leaves of the format argument that were empty strings. */
1041 /* Number of leaves of the format argument that were unterminated
1043 int number_unterminated
;
1044 /* Number of leaves of the format argument that were not counted above. */
1046 /* Location of the format string. */
1047 location_t format_string_loc
;
1050 struct format_check_context
1052 format_check_results
*res
;
1053 function_format_info
*info
;
1055 vec
<location_t
> *arglocs
;
1058 /* Return the format name (as specified in the original table) for the format
1059 type indicated by format_num. */
1061 format_name (int format_num
)
1063 if (format_num
>= 0 && format_num
< n_format_types
)
1064 return format_types
[format_num
].name
;
1068 /* Return the format flags (as specified in the original table) for the format
1069 type indicated by format_num. */
1071 format_flags (int format_num
)
1073 if (format_num
>= 0 && format_num
< n_format_types
)
1074 return format_types
[format_num
].flags
;
1078 static void check_format_info (function_format_info
*, tree
,
1080 static void check_format_arg (void *, tree
, unsigned HOST_WIDE_INT
);
1081 static void check_format_info_main (format_check_results
*,
1082 function_format_info
*, const char *,
1085 unsigned HOST_WIDE_INT
,
1086 object_allocator
<format_wanted_type
> &,
1089 static void init_dollar_format_checking (int, tree
);
1090 static int maybe_read_dollar_number (const char **, int,
1091 tree
, tree
*, const format_kind_info
*);
1092 static bool avoid_dollar_number (const char *);
1093 static void finish_dollar_format_checking (format_check_results
*, int);
1095 static const format_flag_spec
*get_flag_spec (const format_flag_spec
*,
1098 static void check_format_types (const substring_loc
&fmt_loc
,
1099 format_wanted_type
*,
1100 const format_kind_info
*fki
,
1101 int offset_to_type_start
,
1102 char conversion_char
,
1103 vec
<location_t
> *arglocs
);
1104 static void format_type_warning (const substring_loc
&fmt_loc
,
1105 location_t param_loc
,
1106 format_wanted_type
*, tree
,
1108 const format_kind_info
*fki
,
1109 int offset_to_type_start
,
1110 char conversion_char
);
1112 /* Decode a format type from a string, returning the type, or
1113 format_type_error if not valid, in which case the caller should
1114 print an error message. On success, when IS_RAW is non-null, set
1115 *IS_RAW when the format type corresponds to a GCC "raw" diagnostic
1116 formatting function and clear it otherwise. */
1118 decode_format_type (const char *s
, bool *is_raw
/* = NULL */)
1123 is_raw
= &is_raw_buf
;
1127 s
= convert_format_name_to_system_name (s
);
1129 size_t slen
= strlen (s
);
1130 for (int i
= 0; i
< n_format_types
; i
++)
1132 /* Check for a match with no underscores. */
1133 if (!strcmp (s
, format_types
[i
].name
))
1134 return static_cast<format_type
> (i
);
1136 /* Check for leading and trailing underscores. */
1137 size_t alen
= strlen (format_types
[i
].name
);
1138 if (slen
== alen
+ 4 && s
[0] == '_' && s
[1] == '_'
1139 && s
[slen
- 1] == '_' && s
[slen
- 2] == '_'
1140 && !strncmp (s
+ 2, format_types
[i
].name
, alen
))
1141 return static_cast<format_type
>(i
);
1143 /* Check for the "_raw" suffix and no leading underscores. */
1144 if (slen
== alen
+ 4
1145 && !strncmp (s
, format_types
[i
].name
, alen
)
1146 && !strcmp (s
+ alen
, "_raw"))
1149 return static_cast<format_type
>(i
);
1152 /* Check for the "_raw__" suffix and leading underscores. */
1153 if (slen
== alen
+ 8 && s
[0] == '_' && s
[1] == '_'
1154 && !strncmp (s
+ 2, format_types
[i
].name
, alen
)
1155 && !strcmp (s
+ 2 + alen
, "_raw__"))
1158 return static_cast<format_type
>(i
);
1162 return format_type_error
;
1166 /* Check the argument list of a call to printf, scanf, etc.
1167 ATTRS are the attributes on the function type. There are NARGS argument
1168 values in the array ARGARRAY. FN is either a function declaration or
1169 function type. Also, if -Wsuggest-attribute=format, warn for calls to
1170 vprintf or vscanf in functions with no such format attribute themselves. */
1173 check_function_format (const_tree fn
, tree attrs
, int nargs
,
1174 tree
*argarray
, vec
<location_t
> *arglocs
)
1178 tree atname
= get_identifier ("format");
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);
1191 /* FIXME: Rewrite all the internal functions in this file
1192 to use the ARGARRAY directly instead of constructing this
1194 tree params
= NULL_TREE
;
1196 for (i
= nargs
- 1; i
>= 0; i
--)
1197 params
= tree_cons (NULL_TREE
, argarray
[i
], params
);
1198 check_format_info (&info
, params
, arglocs
);
1201 /* Attempt to detect whether the current function might benefit
1202 from the format attribute if the called function is decorated
1203 with it. Avoid using calls with string literal formats for
1204 guidance since those are unlikely to be viable candidates. */
1205 if (warn_suggest_attribute_format
1206 && current_function_decl
!= NULL_TREE
1207 && info
.first_arg_num
== 0
1208 && (format_types
[info
.format_type
].flags
1209 & (int) FMT_FLAG_ARG_CONVERT
)
1210 /* c_strlen will fail for a function parameter but succeed
1211 for a literal or constant array. */
1212 && !c_strlen (argarray
[info
.format_num
- 1], 1))
1215 for (c
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
1218 if (is_attribute_p ("format", get_attribute_name (c
))
1219 && (decode_format_type (IDENTIFIER_POINTER
1220 (TREE_VALUE (TREE_VALUE (c
))))
1221 == info
.format_type
))
1225 /* Check if the current function has a parameter to which
1226 the format attribute could be attached; if not, it
1227 can't be a candidate for a format attribute, despite
1228 the vprintf-like or vscanf-like call. */
1230 for (args
= DECL_ARGUMENTS (current_function_decl
);
1232 args
= DECL_CHAIN (args
))
1234 if (TREE_CODE (TREE_TYPE (args
)) == POINTER_TYPE
1235 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args
)))
1240 warning (OPT_Wsuggest_attribute_format
, "function %qD "
1241 "might be a candidate for %qs format attribute",
1242 current_function_decl
,
1243 format_types
[info
.format_type
].name
);
1251 /* Variables used by the checking of $ operand number formats. */
1252 static char *dollar_arguments_used
= NULL
;
1253 static char *dollar_arguments_pointer_p
= NULL
;
1254 static int dollar_arguments_alloc
= 0;
1255 static int dollar_arguments_count
;
1256 static int dollar_first_arg_num
;
1257 static int dollar_max_arg_used
;
1258 static int dollar_format_warned
;
1260 /* Initialize the checking for a format string that may contain $
1261 parameter number specifications; we will need to keep track of whether
1262 each parameter has been used. FIRST_ARG_NUM is the number of the first
1263 argument that is a parameter to the format, or 0 for a vprintf-style
1264 function; PARAMS is the list of arguments starting at this argument. */
1267 init_dollar_format_checking (int first_arg_num
, tree params
)
1269 tree oparams
= params
;
1271 dollar_first_arg_num
= first_arg_num
;
1272 dollar_arguments_count
= 0;
1273 dollar_max_arg_used
= 0;
1274 dollar_format_warned
= 0;
1275 if (first_arg_num
> 0)
1279 dollar_arguments_count
++;
1280 params
= TREE_CHAIN (params
);
1283 if (dollar_arguments_alloc
< dollar_arguments_count
)
1285 free (dollar_arguments_used
);
1286 free (dollar_arguments_pointer_p
);
1287 dollar_arguments_alloc
= dollar_arguments_count
;
1288 dollar_arguments_used
= XNEWVEC (char, dollar_arguments_alloc
);
1289 dollar_arguments_pointer_p
= XNEWVEC (char, dollar_arguments_alloc
);
1291 if (dollar_arguments_alloc
)
1293 memset (dollar_arguments_used
, 0, dollar_arguments_alloc
);
1294 if (first_arg_num
> 0)
1300 dollar_arguments_pointer_p
[i
] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params
)))
1302 params
= TREE_CHAIN (params
);
1310 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1311 is set, it is an error if one is not found; otherwise, it is OK. If
1312 such a number is found, check whether it is within range and mark that
1313 numbered operand as being used for later checking. Returns the operand
1314 number if found and within range, zero if no such number was found and
1315 this is OK, or -1 on error. PARAMS points to the first operand of the
1316 format; PARAM_PTR is made to point to the parameter referred to. If
1317 a $ format is found, *FORMAT is updated to point just after it. */
1320 maybe_read_dollar_number (const char **format
,
1321 int dollar_needed
, tree params
, tree
*param_ptr
,
1322 const format_kind_info
*fki
)
1326 const char *fcp
= *format
;
1327 if (!ISDIGIT (*fcp
))
1331 warning (OPT_Wformat_
, "missing $ operand number in format");
1339 while (ISDIGIT (*fcp
))
1341 HOST_WIDE_INT nargnum
1342 = HOST_WIDE_INT_UC (10) * argnum
+ (*fcp
- '0');
1343 if ((int) nargnum
!= nargnum
)
1352 warning (OPT_Wformat_
, "missing $ operand number in format");
1359 if (pedantic
&& !dollar_format_warned
)
1361 warning (OPT_Wformat_
, "%s does not support %%n$ operand number formats",
1362 C_STD_NAME (STD_EXT
));
1363 dollar_format_warned
= 1;
1365 if (overflow_flag
|| argnum
== 0
1366 || (dollar_first_arg_num
&& argnum
> dollar_arguments_count
))
1368 warning (OPT_Wformat_
, "operand number out of range in format");
1371 if (argnum
> dollar_max_arg_used
)
1372 dollar_max_arg_used
= argnum
;
1373 /* For vprintf-style functions we may need to allocate more memory to
1374 track which arguments are used. */
1375 while (dollar_arguments_alloc
< dollar_max_arg_used
)
1378 nalloc
= 2 * dollar_arguments_alloc
+ 16;
1379 dollar_arguments_used
= XRESIZEVEC (char, dollar_arguments_used
,
1381 dollar_arguments_pointer_p
= XRESIZEVEC (char, dollar_arguments_pointer_p
,
1383 memset (dollar_arguments_used
+ dollar_arguments_alloc
, 0,
1384 nalloc
- dollar_arguments_alloc
);
1385 dollar_arguments_alloc
= nalloc
;
1387 if (!(fki
->flags
& (int) FMT_FLAG_DOLLAR_MULTIPLE
)
1388 && dollar_arguments_used
[argnum
- 1] == 1)
1390 dollar_arguments_used
[argnum
- 1] = 2;
1391 warning (OPT_Wformat_
, "format argument %d used more than once in %s format",
1395 dollar_arguments_used
[argnum
- 1] = 1;
1396 if (dollar_first_arg_num
)
1399 *param_ptr
= params
;
1400 for (i
= 1; i
< argnum
&& *param_ptr
!= 0; i
++)
1401 *param_ptr
= TREE_CHAIN (*param_ptr
);
1403 /* This case shouldn't be caught here. */
1404 gcc_assert (*param_ptr
);
1411 /* Ensure that FORMAT does not start with a decimal number followed by
1412 a $; give a diagnostic and return true if it does, false otherwise. */
1415 avoid_dollar_number (const char *format
)
1417 if (!ISDIGIT (*format
))
1419 while (ISDIGIT (*format
))
1423 warning (OPT_Wformat_
,
1424 "%<$%>operand number used after format without operand number");
1431 /* Finish the checking for a format string that used $ operand number formats
1432 instead of non-$ formats. We check for unused operands before used ones
1433 (a serious error, since the implementation of the format function
1434 can't know what types to pass to va_arg to find the later arguments).
1435 and for unused operands at the end of the format (if we know how many
1436 arguments the format had, so not for vprintf). If there were operand
1437 numbers out of range on a non-vprintf-style format, we won't have reached
1438 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1442 finish_dollar_format_checking (format_check_results
*res
, int pointer_gap_ok
)
1445 bool found_pointer_gap
= false;
1446 for (i
= 0; i
< dollar_max_arg_used
; i
++)
1448 if (!dollar_arguments_used
[i
])
1450 if (pointer_gap_ok
&& (dollar_first_arg_num
== 0
1451 || dollar_arguments_pointer_p
[i
]))
1452 found_pointer_gap
= true;
1454 warning_at (res
->format_string_loc
, OPT_Wformat_
,
1455 "format argument %d unused before used argument %d "
1456 "in %<$%>-style format",
1457 i
+ 1, dollar_max_arg_used
);
1460 if (found_pointer_gap
1461 || (dollar_first_arg_num
1462 && dollar_max_arg_used
< dollar_arguments_count
))
1464 res
->number_other
--;
1465 res
->number_dollar_extra_args
++;
1470 /* Retrieve the specification for a format flag. SPEC contains the
1471 specifications for format flags for the applicable kind of format.
1472 FLAG is the flag in question. If PREDICATES is NULL, the basic
1473 spec for that flag must be retrieved and must exist. If
1474 PREDICATES is not NULL, it is a string listing possible predicates
1475 for the spec entry; if an entry predicated on any of these is
1476 found, it is returned, otherwise NULL is returned. */
1478 static const format_flag_spec
*
1479 get_flag_spec (const format_flag_spec
*spec
, int flag
, const char *predicates
)
1482 for (i
= 0; spec
[i
].flag_char
!= 0; i
++)
1484 if (spec
[i
].flag_char
!= flag
)
1486 if (predicates
!= NULL
)
1488 if (spec
[i
].predicate
!= 0
1489 && strchr (predicates
, spec
[i
].predicate
) != 0)
1492 else if (spec
[i
].predicate
== 0)
1495 gcc_assert (predicates
);
1500 /* Check the argument list of a call to printf, scanf, etc.
1501 INFO points to the function_format_info structure.
1502 PARAMS is the list of argument values. */
1505 check_format_info (function_format_info
*info
, tree params
,
1506 vec
<location_t
> *arglocs
)
1508 format_check_context format_ctx
;
1509 unsigned HOST_WIDE_INT arg_num
;
1511 format_check_results res
;
1512 /* Skip to format argument. If the argument isn't available, there's
1513 no work for us to do; prototype checking will catch the problem. */
1514 for (arg_num
= 1; ; ++arg_num
)
1518 if (arg_num
== info
->format_num
)
1520 params
= TREE_CHAIN (params
);
1522 format_tree
= TREE_VALUE (params
);
1523 params
= TREE_CHAIN (params
);
1524 if (format_tree
== 0)
1527 res
.number_non_literal
= 0;
1528 res
.number_extra_args
= 0;
1529 res
.extra_arg_loc
= UNKNOWN_LOCATION
;
1530 res
.number_dollar_extra_args
= 0;
1531 res
.number_wide
= 0;
1532 res
.number_non_char
= 0;
1533 res
.number_empty
= 0;
1534 res
.number_unterminated
= 0;
1535 res
.number_other
= 0;
1536 res
.format_string_loc
= input_location
;
1538 format_ctx
.res
= &res
;
1539 format_ctx
.info
= info
;
1540 format_ctx
.params
= params
;
1541 format_ctx
.arglocs
= arglocs
;
1543 check_function_arguments_recurse (check_format_arg
, &format_ctx
,
1544 format_tree
, arg_num
, OPT_Wformat_
);
1546 location_t loc
= format_ctx
.res
->format_string_loc
;
1548 if (res
.number_non_literal
> 0)
1550 /* Functions taking a va_list normally pass a non-literal format
1551 string. These functions typically are declared with
1552 first_arg_num == 0, so avoid warning in those cases. */
1553 if (!(format_types
[info
->format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
))
1555 /* For strftime-like formats, warn for not checking the format
1556 string; but there are no arguments to check. */
1557 warning_at (loc
, OPT_Wformat_nonliteral
,
1558 "format not a string literal, format string not checked");
1560 else if (info
->first_arg_num
!= 0)
1562 /* If there are no arguments for the format at all, we may have
1563 printf (foo) which is likely to be a security hole. */
1564 while (arg_num
+ 1 < info
->first_arg_num
)
1568 params
= TREE_CHAIN (params
);
1571 if (params
== 0 && warn_format_security
)
1572 warning_at (loc
, OPT_Wformat_security
,
1573 "format not a string literal and no format arguments");
1574 else if (params
== 0 && warn_format_nonliteral
)
1575 warning_at (loc
, OPT_Wformat_nonliteral
,
1576 "format not a string literal and no format arguments");
1578 warning_at (loc
, OPT_Wformat_nonliteral
,
1579 "format not a string literal, argument types not checked");
1583 /* If there were extra arguments to the format, normally warn. However,
1584 the standard does say extra arguments are ignored, so in the specific
1585 case where we have multiple leaves (conditional expressions or
1586 ngettext) allow extra arguments if at least one leaf didn't have extra
1587 arguments, but was otherwise OK (either non-literal or checked OK).
1588 If the format is an empty string, this should be counted similarly to the
1589 case of extra format arguments. */
1590 if (res
.number_extra_args
> 0 && res
.number_non_literal
== 0
1591 && res
.number_other
== 0)
1593 if (res
.extra_arg_loc
== UNKNOWN_LOCATION
)
1594 res
.extra_arg_loc
= loc
;
1595 warning_at (res
.extra_arg_loc
, OPT_Wformat_extra_args
,
1596 "too many arguments for format");
1598 if (res
.number_dollar_extra_args
> 0 && res
.number_non_literal
== 0
1599 && res
.number_other
== 0)
1600 warning_at (loc
, OPT_Wformat_extra_args
,
1601 "unused arguments in %<$%>-style format");
1602 if (res
.number_empty
> 0 && res
.number_non_literal
== 0
1603 && res
.number_other
== 0)
1604 warning_at (loc
, OPT_Wformat_zero_length
, "zero-length %s format string",
1605 format_types
[info
->format_type
].name
);
1607 if (res
.number_wide
> 0)
1608 warning_at (loc
, OPT_Wformat_
, "format is a wide character string");
1610 if (res
.number_non_char
> 0)
1611 warning_at (loc
, OPT_Wformat_
,
1612 "format string is not an array of type %qs", "char");
1614 if (res
.number_unterminated
> 0)
1615 warning_at (loc
, OPT_Wformat_
, "unterminated format string");
1618 /* Callback from check_function_arguments_recurse to check a
1619 format string. FORMAT_TREE is the format parameter. ARG_NUM
1620 is the number of the format argument. CTX points to a
1621 format_check_context. */
1624 check_format_arg (void *ctx
, tree format_tree
,
1625 unsigned HOST_WIDE_INT arg_num
)
1627 format_check_context
*format_ctx
= (format_check_context
*) ctx
;
1628 format_check_results
*res
= format_ctx
->res
;
1629 function_format_info
*info
= format_ctx
->info
;
1630 tree params
= format_ctx
->params
;
1631 vec
<location_t
> *arglocs
= format_ctx
->arglocs
;
1634 HOST_WIDE_INT offset
;
1635 const char *format_chars
;
1636 tree array_size
= 0;
1639 location_t fmt_param_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1641 /* Pull out a constant value if the front end didn't, and handle location
1643 format_tree
= fold_for_warn (format_tree
);
1644 STRIP_NOPS (format_tree
);
1646 if (integer_zerop (format_tree
))
1648 /* Skip to first argument to check, so we can see if this format
1649 has any arguments (it shouldn't). */
1650 while (arg_num
+ 1 < info
->first_arg_num
)
1654 params
= TREE_CHAIN (params
);
1659 res
->number_other
++;
1662 if (res
->number_extra_args
== 0)
1663 res
->extra_arg_loc
= EXPR_LOC_OR_LOC (TREE_VALUE (params
),
1665 res
->number_extra_args
++;
1671 if (TREE_CODE (format_tree
) == POINTER_PLUS_EXPR
)
1675 arg0
= TREE_OPERAND (format_tree
, 0);
1676 arg1
= TREE_OPERAND (format_tree
, 1);
1679 if (TREE_CODE (arg1
) == INTEGER_CST
)
1683 res
->number_non_literal
++;
1686 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1687 if (!cst_and_fits_in_hwi (arg1
))
1689 res
->number_non_literal
++;
1692 offset
= int_cst_value (arg1
);
1694 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1696 res
->number_non_literal
++;
1699 res
->format_string_loc
= EXPR_LOC_OR_LOC (format_tree
, input_location
);
1700 format_tree
= TREE_OPERAND (format_tree
, 0);
1701 if (format_types
[info
->format_type
].flags
1702 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL
)
1704 bool objc_str
= (info
->format_type
== gcc_objc_string_format_type
);
1705 /* We cannot examine this string here - but we can check that it is
1707 if (TREE_CODE (format_tree
) != CONST_DECL
1708 || !((objc_str
&& objc_string_ref_type_p (TREE_TYPE (format_tree
)))
1709 || (*targetcm
.string_object_ref_type_p
)
1710 ((const_tree
) TREE_TYPE (format_tree
))))
1712 res
->number_non_literal
++;
1715 /* Skip to first argument to check. */
1716 while (arg_num
+ 1 < info
->first_arg_num
)
1720 params
= TREE_CHAIN (params
);
1723 /* So, we have a valid literal string object and one or more params.
1724 We need to use an external helper to parse the string into format
1725 info. For Objective-C variants we provide the resource within the
1726 objc tree, for target variants, via a hook. */
1728 objc_check_format_arg (format_tree
, params
);
1729 else if (targetcm
.check_string_object_format_arg
)
1730 (*targetcm
.check_string_object_format_arg
) (format_tree
, params
);
1731 /* Else we can't handle it and retire quietly. */
1734 if (TREE_CODE (format_tree
) == ARRAY_REF
1735 && tree_fits_shwi_p (TREE_OPERAND (format_tree
, 1))
1736 && (offset
+= tree_to_shwi (TREE_OPERAND (format_tree
, 1))) >= 0)
1737 format_tree
= TREE_OPERAND (format_tree
, 0);
1740 res
->number_non_literal
++;
1743 if (VAR_P (format_tree
)
1744 && TREE_CODE (TREE_TYPE (format_tree
)) == ARRAY_TYPE
1745 && (array_init
= decl_constant_value (format_tree
)) != format_tree
1746 && TREE_CODE (array_init
) == STRING_CST
)
1748 /* Extract the string constant initializer. Note that this may include
1749 a trailing NUL character that is not in the array (e.g.
1750 const char a[3] = "foo";). */
1751 array_size
= DECL_SIZE_UNIT (format_tree
);
1752 format_tree
= array_init
;
1754 if (TREE_CODE (format_tree
) != STRING_CST
)
1756 res
->number_non_literal
++;
1759 tree underlying_type
1760 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree
)));
1761 if (underlying_type
!= char_type_node
1762 && !(flag_char8_t
&& underlying_type
== char8_type_node
))
1764 if (underlying_type
== char16_type_node
1765 || underlying_type
== char32_type_node
1766 || underlying_type
== wchar_type_node
)
1769 res
->number_non_char
++;
1772 format_chars
= TREE_STRING_POINTER (format_tree
);
1773 format_length
= TREE_STRING_LENGTH (format_tree
);
1774 if (array_size
!= 0)
1776 /* Variable length arrays can't be initialized. */
1777 gcc_assert (TREE_CODE (array_size
) == INTEGER_CST
);
1779 if (tree_fits_shwi_p (array_size
))
1781 HOST_WIDE_INT array_size_value
= tree_to_shwi (array_size
);
1782 if (array_size_value
> 0
1783 && array_size_value
== (int) array_size_value
1784 && format_length
> array_size_value
)
1785 format_length
= array_size_value
;
1790 if (offset
>= format_length
)
1792 res
->number_non_literal
++;
1795 format_chars
+= offset
;
1796 format_length
-= offset
;
1798 if (format_length
< 1 || format_chars
[--format_length
] != 0)
1800 res
->number_unterminated
++;
1803 if (format_length
== 0)
1805 res
->number_empty
++;
1809 /* Skip to first argument to check. */
1810 while (arg_num
+ 1 < info
->first_arg_num
)
1814 params
= TREE_CHAIN (params
);
1817 /* Provisionally increment res->number_other; check_format_info_main
1818 will decrement it if it finds there are extra arguments, but this way
1819 need not adjust it for every return. */
1820 res
->number_other
++;
1821 object_allocator
<format_wanted_type
> fwt_pool ("format_wanted_type pool");
1822 check_format_info_main (res
, info
, format_chars
, fmt_param_loc
, format_tree
,
1823 format_length
, params
, arg_num
, fwt_pool
, arglocs
);
1826 /* Support class for argument_parser and check_format_info_main.
1827 Tracks any flag characters that have been applied to the
1828 current argument. */
1834 bool has_char_p (char ch
) const;
1835 void add_char (char ch
);
1836 void validate (const format_kind_info
*fki
,
1837 const format_char_info
*fci
,
1838 const format_flag_spec
*flag_specs
,
1839 const char * const format_chars
,
1840 tree format_string_cst
,
1841 location_t format_string_loc
,
1842 const char * const orig_format_chars
,
1845 int get_alloc_flag (const format_kind_info
*fki
);
1846 int assignment_suppression_p (const format_kind_info
*fki
);
1849 char m_flag_chars
[256];
1852 /* Support struct for argument_parser and check_format_info_main.
1853 Encapsulates any length modifier applied to the current argument. */
1855 class length_modifier
1859 : chars (NULL
), val (FMT_LEN_none
), std (STD_C89
),
1860 scalar_identity_flag (0)
1864 length_modifier (const char *chars_
,
1865 enum format_lengths val_
,
1866 enum format_std_version std_
,
1867 int scalar_identity_flag_
)
1868 : chars (chars_
), val (val_
), std (std_
),
1869 scalar_identity_flag (scalar_identity_flag_
)
1874 enum format_lengths val
;
1875 enum format_std_version std
;
1876 int scalar_identity_flag
;
1879 /* Parsing one argument within a format string. */
1881 class argument_parser
1884 argument_parser (function_format_info
*info
, const char *&format_chars
,
1885 tree format_string_cst
,
1886 const char * const orig_format_chars
,
1887 location_t format_string_loc
, flag_chars_t
&flag_chars
,
1888 int &has_operand_number
, tree first_fillin_param
,
1889 object_allocator
<format_wanted_type
> &fwt_pool_
,
1890 vec
<location_t
> *arglocs
);
1892 bool read_any_dollar ();
1894 bool read_format_flags ();
1897 read_any_format_width (tree
¶ms
,
1898 unsigned HOST_WIDE_INT
&arg_num
);
1901 read_any_format_left_precision ();
1904 read_any_format_precision (tree
¶ms
,
1905 unsigned HOST_WIDE_INT
&arg_num
);
1907 void handle_alloc_chars ();
1909 length_modifier
read_any_length_modifier ();
1911 void read_any_other_modifier ();
1913 const format_char_info
*find_format_char_info (char format_char
);
1916 validate_flag_pairs (const format_char_info
*fci
,
1920 give_y2k_warnings (const format_char_info
*fci
,
1923 void parse_any_scan_set (const format_char_info
*fci
);
1925 bool handle_conversions (const format_char_info
*fci
,
1926 const length_modifier
&len_modifier
,
1928 const char *&wanted_type_name
,
1929 unsigned HOST_WIDE_INT
&arg_num
,
1934 check_argument_type (const format_char_info
*fci
,
1935 const length_modifier
&len_modifier
,
1937 const char *&wanted_type_name
,
1938 const bool suppressed
,
1939 unsigned HOST_WIDE_INT
&arg_num
,
1941 const int alloc_flag
,
1942 const char * const format_start
,
1943 const char * const type_start
,
1944 location_t fmt_param_loc
,
1945 char conversion_char
);
1948 const function_format_info
*const info
;
1949 const format_kind_info
* const fki
;
1950 const format_flag_spec
* const flag_specs
;
1951 const char *start_of_this_format
;
1952 const char *&format_chars
;
1953 const tree format_string_cst
;
1954 const char * const orig_format_chars
;
1955 const location_t format_string_loc
;
1956 object_allocator
<format_wanted_type
> &fwt_pool
;
1957 flag_chars_t
&flag_chars
;
1959 tree main_arg_params
;
1960 int &has_operand_number
;
1961 const tree first_fillin_param
;
1962 format_wanted_type width_wanted_type
;
1963 format_wanted_type precision_wanted_type
;
1965 format_wanted_type main_wanted_type
;
1967 format_wanted_type
*first_wanted_type
;
1968 format_wanted_type
*last_wanted_type
;
1969 vec
<location_t
> *arglocs
;
1972 /* flag_chars_t's constructor. */
1974 flag_chars_t::flag_chars_t ()
1976 m_flag_chars
[0] = 0;
1979 /* Has CH been seen as a flag within the current argument? */
1982 flag_chars_t::has_char_p (char ch
) const
1984 return strchr (m_flag_chars
, ch
) != 0;
1987 /* Add CH to the flags seen within the current argument. */
1990 flag_chars_t::add_char (char ch
)
1992 int i
= strlen (m_flag_chars
);
1993 m_flag_chars
[i
++] = ch
;
1994 m_flag_chars
[i
] = 0;
1997 /* Validate the individual flags used, removing any that are invalid. */
2000 flag_chars_t::validate (const format_kind_info
*fki
,
2001 const format_char_info
*fci
,
2002 const format_flag_spec
*flag_specs
,
2003 const char * const format_chars
,
2004 tree format_string_cst
,
2005 location_t format_string_loc
,
2006 const char * const orig_format_chars
,
2012 bool quotflag
= false;
2014 for (i
= 0; m_flag_chars
[i
] != 0; i
++)
2016 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2017 m_flag_chars
[i
], NULL
);
2018 m_flag_chars
[i
- d
] = m_flag_chars
[i
];
2019 if (m_flag_chars
[i
] == fki
->length_code_char
)
2022 /* Remember if a quoting flag is seen. */
2023 quotflag
|= s
->quoting
;
2025 if (strchr (fci
->flag_chars
, m_flag_chars
[i
]) == 0)
2027 format_warning_at_char (format_string_loc
, format_string_cst
,
2028 format_chars
- orig_format_chars
,
2030 "%s used with %<%%%c%> %s format",
2031 _(s
->name
), format_char
, fki
->name
);
2037 const format_flag_spec
*t
;
2038 if (ADJ_STD (s
->std
) > C_STD_VER
)
2039 warning_at (format_string_loc
, OPT_Wformat_
,
2040 "%s does not support %s",
2041 C_STD_NAME (s
->std
), _(s
->long_name
));
2042 t
= get_flag_spec (flag_specs
, m_flag_chars
[i
], fci
->flags2
);
2043 if (t
!= NULL
&& ADJ_STD (t
->std
) > ADJ_STD (s
->std
))
2045 const char *long_name
= (t
->long_name
!= NULL
2048 if (ADJ_STD (t
->std
) > C_STD_VER
)
2049 warning_at (format_string_loc
, OPT_Wformat_
,
2050 "%s does not support %s with"
2051 " the %<%%%c%> %s format",
2052 C_STD_NAME (t
->std
), _(long_name
),
2053 format_char
, fki
->name
);
2057 /* Detect quoting directives used within a quoted sequence, such
2058 as GCC's "%<...%qE". */
2059 if (quoted
&& s
->quoting
)
2061 format_warning_at_char (format_string_loc
, format_string_cst
,
2062 format_chars
- orig_format_chars
- 1,
2064 "%s used within a quoted sequence",
2068 m_flag_chars
[i
- d
] = 0;
2072 && strchr (fci
->flags2
, '\''))
2074 format_warning_at_char (format_string_loc
, format_string_cst
,
2075 format_chars
- orig_format_chars
,
2077 "%qc conversion used unquoted",
2082 /* Determine if an assignment-allocation has been set, requiring
2083 an extra char ** for writing back a dynamically-allocated char *.
2084 This is for handling the optional 'm' character in scanf. */
2087 flag_chars_t::get_alloc_flag (const format_kind_info
*fki
)
2089 if ((fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2090 && has_char_p ('a'))
2092 if (fki
->alloc_char
&& has_char_p (fki
->alloc_char
))
2097 /* Determine if an assignment-suppression character was seen.
2098 ('*' in scanf, for discarding the converted input). */
2101 flag_chars_t::assignment_suppression_p (const format_kind_info
*fki
)
2103 if (fki
->suppression_char
2104 && has_char_p (fki
->suppression_char
))
2109 /* Constructor for argument_parser. Initialize for parsing one
2110 argument within a format string. */
2113 argument_parser (function_format_info
*info_
, const char *&format_chars_
,
2114 tree format_string_cst_
,
2115 const char * const orig_format_chars_
,
2116 location_t format_string_loc_
,
2117 flag_chars_t
&flag_chars_
,
2118 int &has_operand_number_
,
2119 tree first_fillin_param_
,
2120 object_allocator
<format_wanted_type
> &fwt_pool_
,
2121 vec
<location_t
> *arglocs_
)
2123 fki (&format_types
[info
->format_type
]),
2124 flag_specs (fki
->flag_specs
),
2125 start_of_this_format (format_chars_
),
2126 format_chars (format_chars_
),
2127 format_string_cst (format_string_cst_
),
2128 orig_format_chars (orig_format_chars_
),
2129 format_string_loc (format_string_loc_
),
2130 fwt_pool (fwt_pool_
),
2131 flag_chars (flag_chars_
),
2133 main_arg_params (NULL
),
2134 has_operand_number (has_operand_number_
),
2135 first_fillin_param (first_fillin_param_
),
2136 first_wanted_type (NULL
),
2137 last_wanted_type (NULL
),
2142 /* Handle dollars at the start of format arguments, setting up main_arg_params
2145 Return true if format parsing is to continue, false otherwise. */
2148 argument_parser::read_any_dollar ()
2150 if ((fki
->flags
& (int) FMT_FLAG_USE_DOLLAR
) && has_operand_number
!= 0)
2152 /* Possibly read a $ operand number at the start of the format.
2153 If one was previously used, one is required here. If one
2154 is not used here, we can't immediately conclude this is a
2155 format without them, since it could be printf %m or scanf %*. */
2157 opnum
= maybe_read_dollar_number (&format_chars
, 0,
2159 &main_arg_params
, fki
);
2164 has_operand_number
= 1;
2165 main_arg_num
= opnum
+ info
->first_arg_num
- 1;
2168 else if (fki
->flags
& FMT_FLAG_USE_DOLLAR
)
2170 if (avoid_dollar_number (format_chars
))
2176 /* Read any format flags, but do not yet validate them beyond removing
2177 duplicates, since in general validation depends on the rest of
2180 Return true if format parsing is to continue, false otherwise. */
2183 argument_parser::read_format_flags ()
2185 while (*format_chars
!= 0
2186 && strchr (fki
->flag_chars
, *format_chars
) != 0)
2188 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2189 *format_chars
, NULL
);
2190 if (flag_chars
.has_char_p (*format_chars
))
2192 format_warning_at_char (format_string_loc
, format_string_cst
,
2193 format_chars
+ 1 - orig_format_chars
,
2195 "repeated %s in format", _(s
->name
));
2198 flag_chars
.add_char (*format_chars
);
2200 if (s
->skip_next_char
)
2203 if (*format_chars
== 0)
2205 warning_at (format_string_loc
, OPT_Wformat_
,
2206 "missing fill character at end of strfmon format");
2216 /* Read any format width, possibly * or *m$.
2218 Return true if format parsing is to continue, false otherwise. */
2222 read_any_format_width (tree
¶ms
,
2223 unsigned HOST_WIDE_INT
&arg_num
)
2225 if (!fki
->width_char
)
2228 if (fki
->width_type
!= NULL
&& *format_chars
== '*')
2230 flag_chars
.add_char (fki
->width_char
);
2231 /* "...a field width...may be indicated by an asterisk.
2232 In this case, an int argument supplies the field width..." */
2234 if (has_operand_number
!= 0)
2237 opnum
= maybe_read_dollar_number (&format_chars
,
2238 has_operand_number
== 1,
2245 has_operand_number
= 1;
2246 arg_num
= opnum
+ info
->first_arg_num
- 1;
2249 has_operand_number
= 0;
2253 if (avoid_dollar_number (format_chars
))
2256 if (info
->first_arg_num
!= 0)
2263 cur_param
= TREE_VALUE (params
);
2264 if (has_operand_number
<= 0)
2266 params
= TREE_CHAIN (params
);
2270 width_wanted_type
.wanted_type
= *fki
->width_type
;
2271 width_wanted_type
.wanted_type_name
= NULL
;
2272 width_wanted_type
.pointer_count
= 0;
2273 width_wanted_type
.char_lenient_flag
= 0;
2274 width_wanted_type
.scalar_identity_flag
= 0;
2275 width_wanted_type
.writing_in_flag
= 0;
2276 width_wanted_type
.reading_from_flag
= 0;
2277 width_wanted_type
.kind
= CF_KIND_FIELD_WIDTH
;
2278 width_wanted_type
.format_start
= format_chars
- 1;
2279 width_wanted_type
.format_length
= 1;
2280 width_wanted_type
.param
= cur_param
;
2281 width_wanted_type
.arg_num
= arg_num
;
2282 width_wanted_type
.offset_loc
=
2283 format_chars
- orig_format_chars
;
2284 width_wanted_type
.next
= NULL
;
2285 if (last_wanted_type
!= 0)
2286 last_wanted_type
->next
= &width_wanted_type
;
2287 if (first_wanted_type
== 0)
2288 first_wanted_type
= &width_wanted_type
;
2289 last_wanted_type
= &width_wanted_type
;
2294 /* Possibly read a numeric width. If the width is zero,
2295 we complain if appropriate. */
2296 int non_zero_width_char
= FALSE
;
2297 int found_width
= FALSE
;
2298 while (ISDIGIT (*format_chars
))
2301 if (*format_chars
!= '0')
2302 non_zero_width_char
= TRUE
;
2305 if (found_width
&& !non_zero_width_char
&&
2306 (fki
->flags
& (int) FMT_FLAG_ZERO_WIDTH_BAD
))
2307 warning_at (format_string_loc
, OPT_Wformat_
,
2308 "zero width in %s format", fki
->name
);
2310 flag_chars
.add_char (fki
->width_char
);
2316 /* Read any format left precision (must be a number, not *). */
2318 argument_parser::read_any_format_left_precision ()
2320 if (fki
->left_precision_char
== 0)
2322 if (*format_chars
!= '#')
2326 flag_chars
.add_char (fki
->left_precision_char
);
2327 if (!ISDIGIT (*format_chars
))
2328 format_warning_at_char (format_string_loc
, format_string_cst
,
2329 format_chars
- orig_format_chars
,
2331 "empty left precision in %s format", fki
->name
);
2332 while (ISDIGIT (*format_chars
))
2336 /* Read any format precision, possibly * or *m$.
2338 Return true if format parsing is to continue, false otherwise. */
2342 read_any_format_precision (tree
¶ms
,
2343 unsigned HOST_WIDE_INT
&arg_num
)
2345 if (fki
->precision_char
== 0)
2347 if (*format_chars
!= '.')
2351 flag_chars
.add_char (fki
->precision_char
);
2352 if (fki
->precision_type
!= NULL
&& *format_chars
== '*')
2354 /* "...a...precision...may be indicated by an asterisk.
2355 In this case, an int argument supplies the...precision." */
2357 if (has_operand_number
!= 0)
2360 opnum
= maybe_read_dollar_number (&format_chars
,
2361 has_operand_number
== 1,
2368 has_operand_number
= 1;
2369 arg_num
= opnum
+ info
->first_arg_num
- 1;
2372 has_operand_number
= 0;
2376 if (avoid_dollar_number (format_chars
))
2379 if (info
->first_arg_num
!= 0)
2386 cur_param
= TREE_VALUE (params
);
2387 if (has_operand_number
<= 0)
2389 params
= TREE_CHAIN (params
);
2393 precision_wanted_type
.wanted_type
= *fki
->precision_type
;
2394 precision_wanted_type
.wanted_type_name
= NULL
;
2395 precision_wanted_type
.pointer_count
= 0;
2396 precision_wanted_type
.char_lenient_flag
= 0;
2397 precision_wanted_type
.scalar_identity_flag
= 0;
2398 precision_wanted_type
.writing_in_flag
= 0;
2399 precision_wanted_type
.reading_from_flag
= 0;
2400 precision_wanted_type
.kind
= CF_KIND_FIELD_PRECISION
;
2401 precision_wanted_type
.param
= cur_param
;
2402 precision_wanted_type
.format_start
= format_chars
- 2;
2403 precision_wanted_type
.format_length
= 2;
2404 precision_wanted_type
.arg_num
= arg_num
;
2405 precision_wanted_type
.offset_loc
=
2406 format_chars
- orig_format_chars
;
2407 precision_wanted_type
.next
= NULL
;
2408 if (last_wanted_type
!= 0)
2409 last_wanted_type
->next
= &precision_wanted_type
;
2410 if (first_wanted_type
== 0)
2411 first_wanted_type
= &precision_wanted_type
;
2412 last_wanted_type
= &precision_wanted_type
;
2417 if (!(fki
->flags
& (int) FMT_FLAG_EMPTY_PREC_OK
)
2418 && !ISDIGIT (*format_chars
))
2419 format_warning_at_char (format_string_loc
, format_string_cst
,
2420 format_chars
- orig_format_chars
,
2422 "empty precision in %s format", fki
->name
);
2423 while (ISDIGIT (*format_chars
))
2430 /* Parse any assignment-allocation flags, which request an extra
2431 char ** for writing back a dynamically-allocated char *.
2432 This is for handling the optional 'm' character in scanf,
2433 and, before C99, 'a' (for compatibility with a non-standard
2434 GNU libc extension). */
2437 argument_parser::handle_alloc_chars ()
2439 if (fki
->alloc_char
&& fki
->alloc_char
== *format_chars
)
2441 flag_chars
.add_char (fki
->alloc_char
);
2445 /* Handle the scanf allocation kludge. */
2446 if (fki
->flags
& (int) FMT_FLAG_SCANF_A_KLUDGE
)
2448 if (*format_chars
== 'a' && !flag_isoc99
)
2450 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
2451 || format_chars
[1] == '[')
2453 /* 'a' is used as a flag. */
2454 flag_chars
.add_char ('a');
2461 /* Look for length modifiers within the current format argument,
2462 returning a length_modifier instance describing it (or the
2463 default if one is not found).
2465 Issue warnings about non-standard modifiers. */
2468 argument_parser::read_any_length_modifier ()
2470 length_modifier result
;
2472 const format_length_info
*fli
= fki
->length_char_specs
;
2476 while (fli
->name
!= 0
2477 && strncmp (fli
->name
, format_chars
, strlen (fli
->name
)))
2481 format_chars
+= strlen (fli
->name
);
2482 if (fli
->double_name
!= 0 && fli
->name
[0] == *format_chars
)
2485 result
= length_modifier (fli
->double_name
, fli
->double_index
,
2486 fli
->double_std
, 0);
2490 result
= length_modifier (fli
->name
, fli
->index
, fli
->std
,
2491 fli
->scalar_identity_flag
);
2493 flag_chars
.add_char (fki
->length_code_char
);
2497 /* Warn if the length modifier is non-standard. */
2498 if (ADJ_STD (result
.std
) > C_STD_VER
)
2499 warning_at (format_string_loc
, OPT_Wformat_
,
2500 "%s does not support the %qs %s length modifier",
2501 C_STD_NAME (result
.std
), result
.chars
,
2508 /* Read any other modifier (strftime E/O). */
2511 argument_parser::read_any_other_modifier ()
2513 if (fki
->modifier_chars
== NULL
)
2516 while (*format_chars
!= 0
2517 && strchr (fki
->modifier_chars
, *format_chars
) != 0)
2519 if (flag_chars
.has_char_p (*format_chars
))
2521 const format_flag_spec
*s
= get_flag_spec (flag_specs
,
2522 *format_chars
, NULL
);
2523 format_warning_at_char (format_string_loc
, format_string_cst
,
2524 format_chars
- orig_format_chars
,
2526 "repeated %s in format", _(s
->name
));
2529 flag_chars
.add_char (*format_chars
);
2534 /* Return the format_char_info corresponding to FORMAT_CHAR,
2535 potentially issuing a warning if the format char is
2536 not supported in the C standard version we are checking
2539 Issue a warning and return NULL if it is not found.
2541 Issue warnings about non-standard modifiers. */
2543 const format_char_info
*
2544 argument_parser::find_format_char_info (char format_char
)
2546 const format_char_info
*fci
= fki
->conversion_specs
;
2548 while (fci
->format_chars
!= 0
2549 && strchr (fci
->format_chars
, format_char
) == 0)
2551 if (fci
->format_chars
== 0)
2553 format_warning_at_char (format_string_loc
, format_string_cst
,
2554 format_chars
- orig_format_chars
,
2556 "unknown conversion type character"
2564 if (ADJ_STD (fci
->std
) > C_STD_VER
)
2565 format_warning_at_char (format_string_loc
, format_string_cst
,
2566 format_chars
- orig_format_chars
,
2568 "%s does not support the %<%%%c%> %s format",
2569 C_STD_NAME (fci
->std
), format_char
, fki
->name
);
2575 /* Validate the pairs of flags used.
2576 Issue warnings about incompatible combinations of flags. */
2579 argument_parser::validate_flag_pairs (const format_char_info
*fci
,
2582 const format_flag_pair
* const bad_flag_pairs
= fki
->bad_flag_pairs
;
2584 for (int i
= 0; bad_flag_pairs
[i
].flag_char1
!= 0; i
++)
2586 const format_flag_spec
*s
, *t
;
2587 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char1
))
2589 if (!flag_chars
.has_char_p (bad_flag_pairs
[i
].flag_char2
))
2591 if (bad_flag_pairs
[i
].predicate
!= 0
2592 && strchr (fci
->flags2
, bad_flag_pairs
[i
].predicate
) == 0)
2594 s
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char1
, NULL
);
2595 t
= get_flag_spec (flag_specs
, bad_flag_pairs
[i
].flag_char2
, NULL
);
2596 if (bad_flag_pairs
[i
].ignored
)
2598 if (bad_flag_pairs
[i
].predicate
!= 0)
2599 warning_at (format_string_loc
, OPT_Wformat_
,
2600 "%s ignored with %s and %<%%%c%> %s format",
2601 _(s
->name
), _(t
->name
), format_char
,
2604 warning_at (format_string_loc
, OPT_Wformat_
,
2605 "%s ignored with %s in %s format",
2606 _(s
->name
), _(t
->name
), fki
->name
);
2610 if (bad_flag_pairs
[i
].predicate
!= 0)
2611 warning_at (format_string_loc
, OPT_Wformat_
,
2612 "use of %s and %s together with %<%%%c%> %s format",
2613 _(s
->name
), _(t
->name
), format_char
,
2616 warning_at (format_string_loc
, OPT_Wformat_
,
2617 "use of %s and %s together in %s format",
2618 _(s
->name
), _(t
->name
), fki
->name
);
2623 /* Give Y2K warnings. */
2626 argument_parser::give_y2k_warnings (const format_char_info
*fci
,
2629 if (!warn_format_y2k
)
2633 if (strchr (fci
->flags2
, '4') != 0)
2634 if (flag_chars
.has_char_p ('E'))
2638 else if (strchr (fci
->flags2
, '3') != 0)
2640 else if (strchr (fci
->flags2
, '2') != 0)
2643 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2644 "%<%%%c%> yields only last 2 digits of "
2645 "year in some locales", format_char
);
2646 else if (y2k_level
== 2)
2647 warning_at (format_string_loc
, OPT_Wformat_y2k
,
2648 "%<%%%c%> yields only last 2 digits of year",
2652 /* Parse any "scan sets" enclosed in square brackets, e.g.
2653 for scanf-style calls. */
2656 argument_parser::parse_any_scan_set (const format_char_info
*fci
)
2658 if (strchr (fci
->flags2
, '[') == NULL
)
2661 /* Skip over scan set, in case it happens to have '%' in it. */
2662 if (*format_chars
== '^')
2664 /* Find closing bracket; if one is hit immediately, then
2665 it's part of the scan set rather than a terminator. */
2666 if (*format_chars
== ']')
2668 while (*format_chars
&& *format_chars
!= ']')
2670 if (*format_chars
!= ']')
2671 /* The end of the format string was reached. */
2672 format_warning_at_char (format_string_loc
, format_string_cst
,
2673 format_chars
- orig_format_chars
,
2675 "no closing %<]%> for %<%%[%> format");
2678 /* Return true if this argument is to be continued to be parsed,
2679 false to skip to next argument. */
2682 argument_parser::handle_conversions (const format_char_info
*fci
,
2683 const length_modifier
&len_modifier
,
2685 const char *&wanted_type_name
,
2686 unsigned HOST_WIDE_INT
&arg_num
,
2690 enum format_std_version wanted_type_std
;
2692 if (!(fki
->flags
& (int) FMT_FLAG_ARG_CONVERT
))
2695 wanted_type
= (fci
->types
[len_modifier
.val
].type
2696 ? *fci
->types
[len_modifier
.val
].type
: 0);
2697 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2698 wanted_type_std
= fci
->types
[len_modifier
.val
].std
;
2699 if (wanted_type
== 0)
2701 format_warning_at_char (format_string_loc
, format_string_cst
,
2702 format_chars
- orig_format_chars
,
2704 "use of %qs length modifier with %qc type"
2705 " character has either no effect"
2706 " or undefined behavior",
2707 len_modifier
.chars
, format_char
);
2708 /* Heuristic: skip one argument when an invalid length/type
2709 combination is encountered. */
2712 params
= TREE_CHAIN (params
);
2716 /* Warn if non-standard, provided it is more non-standard
2717 than the length and type characters that may already
2718 have been warned for. */
2719 && ADJ_STD (wanted_type_std
) > ADJ_STD (len_modifier
.std
)
2720 && ADJ_STD (wanted_type_std
) > ADJ_STD (fci
->std
))
2722 if (ADJ_STD (wanted_type_std
) > C_STD_VER
)
2723 format_warning_at_char (format_string_loc
, format_string_cst
,
2724 format_chars
- orig_format_chars
,
2726 "%s does not support the %<%%%s%c%> %s format",
2727 C_STD_NAME (wanted_type_std
),
2729 format_char
, fki
->name
);
2735 /* Check type of argument against desired type.
2737 Return true if format parsing is to continue, false otherwise. */
2741 check_argument_type (const format_char_info
*fci
,
2742 const length_modifier
&len_modifier
,
2744 const char *&wanted_type_name
,
2745 const bool suppressed
,
2746 unsigned HOST_WIDE_INT
&arg_num
,
2748 const int alloc_flag
,
2749 const char * const format_start
,
2750 const char * const type_start
,
2751 location_t fmt_param_loc
,
2752 char conversion_char
)
2754 if (info
->first_arg_num
== 0)
2757 if ((fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
2760 if (main_arg_num
!= 0)
2763 warning_at (format_string_loc
, OPT_Wformat_
,
2764 "operand number specified with "
2765 "suppressed assignment");
2767 warning_at (format_string_loc
, OPT_Wformat_
,
2768 "operand number specified for format "
2769 "taking no argument");
2774 format_wanted_type
*wanted_type_ptr
;
2776 if (main_arg_num
!= 0)
2778 arg_num
= main_arg_num
;
2779 params
= main_arg_params
;
2784 if (has_operand_number
> 0)
2786 warning_at (format_string_loc
, OPT_Wformat_
,
2787 "missing $ operand number in format");
2791 has_operand_number
= 0;
2794 wanted_type_ptr
= &main_wanted_type
;
2802 cur_param
= TREE_VALUE (params
);
2803 params
= TREE_CHAIN (params
);
2806 wanted_type_ptr
->wanted_type
= wanted_type
;
2807 wanted_type_ptr
->wanted_type_name
= wanted_type_name
;
2808 wanted_type_ptr
->pointer_count
= fci
->pointer_count
+ alloc_flag
;
2809 wanted_type_ptr
->char_lenient_flag
= 0;
2810 if (strchr (fci
->flags2
, 'c') != 0)
2811 wanted_type_ptr
->char_lenient_flag
= 1;
2812 wanted_type_ptr
->scalar_identity_flag
= 0;
2813 if (len_modifier
.scalar_identity_flag
)
2814 wanted_type_ptr
->scalar_identity_flag
= 1;
2815 wanted_type_ptr
->writing_in_flag
= 0;
2816 wanted_type_ptr
->reading_from_flag
= 0;
2818 wanted_type_ptr
->writing_in_flag
= 1;
2821 if (strchr (fci
->flags2
, 'W') != 0)
2822 wanted_type_ptr
->writing_in_flag
= 1;
2823 if (strchr (fci
->flags2
, 'R') != 0)
2824 wanted_type_ptr
->reading_from_flag
= 1;
2826 wanted_type_ptr
->kind
= CF_KIND_FORMAT
;
2827 wanted_type_ptr
->param
= cur_param
;
2828 wanted_type_ptr
->arg_num
= arg_num
;
2829 wanted_type_ptr
->format_start
= format_start
;
2830 wanted_type_ptr
->format_length
= format_chars
- format_start
;
2831 wanted_type_ptr
->offset_loc
= format_chars
- orig_format_chars
;
2832 wanted_type_ptr
->next
= NULL
;
2833 if (last_wanted_type
!= 0)
2834 last_wanted_type
->next
= wanted_type_ptr
;
2835 if (first_wanted_type
== 0)
2836 first_wanted_type
= wanted_type_ptr
;
2837 last_wanted_type
= wanted_type_ptr
;
2842 wanted_type_ptr
= fwt_pool
.allocate ();
2844 wanted_type
= *fci
->types
[len_modifier
.val
].type
;
2845 wanted_type_name
= fci
->types
[len_modifier
.val
].name
;
2850 if (first_wanted_type
!= 0)
2852 ptrdiff_t offset_to_format_start
= (start_of_this_format
- 1) - orig_format_chars
;
2853 ptrdiff_t offset_to_format_end
= (format_chars
- 1) - orig_format_chars
;
2854 /* By default, use the end of the range for the caret location. */
2855 substring_loc
fmt_loc (fmt_param_loc
, TREE_TYPE (format_string_cst
),
2856 offset_to_format_end
,
2857 offset_to_format_start
, offset_to_format_end
);
2858 ptrdiff_t offset_to_type_start
= type_start
- orig_format_chars
;
2859 check_format_types (fmt_loc
, first_wanted_type
, fki
,
2860 offset_to_type_start
,
2861 conversion_char
, arglocs
);
2867 /* Describes "paired tokens" within the format string that are
2868 expected to be balanced. */
2873 baltoks_t (): singlequote (), doublequote () { }
2875 typedef auto_vec
<const char *> balanced_tokens_t
;
2876 /* Vectors of pointers to opening brackets ('['), curly brackets ('{'),
2877 quoting directives (like GCC "%<"), parentheses, and angle brackets
2878 ('<'). Used to detect unbalanced tokens. */
2879 balanced_tokens_t brackets
;
2880 balanced_tokens_t curly
;
2881 balanced_tokens_t quotdirs
;
2882 balanced_tokens_t parens
;
2883 balanced_tokens_t pointy
;
2884 /* Pointer to the last opening quote. */
2885 const char *singlequote
;
2886 const char *doublequote
;
2889 /* Describes a keyword, operator, or other name. */
2893 const char *name
; /* Keyword/operator name. */
2894 unsigned char len
; /* Its length. */
2895 const char *alt
; /* Alternate spelling. */
2898 /* Helper for initializing global token_t arrays below. */
2899 #define NAME(name) { name, sizeof name - 1, NULL }
2901 /* C/C++ operators that are expected to be quoted within the format
2904 static const token_t c_opers
[] =
2906 NAME ("!="), NAME ("%="), NAME ("&&"), NAME ("&="), NAME ("*="),
2907 NAME ("++"), NAME ("+="), NAME ("--"), NAME ("-="), NAME ("->"),
2908 NAME ("/="), NAME ("<<"), NAME ("<<="), NAME ("<="), NAME ("=="),
2909 NAME (">="), NAME (">>="), NAME (">>"), NAME ("?:"), NAME ("^="),
2910 NAME ("|="), NAME ("||")
2913 static const token_t cxx_opers
[] =
2915 NAME ("->*"), NAME (".*"), NAME ("::"), NAME ("<=>")
2918 /* Common C/C++ keywords that are expected to be quoted within the format
2919 string. Keywords like auto, inline, or volatile are excluded because
2920 they are sometimes used in common terms like /auto variables/, /inline
2921 function/, or /volatile access/ where they should not be quoted. */
2923 static const token_t c_keywords
[] =
2926 #define NAME(name, alt) { name, sizeof name - 1, alt }
2928 NAME ("alignas", NULL
),
2929 NAME ("alignof", NULL
),
2931 NAME ("bool", NULL
),
2932 NAME ("char", NULL
),
2933 NAME ("const %", NULL
),
2934 NAME ("const-qualified", "%<const%>-qualified"),
2935 NAME ("float", NULL
),
2936 NAME ("ifunc", NULL
),
2938 NAME ("long double", NULL
),
2939 NAME ("long int", NULL
),
2940 NAME ("long long", NULL
),
2941 NAME ("malloc", NULL
),
2942 NAME ("noclone", NULL
),
2943 NAME ("noinline", NULL
),
2944 NAME ("nonnull", NULL
),
2945 NAME ("noreturn", NULL
),
2946 NAME ("offsetof", NULL
),
2947 NAME ("readonly", "read-only"),
2948 NAME ("readwrite", "read-write"),
2949 NAME ("restrict %", NULL
),
2950 NAME ("restrict-qualified", "%<restrict%>-qualified"),
2951 NAME ("short int", NULL
),
2952 NAME ("signed char", NULL
),
2953 NAME ("signed int", NULL
),
2954 NAME ("signed long", NULL
),
2955 NAME ("signed short", NULL
),
2956 NAME ("sizeof", NULL
),
2957 NAME ("typeof", NULL
),
2958 NAME ("unsigned char", NULL
),
2959 NAME ("unsigned int", NULL
),
2960 NAME ("unsigned long", NULL
),
2961 NAME ("unsigned short", NULL
),
2962 NAME ("volatile %", NULL
),
2963 NAME ("volatile-qualified", "%<volatile%>-qualified"),
2964 NAME ("weakref", NULL
),
2967 static const token_t cxx_keywords
[] =
2969 /* C++ only keywords and operators. */
2970 NAME ("catch", NULL
),
2971 NAME ("constexpr if", NULL
),
2972 NAME ("constexpr", NULL
),
2973 NAME ("constinit", NULL
),
2974 NAME ("consteval", NULL
),
2975 NAME ("decltype", NULL
),
2976 NAME ("nullptr", NULL
),
2977 NAME ("operator delete", NULL
),
2978 NAME ("operator new", NULL
),
2979 NAME ("typeid", NULL
),
2980 NAME ("typeinfo", NULL
)
2983 /* Blacklisted words such as misspellings that should be avoided in favor
2984 of the specified alternatives. */
2987 const char *name
; /* Bad word. */
2988 unsigned char len
; /* Its length. */
2989 const char *alt
; /* Preferred alternative. */
2992 NAME ("arg", "argument"),
2993 NAME ("bitfield", "bit-field"),
2994 NAME ("builtin function", "built-in function"),
2995 NAME ("can not", "cannot"),
2996 NAME ("commandline option", "command-line option"),
2997 NAME ("commandline", "command line"),
2998 NAME ("command line option", "command-line option"),
2999 NAME ("decl", "declaration"),
3000 NAME ("enumeral", "enumerated"),
3001 NAME ("floating point", "floating-point"),
3002 NAME ("nonstatic", "non-static"),
3003 NAME ("non-zero", "nonzero"),
3004 NAME ("reg", "register"),
3005 NAME ("stmt", "statement"),
3008 /* Common contractions that should be avoided in favor of the specified
3013 const char *name
; /* Contraction. */
3014 unsigned char len
; /* Its length. */
3015 const char *alt
; /* Preferred alternative. */
3018 NAME ("can't", "cannot"),
3019 NAME ("didn't", "did not"),
3020 /* These are commonly abused. Avoid diagnosing them for now.
3021 NAME ("isn't", "is not"),
3022 NAME ("don't", "is not"),
3024 NAME ("mustn't", "must not"),
3025 NAME ("needn't", "need not"),
3026 NAME ("should't", "should not"),
3027 NAME ("that's", "that is"),
3028 NAME ("there's", "there is"),
3029 NAME ("they're", "they are"),
3030 NAME ("what's", "what is"),
3031 NAME ("won't", "will not")
3034 /* Check for unquoted TOKENS. FORMAT_STRING_LOC is the location of
3035 the format string, FORMAT_STRING_CST the format string itself (as
3036 a tree), ORIG_FORMAT_CHARS and FORMAT_CHARS are pointers to
3037 the beginning of the format string and the character currently
3038 being processed, and BALTOKS describes paired "tokens" within
3039 the format string that are expected to be balanced.
3040 Returns a pointer to the last processed character or null when
3041 nothing was done. */
3044 check_tokens (const token_t
*tokens
, unsigned ntoks
,
3045 location_t format_string_loc
, tree format_string_cst
,
3046 const char *orig_format_chars
, const char *format_chars
,
3050 const int opt
= OPT_Wformat_diag
;
3051 /* Zero-based starting position of a problem sequence. */
3052 int fmtchrpos
= format_chars
- orig_format_chars
;
3054 /* For identifier-like "words," set to the word length. */
3056 /* Set for an operator, clear for an identifier/word. */
3057 bool is_oper
= false;
3058 bool underscore
= false;
3060 if (format_chars
[0] == '_' || ISALPHA (format_chars
[0]))
3062 while (format_chars
[wlen
] == '_' || ISALNUM (format_chars
[wlen
]))
3064 underscore
|= format_chars
[wlen
] == '_';
3071 for (unsigned i
= 0; i
!= ntoks
; ++i
)
3073 unsigned toklen
= tokens
[i
].len
;
3076 || strncmp (format_chars
, tokens
[i
].name
, toklen
))
3080 && format_chars
- orig_format_chars
> 0
3081 && (TOUPPER (format_chars
[-1]) == 'C'
3082 || TOUPPER (format_chars
[-1]) == 'G'))
3083 return format_chars
+ toklen
- 1; /* Reference to C++ or G++. */
3085 if (ISPUNCT (format_chars
[toklen
- 1]))
3087 if (format_chars
[toklen
- 1] == format_chars
[toklen
])
3088 return NULL
; /* Operator followed by another punctuator. */
3090 else if (ISALNUM (format_chars
[toklen
]))
3091 return NULL
; /* Keyword prefix for a longer word. */
3094 && format_chars
[0] == '-'
3095 && format_chars
[1] == '-'
3096 && ISALNUM (format_chars
[2]))
3097 return NULL
; /* Probably option like --help. */
3099 /* Allow this ugly warning for the time being. */
3101 && format_chars
- orig_format_chars
> 6
3102 && startswith (format_chars
- 7, " count >= width of "))
3103 return format_chars
+ 10;
3105 /* The token is a type if it ends in an alphabetic character. */
3106 bool is_type
= (ISALPHA (tokens
[i
].name
[toklen
- 1])
3107 && strchr (tokens
[i
].name
, ' '));
3109 /* Backtrack to the last alphabetic character (for tokens whose
3110 names end in '%'). */
3112 while (!ISALPHA (tokens
[i
].name
[toklen
- 1]))
3115 if (format_warning_substr (format_string_loc
, format_string_cst
,
3116 fmtchrpos
, fmtchrpos
+ toklen
, opt
,
3118 ? G_("unquoted type name %<%.*s%> in format")
3120 ? G_("unquoted operator %<%.*s%> in format")
3121 : G_("unquoted keyword %<%.*s%> in format"))),
3122 toklen
, format_chars
)
3124 inform (format_string_loc
, "use %qs instead", tokens
[i
].alt
);
3126 return format_chars
+ toklen
- 1;
3129 /* Diagnose unquoted __attribute__. Consider any parenthesized
3130 argument to the attribute to avoid redundant warnings for
3131 the double parentheses that might follow. */
3132 if (startswith (format_chars
, "__attribute"))
3134 unsigned nchars
= sizeof "__attribute" - 1;
3135 while ('_' == format_chars
[nchars
])
3138 for (int i
= nchars
; format_chars
[i
]; ++i
)
3139 if (' ' != format_chars
[i
])
3145 if (format_chars
[nchars
] == '(')
3147 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3151 if (format_chars
[nchars
] == '(')
3153 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3157 for (int i
= nchars
; format_chars
[i
]; ++i
)
3158 if (')' == format_chars
[i
])
3160 if (baltoks
.parens
.length () > 0)
3161 baltoks
.parens
.pop ();
3166 if (close
&& format_chars
[nchars
] == ')')
3168 if (baltoks
.parens
.length () > 0)
3169 baltoks
.parens
.pop ();
3174 format_warning_substr (format_string_loc
, format_string_cst
,
3175 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3176 "unquoted attribute in format");
3177 return format_chars
+ nchars
- 1;
3180 /* Diagnose unquoted built-ins. */
3181 if (format_chars
[0] == '_'
3182 && format_chars
[1] == '_'
3183 && (startswith (format_chars
+ 2, "atomic")
3184 || startswith (format_chars
+ 2, "builtin")
3185 || startswith (format_chars
+ 2, "sync")))
3187 format_warning_substr (format_string_loc
, format_string_cst
,
3188 fmtchrpos
, fmtchrpos
+ wlen
, opt
,
3189 "unquoted name of built-in function %<%.*s%> "
3191 wlen
, format_chars
);
3192 return format_chars
+ wlen
- 1;
3195 /* Diagnose unquoted substrings of alphanumeric characters containing
3196 underscores. They most likely refer to identifiers and should be
3199 format_warning_substr (format_string_loc
, format_string_cst
,
3200 format_chars
- orig_format_chars
,
3201 format_chars
+ wlen
- orig_format_chars
,
3203 "unquoted identifier or keyword %<%.*s%> in format",
3204 wlen
, format_chars
);
3207 /* Diagnose some common misspellings. */
3208 for (unsigned i
= 0; i
!= ARRAY_SIZE (badwords
); ++i
)
3210 unsigned badwlen
= strspn (badwords
[i
].name
, " -");
3212 && (wlen
<= badwords
[i
].len
3213 || (wlen
== badwords
[i
].len
+ 1U
3214 && TOUPPER (format_chars
[wlen
- 1]) == 'S'))
3215 && !strncasecmp (format_chars
, badwords
[i
].name
, badwords
[i
].len
))
3217 /* Handle singular as well as plural forms of all bad words
3218 even though the latter don't necessarily make sense for
3219 all of the former (like "can nots"). */
3220 badwlen
= badwords
[i
].len
;
3221 const char *plural
= "";
3222 if (TOUPPER (format_chars
[badwlen
]) == 'S')
3228 /* As an exception, don't warn about "decl-specifier*" since
3229 it's a C++ grammar production. */
3230 if (badwords
[i
].name
[0] == 'd'
3231 && startswith (format_chars
, "decl-specifier"))
3234 format_warning_substr (format_string_loc
, format_string_cst
,
3235 fmtchrpos
, fmtchrpos
+ badwords
[i
].len
,
3237 "misspelled term %<%.*s%> in format; "
3238 "use %<%s%s%> instead",
3239 badwlen
, format_chars
,
3240 badwords
[i
].alt
, plural
);
3242 return format_chars
+ badwords
[i
].len
- 1;
3247 if (!strncasecmp (format_chars
, "c++", 3)
3248 || !strncasecmp (format_chars
, "g++", 3))
3249 return format_chars
+ 2;
3252 return wlen
? format_chars
+ wlen
- 1 : NULL
;
3255 /* Check plain text in a format string of a GCC diagnostic function
3256 for common quoting, punctuation, and spelling mistakes, and issue
3257 -Wformat-diag warnings if they are found. FORMAT_STRING_LOC is
3258 the location of the format string, FORMAT_STRING_CST the format
3259 string itself (as a tree), ORIG_FORMAT_CHARS and FORMAT_CHARS are
3260 pointers to the beginning of the format string and the character
3261 currently being processed, and BALTOKS describes paired "tokens"
3262 within the format string that are expected to be balanced.
3263 Returns a pointer to the last processed character. */
3266 check_plain (location_t format_string_loc
, tree format_string_cst
,
3267 const char *orig_format_chars
, const char *format_chars
,
3271 const int opt
= OPT_Wformat_diag
;
3272 /* Zero-based starting position of a problem sequence. */
3273 int fmtchrpos
= format_chars
- orig_format_chars
;
3275 if (*format_chars
== '%')
3277 /* Diagnose %<%s%> and suggest using %qs instead. */
3278 if (startswith (format_chars
, "%<%s%>"))
3279 format_warning_substr (format_string_loc
, format_string_cst
,
3280 fmtchrpos
, fmtchrpos
+ 6, opt
,
3281 "quoted %qs directive in format; "
3282 "use %qs instead", "%s", "%qs");
3283 else if (format_chars
- orig_format_chars
> 2
3284 && !strncasecmp (format_chars
- 3, "can%'t", 6))
3285 format_warning_substr (format_string_loc
,
3287 fmtchrpos
- 3, fmtchrpos
+ 3, opt
,
3288 "contraction %<%.*s%> in format; "
3290 6, format_chars
- 3, "cannot");
3292 return format_chars
;
3295 if (baltoks
.quotdirs
.length ())
3297 /* Skip over all plain text within a quoting directive until
3298 the next directive. */
3299 while (*format_chars
&& '%' != *format_chars
)
3302 return format_chars
;
3305 /* The length of the problem sequence. */
3308 /* Diagnose any whitespace characters other than <space> but only
3309 leading, trailing, and two or more consecutive <space>s. Do
3310 this before diagnosing control characters because whitespace
3311 is a subset of controls. */
3312 const char *other_than_space
= NULL
;
3313 while (ISSPACE (format_chars
[nchars
]))
3315 if (format_chars
[nchars
] != ' ' && !other_than_space
)
3316 other_than_space
= format_chars
+ nchars
;
3322 /* This is the most common problem: go the extra mile to describe
3323 the problem in as much helpful detail as possible. */
3324 if (other_than_space
)
3326 format_warning_substr (format_string_loc
, format_string_cst
,
3327 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3328 "unquoted whitespace character %qc in format",
3330 return format_chars
+ nchars
- 1;
3334 /* Accept strings of leading spaces with no warning. */
3335 return format_chars
+ nchars
- 1;
3337 if (!format_chars
[nchars
])
3339 format_warning_substr (format_string_loc
, format_string_cst
,
3340 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3341 "spurious trailing space in format");
3342 return format_chars
+ nchars
- 1;
3348 && orig_format_chars
< format_chars
3349 && format_chars
[-1] == '.'
3350 && format_chars
[0] == ' '
3351 && format_chars
[1] == ' ')
3353 /* A period followed by two spaces. */
3354 if (ISUPPER (*orig_format_chars
))
3356 /* If the part before the period is a capitalized
3357 sentence check to make sure that what follows
3358 is also capitalized. */
3359 if (ISLOWER (format_chars
[2]))
3360 format_warning_substr (format_string_loc
, format_string_cst
,
3361 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3362 "inconsistent capitalization in "
3367 format_warning_substr (format_string_loc
, format_string_cst
,
3368 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3369 "unquoted sequence of %i consecutive "
3370 "space characters in format", nchars
);
3371 return format_chars
+ nchars
- 1;
3374 format_chars
+= nchars
;
3378 fmtchrpos
= format_chars
- orig_format_chars
;
3380 /* Diagnose any unquoted control characters other than the terminating
3382 while (format_chars
[nchars
] && ISCNTRL (format_chars
[nchars
]))
3387 format_warning_substr (format_string_loc
, format_string_cst
,
3388 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3389 "unquoted control characters in format");
3390 return format_chars
+ nchars
- 1;
3394 format_warning_substr (format_string_loc
, format_string_cst
,
3395 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3396 "unquoted control character %qc in format",
3398 return format_chars
+ nchars
- 1;
3401 if (ISPUNCT (format_chars
[0]))
3403 size_t nelts
= ARRAY_SIZE (c_opers
);
3404 if (const char *ret
= check_tokens (c_opers
, nelts
,
3405 format_string_loc
, format_string_cst
,
3406 orig_format_chars
, format_chars
,
3410 nelts
= c_dialect_cxx () ? ARRAY_SIZE (cxx_opers
) : 0;
3411 if (const char *ret
= check_tokens (cxx_opers
, nelts
,
3412 format_string_loc
, format_string_cst
,
3413 orig_format_chars
, format_chars
,
3418 if (ISALPHA (format_chars
[0]))
3420 size_t nelts
= ARRAY_SIZE (c_keywords
);
3421 if (const char *ret
= check_tokens (c_keywords
, nelts
,
3422 format_string_loc
, format_string_cst
,
3423 orig_format_chars
, format_chars
,
3427 nelts
= c_dialect_cxx () ? ARRAY_SIZE (cxx_keywords
) : 0;
3428 if (const char *ret
= check_tokens (cxx_keywords
, nelts
,
3429 format_string_loc
, format_string_cst
,
3430 orig_format_chars
, format_chars
,
3437 /* Diagnose unquoted options. */
3438 if ((format_chars
== orig_format_chars
3439 || format_chars
[-1] == ' ')
3440 && format_chars
[0] == '-'
3441 && ((format_chars
[1] == '-'
3442 && ISALPHA (format_chars
[2]))
3443 || ISALPHA (format_chars
[1])))
3446 while (ISALNUM (format_chars
[nchars
])
3447 || '_' == format_chars
[nchars
]
3448 || '-' == format_chars
[nchars
]
3449 || '+' == format_chars
[nchars
])
3452 format_warning_substr (format_string_loc
, format_string_cst
,
3453 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3454 "unquoted option name %<%.*s%> in format",
3455 nchars
, format_chars
);
3456 return format_chars
+ nchars
- 1;
3459 /* Diagnose leading, trailing, and two or more consecutive punctuation
3461 const char *unbalanced
= NULL
;
3462 while ('%' != format_chars
[nchars
]
3463 && ISPUNCT (format_chars
[nchars
])
3466 switch (format_chars
[nchars
])
3469 baltoks
.brackets
.safe_push (format_chars
+ nchars
);
3472 baltoks
.curly
.safe_push (format_chars
+ nchars
);
3475 baltoks
.parens
.safe_push (format_chars
+ nchars
);
3478 baltoks
.pointy
.safe_push (format_chars
+ nchars
);
3482 if (baltoks
.brackets
.length () > 0)
3483 baltoks
.brackets
.pop ();
3485 unbalanced
= format_chars
+ nchars
;
3488 if (baltoks
.curly
.length () > 0)
3489 baltoks
.curly
.pop ();
3491 unbalanced
= format_chars
+ nchars
;
3494 if (baltoks
.parens
.length () > 0)
3495 baltoks
.parens
.pop ();
3497 unbalanced
= format_chars
+ nchars
;
3500 if (baltoks
.pointy
.length () > 0)
3501 baltoks
.pointy
.pop ();
3503 unbalanced
= format_chars
+ nchars
;
3512 format_warning_substr (format_string_loc
, format_string_cst
,
3513 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3514 "unbalanced punctuation character %qc in format",
3516 return format_chars
+ nchars
- 1;
3521 /* Consider any identifier that follows the pound ('#') sign
3522 a preprocessing directive. */
3524 && format_chars
[0] == '#'
3525 && ISALPHA (format_chars
[1]))
3527 while (ISALNUM (format_chars
[nchars
])
3528 || format_chars
[nchars
] == '_')
3531 format_warning_substr (format_string_loc
, format_string_cst
,
3532 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3533 "unquoted preprocessing directive %<%.*s%> "
3534 "in format", nchars
, format_chars
);
3535 return format_chars
+ nchars
- 1;
3538 /* Diagnose a bare single quote. */
3540 && format_chars
[0] == '\''
3541 && format_chars
- orig_format_chars
3542 && ISALPHA (format_chars
[-1])
3543 && ISALPHA (format_chars
[1]))
3545 /* Diagnose a subset of contractions that are best avoided. */
3546 for (unsigned i
= 0; i
!= ARRAY_SIZE (contrs
); ++i
)
3548 const char *apos
= strchr (contrs
[i
].name
, '\'');
3549 gcc_assert (apos
!= NULL
);
3550 int off
= apos
- contrs
[i
].name
;
3552 if (format_chars
- orig_format_chars
>= off
3553 && !strncmp (format_chars
- off
,
3554 contrs
[i
].name
, contrs
[i
].len
))
3556 format_warning_substr (format_string_loc
,
3558 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3559 "contraction %<%.*s%> in format; "
3561 contrs
[i
].len
, contrs
[i
].name
,
3563 return format_chars
+ nchars
- 1;
3567 if (format_warning_substr (format_string_loc
, format_string_cst
,
3568 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3569 "bare apostrophe %<'%> in format"))
3570 inform (format_string_loc
,
3571 "if avoiding the apostrophe is not feasible, enclose "
3572 "it in a pair of %qs and %qs directives instead",
3574 return format_chars
+ nchars
- 1;
3577 /* Diagnose a backtick (grave accent). */
3579 && format_chars
[0] == '`')
3581 if (format_warning_substr (format_string_loc
, format_string_cst
,
3582 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3583 "grave accent %<`%> in format"))
3584 inform (format_string_loc
,
3585 "use the apostrophe directive %qs instead", "%'");
3586 return format_chars
+ nchars
- 1;
3589 /* Diagnose a punctuation character after a space. */
3591 && format_chars
- orig_format_chars
3592 && format_chars
[-1] == ' '
3593 && strspn (format_chars
, "!?:;.,") == 1)
3595 format_warning_substr (format_string_loc
, format_string_cst
,
3596 fmtchrpos
- 1, fmtchrpos
, opt
,
3597 "space followed by punctuation character "
3598 "%<%c%>", format_chars
[0]);
3599 return format_chars
;
3604 if (startswith (format_chars
, "\"%s\""))
3606 if (format_warning_substr (format_string_loc
, format_string_cst
,
3607 fmtchrpos
, fmtchrpos
+ 4, opt
,
3608 "quoted %qs directive in format",
3610 inform (format_string_loc
, "if using %qs is not feasible, "
3611 "use %qs instead", "%qs", "\"%-s\"");
3614 if (format_chars
[0] == '"')
3616 baltoks
.doublequote
= baltoks
.doublequote
? NULL
: format_chars
;
3617 return format_chars
+ nchars
- 1;
3619 if (format_chars
[0] == '\'')
3621 baltoks
.singlequote
= baltoks
.singlequote
? NULL
: format_chars
;
3622 return format_chars
+ nchars
- 1;
3629 && format_chars
[0] == '(')
3630 ; /* Text beginning in an open parenthesis. */
3631 else if (nchars
== 3
3632 && startswith (format_chars
, "...")
3634 ; /* Text beginning in an ellipsis. */
3637 format_warning_substr (format_string_loc
, format_string_cst
,
3638 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3639 "spurious leading punctuation sequence "
3640 "%<%.*s%> in format",
3641 nchars
, format_chars
);
3642 return format_chars
+ nchars
- 1;
3645 else if (!format_chars
[nchars
])
3648 && (format_chars
[nchars
- 1] == ':'
3649 || format_chars
[nchars
- 1] == ')'))
3650 ; /* Text ending in a colon or a closing parenthesis. */
3651 else if (nchars
== 1
3652 && ((ISUPPER (*orig_format_chars
)
3653 && format_chars
[nchars
- 1] == '.')
3654 || strspn (format_chars
+ nchars
- 1, "?])") == 1))
3655 ; /* Capitalized sentence terminated by a single period,
3656 or text ending in a question mark, closing bracket,
3658 else if (nchars
== 2
3659 && format_chars
[0] == '?'
3660 && format_chars
[1] == ')')
3661 ; /* A question mark after a closing parenthetical note. */
3662 else if (nchars
== 2
3663 && format_chars
[0] == ')'
3664 && (format_chars
[1] == '?'
3665 || format_chars
[1] == ';'
3666 || format_chars
[1] == ':'
3667 || (ISUPPER (*orig_format_chars
)
3668 && format_chars
[1] == '.')))
3669 ; /* Closing parenthetical note followed by a question mark,
3670 semicolon, or colon at the end of the string, or by
3671 a period at the end of a capitalized sentence. */
3672 else if (nchars
== 3
3673 && format_chars
- orig_format_chars
> 0
3674 && startswith (format_chars
, "..."))
3675 ; /* Text ending in the ellipsis. */
3677 format_warning_substr (format_string_loc
, format_string_cst
,
3678 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3679 "spurious trailing punctuation sequence "
3680 "%<%.*s%> in format",
3681 nchars
, format_chars
);
3683 return format_chars
+ nchars
- 1;
3685 else if (nchars
== 2
3686 && format_chars
[0] == ')'
3687 && (format_chars
[1] == ':'
3688 || format_chars
[1] == ';'
3689 || format_chars
[1] == ',')
3690 && format_chars
[2] == ' ')
3691 ; /* Closing parenthetical note followed by a colon, semicolon
3692 or a comma followed by a space in the middle of the string. */
3693 else if (nchars
> 1)
3694 format_warning_substr (format_string_loc
, format_string_cst
,
3695 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3696 "unquoted sequence of %i consecutive "
3697 "punctuation characters %q.*s in format",
3698 nchars
, nchars
, format_chars
);
3699 return format_chars
+ nchars
- 1;
3704 /* Finally, diagnose any unquoted non-graph, non-punctuation characters
3705 other than the terminating NUL. */
3706 while (format_chars
[nchars
]
3707 && '%' != format_chars
[nchars
]
3708 && !ISPUNCT (format_chars
[nchars
])
3709 && !ISGRAPH (format_chars
[nchars
]))
3714 format_warning_substr (format_string_loc
, format_string_cst
,
3715 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3716 "unquoted non-graph characters in format");
3717 return format_chars
+ nchars
- 1;
3721 format_warning_substr (format_string_loc
, format_string_cst
,
3722 fmtchrpos
, fmtchrpos
+ nchars
, opt
,
3723 "unquoted non-graph character %qc in format",
3725 return format_chars
+ nchars
- 1;
3728 return format_chars
;
3731 /* Diagnose unbalanced tokens described by BALTOKS in format string
3732 ORIG_FORMAT_CHARS and the corresponding FORMAT_STRING_CST. */
3735 maybe_diag_unbalanced_tokens (location_t format_string_loc
,
3736 const char *orig_format_chars
,
3737 tree format_string_cst
,
3740 const char *unbalanced
= NULL
;
3742 if (baltoks
.brackets
.length ())
3743 unbalanced
= baltoks
.brackets
.pop ();
3744 else if (baltoks
.curly
.length ())
3745 unbalanced
= baltoks
.curly
.pop ();
3746 else if (baltoks
.parens
.length ())
3747 unbalanced
= baltoks
.parens
.pop ();
3748 else if (baltoks
.pointy
.length ())
3749 unbalanced
= baltoks
.pointy
.pop ();
3752 format_warning_at_char (format_string_loc
, format_string_cst
,
3753 unbalanced
- orig_format_chars
+ 1,
3755 "unbalanced punctuation character %<%c%> in format",
3758 if (baltoks
.quotdirs
.length ())
3759 format_warning_at_char (format_string_loc
, format_string_cst
,
3760 baltoks
.quotdirs
.pop () - orig_format_chars
,
3762 "unterminated quoting directive");
3765 = baltoks
.singlequote
? baltoks
.singlequote
: baltoks
.doublequote
;
3768 format_warning_at_char (format_string_loc
, format_string_cst
,
3769 quote
- orig_format_chars
+ 1,
3771 "unterminated quote character %<%c%> in format",
3775 /* Do the main part of checking a call to a format function. FORMAT_CHARS
3776 is the NUL-terminated format string (which at this point may contain
3777 internal NUL characters); FORMAT_LENGTH is its length (excluding the
3778 terminating NUL character). ARG_NUM is one less than the number of
3779 the first format argument to check; PARAMS points to that format
3780 argument in the list of arguments. */
3783 check_format_info_main (format_check_results
*res
,
3784 function_format_info
*info
, const char *format_chars
,
3785 location_t fmt_param_loc
, tree format_string_cst
,
3786 int format_length
, tree params
,
3787 unsigned HOST_WIDE_INT arg_num
,
3788 object_allocator
<format_wanted_type
> &fwt_pool
,
3789 vec
<location_t
> *arglocs
)
3791 const char * const orig_format_chars
= format_chars
;
3792 const tree first_fillin_param
= params
;
3794 const format_kind_info
* const fki
= &format_types
[info
->format_type
];
3795 const format_flag_spec
* const flag_specs
= fki
->flag_specs
;
3796 const location_t format_string_loc
= res
->format_string_loc
;
3798 /* -1 if no conversions taking an operand have been found; 0 if one has
3799 and it didn't use $; 1 if $ formats are in use. */
3800 int has_operand_number
= -1;
3802 /* Vectors of pointers to opening quoting directives (like GCC "%<"),
3803 opening braces, brackets, and parentheses. Used to detect unbalanced
3807 /* Pointers to the most recent color directives (like GCC's "%r or %R").
3808 A starting color directive much be terminated before the end of
3809 the format string. A terminating directive makes no sense without
3810 a prior starting directive. */
3811 const char *color_begin
= NULL
;
3812 const char *color_end
= NULL
;
3814 init_dollar_format_checking (info
->first_arg_num
, first_fillin_param
);
3816 /* In GCC diagnostic functions check plain directives (substrings within
3817 the format string that don't start with %) for quoting and punctuations
3819 bool ck_plain
= (!info
->is_raw
3820 && (info
->format_type
== gcc_diag_format_type
3821 || info
->format_type
== gcc_tdiag_format_type
3822 || info
->format_type
== gcc_cdiag_format_type
3823 || info
->format_type
== gcc_cxxdiag_format_type
));
3825 while (*format_chars
!= 0)
3828 format_chars
= check_plain (format_string_loc
,
3830 orig_format_chars
, format_chars
,
3833 if (*format_chars
== 0 || *format_chars
++ != '%')
3836 if (*format_chars
== 0)
3838 format_warning_at_char (format_string_loc
, format_string_cst
,
3839 format_chars
- orig_format_chars
,
3841 "spurious trailing %<%%%> in format");
3844 if (*format_chars
== '%')
3850 /* ARGUMENT_PARSER ctor takes FORMAT_CHARS by reference and calls
3851 to ARG_PARSER members may modify the variable. */
3852 flag_chars_t flag_chars
;
3853 argument_parser
arg_parser (info
, format_chars
, format_string_cst
,
3854 orig_format_chars
, format_string_loc
,
3855 flag_chars
, has_operand_number
,
3856 first_fillin_param
, fwt_pool
, arglocs
);
3858 if (!arg_parser
.read_any_dollar ())
3861 if (!arg_parser
.read_format_flags ())
3864 /* Read any format width, possibly * or *m$. */
3865 if (!arg_parser
.read_any_format_width (params
, arg_num
))
3868 /* Read any format left precision (must be a number, not *). */
3869 arg_parser
.read_any_format_left_precision ();
3871 /* Read any format precision, possibly * or *m$. */
3872 if (!arg_parser
.read_any_format_precision (params
, arg_num
))
3875 const char *format_start
= format_chars
;
3877 arg_parser
.handle_alloc_chars ();
3879 /* The rest of the conversion specification is the length modifier
3880 (if any), and the conversion specifier, so this is where the
3881 type information starts. If we need to issue a suggestion
3882 about a type mismatch, then we should preserve everything up
3884 const char *type_start
= format_chars
;
3886 /* Read any length modifier, if this kind of format has them. */
3887 const length_modifier len_modifier
3888 = arg_parser
.read_any_length_modifier ();
3890 /* Read any modifier (strftime E/O). */
3891 arg_parser
.read_any_other_modifier ();
3893 char format_char
= *format_chars
;
3894 if (format_char
== 0
3895 || (!(fki
->flags
& (int) FMT_FLAG_FANCY_PERCENT_OK
)
3896 && format_char
== '%'))
3898 format_warning_at_char (format_string_loc
, format_string_cst
,
3899 format_chars
- orig_format_chars
,
3901 "conversion lacks type at end of format");
3906 const format_char_info
* const fci
3907 = arg_parser
.find_format_char_info (format_char
);
3911 flag_chars
.validate (fki
, fci
, flag_specs
, format_chars
,
3913 format_string_loc
, orig_format_chars
, format_char
,
3914 baltoks
.quotdirs
.length () > 0);
3916 const int alloc_flag
= flag_chars
.get_alloc_flag (fki
);
3917 const bool suppressed
= flag_chars
.assignment_suppression_p (fki
);
3919 /* Diagnose nested or unmatched quoting directives such as GCC's
3920 "%<...%<" and "%>...%>". */
3921 bool quot_begin_p
= strchr (fci
->flags2
, '<');
3922 bool quot_end_p
= strchr (fci
->flags2
, '>');
3924 if (quot_begin_p
&& !quot_end_p
)
3926 if (baltoks
.quotdirs
.length ())
3927 format_warning_at_char (format_string_loc
, format_string_cst
,
3928 format_chars
- orig_format_chars
,
3930 "nested quoting directive");
3931 baltoks
.quotdirs
.safe_push (format_chars
);
3933 else if (!quot_begin_p
&& quot_end_p
)
3935 if (baltoks
.quotdirs
.length ())
3936 baltoks
.quotdirs
.pop ();
3938 format_warning_at_char (format_string_loc
, format_string_cst
,
3939 format_chars
- orig_format_chars
,
3941 "unmatched quoting directive");
3944 bool color_begin_p
= strchr (fci
->flags2
, '/');
3947 color_begin
= format_chars
;
3950 else if (strchr (fci
->flags2
, '\\'))
3953 format_warning_at_char (format_string_loc
, format_string_cst
,
3954 format_chars
- orig_format_chars
,
3956 "%qc directive redundant after prior "
3957 "occurence of the same", format_char
);
3958 else if (!color_begin
)
3959 format_warning_at_char (format_string_loc
, format_string_cst
,
3960 format_chars
- orig_format_chars
,
3962 "unmatched color reset directive");
3963 color_end
= format_chars
;
3966 /* Diagnose directives that shouldn't appear in a quoted sequence.
3967 (They are denoted by a double quote in FLAGS2.) */
3968 if (baltoks
.quotdirs
.length ())
3970 if (strchr (fci
->flags2
, '"'))
3971 format_warning_at_char (format_string_loc
, format_string_cst
,
3972 format_chars
- orig_format_chars
,
3974 "%qc conversion used within a quoted "
3979 /* Validate the pairs of flags used. */
3980 arg_parser
.validate_flag_pairs (fci
, format_char
);
3982 arg_parser
.give_y2k_warnings (fci
, format_char
);
3984 arg_parser
.parse_any_scan_set (fci
);
3986 tree wanted_type
= NULL
;
3987 const char *wanted_type_name
= NULL
;
3989 if (!arg_parser
.handle_conversions (fci
, len_modifier
,
3990 wanted_type
, wanted_type_name
,
3996 arg_parser
.main_wanted_type
.next
= NULL
;
3998 /* Finally. . .check type of argument against desired type! */
3999 if (!arg_parser
.check_argument_type (fci
, len_modifier
,
4000 wanted_type
, wanted_type_name
,
4004 format_start
, type_start
,
4010 if (format_chars
- orig_format_chars
!= format_length
)
4011 format_warning_at_char (format_string_loc
, format_string_cst
,
4012 format_chars
+ 1 - orig_format_chars
,
4013 OPT_Wformat_contains_nul
,
4014 "embedded %<\\0%> in format");
4015 if (info
->first_arg_num
!= 0 && params
!= 0
4016 && has_operand_number
<= 0)
4018 res
->number_other
--;
4019 res
->number_extra_args
++;
4021 if (has_operand_number
> 0)
4022 finish_dollar_format_checking (res
, fki
->flags
& (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK
);
4024 maybe_diag_unbalanced_tokens (format_string_loc
, orig_format_chars
,
4025 format_string_cst
, baltoks
);
4027 if (color_begin
&& !color_end
)
4028 format_warning_at_char (format_string_loc
, format_string_cst
,
4029 color_begin
- orig_format_chars
,
4030 OPT_Wformat_
, "unterminated color directive");
4033 /* Check the argument types from a single format conversion (possibly
4034 including width and precision arguments).
4036 FMT_LOC is the location of the format conversion.
4038 TYPES is a singly-linked list expressing the parts of the format
4039 conversion that expect argument types, and the arguments they
4042 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
4043 format string to where type information begins for the conversion
4044 (the length modifier and conversion specifier).
4046 CONVERSION_CHAR is the user-provided conversion specifier.
4050 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4052 then FMT_LOC covers this range:
4054 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4057 and TYPES in this case is a three-entry singly-linked list consisting of:
4058 (1) the check for the field width here:
4059 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4062 (2) the check for the field precision here:
4063 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4066 (3) the check for the length modifier and conversion char here:
4067 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4071 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
4074 0000000000111111111122
4075 0123456789012345678901
4076 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4078 | ` CONVERSION_CHAR: 'd'
4079 type starts here. */
4082 check_format_types (const substring_loc
&fmt_loc
,
4083 format_wanted_type
*types
, const format_kind_info
*fki
,
4084 int offset_to_type_start
,
4085 char conversion_char
,
4086 vec
<location_t
> *arglocs
)
4088 for (; types
!= 0; types
= types
->next
)
4098 wanted_type
= types
->wanted_type
;
4099 arg_num
= types
->arg_num
;
4101 /* The following should not occur here. */
4102 gcc_assert (wanted_type
);
4103 gcc_assert (wanted_type
!= void_type_node
|| types
->pointer_count
);
4105 if (types
->pointer_count
== 0)
4106 wanted_type
= lang_hooks
.types
.type_promotes_to (wanted_type
);
4108 wanted_type
= TYPE_MAIN_VARIANT (wanted_type
);
4110 cur_param
= types
->param
;
4113 format_type_warning (fmt_loc
, UNKNOWN_LOCATION
, types
, wanted_type
,
4114 NULL
, fki
, offset_to_type_start
,
4119 cur_type
= TREE_TYPE (cur_param
);
4120 if (cur_type
== error_mark_node
)
4122 orig_cur_type
= cur_type
;
4125 location_t param_loc
= UNKNOWN_LOCATION
;
4126 if (EXPR_HAS_LOCATION (cur_param
))
4127 param_loc
= EXPR_LOCATION (cur_param
);
4130 /* arg_num is 1-based. */
4131 gcc_assert (types
->arg_num
> 0);
4132 param_loc
= (*arglocs
)[types
->arg_num
- 1];
4135 STRIP_NOPS (cur_param
);
4137 /* Check the types of any additional pointer arguments
4138 that precede the "real" argument. */
4139 for (i
= 0; i
< types
->pointer_count
; ++i
)
4141 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
4143 cur_type
= TREE_TYPE (cur_type
);
4144 if (cur_type
== error_mark_node
)
4147 /* Check for writing through a NULL pointer. */
4148 if (types
->writing_in_flag
4151 && integer_zerop (cur_param
))
4152 warning (OPT_Wformat_
, "writing through null pointer "
4153 "(argument %d)", arg_num
);
4155 /* Check for reading through a NULL pointer. Ignore
4156 printf-family of functions as they are checked for
4157 null arguments by the middle-end. */
4158 if (fki
->conversion_specs
!= print_char_table
4159 && types
->reading_from_flag
4162 && integer_zerop (cur_param
))
4163 warning (OPT_Wformat_
, "reading through null pointer "
4164 "(argument %d)", arg_num
);
4166 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
4167 cur_param
= TREE_OPERAND (cur_param
, 0);
4171 /* See if this is an attempt to write into a const type with
4172 scanf or with printf "%n". Note: the writing in happens
4173 at the first indirection only, if for example
4174 void * const * is passed to scanf %p; passing
4175 const void ** is simply passing an incompatible type. */
4176 if (types
->writing_in_flag
4178 && (TYPE_READONLY (cur_type
)
4180 && (CONSTANT_CLASS_P (cur_param
)
4181 || (DECL_P (cur_param
)
4182 && TREE_READONLY (cur_param
))))))
4183 warning (OPT_Wformat_
, "writing into constant object "
4184 "(argument %d)", arg_num
);
4186 /* If there are extra type qualifiers beyond the first
4187 indirection, then this makes the types technically
4191 && (TYPE_READONLY (cur_type
)
4192 || TYPE_VOLATILE (cur_type
)
4193 || TYPE_ATOMIC (cur_type
)
4194 || TYPE_RESTRICT (cur_type
)))
4195 warning (OPT_Wformat_
, "extra type qualifiers in format "
4196 "argument (argument %d)",
4202 format_type_warning (fmt_loc
, param_loc
,
4203 types
, wanted_type
, orig_cur_type
, fki
,
4204 offset_to_type_start
, conversion_char
);
4209 if (i
< types
->pointer_count
)
4212 cur_type
= TYPE_MAIN_VARIANT (cur_type
);
4214 /* Check whether the argument type is a character type. This leniency
4215 only applies to certain formats, flagged with 'c'. */
4216 if (types
->char_lenient_flag
)
4217 char_type_flag
= (cur_type
== char_type_node
4218 || cur_type
== signed_char_type_node
4219 || cur_type
== unsigned_char_type_node
);
4221 /* Check the type of the "real" argument, if there's a type we want. */
4222 if (lang_hooks
.types_compatible_p (wanted_type
, cur_type
))
4224 /* If we want 'void *', allow any pointer type.
4225 (Anything else would already have got a warning.)
4226 With -Wpedantic, only allow pointers to void and to character
4228 if (wanted_type
== void_type_node
4229 && (!pedantic
|| (i
== 1 && char_type_flag
)))
4231 /* Don't warn about differences merely in signedness, unless
4232 -Wpedantic. With -Wpedantic, warn if the type is a pointer
4233 target and not a character type, and for character types at
4234 a second level of indirection. */
4235 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
4236 && TREE_CODE (cur_type
) == INTEGER_TYPE
4237 && ((!pedantic
&& !warn_format_signedness
)
4238 || (i
== 0 && !warn_format_signedness
)
4239 || (i
== 1 && char_type_flag
))
4240 && (TYPE_UNSIGNED (wanted_type
)
4241 ? wanted_type
== c_common_unsigned_type (cur_type
)
4242 : wanted_type
== c_common_signed_type (cur_type
)))
4244 /* Don't warn about differences merely in signedness if we know
4245 that the current type is integer-promoted and its original type
4246 was unsigned such as that it is in the range of WANTED_TYPE. */
4247 if (TREE_CODE (wanted_type
) == INTEGER_TYPE
4248 && TREE_CODE (cur_type
) == INTEGER_TYPE
4249 && warn_format_signedness
4250 && TYPE_UNSIGNED (wanted_type
)
4251 && cur_param
!= NULL_TREE
4252 && TREE_CODE (cur_param
) == NOP_EXPR
)
4254 tree t
= TREE_TYPE (TREE_OPERAND (cur_param
, 0));
4255 if (TYPE_UNSIGNED (t
)
4256 && cur_type
== lang_hooks
.types
.type_promotes_to (t
))
4259 /* Likewise, "signed char", "unsigned char" and "char" are
4260 equivalent but the above test won't consider them equivalent. */
4261 if (wanted_type
== char_type_node
4262 && (!pedantic
|| i
< 2)
4265 if (types
->scalar_identity_flag
4266 && (TREE_CODE (cur_type
) == TREE_CODE (wanted_type
)
4267 || (INTEGRAL_TYPE_P (cur_type
)
4268 && INTEGRAL_TYPE_P (wanted_type
)))
4269 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
))
4271 /* Now we have a type mismatch. */
4272 format_type_warning (fmt_loc
, param_loc
, types
,
4273 wanted_type
, orig_cur_type
, fki
,
4274 offset_to_type_start
, conversion_char
);
4278 /* Given type TYPE, attempt to dereference the type N times
4279 (e.g. from ("int ***", 2) to "int *")
4281 Return the derefenced type, with any qualifiers
4282 such as "const" stripped from the result, or
4283 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
4286 deref_n_times (tree type
, int n
)
4290 for (int i
= n
; i
> 0; i
--)
4292 if (TREE_CODE (type
) != POINTER_TYPE
)
4294 type
= TREE_TYPE (type
);
4296 /* Strip off any "const" etc. */
4297 return build_qualified_type (type
, 0);
4300 /* Lookup the format code for FORMAT_LEN within FLI,
4301 returning the string code for expressing it, or NULL
4302 if it is not found. */
4305 get_modifier_for_format_len (const format_length_info
*fli
,
4306 enum format_lengths format_len
)
4308 for (; fli
->name
; fli
++)
4310 if (fli
->index
== format_len
)
4312 if (fli
->double_index
== format_len
)
4313 return fli
->double_name
;
4320 namespace selftest
{
4323 test_get_modifier_for_format_len ()
4326 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_h
));
4328 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_hh
));
4330 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_L
));
4332 get_modifier_for_format_len (printf_length_specs
, FMT_LEN_none
));
4335 } // namespace selftest
4337 #endif /* CHECKING_P */
4339 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
4340 format_type_detail using SPEC_TYPE to be offered as a suggestion for
4341 Wformat type errors where the argument has type ARG_TYPE. */
4344 matching_type_p (tree spec_type
, tree arg_type
)
4346 gcc_assert (spec_type
);
4347 gcc_assert (arg_type
);
4349 /* If any of the types requires structural equality, we can't compare
4350 their canonical types. */
4351 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type
)
4352 || TYPE_STRUCTURAL_EQUALITY_P (arg_type
))
4355 spec_type
= TYPE_CANONICAL (spec_type
);
4356 arg_type
= TYPE_CANONICAL (arg_type
);
4358 if (TREE_CODE (spec_type
) == INTEGER_TYPE
4359 && TREE_CODE (arg_type
) == INTEGER_TYPE
4360 && (TYPE_UNSIGNED (spec_type
)
4361 ? spec_type
== c_common_unsigned_type (arg_type
)
4362 : spec_type
== c_common_signed_type (arg_type
)))
4365 return spec_type
== arg_type
;
4368 /* Subroutine of get_format_for_type.
4370 Generate a string containing the length modifier and conversion specifier
4371 that should be used to format arguments of type ARG_TYPE within FKI
4372 (effectively the inverse of the checking code).
4374 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
4375 is required to use it, for correcting bogus length modifiers.
4376 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
4377 that matches ARG_TYPE.
4379 If successful, returns a non-NULL string which should be freed
4381 Otherwise, returns NULL. */
4384 get_format_for_type_1 (const format_kind_info
*fki
, tree arg_type
,
4385 char conversion_char
)
4387 gcc_assert (arg_type
);
4389 const format_char_info
*spec
;
4390 for (spec
= &fki
->conversion_specs
[0];
4394 if (conversion_char
)
4395 if (!strchr (spec
->format_chars
, conversion_char
))
4398 tree effective_arg_type
= deref_n_times (arg_type
,
4399 spec
->pointer_count
);
4400 if (!effective_arg_type
)
4402 for (int i
= 0; i
< FMT_LEN_MAX
; i
++)
4404 const format_type_detail
*ftd
= &spec
->types
[i
];
4405 if (!ftd
->type
|| *ftd
->type
== NULL_TREE
)
4407 if (matching_type_p (*ftd
->type
, effective_arg_type
))
4409 const char *len_modifier
4410 = get_modifier_for_format_len (fki
->length_char_specs
,
4411 (enum format_lengths
)i
);
4415 if (conversion_char
)
4416 /* We found a match, using the given conversion char - the
4417 length modifier was incorrect (or absent).
4418 Provide a suggestion using the conversion char with the
4419 correct length modifier for the type. */
4420 return xasprintf ("%s%c", len_modifier
, conversion_char
);
4422 /* 2nd pass: no match was possible using the user-provided
4423 conversion char, but we do have a match without using it.
4424 Provide a suggestion using the first conversion char
4425 listed for the given type. */
4426 return xasprintf ("%s%c", len_modifier
, spec
->format_chars
[0]);
4434 /* Generate a string containing the length modifier and conversion specifier
4435 that should be used to format arguments of type ARG_TYPE within FKI
4436 (effectively the inverse of the checking code).
4438 If successful, returns a non-NULL string which should be freed
4440 Otherwise, returns NULL. */
4443 get_format_for_type (const format_kind_info
*fki
, tree arg_type
,
4444 char conversion_char
)
4446 gcc_assert (arg_type
);
4447 gcc_assert (conversion_char
);
4449 /* First pass: look for a format_char_info containing CONVERSION_CHAR
4450 If we find one, then presumably the length modifier was incorrect
4452 char *result
= get_format_for_type_1 (fki
, arg_type
, conversion_char
);
4456 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
4457 matching just on the type. */
4458 return get_format_for_type_1 (fki
, arg_type
, '\0');
4461 /* Attempt to get a string for use as a replacement fix-it hint for the
4462 source range in FMT_LOC.
4464 Preserve all of the text within the range of FMT_LOC up to
4465 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
4466 length modifier and conversion specifier for ARG_TYPE, attempting
4467 to keep the user-provided CONVERSION_CHAR if possible.
4469 For example, given a long vs long long mismatch for arg5 here:
4471 000000000111111111122222222223333333333|
4472 123456789012345678901234567890123456789` column numbers
4473 0000000000111111111122|
4474 0123456789012345678901` string offsets
4475 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
4476 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
4478 | ` CONVERSION_CHAR: 'd'
4481 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
4482 STRING_CST), where the user provided:
4484 the result (assuming "long" argument 5) should be:
4487 If successful, returns a non-NULL string which should be freed
4489 Otherwise, returns NULL. */
4492 get_corrected_substring (const substring_loc
&fmt_loc
,
4493 format_wanted_type
*type
, tree arg_type
,
4494 const format_kind_info
*fki
,
4495 int offset_to_type_start
, char conversion_char
)
4497 /* Attempt to provide hints for argument types, but not for field widths
4501 if (type
->kind
!= CF_KIND_FORMAT
)
4504 /* Locate the current code within the source range, rejecting
4505 any awkward cases where the format string occupies more than
4507 Lookup the place where the type starts (including any length
4508 modifiers), getting it as the caret location. */
4509 substring_loc
type_loc (fmt_loc
);
4510 type_loc
.set_caret_index (offset_to_type_start
);
4512 location_t fmt_substring_loc
;
4513 const char *err
= type_loc
.get_location (&fmt_substring_loc
);
4517 source_range fmt_substring_range
4518 = get_range_from_loc (line_table
, fmt_substring_loc
);
4520 expanded_location caret
4521 = expand_location_to_spelling_point (fmt_substring_loc
);
4522 expanded_location start
4523 = expand_location_to_spelling_point (fmt_substring_range
.m_start
);
4524 expanded_location finish
4525 = expand_location_to_spelling_point (fmt_substring_range
.m_finish
);
4526 if (caret
.file
!= start
.file
)
4528 if (start
.file
!= finish
.file
)
4530 if (caret
.line
!= start
.line
)
4532 if (start
.line
!= finish
.line
)
4534 if (start
.column
> caret
.column
)
4536 if (start
.column
> finish
.column
)
4538 if (caret
.column
> finish
.column
)
4541 char_span line
= location_get_source_line (start
.file
, start
.line
);
4545 /* If we got this far, then we have the line containing the
4546 existing conversion specification.
4548 Generate a trimmed copy, containing the prefix part of the conversion
4549 specification, up to the (but not including) the length modifier.
4550 In the above example, this would be "%-+*.*". */
4551 int length_up_to_type
= caret
.column
- start
.column
;
4552 char_span prefix_span
= line
.subspan (start
.column
- 1, length_up_to_type
);
4553 char *prefix
= prefix_span
.xstrdup ();
4555 /* Now attempt to generate a suggestion for the rest of the specification
4556 (length modifier and conversion char), based on ARG_TYPE and
4558 In the above example, this would be "ld". */
4559 char *format_for_type
= get_format_for_type (fki
, arg_type
, conversion_char
);
4560 if (!format_for_type
)
4566 /* Success. Generate the resulting suggestion for the whole range of
4567 FMT_LOC by concatenating the two strings.
4568 In the above example, this would be "%-+*.*ld". */
4569 char *result
= concat (prefix
, format_for_type
, NULL
);
4570 free (format_for_type
);
4575 /* Helper class for adding zero or more trailing '*' to types.
4577 The format type and name exclude any '*' for pointers, so those
4578 must be formatted manually. For all the types we currently have,
4579 this is adequate, but formats taking pointers to functions or
4580 arrays would require the full type to be built up in order to
4581 print it with %T. */
4583 class indirection_suffix
4586 indirection_suffix (int pointer_count
) : m_pointer_count (pointer_count
) {}
4588 /* Determine the size of the buffer (including NUL-terminator). */
4590 size_t get_buffer_size () const
4592 return m_pointer_count
+ 2;
4595 /* Write the '*' to DST and add a NUL-terminator. */
4597 void fill_buffer (char *dst
) const
4599 if (m_pointer_count
== 0)
4601 else if (c_dialect_cxx ())
4603 memset (dst
, '*', m_pointer_count
);
4604 dst
[m_pointer_count
] = 0;
4609 memset (dst
+ 1, '*', m_pointer_count
);
4610 dst
[m_pointer_count
+ 1] = 0;
4615 int m_pointer_count
;
4618 /* Subclass of range_label for labelling the range in the format string
4619 with the type in question, adding trailing '*' for pointer_count. */
4621 class range_label_for_format_type_mismatch
4622 : public range_label_for_type_mismatch
4625 range_label_for_format_type_mismatch (tree labelled_type
, tree other_type
,
4627 : range_label_for_type_mismatch (labelled_type
, other_type
),
4628 m_pointer_count (pointer_count
)
4632 label_text
get_text (unsigned range_idx
) const final override
4634 label_text text
= range_label_for_type_mismatch::get_text (range_idx
);
4635 if (text
.get () == NULL
)
4638 indirection_suffix
suffix (m_pointer_count
);
4639 char *p
= (char *) alloca (suffix
.get_buffer_size ());
4640 suffix
.fill_buffer (p
);
4642 char *result
= concat (text
.get (), p
, NULL
);
4643 return label_text::take (result
);
4647 int m_pointer_count
;
4650 /* Give a warning about a format argument of different type from that expected.
4651 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
4652 is based on the location of the char at TYPE->offset_loc.
4653 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
4654 if this is unavailable.
4655 WANTED_TYPE is the type the argument should have,
4656 possibly stripped of pointer dereferences. The description (such as "field
4657 precision"), the placement in the format string, a possibly more
4658 friendly name of WANTED_TYPE, and the number of pointer dereferences
4659 are taken from TYPE. ARG_TYPE is the type of the actual argument,
4660 or NULL if it is missing.
4662 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
4663 format string to where type information begins for the conversion
4664 (the length modifier and conversion specifier).
4665 CONVERSION_CHAR is the user-provided conversion specifier.
4667 For example, given a type mismatch for argument 5 here:
4669 00000000011111111112222222222333333333344444444445555555555|
4670 12345678901234567890123456789012345678901234567890123456789` column numbers
4671 0000000000111111111122|
4672 0123456789012345678901` offsets within STRING_CST
4673 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
4674 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
4676 | ` CONVERSION_CHAR: 'd' PARAM_LOC
4679 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
4683 format_type_warning (const substring_loc
&whole_fmt_loc
,
4684 location_t param_loc
,
4685 format_wanted_type
*type
,
4686 tree wanted_type
, tree arg_type
,
4687 const format_kind_info
*fki
,
4688 int offset_to_type_start
,
4689 char conversion_char
)
4691 enum format_specifier_kind kind
= type
->kind
;
4692 const char *wanted_type_name
= type
->wanted_type_name
;
4693 const char *format_start
= type
->format_start
;
4694 int format_length
= type
->format_length
;
4695 int pointer_count
= type
->pointer_count
;
4696 int arg_num
= type
->arg_num
;
4698 /* If ARG_TYPE is a typedef with a misleading name (for example,
4699 size_t but not the standard size_t expected by printf %zu), avoid
4700 printing the typedef name. */
4701 if (wanted_type_name
4703 && TYPE_NAME (arg_type
)
4704 && TREE_CODE (TYPE_NAME (arg_type
)) == TYPE_DECL
4705 && DECL_NAME (TYPE_NAME (arg_type
))
4706 && !strcmp (wanted_type_name
,
4707 lang_hooks
.decl_printable_name (TYPE_NAME (arg_type
), 2)))
4708 arg_type
= TYPE_MAIN_VARIANT (arg_type
);
4710 indirection_suffix
suffix (pointer_count
);
4711 char *p
= (char *) alloca (suffix
.get_buffer_size ());
4712 suffix
.fill_buffer (p
);
4714 /* WHOLE_FMT_LOC has the caret at the end of the range.
4715 Set the caret to be at the offset from TYPE. Subtract one
4716 from the offset for the same reason as in format_warning_at_char. */
4717 substring_loc
fmt_loc (whole_fmt_loc
);
4718 fmt_loc
.set_caret_index (type
->offset_loc
- 1);
4720 range_label_for_format_type_mismatch
fmt_label (wanted_type
, arg_type
,
4722 range_label_for_type_mismatch
param_label (arg_type
, wanted_type
);
4724 /* Get a string for use as a replacement fix-it hint for the range in
4725 fmt_loc, or NULL. */
4726 char *corrected_substring
4727 = get_corrected_substring (fmt_loc
, type
, arg_type
, fki
,
4728 offset_to_type_start
, conversion_char
);
4729 format_string_diagnostic_t
diag (fmt_loc
, &fmt_label
, param_loc
, ¶m_label
,
4730 corrected_substring
);
4731 if (wanted_type_name
)
4736 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
4737 "but argument %d has type %qT",
4738 gettext (kind_descriptions
[kind
]),
4739 (kind
== CF_KIND_FORMAT
? "%" : ""),
4740 format_length
, format_start
,
4741 wanted_type_name
, p
, arg_num
, arg_type
);
4745 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
4746 gettext (kind_descriptions
[kind
]),
4747 (kind
== CF_KIND_FORMAT
? "%" : ""),
4748 format_length
, format_start
, wanted_type_name
, p
);
4755 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
4756 "but argument %d has type %qT",
4757 gettext (kind_descriptions
[kind
]),
4758 (kind
== CF_KIND_FORMAT
? "%" : ""),
4759 format_length
, format_start
,
4760 wanted_type
, p
, arg_num
, arg_type
);
4764 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
4765 gettext (kind_descriptions
[kind
]),
4766 (kind
== CF_KIND_FORMAT
? "%" : ""),
4767 format_length
, format_start
, wanted_type
, p
);
4770 free (corrected_substring
);
4774 /* Given a format_char_info array FCI, and a character C, this function
4775 returns the index into the conversion_specs where that specifier's
4776 data is located. The character must exist. */
4778 find_char_info_specifier_index (const format_char_info
*fci
, int c
)
4782 for (i
= 0; fci
->format_chars
; i
++, fci
++)
4783 if (strchr (fci
->format_chars
, c
))
4786 /* We shouldn't be looking for a non-existent specifier. */
4790 /* Given a format_length_info array FLI, and a character C, this
4791 function returns the index into the conversion_specs where that
4792 modifier's data is located. The character must exist. */
4794 find_length_info_modifier_index (const format_length_info
*fli
, int c
)
4798 for (i
= 0; fli
->name
; i
++, fli
++)
4799 if (strchr (fli
->name
, c
))
4802 /* We shouldn't be looking for a non-existent modifier. */
4806 /* Determine the type of HOST_WIDE_INT in the code being compiled for
4807 use in GCC's __asm_fprintf__ custom format attribute. You must
4808 have set dynamic_format_types before calling this function. */
4810 init_dynamic_asm_fprintf_info (void)
4816 format_length_info
*new_asm_fprintf_length_specs
;
4819 /* Find the underlying type for HOST_WIDE_INT. For the %w
4820 length modifier to work, one must have issued: "typedef
4821 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
4822 prior to using that modifier. */
4823 hwi
= maybe_get_identifier ("__gcc_host_wide_int__");
4826 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4829 hwi
= identifier_global_value (hwi
);
4830 if (!hwi
|| TREE_CODE (hwi
) != TYPE_DECL
)
4832 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4835 hwi
= DECL_ORIGINAL_TYPE (hwi
);
4837 if (hwi
!= long_integer_type_node
&& hwi
!= long_long_integer_type_node
)
4839 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
4840 " or %<long long%>");
4844 /* Create a new (writable) copy of asm_fprintf_length_specs. */
4845 new_asm_fprintf_length_specs
= (format_length_info
*)
4846 xmemdup (asm_fprintf_length_specs
,
4847 sizeof (asm_fprintf_length_specs
),
4848 sizeof (asm_fprintf_length_specs
));
4850 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4851 i
= find_length_info_modifier_index (new_asm_fprintf_length_specs
, 'w');
4852 if (hwi
== long_integer_type_node
)
4853 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_l
;
4854 else if (hwi
== long_long_integer_type_node
)
4855 new_asm_fprintf_length_specs
[i
].index
= FMT_LEN_ll
;
4859 /* Assign the new data for use. */
4860 dynamic_format_types
[asm_fprintf_format_type
].length_char_specs
=
4861 new_asm_fprintf_length_specs
;
4865 static const format_length_info
*
4866 get_init_dynamic_hwi (void)
4869 static format_length_info
*diag_ls
;
4875 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
4876 length modifier to work, one must have issued: "typedef
4877 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
4878 prior to using that modifier. */
4879 if ((hwi
= maybe_get_identifier ("__gcc_host_wide_int__")))
4881 hwi
= identifier_global_value (hwi
);
4884 if (TREE_CODE (hwi
) != TYPE_DECL
)
4886 error ("%<__gcc_host_wide_int__%> is not defined as a type");
4891 hwi
= DECL_ORIGINAL_TYPE (hwi
);
4893 if (hwi
!= long_integer_type_node
4894 && hwi
!= long_long_integer_type_node
)
4896 error ("%<__gcc_host_wide_int__%> is not defined"
4897 " as %<long%> or %<long long%>");
4904 diag_ls
= (format_length_info
*)
4905 xmemdup (gcc_diag_length_specs
,
4906 sizeof (gcc_diag_length_specs
),
4907 sizeof (gcc_diag_length_specs
));
4910 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
4911 i
= find_length_info_modifier_index (diag_ls
, 'w');
4912 if (hwi
== long_integer_type_node
)
4913 diag_ls
[i
].index
= FMT_LEN_l
;
4914 else if (hwi
== long_long_integer_type_node
)
4915 diag_ls
[i
].index
= FMT_LEN_ll
;
4923 /* Determine the type of a "locus" in the code being compiled for use
4924 in GCC's __gcc_gfc__ custom format attribute. You must have set
4925 dynamic_format_types before calling this function. */
4927 init_dynamic_gfc_info (void)
4929 dynamic_format_types
[gcc_gfc_format_type
].length_char_specs
4930 = get_init_dynamic_hwi ();
4934 static format_char_info
*gfc_fci
;
4936 /* For the GCC __gcc_gfc__ custom format specifier to work, one
4937 must have declared 'locus' prior to using this attribute. If
4938 we haven't seen this declarations then you shouldn't use the
4939 specifier requiring that type. */
4940 if ((locus
= maybe_get_identifier ("locus")))
4942 locus
= identifier_global_value (locus
);
4945 if (TREE_CODE (locus
) != TYPE_DECL
4946 || TREE_TYPE (locus
) == error_mark_node
)
4948 error ("%<locus%> is not defined as a type");
4952 locus
= TREE_TYPE (locus
);
4956 /* Assign the new data for use. */
4958 /* Handle the __gcc_gfc__ format specifics. */
4960 dynamic_format_types
[gcc_gfc_format_type
].conversion_specs
=
4961 gfc_fci
= (format_char_info
*)
4962 xmemdup (gcc_gfc_char_table
,
4963 sizeof (gcc_gfc_char_table
),
4964 sizeof (gcc_gfc_char_table
));
4967 const unsigned i
= find_char_info_specifier_index (gfc_fci
, 'L');
4968 gfc_fci
[i
].types
[0].type
= &locus
;
4969 gfc_fci
[i
].pointer_count
= 1;
4974 /* Lookup the type named NAME and return a NAME type if found.
4975 Otherwise, return void_type_node if NAME has not been used yet,
4976 or NULL_TREE if NAME is not a type (issuing an error). */
4979 get_named_type (const char *name
)
4981 if (tree result
= maybe_get_identifier (name
))
4983 result
= identifier_global_tag (result
);
4986 if (TYPE_P (result
))
4988 else if (TREE_CODE (result
) == TYPE_DECL
)
4989 result
= TREE_TYPE (result
);
4992 error ("%qs is not defined as a type", name
);
4999 return void_type_node
;
5002 /* Determine the types of "tree" and "location_t" in the code being
5003 compiled for use in GCC's diagnostic custom format attributes. You
5004 must have set dynamic_format_types before calling this function. */
5006 init_dynamic_diag_info (void)
5008 /* For the GCC-diagnostics custom format specifiers to work, one
5009 must have declared 'tree' and 'location_t' prior to using those
5010 attributes. If we haven't seen these declarations then
5011 the specifiers requiring these types shouldn't be used.
5012 However we don't force a hard ICE because we may see only one
5013 or the other type. */
5014 if (tree loc
= maybe_get_identifier ("location_t"))
5016 loc
= identifier_global_value (loc
);
5017 if (loc
&& TREE_CODE (loc
) != TYPE_DECL
)
5018 error ("%<location_t%> is not defined as a type");
5021 /* Initialize the global tree node type local to this file. */
5022 if (!local_tree_type_node
5023 || local_tree_type_node
== void_type_node
)
5025 /* We need to grab the underlying 'union tree_node' so peek into
5026 an extra type level. */
5027 if ((local_tree_type_node
= maybe_get_identifier ("tree")))
5029 local_tree_type_node
5030 = identifier_global_value (local_tree_type_node
);
5031 if (local_tree_type_node
)
5033 if (TREE_CODE (local_tree_type_node
) != TYPE_DECL
)
5035 error ("%<tree%> is not defined as a type");
5036 local_tree_type_node
= NULL_TREE
;
5038 else if (TREE_CODE (TREE_TYPE (local_tree_type_node
))
5041 error ("%<tree%> is not defined as a pointer type");
5042 local_tree_type_node
= NULL_TREE
;
5045 local_tree_type_node
5046 = TREE_TYPE (TREE_TYPE (local_tree_type_node
));
5050 local_tree_type_node
= void_type_node
;
5053 /* Similar to the above but for gimple*. */
5054 if (!local_gimple_ptr_node
5055 || local_gimple_ptr_node
== void_type_node
)
5056 local_gimple_ptr_node
= get_named_type ("gimple");
5058 /* Similar to the above but for cgraph_node*. */
5059 if (!local_cgraph_node_ptr_node
5060 || local_cgraph_node_ptr_node
== void_type_node
)
5061 local_cgraph_node_ptr_node
= get_named_type ("cgraph_node");
5063 /* Similar to the above but for diagnostic_event_id_t*. */
5064 if (!local_event_ptr_node
5065 || local_event_ptr_node
== void_type_node
)
5066 local_event_ptr_node
= get_named_type ("diagnostic_event_id_t");
5068 /* All the GCC diag formats use the same length specs. */
5069 dynamic_format_types
[gcc_diag_format_type
].length_char_specs
=
5070 dynamic_format_types
[gcc_tdiag_format_type
].length_char_specs
=
5071 dynamic_format_types
[gcc_cdiag_format_type
].length_char_specs
=
5072 dynamic_format_types
[gcc_cxxdiag_format_type
].length_char_specs
=
5073 dynamic_format_types
[gcc_dump_printf_format_type
].length_char_specs
5074 = get_init_dynamic_hwi ();
5076 /* It's safe to "re-initialize these to the same values. */
5077 dynamic_format_types
[gcc_diag_format_type
].conversion_specs
=
5078 gcc_diag_char_table
;
5079 dynamic_format_types
[gcc_tdiag_format_type
].conversion_specs
=
5080 gcc_tdiag_char_table
;
5081 dynamic_format_types
[gcc_cdiag_format_type
].conversion_specs
=
5082 gcc_cdiag_char_table
;
5083 dynamic_format_types
[gcc_cxxdiag_format_type
].conversion_specs
=
5084 gcc_cxxdiag_char_table
;
5085 dynamic_format_types
[gcc_dump_printf_format_type
].conversion_specs
=
5086 gcc_dump_printf_char_table
;
5089 #ifdef TARGET_FORMAT_TYPES
5090 extern const format_kind_info TARGET_FORMAT_TYPES
[];
5093 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5094 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[];
5096 #ifdef TARGET_OVERRIDES_FORMAT_INIT
5097 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
5100 /* Attributes such as "printf" are equivalent to those such as
5101 "gnu_printf" unless this is overridden by a target. */
5102 static const target_ovr_attr gnu_target_overrides_format_attributes
[] =
5104 { "gnu_printf", "printf" },
5105 { "gnu_scanf", "scanf" },
5106 { "gnu_strftime", "strftime" },
5107 { "gnu_strfmon", "strfmon" },
5111 /* Translate to unified attribute name. This is used in decode_format_type and
5112 decode_format_attr. In attr_name the user specified argument is passed. It
5113 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5114 or the attr_name passed to this function, if there is no matching entry. */
5116 convert_format_name_to_system_name (const char *attr_name
)
5120 if (attr_name
== NULL
|| *attr_name
== 0
5121 || startswith (attr_name
, "gcc_"))
5123 #ifdef TARGET_OVERRIDES_FORMAT_INIT
5124 TARGET_OVERRIDES_FORMAT_INIT ();
5127 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
5128 /* Check if format attribute is overridden by target. */
5129 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
> 0)
5131 for (i
= 0; i
< TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
; ++i
)
5133 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
,
5136 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_dst
,
5138 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES
[i
].named_attr_src
;
5142 /* Otherwise default to gnu format. */
5144 gnu_target_overrides_format_attributes
[i
].named_attr_src
!= NULL
;
5147 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_src
,
5150 if (cmp_attribs (gnu_target_overrides_format_attributes
[i
].named_attr_dst
,
5152 return gnu_target_overrides_format_attributes
[i
].named_attr_src
;
5158 /* Handle a "format" attribute; arguments as in
5159 struct attribute_spec.handler. */
5161 handle_format_attribute (tree node
[3], tree atname
, tree args
,
5162 int flags
, bool *no_add_attrs
)
5164 const_tree type
= *node
;
5165 /* NODE[2] may be NULL, and it also may be a PARM_DECL for function
5167 const_tree fndecl
= ((node
[2] && TREE_CODE (node
[2]) == FUNCTION_DECL
)
5168 ? node
[2] : NULL_TREE
);
5169 function_format_info info
;
5171 #ifdef TARGET_FORMAT_TYPES
5172 /* If the target provides additional format types, we need to
5173 add them to FORMAT_TYPES at first use. */
5174 if (!dynamic_format_types
)
5176 dynamic_format_types
= XNEWVEC (format_kind_info
,
5177 n_format_types
+ TARGET_N_FORMAT_TYPES
);
5178 memcpy (dynamic_format_types
, format_types_orig
,
5179 sizeof (format_types_orig
));
5180 memcpy (&dynamic_format_types
[n_format_types
], TARGET_FORMAT_TYPES
,
5181 TARGET_N_FORMAT_TYPES
* sizeof (dynamic_format_types
[0]));
5183 format_types
= dynamic_format_types
;
5184 /* Provide a reference for the first potential external type. */
5185 first_target_format_type
= n_format_types
;
5186 n_format_types
+= TARGET_N_FORMAT_TYPES
;
5190 /* Canonicalize name of format function. */
5191 if (TREE_CODE (TREE_VALUE (args
)) == IDENTIFIER_NODE
)
5192 TREE_VALUE (args
) = canonicalize_attr_name (TREE_VALUE (args
));
5194 if (!decode_format_attr (fndecl
? fndecl
: type
, atname
, args
, &info
,
5195 /* validated_p = */false))
5197 *no_add_attrs
= true;
5201 if (prototype_p (type
))
5203 if (!check_format_string (type
, info
.format_num
, flags
,
5204 no_add_attrs
, info
.format_type
))
5207 if (info
.first_arg_num
!= 0)
5209 unsigned HOST_WIDE_INT arg_num
= 1;
5210 function_args_iterator iter
;
5213 /* Verify that first_arg_num points to the last arg,
5215 FOREACH_FUNCTION_ARGS (type
, arg_type
, iter
)
5218 if (arg_num
!= info
.first_arg_num
)
5220 if (!(flags
& (int) ATTR_FLAG_BUILT_IN
))
5221 error ("argument to be formatted is not %<...%>");
5222 *no_add_attrs
= true;
5228 /* Check if this is a strftime variant. Just for this variant
5229 FMT_FLAG_ARG_CONVERT is not set. */
5230 if ((format_types
[info
.format_type
].flags
& (int) FMT_FLAG_ARG_CONVERT
) == 0
5231 && info
.first_arg_num
!= 0)
5233 error ("strftime formats cannot format arguments");
5234 *no_add_attrs
= true;
5238 /* If this is a custom GCC-internal format type, we have to
5239 initialize certain bits at runtime. */
5240 if (info
.format_type
== asm_fprintf_format_type
5241 || info
.format_type
== gcc_gfc_format_type
5242 || info
.format_type
== gcc_diag_format_type
5243 || info
.format_type
== gcc_tdiag_format_type
5244 || info
.format_type
== gcc_cdiag_format_type
5245 || info
.format_type
== gcc_cxxdiag_format_type
5246 || info
.format_type
== gcc_dump_printf_format_type
)
5248 /* Our first time through, we have to make sure that our
5249 format_type data is allocated dynamically and is modifiable. */
5250 if (!dynamic_format_types
)
5251 format_types
= dynamic_format_types
= (format_kind_info
*)
5252 xmemdup (format_types_orig
, sizeof (format_types_orig
),
5253 sizeof (format_types_orig
));
5255 /* If this is format __asm_fprintf__, we have to initialize
5256 GCC's notion of HOST_WIDE_INT for checking %wd. */
5257 if (info
.format_type
== asm_fprintf_format_type
)
5258 init_dynamic_asm_fprintf_info ();
5259 /* If this is format __gcc_gfc__, we have to initialize GCC's
5260 notion of 'locus' at runtime for %L. */
5261 else if (info
.format_type
== gcc_gfc_format_type
)
5262 init_dynamic_gfc_info ();
5263 /* If this is one of the diagnostic attributes, then we have to
5264 initialize 'location_t' and 'tree' at runtime. */
5265 else if (info
.format_type
== gcc_diag_format_type
5266 || info
.format_type
== gcc_tdiag_format_type
5267 || info
.format_type
== gcc_cdiag_format_type
5268 || info
.format_type
== gcc_cxxdiag_format_type
5269 || info
.format_type
== gcc_dump_printf_format_type
)
5270 init_dynamic_diag_info ();
5280 namespace selftest
{
5282 /* Selftests of location handling. */
5284 /* Get the format_kind_info with the given name. */
5286 static const format_kind_info
*
5287 get_info (const char *name
)
5289 int idx
= decode_format_type (name
);
5290 const format_kind_info
*fki
= &format_types
[idx
];
5291 ASSERT_STREQ (fki
->name
, name
);
5295 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
5296 is EXPECTED_FORMAT. */
5299 assert_format_for_type_streq (const location
&loc
, const format_kind_info
*fki
,
5300 const char *expected_format
, tree type
,
5301 char conversion_char
)
5304 gcc_assert (expected_format
);
5307 char *actual_format
= get_format_for_type (fki
, type
, conversion_char
);
5308 ASSERT_STREQ_AT (loc
, expected_format
, actual_format
);
5309 free (actual_format
);
5312 /* Selftests for get_format_for_type. */
5314 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
5315 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
5316 (TYPE), (CONVERSION_CHAR))
5318 /* Selftest for get_format_for_type for "printf"-style functions. */
5321 test_get_format_for_type_printf ()
5323 const format_kind_info
*fki
= get_info ("gnu_printf");
5324 ASSERT_NE (fki
, NULL
);
5326 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'i');
5327 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'i');
5328 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'o');
5329 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'o');
5330 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'x');
5331 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'x');
5332 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node
, 'X');
5333 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node
, 'X');
5334 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node
, 'd');
5335 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node
, 'i');
5336 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node
, 'o');
5337 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node
, 'x');
5338 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node
, 'X');
5339 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node
, 'd');
5340 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node
, 'i');
5341 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node
, 'o');
5342 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node
, 'x');
5343 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node
, 'X');
5344 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node
, 'd');
5345 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node
, 'i');
5346 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node
, 'x');
5347 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node
, 'o');
5348 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node
, 'x');
5349 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node
, 'd');
5350 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node
, 'i');
5351 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node
, 'o');
5352 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node
, 'x');
5353 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node
), 'i');
5356 /* Selftest for get_format_for_type for "scanf"-style functions. */
5359 test_get_format_for_type_scanf ()
5361 const format_kind_info
*fki
= get_info ("gnu_scanf");
5362 ASSERT_NE (fki
, NULL
);
5363 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node
), 'd');
5364 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node
), 'u');
5365 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
5366 build_pointer_type (long_integer_type_node
), 'd');
5367 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
5368 build_pointer_type (long_unsigned_type_node
), 'u');
5369 ASSERT_FORMAT_FOR_TYPE_STREQ
5370 ("lld", build_pointer_type (long_long_integer_type_node
), 'd');
5371 ASSERT_FORMAT_FOR_TYPE_STREQ
5372 ("llu", build_pointer_type (long_long_unsigned_type_node
), 'u');
5373 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node
), 'e');
5374 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node
), 'e');
5377 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
5379 /* Exercise the type-printing label code, to give some coverage
5380 under "make selftest-valgrind" (in particular, to ensure that
5381 the label-printing machinery doesn't leak). */
5384 test_type_mismatch_range_labels ()
5386 /* Create a tempfile and write some text to it.
5387 ....................0000000001 11111111 12 22222222
5388 ....................1234567890 12345678 90 12345678. */
5389 const char *content
= " printf (\"msg: %i\\n\", msg);\n";
5390 temp_source_file
tmp (SELFTEST_LOCATION
, ".c", content
);
5391 line_table_test ltt
;
5393 linemap_add (line_table
, LC_ENTER
, false, tmp
.get_filename (), 1);
5395 location_t c17
= linemap_position_for_column (line_table
, 17);
5396 ASSERT_EQ (LOCATION_COLUMN (c17
), 17);
5397 location_t c18
= linemap_position_for_column (line_table
, 18);
5398 location_t c24
= linemap_position_for_column (line_table
, 24);
5399 location_t c26
= linemap_position_for_column (line_table
, 26);
5401 /* Don't attempt to run the tests if column data might be unavailable. */
5402 if (c26
> LINE_MAP_MAX_LOCATION_WITH_COLS
)
5405 location_t fmt
= make_location (c18
, c17
, c18
);
5406 ASSERT_EQ (LOCATION_COLUMN (fmt
), 18);
5408 location_t param
= make_location (c24
, c24
, c26
);
5409 ASSERT_EQ (LOCATION_COLUMN (param
), 24);
5411 range_label_for_format_type_mismatch
fmt_label (char_type_node
,
5412 integer_type_node
, 1);
5413 range_label_for_type_mismatch
param_label (integer_type_node
,
5415 gcc_rich_location
richloc (fmt
, &fmt_label
);
5416 richloc
.add_range (param
, SHOW_RANGE_WITHOUT_CARET
, ¶m_label
);
5418 test_diagnostic_context dc
;
5419 diagnostic_show_locus (&dc
, &richloc
, DK_ERROR
);
5420 if (c_dialect_cxx ())
5421 /* "char*", without a space. */
5422 ASSERT_STREQ (" printf (\"msg: %i\\n\", msg);\n"
5426 pp_formatted_text (dc
.printer
));
5428 /* "char *", with a space. */
5429 ASSERT_STREQ (" printf (\"msg: %i\\n\", msg);\n"
5434 pp_formatted_text (dc
.printer
));
5437 /* Run all of the selftests within this file. */
5440 c_format_cc_tests ()
5442 test_get_modifier_for_format_len ();
5443 test_get_format_for_type_printf ();
5444 test_get_format_for_type_scanf ();
5445 test_type_mismatch_range_labels ();
5448 } // namespace selftest
5450 #endif /* CHECKING_P */
5452 #include "gt-c-family-c-format.h"