Partial implementation of C++11 thread_local.
[official-gcc.git] / gcc / c-family / c-format.c
blob2d1ed8176f4c9d4397b227138204970c2d92614f
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "c-common.h"
29 #include "c-objc.h"
30 #include "intl.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
33 #include "c-format.h"
34 #include "alloc-pool.h"
35 #include "c-target.h"
37 /* Set format warning options according to a -Wformat=n option. */
39 void
40 set_Wformat (int setting)
42 warn_format = setting;
43 warn_format_extra_args = setting;
44 warn_format_zero_length = setting;
45 warn_format_contains_nul = setting;
46 if (setting != 1)
48 warn_format_nonliteral = setting;
49 warn_format_security = setting;
50 warn_format_y2k = setting;
52 /* Make sure not to disable -Wnonnull if -Wformat=0 is specified. */
53 if (setting)
54 warn_nonnull = setting;
58 /* Handle attributes associated with format checking. */
60 /* This must be in the same order as format_types, except for
61 format_type_error. Target-specific format types do not have
62 matching enum values. */
63 enum format_type { printf_format_type, asm_fprintf_format_type,
64 gcc_diag_format_type, gcc_tdiag_format_type,
65 gcc_cdiag_format_type,
66 gcc_cxxdiag_format_type, gcc_gfc_format_type,
67 gcc_objc_string_format_type,
68 format_type_error = -1};
70 typedef struct function_format_info
72 int format_type; /* type of format (printf, scanf, etc.) */
73 unsigned HOST_WIDE_INT format_num; /* number of format argument */
74 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
75 } function_format_info;
77 static bool decode_format_attr (tree, function_format_info *, int);
78 static int decode_format_type (const char *);
80 static bool check_format_string (tree argument,
81 unsigned HOST_WIDE_INT format_num,
82 int flags, bool *no_add_attrs,
83 int expected_format_type);
84 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
85 int validated_p);
86 static const char *convert_format_name_to_system_name (const char *attr_name);
87 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
89 static int first_target_format_type;
90 static const char *format_name (int format_num);
91 static int format_flags (int format_num);
93 /* Check that we have a pointer to a string suitable for use as a format.
94 The default is to check for a char type.
95 For objective-c dialects, this is extended to include references to string
96 objects validated by objc_string_ref_type_p ().
97 Targets may also provide a string object type that can be used within c and
98 c++ and shared with their respective objective-c dialects. In this case the
99 reference to a format string is checked for validity via a hook.
101 The function returns true if strref points to any string type valid for the
102 language dialect and target. */
104 static bool
105 valid_stringptr_type_p (tree strref)
107 return (strref != NULL
108 && TREE_CODE (strref) == POINTER_TYPE
109 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
110 || objc_string_ref_type_p (strref)
111 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
114 /* Handle a "format_arg" attribute; arguments as in
115 struct attribute_spec.handler. */
116 tree
117 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
118 tree args, int flags, bool *no_add_attrs)
120 tree type = *node;
121 tree format_num_expr = TREE_VALUE (args);
122 unsigned HOST_WIDE_INT format_num = 0;
124 if (!get_constant (format_num_expr, &format_num, 0))
126 error ("format string has invalid operand number");
127 *no_add_attrs = true;
128 return NULL_TREE;
131 if (prototype_p (type))
133 /* The format arg can be any string reference valid for the language and
134 target. We cannot be more specific in this case. */
135 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
136 return NULL_TREE;
139 if (!valid_stringptr_type_p (TREE_TYPE (type)))
141 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
142 error ("function does not return string type");
143 *no_add_attrs = true;
144 return NULL_TREE;
147 return NULL_TREE;
150 /* Verify that the format_num argument is actually a string reference suitable,
151 for the language dialect and target (in case the format attribute is in
152 error). When we know the specific reference type expected, this is also
153 checked. */
154 static bool
155 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
156 int flags, bool *no_add_attrs, int expected_format_type)
158 unsigned HOST_WIDE_INT i;
159 bool is_objc_sref, is_target_sref, is_char_ref;
160 tree ref;
161 int fmt_flags;
162 function_args_iterator iter;
164 i = 1;
165 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
167 if (i == format_num)
168 break;
169 i++;
172 if (!ref
173 || !valid_stringptr_type_p (ref))
175 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
176 error ("format string argument is not a string type");
177 *no_add_attrs = true;
178 return false;
181 /* We only know that we want a suitable string reference. */
182 if (expected_format_type < 0)
183 return true;
185 /* Now check that the arg matches the expected type. */
186 is_char_ref =
187 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
189 fmt_flags = format_flags (expected_format_type);
190 is_objc_sref = is_target_sref = false;
191 if (!is_char_ref)
192 is_objc_sref = objc_string_ref_type_p (ref);
194 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
196 if (is_char_ref)
197 return true; /* OK, we expected a char and found one. */
198 else
200 /* We expected a char but found an extended string type. */
201 if (is_objc_sref)
202 error ("found a %<%s%> reference but the format argument should"
203 " be a string", format_name (gcc_objc_string_format_type));
204 else
205 error ("found a %qT but the format argument should be a string",
206 ref);
207 *no_add_attrs = true;
208 return false;
212 /* We expect a string object type as the format arg. */
213 if (is_char_ref)
215 error ("format argument should be a %<%s%> reference but"
216 " a string was found", format_name (expected_format_type));
217 *no_add_attrs = true;
218 return false;
221 /* We will assert that objective-c will support either its own string type
222 or the target-supplied variant. */
223 if (!is_objc_sref)
224 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
226 if (expected_format_type == (int) gcc_objc_string_format_type
227 && (is_objc_sref || is_target_sref))
228 return true;
230 /* We will allow a target string ref to match only itself. */
231 if (first_target_format_type
232 && expected_format_type >= first_target_format_type
233 && is_target_sref)
234 return true;
235 else
237 error ("format argument should be a %<%s%> reference",
238 format_name (expected_format_type));
239 *no_add_attrs = true;
240 return false;
243 gcc_unreachable ();
246 /* Verify EXPR is a constant, and store its value.
247 If validated_p is true there should be no errors.
248 Returns true on success, false otherwise. */
249 static bool
250 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
252 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
254 gcc_assert (!validated_p);
255 return false;
258 *value = TREE_INT_CST_LOW (expr);
260 return true;
263 /* Decode the arguments to a "format" attribute into a
264 function_format_info structure. It is already known that the list
265 is of the right length. If VALIDATED_P is true, then these
266 attributes have already been validated and must not be erroneous;
267 if false, it will give an error message. Returns true if the
268 attributes are successfully decoded, false otherwise. */
270 static bool
271 decode_format_attr (tree args, function_format_info *info, int validated_p)
273 tree format_type_id = TREE_VALUE (args);
274 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
275 tree first_arg_num_expr
276 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
278 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
280 gcc_assert (!validated_p);
281 error ("unrecognized format specifier");
282 return false;
284 else
286 const char *p = IDENTIFIER_POINTER (format_type_id);
288 p = convert_format_name_to_system_name (p);
290 info->format_type = decode_format_type (p);
292 if (!c_dialect_objc ()
293 && info->format_type == gcc_objc_string_format_type)
295 gcc_assert (!validated_p);
296 warning (OPT_Wformat, "%qE is only allowed in Objective-C dialects",
297 format_type_id);
298 info->format_type = format_type_error;
299 return false;
302 if (info->format_type == format_type_error)
304 gcc_assert (!validated_p);
305 warning (OPT_Wformat, "%qE is an unrecognized format function type",
306 format_type_id);
307 return false;
311 if (!get_constant (format_num_expr, &info->format_num, validated_p))
313 error ("format string has invalid operand number");
314 return false;
317 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
319 error ("%<...%> has invalid operand number");
320 return false;
323 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
325 gcc_assert (!validated_p);
326 error ("format string argument follows the args to be formatted");
327 return false;
330 return true;
333 /* Check a call to a format function against a parameter list. */
335 /* The C standard version C++ is treated as equivalent to
336 or inheriting from, for the purpose of format features supported. */
337 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
338 /* The C standard version we are checking formats against when pedantic. */
339 #define C_STD_VER ((int) (c_dialect_cxx () \
340 ? CPLUSPLUS_STD_VER \
341 : (flag_isoc99 \
342 ? STD_C99 \
343 : (flag_isoc94 ? STD_C94 : STD_C89))))
344 /* The name to give to the standard version we are warning about when
345 pedantic. FEATURE_VER is the version in which the feature warned out
346 appeared, which is higher than C_STD_VER. */
347 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
348 ? (cxx_dialect < cxx11 ? "ISO C++98" \
349 : "ISO C++11") \
350 : ((FEATURE_VER) == STD_EXT \
351 ? "ISO C" \
352 : "ISO C90"))
353 /* Adjust a C standard version, which may be STD_C9L, to account for
354 -Wno-long-long. Returns other standard versions unchanged. */
355 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
356 ? (warn_long_long ? STD_C99 : STD_C89) \
357 : (VER)))
359 /* Enum describing the kind of specifiers present in the format and
360 requiring an argument. */
361 enum format_specifier_kind {
362 CF_KIND_FORMAT,
363 CF_KIND_FIELD_WIDTH,
364 CF_KIND_FIELD_PRECISION
367 static const char *kind_descriptions[] = {
368 N_("format"),
369 N_("field width specifier"),
370 N_("field precision specifier")
373 /* Structure describing details of a type expected in format checking,
374 and the type to check against it. */
375 typedef struct format_wanted_type
377 /* The type wanted. */
378 tree wanted_type;
379 /* The name of this type to use in diagnostics. */
380 const char *wanted_type_name;
381 /* Should be type checked just for scalar width identity. */
382 int scalar_identity_flag;
383 /* The level of indirection through pointers at which this type occurs. */
384 int pointer_count;
385 /* Whether, when pointer_count is 1, to allow any character type when
386 pedantic, rather than just the character or void type specified. */
387 int char_lenient_flag;
388 /* Whether the argument, dereferenced once, is written into and so the
389 argument must not be a pointer to a const-qualified type. */
390 int writing_in_flag;
391 /* Whether the argument, dereferenced once, is read from and so
392 must not be a NULL pointer. */
393 int reading_from_flag;
394 /* The kind of specifier that this type is used for. */
395 enum format_specifier_kind kind;
396 /* The starting character of the specifier. This never includes the
397 initial percent sign. */
398 const char *format_start;
399 /* The length of the specifier. */
400 int format_length;
401 /* The actual parameter to check against the wanted type. */
402 tree param;
403 /* The argument number of that parameter. */
404 int arg_num;
405 /* The next type to check for this format conversion, or NULL if none. */
406 struct format_wanted_type *next;
407 } format_wanted_type;
409 /* Convenience macro for format_length_info meaning unused. */
410 #define NO_FMT NULL, FMT_LEN_none, STD_C89
412 static const format_length_info printf_length_specs[] =
414 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
415 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
416 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
417 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
418 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
419 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
420 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
421 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
422 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
423 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
424 { NO_FMT, NO_FMT, 0 }
427 /* Length specifiers valid for asm_fprintf. */
428 static const format_length_info asm_fprintf_length_specs[] =
430 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
431 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
432 { NO_FMT, NO_FMT, 0 }
435 /* Length specifiers valid for GCC diagnostics. */
436 static const format_length_info gcc_diag_length_specs[] =
438 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
439 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
440 { NO_FMT, NO_FMT, 0 }
443 /* The custom diagnostics all accept the same length specifiers. */
444 #define gcc_tdiag_length_specs gcc_diag_length_specs
445 #define gcc_cdiag_length_specs gcc_diag_length_specs
446 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
448 /* This differs from printf_length_specs only in that "Z" is not accepted. */
449 static const format_length_info scanf_length_specs[] =
451 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
452 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
453 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
454 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
455 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
456 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
457 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
458 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
459 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
460 { NO_FMT, NO_FMT, 0 }
464 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
465 make no sense for a format type not part of any C standard version. */
466 static const format_length_info strfmon_length_specs[] =
468 /* A GNU extension. */
469 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
470 { NO_FMT, NO_FMT, 0 }
474 /* For now, the Fortran front-end routines only use l as length modifier. */
475 static const format_length_info gcc_gfc_length_specs[] =
477 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
478 { NO_FMT, NO_FMT, 0 }
482 static const format_flag_spec printf_flag_specs[] =
484 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
485 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
486 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
487 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
488 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
489 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
490 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
491 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
492 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
493 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
494 { 0, 0, 0, NULL, NULL, STD_C89 }
498 static const format_flag_pair printf_flag_pairs[] =
500 { ' ', '+', 1, 0 },
501 { '0', '-', 1, 0 },
502 { '0', 'p', 1, 'i' },
503 { 0, 0, 0, 0 }
506 static const format_flag_spec asm_fprintf_flag_specs[] =
508 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
509 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
510 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
511 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
512 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
513 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
514 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
515 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
516 { 0, 0, 0, NULL, NULL, STD_C89 }
519 static const format_flag_pair asm_fprintf_flag_pairs[] =
521 { ' ', '+', 1, 0 },
522 { '0', '-', 1, 0 },
523 { '0', 'p', 1, 'i' },
524 { 0, 0, 0, 0 }
527 static const format_flag_pair gcc_diag_flag_pairs[] =
529 { 0, 0, 0, 0 }
532 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
533 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
534 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
536 static const format_flag_pair gcc_gfc_flag_pairs[] =
538 { 0, 0, 0, 0 }
541 static const format_flag_spec gcc_diag_flag_specs[] =
543 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
544 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
545 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
546 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
547 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
548 { 0, 0, 0, NULL, NULL, STD_C89 }
551 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
552 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
553 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
555 static const format_flag_spec scanf_flag_specs[] =
557 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
558 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
559 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
560 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
561 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
562 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
563 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
564 { 0, 0, 0, NULL, NULL, STD_C89 }
568 static const format_flag_pair scanf_flag_pairs[] =
570 { '*', 'L', 0, 0 },
571 { 'a', 'm', 0, 0 },
572 { 0, 0, 0, 0 }
576 static const format_flag_spec strftime_flag_specs[] =
578 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
579 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
580 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
581 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
582 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
583 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
584 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
585 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
586 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
587 { 0, 0, 0, NULL, NULL, STD_C89 }
591 static const format_flag_pair strftime_flag_pairs[] =
593 { 'E', 'O', 0, 0 },
594 { '_', '-', 0, 0 },
595 { '_', '0', 0, 0 },
596 { '-', '0', 0, 0 },
597 { '^', '#', 0, 0 },
598 { 0, 0, 0, 0 }
602 static const format_flag_spec strfmon_flag_specs[] =
604 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
605 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
606 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
607 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
608 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
609 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
610 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
611 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
612 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
613 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
614 { 0, 0, 0, NULL, NULL, STD_C89 }
617 static const format_flag_pair strfmon_flag_pairs[] =
619 { '+', '(', 0, 0 },
620 { 0, 0, 0, 0 }
624 static const format_char_info print_char_table[] =
626 /* C89 conversion specifiers. */
627 { "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 },
628 { "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 },
629 { "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 },
630 { "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 },
631 { "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 },
632 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
633 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
634 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
635 { "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 },
636 /* C99 conversion specifiers. */
637 { "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 },
638 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
639 /* X/Open conversion specifiers. */
640 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
641 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
642 /* GNU conversion specifiers. */
643 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
644 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
647 static const format_char_info asm_fprintf_char_table[] =
649 /* C89 conversion specifiers. */
650 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
651 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
652 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
653 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
654 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
656 /* asm_fprintf conversion specifiers. */
657 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
658 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
659 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
660 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
661 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
662 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
663 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
664 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
667 static const format_char_info gcc_diag_char_table[] =
669 /* C89 conversion specifiers. */
670 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
671 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
672 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
673 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
674 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
675 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
677 /* Custom conversion specifiers. */
679 /* These will require a "tree" at runtime. */
680 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
682 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
683 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
684 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
687 static const format_char_info gcc_tdiag_char_table[] =
689 /* C89 conversion specifiers. */
690 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
691 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
692 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
693 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
694 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
695 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
697 /* Custom conversion specifiers. */
699 /* These will require a "tree" at runtime. */
700 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
702 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
704 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
705 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
706 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
709 static const format_char_info gcc_cdiag_char_table[] =
711 /* C89 conversion specifiers. */
712 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
713 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
714 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
715 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
716 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
717 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
719 /* Custom conversion specifiers. */
721 /* These will require a "tree" at runtime. */
722 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
724 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
726 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
727 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
728 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
731 static const format_char_info gcc_cxxdiag_char_table[] =
733 /* C89 conversion specifiers. */
734 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
735 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
736 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
737 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
738 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
739 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
741 /* Custom conversion specifiers. */
743 /* These will require a "tree" at runtime. */
744 { "ADEFKSTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
746 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
748 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
749 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
751 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
752 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
753 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
756 static const format_char_info gcc_gfc_char_table[] =
758 /* C89 conversion specifiers. */
759 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
760 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
761 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
762 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
764 /* gfc conversion specifiers. */
766 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
768 /* This will require a "locus" at runtime. */
769 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
771 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
774 static const format_char_info scan_char_table[] =
776 /* C89 conversion specifiers. */
777 { "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 },
778 { "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 },
779 { "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 },
780 { "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 },
781 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
782 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
783 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
784 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
785 { "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 },
786 /* C99 conversion specifiers. */
787 { "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 },
788 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
789 /* X/Open conversion specifiers. */
790 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
791 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
792 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
795 static const format_char_info time_char_table[] =
797 /* C89 conversion specifiers. */
798 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
799 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
800 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
801 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
802 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
803 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
804 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
805 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
806 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
807 /* C99 conversion specifiers. */
808 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
809 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
810 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
811 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
812 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
813 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
814 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
815 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
816 /* GNU conversion specifiers. */
817 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
818 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
819 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
822 static const format_char_info monetary_char_table[] =
824 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
825 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
828 /* This must be in the same order as enum format_type. */
829 static const format_kind_info format_types_orig[] =
831 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
832 printf_flag_specs, printf_flag_pairs,
833 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
834 'w', 0, 'p', 0, 'L', 0,
835 &integer_type_node, &integer_type_node
837 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
838 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
839 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
840 'w', 0, 'p', 0, 'L', 0,
841 NULL, NULL
843 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
844 gcc_diag_flag_specs, gcc_diag_flag_pairs,
845 FMT_FLAG_ARG_CONVERT,
846 0, 0, 'p', 0, 'L', 0,
847 NULL, &integer_type_node
849 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
850 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
851 FMT_FLAG_ARG_CONVERT,
852 0, 0, 'p', 0, 'L', 0,
853 NULL, &integer_type_node
855 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
856 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
857 FMT_FLAG_ARG_CONVERT,
858 0, 0, 'p', 0, 'L', 0,
859 NULL, &integer_type_node
861 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
862 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
863 FMT_FLAG_ARG_CONVERT,
864 0, 0, 'p', 0, 'L', 0,
865 NULL, &integer_type_node
867 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
868 NULL, gcc_gfc_flag_pairs,
869 FMT_FLAG_ARG_CONVERT,
870 0, 0, 0, 0, 0, 0,
871 NULL, NULL
873 { "NSString", NULL, NULL, NULL, NULL,
874 NULL, NULL,
875 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
876 NULL, NULL
878 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
879 scanf_flag_specs, scanf_flag_pairs,
880 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
881 'w', 0, 0, '*', 'L', 'm',
882 NULL, NULL
884 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
885 strftime_flag_specs, strftime_flag_pairs,
886 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
887 NULL, NULL
889 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
890 strfmon_flag_specs, strfmon_flag_pairs,
891 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
892 NULL, NULL
896 /* This layer of indirection allows GCC to reassign format_types with
897 new data if necessary, while still allowing the original data to be
898 const. */
899 static const format_kind_info *format_types = format_types_orig;
900 /* We can modify this one. We also add target-specific format types
901 to the end of the array. */
902 static format_kind_info *dynamic_format_types;
904 static int n_format_types = ARRAY_SIZE (format_types_orig);
906 /* Structure detailing the results of checking a format function call
907 where the format expression may be a conditional expression with
908 many leaves resulting from nested conditional expressions. */
909 typedef struct
911 /* Number of leaves of the format argument that could not be checked
912 as they were not string literals. */
913 int number_non_literal;
914 /* Number of leaves of the format argument that were null pointers or
915 string literals, but had extra format arguments. */
916 int number_extra_args;
917 /* Number of leaves of the format argument that were null pointers or
918 string literals, but had extra format arguments and used $ operand
919 numbers. */
920 int number_dollar_extra_args;
921 /* Number of leaves of the format argument that were wide string
922 literals. */
923 int number_wide;
924 /* Number of leaves of the format argument that were empty strings. */
925 int number_empty;
926 /* Number of leaves of the format argument that were unterminated
927 strings. */
928 int number_unterminated;
929 /* Number of leaves of the format argument that were not counted above. */
930 int number_other;
931 } format_check_results;
933 typedef struct
935 format_check_results *res;
936 function_format_info *info;
937 tree params;
938 } format_check_context;
940 /* Return the format name (as specified in the original table) for the format
941 type indicated by format_num. */
942 static const char *
943 format_name (int format_num)
945 if (format_num >= 0 && format_num < n_format_types)
946 return format_types[format_num].name;
947 gcc_unreachable ();
950 /* Return the format flags (as specified in the original table) for the format
951 type indicated by format_num. */
952 static int
953 format_flags (int format_num)
955 if (format_num >= 0 && format_num < n_format_types)
956 return format_types[format_num].flags;
957 gcc_unreachable ();
960 static void check_format_info (function_format_info *, tree);
961 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
962 static void check_format_info_main (format_check_results *,
963 function_format_info *,
964 const char *, int, tree,
965 unsigned HOST_WIDE_INT, alloc_pool);
967 static void init_dollar_format_checking (int, tree);
968 static int maybe_read_dollar_number (const char **, int,
969 tree, tree *, const format_kind_info *);
970 static bool avoid_dollar_number (const char *);
971 static void finish_dollar_format_checking (format_check_results *, int);
973 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
974 int, const char *);
976 static void check_format_types (format_wanted_type *);
977 static void format_type_warning (format_wanted_type *, tree, tree);
979 /* Decode a format type from a string, returning the type, or
980 format_type_error if not valid, in which case the caller should print an
981 error message. */
982 static int
983 decode_format_type (const char *s)
985 int i;
986 int slen;
988 s = convert_format_name_to_system_name (s);
989 slen = strlen (s);
990 for (i = 0; i < n_format_types; i++)
992 int alen;
993 if (!strcmp (s, format_types[i].name))
994 return i;
995 alen = strlen (format_types[i].name);
996 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
997 && s[slen - 1] == '_' && s[slen - 2] == '_'
998 && !strncmp (s + 2, format_types[i].name, alen))
999 return i;
1001 return format_type_error;
1005 /* Check the argument list of a call to printf, scanf, etc.
1006 ATTRS are the attributes on the function type. There are NARGS argument
1007 values in the array ARGARRAY.
1008 Also, if -Wsuggest-attribute=format,
1009 warn for calls to vprintf or vscanf in functions with no such format
1010 attribute themselves. */
1012 void
1013 check_function_format (tree attrs, int nargs, tree *argarray)
1015 tree a;
1017 /* See if this function has any format attributes. */
1018 for (a = attrs; a; a = TREE_CHAIN (a))
1020 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1022 /* Yup; check it. */
1023 function_format_info info;
1024 decode_format_attr (TREE_VALUE (a), &info, 1);
1025 if (warn_format)
1027 /* FIXME: Rewrite all the internal functions in this file
1028 to use the ARGARRAY directly instead of constructing this
1029 temporary list. */
1030 tree params = NULL_TREE;
1031 int i;
1032 for (i = nargs - 1; i >= 0; i--)
1033 params = tree_cons (NULL_TREE, argarray[i], params);
1034 check_format_info (&info, params);
1036 if (warn_suggest_attribute_format && info.first_arg_num == 0
1037 && (format_types[info.format_type].flags
1038 & (int) FMT_FLAG_ARG_CONVERT))
1040 tree c;
1041 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1043 c = TREE_CHAIN (c))
1044 if (is_attribute_p ("format", TREE_PURPOSE (c))
1045 && (decode_format_type (IDENTIFIER_POINTER
1046 (TREE_VALUE (TREE_VALUE (c))))
1047 == info.format_type))
1048 break;
1049 if (c == NULL_TREE)
1051 /* Check if the current function has a parameter to which
1052 the format attribute could be attached; if not, it
1053 can't be a candidate for a format attribute, despite
1054 the vprintf-like or vscanf-like call. */
1055 tree args;
1056 for (args = DECL_ARGUMENTS (current_function_decl);
1057 args != 0;
1058 args = DECL_CHAIN (args))
1060 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1061 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1062 == char_type_node))
1063 break;
1065 if (args != 0)
1066 warning (OPT_Wsuggest_attribute_format, "function might "
1067 "be possible candidate for %qs format attribute",
1068 format_types[info.format_type].name);
1076 /* Variables used by the checking of $ operand number formats. */
1077 static char *dollar_arguments_used = NULL;
1078 static char *dollar_arguments_pointer_p = NULL;
1079 static int dollar_arguments_alloc = 0;
1080 static int dollar_arguments_count;
1081 static int dollar_first_arg_num;
1082 static int dollar_max_arg_used;
1083 static int dollar_format_warned;
1085 /* Initialize the checking for a format string that may contain $
1086 parameter number specifications; we will need to keep track of whether
1087 each parameter has been used. FIRST_ARG_NUM is the number of the first
1088 argument that is a parameter to the format, or 0 for a vprintf-style
1089 function; PARAMS is the list of arguments starting at this argument. */
1091 static void
1092 init_dollar_format_checking (int first_arg_num, tree params)
1094 tree oparams = params;
1096 dollar_first_arg_num = first_arg_num;
1097 dollar_arguments_count = 0;
1098 dollar_max_arg_used = 0;
1099 dollar_format_warned = 0;
1100 if (first_arg_num > 0)
1102 while (params)
1104 dollar_arguments_count++;
1105 params = TREE_CHAIN (params);
1108 if (dollar_arguments_alloc < dollar_arguments_count)
1110 free (dollar_arguments_used);
1111 free (dollar_arguments_pointer_p);
1112 dollar_arguments_alloc = dollar_arguments_count;
1113 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1114 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1116 if (dollar_arguments_alloc)
1118 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1119 if (first_arg_num > 0)
1121 int i = 0;
1122 params = oparams;
1123 while (params)
1125 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1126 == POINTER_TYPE);
1127 params = TREE_CHAIN (params);
1128 i++;
1135 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1136 is set, it is an error if one is not found; otherwise, it is OK. If
1137 such a number is found, check whether it is within range and mark that
1138 numbered operand as being used for later checking. Returns the operand
1139 number if found and within range, zero if no such number was found and
1140 this is OK, or -1 on error. PARAMS points to the first operand of the
1141 format; PARAM_PTR is made to point to the parameter referred to. If
1142 a $ format is found, *FORMAT is updated to point just after it. */
1144 static int
1145 maybe_read_dollar_number (const char **format,
1146 int dollar_needed, tree params, tree *param_ptr,
1147 const format_kind_info *fki)
1149 int argnum;
1150 int overflow_flag;
1151 const char *fcp = *format;
1152 if (!ISDIGIT (*fcp))
1154 if (dollar_needed)
1156 warning (OPT_Wformat, "missing $ operand number in format");
1157 return -1;
1159 else
1160 return 0;
1162 argnum = 0;
1163 overflow_flag = 0;
1164 while (ISDIGIT (*fcp))
1166 int nargnum;
1167 nargnum = 10 * argnum + (*fcp - '0');
1168 if (nargnum < 0 || nargnum / 10 != argnum)
1169 overflow_flag = 1;
1170 argnum = nargnum;
1171 fcp++;
1173 if (*fcp != '$')
1175 if (dollar_needed)
1177 warning (OPT_Wformat, "missing $ operand number in format");
1178 return -1;
1180 else
1181 return 0;
1183 *format = fcp + 1;
1184 if (pedantic && !dollar_format_warned)
1186 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
1187 C_STD_NAME (STD_EXT));
1188 dollar_format_warned = 1;
1190 if (overflow_flag || argnum == 0
1191 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1193 warning (OPT_Wformat, "operand number out of range in format");
1194 return -1;
1196 if (argnum > dollar_max_arg_used)
1197 dollar_max_arg_used = argnum;
1198 /* For vprintf-style functions we may need to allocate more memory to
1199 track which arguments are used. */
1200 while (dollar_arguments_alloc < dollar_max_arg_used)
1202 int nalloc;
1203 nalloc = 2 * dollar_arguments_alloc + 16;
1204 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1205 nalloc);
1206 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1207 nalloc);
1208 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1209 nalloc - dollar_arguments_alloc);
1210 dollar_arguments_alloc = nalloc;
1212 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1213 && dollar_arguments_used[argnum - 1] == 1)
1215 dollar_arguments_used[argnum - 1] = 2;
1216 warning (OPT_Wformat, "format argument %d used more than once in %s format",
1217 argnum, fki->name);
1219 else
1220 dollar_arguments_used[argnum - 1] = 1;
1221 if (dollar_first_arg_num)
1223 int i;
1224 *param_ptr = params;
1225 for (i = 1; i < argnum && *param_ptr != 0; i++)
1226 *param_ptr = TREE_CHAIN (*param_ptr);
1228 /* This case shouldn't be caught here. */
1229 gcc_assert (*param_ptr);
1231 else
1232 *param_ptr = 0;
1233 return argnum;
1236 /* Ensure that FORMAT does not start with a decimal number followed by
1237 a $; give a diagnostic and return true if it does, false otherwise. */
1239 static bool
1240 avoid_dollar_number (const char *format)
1242 if (!ISDIGIT (*format))
1243 return false;
1244 while (ISDIGIT (*format))
1245 format++;
1246 if (*format == '$')
1248 warning (OPT_Wformat, "$ operand number used after format without operand number");
1249 return true;
1251 return false;
1255 /* Finish the checking for a format string that used $ operand number formats
1256 instead of non-$ formats. We check for unused operands before used ones
1257 (a serious error, since the implementation of the format function
1258 can't know what types to pass to va_arg to find the later arguments).
1259 and for unused operands at the end of the format (if we know how many
1260 arguments the format had, so not for vprintf). If there were operand
1261 numbers out of range on a non-vprintf-style format, we won't have reached
1262 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1263 pointers. */
1265 static void
1266 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1268 int i;
1269 bool found_pointer_gap = false;
1270 for (i = 0; i < dollar_max_arg_used; i++)
1272 if (!dollar_arguments_used[i])
1274 if (pointer_gap_ok && (dollar_first_arg_num == 0
1275 || dollar_arguments_pointer_p[i]))
1276 found_pointer_gap = true;
1277 else
1278 warning (OPT_Wformat,
1279 "format argument %d unused before used argument %d in $-style format",
1280 i + 1, dollar_max_arg_used);
1283 if (found_pointer_gap
1284 || (dollar_first_arg_num
1285 && dollar_max_arg_used < dollar_arguments_count))
1287 res->number_other--;
1288 res->number_dollar_extra_args++;
1293 /* Retrieve the specification for a format flag. SPEC contains the
1294 specifications for format flags for the applicable kind of format.
1295 FLAG is the flag in question. If PREDICATES is NULL, the basic
1296 spec for that flag must be retrieved and must exist. If
1297 PREDICATES is not NULL, it is a string listing possible predicates
1298 for the spec entry; if an entry predicated on any of these is
1299 found, it is returned, otherwise NULL is returned. */
1301 static const format_flag_spec *
1302 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1304 int i;
1305 for (i = 0; spec[i].flag_char != 0; i++)
1307 if (spec[i].flag_char != flag)
1308 continue;
1309 if (predicates != NULL)
1311 if (spec[i].predicate != 0
1312 && strchr (predicates, spec[i].predicate) != 0)
1313 return &spec[i];
1315 else if (spec[i].predicate == 0)
1316 return &spec[i];
1318 gcc_assert (predicates);
1319 return NULL;
1323 /* Check the argument list of a call to printf, scanf, etc.
1324 INFO points to the function_format_info structure.
1325 PARAMS is the list of argument values. */
1327 static void
1328 check_format_info (function_format_info *info, tree params)
1330 format_check_context format_ctx;
1331 unsigned HOST_WIDE_INT arg_num;
1332 tree format_tree;
1333 format_check_results res;
1334 /* Skip to format argument. If the argument isn't available, there's
1335 no work for us to do; prototype checking will catch the problem. */
1336 for (arg_num = 1; ; ++arg_num)
1338 if (params == 0)
1339 return;
1340 if (arg_num == info->format_num)
1341 break;
1342 params = TREE_CHAIN (params);
1344 format_tree = TREE_VALUE (params);
1345 params = TREE_CHAIN (params);
1346 if (format_tree == 0)
1347 return;
1349 res.number_non_literal = 0;
1350 res.number_extra_args = 0;
1351 res.number_dollar_extra_args = 0;
1352 res.number_wide = 0;
1353 res.number_empty = 0;
1354 res.number_unterminated = 0;
1355 res.number_other = 0;
1357 format_ctx.res = &res;
1358 format_ctx.info = info;
1359 format_ctx.params = params;
1361 check_function_arguments_recurse (check_format_arg, &format_ctx,
1362 format_tree, arg_num);
1364 if (res.number_non_literal > 0)
1366 /* Functions taking a va_list normally pass a non-literal format
1367 string. These functions typically are declared with
1368 first_arg_num == 0, so avoid warning in those cases. */
1369 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1371 /* For strftime-like formats, warn for not checking the format
1372 string; but there are no arguments to check. */
1373 warning (OPT_Wformat_nonliteral,
1374 "format not a string literal, format string not checked");
1376 else if (info->first_arg_num != 0)
1378 /* If there are no arguments for the format at all, we may have
1379 printf (foo) which is likely to be a security hole. */
1380 while (arg_num + 1 < info->first_arg_num)
1382 if (params == 0)
1383 break;
1384 params = TREE_CHAIN (params);
1385 ++arg_num;
1387 if (params == 0 && warn_format_security)
1388 warning (OPT_Wformat_security,
1389 "format not a string literal and no format arguments");
1390 else if (params == 0 && warn_format_nonliteral)
1391 warning (OPT_Wformat_nonliteral,
1392 "format not a string literal and no format arguments");
1393 else
1394 warning (OPT_Wformat_nonliteral,
1395 "format not a string literal, argument types not checked");
1399 /* If there were extra arguments to the format, normally warn. However,
1400 the standard does say extra arguments are ignored, so in the specific
1401 case where we have multiple leaves (conditional expressions or
1402 ngettext) allow extra arguments if at least one leaf didn't have extra
1403 arguments, but was otherwise OK (either non-literal or checked OK).
1404 If the format is an empty string, this should be counted similarly to the
1405 case of extra format arguments. */
1406 if (res.number_extra_args > 0 && res.number_non_literal == 0
1407 && res.number_other == 0)
1408 warning (OPT_Wformat_extra_args, "too many arguments for format");
1409 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1410 && res.number_other == 0)
1411 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1412 if (res.number_empty > 0 && res.number_non_literal == 0
1413 && res.number_other == 0)
1414 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1415 format_types[info->format_type].name);
1417 if (res.number_wide > 0)
1418 warning (OPT_Wformat, "format is a wide character string");
1420 if (res.number_unterminated > 0)
1421 warning (OPT_Wformat, "unterminated format string");
1424 /* Callback from check_function_arguments_recurse to check a
1425 format string. FORMAT_TREE is the format parameter. ARG_NUM
1426 is the number of the format argument. CTX points to a
1427 format_check_context. */
1429 static void
1430 check_format_arg (void *ctx, tree format_tree,
1431 unsigned HOST_WIDE_INT arg_num)
1433 format_check_context *format_ctx = (format_check_context *) ctx;
1434 format_check_results *res = format_ctx->res;
1435 function_format_info *info = format_ctx->info;
1436 tree params = format_ctx->params;
1438 int format_length;
1439 HOST_WIDE_INT offset;
1440 const char *format_chars;
1441 tree array_size = 0;
1442 tree array_init;
1443 alloc_pool fwt_pool;
1445 if (integer_zerop (format_tree))
1447 /* Skip to first argument to check, so we can see if this format
1448 has any arguments (it shouldn't). */
1449 while (arg_num + 1 < info->first_arg_num)
1451 if (params == 0)
1452 return;
1453 params = TREE_CHAIN (params);
1454 ++arg_num;
1457 if (params == 0)
1458 res->number_other++;
1459 else
1460 res->number_extra_args++;
1462 return;
1465 offset = 0;
1466 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1468 tree arg0, arg1;
1470 arg0 = TREE_OPERAND (format_tree, 0);
1471 arg1 = TREE_OPERAND (format_tree, 1);
1472 STRIP_NOPS (arg0);
1473 STRIP_NOPS (arg1);
1474 if (TREE_CODE (arg1) == INTEGER_CST)
1475 format_tree = arg0;
1476 else
1478 res->number_non_literal++;
1479 return;
1481 if (!host_integerp (arg1, 0)
1482 || (offset = tree_low_cst (arg1, 0)) < 0)
1484 res->number_non_literal++;
1485 return;
1488 if (TREE_CODE (format_tree) != ADDR_EXPR)
1490 res->number_non_literal++;
1491 return;
1493 format_tree = TREE_OPERAND (format_tree, 0);
1494 if (format_types[info->format_type].flags
1495 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1497 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1498 /* We cannot examine this string here - but we can check that it is
1499 a valid type. */
1500 if (TREE_CODE (format_tree) != CONST_DECL
1501 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1502 || (*targetcm.string_object_ref_type_p)
1503 ((const_tree) TREE_TYPE (format_tree))))
1505 res->number_non_literal++;
1506 return;
1508 /* Skip to first argument to check. */
1509 while (arg_num + 1 < info->first_arg_num)
1511 if (params == 0)
1512 return;
1513 params = TREE_CHAIN (params);
1514 ++arg_num;
1516 /* So, we have a valid literal string object and one or more params.
1517 We need to use an external helper to parse the string into format
1518 info. For Objective-C variants we provide the resource within the
1519 objc tree, for target variants, via a hook. */
1520 if (objc_str)
1521 objc_check_format_arg (format_tree, params);
1522 else if (targetcm.check_string_object_format_arg)
1523 (*targetcm.check_string_object_format_arg) (format_tree, params);
1524 /* Else we can't handle it and retire quietly. */
1525 return;
1527 if (TREE_CODE (format_tree) == ARRAY_REF
1528 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1529 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1530 format_tree = TREE_OPERAND (format_tree, 0);
1531 if (TREE_CODE (format_tree) == VAR_DECL
1532 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1533 && (array_init = decl_constant_value (format_tree)) != format_tree
1534 && TREE_CODE (array_init) == STRING_CST)
1536 /* Extract the string constant initializer. Note that this may include
1537 a trailing NUL character that is not in the array (e.g.
1538 const char a[3] = "foo";). */
1539 array_size = DECL_SIZE_UNIT (format_tree);
1540 format_tree = array_init;
1542 if (TREE_CODE (format_tree) != STRING_CST)
1544 res->number_non_literal++;
1545 return;
1547 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1549 res->number_wide++;
1550 return;
1552 format_chars = TREE_STRING_POINTER (format_tree);
1553 format_length = TREE_STRING_LENGTH (format_tree);
1554 if (array_size != 0)
1556 /* Variable length arrays can't be initialized. */
1557 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1559 if (host_integerp (array_size, 0))
1561 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1562 if (array_size_value > 0
1563 && array_size_value == (int) array_size_value
1564 && format_length > array_size_value)
1565 format_length = array_size_value;
1568 if (offset)
1570 if (offset >= format_length)
1572 res->number_non_literal++;
1573 return;
1575 format_chars += offset;
1576 format_length -= offset;
1578 if (format_length < 1 || format_chars[--format_length] != 0)
1580 res->number_unterminated++;
1581 return;
1583 if (format_length == 0)
1585 res->number_empty++;
1586 return;
1589 /* Skip to first argument to check. */
1590 while (arg_num + 1 < info->first_arg_num)
1592 if (params == 0)
1593 return;
1594 params = TREE_CHAIN (params);
1595 ++arg_num;
1597 /* Provisionally increment res->number_other; check_format_info_main
1598 will decrement it if it finds there are extra arguments, but this way
1599 need not adjust it for every return. */
1600 res->number_other++;
1601 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1602 sizeof (format_wanted_type), 10);
1603 check_format_info_main (res, info, format_chars, format_length,
1604 params, arg_num, fwt_pool);
1605 free_alloc_pool (fwt_pool);
1609 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1610 is the NUL-terminated format string (which at this point may contain
1611 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1612 terminating NUL character). ARG_NUM is one less than the number of
1613 the first format argument to check; PARAMS points to that format
1614 argument in the list of arguments. */
1616 static void
1617 check_format_info_main (format_check_results *res,
1618 function_format_info *info, const char *format_chars,
1619 int format_length, tree params,
1620 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1622 const char *orig_format_chars = format_chars;
1623 tree first_fillin_param = params;
1625 const format_kind_info *fki = &format_types[info->format_type];
1626 const format_flag_spec *flag_specs = fki->flag_specs;
1627 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1629 /* -1 if no conversions taking an operand have been found; 0 if one has
1630 and it didn't use $; 1 if $ formats are in use. */
1631 int has_operand_number = -1;
1633 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1635 while (*format_chars != 0)
1637 int i;
1638 int suppressed = FALSE;
1639 const char *length_chars = NULL;
1640 enum format_lengths length_chars_val = FMT_LEN_none;
1641 enum format_std_version length_chars_std = STD_C89;
1642 int format_char;
1643 tree cur_param;
1644 tree wanted_type;
1645 int main_arg_num = 0;
1646 tree main_arg_params = 0;
1647 enum format_std_version wanted_type_std;
1648 const char *wanted_type_name;
1649 format_wanted_type width_wanted_type;
1650 format_wanted_type precision_wanted_type;
1651 format_wanted_type main_wanted_type;
1652 format_wanted_type *first_wanted_type = NULL;
1653 format_wanted_type *last_wanted_type = NULL;
1654 const format_length_info *fli = NULL;
1655 const format_char_info *fci = NULL;
1656 char flag_chars[256];
1657 int alloc_flag = 0;
1658 int scalar_identity_flag = 0;
1659 const char *format_start;
1661 if (*format_chars++ != '%')
1662 continue;
1663 if (*format_chars == 0)
1665 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
1666 continue;
1668 if (*format_chars == '%')
1670 ++format_chars;
1671 continue;
1673 flag_chars[0] = 0;
1675 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1677 /* Possibly read a $ operand number at the start of the format.
1678 If one was previously used, one is required here. If one
1679 is not used here, we can't immediately conclude this is a
1680 format without them, since it could be printf %m or scanf %*. */
1681 int opnum;
1682 opnum = maybe_read_dollar_number (&format_chars, 0,
1683 first_fillin_param,
1684 &main_arg_params, fki);
1685 if (opnum == -1)
1686 return;
1687 else if (opnum > 0)
1689 has_operand_number = 1;
1690 main_arg_num = opnum + info->first_arg_num - 1;
1693 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1695 if (avoid_dollar_number (format_chars))
1696 return;
1699 /* Read any format flags, but do not yet validate them beyond removing
1700 duplicates, since in general validation depends on the rest of
1701 the format. */
1702 while (*format_chars != 0
1703 && strchr (fki->flag_chars, *format_chars) != 0)
1705 const format_flag_spec *s = get_flag_spec (flag_specs,
1706 *format_chars, NULL);
1707 if (strchr (flag_chars, *format_chars) != 0)
1709 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1711 else
1713 i = strlen (flag_chars);
1714 flag_chars[i++] = *format_chars;
1715 flag_chars[i] = 0;
1717 if (s->skip_next_char)
1719 ++format_chars;
1720 if (*format_chars == 0)
1722 warning (OPT_Wformat, "missing fill character at end of strfmon format");
1723 return;
1726 ++format_chars;
1729 /* Read any format width, possibly * or *m$. */
1730 if (fki->width_char != 0)
1732 if (fki->width_type != NULL && *format_chars == '*')
1734 i = strlen (flag_chars);
1735 flag_chars[i++] = fki->width_char;
1736 flag_chars[i] = 0;
1737 /* "...a field width...may be indicated by an asterisk.
1738 In this case, an int argument supplies the field width..." */
1739 ++format_chars;
1740 if (has_operand_number != 0)
1742 int opnum;
1743 opnum = maybe_read_dollar_number (&format_chars,
1744 has_operand_number == 1,
1745 first_fillin_param,
1746 &params, fki);
1747 if (opnum == -1)
1748 return;
1749 else if (opnum > 0)
1751 has_operand_number = 1;
1752 arg_num = opnum + info->first_arg_num - 1;
1754 else
1755 has_operand_number = 0;
1757 else
1759 if (avoid_dollar_number (format_chars))
1760 return;
1762 if (info->first_arg_num != 0)
1764 if (params == 0)
1765 cur_param = NULL;
1766 else
1768 cur_param = TREE_VALUE (params);
1769 if (has_operand_number <= 0)
1771 params = TREE_CHAIN (params);
1772 ++arg_num;
1775 width_wanted_type.wanted_type = *fki->width_type;
1776 width_wanted_type.wanted_type_name = NULL;
1777 width_wanted_type.pointer_count = 0;
1778 width_wanted_type.char_lenient_flag = 0;
1779 width_wanted_type.scalar_identity_flag = 0;
1780 width_wanted_type.writing_in_flag = 0;
1781 width_wanted_type.reading_from_flag = 0;
1782 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1783 width_wanted_type.format_start = format_chars - 1;
1784 width_wanted_type.format_length = 1;
1785 width_wanted_type.param = cur_param;
1786 width_wanted_type.arg_num = arg_num;
1787 width_wanted_type.next = NULL;
1788 if (last_wanted_type != 0)
1789 last_wanted_type->next = &width_wanted_type;
1790 if (first_wanted_type == 0)
1791 first_wanted_type = &width_wanted_type;
1792 last_wanted_type = &width_wanted_type;
1795 else
1797 /* Possibly read a numeric width. If the width is zero,
1798 we complain if appropriate. */
1799 int non_zero_width_char = FALSE;
1800 int found_width = FALSE;
1801 while (ISDIGIT (*format_chars))
1803 found_width = TRUE;
1804 if (*format_chars != '0')
1805 non_zero_width_char = TRUE;
1806 ++format_chars;
1808 if (found_width && !non_zero_width_char &&
1809 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1810 warning (OPT_Wformat, "zero width in %s format", fki->name);
1811 if (found_width)
1813 i = strlen (flag_chars);
1814 flag_chars[i++] = fki->width_char;
1815 flag_chars[i] = 0;
1820 /* Read any format left precision (must be a number, not *). */
1821 if (fki->left_precision_char != 0 && *format_chars == '#')
1823 ++format_chars;
1824 i = strlen (flag_chars);
1825 flag_chars[i++] = fki->left_precision_char;
1826 flag_chars[i] = 0;
1827 if (!ISDIGIT (*format_chars))
1828 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
1829 while (ISDIGIT (*format_chars))
1830 ++format_chars;
1833 /* Read any format precision, possibly * or *m$. */
1834 if (fki->precision_char != 0 && *format_chars == '.')
1836 ++format_chars;
1837 i = strlen (flag_chars);
1838 flag_chars[i++] = fki->precision_char;
1839 flag_chars[i] = 0;
1840 if (fki->precision_type != NULL && *format_chars == '*')
1842 /* "...a...precision...may be indicated by an asterisk.
1843 In this case, an int argument supplies the...precision." */
1844 ++format_chars;
1845 if (has_operand_number != 0)
1847 int opnum;
1848 opnum = maybe_read_dollar_number (&format_chars,
1849 has_operand_number == 1,
1850 first_fillin_param,
1851 &params, fki);
1852 if (opnum == -1)
1853 return;
1854 else if (opnum > 0)
1856 has_operand_number = 1;
1857 arg_num = opnum + info->first_arg_num - 1;
1859 else
1860 has_operand_number = 0;
1862 else
1864 if (avoid_dollar_number (format_chars))
1865 return;
1867 if (info->first_arg_num != 0)
1869 if (params == 0)
1870 cur_param = NULL;
1871 else
1873 cur_param = TREE_VALUE (params);
1874 if (has_operand_number <= 0)
1876 params = TREE_CHAIN (params);
1877 ++arg_num;
1880 precision_wanted_type.wanted_type = *fki->precision_type;
1881 precision_wanted_type.wanted_type_name = NULL;
1882 precision_wanted_type.pointer_count = 0;
1883 precision_wanted_type.char_lenient_flag = 0;
1884 precision_wanted_type.scalar_identity_flag = 0;
1885 precision_wanted_type.writing_in_flag = 0;
1886 precision_wanted_type.reading_from_flag = 0;
1887 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1888 precision_wanted_type.param = cur_param;
1889 precision_wanted_type.format_start = format_chars - 2;
1890 precision_wanted_type.format_length = 2;
1891 precision_wanted_type.arg_num = arg_num;
1892 precision_wanted_type.next = NULL;
1893 if (last_wanted_type != 0)
1894 last_wanted_type->next = &precision_wanted_type;
1895 if (first_wanted_type == 0)
1896 first_wanted_type = &precision_wanted_type;
1897 last_wanted_type = &precision_wanted_type;
1900 else
1902 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1903 && !ISDIGIT (*format_chars))
1904 warning (OPT_Wformat, "empty precision in %s format", fki->name);
1905 while (ISDIGIT (*format_chars))
1906 ++format_chars;
1910 format_start = format_chars;
1911 if (fki->alloc_char && fki->alloc_char == *format_chars)
1913 i = strlen (flag_chars);
1914 flag_chars[i++] = fki->alloc_char;
1915 flag_chars[i] = 0;
1916 format_chars++;
1919 /* Handle the scanf allocation kludge. */
1920 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1922 if (*format_chars == 'a' && !flag_isoc99)
1924 if (format_chars[1] == 's' || format_chars[1] == 'S'
1925 || format_chars[1] == '[')
1927 /* 'a' is used as a flag. */
1928 i = strlen (flag_chars);
1929 flag_chars[i++] = 'a';
1930 flag_chars[i] = 0;
1931 format_chars++;
1936 /* Read any length modifier, if this kind of format has them. */
1937 fli = fki->length_char_specs;
1938 length_chars = NULL;
1939 length_chars_val = FMT_LEN_none;
1940 length_chars_std = STD_C89;
1941 scalar_identity_flag = 0;
1942 if (fli)
1944 while (fli->name != 0
1945 && strncmp (fli->name, format_chars, strlen (fli->name)))
1946 fli++;
1947 if (fli->name != 0)
1949 format_chars += strlen (fli->name);
1950 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1952 format_chars++;
1953 length_chars = fli->double_name;
1954 length_chars_val = fli->double_index;
1955 length_chars_std = fli->double_std;
1957 else
1959 length_chars = fli->name;
1960 length_chars_val = fli->index;
1961 length_chars_std = fli->std;
1962 scalar_identity_flag = fli->scalar_identity_flag;
1964 i = strlen (flag_chars);
1965 flag_chars[i++] = fki->length_code_char;
1966 flag_chars[i] = 0;
1968 if (pedantic)
1970 /* Warn if the length modifier is non-standard. */
1971 if (ADJ_STD (length_chars_std) > C_STD_VER)
1972 warning (OPT_Wformat,
1973 "%s does not support the %qs %s length modifier",
1974 C_STD_NAME (length_chars_std), length_chars,
1975 fki->name);
1979 /* Read any modifier (strftime E/O). */
1980 if (fki->modifier_chars != NULL)
1982 while (*format_chars != 0
1983 && strchr (fki->modifier_chars, *format_chars) != 0)
1985 if (strchr (flag_chars, *format_chars) != 0)
1987 const format_flag_spec *s = get_flag_spec (flag_specs,
1988 *format_chars, NULL);
1989 warning (OPT_Wformat, "repeated %s in format", _(s->name));
1991 else
1993 i = strlen (flag_chars);
1994 flag_chars[i++] = *format_chars;
1995 flag_chars[i] = 0;
1997 ++format_chars;
2001 format_char = *format_chars;
2002 if (format_char == 0
2003 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2004 && format_char == '%'))
2006 warning (OPT_Wformat, "conversion lacks type at end of format");
2007 continue;
2009 format_chars++;
2010 fci = fki->conversion_specs;
2011 while (fci->format_chars != 0
2012 && strchr (fci->format_chars, format_char) == 0)
2013 ++fci;
2014 if (fci->format_chars == 0)
2016 if (ISGRAPH (format_char))
2017 warning (OPT_Wformat, "unknown conversion type character %qc in format",
2018 format_char);
2019 else
2020 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
2021 format_char);
2022 continue;
2024 if (pedantic)
2026 if (ADJ_STD (fci->std) > C_STD_VER)
2027 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
2028 C_STD_NAME (fci->std), format_char, fki->name);
2031 /* Validate the individual flags used, removing any that are invalid. */
2033 int d = 0;
2034 for (i = 0; flag_chars[i] != 0; i++)
2036 const format_flag_spec *s = get_flag_spec (flag_specs,
2037 flag_chars[i], NULL);
2038 flag_chars[i - d] = flag_chars[i];
2039 if (flag_chars[i] == fki->length_code_char)
2040 continue;
2041 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2043 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
2044 _(s->name), format_char, fki->name);
2045 d++;
2046 continue;
2048 if (pedantic)
2050 const format_flag_spec *t;
2051 if (ADJ_STD (s->std) > C_STD_VER)
2052 warning (OPT_Wformat, "%s does not support %s",
2053 C_STD_NAME (s->std), _(s->long_name));
2054 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2055 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2057 const char *long_name = (t->long_name != NULL
2058 ? t->long_name
2059 : s->long_name);
2060 if (ADJ_STD (t->std) > C_STD_VER)
2061 warning (OPT_Wformat,
2062 "%s does not support %s with the %<%%%c%> %s format",
2063 C_STD_NAME (t->std), _(long_name),
2064 format_char, fki->name);
2068 flag_chars[i - d] = 0;
2071 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2072 && strchr (flag_chars, 'a') != 0)
2073 alloc_flag = 1;
2074 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2075 alloc_flag = 1;
2077 if (fki->suppression_char
2078 && strchr (flag_chars, fki->suppression_char) != 0)
2079 suppressed = 1;
2081 /* Validate the pairs of flags used. */
2082 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2084 const format_flag_spec *s, *t;
2085 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2086 continue;
2087 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2088 continue;
2089 if (bad_flag_pairs[i].predicate != 0
2090 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2091 continue;
2092 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2093 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2094 if (bad_flag_pairs[i].ignored)
2096 if (bad_flag_pairs[i].predicate != 0)
2097 warning (OPT_Wformat,
2098 "%s ignored with %s and %<%%%c%> %s format",
2099 _(s->name), _(t->name), format_char,
2100 fki->name);
2101 else
2102 warning (OPT_Wformat, "%s ignored with %s in %s format",
2103 _(s->name), _(t->name), fki->name);
2105 else
2107 if (bad_flag_pairs[i].predicate != 0)
2108 warning (OPT_Wformat,
2109 "use of %s and %s together with %<%%%c%> %s format",
2110 _(s->name), _(t->name), format_char,
2111 fki->name);
2112 else
2113 warning (OPT_Wformat, "use of %s and %s together in %s format",
2114 _(s->name), _(t->name), fki->name);
2118 /* Give Y2K warnings. */
2119 if (warn_format_y2k)
2121 int y2k_level = 0;
2122 if (strchr (fci->flags2, '4') != 0)
2123 if (strchr (flag_chars, 'E') != 0)
2124 y2k_level = 3;
2125 else
2126 y2k_level = 2;
2127 else if (strchr (fci->flags2, '3') != 0)
2128 y2k_level = 3;
2129 else if (strchr (fci->flags2, '2') != 0)
2130 y2k_level = 2;
2131 if (y2k_level == 3)
2132 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2133 "year in some locales", format_char);
2134 else if (y2k_level == 2)
2135 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2136 "year", format_char);
2139 if (strchr (fci->flags2, '[') != 0)
2141 /* Skip over scan set, in case it happens to have '%' in it. */
2142 if (*format_chars == '^')
2143 ++format_chars;
2144 /* Find closing bracket; if one is hit immediately, then
2145 it's part of the scan set rather than a terminator. */
2146 if (*format_chars == ']')
2147 ++format_chars;
2148 while (*format_chars && *format_chars != ']')
2149 ++format_chars;
2150 if (*format_chars != ']')
2151 /* The end of the format string was reached. */
2152 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
2155 wanted_type = 0;
2156 wanted_type_name = 0;
2157 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2159 wanted_type = (fci->types[length_chars_val].type
2160 ? *fci->types[length_chars_val].type : 0);
2161 wanted_type_name = fci->types[length_chars_val].name;
2162 wanted_type_std = fci->types[length_chars_val].std;
2163 if (wanted_type == 0)
2165 warning (OPT_Wformat,
2166 "use of %qs length modifier with %qc type character",
2167 length_chars, format_char);
2168 /* Heuristic: skip one argument when an invalid length/type
2169 combination is encountered. */
2170 arg_num++;
2171 if (params != 0)
2172 params = TREE_CHAIN (params);
2173 continue;
2175 else if (pedantic
2176 /* Warn if non-standard, provided it is more non-standard
2177 than the length and type characters that may already
2178 have been warned for. */
2179 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2180 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2182 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2183 warning (OPT_Wformat,
2184 "%s does not support the %<%%%s%c%> %s format",
2185 C_STD_NAME (wanted_type_std), length_chars,
2186 format_char, fki->name);
2190 main_wanted_type.next = NULL;
2192 /* Finally. . .check type of argument against desired type! */
2193 if (info->first_arg_num == 0)
2194 continue;
2195 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2196 || suppressed)
2198 if (main_arg_num != 0)
2200 if (suppressed)
2201 warning (OPT_Wformat, "operand number specified with "
2202 "suppressed assignment");
2203 else
2204 warning (OPT_Wformat, "operand number specified for format "
2205 "taking no argument");
2208 else
2210 format_wanted_type *wanted_type_ptr;
2212 if (main_arg_num != 0)
2214 arg_num = main_arg_num;
2215 params = main_arg_params;
2217 else
2219 ++arg_num;
2220 if (has_operand_number > 0)
2222 warning (OPT_Wformat, "missing $ operand number in format");
2223 return;
2225 else
2226 has_operand_number = 0;
2229 wanted_type_ptr = &main_wanted_type;
2230 while (fci)
2232 if (params == 0)
2233 cur_param = NULL;
2234 else
2236 cur_param = TREE_VALUE (params);
2237 params = TREE_CHAIN (params);
2240 wanted_type_ptr->wanted_type = wanted_type;
2241 wanted_type_ptr->wanted_type_name = wanted_type_name;
2242 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2243 wanted_type_ptr->char_lenient_flag = 0;
2244 if (strchr (fci->flags2, 'c') != 0)
2245 wanted_type_ptr->char_lenient_flag = 1;
2246 wanted_type_ptr->scalar_identity_flag = 0;
2247 if (scalar_identity_flag)
2248 wanted_type_ptr->scalar_identity_flag = 1;
2249 wanted_type_ptr->writing_in_flag = 0;
2250 wanted_type_ptr->reading_from_flag = 0;
2251 if (alloc_flag)
2252 wanted_type_ptr->writing_in_flag = 1;
2253 else
2255 if (strchr (fci->flags2, 'W') != 0)
2256 wanted_type_ptr->writing_in_flag = 1;
2257 if (strchr (fci->flags2, 'R') != 0)
2258 wanted_type_ptr->reading_from_flag = 1;
2260 wanted_type_ptr->kind = CF_KIND_FORMAT;
2261 wanted_type_ptr->param = cur_param;
2262 wanted_type_ptr->arg_num = arg_num;
2263 wanted_type_ptr->format_start = format_start;
2264 wanted_type_ptr->format_length = format_chars - format_start;
2265 wanted_type_ptr->next = NULL;
2266 if (last_wanted_type != 0)
2267 last_wanted_type->next = wanted_type_ptr;
2268 if (first_wanted_type == 0)
2269 first_wanted_type = wanted_type_ptr;
2270 last_wanted_type = wanted_type_ptr;
2272 fci = fci->chain;
2273 if (fci)
2275 wanted_type_ptr = (format_wanted_type *)
2276 pool_alloc (fwt_pool);
2277 arg_num++;
2278 wanted_type = *fci->types[length_chars_val].type;
2279 wanted_type_name = fci->types[length_chars_val].name;
2284 if (first_wanted_type != 0)
2285 check_format_types (first_wanted_type);
2288 if (format_chars - orig_format_chars != format_length)
2289 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
2290 if (info->first_arg_num != 0 && params != 0
2291 && has_operand_number <= 0)
2293 res->number_other--;
2294 res->number_extra_args++;
2296 if (has_operand_number > 0)
2297 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2301 /* Check the argument types from a single format conversion (possibly
2302 including width and precision arguments). */
2303 static void
2304 check_format_types (format_wanted_type *types)
2306 for (; types != 0; types = types->next)
2308 tree cur_param;
2309 tree cur_type;
2310 tree orig_cur_type;
2311 tree wanted_type;
2312 int arg_num;
2313 int i;
2314 int char_type_flag;
2316 wanted_type = types->wanted_type;
2317 arg_num = types->arg_num;
2319 /* The following should not occur here. */
2320 gcc_assert (wanted_type);
2321 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2323 if (types->pointer_count == 0)
2324 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2326 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2328 cur_param = types->param;
2329 if (!cur_param)
2331 format_type_warning (types, wanted_type, NULL);
2332 continue;
2335 cur_type = TREE_TYPE (cur_param);
2336 if (cur_type == error_mark_node)
2337 continue;
2338 orig_cur_type = cur_type;
2339 char_type_flag = 0;
2341 STRIP_NOPS (cur_param);
2343 /* Check the types of any additional pointer arguments
2344 that precede the "real" argument. */
2345 for (i = 0; i < types->pointer_count; ++i)
2347 if (TREE_CODE (cur_type) == POINTER_TYPE)
2349 cur_type = TREE_TYPE (cur_type);
2350 if (cur_type == error_mark_node)
2351 break;
2353 /* Check for writing through a NULL pointer. */
2354 if (types->writing_in_flag
2355 && i == 0
2356 && cur_param != 0
2357 && integer_zerop (cur_param))
2358 warning (OPT_Wformat, "writing through null pointer "
2359 "(argument %d)", arg_num);
2361 /* Check for reading through a NULL pointer. */
2362 if (types->reading_from_flag
2363 && i == 0
2364 && cur_param != 0
2365 && integer_zerop (cur_param))
2366 warning (OPT_Wformat, "reading through null pointer "
2367 "(argument %d)", arg_num);
2369 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2370 cur_param = TREE_OPERAND (cur_param, 0);
2371 else
2372 cur_param = 0;
2374 /* See if this is an attempt to write into a const type with
2375 scanf or with printf "%n". Note: the writing in happens
2376 at the first indirection only, if for example
2377 void * const * is passed to scanf %p; passing
2378 const void ** is simply passing an incompatible type. */
2379 if (types->writing_in_flag
2380 && i == 0
2381 && (TYPE_READONLY (cur_type)
2382 || (cur_param != 0
2383 && (CONSTANT_CLASS_P (cur_param)
2384 || (DECL_P (cur_param)
2385 && TREE_READONLY (cur_param))))))
2386 warning (OPT_Wformat, "writing into constant object "
2387 "(argument %d)", arg_num);
2389 /* If there are extra type qualifiers beyond the first
2390 indirection, then this makes the types technically
2391 incompatible. */
2392 if (i > 0
2393 && pedantic
2394 && (TYPE_READONLY (cur_type)
2395 || TYPE_VOLATILE (cur_type)
2396 || TYPE_RESTRICT (cur_type)))
2397 warning (OPT_Wformat, "extra type qualifiers in format "
2398 "argument (argument %d)",
2399 arg_num);
2402 else
2404 format_type_warning (types, wanted_type, orig_cur_type);
2405 break;
2409 if (i < types->pointer_count)
2410 continue;
2412 cur_type = TYPE_MAIN_VARIANT (cur_type);
2414 /* Check whether the argument type is a character type. This leniency
2415 only applies to certain formats, flagged with 'c'.
2417 if (types->char_lenient_flag)
2418 char_type_flag = (cur_type == char_type_node
2419 || cur_type == signed_char_type_node
2420 || cur_type == unsigned_char_type_node);
2422 /* Check the type of the "real" argument, if there's a type we want. */
2423 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2424 continue;
2425 /* If we want 'void *', allow any pointer type.
2426 (Anything else would already have got a warning.)
2427 With -Wpedantic, only allow pointers to void and to character
2428 types. */
2429 if (wanted_type == void_type_node
2430 && (!pedantic || (i == 1 && char_type_flag)))
2431 continue;
2432 /* Don't warn about differences merely in signedness, unless
2433 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2434 target and not a character type, and for character types at
2435 a second level of indirection. */
2436 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2437 && TREE_CODE (cur_type) == INTEGER_TYPE
2438 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2439 && (TYPE_UNSIGNED (wanted_type)
2440 ? wanted_type == c_common_unsigned_type (cur_type)
2441 : wanted_type == c_common_signed_type (cur_type)))
2442 continue;
2443 /* Likewise, "signed char", "unsigned char" and "char" are
2444 equivalent but the above test won't consider them equivalent. */
2445 if (wanted_type == char_type_node
2446 && (!pedantic || i < 2)
2447 && char_type_flag)
2448 continue;
2449 if (types->scalar_identity_flag
2450 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2451 || (INTEGRAL_TYPE_P (cur_type)
2452 && INTEGRAL_TYPE_P (wanted_type)))
2453 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2454 continue;
2455 /* Now we have a type mismatch. */
2456 format_type_warning (types, wanted_type, orig_cur_type);
2461 /* Give a warning about a format argument of different type from that
2462 expected. WANTED_TYPE is the type the argument should have, possibly
2463 stripped of pointer dereferences. The description (such as "field
2464 precision"), the placement in the format string, a possibly more
2465 friendly name of WANTED_TYPE, and the number of pointer dereferences
2466 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2467 or NULL if it is missing. */
2468 static void
2469 format_type_warning (format_wanted_type *type, tree wanted_type, tree arg_type)
2471 int kind = type->kind;
2472 const char *wanted_type_name = type->wanted_type_name;
2473 const char *format_start = type->format_start;
2474 int format_length = type->format_length;
2475 int pointer_count = type->pointer_count;
2476 int arg_num = type->arg_num;
2478 char *p;
2479 /* If ARG_TYPE is a typedef with a misleading name (for example,
2480 size_t but not the standard size_t expected by printf %zu), avoid
2481 printing the typedef name. */
2482 if (wanted_type_name
2483 && arg_type
2484 && TYPE_NAME (arg_type)
2485 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2486 && DECL_NAME (TYPE_NAME (arg_type))
2487 && !strcmp (wanted_type_name,
2488 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2489 arg_type = TYPE_MAIN_VARIANT (arg_type);
2490 /* The format type and name exclude any '*' for pointers, so those
2491 must be formatted manually. For all the types we currently have,
2492 this is adequate, but formats taking pointers to functions or
2493 arrays would require the full type to be built up in order to
2494 print it with %T. */
2495 p = (char *) alloca (pointer_count + 2);
2496 if (pointer_count == 0)
2497 p[0] = 0;
2498 else if (c_dialect_cxx ())
2500 memset (p, '*', pointer_count);
2501 p[pointer_count] = 0;
2503 else
2505 p[0] = ' ';
2506 memset (p + 1, '*', pointer_count);
2507 p[pointer_count + 1] = 0;
2510 if (wanted_type_name)
2512 if (arg_type)
2513 warning (OPT_Wformat, "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2514 "but argument %d has type %qT",
2515 gettext (kind_descriptions[kind]),
2516 (kind == CF_KIND_FORMAT ? "%" : ""),
2517 format_length, format_start,
2518 wanted_type_name, p, arg_num, arg_type);
2519 else
2520 warning (OPT_Wformat, "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2521 gettext (kind_descriptions[kind]),
2522 (kind == CF_KIND_FORMAT ? "%" : ""),
2523 format_length, format_start, wanted_type_name, p);
2525 else
2527 if (arg_type)
2528 warning (OPT_Wformat, "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2529 "but argument %d has type %qT",
2530 gettext (kind_descriptions[kind]),
2531 (kind == CF_KIND_FORMAT ? "%" : ""),
2532 format_length, format_start,
2533 wanted_type, p, arg_num, arg_type);
2534 else
2535 warning (OPT_Wformat, "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2536 gettext (kind_descriptions[kind]),
2537 (kind == CF_KIND_FORMAT ? "%" : ""),
2538 format_length, format_start, wanted_type, p);
2543 /* Given a format_char_info array FCI, and a character C, this function
2544 returns the index into the conversion_specs where that specifier's
2545 data is located. The character must exist. */
2546 static unsigned int
2547 find_char_info_specifier_index (const format_char_info *fci, int c)
2549 unsigned i;
2551 for (i = 0; fci->format_chars; i++, fci++)
2552 if (strchr (fci->format_chars, c))
2553 return i;
2555 /* We shouldn't be looking for a non-existent specifier. */
2556 gcc_unreachable ();
2559 /* Given a format_length_info array FLI, and a character C, this
2560 function returns the index into the conversion_specs where that
2561 modifier's data is located. The character must exist. */
2562 static unsigned int
2563 find_length_info_modifier_index (const format_length_info *fli, int c)
2565 unsigned i;
2567 for (i = 0; fli->name; i++, fli++)
2568 if (strchr (fli->name, c))
2569 return i;
2571 /* We shouldn't be looking for a non-existent modifier. */
2572 gcc_unreachable ();
2575 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2576 use in GCC's __asm_fprintf__ custom format attribute. You must
2577 have set dynamic_format_types before calling this function. */
2578 static void
2579 init_dynamic_asm_fprintf_info (void)
2581 static tree hwi;
2583 if (!hwi)
2585 format_length_info *new_asm_fprintf_length_specs;
2586 unsigned int i;
2588 /* Find the underlying type for HOST_WIDE_INT. For the %w
2589 length modifier to work, one must have issued: "typedef
2590 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2591 prior to using that modifier. */
2592 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2593 if (!hwi)
2595 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2596 return;
2598 hwi = identifier_global_value (hwi);
2599 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2601 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2602 return;
2604 hwi = DECL_ORIGINAL_TYPE (hwi);
2605 gcc_assert (hwi);
2606 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2608 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2609 " or %<long long%>");
2610 return;
2613 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2614 new_asm_fprintf_length_specs = (format_length_info *)
2615 xmemdup (asm_fprintf_length_specs,
2616 sizeof (asm_fprintf_length_specs),
2617 sizeof (asm_fprintf_length_specs));
2619 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2620 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2621 if (hwi == long_integer_type_node)
2622 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2623 else if (hwi == long_long_integer_type_node)
2624 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2625 else
2626 gcc_unreachable ();
2628 /* Assign the new data for use. */
2629 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2630 new_asm_fprintf_length_specs;
2634 /* Determine the type of a "locus" in the code being compiled for use
2635 in GCC's __gcc_gfc__ custom format attribute. You must have set
2636 dynamic_format_types before calling this function. */
2637 static void
2638 init_dynamic_gfc_info (void)
2640 static tree locus;
2642 if (!locus)
2644 static format_char_info *gfc_fci;
2646 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2647 must have declared 'locus' prior to using this attribute. If
2648 we haven't seen this declarations then you shouldn't use the
2649 specifier requiring that type. */
2650 if ((locus = maybe_get_identifier ("locus")))
2652 locus = identifier_global_value (locus);
2653 if (locus)
2655 if (TREE_CODE (locus) != TYPE_DECL
2656 || TREE_TYPE (locus) == error_mark_node)
2658 error ("%<locus%> is not defined as a type");
2659 locus = 0;
2661 else
2662 locus = TREE_TYPE (locus);
2666 /* Assign the new data for use. */
2668 /* Handle the __gcc_gfc__ format specifics. */
2669 if (!gfc_fci)
2670 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2671 gfc_fci = (format_char_info *)
2672 xmemdup (gcc_gfc_char_table,
2673 sizeof (gcc_gfc_char_table),
2674 sizeof (gcc_gfc_char_table));
2675 if (locus)
2677 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2678 gfc_fci[i].types[0].type = &locus;
2679 gfc_fci[i].pointer_count = 1;
2684 /* Determine the types of "tree" and "location_t" in the code being
2685 compiled for use in GCC's diagnostic custom format attributes. You
2686 must have set dynamic_format_types before calling this function. */
2687 static void
2688 init_dynamic_diag_info (void)
2690 static tree t, loc, hwi;
2692 if (!loc || !t || !hwi)
2694 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2695 static format_length_info *diag_ls;
2696 unsigned int i;
2698 /* For the GCC-diagnostics custom format specifiers to work, one
2699 must have declared 'tree' and/or 'location_t' prior to using
2700 those attributes. If we haven't seen these declarations then
2701 you shouldn't use the specifiers requiring these types.
2702 However we don't force a hard ICE because we may see only one
2703 or the other type. */
2704 if ((loc = maybe_get_identifier ("location_t")))
2706 loc = identifier_global_value (loc);
2707 if (loc)
2709 if (TREE_CODE (loc) != TYPE_DECL)
2711 error ("%<location_t%> is not defined as a type");
2712 loc = 0;
2714 else
2715 loc = TREE_TYPE (loc);
2719 /* We need to grab the underlying 'union tree_node' so peek into
2720 an extra type level. */
2721 if ((t = maybe_get_identifier ("tree")))
2723 t = identifier_global_value (t);
2724 if (t)
2726 if (TREE_CODE (t) != TYPE_DECL)
2728 error ("%<tree%> is not defined as a type");
2729 t = 0;
2731 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2733 error ("%<tree%> is not defined as a pointer type");
2734 t = 0;
2736 else
2737 t = TREE_TYPE (TREE_TYPE (t));
2741 /* Find the underlying type for HOST_WIDE_INT. For the %w
2742 length modifier to work, one must have issued: "typedef
2743 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2744 prior to using that modifier. */
2745 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2747 hwi = identifier_global_value (hwi);
2748 if (hwi)
2750 if (TREE_CODE (hwi) != TYPE_DECL)
2752 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2753 hwi = 0;
2755 else
2757 hwi = DECL_ORIGINAL_TYPE (hwi);
2758 gcc_assert (hwi);
2759 if (hwi != long_integer_type_node
2760 && hwi != long_long_integer_type_node)
2762 error ("%<__gcc_host_wide_int__%> is not defined"
2763 " as %<long%> or %<long long%>");
2764 hwi = 0;
2770 /* Assign the new data for use. */
2772 /* All the GCC diag formats use the same length specs. */
2773 if (!diag_ls)
2774 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2775 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2776 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2777 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2778 diag_ls = (format_length_info *)
2779 xmemdup (gcc_diag_length_specs,
2780 sizeof (gcc_diag_length_specs),
2781 sizeof (gcc_diag_length_specs));
2782 if (hwi)
2784 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2785 i = find_length_info_modifier_index (diag_ls, 'w');
2786 if (hwi == long_integer_type_node)
2787 diag_ls[i].index = FMT_LEN_l;
2788 else if (hwi == long_long_integer_type_node)
2789 diag_ls[i].index = FMT_LEN_ll;
2790 else
2791 gcc_unreachable ();
2794 /* Handle the __gcc_diag__ format specifics. */
2795 if (!diag_fci)
2796 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2797 diag_fci = (format_char_info *)
2798 xmemdup (gcc_diag_char_table,
2799 sizeof (gcc_diag_char_table),
2800 sizeof (gcc_diag_char_table));
2801 if (t)
2803 i = find_char_info_specifier_index (diag_fci, 'K');
2804 diag_fci[i].types[0].type = &t;
2805 diag_fci[i].pointer_count = 1;
2808 /* Handle the __gcc_tdiag__ format specifics. */
2809 if (!tdiag_fci)
2810 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2811 tdiag_fci = (format_char_info *)
2812 xmemdup (gcc_tdiag_char_table,
2813 sizeof (gcc_tdiag_char_table),
2814 sizeof (gcc_tdiag_char_table));
2815 if (t)
2817 /* All specifiers taking a tree share the same struct. */
2818 i = find_char_info_specifier_index (tdiag_fci, 'D');
2819 tdiag_fci[i].types[0].type = &t;
2820 tdiag_fci[i].pointer_count = 1;
2821 i = find_char_info_specifier_index (tdiag_fci, 'K');
2822 tdiag_fci[i].types[0].type = &t;
2823 tdiag_fci[i].pointer_count = 1;
2826 /* Handle the __gcc_cdiag__ format specifics. */
2827 if (!cdiag_fci)
2828 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2829 cdiag_fci = (format_char_info *)
2830 xmemdup (gcc_cdiag_char_table,
2831 sizeof (gcc_cdiag_char_table),
2832 sizeof (gcc_cdiag_char_table));
2833 if (t)
2835 /* All specifiers taking a tree share the same struct. */
2836 i = find_char_info_specifier_index (cdiag_fci, 'D');
2837 cdiag_fci[i].types[0].type = &t;
2838 cdiag_fci[i].pointer_count = 1;
2839 i = find_char_info_specifier_index (cdiag_fci, 'K');
2840 cdiag_fci[i].types[0].type = &t;
2841 cdiag_fci[i].pointer_count = 1;
2844 /* Handle the __gcc_cxxdiag__ format specifics. */
2845 if (!cxxdiag_fci)
2846 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2847 cxxdiag_fci = (format_char_info *)
2848 xmemdup (gcc_cxxdiag_char_table,
2849 sizeof (gcc_cxxdiag_char_table),
2850 sizeof (gcc_cxxdiag_char_table));
2851 if (t)
2853 /* All specifiers taking a tree share the same struct. */
2854 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2855 cxxdiag_fci[i].types[0].type = &t;
2856 cxxdiag_fci[i].pointer_count = 1;
2857 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2858 cxxdiag_fci[i].types[0].type = &t;
2859 cxxdiag_fci[i].pointer_count = 1;
2864 #ifdef TARGET_FORMAT_TYPES
2865 extern const format_kind_info TARGET_FORMAT_TYPES[];
2866 #endif
2868 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2869 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2870 #endif
2871 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2872 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2873 #endif
2875 /* Attributes such as "printf" are equivalent to those such as
2876 "gnu_printf" unless this is overridden by a target. */
2877 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2879 { "gnu_printf", "printf" },
2880 { "gnu_scanf", "scanf" },
2881 { "gnu_strftime", "strftime" },
2882 { "gnu_strfmon", "strfmon" },
2883 { NULL, NULL }
2886 /* Translate to unified attribute name. This is used in decode_format_type and
2887 decode_format_attr. In attr_name the user specified argument is passed. It
2888 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2889 or the attr_name passed to this function, if there is no matching entry. */
2890 static const char *
2891 convert_format_name_to_system_name (const char *attr_name)
2893 int i;
2895 if (attr_name == NULL || *attr_name == 0
2896 || strncmp (attr_name, "gcc_", 4) == 0)
2897 return attr_name;
2898 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2899 TARGET_OVERRIDES_FORMAT_INIT ();
2900 #endif
2902 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2903 /* Check if format attribute is overridden by target. */
2904 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2905 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2907 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2909 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2910 attr_name))
2911 return attr_name;
2912 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2913 attr_name))
2914 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2917 #endif
2918 /* Otherwise default to gnu format. */
2919 for (i = 0;
2920 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2921 ++i)
2923 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2924 attr_name))
2925 return attr_name;
2926 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2927 attr_name))
2928 return gnu_target_overrides_format_attributes[i].named_attr_src;
2931 return attr_name;
2934 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2935 counting "name" and "__name__" as the same, false otherwise. */
2936 static bool
2937 cmp_attribs (const char *tattr_name, const char *attr_name)
2939 int alen = strlen (attr_name);
2940 int slen = (tattr_name ? strlen (tattr_name) : 0);
2941 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2942 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2944 attr_name += 2;
2945 alen -= 4;
2947 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2948 return false;
2949 return true;
2952 /* Handle a "format" attribute; arguments as in
2953 struct attribute_spec.handler. */
2954 tree
2955 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2956 int flags, bool *no_add_attrs)
2958 tree type = *node;
2959 function_format_info info;
2961 #ifdef TARGET_FORMAT_TYPES
2962 /* If the target provides additional format types, we need to
2963 add them to FORMAT_TYPES at first use. */
2964 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2966 dynamic_format_types = XNEWVEC (format_kind_info,
2967 n_format_types + TARGET_N_FORMAT_TYPES);
2968 memcpy (dynamic_format_types, format_types_orig,
2969 sizeof (format_types_orig));
2970 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2971 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2973 format_types = dynamic_format_types;
2974 /* Provide a reference for the first potential external type. */
2975 first_target_format_type = n_format_types;
2976 n_format_types += TARGET_N_FORMAT_TYPES;
2978 #endif
2980 if (!decode_format_attr (args, &info, 0))
2982 *no_add_attrs = true;
2983 return NULL_TREE;
2986 if (prototype_p (type))
2988 if (!check_format_string (type, info.format_num, flags,
2989 no_add_attrs, info.format_type))
2990 return NULL_TREE;
2992 if (info.first_arg_num != 0)
2994 unsigned HOST_WIDE_INT arg_num = 1;
2995 function_args_iterator iter;
2996 tree arg_type;
2998 /* Verify that first_arg_num points to the last arg,
2999 the ... */
3000 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
3001 arg_num++;
3003 if (arg_num != info.first_arg_num)
3005 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
3006 error ("args to be formatted is not %<...%>");
3007 *no_add_attrs = true;
3008 return NULL_TREE;
3013 /* Check if this is a strftime variant. Just for this variant
3014 FMT_FLAG_ARG_CONVERT is not set. */
3015 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3016 && info.first_arg_num != 0)
3018 error ("strftime formats cannot format arguments");
3019 *no_add_attrs = true;
3020 return NULL_TREE;
3023 /* If this is a custom GCC-internal format type, we have to
3024 initialize certain bits at runtime. */
3025 if (info.format_type == asm_fprintf_format_type
3026 || info.format_type == gcc_gfc_format_type
3027 || info.format_type == gcc_diag_format_type
3028 || info.format_type == gcc_tdiag_format_type
3029 || info.format_type == gcc_cdiag_format_type
3030 || info.format_type == gcc_cxxdiag_format_type)
3032 /* Our first time through, we have to make sure that our
3033 format_type data is allocated dynamically and is modifiable. */
3034 if (!dynamic_format_types)
3035 format_types = dynamic_format_types = (format_kind_info *)
3036 xmemdup (format_types_orig, sizeof (format_types_orig),
3037 sizeof (format_types_orig));
3039 /* If this is format __asm_fprintf__, we have to initialize
3040 GCC's notion of HOST_WIDE_INT for checking %wd. */
3041 if (info.format_type == asm_fprintf_format_type)
3042 init_dynamic_asm_fprintf_info ();
3043 /* If this is format __gcc_gfc__, we have to initialize GCC's
3044 notion of 'locus' at runtime for %L. */
3045 else if (info.format_type == gcc_gfc_format_type)
3046 init_dynamic_gfc_info ();
3047 /* If this is one of the diagnostic attributes, then we have to
3048 initialize 'location_t' and 'tree' at runtime. */
3049 else if (info.format_type == gcc_diag_format_type
3050 || info.format_type == gcc_tdiag_format_type
3051 || info.format_type == gcc_cdiag_format_type
3052 || info.format_type == gcc_cxxdiag_format_type)
3053 init_dynamic_diag_info ();
3054 else
3055 gcc_unreachable ();
3058 return NULL_TREE;