[AArch64] Avoid GET_MODE_NUNITS in v8.4 support
[official-gcc.git] / gcc / c-family / c-format.c
blob7a69d5a295cf639875258128029170cd00829bf1
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2018 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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "c-target.h"
25 #include "c-common.h"
26 #include "alloc-pool.h"
27 #include "stringpool.h"
28 #include "c-objc.h"
29 #include "intl.h"
30 #include "langhooks.h"
31 #include "c-format.h"
32 #include "diagnostic.h"
33 #include "substring-locations.h"
34 #include "selftest.h"
35 #include "builtins.h"
36 #include "attribs.h"
38 /* Handle attributes associated with format checking. */
40 /* This must be in the same order as format_types, except for
41 format_type_error. Target-specific format types do not have
42 matching enum values. */
43 enum format_type { printf_format_type, asm_fprintf_format_type,
44 gcc_diag_format_type, gcc_tdiag_format_type,
45 gcc_cdiag_format_type,
46 gcc_cxxdiag_format_type, gcc_gfc_format_type,
47 gcc_objc_string_format_type,
48 format_type_error = -1};
50 struct function_format_info
52 int format_type; /* type of format (printf, scanf, etc.) */
53 unsigned HOST_WIDE_INT format_num; /* number of format argument */
54 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
57 /* Initialized in init_dynamic_diag_info. */
58 static GTY(()) tree local_tree_type_node;
59 static GTY(()) tree local_gcall_ptr_node;
60 static GTY(()) tree locus;
62 static bool decode_format_attr (tree, function_format_info *, int);
63 static int decode_format_type (const char *);
65 static bool check_format_string (tree argument,
66 unsigned HOST_WIDE_INT format_num,
67 int flags, bool *no_add_attrs,
68 int expected_format_type);
69 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
70 int validated_p);
71 static const char *convert_format_name_to_system_name (const char *attr_name);
73 static int first_target_format_type;
74 static const char *format_name (int format_num);
75 static int format_flags (int format_num);
77 /* Emit a warning as per format_warning_va, but construct the substring_loc
78 for the character at offset (CHAR_IDX - 1) within a string constant
79 FORMAT_STRING_CST at FMT_STRING_LOC. */
81 ATTRIBUTE_GCC_DIAG (5,6)
82 static bool
83 format_warning_at_char (location_t fmt_string_loc, tree format_string_cst,
84 int char_idx, int opt, const char *gmsgid, ...)
86 va_list ap;
87 va_start (ap, gmsgid);
88 tree string_type = TREE_TYPE (format_string_cst);
90 /* The callers are of the form:
91 format_warning (format_string_loc, format_string_cst,
92 format_chars - orig_format_chars,
93 where format_chars has already been incremented, so that
94 CHAR_IDX is one character beyond where the warning should
95 be emitted. Fix it. */
96 char_idx -= 1;
98 substring_loc fmt_loc (fmt_string_loc, string_type, char_idx, char_idx,
99 char_idx);
100 bool warned = format_warning_va (fmt_loc, UNKNOWN_LOCATION, NULL, opt,
101 gmsgid, &ap);
102 va_end (ap);
104 return warned;
107 /* Check that we have a pointer to a string suitable for use as a format.
108 The default is to check for a char type.
109 For objective-c dialects, this is extended to include references to string
110 objects validated by objc_string_ref_type_p ().
111 Targets may also provide a string object type that can be used within c and
112 c++ and shared with their respective objective-c dialects. In this case the
113 reference to a format string is checked for validity via a hook.
115 The function returns true if strref points to any string type valid for the
116 language dialect and target. */
118 static bool
119 valid_stringptr_type_p (tree strref)
121 return (strref != NULL
122 && TREE_CODE (strref) == POINTER_TYPE
123 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
124 || objc_string_ref_type_p (strref)
125 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
128 /* Handle a "format_arg" attribute; arguments as in
129 struct attribute_spec.handler. */
130 tree
131 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
132 tree args, int flags, bool *no_add_attrs)
134 tree type = *node;
135 tree format_num_expr = TREE_VALUE (args);
136 unsigned HOST_WIDE_INT format_num = 0;
138 if (!get_constant (format_num_expr, &format_num, 0))
140 error ("format string has invalid operand number");
141 *no_add_attrs = true;
142 return NULL_TREE;
145 if (prototype_p (type))
147 /* The format arg can be any string reference valid for the language and
148 target. We cannot be more specific in this case. */
149 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
150 return NULL_TREE;
153 if (!valid_stringptr_type_p (TREE_TYPE (type)))
155 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
156 error ("function does not return string type");
157 *no_add_attrs = true;
158 return NULL_TREE;
161 return NULL_TREE;
164 /* Verify that the format_num argument is actually a string reference suitable,
165 for the language dialect and target (in case the format attribute is in
166 error). When we know the specific reference type expected, this is also
167 checked. */
168 static bool
169 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
170 int flags, bool *no_add_attrs, int expected_format_type)
172 unsigned HOST_WIDE_INT i;
173 bool is_objc_sref, is_target_sref, is_char_ref;
174 tree ref;
175 int fmt_flags;
176 function_args_iterator iter;
178 i = 1;
179 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
181 if (i == format_num)
182 break;
183 i++;
186 if (!ref
187 || !valid_stringptr_type_p (ref))
189 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
190 error ("format string argument is not a string type");
191 *no_add_attrs = true;
192 return false;
195 /* We only know that we want a suitable string reference. */
196 if (expected_format_type < 0)
197 return true;
199 /* Now check that the arg matches the expected type. */
200 is_char_ref =
201 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
203 fmt_flags = format_flags (expected_format_type);
204 is_objc_sref = is_target_sref = false;
205 if (!is_char_ref)
206 is_objc_sref = objc_string_ref_type_p (ref);
208 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
210 if (is_char_ref)
211 return true; /* OK, we expected a char and found one. */
212 else
214 /* We expected a char but found an extended string type. */
215 if (is_objc_sref)
216 error ("found a %qs reference but the format argument should"
217 " be a string", format_name (gcc_objc_string_format_type));
218 else
219 error ("found a %qT but the format argument should be a string",
220 ref);
221 *no_add_attrs = true;
222 return false;
226 /* We expect a string object type as the format arg. */
227 if (is_char_ref)
229 error ("format argument should be a %qs reference but"
230 " a string was found", format_name (expected_format_type));
231 *no_add_attrs = true;
232 return false;
235 /* We will assert that objective-c will support either its own string type
236 or the target-supplied variant. */
237 if (!is_objc_sref)
238 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
240 if (expected_format_type == (int) gcc_objc_string_format_type
241 && (is_objc_sref || is_target_sref))
242 return true;
244 /* We will allow a target string ref to match only itself. */
245 if (first_target_format_type
246 && expected_format_type >= first_target_format_type
247 && is_target_sref)
248 return true;
249 else
251 error ("format argument should be a %qs reference",
252 format_name (expected_format_type));
253 *no_add_attrs = true;
254 return false;
257 gcc_unreachable ();
260 /* Verify EXPR is a constant, and store its value.
261 If validated_p is true there should be no errors.
262 Returns true on success, false otherwise. */
263 static bool
264 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
266 if (!tree_fits_uhwi_p (expr))
268 gcc_assert (!validated_p);
269 return false;
272 *value = TREE_INT_CST_LOW (expr);
274 return true;
277 /* Decode the arguments to a "format" attribute into a
278 function_format_info structure. It is already known that the list
279 is of the right length. If VALIDATED_P is true, then these
280 attributes have already been validated and must not be erroneous;
281 if false, it will give an error message. Returns true if the
282 attributes are successfully decoded, false otherwise. */
284 static bool
285 decode_format_attr (tree args, function_format_info *info, int validated_p)
287 tree format_type_id = TREE_VALUE (args);
288 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
289 tree first_arg_num_expr
290 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
292 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
294 gcc_assert (!validated_p);
295 error ("unrecognized format specifier");
296 return false;
298 else
300 const char *p = IDENTIFIER_POINTER (format_type_id);
302 p = convert_format_name_to_system_name (p);
304 info->format_type = decode_format_type (p);
306 if (!c_dialect_objc ()
307 && info->format_type == gcc_objc_string_format_type)
309 gcc_assert (!validated_p);
310 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
311 format_type_id);
312 info->format_type = format_type_error;
313 return false;
316 if (info->format_type == format_type_error)
318 gcc_assert (!validated_p);
319 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
320 format_type_id);
321 return false;
325 if (!get_constant (format_num_expr, &info->format_num, validated_p))
327 error ("format string has invalid operand number");
328 return false;
331 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
333 error ("%<...%> has invalid operand number");
334 return false;
337 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
339 gcc_assert (!validated_p);
340 error ("format string argument follows the args to be formatted");
341 return false;
344 return true;
347 /* Check a call to a format function against a parameter list. */
349 /* The C standard version C++ is treated as equivalent to
350 or inheriting from, for the purpose of format features supported. */
351 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
352 /* The C standard version we are checking formats against when pedantic. */
353 #define C_STD_VER ((int) (c_dialect_cxx () \
354 ? CPLUSPLUS_STD_VER \
355 : (flag_isoc99 \
356 ? STD_C99 \
357 : (flag_isoc94 ? STD_C94 : STD_C89))))
358 /* The name to give to the standard version we are warning about when
359 pedantic. FEATURE_VER is the version in which the feature warned out
360 appeared, which is higher than C_STD_VER. */
361 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
362 ? (cxx_dialect < cxx11 ? "ISO C++98" \
363 : "ISO C++11") \
364 : ((FEATURE_VER) == STD_EXT \
365 ? "ISO C" \
366 : "ISO C90"))
367 /* Adjust a C standard version, which may be STD_C9L, to account for
368 -Wno-long-long. Returns other standard versions unchanged. */
369 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
370 ? (warn_long_long ? STD_C99 : STD_C89) \
371 : (VER)))
373 /* Enum describing the kind of specifiers present in the format and
374 requiring an argument. */
375 enum format_specifier_kind {
376 CF_KIND_FORMAT,
377 CF_KIND_FIELD_WIDTH,
378 CF_KIND_FIELD_PRECISION
381 static const char *kind_descriptions[] = {
382 N_("format"),
383 N_("field width specifier"),
384 N_("field precision specifier")
387 /* Structure describing details of a type expected in format checking,
388 and the type to check against it. */
389 struct format_wanted_type
391 /* The type wanted. */
392 tree wanted_type;
393 /* The name of this type to use in diagnostics. */
394 const char *wanted_type_name;
395 /* Should be type checked just for scalar width identity. */
396 int scalar_identity_flag;
397 /* The level of indirection through pointers at which this type occurs. */
398 int pointer_count;
399 /* Whether, when pointer_count is 1, to allow any character type when
400 pedantic, rather than just the character or void type specified. */
401 int char_lenient_flag;
402 /* Whether the argument, dereferenced once, is written into and so the
403 argument must not be a pointer to a const-qualified type. */
404 int writing_in_flag;
405 /* Whether the argument, dereferenced once, is read from and so
406 must not be a NULL pointer. */
407 int reading_from_flag;
408 /* The kind of specifier that this type is used for. */
409 enum format_specifier_kind kind;
410 /* The starting character of the specifier. This never includes the
411 initial percent sign. */
412 const char *format_start;
413 /* The length of the specifier. */
414 int format_length;
415 /* The actual parameter to check against the wanted type. */
416 tree param;
417 /* The argument number of that parameter. */
418 int arg_num;
419 /* The offset location of this argument with respect to the format
420 string location. */
421 unsigned int offset_loc;
422 /* The next type to check for this format conversion, or NULL if none. */
423 struct format_wanted_type *next;
426 /* Convenience macro for format_length_info meaning unused. */
427 #define NO_FMT NULL, FMT_LEN_none, STD_C89
429 static const format_length_info printf_length_specs[] =
431 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
432 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
433 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
434 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
435 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
436 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
437 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
438 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
439 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
440 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
441 { NO_FMT, NO_FMT, 0 }
444 /* Length specifiers valid for asm_fprintf. */
445 static const format_length_info asm_fprintf_length_specs[] =
447 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
448 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
449 { NO_FMT, NO_FMT, 0 }
452 /* Length specifiers valid for GCC diagnostics. */
453 static const format_length_info gcc_diag_length_specs[] =
455 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
456 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
457 { NO_FMT, NO_FMT, 0 }
460 /* The custom diagnostics all accept the same length specifiers. */
461 #define gcc_tdiag_length_specs gcc_diag_length_specs
462 #define gcc_cdiag_length_specs gcc_diag_length_specs
463 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
465 /* This differs from printf_length_specs only in that "Z" is not accepted. */
466 static const format_length_info scanf_length_specs[] =
468 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
469 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
470 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
471 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
472 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
473 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
474 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
475 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
476 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
477 { NO_FMT, NO_FMT, 0 }
481 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
482 make no sense for a format type not part of any C standard version. */
483 static const format_length_info strfmon_length_specs[] =
485 /* A GNU extension. */
486 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
487 { NO_FMT, NO_FMT, 0 }
491 /* For now, the Fortran front-end routines only use l as length modifier. */
492 static const format_length_info gcc_gfc_length_specs[] =
494 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
495 { NO_FMT, NO_FMT, 0 }
499 static const format_flag_spec printf_flag_specs[] =
501 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
502 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
503 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
504 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
505 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
506 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
507 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
508 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
509 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
510 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
511 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
515 static const format_flag_pair printf_flag_pairs[] =
517 { ' ', '+', 1, 0 },
518 { '0', '-', 1, 0 },
519 { '0', 'p', 1, 'i' },
520 { 0, 0, 0, 0 }
523 static const format_flag_spec asm_fprintf_flag_specs[] =
525 { ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
526 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
527 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
528 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
529 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
530 { 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
531 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
532 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
533 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
536 static const format_flag_pair asm_fprintf_flag_pairs[] =
538 { ' ', '+', 1, 0 },
539 { '0', '-', 1, 0 },
540 { '0', 'p', 1, 'i' },
541 { 0, 0, 0, 0 }
544 static const format_flag_pair gcc_diag_flag_pairs[] =
546 { 0, 0, 0, 0 }
549 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
550 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
551 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
552 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
554 static const format_flag_spec gcc_diag_flag_specs[] =
556 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
557 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
558 { 'q', 0, 0, 1, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
559 { 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
560 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
561 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
564 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
565 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
566 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
567 #define gcc_gfc_flag_specs gcc_diag_flag_specs
569 static const format_flag_spec scanf_flag_specs[] =
571 { '*', 0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
572 { 'a', 0, 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
573 { 'm', 0, 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
574 { 'w', 0, 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
575 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
576 { '\'', 0, 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
577 { 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
578 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
582 static const format_flag_pair scanf_flag_pairs[] =
584 { '*', 'L', 0, 0 },
585 { 'a', 'm', 0, 0 },
586 { 0, 0, 0, 0 }
590 static const format_flag_spec strftime_flag_specs[] =
592 { '_', 0, 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
593 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
594 { '0', 0, 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
595 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
596 { '#', 0, 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
597 { 'w', 0, 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
598 { 'E', 0, 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
599 { 'O', 0, 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
600 { 'O', 'o', 0, 0, NULL, N_("the 'O' modifier"), STD_EXT },
601 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
605 static const format_flag_pair strftime_flag_pairs[] =
607 { 'E', 'O', 0, 0 },
608 { '_', '-', 0, 0 },
609 { '_', '0', 0, 0 },
610 { '-', '0', 0, 0 },
611 { '^', '#', 0, 0 },
612 { 0, 0, 0, 0 }
616 static const format_flag_spec strfmon_flag_specs[] =
618 { '=', 0, 1, 0, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
619 { '^', 0, 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
620 { '+', 0, 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
621 { '(', 0, 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
622 { '!', 0, 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
623 { '-', 0, 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
624 { 'w', 0, 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
625 { '#', 0, 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
626 { 'p', 0, 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
627 { 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
628 { 0, 0, 0, 0, NULL, NULL, STD_C89 }
631 static const format_flag_pair strfmon_flag_pairs[] =
633 { '+', '(', 0, 0 },
634 { 0, 0, 0, 0 }
638 static const format_char_info print_char_table[] =
640 /* C89 conversion specifiers. */
641 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "-wp0 +'I", "i", NULL },
642 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
643 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0'I", "i", NULL },
644 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
645 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#I", "", NULL },
646 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
647 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
648 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
649 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
650 /* C99 conversion specifiers. */
651 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
652 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
653 /* X/Open conversion specifiers. */
654 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
655 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
656 /* GNU conversion specifiers. */
657 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
658 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
661 static const format_char_info asm_fprintf_char_table[] =
663 /* C89 conversion specifiers. */
664 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
665 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
666 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
667 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
668 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
670 /* asm_fprintf conversion specifiers. */
671 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
672 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
673 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
674 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
675 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
676 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
677 { "z", 0, STD_C89, NOARGUMENTS, "", "", NULL },
678 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
679 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
682 static const format_char_info gcc_diag_char_table[] =
684 /* C89 conversion specifiers. */
685 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
686 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
687 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
688 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
689 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
690 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
692 /* Custom conversion specifiers. */
694 /* G requires a "gcall*" argument at runtime. */
695 { "G", 1, STD_C89, { T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
696 /* K requires a "tree" argument at runtime. */
697 { "K", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
699 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "//cR", NULL },
700 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL },
701 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL },
702 { "'" , 0, STD_C89, NOARGUMENTS, "", "", NULL },
703 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL },
704 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
705 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
708 static const format_char_info gcc_tdiag_char_table[] =
710 /* C89 conversion specifiers. */
711 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
712 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
713 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
714 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
715 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
716 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
718 /* Custom conversion specifiers. */
720 /* These will require a "tree" at runtime. */
721 { "DFTV", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "'", NULL },
722 { "E", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
723 { "K", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
725 /* G requires a "gcall*" argument at runtime. */
726 { "G", 1, STD_C89, { T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
728 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
730 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "/cR", NULL },
731 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL },
732 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL },
733 { "'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
734 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL },
735 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
736 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_tdiag_char_table[0] },
737 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
740 static const format_char_info gcc_cdiag_char_table[] =
742 /* C89 conversion specifiers. */
743 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
744 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
745 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
746 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
747 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
748 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
750 /* Custom conversion specifiers. */
752 /* These will require a "tree" at runtime. */
753 { "DFTV", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "'", NULL },
754 { "E", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
755 { "K", 1, STD_C89, { T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
757 /* G requires a "gcall*" argument at runtime. */
758 { "G", 1, STD_C89, { T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
760 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
762 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "/cR", NULL },
763 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL },
764 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL },
765 { "'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
766 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL },
767 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
768 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_tdiag_char_table[0] },
769 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
772 static const format_char_info gcc_cxxdiag_char_table[] =
774 /* C89 conversion specifiers. */
775 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
776 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
777 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
778 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
779 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
780 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
782 /* Custom conversion specifiers. */
784 /* These will require a "tree" at runtime. */
785 { "ADFHISTVX",1,STD_C89,{ T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "'", NULL },
786 { "E", 1,STD_C89,{ T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
787 { "K", 1, STD_C89,{ T89_T, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
788 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
790 /* G requires a "gcall*" argument at runtime. */
791 { "G", 1, STD_C89,{ T89_G, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "\"", NULL },
793 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
794 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
796 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "/cR", NULL },
797 { "<", 0, STD_C89, NOARGUMENTS, "", "<", NULL },
798 { ">", 0, STD_C89, NOARGUMENTS, "", ">", NULL },
799 { "'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
800 { "R", 0, STD_C89, NOARGUMENTS, "", "\\", NULL },
801 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
802 { "Z", 1, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", &gcc_tdiag_char_table[0] },
803 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
806 static const format_char_info gcc_gfc_char_table[] =
808 /* C89 conversion specifiers. */
809 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
810 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
811 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
812 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "cR", NULL },
814 /* gfc conversion specifiers. */
816 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
818 /* This will require a "locus" at runtime. */
819 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
821 /* These will require nothing. */
822 { "<>",0, STD_C89, NOARGUMENTS, "", "", NULL },
823 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
826 static const format_char_info scan_char_table[] =
828 /* C89 conversion specifiers. */
829 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
830 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
831 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
832 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
833 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
834 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
835 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
836 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
837 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
838 /* C99 conversion specifiers. */
839 { "F", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
840 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
841 /* X/Open conversion specifiers. */
842 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
843 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
844 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
847 static const format_char_info time_char_table[] =
849 /* C89 conversion specifiers. */
850 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
851 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
852 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
853 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
854 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
855 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
856 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
857 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
858 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
859 /* C99 conversion specifiers. */
860 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
861 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
862 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
863 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
864 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
865 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
866 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
867 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
868 /* GNU conversion specifiers. */
869 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
870 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
871 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
874 static const format_char_info monetary_char_table[] =
876 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
877 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
880 /* This must be in the same order as enum format_type. */
881 static const format_kind_info format_types_orig[] =
883 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
884 printf_flag_specs, printf_flag_pairs,
885 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
886 'w', 0, 'p', 0, 'L', 0,
887 &integer_type_node, &integer_type_node
889 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
890 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
891 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
892 'w', 0, 'p', 0, 'L', 0,
893 NULL, NULL
895 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
896 gcc_diag_flag_specs, gcc_diag_flag_pairs,
897 FMT_FLAG_ARG_CONVERT,
898 0, 0, 'p', 0, 'L', 0,
899 NULL, &integer_type_node
901 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
902 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
903 FMT_FLAG_ARG_CONVERT,
904 0, 0, 'p', 0, 'L', 0,
905 NULL, &integer_type_node
907 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
908 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
909 FMT_FLAG_ARG_CONVERT,
910 0, 0, 'p', 0, 'L', 0,
911 NULL, &integer_type_node
913 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
914 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
915 FMT_FLAG_ARG_CONVERT,
916 0, 0, 'p', 0, 'L', 0,
917 NULL, &integer_type_node
919 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "q+#", NULL,
920 gcc_gfc_flag_specs, gcc_gfc_flag_pairs,
921 FMT_FLAG_ARG_CONVERT,
922 0, 0, 0, 0, 0, 0,
923 NULL, NULL
925 { "NSString", NULL, NULL, NULL, NULL,
926 NULL, NULL,
927 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
928 NULL, NULL
930 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
931 scanf_flag_specs, scanf_flag_pairs,
932 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
933 'w', 0, 0, '*', 'L', 'm',
934 NULL, NULL
936 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
937 strftime_flag_specs, strftime_flag_pairs,
938 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
939 NULL, NULL
941 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
942 strfmon_flag_specs, strfmon_flag_pairs,
943 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
944 NULL, NULL
948 /* This layer of indirection allows GCC to reassign format_types with
949 new data if necessary, while still allowing the original data to be
950 const. */
951 static const format_kind_info *format_types = format_types_orig;
952 /* We can modify this one. We also add target-specific format types
953 to the end of the array. */
954 static format_kind_info *dynamic_format_types;
956 static int n_format_types = ARRAY_SIZE (format_types_orig);
958 /* Structure detailing the results of checking a format function call
959 where the format expression may be a conditional expression with
960 many leaves resulting from nested conditional expressions. */
961 struct format_check_results
963 /* Number of leaves of the format argument that could not be checked
964 as they were not string literals. */
965 int number_non_literal;
966 /* Number of leaves of the format argument that were null pointers or
967 string literals, but had extra format arguments. */
968 int number_extra_args;
969 location_t extra_arg_loc;
970 /* Number of leaves of the format argument that were null pointers or
971 string literals, but had extra format arguments and used $ operand
972 numbers. */
973 int number_dollar_extra_args;
974 /* Number of leaves of the format argument that were wide string
975 literals. */
976 int number_wide;
977 /* Number of leaves of the format argument that were empty strings. */
978 int number_empty;
979 /* Number of leaves of the format argument that were unterminated
980 strings. */
981 int number_unterminated;
982 /* Number of leaves of the format argument that were not counted above. */
983 int number_other;
984 /* Location of the format string. */
985 location_t format_string_loc;
988 struct format_check_context
990 format_check_results *res;
991 function_format_info *info;
992 tree params;
993 vec<location_t> *arglocs;
996 /* Return the format name (as specified in the original table) for the format
997 type indicated by format_num. */
998 static const char *
999 format_name (int format_num)
1001 if (format_num >= 0 && format_num < n_format_types)
1002 return format_types[format_num].name;
1003 gcc_unreachable ();
1006 /* Return the format flags (as specified in the original table) for the format
1007 type indicated by format_num. */
1008 static int
1009 format_flags (int format_num)
1011 if (format_num >= 0 && format_num < n_format_types)
1012 return format_types[format_num].flags;
1013 gcc_unreachable ();
1016 static void check_format_info (function_format_info *, tree,
1017 vec<location_t> *);
1018 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
1019 static void check_format_info_main (format_check_results *,
1020 function_format_info *, const char *,
1021 location_t, tree,
1022 int, tree,
1023 unsigned HOST_WIDE_INT,
1024 object_allocator<format_wanted_type> &,
1025 vec<location_t> *);
1027 static void init_dollar_format_checking (int, tree);
1028 static int maybe_read_dollar_number (const char **, int,
1029 tree, tree *, const format_kind_info *);
1030 static bool avoid_dollar_number (const char *);
1031 static void finish_dollar_format_checking (format_check_results *, int);
1033 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
1034 int, const char *);
1036 static void check_format_types (const substring_loc &fmt_loc,
1037 format_wanted_type *,
1038 const format_kind_info *fki,
1039 int offset_to_type_start,
1040 char conversion_char,
1041 vec<location_t> *arglocs);
1042 static void format_type_warning (const substring_loc &fmt_loc,
1043 location_t param_loc,
1044 format_wanted_type *, tree,
1045 tree,
1046 const format_kind_info *fki,
1047 int offset_to_type_start,
1048 char conversion_char);
1050 /* Decode a format type from a string, returning the type, or
1051 format_type_error if not valid, in which case the caller should print an
1052 error message. */
1053 static int
1054 decode_format_type (const char *s)
1056 int i;
1057 int slen;
1059 s = convert_format_name_to_system_name (s);
1060 slen = strlen (s);
1061 for (i = 0; i < n_format_types; i++)
1063 int alen;
1064 if (!strcmp (s, format_types[i].name))
1065 return i;
1066 alen = strlen (format_types[i].name);
1067 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1068 && s[slen - 1] == '_' && s[slen - 2] == '_'
1069 && !strncmp (s + 2, format_types[i].name, alen))
1070 return i;
1072 return format_type_error;
1076 /* Check the argument list of a call to printf, scanf, etc.
1077 ATTRS are the attributes on the function type. There are NARGS argument
1078 values in the array ARGARRAY.
1079 Also, if -Wsuggest-attribute=format,
1080 warn for calls to vprintf or vscanf in functions with no such format
1081 attribute themselves. */
1083 void
1084 check_function_format (tree attrs, int nargs, tree *argarray,
1085 vec<location_t> *arglocs)
1087 tree a;
1089 /* See if this function has any format attributes. */
1090 for (a = attrs; a; a = TREE_CHAIN (a))
1092 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1094 /* Yup; check it. */
1095 function_format_info info;
1096 decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
1097 if (warn_format)
1099 /* FIXME: Rewrite all the internal functions in this file
1100 to use the ARGARRAY directly instead of constructing this
1101 temporary list. */
1102 tree params = NULL_TREE;
1103 int i;
1104 for (i = nargs - 1; i >= 0; i--)
1105 params = tree_cons (NULL_TREE, argarray[i], params);
1106 check_format_info (&info, params, arglocs);
1109 /* Attempt to detect whether the current function might benefit
1110 from the format attribute if the called function is decorated
1111 with it. Avoid using calls with string literal formats for
1112 guidance since those are unlikely to be viable candidates. */
1113 if (warn_suggest_attribute_format && info.first_arg_num == 0
1114 && (format_types[info.format_type].flags
1115 & (int) FMT_FLAG_ARG_CONVERT)
1116 /* c_strlen will fail for a function parameter but succeed
1117 for a literal or constant array. */
1118 && !c_strlen (argarray[info.format_num - 1], 1))
1120 tree c;
1121 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1123 c = TREE_CHAIN (c))
1124 if (is_attribute_p ("format", TREE_PURPOSE (c))
1125 && (decode_format_type (IDENTIFIER_POINTER
1126 (TREE_VALUE (TREE_VALUE (c))))
1127 == info.format_type))
1128 break;
1129 if (c == NULL_TREE)
1131 /* Check if the current function has a parameter to which
1132 the format attribute could be attached; if not, it
1133 can't be a candidate for a format attribute, despite
1134 the vprintf-like or vscanf-like call. */
1135 tree args;
1136 for (args = DECL_ARGUMENTS (current_function_decl);
1137 args != 0;
1138 args = DECL_CHAIN (args))
1140 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1141 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1142 == char_type_node))
1143 break;
1145 if (args != 0)
1146 warning (OPT_Wsuggest_attribute_format, "function %qD "
1147 "might be a candidate for %qs format attribute",
1148 current_function_decl,
1149 format_types[info.format_type].name);
1157 /* Variables used by the checking of $ operand number formats. */
1158 static char *dollar_arguments_used = NULL;
1159 static char *dollar_arguments_pointer_p = NULL;
1160 static int dollar_arguments_alloc = 0;
1161 static int dollar_arguments_count;
1162 static int dollar_first_arg_num;
1163 static int dollar_max_arg_used;
1164 static int dollar_format_warned;
1166 /* Initialize the checking for a format string that may contain $
1167 parameter number specifications; we will need to keep track of whether
1168 each parameter has been used. FIRST_ARG_NUM is the number of the first
1169 argument that is a parameter to the format, or 0 for a vprintf-style
1170 function; PARAMS is the list of arguments starting at this argument. */
1172 static void
1173 init_dollar_format_checking (int first_arg_num, tree params)
1175 tree oparams = params;
1177 dollar_first_arg_num = first_arg_num;
1178 dollar_arguments_count = 0;
1179 dollar_max_arg_used = 0;
1180 dollar_format_warned = 0;
1181 if (first_arg_num > 0)
1183 while (params)
1185 dollar_arguments_count++;
1186 params = TREE_CHAIN (params);
1189 if (dollar_arguments_alloc < dollar_arguments_count)
1191 free (dollar_arguments_used);
1192 free (dollar_arguments_pointer_p);
1193 dollar_arguments_alloc = dollar_arguments_count;
1194 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1195 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1197 if (dollar_arguments_alloc)
1199 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1200 if (first_arg_num > 0)
1202 int i = 0;
1203 params = oparams;
1204 while (params)
1206 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1207 == POINTER_TYPE);
1208 params = TREE_CHAIN (params);
1209 i++;
1216 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1217 is set, it is an error if one is not found; otherwise, it is OK. If
1218 such a number is found, check whether it is within range and mark that
1219 numbered operand as being used for later checking. Returns the operand
1220 number if found and within range, zero if no such number was found and
1221 this is OK, or -1 on error. PARAMS points to the first operand of the
1222 format; PARAM_PTR is made to point to the parameter referred to. If
1223 a $ format is found, *FORMAT is updated to point just after it. */
1225 static int
1226 maybe_read_dollar_number (const char **format,
1227 int dollar_needed, tree params, tree *param_ptr,
1228 const format_kind_info *fki)
1230 int argnum;
1231 int overflow_flag;
1232 const char *fcp = *format;
1233 if (!ISDIGIT (*fcp))
1235 if (dollar_needed)
1237 warning (OPT_Wformat_, "missing $ operand number in format");
1238 return -1;
1240 else
1241 return 0;
1243 argnum = 0;
1244 overflow_flag = 0;
1245 while (ISDIGIT (*fcp))
1247 int nargnum;
1248 nargnum = 10 * argnum + (*fcp - '0');
1249 if (nargnum < 0 || nargnum / 10 != argnum)
1250 overflow_flag = 1;
1251 argnum = nargnum;
1252 fcp++;
1254 if (*fcp != '$')
1256 if (dollar_needed)
1258 warning (OPT_Wformat_, "missing $ operand number in format");
1259 return -1;
1261 else
1262 return 0;
1264 *format = fcp + 1;
1265 if (pedantic && !dollar_format_warned)
1267 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1268 C_STD_NAME (STD_EXT));
1269 dollar_format_warned = 1;
1271 if (overflow_flag || argnum == 0
1272 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1274 warning (OPT_Wformat_, "operand number out of range in format");
1275 return -1;
1277 if (argnum > dollar_max_arg_used)
1278 dollar_max_arg_used = argnum;
1279 /* For vprintf-style functions we may need to allocate more memory to
1280 track which arguments are used. */
1281 while (dollar_arguments_alloc < dollar_max_arg_used)
1283 int nalloc;
1284 nalloc = 2 * dollar_arguments_alloc + 16;
1285 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1286 nalloc);
1287 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1288 nalloc);
1289 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1290 nalloc - dollar_arguments_alloc);
1291 dollar_arguments_alloc = nalloc;
1293 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1294 && dollar_arguments_used[argnum - 1] == 1)
1296 dollar_arguments_used[argnum - 1] = 2;
1297 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1298 argnum, fki->name);
1300 else
1301 dollar_arguments_used[argnum - 1] = 1;
1302 if (dollar_first_arg_num)
1304 int i;
1305 *param_ptr = params;
1306 for (i = 1; i < argnum && *param_ptr != 0; i++)
1307 *param_ptr = TREE_CHAIN (*param_ptr);
1309 /* This case shouldn't be caught here. */
1310 gcc_assert (*param_ptr);
1312 else
1313 *param_ptr = 0;
1314 return argnum;
1317 /* Ensure that FORMAT does not start with a decimal number followed by
1318 a $; give a diagnostic and return true if it does, false otherwise. */
1320 static bool
1321 avoid_dollar_number (const char *format)
1323 if (!ISDIGIT (*format))
1324 return false;
1325 while (ISDIGIT (*format))
1326 format++;
1327 if (*format == '$')
1329 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1330 return true;
1332 return false;
1336 /* Finish the checking for a format string that used $ operand number formats
1337 instead of non-$ formats. We check for unused operands before used ones
1338 (a serious error, since the implementation of the format function
1339 can't know what types to pass to va_arg to find the later arguments).
1340 and for unused operands at the end of the format (if we know how many
1341 arguments the format had, so not for vprintf). If there were operand
1342 numbers out of range on a non-vprintf-style format, we won't have reached
1343 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1344 pointers. */
1346 static void
1347 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1349 int i;
1350 bool found_pointer_gap = false;
1351 for (i = 0; i < dollar_max_arg_used; i++)
1353 if (!dollar_arguments_used[i])
1355 if (pointer_gap_ok && (dollar_first_arg_num == 0
1356 || dollar_arguments_pointer_p[i]))
1357 found_pointer_gap = true;
1358 else
1359 warning_at (res->format_string_loc, OPT_Wformat_,
1360 "format argument %d unused before used argument %d in $-style format",
1361 i + 1, dollar_max_arg_used);
1364 if (found_pointer_gap
1365 || (dollar_first_arg_num
1366 && dollar_max_arg_used < dollar_arguments_count))
1368 res->number_other--;
1369 res->number_dollar_extra_args++;
1374 /* Retrieve the specification for a format flag. SPEC contains the
1375 specifications for format flags for the applicable kind of format.
1376 FLAG is the flag in question. If PREDICATES is NULL, the basic
1377 spec for that flag must be retrieved and must exist. If
1378 PREDICATES is not NULL, it is a string listing possible predicates
1379 for the spec entry; if an entry predicated on any of these is
1380 found, it is returned, otherwise NULL is returned. */
1382 static const format_flag_spec *
1383 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1385 int i;
1386 for (i = 0; spec[i].flag_char != 0; i++)
1388 if (spec[i].flag_char != flag)
1389 continue;
1390 if (predicates != NULL)
1392 if (spec[i].predicate != 0
1393 && strchr (predicates, spec[i].predicate) != 0)
1394 return &spec[i];
1396 else if (spec[i].predicate == 0)
1397 return &spec[i];
1399 gcc_assert (predicates);
1400 return NULL;
1404 /* Check the argument list of a call to printf, scanf, etc.
1405 INFO points to the function_format_info structure.
1406 PARAMS is the list of argument values. */
1408 static void
1409 check_format_info (function_format_info *info, tree params,
1410 vec<location_t> *arglocs)
1412 format_check_context format_ctx;
1413 unsigned HOST_WIDE_INT arg_num;
1414 tree format_tree;
1415 format_check_results res;
1416 /* Skip to format argument. If the argument isn't available, there's
1417 no work for us to do; prototype checking will catch the problem. */
1418 for (arg_num = 1; ; ++arg_num)
1420 if (params == 0)
1421 return;
1422 if (arg_num == info->format_num)
1423 break;
1424 params = TREE_CHAIN (params);
1426 format_tree = TREE_VALUE (params);
1427 params = TREE_CHAIN (params);
1428 if (format_tree == 0)
1429 return;
1431 res.number_non_literal = 0;
1432 res.number_extra_args = 0;
1433 res.extra_arg_loc = UNKNOWN_LOCATION;
1434 res.number_dollar_extra_args = 0;
1435 res.number_wide = 0;
1436 res.number_empty = 0;
1437 res.number_unterminated = 0;
1438 res.number_other = 0;
1439 res.format_string_loc = input_location;
1441 format_ctx.res = &res;
1442 format_ctx.info = info;
1443 format_ctx.params = params;
1444 format_ctx.arglocs = arglocs;
1446 check_function_arguments_recurse (check_format_arg, &format_ctx,
1447 format_tree, arg_num);
1449 location_t loc = format_ctx.res->format_string_loc;
1451 if (res.number_non_literal > 0)
1453 /* Functions taking a va_list normally pass a non-literal format
1454 string. These functions typically are declared with
1455 first_arg_num == 0, so avoid warning in those cases. */
1456 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1458 /* For strftime-like formats, warn for not checking the format
1459 string; but there are no arguments to check. */
1460 warning_at (loc, OPT_Wformat_nonliteral,
1461 "format not a string literal, format string not checked");
1463 else if (info->first_arg_num != 0)
1465 /* If there are no arguments for the format at all, we may have
1466 printf (foo) which is likely to be a security hole. */
1467 while (arg_num + 1 < info->first_arg_num)
1469 if (params == 0)
1470 break;
1471 params = TREE_CHAIN (params);
1472 ++arg_num;
1474 if (params == 0 && warn_format_security)
1475 warning_at (loc, OPT_Wformat_security,
1476 "format not a string literal and no format arguments");
1477 else if (params == 0 && warn_format_nonliteral)
1478 warning_at (loc, OPT_Wformat_nonliteral,
1479 "format not a string literal and no format arguments");
1480 else
1481 warning_at (loc, OPT_Wformat_nonliteral,
1482 "format not a string literal, argument types not checked");
1486 /* If there were extra arguments to the format, normally warn. However,
1487 the standard does say extra arguments are ignored, so in the specific
1488 case where we have multiple leaves (conditional expressions or
1489 ngettext) allow extra arguments if at least one leaf didn't have extra
1490 arguments, but was otherwise OK (either non-literal or checked OK).
1491 If the format is an empty string, this should be counted similarly to the
1492 case of extra format arguments. */
1493 if (res.number_extra_args > 0 && res.number_non_literal == 0
1494 && res.number_other == 0)
1496 if (res.extra_arg_loc == UNKNOWN_LOCATION)
1497 res.extra_arg_loc = loc;
1498 warning_at (res.extra_arg_loc, OPT_Wformat_extra_args,
1499 "too many arguments for format");
1501 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1502 && res.number_other == 0)
1503 warning_at (loc, OPT_Wformat_extra_args, "unused arguments in $-style format");
1504 if (res.number_empty > 0 && res.number_non_literal == 0
1505 && res.number_other == 0)
1506 warning_at (loc, OPT_Wformat_zero_length, "zero-length %s format string",
1507 format_types[info->format_type].name);
1509 if (res.number_wide > 0)
1510 warning_at (loc, OPT_Wformat_, "format is a wide character string");
1512 if (res.number_unterminated > 0)
1513 warning_at (loc, OPT_Wformat_, "unterminated format string");
1516 /* Callback from check_function_arguments_recurse to check a
1517 format string. FORMAT_TREE is the format parameter. ARG_NUM
1518 is the number of the format argument. CTX points to a
1519 format_check_context. */
1521 static void
1522 check_format_arg (void *ctx, tree format_tree,
1523 unsigned HOST_WIDE_INT arg_num)
1525 format_check_context *format_ctx = (format_check_context *) ctx;
1526 format_check_results *res = format_ctx->res;
1527 function_format_info *info = format_ctx->info;
1528 tree params = format_ctx->params;
1529 vec<location_t> *arglocs = format_ctx->arglocs;
1531 int format_length;
1532 HOST_WIDE_INT offset;
1533 const char *format_chars;
1534 tree array_size = 0;
1535 tree array_init;
1537 location_t fmt_param_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1539 /* Pull out a constant value if the front end didn't, and handle location
1540 wrappers. */
1541 format_tree = fold_for_warn (format_tree);
1542 STRIP_NOPS (format_tree);
1544 if (integer_zerop (format_tree))
1546 /* Skip to first argument to check, so we can see if this format
1547 has any arguments (it shouldn't). */
1548 while (arg_num + 1 < info->first_arg_num)
1550 if (params == 0)
1551 return;
1552 params = TREE_CHAIN (params);
1553 ++arg_num;
1556 if (params == 0)
1557 res->number_other++;
1558 else
1560 if (res->number_extra_args == 0)
1561 res->extra_arg_loc = EXPR_LOC_OR_LOC (TREE_VALUE (params),
1562 input_location);
1563 res->number_extra_args++;
1565 return;
1568 offset = 0;
1569 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1571 tree arg0, arg1;
1573 arg0 = TREE_OPERAND (format_tree, 0);
1574 arg1 = TREE_OPERAND (format_tree, 1);
1575 STRIP_NOPS (arg0);
1576 STRIP_NOPS (arg1);
1577 if (TREE_CODE (arg1) == INTEGER_CST)
1578 format_tree = arg0;
1579 else
1581 res->number_non_literal++;
1582 return;
1584 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1585 if (!cst_and_fits_in_hwi (arg1))
1587 res->number_non_literal++;
1588 return;
1590 offset = int_cst_value (arg1);
1592 if (TREE_CODE (format_tree) != ADDR_EXPR)
1594 res->number_non_literal++;
1595 return;
1597 res->format_string_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1598 format_tree = TREE_OPERAND (format_tree, 0);
1599 if (format_types[info->format_type].flags
1600 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1602 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1603 /* We cannot examine this string here - but we can check that it is
1604 a valid type. */
1605 if (TREE_CODE (format_tree) != CONST_DECL
1606 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1607 || (*targetcm.string_object_ref_type_p)
1608 ((const_tree) TREE_TYPE (format_tree))))
1610 res->number_non_literal++;
1611 return;
1613 /* Skip to first argument to check. */
1614 while (arg_num + 1 < info->first_arg_num)
1616 if (params == 0)
1617 return;
1618 params = TREE_CHAIN (params);
1619 ++arg_num;
1621 /* So, we have a valid literal string object and one or more params.
1622 We need to use an external helper to parse the string into format
1623 info. For Objective-C variants we provide the resource within the
1624 objc tree, for target variants, via a hook. */
1625 if (objc_str)
1626 objc_check_format_arg (format_tree, params);
1627 else if (targetcm.check_string_object_format_arg)
1628 (*targetcm.check_string_object_format_arg) (format_tree, params);
1629 /* Else we can't handle it and retire quietly. */
1630 return;
1632 if (TREE_CODE (format_tree) == ARRAY_REF
1633 && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1634 && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1635 format_tree = TREE_OPERAND (format_tree, 0);
1636 if (offset < 0)
1638 res->number_non_literal++;
1639 return;
1641 if (VAR_P (format_tree)
1642 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1643 && (array_init = decl_constant_value (format_tree)) != format_tree
1644 && TREE_CODE (array_init) == STRING_CST)
1646 /* Extract the string constant initializer. Note that this may include
1647 a trailing NUL character that is not in the array (e.g.
1648 const char a[3] = "foo";). */
1649 array_size = DECL_SIZE_UNIT (format_tree);
1650 format_tree = array_init;
1652 if (TREE_CODE (format_tree) != STRING_CST)
1654 res->number_non_literal++;
1655 return;
1657 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1659 res->number_wide++;
1660 return;
1662 format_chars = TREE_STRING_POINTER (format_tree);
1663 format_length = TREE_STRING_LENGTH (format_tree);
1664 if (array_size != 0)
1666 /* Variable length arrays can't be initialized. */
1667 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1669 if (tree_fits_shwi_p (array_size))
1671 HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1672 if (array_size_value > 0
1673 && array_size_value == (int) array_size_value
1674 && format_length > array_size_value)
1675 format_length = array_size_value;
1678 if (offset)
1680 if (offset >= format_length)
1682 res->number_non_literal++;
1683 return;
1685 format_chars += offset;
1686 format_length -= offset;
1688 if (format_length < 1 || format_chars[--format_length] != 0)
1690 res->number_unterminated++;
1691 return;
1693 if (format_length == 0)
1695 res->number_empty++;
1696 return;
1699 /* Skip to first argument to check. */
1700 while (arg_num + 1 < info->first_arg_num)
1702 if (params == 0)
1703 return;
1704 params = TREE_CHAIN (params);
1705 ++arg_num;
1707 /* Provisionally increment res->number_other; check_format_info_main
1708 will decrement it if it finds there are extra arguments, but this way
1709 need not adjust it for every return. */
1710 res->number_other++;
1711 object_allocator <format_wanted_type> fwt_pool ("format_wanted_type pool");
1712 check_format_info_main (res, info, format_chars, fmt_param_loc, format_tree,
1713 format_length, params, arg_num, fwt_pool, arglocs);
1716 /* Support class for argument_parser and check_format_info_main.
1717 Tracks any flag characters that have been applied to the
1718 current argument. */
1720 class flag_chars_t
1722 public:
1723 flag_chars_t ();
1724 bool has_char_p (char ch) const;
1725 void add_char (char ch);
1726 void validate (const format_kind_info *fki,
1727 const format_char_info *fci,
1728 const format_flag_spec *flag_specs,
1729 const char * const format_chars,
1730 tree format_string_cst,
1731 location_t format_string_loc,
1732 const char * const orig_format_chars,
1733 char format_char,
1734 bool quoted);
1735 int get_alloc_flag (const format_kind_info *fki);
1736 int assignment_suppression_p (const format_kind_info *fki);
1738 private:
1739 char m_flag_chars[256];
1742 /* Support struct for argument_parser and check_format_info_main.
1743 Encapsulates any length modifier applied to the current argument. */
1745 struct length_modifier
1747 length_modifier ()
1748 : chars (NULL), val (FMT_LEN_none), std (STD_C89),
1749 scalar_identity_flag (0)
1753 length_modifier (const char *chars_,
1754 enum format_lengths val_,
1755 enum format_std_version std_,
1756 int scalar_identity_flag_)
1757 : chars (chars_), val (val_), std (std_),
1758 scalar_identity_flag (scalar_identity_flag_)
1762 const char *chars;
1763 enum format_lengths val;
1764 enum format_std_version std;
1765 int scalar_identity_flag;
1768 /* Parsing one argument within a format string. */
1770 class argument_parser
1772 public:
1773 argument_parser (function_format_info *info, const char *&format_chars,
1774 tree format_string_cst,
1775 const char * const orig_format_chars,
1776 location_t format_string_loc, flag_chars_t &flag_chars,
1777 int &has_operand_number, tree first_fillin_param,
1778 object_allocator <format_wanted_type> &fwt_pool_,
1779 vec<location_t> *arglocs);
1781 bool read_any_dollar ();
1783 bool read_format_flags ();
1785 bool
1786 read_any_format_width (tree &params,
1787 unsigned HOST_WIDE_INT &arg_num);
1789 void
1790 read_any_format_left_precision ();
1792 bool
1793 read_any_format_precision (tree &params,
1794 unsigned HOST_WIDE_INT &arg_num);
1796 void handle_alloc_chars ();
1798 length_modifier read_any_length_modifier ();
1800 void read_any_other_modifier ();
1802 const format_char_info *find_format_char_info (char format_char);
1804 void
1805 validate_flag_pairs (const format_char_info *fci,
1806 char format_char);
1808 void
1809 give_y2k_warnings (const format_char_info *fci,
1810 char format_char);
1812 void parse_any_scan_set (const format_char_info *fci);
1814 bool handle_conversions (const format_char_info *fci,
1815 const length_modifier &len_modifier,
1816 tree &wanted_type,
1817 const char *&wanted_type_name,
1818 unsigned HOST_WIDE_INT &arg_num,
1819 tree &params,
1820 char format_char);
1822 bool
1823 check_argument_type (const format_char_info *fci,
1824 const length_modifier &len_modifier,
1825 tree &wanted_type,
1826 const char *&wanted_type_name,
1827 const bool suppressed,
1828 unsigned HOST_WIDE_INT &arg_num,
1829 tree &params,
1830 const int alloc_flag,
1831 const char * const format_start,
1832 const char * const type_start,
1833 location_t fmt_param_loc,
1834 char conversion_char);
1836 private:
1837 const function_format_info *const info;
1838 const format_kind_info * const fki;
1839 const format_flag_spec * const flag_specs;
1840 const char *start_of_this_format;
1841 const char *&format_chars;
1842 const tree format_string_cst;
1843 const char * const orig_format_chars;
1844 const location_t format_string_loc;
1845 object_allocator <format_wanted_type> &fwt_pool;
1846 flag_chars_t &flag_chars;
1847 int main_arg_num;
1848 tree main_arg_params;
1849 int &has_operand_number;
1850 const tree first_fillin_param;
1851 format_wanted_type width_wanted_type;
1852 format_wanted_type precision_wanted_type;
1853 public:
1854 format_wanted_type main_wanted_type;
1855 private:
1856 format_wanted_type *first_wanted_type;
1857 format_wanted_type *last_wanted_type;
1858 vec<location_t> *arglocs;
1861 /* flag_chars_t's constructor. */
1863 flag_chars_t::flag_chars_t ()
1865 m_flag_chars[0] = 0;
1868 /* Has CH been seen as a flag within the current argument? */
1870 bool
1871 flag_chars_t::has_char_p (char ch) const
1873 return strchr (m_flag_chars, ch) != 0;
1876 /* Add CH to the flags seen within the current argument. */
1878 void
1879 flag_chars_t::add_char (char ch)
1881 int i = strlen (m_flag_chars);
1882 m_flag_chars[i++] = ch;
1883 m_flag_chars[i] = 0;
1886 /* Validate the individual flags used, removing any that are invalid. */
1888 void
1889 flag_chars_t::validate (const format_kind_info *fki,
1890 const format_char_info *fci,
1891 const format_flag_spec *flag_specs,
1892 const char * const format_chars,
1893 tree format_string_cst,
1894 location_t format_string_loc,
1895 const char * const orig_format_chars,
1896 char format_char,
1897 bool quoted)
1899 int i;
1900 int d = 0;
1901 bool quotflag = false;
1903 for (i = 0; m_flag_chars[i] != 0; i++)
1905 const format_flag_spec *s = get_flag_spec (flag_specs,
1906 m_flag_chars[i], NULL);
1907 m_flag_chars[i - d] = m_flag_chars[i];
1908 if (m_flag_chars[i] == fki->length_code_char)
1909 continue;
1911 /* Remember if a quoting flag is seen. */
1912 quotflag |= s->quoting;
1914 if (strchr (fci->flag_chars, m_flag_chars[i]) == 0)
1916 format_warning_at_char (format_string_loc, format_string_cst,
1917 format_chars - orig_format_chars,
1918 OPT_Wformat_,
1919 "%s used with %<%%%c%> %s format",
1920 _(s->name), format_char, fki->name);
1921 d++;
1922 continue;
1924 if (pedantic)
1926 const format_flag_spec *t;
1927 if (ADJ_STD (s->std) > C_STD_VER)
1928 warning_at (format_string_loc, OPT_Wformat_,
1929 "%s does not support %s",
1930 C_STD_NAME (s->std), _(s->long_name));
1931 t = get_flag_spec (flag_specs, m_flag_chars[i], fci->flags2);
1932 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
1934 const char *long_name = (t->long_name != NULL
1935 ? t->long_name
1936 : s->long_name);
1937 if (ADJ_STD (t->std) > C_STD_VER)
1938 warning_at (format_string_loc, OPT_Wformat_,
1939 "%s does not support %s with"
1940 " the %<%%%c%> %s format",
1941 C_STD_NAME (t->std), _(long_name),
1942 format_char, fki->name);
1946 /* Detect quoting directives used within a quoted sequence, such
1947 as GCC's "%<...%qE". */
1948 if (quoted && s->quoting)
1950 format_warning_at_char (format_string_loc, format_string_cst,
1951 format_chars - orig_format_chars - 1,
1952 OPT_Wformat_,
1953 "%s used within a quoted sequence",
1954 _(s->name));
1957 m_flag_chars[i - d] = 0;
1959 if (!quoted
1960 && !quotflag
1961 && strchr (fci->flags2, '\''))
1963 format_warning_at_char (format_string_loc, format_string_cst,
1964 format_chars - orig_format_chars,
1965 OPT_Wformat_,
1966 "%qc conversion used unquoted",
1967 format_char);
1971 /* Determine if an assignment-allocation has been set, requiring
1972 an extra char ** for writing back a dynamically-allocated char *.
1973 This is for handling the optional 'm' character in scanf. */
1976 flag_chars_t::get_alloc_flag (const format_kind_info *fki)
1978 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1979 && has_char_p ('a'))
1980 return 1;
1981 if (fki->alloc_char && has_char_p (fki->alloc_char))
1982 return 1;
1983 return 0;
1986 /* Determine if an assignment-suppression character was seen.
1987 ('*' in scanf, for discarding the converted input). */
1990 flag_chars_t::assignment_suppression_p (const format_kind_info *fki)
1992 if (fki->suppression_char
1993 && has_char_p (fki->suppression_char))
1994 return 1;
1995 return 0;
1998 /* Constructor for argument_parser. Initialize for parsing one
1999 argument within a format string. */
2001 argument_parser::
2002 argument_parser (function_format_info *info_, const char *&format_chars_,
2003 tree format_string_cst_,
2004 const char * const orig_format_chars_,
2005 location_t format_string_loc_,
2006 flag_chars_t &flag_chars_,
2007 int &has_operand_number_,
2008 tree first_fillin_param_,
2009 object_allocator <format_wanted_type> &fwt_pool_,
2010 vec<location_t> *arglocs_)
2011 : info (info_),
2012 fki (&format_types[info->format_type]),
2013 flag_specs (fki->flag_specs),
2014 start_of_this_format (format_chars_),
2015 format_chars (format_chars_),
2016 format_string_cst (format_string_cst_),
2017 orig_format_chars (orig_format_chars_),
2018 format_string_loc (format_string_loc_),
2019 fwt_pool (fwt_pool_),
2020 flag_chars (flag_chars_),
2021 main_arg_num (0),
2022 main_arg_params (NULL),
2023 has_operand_number (has_operand_number_),
2024 first_fillin_param (first_fillin_param_),
2025 first_wanted_type (NULL),
2026 last_wanted_type (NULL),
2027 arglocs (arglocs_)
2031 /* Handle dollars at the start of format arguments, setting up main_arg_params
2032 and main_arg_num.
2034 Return true if format parsing is to continue, false otherwise. */
2036 bool
2037 argument_parser::read_any_dollar ()
2039 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
2041 /* Possibly read a $ operand number at the start of the format.
2042 If one was previously used, one is required here. If one
2043 is not used here, we can't immediately conclude this is a
2044 format without them, since it could be printf %m or scanf %*. */
2045 int opnum;
2046 opnum = maybe_read_dollar_number (&format_chars, 0,
2047 first_fillin_param,
2048 &main_arg_params, fki);
2049 if (opnum == -1)
2050 return false;
2051 else if (opnum > 0)
2053 has_operand_number = 1;
2054 main_arg_num = opnum + info->first_arg_num - 1;
2057 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
2059 if (avoid_dollar_number (format_chars))
2060 return false;
2062 return true;
2065 /* Read any format flags, but do not yet validate them beyond removing
2066 duplicates, since in general validation depends on the rest of
2067 the format.
2069 Return true if format parsing is to continue, false otherwise. */
2071 bool
2072 argument_parser::read_format_flags ()
2074 while (*format_chars != 0
2075 && strchr (fki->flag_chars, *format_chars) != 0)
2077 const format_flag_spec *s = get_flag_spec (flag_specs,
2078 *format_chars, NULL);
2079 if (flag_chars.has_char_p (*format_chars))
2081 format_warning_at_char (format_string_loc, format_string_cst,
2082 format_chars + 1 - orig_format_chars,
2083 OPT_Wformat_,
2084 "repeated %s in format", _(s->name));
2086 else
2087 flag_chars.add_char (*format_chars);
2089 if (s->skip_next_char)
2091 ++format_chars;
2092 if (*format_chars == 0)
2094 warning_at (format_string_loc, OPT_Wformat_,
2095 "missing fill character at end of strfmon format");
2096 return false;
2099 ++format_chars;
2102 return true;
2105 /* Read any format width, possibly * or *m$.
2107 Return true if format parsing is to continue, false otherwise. */
2109 bool
2110 argument_parser::
2111 read_any_format_width (tree &params,
2112 unsigned HOST_WIDE_INT &arg_num)
2114 if (!fki->width_char)
2115 return true;
2117 if (fki->width_type != NULL && *format_chars == '*')
2119 flag_chars.add_char (fki->width_char);
2120 /* "...a field width...may be indicated by an asterisk.
2121 In this case, an int argument supplies the field width..." */
2122 ++format_chars;
2123 if (has_operand_number != 0)
2125 int opnum;
2126 opnum = maybe_read_dollar_number (&format_chars,
2127 has_operand_number == 1,
2128 first_fillin_param,
2129 &params, fki);
2130 if (opnum == -1)
2131 return false;
2132 else if (opnum > 0)
2134 has_operand_number = 1;
2135 arg_num = opnum + info->first_arg_num - 1;
2137 else
2138 has_operand_number = 0;
2140 else
2142 if (avoid_dollar_number (format_chars))
2143 return false;
2145 if (info->first_arg_num != 0)
2147 tree cur_param;
2148 if (params == 0)
2149 cur_param = NULL;
2150 else
2152 cur_param = TREE_VALUE (params);
2153 if (has_operand_number <= 0)
2155 params = TREE_CHAIN (params);
2156 ++arg_num;
2159 width_wanted_type.wanted_type = *fki->width_type;
2160 width_wanted_type.wanted_type_name = NULL;
2161 width_wanted_type.pointer_count = 0;
2162 width_wanted_type.char_lenient_flag = 0;
2163 width_wanted_type.scalar_identity_flag = 0;
2164 width_wanted_type.writing_in_flag = 0;
2165 width_wanted_type.reading_from_flag = 0;
2166 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
2167 width_wanted_type.format_start = format_chars - 1;
2168 width_wanted_type.format_length = 1;
2169 width_wanted_type.param = cur_param;
2170 width_wanted_type.arg_num = arg_num;
2171 width_wanted_type.offset_loc =
2172 format_chars - orig_format_chars;
2173 width_wanted_type.next = NULL;
2174 if (last_wanted_type != 0)
2175 last_wanted_type->next = &width_wanted_type;
2176 if (first_wanted_type == 0)
2177 first_wanted_type = &width_wanted_type;
2178 last_wanted_type = &width_wanted_type;
2181 else
2183 /* Possibly read a numeric width. If the width is zero,
2184 we complain if appropriate. */
2185 int non_zero_width_char = FALSE;
2186 int found_width = FALSE;
2187 while (ISDIGIT (*format_chars))
2189 found_width = TRUE;
2190 if (*format_chars != '0')
2191 non_zero_width_char = TRUE;
2192 ++format_chars;
2194 if (found_width && !non_zero_width_char &&
2195 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
2196 warning_at (format_string_loc, OPT_Wformat_,
2197 "zero width in %s format", fki->name);
2198 if (found_width)
2199 flag_chars.add_char (fki->width_char);
2202 return true;
2205 /* Read any format left precision (must be a number, not *). */
2206 void
2207 argument_parser::read_any_format_left_precision ()
2209 if (fki->left_precision_char == 0)
2210 return;
2211 if (*format_chars != '#')
2212 return;
2214 ++format_chars;
2215 flag_chars.add_char (fki->left_precision_char);
2216 if (!ISDIGIT (*format_chars))
2217 format_warning_at_char (format_string_loc, format_string_cst,
2218 format_chars - orig_format_chars,
2219 OPT_Wformat_,
2220 "empty left precision in %s format", fki->name);
2221 while (ISDIGIT (*format_chars))
2222 ++format_chars;
2225 /* Read any format precision, possibly * or *m$.
2227 Return true if format parsing is to continue, false otherwise. */
2229 bool
2230 argument_parser::
2231 read_any_format_precision (tree &params,
2232 unsigned HOST_WIDE_INT &arg_num)
2234 if (fki->precision_char == 0)
2235 return true;
2236 if (*format_chars != '.')
2237 return true;
2239 ++format_chars;
2240 flag_chars.add_char (fki->precision_char);
2241 if (fki->precision_type != NULL && *format_chars == '*')
2243 /* "...a...precision...may be indicated by an asterisk.
2244 In this case, an int argument supplies the...precision." */
2245 ++format_chars;
2246 if (has_operand_number != 0)
2248 int opnum;
2249 opnum = maybe_read_dollar_number (&format_chars,
2250 has_operand_number == 1,
2251 first_fillin_param,
2252 &params, fki);
2253 if (opnum == -1)
2254 return false;
2255 else if (opnum > 0)
2257 has_operand_number = 1;
2258 arg_num = opnum + info->first_arg_num - 1;
2260 else
2261 has_operand_number = 0;
2263 else
2265 if (avoid_dollar_number (format_chars))
2266 return false;
2268 if (info->first_arg_num != 0)
2270 tree cur_param;
2271 if (params == 0)
2272 cur_param = NULL;
2273 else
2275 cur_param = TREE_VALUE (params);
2276 if (has_operand_number <= 0)
2278 params = TREE_CHAIN (params);
2279 ++arg_num;
2282 precision_wanted_type.wanted_type = *fki->precision_type;
2283 precision_wanted_type.wanted_type_name = NULL;
2284 precision_wanted_type.pointer_count = 0;
2285 precision_wanted_type.char_lenient_flag = 0;
2286 precision_wanted_type.scalar_identity_flag = 0;
2287 precision_wanted_type.writing_in_flag = 0;
2288 precision_wanted_type.reading_from_flag = 0;
2289 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
2290 precision_wanted_type.param = cur_param;
2291 precision_wanted_type.format_start = format_chars - 2;
2292 precision_wanted_type.format_length = 2;
2293 precision_wanted_type.arg_num = arg_num;
2294 precision_wanted_type.offset_loc =
2295 format_chars - orig_format_chars;
2296 precision_wanted_type.next = NULL;
2297 if (last_wanted_type != 0)
2298 last_wanted_type->next = &precision_wanted_type;
2299 if (first_wanted_type == 0)
2300 first_wanted_type = &precision_wanted_type;
2301 last_wanted_type = &precision_wanted_type;
2304 else
2306 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
2307 && !ISDIGIT (*format_chars))
2308 format_warning_at_char (format_string_loc, format_string_cst,
2309 format_chars - orig_format_chars,
2310 OPT_Wformat_,
2311 "empty precision in %s format", fki->name);
2312 while (ISDIGIT (*format_chars))
2313 ++format_chars;
2316 return true;
2319 /* Parse any assignment-allocation flags, which request an extra
2320 char ** for writing back a dynamically-allocated char *.
2321 This is for handling the optional 'm' character in scanf,
2322 and, before C99, 'a' (for compatibility with a non-standard
2323 GNU libc extension). */
2325 void
2326 argument_parser::handle_alloc_chars ()
2328 if (fki->alloc_char && fki->alloc_char == *format_chars)
2330 flag_chars.add_char (fki->alloc_char);
2331 format_chars++;
2334 /* Handle the scanf allocation kludge. */
2335 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2337 if (*format_chars == 'a' && !flag_isoc99)
2339 if (format_chars[1] == 's' || format_chars[1] == 'S'
2340 || format_chars[1] == '[')
2342 /* 'a' is used as a flag. */
2343 flag_chars.add_char ('a');
2344 format_chars++;
2350 /* Look for length modifiers within the current format argument,
2351 returning a length_modifier instance describing it (or the
2352 default if one is not found).
2354 Issue warnings about non-standard modifiers. */
2356 length_modifier
2357 argument_parser::read_any_length_modifier ()
2359 length_modifier result;
2361 const format_length_info *fli = fki->length_char_specs;
2362 if (!fli)
2363 return result;
2365 while (fli->name != 0
2366 && strncmp (fli->name, format_chars, strlen (fli->name)))
2367 fli++;
2368 if (fli->name != 0)
2370 format_chars += strlen (fli->name);
2371 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2373 format_chars++;
2374 result = length_modifier (fli->double_name, fli->double_index,
2375 fli->double_std, 0);
2377 else
2379 result = length_modifier (fli->name, fli->index, fli->std,
2380 fli->scalar_identity_flag);
2382 flag_chars.add_char (fki->length_code_char);
2384 if (pedantic)
2386 /* Warn if the length modifier is non-standard. */
2387 if (ADJ_STD (result.std) > C_STD_VER)
2388 warning_at (format_string_loc, OPT_Wformat_,
2389 "%s does not support the %qs %s length modifier",
2390 C_STD_NAME (result.std), result.chars,
2391 fki->name);
2394 return result;
2397 /* Read any other modifier (strftime E/O). */
2399 void
2400 argument_parser::read_any_other_modifier ()
2402 if (fki->modifier_chars == NULL)
2403 return;
2405 while (*format_chars != 0
2406 && strchr (fki->modifier_chars, *format_chars) != 0)
2408 if (flag_chars.has_char_p (*format_chars))
2410 const format_flag_spec *s = get_flag_spec (flag_specs,
2411 *format_chars, NULL);
2412 format_warning_at_char (format_string_loc, format_string_cst,
2413 format_chars - orig_format_chars,
2414 OPT_Wformat_,
2415 "repeated %s in format", _(s->name));
2417 else
2418 flag_chars.add_char (*format_chars);
2419 ++format_chars;
2423 /* Return the format_char_info corresponding to FORMAT_CHAR,
2424 potentially issuing a warning if the format char is
2425 not supported in the C standard version we are checking
2426 against.
2428 Issue a warning and return NULL if it is not found.
2430 Issue warnings about non-standard modifiers. */
2432 const format_char_info *
2433 argument_parser::find_format_char_info (char format_char)
2435 const format_char_info *fci = fki->conversion_specs;
2437 while (fci->format_chars != 0
2438 && strchr (fci->format_chars, format_char) == 0)
2439 ++fci;
2440 if (fci->format_chars == 0)
2442 format_warning_at_char (format_string_loc, format_string_cst,
2443 format_chars - orig_format_chars,
2444 OPT_Wformat_,
2445 "unknown conversion type character"
2446 " %qc in format",
2447 format_char);
2448 return NULL;
2451 if (pedantic)
2453 if (ADJ_STD (fci->std) > C_STD_VER)
2454 format_warning_at_char (format_string_loc, format_string_cst,
2455 format_chars - orig_format_chars,
2456 OPT_Wformat_,
2457 "%s does not support the %<%%%c%> %s format",
2458 C_STD_NAME (fci->std), format_char, fki->name);
2461 return fci;
2464 /* Validate the pairs of flags used.
2465 Issue warnings about incompatible combinations of flags. */
2467 void
2468 argument_parser::validate_flag_pairs (const format_char_info *fci,
2469 char format_char)
2471 const format_flag_pair * const bad_flag_pairs = fki->bad_flag_pairs;
2473 for (int i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2475 const format_flag_spec *s, *t;
2476 if (!flag_chars.has_char_p (bad_flag_pairs[i].flag_char1))
2477 continue;
2478 if (!flag_chars.has_char_p (bad_flag_pairs[i].flag_char2))
2479 continue;
2480 if (bad_flag_pairs[i].predicate != 0
2481 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2482 continue;
2483 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2484 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2485 if (bad_flag_pairs[i].ignored)
2487 if (bad_flag_pairs[i].predicate != 0)
2488 warning_at (format_string_loc, OPT_Wformat_,
2489 "%s ignored with %s and %<%%%c%> %s format",
2490 _(s->name), _(t->name), format_char,
2491 fki->name);
2492 else
2493 warning_at (format_string_loc, OPT_Wformat_,
2494 "%s ignored with %s in %s format",
2495 _(s->name), _(t->name), fki->name);
2497 else
2499 if (bad_flag_pairs[i].predicate != 0)
2500 warning_at (format_string_loc, OPT_Wformat_,
2501 "use of %s and %s together with %<%%%c%> %s format",
2502 _(s->name), _(t->name), format_char,
2503 fki->name);
2504 else
2505 warning_at (format_string_loc, OPT_Wformat_,
2506 "use of %s and %s together in %s format",
2507 _(s->name), _(t->name), fki->name);
2512 /* Give Y2K warnings. */
2514 void
2515 argument_parser::give_y2k_warnings (const format_char_info *fci,
2516 char format_char)
2518 if (!warn_format_y2k)
2519 return;
2521 int y2k_level = 0;
2522 if (strchr (fci->flags2, '4') != 0)
2523 if (flag_chars.has_char_p ('E'))
2524 y2k_level = 3;
2525 else
2526 y2k_level = 2;
2527 else if (strchr (fci->flags2, '3') != 0)
2528 y2k_level = 3;
2529 else if (strchr (fci->flags2, '2') != 0)
2530 y2k_level = 2;
2531 if (y2k_level == 3)
2532 warning_at (format_string_loc, OPT_Wformat_y2k,
2533 "%<%%%c%> yields only last 2 digits of "
2534 "year in some locales", format_char);
2535 else if (y2k_level == 2)
2536 warning_at (format_string_loc, OPT_Wformat_y2k,
2537 "%<%%%c%> yields only last 2 digits of year",
2538 format_char);
2541 /* Parse any "scan sets" enclosed in square brackets, e.g.
2542 for scanf-style calls. */
2544 void
2545 argument_parser::parse_any_scan_set (const format_char_info *fci)
2547 if (strchr (fci->flags2, '[') == NULL)
2548 return;
2550 /* Skip over scan set, in case it happens to have '%' in it. */
2551 if (*format_chars == '^')
2552 ++format_chars;
2553 /* Find closing bracket; if one is hit immediately, then
2554 it's part of the scan set rather than a terminator. */
2555 if (*format_chars == ']')
2556 ++format_chars;
2557 while (*format_chars && *format_chars != ']')
2558 ++format_chars;
2559 if (*format_chars != ']')
2560 /* The end of the format string was reached. */
2561 format_warning_at_char (format_string_loc, format_string_cst,
2562 format_chars - orig_format_chars,
2563 OPT_Wformat_,
2564 "no closing %<]%> for %<%%[%> format");
2567 /* Return true if this argument is to be continued to be parsed,
2568 false to skip to next argument. */
2570 bool
2571 argument_parser::handle_conversions (const format_char_info *fci,
2572 const length_modifier &len_modifier,
2573 tree &wanted_type,
2574 const char *&wanted_type_name,
2575 unsigned HOST_WIDE_INT &arg_num,
2576 tree &params,
2577 char format_char)
2579 enum format_std_version wanted_type_std;
2581 if (!(fki->flags & (int) FMT_FLAG_ARG_CONVERT))
2582 return true;
2584 wanted_type = (fci->types[len_modifier.val].type
2585 ? *fci->types[len_modifier.val].type : 0);
2586 wanted_type_name = fci->types[len_modifier.val].name;
2587 wanted_type_std = fci->types[len_modifier.val].std;
2588 if (wanted_type == 0)
2590 format_warning_at_char (format_string_loc, format_string_cst,
2591 format_chars - orig_format_chars,
2592 OPT_Wformat_,
2593 "use of %qs length modifier with %qc type"
2594 " character has either no effect"
2595 " or undefined behavior",
2596 len_modifier.chars, format_char);
2597 /* Heuristic: skip one argument when an invalid length/type
2598 combination is encountered. */
2599 arg_num++;
2600 if (params != 0)
2601 params = TREE_CHAIN (params);
2602 return false;
2604 else if (pedantic
2605 /* Warn if non-standard, provided it is more non-standard
2606 than the length and type characters that may already
2607 have been warned for. */
2608 && ADJ_STD (wanted_type_std) > ADJ_STD (len_modifier.std)
2609 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2611 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2612 format_warning_at_char (format_string_loc, format_string_cst,
2613 format_chars - orig_format_chars,
2614 OPT_Wformat_,
2615 "%s does not support the %<%%%s%c%> %s format",
2616 C_STD_NAME (wanted_type_std),
2617 len_modifier.chars,
2618 format_char, fki->name);
2621 return true;
2624 /* Check type of argument against desired type.
2626 Return true if format parsing is to continue, false otherwise. */
2628 bool
2629 argument_parser::
2630 check_argument_type (const format_char_info *fci,
2631 const length_modifier &len_modifier,
2632 tree &wanted_type,
2633 const char *&wanted_type_name,
2634 const bool suppressed,
2635 unsigned HOST_WIDE_INT &arg_num,
2636 tree &params,
2637 const int alloc_flag,
2638 const char * const format_start,
2639 const char * const type_start,
2640 location_t fmt_param_loc,
2641 char conversion_char)
2643 if (info->first_arg_num == 0)
2644 return true;
2646 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2647 || suppressed)
2649 if (main_arg_num != 0)
2651 if (suppressed)
2652 warning_at (format_string_loc, OPT_Wformat_,
2653 "operand number specified with "
2654 "suppressed assignment");
2655 else
2656 warning_at (format_string_loc, OPT_Wformat_,
2657 "operand number specified for format "
2658 "taking no argument");
2661 else
2663 format_wanted_type *wanted_type_ptr;
2665 if (main_arg_num != 0)
2667 arg_num = main_arg_num;
2668 params = main_arg_params;
2670 else
2672 ++arg_num;
2673 if (has_operand_number > 0)
2675 warning_at (format_string_loc, OPT_Wformat_,
2676 "missing $ operand number in format");
2677 return false;
2679 else
2680 has_operand_number = 0;
2683 wanted_type_ptr = &main_wanted_type;
2684 while (fci)
2686 tree cur_param;
2687 if (params == 0)
2688 cur_param = NULL;
2689 else
2691 cur_param = TREE_VALUE (params);
2692 params = TREE_CHAIN (params);
2695 wanted_type_ptr->wanted_type = wanted_type;
2696 wanted_type_ptr->wanted_type_name = wanted_type_name;
2697 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2698 wanted_type_ptr->char_lenient_flag = 0;
2699 if (strchr (fci->flags2, 'c') != 0)
2700 wanted_type_ptr->char_lenient_flag = 1;
2701 wanted_type_ptr->scalar_identity_flag = 0;
2702 if (len_modifier.scalar_identity_flag)
2703 wanted_type_ptr->scalar_identity_flag = 1;
2704 wanted_type_ptr->writing_in_flag = 0;
2705 wanted_type_ptr->reading_from_flag = 0;
2706 if (alloc_flag)
2707 wanted_type_ptr->writing_in_flag = 1;
2708 else
2710 if (strchr (fci->flags2, 'W') != 0)
2711 wanted_type_ptr->writing_in_flag = 1;
2712 if (strchr (fci->flags2, 'R') != 0)
2713 wanted_type_ptr->reading_from_flag = 1;
2715 wanted_type_ptr->kind = CF_KIND_FORMAT;
2716 wanted_type_ptr->param = cur_param;
2717 wanted_type_ptr->arg_num = arg_num;
2718 wanted_type_ptr->format_start = format_start;
2719 wanted_type_ptr->format_length = format_chars - format_start;
2720 wanted_type_ptr->offset_loc = format_chars - orig_format_chars;
2721 wanted_type_ptr->next = NULL;
2722 if (last_wanted_type != 0)
2723 last_wanted_type->next = wanted_type_ptr;
2724 if (first_wanted_type == 0)
2725 first_wanted_type = wanted_type_ptr;
2726 last_wanted_type = wanted_type_ptr;
2728 fci = fci->chain;
2729 if (fci)
2731 wanted_type_ptr = fwt_pool.allocate ();
2732 arg_num++;
2733 wanted_type = *fci->types[len_modifier.val].type;
2734 wanted_type_name = fci->types[len_modifier.val].name;
2739 if (first_wanted_type != 0)
2741 ptrdiff_t offset_to_format_start = (start_of_this_format - 1) - orig_format_chars;
2742 ptrdiff_t offset_to_format_end = (format_chars - 1) - orig_format_chars;
2743 /* By default, use the end of the range for the caret location. */
2744 substring_loc fmt_loc (fmt_param_loc, TREE_TYPE (format_string_cst),
2745 offset_to_format_end,
2746 offset_to_format_start, offset_to_format_end);
2747 ptrdiff_t offset_to_type_start = type_start - orig_format_chars;
2748 check_format_types (fmt_loc, first_wanted_type, fki,
2749 offset_to_type_start,
2750 conversion_char, arglocs);
2753 return true;
2756 /* Do the main part of checking a call to a format function. FORMAT_CHARS
2757 is the NUL-terminated format string (which at this point may contain
2758 internal NUL characters); FORMAT_LENGTH is its length (excluding the
2759 terminating NUL character). ARG_NUM is one less than the number of
2760 the first format argument to check; PARAMS points to that format
2761 argument in the list of arguments. */
2763 static void
2764 check_format_info_main (format_check_results *res,
2765 function_format_info *info, const char *format_chars,
2766 location_t fmt_param_loc, tree format_string_cst,
2767 int format_length, tree params,
2768 unsigned HOST_WIDE_INT arg_num,
2769 object_allocator <format_wanted_type> &fwt_pool,
2770 vec<location_t> *arglocs)
2772 const char * const orig_format_chars = format_chars;
2773 const tree first_fillin_param = params;
2775 const format_kind_info * const fki = &format_types[info->format_type];
2776 const format_flag_spec * const flag_specs = fki->flag_specs;
2777 const location_t format_string_loc = res->format_string_loc;
2779 /* -1 if no conversions taking an operand have been found; 0 if one has
2780 and it didn't use $; 1 if $ formats are in use. */
2781 int has_operand_number = -1;
2783 /* Vector of pointers to opening quoting directives (like GCC "%<"). */
2784 auto_vec<const char*> quotdirs;
2786 /* Pointers to the most recent color directives (like GCC's "%r or %R").
2787 A starting color directive much be terminated before the end of
2788 the format string. A terminating directive makes no sense without
2789 a prior starting directive. */
2790 const char *color_begin = NULL;
2791 const char *color_end = NULL;
2793 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
2795 while (*format_chars != 0)
2797 if (*format_chars++ != '%')
2798 continue;
2799 if (*format_chars == 0)
2801 format_warning_at_char (format_string_loc, format_string_cst,
2802 format_chars - orig_format_chars,
2803 OPT_Wformat_,
2804 "spurious trailing %<%%%> in format");
2805 continue;
2807 if (*format_chars == '%')
2809 ++format_chars;
2810 continue;
2813 flag_chars_t flag_chars;
2814 argument_parser arg_parser (info, format_chars, format_string_cst,
2815 orig_format_chars, format_string_loc,
2816 flag_chars, has_operand_number,
2817 first_fillin_param, fwt_pool, arglocs);
2819 if (!arg_parser.read_any_dollar ())
2820 return;
2822 if (!arg_parser.read_format_flags ())
2823 return;
2825 /* Read any format width, possibly * or *m$. */
2826 if (!arg_parser.read_any_format_width (params, arg_num))
2827 return;
2829 /* Read any format left precision (must be a number, not *). */
2830 arg_parser.read_any_format_left_precision ();
2832 /* Read any format precision, possibly * or *m$. */
2833 if (!arg_parser.read_any_format_precision (params, arg_num))
2834 return;
2836 const char *format_start = format_chars;
2838 arg_parser.handle_alloc_chars ();
2840 /* The rest of the conversion specification is the length modifier
2841 (if any), and the conversion specifier, so this is where the
2842 type information starts. If we need to issue a suggestion
2843 about a type mismatch, then we should preserve everything up
2844 to here. */
2845 const char *type_start = format_chars;
2847 /* Read any length modifier, if this kind of format has them. */
2848 const length_modifier len_modifier
2849 = arg_parser.read_any_length_modifier ();
2851 /* Read any modifier (strftime E/O). */
2852 arg_parser.read_any_other_modifier ();
2854 char format_char = *format_chars;
2855 if (format_char == 0
2856 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2857 && format_char == '%'))
2859 format_warning_at_char (format_string_loc, format_string_cst,
2860 format_chars - orig_format_chars,
2861 OPT_Wformat_,
2862 "conversion lacks type at end of format");
2863 continue;
2865 format_chars++;
2867 const format_char_info * const fci
2868 = arg_parser.find_format_char_info (format_char);
2869 if (!fci)
2870 continue;
2872 flag_chars.validate (fki, fci, flag_specs, format_chars,
2873 format_string_cst,
2874 format_string_loc, orig_format_chars, format_char,
2875 quotdirs.length () > 0);
2877 const int alloc_flag = flag_chars.get_alloc_flag (fki);
2878 const bool suppressed = flag_chars.assignment_suppression_p (fki);
2880 /* Diagnose nested or unmatched quoting directives such as GCC's
2881 "%<...%<" and "%>...%>". */
2882 bool quot_begin_p = strchr (fci->flags2, '<');
2883 bool quot_end_p = strchr (fci->flags2, '>');
2885 if (quot_begin_p && !quot_end_p)
2887 if (quotdirs.length ())
2888 format_warning_at_char (format_string_loc, format_string_cst,
2889 format_chars - orig_format_chars,
2890 OPT_Wformat_,
2891 "nested quoting directive");
2892 quotdirs.safe_push (format_chars);
2894 else if (!quot_begin_p && quot_end_p)
2896 if (quotdirs.length ())
2897 quotdirs.pop ();
2898 else
2899 format_warning_at_char (format_string_loc, format_string_cst,
2900 format_chars - orig_format_chars,
2901 OPT_Wformat_,
2902 "unmatched quoting directive");
2905 bool color_begin_p = strchr (fci->flags2, '/');
2906 if (color_begin_p)
2908 color_begin = format_chars;
2909 color_end = NULL;
2911 else if (strchr (fci->flags2, '\\'))
2913 if (color_end)
2914 format_warning_at_char (format_string_loc, format_string_cst,
2915 format_chars - orig_format_chars,
2916 OPT_Wformat_,
2917 "%qc directive redundant after prior "
2918 "occurence of the same", format_char);
2919 else if (!color_begin)
2920 format_warning_at_char (format_string_loc, format_string_cst,
2921 format_chars - orig_format_chars,
2922 OPT_Wformat_,
2923 "unmatched color reset directive");
2924 color_end = format_chars;
2927 /* Diagnose directives that shouldn't appear in a quoted sequence.
2928 (They are denoted by a double quote in FLAGS2.) */
2929 if (quotdirs.length ())
2931 if (strchr (fci->flags2, '"'))
2932 format_warning_at_char (format_string_loc, format_string_cst,
2933 format_chars - orig_format_chars,
2934 OPT_Wformat_,
2935 "%qc conversion used within a quoted "
2936 "sequence",
2937 format_char);
2940 /* Validate the pairs of flags used. */
2941 arg_parser.validate_flag_pairs (fci, format_char);
2943 arg_parser.give_y2k_warnings (fci, format_char);
2945 arg_parser.parse_any_scan_set (fci);
2947 tree wanted_type = NULL;
2948 const char *wanted_type_name = NULL;
2950 if (!arg_parser.handle_conversions (fci, len_modifier,
2951 wanted_type, wanted_type_name,
2952 arg_num,
2953 params,
2954 format_char))
2955 continue;
2957 arg_parser.main_wanted_type.next = NULL;
2959 /* Finally. . .check type of argument against desired type! */
2960 if (!arg_parser.check_argument_type (fci, len_modifier,
2961 wanted_type, wanted_type_name,
2962 suppressed,
2963 arg_num, params,
2964 alloc_flag,
2965 format_start, type_start,
2966 fmt_param_loc,
2967 format_char))
2968 return;
2971 if (format_chars - orig_format_chars != format_length)
2972 format_warning_at_char (format_string_loc, format_string_cst,
2973 format_chars + 1 - orig_format_chars,
2974 OPT_Wformat_contains_nul,
2975 "embedded %<\\0%> in format");
2976 if (info->first_arg_num != 0 && params != 0
2977 && has_operand_number <= 0)
2979 res->number_other--;
2980 res->number_extra_args++;
2982 if (has_operand_number > 0)
2983 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2985 if (quotdirs.length ())
2986 format_warning_at_char (format_string_loc, format_string_cst,
2987 quotdirs.pop () - orig_format_chars,
2988 OPT_Wformat_, "unterminated quoting directive");
2989 if (color_begin && !color_end)
2990 format_warning_at_char (format_string_loc, format_string_cst,
2991 color_begin - orig_format_chars,
2992 OPT_Wformat_, "unterminated color directive");
2995 /* Check the argument types from a single format conversion (possibly
2996 including width and precision arguments).
2998 FMT_LOC is the location of the format conversion.
3000 TYPES is a singly-linked list expressing the parts of the format
3001 conversion that expect argument types, and the arguments they
3002 correspond to.
3004 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3005 format string to where type information begins for the conversion
3006 (the length modifier and conversion specifier).
3008 CONVERSION_CHAR is the user-provided conversion specifier.
3010 For example, given:
3012 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3014 then FMT_LOC covers this range:
3016 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3017 ^^^^^^^^^
3019 and TYPES in this case is a three-entry singly-linked list consisting of:
3020 (1) the check for the field width here:
3021 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3022 ^ ^^^^
3023 against arg3, and
3024 (2) the check for the field precision here:
3025 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3026 ^^ ^^^^
3027 against arg4, and
3028 (3) the check for the length modifier and conversion char here:
3029 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3030 ^^^ ^^^^
3031 against arg5.
3033 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3034 STRING_CST:
3036 0000000000111111111122
3037 0123456789012345678901
3038 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3040 | ` CONVERSION_CHAR: 'd'
3041 type starts here. */
3043 static void
3044 check_format_types (const substring_loc &fmt_loc,
3045 format_wanted_type *types, const format_kind_info *fki,
3046 int offset_to_type_start,
3047 char conversion_char,
3048 vec<location_t> *arglocs)
3050 for (; types != 0; types = types->next)
3052 tree cur_param;
3053 tree cur_type;
3054 tree orig_cur_type;
3055 tree wanted_type;
3056 int arg_num;
3057 int i;
3058 int char_type_flag;
3060 wanted_type = types->wanted_type;
3061 arg_num = types->arg_num;
3063 /* The following should not occur here. */
3064 gcc_assert (wanted_type);
3065 gcc_assert (wanted_type != void_type_node || types->pointer_count);
3067 if (types->pointer_count == 0)
3068 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
3070 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
3072 cur_param = types->param;
3073 if (!cur_param)
3075 format_type_warning (fmt_loc, UNKNOWN_LOCATION, types, wanted_type,
3076 NULL, fki, offset_to_type_start,
3077 conversion_char);
3078 continue;
3081 cur_type = TREE_TYPE (cur_param);
3082 if (cur_type == error_mark_node)
3083 continue;
3084 orig_cur_type = cur_type;
3085 char_type_flag = 0;
3087 location_t param_loc = UNKNOWN_LOCATION;
3088 if (EXPR_HAS_LOCATION (cur_param))
3089 param_loc = EXPR_LOCATION (cur_param);
3090 else if (arglocs)
3092 /* arg_num is 1-based. */
3093 gcc_assert (types->arg_num > 0);
3094 param_loc = (*arglocs)[types->arg_num - 1];
3097 STRIP_NOPS (cur_param);
3099 /* Check the types of any additional pointer arguments
3100 that precede the "real" argument. */
3101 for (i = 0; i < types->pointer_count; ++i)
3103 if (TREE_CODE (cur_type) == POINTER_TYPE)
3105 cur_type = TREE_TYPE (cur_type);
3106 if (cur_type == error_mark_node)
3107 break;
3109 /* Check for writing through a NULL pointer. */
3110 if (types->writing_in_flag
3111 && i == 0
3112 && cur_param != 0
3113 && integer_zerop (cur_param))
3114 warning (OPT_Wformat_, "writing through null pointer "
3115 "(argument %d)", arg_num);
3117 /* Check for reading through a NULL pointer. */
3118 if (types->reading_from_flag
3119 && i == 0
3120 && cur_param != 0
3121 && integer_zerop (cur_param))
3122 warning (OPT_Wformat_, "reading through null pointer "
3123 "(argument %d)", arg_num);
3125 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
3126 cur_param = TREE_OPERAND (cur_param, 0);
3127 else
3128 cur_param = 0;
3130 /* See if this is an attempt to write into a const type with
3131 scanf or with printf "%n". Note: the writing in happens
3132 at the first indirection only, if for example
3133 void * const * is passed to scanf %p; passing
3134 const void ** is simply passing an incompatible type. */
3135 if (types->writing_in_flag
3136 && i == 0
3137 && (TYPE_READONLY (cur_type)
3138 || (cur_param != 0
3139 && (CONSTANT_CLASS_P (cur_param)
3140 || (DECL_P (cur_param)
3141 && TREE_READONLY (cur_param))))))
3142 warning (OPT_Wformat_, "writing into constant object "
3143 "(argument %d)", arg_num);
3145 /* If there are extra type qualifiers beyond the first
3146 indirection, then this makes the types technically
3147 incompatible. */
3148 if (i > 0
3149 && pedantic
3150 && (TYPE_READONLY (cur_type)
3151 || TYPE_VOLATILE (cur_type)
3152 || TYPE_ATOMIC (cur_type)
3153 || TYPE_RESTRICT (cur_type)))
3154 warning (OPT_Wformat_, "extra type qualifiers in format "
3155 "argument (argument %d)",
3156 arg_num);
3159 else
3161 format_type_warning (fmt_loc, param_loc,
3162 types, wanted_type, orig_cur_type, fki,
3163 offset_to_type_start, conversion_char);
3164 break;
3168 if (i < types->pointer_count)
3169 continue;
3171 cur_type = TYPE_MAIN_VARIANT (cur_type);
3173 /* Check whether the argument type is a character type. This leniency
3174 only applies to certain formats, flagged with 'c'. */
3175 if (types->char_lenient_flag)
3176 char_type_flag = (cur_type == char_type_node
3177 || cur_type == signed_char_type_node
3178 || cur_type == unsigned_char_type_node);
3180 /* Check the type of the "real" argument, if there's a type we want. */
3181 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
3182 continue;
3183 /* If we want 'void *', allow any pointer type.
3184 (Anything else would already have got a warning.)
3185 With -Wpedantic, only allow pointers to void and to character
3186 types. */
3187 if (wanted_type == void_type_node
3188 && (!pedantic || (i == 1 && char_type_flag)))
3189 continue;
3190 /* Don't warn about differences merely in signedness, unless
3191 -Wpedantic. With -Wpedantic, warn if the type is a pointer
3192 target and not a character type, and for character types at
3193 a second level of indirection. */
3194 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3195 && TREE_CODE (cur_type) == INTEGER_TYPE
3196 && ((!pedantic && !warn_format_signedness)
3197 || (i == 0 && !warn_format_signedness)
3198 || (i == 1 && char_type_flag))
3199 && (TYPE_UNSIGNED (wanted_type)
3200 ? wanted_type == c_common_unsigned_type (cur_type)
3201 : wanted_type == c_common_signed_type (cur_type)))
3202 continue;
3203 /* Don't warn about differences merely in signedness if we know
3204 that the current type is integer-promoted and its original type
3205 was unsigned such as that it is in the range of WANTED_TYPE. */
3206 if (TREE_CODE (wanted_type) == INTEGER_TYPE
3207 && TREE_CODE (cur_type) == INTEGER_TYPE
3208 && warn_format_signedness
3209 && TYPE_UNSIGNED (wanted_type)
3210 && cur_param != NULL_TREE
3211 && TREE_CODE (cur_param) == NOP_EXPR)
3213 tree t = TREE_TYPE (TREE_OPERAND (cur_param, 0));
3214 if (TYPE_UNSIGNED (t)
3215 && cur_type == lang_hooks.types.type_promotes_to (t))
3216 continue;
3218 /* Likewise, "signed char", "unsigned char" and "char" are
3219 equivalent but the above test won't consider them equivalent. */
3220 if (wanted_type == char_type_node
3221 && (!pedantic || i < 2)
3222 && char_type_flag)
3223 continue;
3224 if (types->scalar_identity_flag
3225 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
3226 || (INTEGRAL_TYPE_P (cur_type)
3227 && INTEGRAL_TYPE_P (wanted_type)))
3228 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
3229 continue;
3230 /* Now we have a type mismatch. */
3231 format_type_warning (fmt_loc, param_loc, types,
3232 wanted_type, orig_cur_type, fki,
3233 offset_to_type_start, conversion_char);
3237 /* Given type TYPE, attempt to dereference the type N times
3238 (e.g. from ("int ***", 2) to "int *")
3240 Return the derefenced type, with any qualifiers
3241 such as "const" stripped from the result, or
3242 NULL if unsuccessful (e.g. TYPE is not a pointer type). */
3244 static tree
3245 deref_n_times (tree type, int n)
3247 gcc_assert (type);
3249 for (int i = n; i > 0; i--)
3251 if (TREE_CODE (type) != POINTER_TYPE)
3252 return NULL_TREE;
3253 type = TREE_TYPE (type);
3255 /* Strip off any "const" etc. */
3256 return build_qualified_type (type, 0);
3259 /* Lookup the format code for FORMAT_LEN within FLI,
3260 returning the string code for expressing it, or NULL
3261 if it is not found. */
3263 static const char *
3264 get_modifier_for_format_len (const format_length_info *fli,
3265 enum format_lengths format_len)
3267 for (; fli->name; fli++)
3269 if (fli->index == format_len)
3270 return fli->name;
3271 if (fli->double_index == format_len)
3272 return fli->double_name;
3274 return NULL;
3277 #if CHECKING_P
3279 namespace selftest {
3281 static void
3282 test_get_modifier_for_format_len ()
3284 ASSERT_STREQ ("h",
3285 get_modifier_for_format_len (printf_length_specs, FMT_LEN_h));
3286 ASSERT_STREQ ("hh",
3287 get_modifier_for_format_len (printf_length_specs, FMT_LEN_hh));
3288 ASSERT_STREQ ("L",
3289 get_modifier_for_format_len (printf_length_specs, FMT_LEN_L));
3290 ASSERT_EQ (NULL,
3291 get_modifier_for_format_len (printf_length_specs, FMT_LEN_none));
3294 } // namespace selftest
3296 #endif /* CHECKING_P */
3298 /* Determine if SPEC_TYPE and ARG_TYPE are sufficiently similar for a
3299 format_type_detail using SPEC_TYPE to be offered as a suggestion for
3300 Wformat type errors where the argument has type ARG_TYPE. */
3302 static bool
3303 matching_type_p (tree spec_type, tree arg_type)
3305 gcc_assert (spec_type);
3306 gcc_assert (arg_type);
3308 /* If any of the types requires structural equality, we can't compare
3309 their canonical types. */
3310 if (TYPE_STRUCTURAL_EQUALITY_P (spec_type)
3311 || TYPE_STRUCTURAL_EQUALITY_P (arg_type))
3312 return false;
3314 spec_type = TYPE_CANONICAL (spec_type);
3315 arg_type = TYPE_CANONICAL (arg_type);
3317 if (TREE_CODE (spec_type) == INTEGER_TYPE
3318 && TREE_CODE (arg_type) == INTEGER_TYPE
3319 && (TYPE_UNSIGNED (spec_type)
3320 ? spec_type == c_common_unsigned_type (arg_type)
3321 : spec_type == c_common_signed_type (arg_type)))
3322 return true;
3324 return spec_type == arg_type;
3327 /* Subroutine of get_format_for_type.
3329 Generate a string containing the length modifier and conversion specifier
3330 that should be used to format arguments of type ARG_TYPE within FKI
3331 (effectively the inverse of the checking code).
3333 If CONVERSION_CHAR is not zero (the first pass), the resulting suggestion
3334 is required to use it, for correcting bogus length modifiers.
3335 If CONVERSION_CHAR is zero (the second pass), then allow any suggestion
3336 that matches ARG_TYPE.
3338 If successful, returns a non-NULL string which should be freed
3339 by the caller.
3340 Otherwise, returns NULL. */
3342 static char *
3343 get_format_for_type_1 (const format_kind_info *fki, tree arg_type,
3344 char conversion_char)
3346 gcc_assert (arg_type);
3348 const format_char_info *spec;
3349 for (spec = &fki->conversion_specs[0];
3350 spec->format_chars;
3351 spec++)
3353 if (conversion_char)
3354 if (!strchr (spec->format_chars, conversion_char))
3355 continue;
3357 tree effective_arg_type = deref_n_times (arg_type,
3358 spec->pointer_count);
3359 if (!effective_arg_type)
3360 continue;
3361 for (int i = 0; i < FMT_LEN_MAX; i++)
3363 const format_type_detail *ftd = &spec->types[i];
3364 if (!ftd->type)
3365 continue;
3366 if (matching_type_p (*ftd->type, effective_arg_type))
3368 const char *len_modifier
3369 = get_modifier_for_format_len (fki->length_char_specs,
3370 (enum format_lengths)i);
3371 if (!len_modifier)
3372 len_modifier = "";
3374 if (conversion_char)
3375 /* We found a match, using the given conversion char - the
3376 length modifier was incorrect (or absent).
3377 Provide a suggestion using the conversion char with the
3378 correct length modifier for the type. */
3379 return xasprintf ("%s%c", len_modifier, conversion_char);
3380 else
3381 /* 2nd pass: no match was possible using the user-provided
3382 conversion char, but we do have a match without using it.
3383 Provide a suggestion using the first conversion char
3384 listed for the given type. */
3385 return xasprintf ("%s%c", len_modifier, spec->format_chars[0]);
3390 return NULL;
3393 /* Generate a string containing the length modifier and conversion specifier
3394 that should be used to format arguments of type ARG_TYPE within FKI
3395 (effectively the inverse of the checking code).
3397 If successful, returns a non-NULL string which should be freed
3398 by the caller.
3399 Otherwise, returns NULL. */
3401 static char *
3402 get_format_for_type (const format_kind_info *fki, tree arg_type,
3403 char conversion_char)
3405 gcc_assert (arg_type);
3406 gcc_assert (conversion_char);
3408 /* First pass: look for a format_char_info containing CONVERSION_CHAR
3409 If we find one, then presumably the length modifier was incorrect
3410 (or absent). */
3411 char *result = get_format_for_type_1 (fki, arg_type, conversion_char);
3412 if (result)
3413 return result;
3415 /* Second pass: we didn't find a match for CONVERSION_CHAR, so try
3416 matching just on the type. */
3417 return get_format_for_type_1 (fki, arg_type, '\0');
3420 /* Attempt to get a string for use as a replacement fix-it hint for the
3421 source range in FMT_LOC.
3423 Preserve all of the text within the range of FMT_LOC up to
3424 OFFSET_TO_TYPE_START, replacing the rest with an appropriate
3425 length modifier and conversion specifier for ARG_TYPE, attempting
3426 to keep the user-provided CONVERSION_CHAR if possible.
3428 For example, given a long vs long long mismatch for arg5 here:
3430 000000000111111111122222222223333333333|
3431 123456789012345678901234567890123456789` column numbers
3432 0000000000111111111122|
3433 0123456789012345678901` string offsets
3434 V~~~~~~~~ : range of FMT_LOC, from cols 23-31
3435 sprintf (d, "before %-+*.*lld after", arg3, arg4, arg5);
3437 | ` CONVERSION_CHAR: 'd'
3438 type starts here
3440 where OFFSET_TO_TYPE_START is 13 (the offset to the "lld" within the
3441 STRING_CST), where the user provided:
3442 %-+*.*lld
3443 the result (assuming "long" argument 5) should be:
3444 %-+*.*ld
3446 If successful, returns a non-NULL string which should be freed
3447 by the caller.
3448 Otherwise, returns NULL. */
3450 static char *
3451 get_corrected_substring (const substring_loc &fmt_loc,
3452 format_wanted_type *type, tree arg_type,
3453 const format_kind_info *fki,
3454 int offset_to_type_start, char conversion_char)
3456 /* Attempt to provide hints for argument types, but not for field widths
3457 and precisions. */
3458 if (!arg_type)
3459 return NULL;
3460 if (type->kind != CF_KIND_FORMAT)
3461 return NULL;
3463 /* Locate the current code within the source range, rejecting
3464 any awkward cases where the format string occupies more than
3465 one line.
3466 Lookup the place where the type starts (including any length
3467 modifiers), getting it as the caret location. */
3468 substring_loc type_loc (fmt_loc);
3469 type_loc.set_caret_index (offset_to_type_start);
3471 location_t fmt_substring_loc;
3472 const char *err = type_loc.get_location (&fmt_substring_loc);
3473 if (err)
3474 return NULL;
3476 source_range fmt_substring_range
3477 = get_range_from_loc (line_table, fmt_substring_loc);
3479 expanded_location caret
3480 = expand_location_to_spelling_point (fmt_substring_loc);
3481 expanded_location start
3482 = expand_location_to_spelling_point (fmt_substring_range.m_start);
3483 expanded_location finish
3484 = expand_location_to_spelling_point (fmt_substring_range.m_finish);
3485 if (caret.file != start.file)
3486 return NULL;
3487 if (start.file != finish.file)
3488 return NULL;
3489 if (caret.line != start.line)
3490 return NULL;
3491 if (start.line != finish.line)
3492 return NULL;
3493 if (start.column > caret.column)
3494 return NULL;
3495 if (start.column > finish.column)
3496 return NULL;
3497 if (caret.column > finish.column)
3498 return NULL;
3500 int line_width;
3501 const char *line = location_get_source_line (start.file, start.line,
3502 &line_width);
3503 if (line == NULL)
3504 return NULL;
3506 /* If we got this far, then we have the line containing the
3507 existing conversion specification.
3509 Generate a trimmed copy, containing the prefix part of the conversion
3510 specification, up to the (but not including) the length modifier.
3511 In the above example, this would be "%-+*.*". */
3512 const char *current_content = line + start.column - 1;
3513 int length_up_to_type = caret.column - start.column;
3514 char *prefix = xstrndup (current_content, length_up_to_type);
3516 /* Now attempt to generate a suggestion for the rest of the specification
3517 (length modifier and conversion char), based on ARG_TYPE and
3518 CONVERSION_CHAR.
3519 In the above example, this would be "ld". */
3520 char *format_for_type = get_format_for_type (fki, arg_type, conversion_char);
3521 if (!format_for_type)
3523 free (prefix);
3524 return NULL;
3527 /* Success. Generate the resulting suggestion for the whole range of
3528 FMT_LOC by concatenating the two strings.
3529 In the above example, this would be "%-+*.*ld". */
3530 char *result = concat (prefix, format_for_type, NULL);
3531 free (format_for_type);
3532 free (prefix);
3533 return result;
3536 /* Give a warning about a format argument of different type from that expected.
3537 The range of the diagnostic is taken from WHOLE_FMT_LOC; the caret location
3538 is based on the location of the char at TYPE->offset_loc.
3539 PARAM_LOC is the location of the relevant argument, or UNKNOWN_LOCATION
3540 if this is unavailable.
3541 WANTED_TYPE is the type the argument should have,
3542 possibly stripped of pointer dereferences. The description (such as "field
3543 precision"), the placement in the format string, a possibly more
3544 friendly name of WANTED_TYPE, and the number of pointer dereferences
3545 are taken from TYPE. ARG_TYPE is the type of the actual argument,
3546 or NULL if it is missing.
3548 OFFSET_TO_TYPE_START is the offset within the execution-charset encoded
3549 format string to where type information begins for the conversion
3550 (the length modifier and conversion specifier).
3551 CONVERSION_CHAR is the user-provided conversion specifier.
3553 For example, given a type mismatch for argument 5 here:
3555 00000000011111111112222222222333333333344444444445555555555|
3556 12345678901234567890123456789012345678901234567890123456789` column numbers
3557 0000000000111111111122|
3558 0123456789012345678901` offsets within STRING_CST
3559 V~~~~~~~~ : range of WHOLE_FMT_LOC, from cols 23-31
3560 sprintf (d, "before %-+*.*lld after", int_expr, int_expr, long_expr);
3561 ^ ^ ^~~~~~~~~
3562 | ` CONVERSION_CHAR: 'd' PARAM_LOC
3563 type starts here
3565 OFFSET_TO_TYPE_START is 13, the offset to the "lld" within the
3566 STRING_CST. */
3568 static void
3569 format_type_warning (const substring_loc &whole_fmt_loc,
3570 location_t param_loc,
3571 format_wanted_type *type,
3572 tree wanted_type, tree arg_type,
3573 const format_kind_info *fki,
3574 int offset_to_type_start,
3575 char conversion_char)
3577 enum format_specifier_kind kind = type->kind;
3578 const char *wanted_type_name = type->wanted_type_name;
3579 const char *format_start = type->format_start;
3580 int format_length = type->format_length;
3581 int pointer_count = type->pointer_count;
3582 int arg_num = type->arg_num;
3584 char *p;
3585 /* If ARG_TYPE is a typedef with a misleading name (for example,
3586 size_t but not the standard size_t expected by printf %zu), avoid
3587 printing the typedef name. */
3588 if (wanted_type_name
3589 && arg_type
3590 && TYPE_NAME (arg_type)
3591 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
3592 && DECL_NAME (TYPE_NAME (arg_type))
3593 && !strcmp (wanted_type_name,
3594 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
3595 arg_type = TYPE_MAIN_VARIANT (arg_type);
3596 /* The format type and name exclude any '*' for pointers, so those
3597 must be formatted manually. For all the types we currently have,
3598 this is adequate, but formats taking pointers to functions or
3599 arrays would require the full type to be built up in order to
3600 print it with %T. */
3601 p = (char *) alloca (pointer_count + 2);
3602 if (pointer_count == 0)
3603 p[0] = 0;
3604 else if (c_dialect_cxx ())
3606 memset (p, '*', pointer_count);
3607 p[pointer_count] = 0;
3609 else
3611 p[0] = ' ';
3612 memset (p + 1, '*', pointer_count);
3613 p[pointer_count + 1] = 0;
3616 /* WHOLE_FMT_LOC has the caret at the end of the range.
3617 Set the caret to be at the offset from TYPE. Subtract one
3618 from the offset for the same reason as in format_warning_at_char. */
3619 substring_loc fmt_loc (whole_fmt_loc);
3620 fmt_loc.set_caret_index (type->offset_loc - 1);
3622 /* Get a string for use as a replacement fix-it hint for the range in
3623 fmt_loc, or NULL. */
3624 char *corrected_substring
3625 = get_corrected_substring (fmt_loc, type, arg_type, fki,
3626 offset_to_type_start, conversion_char);
3628 if (wanted_type_name)
3630 if (arg_type)
3631 format_warning_at_substring
3632 (fmt_loc, param_loc,
3633 corrected_substring, OPT_Wformat_,
3634 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
3635 "but argument %d has type %qT",
3636 gettext (kind_descriptions[kind]),
3637 (kind == CF_KIND_FORMAT ? "%" : ""),
3638 format_length, format_start,
3639 wanted_type_name, p, arg_num, arg_type);
3640 else
3641 format_warning_at_substring
3642 (fmt_loc, param_loc,
3643 corrected_substring, OPT_Wformat_,
3644 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
3645 gettext (kind_descriptions[kind]),
3646 (kind == CF_KIND_FORMAT ? "%" : ""),
3647 format_length, format_start, wanted_type_name, p);
3649 else
3651 if (arg_type)
3652 format_warning_at_substring
3653 (fmt_loc, param_loc,
3654 corrected_substring, OPT_Wformat_,
3655 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
3656 "but argument %d has type %qT",
3657 gettext (kind_descriptions[kind]),
3658 (kind == CF_KIND_FORMAT ? "%" : ""),
3659 format_length, format_start,
3660 wanted_type, p, arg_num, arg_type);
3661 else
3662 format_warning_at_substring
3663 (fmt_loc, param_loc,
3664 corrected_substring, OPT_Wformat_,
3665 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
3666 gettext (kind_descriptions[kind]),
3667 (kind == CF_KIND_FORMAT ? "%" : ""),
3668 format_length, format_start, wanted_type, p);
3671 free (corrected_substring);
3675 /* Given a format_char_info array FCI, and a character C, this function
3676 returns the index into the conversion_specs where that specifier's
3677 data is located. The character must exist. */
3678 static unsigned int
3679 find_char_info_specifier_index (const format_char_info *fci, int c)
3681 unsigned i;
3683 for (i = 0; fci->format_chars; i++, fci++)
3684 if (strchr (fci->format_chars, c))
3685 return i;
3687 /* We shouldn't be looking for a non-existent specifier. */
3688 gcc_unreachable ();
3691 /* Given a format_length_info array FLI, and a character C, this
3692 function returns the index into the conversion_specs where that
3693 modifier's data is located. The character must exist. */
3694 static unsigned int
3695 find_length_info_modifier_index (const format_length_info *fli, int c)
3697 unsigned i;
3699 for (i = 0; fli->name; i++, fli++)
3700 if (strchr (fli->name, c))
3701 return i;
3703 /* We shouldn't be looking for a non-existent modifier. */
3704 gcc_unreachable ();
3707 /* Determine the type of HOST_WIDE_INT in the code being compiled for
3708 use in GCC's __asm_fprintf__ custom format attribute. You must
3709 have set dynamic_format_types before calling this function. */
3710 static void
3711 init_dynamic_asm_fprintf_info (void)
3713 static tree hwi;
3715 if (!hwi)
3717 format_length_info *new_asm_fprintf_length_specs;
3718 unsigned int i;
3720 /* Find the underlying type for HOST_WIDE_INT. For the %w
3721 length modifier to work, one must have issued: "typedef
3722 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3723 prior to using that modifier. */
3724 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
3725 if (!hwi)
3727 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3728 return;
3730 hwi = identifier_global_value (hwi);
3731 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
3733 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3734 return;
3736 hwi = DECL_ORIGINAL_TYPE (hwi);
3737 gcc_assert (hwi);
3738 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
3740 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
3741 " or %<long long%>");
3742 return;
3745 /* Create a new (writable) copy of asm_fprintf_length_specs. */
3746 new_asm_fprintf_length_specs = (format_length_info *)
3747 xmemdup (asm_fprintf_length_specs,
3748 sizeof (asm_fprintf_length_specs),
3749 sizeof (asm_fprintf_length_specs));
3751 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3752 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
3753 if (hwi == long_integer_type_node)
3754 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
3755 else if (hwi == long_long_integer_type_node)
3756 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
3757 else
3758 gcc_unreachable ();
3760 /* Assign the new data for use. */
3761 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
3762 new_asm_fprintf_length_specs;
3766 /* Determine the type of a "locus" in the code being compiled for use
3767 in GCC's __gcc_gfc__ custom format attribute. You must have set
3768 dynamic_format_types before calling this function. */
3769 static void
3770 init_dynamic_gfc_info (void)
3772 if (!locus)
3774 static format_char_info *gfc_fci;
3776 /* For the GCC __gcc_gfc__ custom format specifier to work, one
3777 must have declared 'locus' prior to using this attribute. If
3778 we haven't seen this declarations then you shouldn't use the
3779 specifier requiring that type. */
3780 if ((locus = maybe_get_identifier ("locus")))
3782 locus = identifier_global_value (locus);
3783 if (locus)
3785 if (TREE_CODE (locus) != TYPE_DECL
3786 || TREE_TYPE (locus) == error_mark_node)
3788 error ("%<locus%> is not defined as a type");
3789 locus = 0;
3791 else
3792 locus = TREE_TYPE (locus);
3796 /* Assign the new data for use. */
3798 /* Handle the __gcc_gfc__ format specifics. */
3799 if (!gfc_fci)
3800 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
3801 gfc_fci = (format_char_info *)
3802 xmemdup (gcc_gfc_char_table,
3803 sizeof (gcc_gfc_char_table),
3804 sizeof (gcc_gfc_char_table));
3805 if (locus)
3807 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
3808 gfc_fci[i].types[0].type = &locus;
3809 gfc_fci[i].pointer_count = 1;
3814 /* Determine the types of "tree" and "location_t" in the code being
3815 compiled for use in GCC's diagnostic custom format attributes. You
3816 must have set dynamic_format_types before calling this function. */
3817 static void
3818 init_dynamic_diag_info (void)
3820 /* For the GCC-diagnostics custom format specifiers to work, one
3821 must have declared 'tree' and 'location_t' prior to using those
3822 attributes. If we haven't seen these declarations then
3823 the specifiers requiring these types shouldn't be used.
3824 However we don't force a hard ICE because we may see only one
3825 or the other type. */
3826 if (tree loc = maybe_get_identifier ("location_t"))
3828 loc = identifier_global_value (loc);
3829 if (loc && TREE_CODE (loc) != TYPE_DECL)
3830 error ("%<location_t%> is not defined as a type");
3833 /* Initialize the global tree node type local to this file. */
3834 if (!local_tree_type_node
3835 || local_tree_type_node == void_type_node)
3837 /* We need to grab the underlying 'union tree_node' so peek into
3838 an extra type level. */
3839 if ((local_tree_type_node = maybe_get_identifier ("tree")))
3841 local_tree_type_node = identifier_global_value (local_tree_type_node);
3842 if (local_tree_type_node)
3844 if (TREE_CODE (local_tree_type_node) != TYPE_DECL)
3846 error ("%<tree%> is not defined as a type");
3847 local_tree_type_node = 0;
3849 else if (TREE_CODE (TREE_TYPE (local_tree_type_node))
3850 != POINTER_TYPE)
3852 error ("%<tree%> is not defined as a pointer type");
3853 local_tree_type_node = 0;
3855 else
3856 local_tree_type_node =
3857 TREE_TYPE (TREE_TYPE (local_tree_type_node));
3860 else
3861 local_tree_type_node = void_type_node;
3864 /* Similar to the above but for gcall*. */
3865 if (!local_gcall_ptr_node
3866 || local_gcall_ptr_node == void_type_node)
3868 if ((local_gcall_ptr_node = maybe_get_identifier ("gcall")))
3870 local_gcall_ptr_node
3871 = identifier_global_value (local_gcall_ptr_node);
3872 if (local_gcall_ptr_node)
3874 if (TREE_CODE (local_gcall_ptr_node) != TYPE_DECL)
3876 error ("%<gcall%> is not defined as a type");
3877 local_gcall_ptr_node = 0;
3879 else
3880 local_gcall_ptr_node = TREE_TYPE (local_gcall_ptr_node);
3883 else
3884 local_gcall_ptr_node = void_type_node;
3887 static tree hwi;
3889 if (!hwi)
3891 static format_length_info *diag_ls;
3892 unsigned int i;
3894 /* Find the underlying type for HOST_WIDE_INT. For the 'w'
3895 length modifier to work, one must have issued: "typedef
3896 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
3897 prior to using that modifier. */
3898 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
3900 hwi = identifier_global_value (hwi);
3901 if (hwi)
3903 if (TREE_CODE (hwi) != TYPE_DECL)
3905 error ("%<__gcc_host_wide_int__%> is not defined as a type");
3906 hwi = 0;
3908 else
3910 hwi = DECL_ORIGINAL_TYPE (hwi);
3911 gcc_assert (hwi);
3912 if (hwi != long_integer_type_node
3913 && hwi != long_long_integer_type_node)
3915 error ("%<__gcc_host_wide_int__%> is not defined"
3916 " as %<long%> or %<long long%>");
3917 hwi = 0;
3923 /* Assign the new data for use. */
3925 /* All the GCC diag formats use the same length specs. */
3926 if (!diag_ls)
3927 dynamic_format_types[gcc_diag_format_type].length_char_specs =
3928 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
3929 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
3930 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
3931 diag_ls = (format_length_info *)
3932 xmemdup (gcc_diag_length_specs,
3933 sizeof (gcc_diag_length_specs),
3934 sizeof (gcc_diag_length_specs));
3935 if (hwi)
3937 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
3938 i = find_length_info_modifier_index (diag_ls, 'w');
3939 if (hwi == long_integer_type_node)
3940 diag_ls[i].index = FMT_LEN_l;
3941 else if (hwi == long_long_integer_type_node)
3942 diag_ls[i].index = FMT_LEN_ll;
3943 else
3944 gcc_unreachable ();
3948 /* It's safe to "re-initialize these to the same values. */
3949 dynamic_format_types[gcc_diag_format_type].conversion_specs =
3950 gcc_diag_char_table;
3951 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
3952 gcc_tdiag_char_table;
3953 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
3954 gcc_cdiag_char_table;
3955 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
3956 gcc_cxxdiag_char_table;
3959 #ifdef TARGET_FORMAT_TYPES
3960 extern const format_kind_info TARGET_FORMAT_TYPES[];
3961 #endif
3963 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3964 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
3965 #endif
3966 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3967 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3968 #endif
3970 /* Attributes such as "printf" are equivalent to those such as
3971 "gnu_printf" unless this is overridden by a target. */
3972 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
3974 { "gnu_printf", "printf" },
3975 { "gnu_scanf", "scanf" },
3976 { "gnu_strftime", "strftime" },
3977 { "gnu_strfmon", "strfmon" },
3978 { NULL, NULL }
3981 /* Translate to unified attribute name. This is used in decode_format_type and
3982 decode_format_attr. In attr_name the user specified argument is passed. It
3983 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3984 or the attr_name passed to this function, if there is no matching entry. */
3985 static const char *
3986 convert_format_name_to_system_name (const char *attr_name)
3988 int i;
3990 if (attr_name == NULL || *attr_name == 0
3991 || strncmp (attr_name, "gcc_", 4) == 0)
3992 return attr_name;
3993 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3994 TARGET_OVERRIDES_FORMAT_INIT ();
3995 #endif
3997 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3998 /* Check if format attribute is overridden by target. */
3999 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
4000 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
4002 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
4004 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
4005 attr_name))
4006 return attr_name;
4007 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
4008 attr_name))
4009 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
4012 #endif
4013 /* Otherwise default to gnu format. */
4014 for (i = 0;
4015 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
4016 ++i)
4018 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
4019 attr_name))
4020 return attr_name;
4021 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
4022 attr_name))
4023 return gnu_target_overrides_format_attributes[i].named_attr_src;
4026 return attr_name;
4029 /* Handle a "format" attribute; arguments as in
4030 struct attribute_spec.handler. */
4031 tree
4032 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
4033 int flags, bool *no_add_attrs)
4035 tree type = *node;
4036 function_format_info info;
4038 #ifdef TARGET_FORMAT_TYPES
4039 /* If the target provides additional format types, we need to
4040 add them to FORMAT_TYPES at first use. */
4041 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
4043 dynamic_format_types = XNEWVEC (format_kind_info,
4044 n_format_types + TARGET_N_FORMAT_TYPES);
4045 memcpy (dynamic_format_types, format_types_orig,
4046 sizeof (format_types_orig));
4047 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
4048 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
4050 format_types = dynamic_format_types;
4051 /* Provide a reference for the first potential external type. */
4052 first_target_format_type = n_format_types;
4053 n_format_types += TARGET_N_FORMAT_TYPES;
4055 #endif
4057 /* Canonicalize name of format function. */
4058 if (TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
4059 TREE_VALUE (args) = canonicalize_attr_name (TREE_VALUE (args));
4061 if (!decode_format_attr (args, &info, 0))
4063 *no_add_attrs = true;
4064 return NULL_TREE;
4067 if (prototype_p (type))
4069 if (!check_format_string (type, info.format_num, flags,
4070 no_add_attrs, info.format_type))
4071 return NULL_TREE;
4073 if (info.first_arg_num != 0)
4075 unsigned HOST_WIDE_INT arg_num = 1;
4076 function_args_iterator iter;
4077 tree arg_type;
4079 /* Verify that first_arg_num points to the last arg,
4080 the ... */
4081 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
4082 arg_num++;
4084 if (arg_num != info.first_arg_num)
4086 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
4087 error ("args to be formatted is not %<...%>");
4088 *no_add_attrs = true;
4089 return NULL_TREE;
4094 /* Check if this is a strftime variant. Just for this variant
4095 FMT_FLAG_ARG_CONVERT is not set. */
4096 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
4097 && info.first_arg_num != 0)
4099 error ("strftime formats cannot format arguments");
4100 *no_add_attrs = true;
4101 return NULL_TREE;
4104 /* If this is a custom GCC-internal format type, we have to
4105 initialize certain bits at runtime. */
4106 if (info.format_type == asm_fprintf_format_type
4107 || info.format_type == gcc_gfc_format_type
4108 || info.format_type == gcc_diag_format_type
4109 || info.format_type == gcc_tdiag_format_type
4110 || info.format_type == gcc_cdiag_format_type
4111 || info.format_type == gcc_cxxdiag_format_type)
4113 /* Our first time through, we have to make sure that our
4114 format_type data is allocated dynamically and is modifiable. */
4115 if (!dynamic_format_types)
4116 format_types = dynamic_format_types = (format_kind_info *)
4117 xmemdup (format_types_orig, sizeof (format_types_orig),
4118 sizeof (format_types_orig));
4120 /* If this is format __asm_fprintf__, we have to initialize
4121 GCC's notion of HOST_WIDE_INT for checking %wd. */
4122 if (info.format_type == asm_fprintf_format_type)
4123 init_dynamic_asm_fprintf_info ();
4124 /* If this is format __gcc_gfc__, we have to initialize GCC's
4125 notion of 'locus' at runtime for %L. */
4126 else if (info.format_type == gcc_gfc_format_type)
4127 init_dynamic_gfc_info ();
4128 /* If this is one of the diagnostic attributes, then we have to
4129 initialize 'location_t' and 'tree' at runtime. */
4130 else if (info.format_type == gcc_diag_format_type
4131 || info.format_type == gcc_tdiag_format_type
4132 || info.format_type == gcc_cdiag_format_type
4133 || info.format_type == gcc_cxxdiag_format_type)
4134 init_dynamic_diag_info ();
4135 else
4136 gcc_unreachable ();
4139 return NULL_TREE;
4142 #if CHECKING_P
4144 namespace selftest {
4146 /* Selftests of location handling. */
4148 /* Get the format_kind_info with the given name. */
4150 static const format_kind_info *
4151 get_info (const char *name)
4153 int idx = decode_format_type (name);
4154 const format_kind_info *fki = &format_types[idx];
4155 ASSERT_STREQ (fki->name, name);
4156 return fki;
4159 /* Verify that get_format_for_type (FKI, TYPE, CONVERSION_CHAR)
4160 is EXPECTED_FORMAT. */
4162 static void
4163 assert_format_for_type_streq (const location &loc, const format_kind_info *fki,
4164 const char *expected_format, tree type,
4165 char conversion_char)
4167 gcc_assert (fki);
4168 gcc_assert (expected_format);
4169 gcc_assert (type);
4171 char *actual_format = get_format_for_type (fki, type, conversion_char);
4172 ASSERT_STREQ_AT (loc, expected_format, actual_format);
4173 free (actual_format);
4176 /* Selftests for get_format_for_type. */
4178 #define ASSERT_FORMAT_FOR_TYPE_STREQ(EXPECTED_FORMAT, TYPE, CONVERSION_CHAR) \
4179 assert_format_for_type_streq (SELFTEST_LOCATION, (fki), (EXPECTED_FORMAT), \
4180 (TYPE), (CONVERSION_CHAR))
4182 /* Selftest for get_format_for_type for "printf"-style functions. */
4184 static void
4185 test_get_format_for_type_printf ()
4187 const format_kind_info *fki = get_info ("gnu_printf");
4188 ASSERT_NE (fki, NULL);
4190 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'i');
4191 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'i');
4192 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'o');
4193 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'o');
4194 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'x');
4195 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'x');
4196 ASSERT_FORMAT_FOR_TYPE_STREQ ("f", double_type_node, 'X');
4197 ASSERT_FORMAT_FOR_TYPE_STREQ ("Lf", long_double_type_node, 'X');
4198 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", integer_type_node, 'd');
4199 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", integer_type_node, 'i');
4200 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", integer_type_node, 'o');
4201 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", integer_type_node, 'x');
4202 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", integer_type_node, 'X');
4203 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", unsigned_type_node, 'd');
4204 ASSERT_FORMAT_FOR_TYPE_STREQ ("i", unsigned_type_node, 'i');
4205 ASSERT_FORMAT_FOR_TYPE_STREQ ("o", unsigned_type_node, 'o');
4206 ASSERT_FORMAT_FOR_TYPE_STREQ ("x", unsigned_type_node, 'x');
4207 ASSERT_FORMAT_FOR_TYPE_STREQ ("X", unsigned_type_node, 'X');
4208 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld", long_integer_type_node, 'd');
4209 ASSERT_FORMAT_FOR_TYPE_STREQ ("li", long_integer_type_node, 'i');
4210 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_integer_type_node, 'x');
4211 ASSERT_FORMAT_FOR_TYPE_STREQ ("lo", long_unsigned_type_node, 'o');
4212 ASSERT_FORMAT_FOR_TYPE_STREQ ("lx", long_unsigned_type_node, 'x');
4213 ASSERT_FORMAT_FOR_TYPE_STREQ ("lld", long_long_integer_type_node, 'd');
4214 ASSERT_FORMAT_FOR_TYPE_STREQ ("lli", long_long_integer_type_node, 'i');
4215 ASSERT_FORMAT_FOR_TYPE_STREQ ("llo", long_long_unsigned_type_node, 'o');
4216 ASSERT_FORMAT_FOR_TYPE_STREQ ("llx", long_long_unsigned_type_node, 'x');
4217 ASSERT_FORMAT_FOR_TYPE_STREQ ("s", build_pointer_type (char_type_node), 'i');
4220 /* Selftest for get_format_for_type for "scanf"-style functions. */
4222 static void
4223 test_get_format_for_type_scanf ()
4225 const format_kind_info *fki = get_info ("gnu_scanf");
4226 ASSERT_NE (fki, NULL);
4227 ASSERT_FORMAT_FOR_TYPE_STREQ ("d", build_pointer_type (integer_type_node), 'd');
4228 ASSERT_FORMAT_FOR_TYPE_STREQ ("u", build_pointer_type (unsigned_type_node), 'u');
4229 ASSERT_FORMAT_FOR_TYPE_STREQ ("ld",
4230 build_pointer_type (long_integer_type_node), 'd');
4231 ASSERT_FORMAT_FOR_TYPE_STREQ ("lu",
4232 build_pointer_type (long_unsigned_type_node), 'u');
4233 ASSERT_FORMAT_FOR_TYPE_STREQ
4234 ("lld", build_pointer_type (long_long_integer_type_node), 'd');
4235 ASSERT_FORMAT_FOR_TYPE_STREQ
4236 ("llu", build_pointer_type (long_long_unsigned_type_node), 'u');
4237 ASSERT_FORMAT_FOR_TYPE_STREQ ("e", build_pointer_type (float_type_node), 'e');
4238 ASSERT_FORMAT_FOR_TYPE_STREQ ("le", build_pointer_type (double_type_node), 'e');
4241 #undef ASSERT_FORMAT_FOR_TYPE_STREQ
4243 /* Run all of the selftests within this file. */
4245 void
4246 c_format_c_tests ()
4248 test_get_modifier_for_format_len ();
4249 test_get_format_for_type_printf ();
4250 test_get_format_for_type_scanf ();
4253 } // namespace selftest
4255 #endif /* CHECKING_P */
4257 #include "gt-c-family-c-format.h"