Daily bump.
[official-gcc.git] / gcc / c-family / c-format.c
blob6b0bbce84f10477ce73420f9641e31d3111f4152
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stringpool.h"
26 #include "flags.h"
27 #include "c-common.h"
28 #include "c-objc.h"
29 #include "intl.h"
30 #include "diagnostic-core.h"
31 #include "langhooks.h"
32 #include "c-format.h"
33 #include "alloc-pool.h"
34 #include "c-target.h"
36 /* Handle attributes associated with format checking. */
38 /* This must be in the same order as format_types, except for
39 format_type_error. Target-specific format types do not have
40 matching enum values. */
41 enum format_type { printf_format_type, asm_fprintf_format_type,
42 gcc_diag_format_type, gcc_tdiag_format_type,
43 gcc_cdiag_format_type,
44 gcc_cxxdiag_format_type, gcc_gfc_format_type,
45 gcc_objc_string_format_type,
46 format_type_error = -1};
48 typedef struct function_format_info
50 int format_type; /* type of format (printf, scanf, etc.) */
51 unsigned HOST_WIDE_INT format_num; /* number of format argument */
52 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
53 } function_format_info;
55 static bool decode_format_attr (tree, function_format_info *, int);
56 static int decode_format_type (const char *);
58 static bool check_format_string (tree argument,
59 unsigned HOST_WIDE_INT format_num,
60 int flags, bool *no_add_attrs,
61 int expected_format_type);
62 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
63 int validated_p);
64 static const char *convert_format_name_to_system_name (const char *attr_name);
65 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
67 static int first_target_format_type;
68 static const char *format_name (int format_num);
69 static int format_flags (int format_num);
71 /* Check that we have a pointer to a string suitable for use as a format.
72 The default is to check for a char type.
73 For objective-c dialects, this is extended to include references to string
74 objects validated by objc_string_ref_type_p ().
75 Targets may also provide a string object type that can be used within c and
76 c++ and shared with their respective objective-c dialects. In this case the
77 reference to a format string is checked for validity via a hook.
79 The function returns true if strref points to any string type valid for the
80 language dialect and target. */
82 static bool
83 valid_stringptr_type_p (tree strref)
85 return (strref != NULL
86 && TREE_CODE (strref) == POINTER_TYPE
87 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
88 || objc_string_ref_type_p (strref)
89 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
92 /* Handle a "format_arg" attribute; arguments as in
93 struct attribute_spec.handler. */
94 tree
95 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
96 tree args, int flags, bool *no_add_attrs)
98 tree type = *node;
99 tree format_num_expr = TREE_VALUE (args);
100 unsigned HOST_WIDE_INT format_num = 0;
102 if (!get_constant (format_num_expr, &format_num, 0))
104 error ("format string has invalid operand number");
105 *no_add_attrs = true;
106 return NULL_TREE;
109 if (prototype_p (type))
111 /* The format arg can be any string reference valid for the language and
112 target. We cannot be more specific in this case. */
113 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
114 return NULL_TREE;
117 if (!valid_stringptr_type_p (TREE_TYPE (type)))
119 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
120 error ("function does not return string type");
121 *no_add_attrs = true;
122 return NULL_TREE;
125 return NULL_TREE;
128 /* Verify that the format_num argument is actually a string reference suitable,
129 for the language dialect and target (in case the format attribute is in
130 error). When we know the specific reference type expected, this is also
131 checked. */
132 static bool
133 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
134 int flags, bool *no_add_attrs, int expected_format_type)
136 unsigned HOST_WIDE_INT i;
137 bool is_objc_sref, is_target_sref, is_char_ref;
138 tree ref;
139 int fmt_flags;
140 function_args_iterator iter;
142 i = 1;
143 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
145 if (i == format_num)
146 break;
147 i++;
150 if (!ref
151 || !valid_stringptr_type_p (ref))
153 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
154 error ("format string argument is not a string type");
155 *no_add_attrs = true;
156 return false;
159 /* We only know that we want a suitable string reference. */
160 if (expected_format_type < 0)
161 return true;
163 /* Now check that the arg matches the expected type. */
164 is_char_ref =
165 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
167 fmt_flags = format_flags (expected_format_type);
168 is_objc_sref = is_target_sref = false;
169 if (!is_char_ref)
170 is_objc_sref = objc_string_ref_type_p (ref);
172 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
174 if (is_char_ref)
175 return true; /* OK, we expected a char and found one. */
176 else
178 /* We expected a char but found an extended string type. */
179 if (is_objc_sref)
180 error ("found a %<%s%> reference but the format argument should"
181 " be a string", format_name (gcc_objc_string_format_type));
182 else
183 error ("found a %qT but the format argument should be a string",
184 ref);
185 *no_add_attrs = true;
186 return false;
190 /* We expect a string object type as the format arg. */
191 if (is_char_ref)
193 error ("format argument should be a %<%s%> reference but"
194 " a string was found", format_name (expected_format_type));
195 *no_add_attrs = true;
196 return false;
199 /* We will assert that objective-c will support either its own string type
200 or the target-supplied variant. */
201 if (!is_objc_sref)
202 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
204 if (expected_format_type == (int) gcc_objc_string_format_type
205 && (is_objc_sref || is_target_sref))
206 return true;
208 /* We will allow a target string ref to match only itself. */
209 if (first_target_format_type
210 && expected_format_type >= first_target_format_type
211 && is_target_sref)
212 return true;
213 else
215 error ("format argument should be a %<%s%> reference",
216 format_name (expected_format_type));
217 *no_add_attrs = true;
218 return false;
221 gcc_unreachable ();
224 /* Verify EXPR is a constant, and store its value.
225 If validated_p is true there should be no errors.
226 Returns true on success, false otherwise. */
227 static bool
228 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
230 if (!tree_fits_uhwi_p (expr))
232 gcc_assert (!validated_p);
233 return false;
236 *value = TREE_INT_CST_LOW (expr);
238 return true;
241 /* Decode the arguments to a "format" attribute into a
242 function_format_info structure. It is already known that the list
243 is of the right length. If VALIDATED_P is true, then these
244 attributes have already been validated and must not be erroneous;
245 if false, it will give an error message. Returns true if the
246 attributes are successfully decoded, false otherwise. */
248 static bool
249 decode_format_attr (tree args, function_format_info *info, int validated_p)
251 tree format_type_id = TREE_VALUE (args);
252 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
253 tree first_arg_num_expr
254 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
256 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
258 gcc_assert (!validated_p);
259 error ("unrecognized format specifier");
260 return false;
262 else
264 const char *p = IDENTIFIER_POINTER (format_type_id);
266 p = convert_format_name_to_system_name (p);
268 info->format_type = decode_format_type (p);
270 if (!c_dialect_objc ()
271 && info->format_type == gcc_objc_string_format_type)
273 gcc_assert (!validated_p);
274 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
275 format_type_id);
276 info->format_type = format_type_error;
277 return false;
280 if (info->format_type == format_type_error)
282 gcc_assert (!validated_p);
283 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
284 format_type_id);
285 return false;
289 if (!get_constant (format_num_expr, &info->format_num, validated_p))
291 error ("format string has invalid operand number");
292 return false;
295 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
297 error ("%<...%> has invalid operand number");
298 return false;
301 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
303 gcc_assert (!validated_p);
304 error ("format string argument follows the args to be formatted");
305 return false;
308 return true;
311 /* Check a call to a format function against a parameter list. */
313 /* The C standard version C++ is treated as equivalent to
314 or inheriting from, for the purpose of format features supported. */
315 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
316 /* The C standard version we are checking formats against when pedantic. */
317 #define C_STD_VER ((int) (c_dialect_cxx () \
318 ? CPLUSPLUS_STD_VER \
319 : (flag_isoc99 \
320 ? STD_C99 \
321 : (flag_isoc94 ? STD_C94 : STD_C89))))
322 /* The name to give to the standard version we are warning about when
323 pedantic. FEATURE_VER is the version in which the feature warned out
324 appeared, which is higher than C_STD_VER. */
325 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
326 ? (cxx_dialect < cxx11 ? "ISO C++98" \
327 : "ISO C++11") \
328 : ((FEATURE_VER) == STD_EXT \
329 ? "ISO C" \
330 : "ISO C90"))
331 /* Adjust a C standard version, which may be STD_C9L, to account for
332 -Wno-long-long. Returns other standard versions unchanged. */
333 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
334 ? (warn_long_long ? STD_C99 : STD_C89) \
335 : (VER)))
337 /* Enum describing the kind of specifiers present in the format and
338 requiring an argument. */
339 enum format_specifier_kind {
340 CF_KIND_FORMAT,
341 CF_KIND_FIELD_WIDTH,
342 CF_KIND_FIELD_PRECISION
345 static const char *kind_descriptions[] = {
346 N_("format"),
347 N_("field width specifier"),
348 N_("field precision specifier")
351 /* Structure describing details of a type expected in format checking,
352 and the type to check against it. */
353 typedef struct format_wanted_type
355 /* The type wanted. */
356 tree wanted_type;
357 /* The name of this type to use in diagnostics. */
358 const char *wanted_type_name;
359 /* Should be type checked just for scalar width identity. */
360 int scalar_identity_flag;
361 /* The level of indirection through pointers at which this type occurs. */
362 int pointer_count;
363 /* Whether, when pointer_count is 1, to allow any character type when
364 pedantic, rather than just the character or void type specified. */
365 int char_lenient_flag;
366 /* Whether the argument, dereferenced once, is written into and so the
367 argument must not be a pointer to a const-qualified type. */
368 int writing_in_flag;
369 /* Whether the argument, dereferenced once, is read from and so
370 must not be a NULL pointer. */
371 int reading_from_flag;
372 /* The kind of specifier that this type is used for. */
373 enum format_specifier_kind kind;
374 /* The starting character of the specifier. This never includes the
375 initial percent sign. */
376 const char *format_start;
377 /* The length of the specifier. */
378 int format_length;
379 /* The actual parameter to check against the wanted type. */
380 tree param;
381 /* The argument number of that parameter. */
382 int arg_num;
383 /* The next type to check for this format conversion, or NULL if none. */
384 struct format_wanted_type *next;
385 } format_wanted_type;
387 /* Convenience macro for format_length_info meaning unused. */
388 #define NO_FMT NULL, FMT_LEN_none, STD_C89
390 static const format_length_info printf_length_specs[] =
392 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
393 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
394 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
395 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
396 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
397 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
398 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
399 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
400 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
401 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
402 { NO_FMT, NO_FMT, 0 }
405 /* Length specifiers valid for asm_fprintf. */
406 static const format_length_info asm_fprintf_length_specs[] =
408 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
409 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
410 { NO_FMT, NO_FMT, 0 }
413 /* Length specifiers valid for GCC diagnostics. */
414 static const format_length_info gcc_diag_length_specs[] =
416 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
417 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
418 { NO_FMT, NO_FMT, 0 }
421 /* The custom diagnostics all accept the same length specifiers. */
422 #define gcc_tdiag_length_specs gcc_diag_length_specs
423 #define gcc_cdiag_length_specs gcc_diag_length_specs
424 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
426 /* This differs from printf_length_specs only in that "Z" is not accepted. */
427 static const format_length_info scanf_length_specs[] =
429 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
430 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
431 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
432 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
433 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
434 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
435 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
436 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
437 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
438 { NO_FMT, NO_FMT, 0 }
442 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
443 make no sense for a format type not part of any C standard version. */
444 static const format_length_info strfmon_length_specs[] =
446 /* A GNU extension. */
447 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
448 { NO_FMT, NO_FMT, 0 }
452 /* For now, the Fortran front-end routines only use l as length modifier. */
453 static const format_length_info gcc_gfc_length_specs[] =
455 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
456 { NO_FMT, NO_FMT, 0 }
460 static const format_flag_spec printf_flag_specs[] =
462 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
463 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
464 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
465 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
466 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
467 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
468 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
469 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
470 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
471 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
472 { 0, 0, 0, NULL, NULL, STD_C89 }
476 static const format_flag_pair printf_flag_pairs[] =
478 { ' ', '+', 1, 0 },
479 { '0', '-', 1, 0 },
480 { '0', 'p', 1, 'i' },
481 { 0, 0, 0, 0 }
484 static const format_flag_spec asm_fprintf_flag_specs[] =
486 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
487 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
488 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
489 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
490 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
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 }
497 static const format_flag_pair asm_fprintf_flag_pairs[] =
499 { ' ', '+', 1, 0 },
500 { '0', '-', 1, 0 },
501 { '0', 'p', 1, 'i' },
502 { 0, 0, 0, 0 }
505 static const format_flag_pair gcc_diag_flag_pairs[] =
507 { 0, 0, 0, 0 }
510 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
511 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
512 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
513 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
515 static const format_flag_spec gcc_diag_flag_specs[] =
517 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
518 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
519 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
520 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
521 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
522 { 0, 0, 0, NULL, NULL, STD_C89 }
525 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
526 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
527 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
528 #define gcc_gfc_flag_specs gcc_diag_flag_specs
530 static const format_flag_spec scanf_flag_specs[] =
532 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
533 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
534 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
535 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
536 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
537 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
538 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
539 { 0, 0, 0, NULL, NULL, STD_C89 }
543 static const format_flag_pair scanf_flag_pairs[] =
545 { '*', 'L', 0, 0 },
546 { 'a', 'm', 0, 0 },
547 { 0, 0, 0, 0 }
551 static const format_flag_spec strftime_flag_specs[] =
553 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
554 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
555 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
556 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
557 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
558 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
559 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
560 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
561 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
562 { 0, 0, 0, NULL, NULL, STD_C89 }
566 static const format_flag_pair strftime_flag_pairs[] =
568 { 'E', 'O', 0, 0 },
569 { '_', '-', 0, 0 },
570 { '_', '0', 0, 0 },
571 { '-', '0', 0, 0 },
572 { '^', '#', 0, 0 },
573 { 0, 0, 0, 0 }
577 static const format_flag_spec strfmon_flag_specs[] =
579 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
580 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
581 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
582 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
583 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
584 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
585 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
586 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
587 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
588 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
589 { 0, 0, 0, NULL, NULL, STD_C89 }
592 static const format_flag_pair strfmon_flag_pairs[] =
594 { '+', '(', 0, 0 },
595 { 0, 0, 0, 0 }
599 static const format_char_info print_char_table[] =
601 /* C89 conversion specifiers. */
602 { "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 },
603 { "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 },
604 { "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 },
605 { "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 },
606 { "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 },
607 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
608 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
609 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
610 { "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 },
611 /* C99 conversion specifiers. */
612 { "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 },
613 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
614 /* X/Open conversion specifiers. */
615 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
616 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
617 /* GNU conversion specifiers. */
618 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
619 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
622 static const format_char_info asm_fprintf_char_table[] =
624 /* C89 conversion specifiers. */
625 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
626 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
627 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
628 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
629 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
631 /* asm_fprintf conversion specifiers. */
632 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
633 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
634 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
635 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
636 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
637 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
638 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
639 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
642 static const format_char_info gcc_diag_char_table[] =
644 /* C89 conversion specifiers. */
645 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
646 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
647 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
648 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
649 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
650 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
652 /* Custom conversion specifiers. */
654 /* These will require a "tree" at runtime. */
655 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
657 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
658 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
659 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
660 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
663 static const format_char_info gcc_tdiag_char_table[] =
665 /* C89 conversion specifiers. */
666 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
667 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
668 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
669 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
670 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
671 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
673 /* Custom conversion specifiers. */
675 /* These will require a "tree" at runtime. */
676 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
678 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
680 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
681 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
682 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
683 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
686 static const format_char_info gcc_cdiag_char_table[] =
688 /* C89 conversion specifiers. */
689 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
690 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
691 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
692 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
693 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
694 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
696 /* Custom conversion specifiers. */
698 /* These will require a "tree" at runtime. */
699 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
701 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
703 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
704 { "<>'R",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_cxxdiag_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 { "ADEFKSTVX",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 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
727 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
729 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
730 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
731 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
732 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
735 static const format_char_info gcc_gfc_char_table[] =
737 /* C89 conversion specifiers. */
738 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
739 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
740 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
741 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "cR", NULL },
743 /* gfc conversion specifiers. */
745 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
747 /* This will require a "locus" at runtime. */
748 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
750 /* These will require nothing. */
751 { "<>",0, STD_C89, NOARGUMENTS, "", "", NULL },
752 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
755 static const format_char_info scan_char_table[] =
757 /* C89 conversion specifiers. */
758 { "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 },
759 { "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 },
760 { "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 },
761 { "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 },
762 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
763 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
764 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
765 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
766 { "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 },
767 /* C99 conversion specifiers. */
768 { "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 },
769 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
770 /* X/Open conversion specifiers. */
771 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
772 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
773 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
776 static const format_char_info time_char_table[] =
778 /* C89 conversion specifiers. */
779 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
780 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
781 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
782 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
783 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
784 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
785 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
786 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
787 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
788 /* C99 conversion specifiers. */
789 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
790 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
791 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
792 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
793 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
794 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
795 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
796 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
797 /* GNU conversion specifiers. */
798 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
799 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
800 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
803 static const format_char_info monetary_char_table[] =
805 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
806 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
809 /* This must be in the same order as enum format_type. */
810 static const format_kind_info format_types_orig[] =
812 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
813 printf_flag_specs, printf_flag_pairs,
814 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
815 'w', 0, 'p', 0, 'L', 0,
816 &integer_type_node, &integer_type_node
818 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
819 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
820 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
821 'w', 0, 'p', 0, 'L', 0,
822 NULL, NULL
824 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
825 gcc_diag_flag_specs, gcc_diag_flag_pairs,
826 FMT_FLAG_ARG_CONVERT,
827 0, 0, 'p', 0, 'L', 0,
828 NULL, &integer_type_node
830 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
831 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
832 FMT_FLAG_ARG_CONVERT,
833 0, 0, 'p', 0, 'L', 0,
834 NULL, &integer_type_node
836 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
837 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
838 FMT_FLAG_ARG_CONVERT,
839 0, 0, 'p', 0, 'L', 0,
840 NULL, &integer_type_node
842 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
843 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
844 FMT_FLAG_ARG_CONVERT,
845 0, 0, 'p', 0, 'L', 0,
846 NULL, &integer_type_node
848 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "q+#", NULL,
849 gcc_gfc_flag_specs, gcc_gfc_flag_pairs,
850 FMT_FLAG_ARG_CONVERT,
851 0, 0, 0, 0, 0, 0,
852 NULL, NULL
854 { "NSString", NULL, NULL, NULL, NULL,
855 NULL, NULL,
856 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
857 NULL, NULL
859 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
860 scanf_flag_specs, scanf_flag_pairs,
861 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
862 'w', 0, 0, '*', 'L', 'm',
863 NULL, NULL
865 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
866 strftime_flag_specs, strftime_flag_pairs,
867 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
868 NULL, NULL
870 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
871 strfmon_flag_specs, strfmon_flag_pairs,
872 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
873 NULL, NULL
877 /* This layer of indirection allows GCC to reassign format_types with
878 new data if necessary, while still allowing the original data to be
879 const. */
880 static const format_kind_info *format_types = format_types_orig;
881 /* We can modify this one. We also add target-specific format types
882 to the end of the array. */
883 static format_kind_info *dynamic_format_types;
885 static int n_format_types = ARRAY_SIZE (format_types_orig);
887 /* Structure detailing the results of checking a format function call
888 where the format expression may be a conditional expression with
889 many leaves resulting from nested conditional expressions. */
890 typedef struct
892 /* Number of leaves of the format argument that could not be checked
893 as they were not string literals. */
894 int number_non_literal;
895 /* Number of leaves of the format argument that were null pointers or
896 string literals, but had extra format arguments. */
897 int number_extra_args;
898 location_t extra_arg_loc;
899 /* Number of leaves of the format argument that were null pointers or
900 string literals, but had extra format arguments and used $ operand
901 numbers. */
902 int number_dollar_extra_args;
903 /* Number of leaves of the format argument that were wide string
904 literals. */
905 int number_wide;
906 /* Number of leaves of the format argument that were empty strings. */
907 int number_empty;
908 /* Number of leaves of the format argument that were unterminated
909 strings. */
910 int number_unterminated;
911 /* Number of leaves of the format argument that were not counted above. */
912 int number_other;
913 /* Location of the format string. */
914 location_t format_string_loc;
915 } format_check_results;
917 typedef struct
919 format_check_results *res;
920 function_format_info *info;
921 tree params;
922 } format_check_context;
924 /* Return the format name (as specified in the original table) for the format
925 type indicated by format_num. */
926 static const char *
927 format_name (int format_num)
929 if (format_num >= 0 && format_num < n_format_types)
930 return format_types[format_num].name;
931 gcc_unreachable ();
934 /* Return the format flags (as specified in the original table) for the format
935 type indicated by format_num. */
936 static int
937 format_flags (int format_num)
939 if (format_num >= 0 && format_num < n_format_types)
940 return format_types[format_num].flags;
941 gcc_unreachable ();
944 static void check_format_info (function_format_info *, tree);
945 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
946 static void check_format_info_main (format_check_results *,
947 function_format_info *,
948 const char *, int, tree,
949 unsigned HOST_WIDE_INT, alloc_pool);
951 static void init_dollar_format_checking (int, tree);
952 static int maybe_read_dollar_number (const char **, int,
953 tree, tree *, const format_kind_info *);
954 static bool avoid_dollar_number (const char *);
955 static void finish_dollar_format_checking (format_check_results *, int);
957 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
958 int, const char *);
960 static void check_format_types (location_t, format_wanted_type *);
961 static void format_type_warning (location_t, format_wanted_type *, tree, tree);
963 /* Decode a format type from a string, returning the type, or
964 format_type_error if not valid, in which case the caller should print an
965 error message. */
966 static int
967 decode_format_type (const char *s)
969 int i;
970 int slen;
972 s = convert_format_name_to_system_name (s);
973 slen = strlen (s);
974 for (i = 0; i < n_format_types; i++)
976 int alen;
977 if (!strcmp (s, format_types[i].name))
978 return i;
979 alen = strlen (format_types[i].name);
980 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
981 && s[slen - 1] == '_' && s[slen - 2] == '_'
982 && !strncmp (s + 2, format_types[i].name, alen))
983 return i;
985 return format_type_error;
989 /* Check the argument list of a call to printf, scanf, etc.
990 ATTRS are the attributes on the function type. There are NARGS argument
991 values in the array ARGARRAY.
992 Also, if -Wsuggest-attribute=format,
993 warn for calls to vprintf or vscanf in functions with no such format
994 attribute themselves. */
996 void
997 check_function_format (tree attrs, int nargs, tree *argarray)
999 tree a;
1001 /* See if this function has any format attributes. */
1002 for (a = attrs; a; a = TREE_CHAIN (a))
1004 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1006 /* Yup; check it. */
1007 function_format_info info;
1008 decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
1009 if (warn_format)
1011 /* FIXME: Rewrite all the internal functions in this file
1012 to use the ARGARRAY directly instead of constructing this
1013 temporary list. */
1014 tree params = NULL_TREE;
1015 int i;
1016 for (i = nargs - 1; i >= 0; i--)
1017 params = tree_cons (NULL_TREE, argarray[i], params);
1018 check_format_info (&info, params);
1020 if (warn_suggest_attribute_format && info.first_arg_num == 0
1021 && (format_types[info.format_type].flags
1022 & (int) FMT_FLAG_ARG_CONVERT))
1024 tree c;
1025 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1027 c = TREE_CHAIN (c))
1028 if (is_attribute_p ("format", TREE_PURPOSE (c))
1029 && (decode_format_type (IDENTIFIER_POINTER
1030 (TREE_VALUE (TREE_VALUE (c))))
1031 == info.format_type))
1032 break;
1033 if (c == NULL_TREE)
1035 /* Check if the current function has a parameter to which
1036 the format attribute could be attached; if not, it
1037 can't be a candidate for a format attribute, despite
1038 the vprintf-like or vscanf-like call. */
1039 tree args;
1040 for (args = DECL_ARGUMENTS (current_function_decl);
1041 args != 0;
1042 args = DECL_CHAIN (args))
1044 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1045 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1046 == char_type_node))
1047 break;
1049 if (args != 0)
1050 warning (OPT_Wsuggest_attribute_format, "function might "
1051 "be possible candidate for %qs format attribute",
1052 format_types[info.format_type].name);
1060 /* Variables used by the checking of $ operand number formats. */
1061 static char *dollar_arguments_used = NULL;
1062 static char *dollar_arguments_pointer_p = NULL;
1063 static int dollar_arguments_alloc = 0;
1064 static int dollar_arguments_count;
1065 static int dollar_first_arg_num;
1066 static int dollar_max_arg_used;
1067 static int dollar_format_warned;
1069 /* Initialize the checking for a format string that may contain $
1070 parameter number specifications; we will need to keep track of whether
1071 each parameter has been used. FIRST_ARG_NUM is the number of the first
1072 argument that is a parameter to the format, or 0 for a vprintf-style
1073 function; PARAMS is the list of arguments starting at this argument. */
1075 static void
1076 init_dollar_format_checking (int first_arg_num, tree params)
1078 tree oparams = params;
1080 dollar_first_arg_num = first_arg_num;
1081 dollar_arguments_count = 0;
1082 dollar_max_arg_used = 0;
1083 dollar_format_warned = 0;
1084 if (first_arg_num > 0)
1086 while (params)
1088 dollar_arguments_count++;
1089 params = TREE_CHAIN (params);
1092 if (dollar_arguments_alloc < dollar_arguments_count)
1094 free (dollar_arguments_used);
1095 free (dollar_arguments_pointer_p);
1096 dollar_arguments_alloc = dollar_arguments_count;
1097 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1098 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1100 if (dollar_arguments_alloc)
1102 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1103 if (first_arg_num > 0)
1105 int i = 0;
1106 params = oparams;
1107 while (params)
1109 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1110 == POINTER_TYPE);
1111 params = TREE_CHAIN (params);
1112 i++;
1119 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1120 is set, it is an error if one is not found; otherwise, it is OK. If
1121 such a number is found, check whether it is within range and mark that
1122 numbered operand as being used for later checking. Returns the operand
1123 number if found and within range, zero if no such number was found and
1124 this is OK, or -1 on error. PARAMS points to the first operand of the
1125 format; PARAM_PTR is made to point to the parameter referred to. If
1126 a $ format is found, *FORMAT is updated to point just after it. */
1128 static int
1129 maybe_read_dollar_number (const char **format,
1130 int dollar_needed, tree params, tree *param_ptr,
1131 const format_kind_info *fki)
1133 int argnum;
1134 int overflow_flag;
1135 const char *fcp = *format;
1136 if (!ISDIGIT (*fcp))
1138 if (dollar_needed)
1140 warning (OPT_Wformat_, "missing $ operand number in format");
1141 return -1;
1143 else
1144 return 0;
1146 argnum = 0;
1147 overflow_flag = 0;
1148 while (ISDIGIT (*fcp))
1150 int nargnum;
1151 nargnum = 10 * argnum + (*fcp - '0');
1152 if (nargnum < 0 || nargnum / 10 != argnum)
1153 overflow_flag = 1;
1154 argnum = nargnum;
1155 fcp++;
1157 if (*fcp != '$')
1159 if (dollar_needed)
1161 warning (OPT_Wformat_, "missing $ operand number in format");
1162 return -1;
1164 else
1165 return 0;
1167 *format = fcp + 1;
1168 if (pedantic && !dollar_format_warned)
1170 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1171 C_STD_NAME (STD_EXT));
1172 dollar_format_warned = 1;
1174 if (overflow_flag || argnum == 0
1175 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1177 warning (OPT_Wformat_, "operand number out of range in format");
1178 return -1;
1180 if (argnum > dollar_max_arg_used)
1181 dollar_max_arg_used = argnum;
1182 /* For vprintf-style functions we may need to allocate more memory to
1183 track which arguments are used. */
1184 while (dollar_arguments_alloc < dollar_max_arg_used)
1186 int nalloc;
1187 nalloc = 2 * dollar_arguments_alloc + 16;
1188 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1189 nalloc);
1190 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1191 nalloc);
1192 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1193 nalloc - dollar_arguments_alloc);
1194 dollar_arguments_alloc = nalloc;
1196 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1197 && dollar_arguments_used[argnum - 1] == 1)
1199 dollar_arguments_used[argnum - 1] = 2;
1200 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1201 argnum, fki->name);
1203 else
1204 dollar_arguments_used[argnum - 1] = 1;
1205 if (dollar_first_arg_num)
1207 int i;
1208 *param_ptr = params;
1209 for (i = 1; i < argnum && *param_ptr != 0; i++)
1210 *param_ptr = TREE_CHAIN (*param_ptr);
1212 /* This case shouldn't be caught here. */
1213 gcc_assert (*param_ptr);
1215 else
1216 *param_ptr = 0;
1217 return argnum;
1220 /* Ensure that FORMAT does not start with a decimal number followed by
1221 a $; give a diagnostic and return true if it does, false otherwise. */
1223 static bool
1224 avoid_dollar_number (const char *format)
1226 if (!ISDIGIT (*format))
1227 return false;
1228 while (ISDIGIT (*format))
1229 format++;
1230 if (*format == '$')
1232 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1233 return true;
1235 return false;
1239 /* Finish the checking for a format string that used $ operand number formats
1240 instead of non-$ formats. We check for unused operands before used ones
1241 (a serious error, since the implementation of the format function
1242 can't know what types to pass to va_arg to find the later arguments).
1243 and for unused operands at the end of the format (if we know how many
1244 arguments the format had, so not for vprintf). If there were operand
1245 numbers out of range on a non-vprintf-style format, we won't have reached
1246 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1247 pointers. */
1249 static void
1250 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1252 int i;
1253 bool found_pointer_gap = false;
1254 for (i = 0; i < dollar_max_arg_used; i++)
1256 if (!dollar_arguments_used[i])
1258 if (pointer_gap_ok && (dollar_first_arg_num == 0
1259 || dollar_arguments_pointer_p[i]))
1260 found_pointer_gap = true;
1261 else
1262 warning_at (res->format_string_loc, OPT_Wformat_,
1263 "format argument %d unused before used argument %d in $-style format",
1264 i + 1, dollar_max_arg_used);
1267 if (found_pointer_gap
1268 || (dollar_first_arg_num
1269 && dollar_max_arg_used < dollar_arguments_count))
1271 res->number_other--;
1272 res->number_dollar_extra_args++;
1277 /* Retrieve the specification for a format flag. SPEC contains the
1278 specifications for format flags for the applicable kind of format.
1279 FLAG is the flag in question. If PREDICATES is NULL, the basic
1280 spec for that flag must be retrieved and must exist. If
1281 PREDICATES is not NULL, it is a string listing possible predicates
1282 for the spec entry; if an entry predicated on any of these is
1283 found, it is returned, otherwise NULL is returned. */
1285 static const format_flag_spec *
1286 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1288 int i;
1289 for (i = 0; spec[i].flag_char != 0; i++)
1291 if (spec[i].flag_char != flag)
1292 continue;
1293 if (predicates != NULL)
1295 if (spec[i].predicate != 0
1296 && strchr (predicates, spec[i].predicate) != 0)
1297 return &spec[i];
1299 else if (spec[i].predicate == 0)
1300 return &spec[i];
1302 gcc_assert (predicates);
1303 return NULL;
1307 /* Check the argument list of a call to printf, scanf, etc.
1308 INFO points to the function_format_info structure.
1309 PARAMS is the list of argument values. */
1311 static void
1312 check_format_info (function_format_info *info, tree params)
1314 format_check_context format_ctx;
1315 unsigned HOST_WIDE_INT arg_num;
1316 tree format_tree;
1317 format_check_results res;
1318 /* Skip to format argument. If the argument isn't available, there's
1319 no work for us to do; prototype checking will catch the problem. */
1320 for (arg_num = 1; ; ++arg_num)
1322 if (params == 0)
1323 return;
1324 if (arg_num == info->format_num)
1325 break;
1326 params = TREE_CHAIN (params);
1328 format_tree = TREE_VALUE (params);
1329 params = TREE_CHAIN (params);
1330 if (format_tree == 0)
1331 return;
1333 res.number_non_literal = 0;
1334 res.number_extra_args = 0;
1335 res.extra_arg_loc = UNKNOWN_LOCATION;
1336 res.number_dollar_extra_args = 0;
1337 res.number_wide = 0;
1338 res.number_empty = 0;
1339 res.number_unterminated = 0;
1340 res.number_other = 0;
1341 res.format_string_loc = input_location;
1343 format_ctx.res = &res;
1344 format_ctx.info = info;
1345 format_ctx.params = params;
1347 check_function_arguments_recurse (check_format_arg, &format_ctx,
1348 format_tree, arg_num);
1350 location_t loc = format_ctx.res->format_string_loc;
1351 if (res.extra_arg_loc == UNKNOWN_LOCATION)
1352 res.extra_arg_loc = loc;
1354 if (res.number_non_literal > 0)
1356 /* Functions taking a va_list normally pass a non-literal format
1357 string. These functions typically are declared with
1358 first_arg_num == 0, so avoid warning in those cases. */
1359 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1361 /* For strftime-like formats, warn for not checking the format
1362 string; but there are no arguments to check. */
1363 warning_at (loc, OPT_Wformat_nonliteral,
1364 "format not a string literal, format string not checked");
1366 else if (info->first_arg_num != 0)
1368 /* If there are no arguments for the format at all, we may have
1369 printf (foo) which is likely to be a security hole. */
1370 while (arg_num + 1 < info->first_arg_num)
1372 if (params == 0)
1373 break;
1374 params = TREE_CHAIN (params);
1375 ++arg_num;
1377 if (params == 0 && warn_format_security)
1378 warning_at (loc, OPT_Wformat_security,
1379 "format not a string literal and no format arguments");
1380 else if (params == 0 && warn_format_nonliteral)
1381 warning_at (loc, OPT_Wformat_nonliteral,
1382 "format not a string literal and no format arguments");
1383 else
1384 warning_at (loc, OPT_Wformat_nonliteral,
1385 "format not a string literal, argument types not checked");
1389 /* If there were extra arguments to the format, normally warn. However,
1390 the standard does say extra arguments are ignored, so in the specific
1391 case where we have multiple leaves (conditional expressions or
1392 ngettext) allow extra arguments if at least one leaf didn't have extra
1393 arguments, but was otherwise OK (either non-literal or checked OK).
1394 If the format is an empty string, this should be counted similarly to the
1395 case of extra format arguments. */
1396 if (res.number_extra_args > 0 && res.number_non_literal == 0
1397 && res.number_other == 0)
1398 warning_at (res.extra_arg_loc, OPT_Wformat_extra_args,
1399 "too many arguments for format");
1400 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1401 && res.number_other == 0)
1402 warning_at (loc, OPT_Wformat_extra_args, "unused arguments in $-style format");
1403 if (res.number_empty > 0 && res.number_non_literal == 0
1404 && res.number_other == 0)
1405 warning_at (loc, OPT_Wformat_zero_length, "zero-length %s format string",
1406 format_types[info->format_type].name);
1408 if (res.number_wide > 0)
1409 warning_at (loc, OPT_Wformat_, "format is a wide character string");
1411 if (res.number_unterminated > 0)
1412 warning_at (loc, OPT_Wformat_, "unterminated format string");
1415 /* Callback from check_function_arguments_recurse to check a
1416 format string. FORMAT_TREE is the format parameter. ARG_NUM
1417 is the number of the format argument. CTX points to a
1418 format_check_context. */
1420 static void
1421 check_format_arg (void *ctx, tree format_tree,
1422 unsigned HOST_WIDE_INT arg_num)
1424 format_check_context *format_ctx = (format_check_context *) ctx;
1425 format_check_results *res = format_ctx->res;
1426 function_format_info *info = format_ctx->info;
1427 tree params = format_ctx->params;
1429 int format_length;
1430 HOST_WIDE_INT offset;
1431 const char *format_chars;
1432 tree array_size = 0;
1433 tree array_init;
1434 alloc_pool fwt_pool;
1436 if (integer_zerop (format_tree))
1438 /* Skip to first argument to check, so we can see if this format
1439 has any arguments (it shouldn't). */
1440 while (arg_num + 1 < info->first_arg_num)
1442 if (params == 0)
1443 return;
1444 params = TREE_CHAIN (params);
1445 ++arg_num;
1448 if (params == 0)
1449 res->number_other++;
1450 else
1452 if (res->number_extra_args == 0)
1453 res->extra_arg_loc = EXPR_LOC_OR_LOC (TREE_VALUE (params),
1454 input_location);
1455 res->number_extra_args++;
1457 return;
1460 offset = 0;
1461 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1463 tree arg0, arg1;
1465 arg0 = TREE_OPERAND (format_tree, 0);
1466 arg1 = TREE_OPERAND (format_tree, 1);
1467 STRIP_NOPS (arg0);
1468 STRIP_NOPS (arg1);
1469 if (TREE_CODE (arg1) == INTEGER_CST)
1470 format_tree = arg0;
1471 else
1473 res->number_non_literal++;
1474 return;
1476 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1477 if (!cst_and_fits_in_hwi (arg1))
1479 res->number_non_literal++;
1480 return;
1482 offset = int_cst_value (arg1);
1484 if (TREE_CODE (format_tree) != ADDR_EXPR)
1486 res->number_non_literal++;
1487 return;
1489 res->format_string_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1490 format_tree = TREE_OPERAND (format_tree, 0);
1491 if (format_types[info->format_type].flags
1492 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1494 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1495 /* We cannot examine this string here - but we can check that it is
1496 a valid type. */
1497 if (TREE_CODE (format_tree) != CONST_DECL
1498 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1499 || (*targetcm.string_object_ref_type_p)
1500 ((const_tree) TREE_TYPE (format_tree))))
1502 res->number_non_literal++;
1503 return;
1505 /* Skip to first argument to check. */
1506 while (arg_num + 1 < info->first_arg_num)
1508 if (params == 0)
1509 return;
1510 params = TREE_CHAIN (params);
1511 ++arg_num;
1513 /* So, we have a valid literal string object and one or more params.
1514 We need to use an external helper to parse the string into format
1515 info. For Objective-C variants we provide the resource within the
1516 objc tree, for target variants, via a hook. */
1517 if (objc_str)
1518 objc_check_format_arg (format_tree, params);
1519 else if (targetcm.check_string_object_format_arg)
1520 (*targetcm.check_string_object_format_arg) (format_tree, params);
1521 /* Else we can't handle it and retire quietly. */
1522 return;
1524 if (TREE_CODE (format_tree) == ARRAY_REF
1525 && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1526 && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1527 format_tree = TREE_OPERAND (format_tree, 0);
1528 if (offset < 0)
1530 res->number_non_literal++;
1531 return;
1533 if (TREE_CODE (format_tree) == VAR_DECL
1534 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1535 && (array_init = decl_constant_value (format_tree)) != format_tree
1536 && TREE_CODE (array_init) == STRING_CST)
1538 /* Extract the string constant initializer. Note that this may include
1539 a trailing NUL character that is not in the array (e.g.
1540 const char a[3] = "foo";). */
1541 array_size = DECL_SIZE_UNIT (format_tree);
1542 format_tree = array_init;
1544 if (TREE_CODE (format_tree) != STRING_CST)
1546 res->number_non_literal++;
1547 return;
1549 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1551 res->number_wide++;
1552 return;
1554 format_chars = TREE_STRING_POINTER (format_tree);
1555 format_length = TREE_STRING_LENGTH (format_tree);
1556 if (array_size != 0)
1558 /* Variable length arrays can't be initialized. */
1559 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1561 if (tree_fits_shwi_p (array_size))
1563 HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1564 if (array_size_value > 0
1565 && array_size_value == (int) array_size_value
1566 && format_length > array_size_value)
1567 format_length = array_size_value;
1570 if (offset)
1572 if (offset >= format_length)
1574 res->number_non_literal++;
1575 return;
1577 format_chars += offset;
1578 format_length -= offset;
1580 if (format_length < 1 || format_chars[--format_length] != 0)
1582 res->number_unterminated++;
1583 return;
1585 if (format_length == 0)
1587 res->number_empty++;
1588 return;
1591 /* Skip to first argument to check. */
1592 while (arg_num + 1 < info->first_arg_num)
1594 if (params == 0)
1595 return;
1596 params = TREE_CHAIN (params);
1597 ++arg_num;
1599 /* Provisionally increment res->number_other; check_format_info_main
1600 will decrement it if it finds there are extra arguments, but this way
1601 need not adjust it for every return. */
1602 res->number_other++;
1603 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1604 sizeof (format_wanted_type), 10);
1605 check_format_info_main (res, info, format_chars, format_length,
1606 params, arg_num, fwt_pool);
1607 free_alloc_pool (fwt_pool);
1611 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1612 is the NUL-terminated format string (which at this point may contain
1613 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1614 terminating NUL character). ARG_NUM is one less than the number of
1615 the first format argument to check; PARAMS points to that format
1616 argument in the list of arguments. */
1618 static void
1619 check_format_info_main (format_check_results *res,
1620 function_format_info *info, const char *format_chars,
1621 int format_length, tree params,
1622 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1624 const char *orig_format_chars = format_chars;
1625 tree first_fillin_param = params;
1627 const format_kind_info *fki = &format_types[info->format_type];
1628 const format_flag_spec *flag_specs = fki->flag_specs;
1629 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1630 location_t format_string_loc = res->format_string_loc;
1632 /* -1 if no conversions taking an operand have been found; 0 if one has
1633 and it didn't use $; 1 if $ formats are in use. */
1634 int has_operand_number = -1;
1636 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1638 while (*format_chars != 0)
1640 int i;
1641 int suppressed = FALSE;
1642 const char *length_chars = NULL;
1643 enum format_lengths length_chars_val = FMT_LEN_none;
1644 enum format_std_version length_chars_std = STD_C89;
1645 int format_char;
1646 tree cur_param;
1647 tree wanted_type;
1648 int main_arg_num = 0;
1649 tree main_arg_params = 0;
1650 enum format_std_version wanted_type_std;
1651 const char *wanted_type_name;
1652 format_wanted_type width_wanted_type;
1653 format_wanted_type precision_wanted_type;
1654 format_wanted_type main_wanted_type;
1655 format_wanted_type *first_wanted_type = NULL;
1656 format_wanted_type *last_wanted_type = NULL;
1657 const format_length_info *fli = NULL;
1658 const format_char_info *fci = NULL;
1659 char flag_chars[256];
1660 int alloc_flag = 0;
1661 int scalar_identity_flag = 0;
1662 const char *format_start;
1664 if (*format_chars++ != '%')
1665 continue;
1666 if (*format_chars == 0)
1668 warning_at (format_string_loc, OPT_Wformat_,
1669 "spurious trailing %<%%%> in format");
1670 continue;
1672 if (*format_chars == '%')
1674 ++format_chars;
1675 continue;
1677 flag_chars[0] = 0;
1679 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1681 /* Possibly read a $ operand number at the start of the format.
1682 If one was previously used, one is required here. If one
1683 is not used here, we can't immediately conclude this is a
1684 format without them, since it could be printf %m or scanf %*. */
1685 int opnum;
1686 opnum = maybe_read_dollar_number (&format_chars, 0,
1687 first_fillin_param,
1688 &main_arg_params, fki);
1689 if (opnum == -1)
1690 return;
1691 else if (opnum > 0)
1693 has_operand_number = 1;
1694 main_arg_num = opnum + info->first_arg_num - 1;
1697 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1699 if (avoid_dollar_number (format_chars))
1700 return;
1703 /* Read any format flags, but do not yet validate them beyond removing
1704 duplicates, since in general validation depends on the rest of
1705 the format. */
1706 while (*format_chars != 0
1707 && strchr (fki->flag_chars, *format_chars) != 0)
1709 const format_flag_spec *s = get_flag_spec (flag_specs,
1710 *format_chars, NULL);
1711 if (strchr (flag_chars, *format_chars) != 0)
1713 warning_at (format_string_loc, OPT_Wformat_,
1714 "repeated %s in format", _(s->name));
1716 else
1718 i = strlen (flag_chars);
1719 flag_chars[i++] = *format_chars;
1720 flag_chars[i] = 0;
1722 if (s->skip_next_char)
1724 ++format_chars;
1725 if (*format_chars == 0)
1727 warning_at (format_string_loc, OPT_Wformat_,
1728 "missing fill character at end of strfmon format");
1729 return;
1732 ++format_chars;
1735 /* Read any format width, possibly * or *m$. */
1736 if (fki->width_char != 0)
1738 if (fki->width_type != NULL && *format_chars == '*')
1740 i = strlen (flag_chars);
1741 flag_chars[i++] = fki->width_char;
1742 flag_chars[i] = 0;
1743 /* "...a field width...may be indicated by an asterisk.
1744 In this case, an int argument supplies the field width..." */
1745 ++format_chars;
1746 if (has_operand_number != 0)
1748 int opnum;
1749 opnum = maybe_read_dollar_number (&format_chars,
1750 has_operand_number == 1,
1751 first_fillin_param,
1752 &params, fki);
1753 if (opnum == -1)
1754 return;
1755 else if (opnum > 0)
1757 has_operand_number = 1;
1758 arg_num = opnum + info->first_arg_num - 1;
1760 else
1761 has_operand_number = 0;
1763 else
1765 if (avoid_dollar_number (format_chars))
1766 return;
1768 if (info->first_arg_num != 0)
1770 if (params == 0)
1771 cur_param = NULL;
1772 else
1774 cur_param = TREE_VALUE (params);
1775 if (has_operand_number <= 0)
1777 params = TREE_CHAIN (params);
1778 ++arg_num;
1781 width_wanted_type.wanted_type = *fki->width_type;
1782 width_wanted_type.wanted_type_name = NULL;
1783 width_wanted_type.pointer_count = 0;
1784 width_wanted_type.char_lenient_flag = 0;
1785 width_wanted_type.scalar_identity_flag = 0;
1786 width_wanted_type.writing_in_flag = 0;
1787 width_wanted_type.reading_from_flag = 0;
1788 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1789 width_wanted_type.format_start = format_chars - 1;
1790 width_wanted_type.format_length = 1;
1791 width_wanted_type.param = cur_param;
1792 width_wanted_type.arg_num = arg_num;
1793 width_wanted_type.next = NULL;
1794 if (last_wanted_type != 0)
1795 last_wanted_type->next = &width_wanted_type;
1796 if (first_wanted_type == 0)
1797 first_wanted_type = &width_wanted_type;
1798 last_wanted_type = &width_wanted_type;
1801 else
1803 /* Possibly read a numeric width. If the width is zero,
1804 we complain if appropriate. */
1805 int non_zero_width_char = FALSE;
1806 int found_width = FALSE;
1807 while (ISDIGIT (*format_chars))
1809 found_width = TRUE;
1810 if (*format_chars != '0')
1811 non_zero_width_char = TRUE;
1812 ++format_chars;
1814 if (found_width && !non_zero_width_char &&
1815 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1816 warning_at (format_string_loc, OPT_Wformat_,
1817 "zero width in %s format", fki->name);
1818 if (found_width)
1820 i = strlen (flag_chars);
1821 flag_chars[i++] = fki->width_char;
1822 flag_chars[i] = 0;
1827 /* Read any format left precision (must be a number, not *). */
1828 if (fki->left_precision_char != 0 && *format_chars == '#')
1830 ++format_chars;
1831 i = strlen (flag_chars);
1832 flag_chars[i++] = fki->left_precision_char;
1833 flag_chars[i] = 0;
1834 if (!ISDIGIT (*format_chars))
1835 warning_at (format_string_loc, OPT_Wformat_,
1836 "empty left precision in %s format", fki->name);
1837 while (ISDIGIT (*format_chars))
1838 ++format_chars;
1841 /* Read any format precision, possibly * or *m$. */
1842 if (fki->precision_char != 0 && *format_chars == '.')
1844 ++format_chars;
1845 i = strlen (flag_chars);
1846 flag_chars[i++] = fki->precision_char;
1847 flag_chars[i] = 0;
1848 if (fki->precision_type != NULL && *format_chars == '*')
1850 /* "...a...precision...may be indicated by an asterisk.
1851 In this case, an int argument supplies the...precision." */
1852 ++format_chars;
1853 if (has_operand_number != 0)
1855 int opnum;
1856 opnum = maybe_read_dollar_number (&format_chars,
1857 has_operand_number == 1,
1858 first_fillin_param,
1859 &params, fki);
1860 if (opnum == -1)
1861 return;
1862 else if (opnum > 0)
1864 has_operand_number = 1;
1865 arg_num = opnum + info->first_arg_num - 1;
1867 else
1868 has_operand_number = 0;
1870 else
1872 if (avoid_dollar_number (format_chars))
1873 return;
1875 if (info->first_arg_num != 0)
1877 if (params == 0)
1878 cur_param = NULL;
1879 else
1881 cur_param = TREE_VALUE (params);
1882 if (has_operand_number <= 0)
1884 params = TREE_CHAIN (params);
1885 ++arg_num;
1888 precision_wanted_type.wanted_type = *fki->precision_type;
1889 precision_wanted_type.wanted_type_name = NULL;
1890 precision_wanted_type.pointer_count = 0;
1891 precision_wanted_type.char_lenient_flag = 0;
1892 precision_wanted_type.scalar_identity_flag = 0;
1893 precision_wanted_type.writing_in_flag = 0;
1894 precision_wanted_type.reading_from_flag = 0;
1895 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1896 precision_wanted_type.param = cur_param;
1897 precision_wanted_type.format_start = format_chars - 2;
1898 precision_wanted_type.format_length = 2;
1899 precision_wanted_type.arg_num = arg_num;
1900 precision_wanted_type.next = NULL;
1901 if (last_wanted_type != 0)
1902 last_wanted_type->next = &precision_wanted_type;
1903 if (first_wanted_type == 0)
1904 first_wanted_type = &precision_wanted_type;
1905 last_wanted_type = &precision_wanted_type;
1908 else
1910 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1911 && !ISDIGIT (*format_chars))
1912 warning_at (format_string_loc, OPT_Wformat_,
1913 "empty precision in %s format", fki->name);
1914 while (ISDIGIT (*format_chars))
1915 ++format_chars;
1919 format_start = format_chars;
1920 if (fki->alloc_char && fki->alloc_char == *format_chars)
1922 i = strlen (flag_chars);
1923 flag_chars[i++] = fki->alloc_char;
1924 flag_chars[i] = 0;
1925 format_chars++;
1928 /* Handle the scanf allocation kludge. */
1929 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1931 if (*format_chars == 'a' && !flag_isoc99)
1933 if (format_chars[1] == 's' || format_chars[1] == 'S'
1934 || format_chars[1] == '[')
1936 /* 'a' is used as a flag. */
1937 i = strlen (flag_chars);
1938 flag_chars[i++] = 'a';
1939 flag_chars[i] = 0;
1940 format_chars++;
1945 /* Read any length modifier, if this kind of format has them. */
1946 fli = fki->length_char_specs;
1947 length_chars = NULL;
1948 length_chars_val = FMT_LEN_none;
1949 length_chars_std = STD_C89;
1950 scalar_identity_flag = 0;
1951 if (fli)
1953 while (fli->name != 0
1954 && strncmp (fli->name, format_chars, strlen (fli->name)))
1955 fli++;
1956 if (fli->name != 0)
1958 format_chars += strlen (fli->name);
1959 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1961 format_chars++;
1962 length_chars = fli->double_name;
1963 length_chars_val = fli->double_index;
1964 length_chars_std = fli->double_std;
1966 else
1968 length_chars = fli->name;
1969 length_chars_val = fli->index;
1970 length_chars_std = fli->std;
1971 scalar_identity_flag = fli->scalar_identity_flag;
1973 i = strlen (flag_chars);
1974 flag_chars[i++] = fki->length_code_char;
1975 flag_chars[i] = 0;
1977 if (pedantic)
1979 /* Warn if the length modifier is non-standard. */
1980 if (ADJ_STD (length_chars_std) > C_STD_VER)
1981 warning_at (format_string_loc, OPT_Wformat_,
1982 "%s does not support the %qs %s length modifier",
1983 C_STD_NAME (length_chars_std), length_chars,
1984 fki->name);
1988 /* Read any modifier (strftime E/O). */
1989 if (fki->modifier_chars != NULL)
1991 while (*format_chars != 0
1992 && strchr (fki->modifier_chars, *format_chars) != 0)
1994 if (strchr (flag_chars, *format_chars) != 0)
1996 const format_flag_spec *s = get_flag_spec (flag_specs,
1997 *format_chars, NULL);
1998 warning_at (format_string_loc, OPT_Wformat_,
1999 "repeated %s in format", _(s->name));
2001 else
2003 i = strlen (flag_chars);
2004 flag_chars[i++] = *format_chars;
2005 flag_chars[i] = 0;
2007 ++format_chars;
2011 format_char = *format_chars;
2012 if (format_char == 0
2013 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2014 && format_char == '%'))
2016 warning_at (format_string_loc, OPT_Wformat_,
2017 "conversion lacks type at end of format");
2018 continue;
2020 format_chars++;
2021 fci = fki->conversion_specs;
2022 while (fci->format_chars != 0
2023 && strchr (fci->format_chars, format_char) == 0)
2024 ++fci;
2025 if (fci->format_chars == 0)
2027 if (ISGRAPH (format_char))
2028 warning_at (format_string_loc, OPT_Wformat_,
2029 "unknown conversion type character %qc in format",
2030 format_char);
2031 else
2032 warning_at (format_string_loc, OPT_Wformat_,
2033 "unknown conversion type character 0x%x in format",
2034 format_char);
2035 continue;
2037 if (pedantic)
2039 if (ADJ_STD (fci->std) > C_STD_VER)
2040 warning_at (format_string_loc, OPT_Wformat_,
2041 "%s does not support the %<%%%c%> %s format",
2042 C_STD_NAME (fci->std), format_char, fki->name);
2045 /* Validate the individual flags used, removing any that are invalid. */
2047 int d = 0;
2048 for (i = 0; flag_chars[i] != 0; i++)
2050 const format_flag_spec *s = get_flag_spec (flag_specs,
2051 flag_chars[i], NULL);
2052 flag_chars[i - d] = flag_chars[i];
2053 if (flag_chars[i] == fki->length_code_char)
2054 continue;
2055 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2057 warning_at (format_string_loc,
2058 OPT_Wformat_, "%s used with %<%%%c%> %s format",
2059 _(s->name), format_char, fki->name);
2060 d++;
2061 continue;
2063 if (pedantic)
2065 const format_flag_spec *t;
2066 if (ADJ_STD (s->std) > C_STD_VER)
2067 warning_at (format_string_loc, OPT_Wformat_,
2068 "%s does not support %s",
2069 C_STD_NAME (s->std), _(s->long_name));
2070 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2071 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2073 const char *long_name = (t->long_name != NULL
2074 ? t->long_name
2075 : s->long_name);
2076 if (ADJ_STD (t->std) > C_STD_VER)
2077 warning_at (format_string_loc, OPT_Wformat_,
2078 "%s does not support %s with the %<%%%c%> %s format",
2079 C_STD_NAME (t->std), _(long_name),
2080 format_char, fki->name);
2084 flag_chars[i - d] = 0;
2087 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2088 && strchr (flag_chars, 'a') != 0)
2089 alloc_flag = 1;
2090 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2091 alloc_flag = 1;
2093 if (fki->suppression_char
2094 && strchr (flag_chars, fki->suppression_char) != 0)
2095 suppressed = 1;
2097 /* Validate the pairs of flags used. */
2098 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2100 const format_flag_spec *s, *t;
2101 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2102 continue;
2103 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2104 continue;
2105 if (bad_flag_pairs[i].predicate != 0
2106 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2107 continue;
2108 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2109 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2110 if (bad_flag_pairs[i].ignored)
2112 if (bad_flag_pairs[i].predicate != 0)
2113 warning_at (format_string_loc, OPT_Wformat_,
2114 "%s ignored with %s and %<%%%c%> %s format",
2115 _(s->name), _(t->name), format_char,
2116 fki->name);
2117 else
2118 warning_at (format_string_loc, OPT_Wformat_,
2119 "%s ignored with %s in %s format",
2120 _(s->name), _(t->name), fki->name);
2122 else
2124 if (bad_flag_pairs[i].predicate != 0)
2125 warning_at (format_string_loc, OPT_Wformat_,
2126 "use of %s and %s together with %<%%%c%> %s format",
2127 _(s->name), _(t->name), format_char,
2128 fki->name);
2129 else
2130 warning_at (format_string_loc, OPT_Wformat_,
2131 "use of %s and %s together in %s format",
2132 _(s->name), _(t->name), fki->name);
2136 /* Give Y2K warnings. */
2137 if (warn_format_y2k)
2139 int y2k_level = 0;
2140 if (strchr (fci->flags2, '4') != 0)
2141 if (strchr (flag_chars, 'E') != 0)
2142 y2k_level = 3;
2143 else
2144 y2k_level = 2;
2145 else if (strchr (fci->flags2, '3') != 0)
2146 y2k_level = 3;
2147 else if (strchr (fci->flags2, '2') != 0)
2148 y2k_level = 2;
2149 if (y2k_level == 3)
2150 warning_at (format_string_loc, OPT_Wformat_y2k,
2151 "%<%%%c%> yields only last 2 digits of "
2152 "year in some locales", format_char);
2153 else if (y2k_level == 2)
2154 warning_at (format_string_loc, OPT_Wformat_y2k,
2155 "%<%%%c%> yields only last 2 digits of year",
2156 format_char);
2159 if (strchr (fci->flags2, '[') != 0)
2161 /* Skip over scan set, in case it happens to have '%' in it. */
2162 if (*format_chars == '^')
2163 ++format_chars;
2164 /* Find closing bracket; if one is hit immediately, then
2165 it's part of the scan set rather than a terminator. */
2166 if (*format_chars == ']')
2167 ++format_chars;
2168 while (*format_chars && *format_chars != ']')
2169 ++format_chars;
2170 if (*format_chars != ']')
2171 /* The end of the format string was reached. */
2172 warning_at (format_string_loc, OPT_Wformat_,
2173 "no closing %<]%> for %<%%[%> format");
2176 wanted_type = 0;
2177 wanted_type_name = 0;
2178 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2180 wanted_type = (fci->types[length_chars_val].type
2181 ? *fci->types[length_chars_val].type : 0);
2182 wanted_type_name = fci->types[length_chars_val].name;
2183 wanted_type_std = fci->types[length_chars_val].std;
2184 if (wanted_type == 0)
2186 warning_at (format_string_loc, OPT_Wformat_,
2187 "use of %qs length modifier with %qc type character",
2188 length_chars, format_char);
2189 /* Heuristic: skip one argument when an invalid length/type
2190 combination is encountered. */
2191 arg_num++;
2192 if (params != 0)
2193 params = TREE_CHAIN (params);
2194 continue;
2196 else if (pedantic
2197 /* Warn if non-standard, provided it is more non-standard
2198 than the length and type characters that may already
2199 have been warned for. */
2200 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2201 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2203 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2204 warning_at (format_string_loc, OPT_Wformat_,
2205 "%s does not support the %<%%%s%c%> %s format",
2206 C_STD_NAME (wanted_type_std), length_chars,
2207 format_char, fki->name);
2211 main_wanted_type.next = NULL;
2213 /* Finally. . .check type of argument against desired type! */
2214 if (info->first_arg_num == 0)
2215 continue;
2216 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2217 || suppressed)
2219 if (main_arg_num != 0)
2221 if (suppressed)
2222 warning_at (format_string_loc, OPT_Wformat_,
2223 "operand number specified with "
2224 "suppressed assignment");
2225 else
2226 warning_at (format_string_loc, OPT_Wformat_,
2227 "operand number specified for format "
2228 "taking no argument");
2231 else
2233 format_wanted_type *wanted_type_ptr;
2235 if (main_arg_num != 0)
2237 arg_num = main_arg_num;
2238 params = main_arg_params;
2240 else
2242 ++arg_num;
2243 if (has_operand_number > 0)
2245 warning_at (format_string_loc, OPT_Wformat_,
2246 "missing $ operand number in format");
2247 return;
2249 else
2250 has_operand_number = 0;
2253 wanted_type_ptr = &main_wanted_type;
2254 while (fci)
2256 if (params == 0)
2257 cur_param = NULL;
2258 else
2260 cur_param = TREE_VALUE (params);
2261 params = TREE_CHAIN (params);
2264 wanted_type_ptr->wanted_type = wanted_type;
2265 wanted_type_ptr->wanted_type_name = wanted_type_name;
2266 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2267 wanted_type_ptr->char_lenient_flag = 0;
2268 if (strchr (fci->flags2, 'c') != 0)
2269 wanted_type_ptr->char_lenient_flag = 1;
2270 wanted_type_ptr->scalar_identity_flag = 0;
2271 if (scalar_identity_flag)
2272 wanted_type_ptr->scalar_identity_flag = 1;
2273 wanted_type_ptr->writing_in_flag = 0;
2274 wanted_type_ptr->reading_from_flag = 0;
2275 if (alloc_flag)
2276 wanted_type_ptr->writing_in_flag = 1;
2277 else
2279 if (strchr (fci->flags2, 'W') != 0)
2280 wanted_type_ptr->writing_in_flag = 1;
2281 if (strchr (fci->flags2, 'R') != 0)
2282 wanted_type_ptr->reading_from_flag = 1;
2284 wanted_type_ptr->kind = CF_KIND_FORMAT;
2285 wanted_type_ptr->param = cur_param;
2286 wanted_type_ptr->arg_num = arg_num;
2287 wanted_type_ptr->format_start = format_start;
2288 wanted_type_ptr->format_length = format_chars - format_start;
2289 wanted_type_ptr->next = NULL;
2290 if (last_wanted_type != 0)
2291 last_wanted_type->next = wanted_type_ptr;
2292 if (first_wanted_type == 0)
2293 first_wanted_type = wanted_type_ptr;
2294 last_wanted_type = wanted_type_ptr;
2296 fci = fci->chain;
2297 if (fci)
2299 wanted_type_ptr = (format_wanted_type *)
2300 pool_alloc (fwt_pool);
2301 arg_num++;
2302 wanted_type = *fci->types[length_chars_val].type;
2303 wanted_type_name = fci->types[length_chars_val].name;
2308 if (first_wanted_type != 0)
2309 check_format_types (format_string_loc, first_wanted_type);
2312 if (format_chars - orig_format_chars != format_length)
2313 warning_at (format_string_loc, OPT_Wformat_contains_nul,
2314 "embedded %<\\0%> in format");
2315 if (info->first_arg_num != 0 && params != 0
2316 && has_operand_number <= 0)
2318 res->number_other--;
2319 res->number_extra_args++;
2321 if (has_operand_number > 0)
2322 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2326 /* Check the argument types from a single format conversion (possibly
2327 including width and precision arguments). LOC is the location of
2328 the format string. */
2329 static void
2330 check_format_types (location_t loc, format_wanted_type *types)
2332 for (; types != 0; types = types->next)
2334 tree cur_param;
2335 tree cur_type;
2336 tree orig_cur_type;
2337 tree wanted_type;
2338 int arg_num;
2339 int i;
2340 int char_type_flag;
2342 wanted_type = types->wanted_type;
2343 arg_num = types->arg_num;
2345 /* The following should not occur here. */
2346 gcc_assert (wanted_type);
2347 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2349 if (types->pointer_count == 0)
2350 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2352 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2354 cur_param = types->param;
2355 if (!cur_param)
2357 format_type_warning (loc, types, wanted_type, NULL);
2358 continue;
2361 cur_type = TREE_TYPE (cur_param);
2362 if (cur_type == error_mark_node)
2363 continue;
2364 orig_cur_type = cur_type;
2365 char_type_flag = 0;
2367 STRIP_NOPS (cur_param);
2369 /* Check the types of any additional pointer arguments
2370 that precede the "real" argument. */
2371 for (i = 0; i < types->pointer_count; ++i)
2373 if (TREE_CODE (cur_type) == POINTER_TYPE)
2375 cur_type = TREE_TYPE (cur_type);
2376 if (cur_type == error_mark_node)
2377 break;
2379 /* Check for writing through a NULL pointer. */
2380 if (types->writing_in_flag
2381 && i == 0
2382 && cur_param != 0
2383 && integer_zerop (cur_param))
2384 warning (OPT_Wformat_, "writing through null pointer "
2385 "(argument %d)", arg_num);
2387 /* Check for reading through a NULL pointer. */
2388 if (types->reading_from_flag
2389 && i == 0
2390 && cur_param != 0
2391 && integer_zerop (cur_param))
2392 warning (OPT_Wformat_, "reading through null pointer "
2393 "(argument %d)", arg_num);
2395 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2396 cur_param = TREE_OPERAND (cur_param, 0);
2397 else
2398 cur_param = 0;
2400 /* See if this is an attempt to write into a const type with
2401 scanf or with printf "%n". Note: the writing in happens
2402 at the first indirection only, if for example
2403 void * const * is passed to scanf %p; passing
2404 const void ** is simply passing an incompatible type. */
2405 if (types->writing_in_flag
2406 && i == 0
2407 && (TYPE_READONLY (cur_type)
2408 || (cur_param != 0
2409 && (CONSTANT_CLASS_P (cur_param)
2410 || (DECL_P (cur_param)
2411 && TREE_READONLY (cur_param))))))
2412 warning (OPT_Wformat_, "writing into constant object "
2413 "(argument %d)", arg_num);
2415 /* If there are extra type qualifiers beyond the first
2416 indirection, then this makes the types technically
2417 incompatible. */
2418 if (i > 0
2419 && pedantic
2420 && (TYPE_READONLY (cur_type)
2421 || TYPE_VOLATILE (cur_type)
2422 || TYPE_ATOMIC (cur_type)
2423 || TYPE_RESTRICT (cur_type)))
2424 warning (OPT_Wformat_, "extra type qualifiers in format "
2425 "argument (argument %d)",
2426 arg_num);
2429 else
2431 format_type_warning (loc, types, wanted_type, orig_cur_type);
2432 break;
2436 if (i < types->pointer_count)
2437 continue;
2439 cur_type = TYPE_MAIN_VARIANT (cur_type);
2441 /* Check whether the argument type is a character type. This leniency
2442 only applies to certain formats, flagged with 'c'.
2444 if (types->char_lenient_flag)
2445 char_type_flag = (cur_type == char_type_node
2446 || cur_type == signed_char_type_node
2447 || cur_type == unsigned_char_type_node);
2449 /* Check the type of the "real" argument, if there's a type we want. */
2450 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2451 continue;
2452 /* If we want 'void *', allow any pointer type.
2453 (Anything else would already have got a warning.)
2454 With -Wpedantic, only allow pointers to void and to character
2455 types. */
2456 if (wanted_type == void_type_node
2457 && (!pedantic || (i == 1 && char_type_flag)))
2458 continue;
2459 /* Don't warn about differences merely in signedness, unless
2460 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2461 target and not a character type, and for character types at
2462 a second level of indirection. */
2463 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2464 && TREE_CODE (cur_type) == INTEGER_TYPE
2465 && ((!pedantic && !warn_format_signedness)
2466 || (i == 0 && !warn_format_signedness)
2467 || (i == 1 && char_type_flag))
2468 && (TYPE_UNSIGNED (wanted_type)
2469 ? wanted_type == c_common_unsigned_type (cur_type)
2470 : wanted_type == c_common_signed_type (cur_type)))
2471 continue;
2472 /* Likewise, "signed char", "unsigned char" and "char" are
2473 equivalent but the above test won't consider them equivalent. */
2474 if (wanted_type == char_type_node
2475 && (!pedantic || i < 2)
2476 && char_type_flag)
2477 continue;
2478 if (types->scalar_identity_flag
2479 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2480 || (INTEGRAL_TYPE_P (cur_type)
2481 && INTEGRAL_TYPE_P (wanted_type)))
2482 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2483 continue;
2484 /* Now we have a type mismatch. */
2485 format_type_warning (loc, types, wanted_type, orig_cur_type);
2490 /* Give a warning at LOC about a format argument of different type from that
2491 expected. WANTED_TYPE is the type the argument should have, possibly
2492 stripped of pointer dereferences. The description (such as "field
2493 precision"), the placement in the format string, a possibly more
2494 friendly name of WANTED_TYPE, and the number of pointer dereferences
2495 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2496 or NULL if it is missing. */
2497 static void
2498 format_type_warning (location_t loc, format_wanted_type *type,
2499 tree wanted_type, tree arg_type)
2501 int kind = type->kind;
2502 const char *wanted_type_name = type->wanted_type_name;
2503 const char *format_start = type->format_start;
2504 int format_length = type->format_length;
2505 int pointer_count = type->pointer_count;
2506 int arg_num = type->arg_num;
2508 char *p;
2509 /* If ARG_TYPE is a typedef with a misleading name (for example,
2510 size_t but not the standard size_t expected by printf %zu), avoid
2511 printing the typedef name. */
2512 if (wanted_type_name
2513 && arg_type
2514 && TYPE_NAME (arg_type)
2515 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2516 && DECL_NAME (TYPE_NAME (arg_type))
2517 && !strcmp (wanted_type_name,
2518 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2519 arg_type = TYPE_MAIN_VARIANT (arg_type);
2520 /* The format type and name exclude any '*' for pointers, so those
2521 must be formatted manually. For all the types we currently have,
2522 this is adequate, but formats taking pointers to functions or
2523 arrays would require the full type to be built up in order to
2524 print it with %T. */
2525 p = (char *) alloca (pointer_count + 2);
2526 if (pointer_count == 0)
2527 p[0] = 0;
2528 else if (c_dialect_cxx ())
2530 memset (p, '*', pointer_count);
2531 p[pointer_count] = 0;
2533 else
2535 p[0] = ' ';
2536 memset (p + 1, '*', pointer_count);
2537 p[pointer_count + 1] = 0;
2540 if (wanted_type_name)
2542 if (arg_type)
2543 warning_at (loc, OPT_Wformat_,
2544 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2545 "but argument %d has type %qT",
2546 gettext (kind_descriptions[kind]),
2547 (kind == CF_KIND_FORMAT ? "%" : ""),
2548 format_length, format_start,
2549 wanted_type_name, p, arg_num, arg_type);
2550 else
2551 warning_at (loc, OPT_Wformat_,
2552 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2553 gettext (kind_descriptions[kind]),
2554 (kind == CF_KIND_FORMAT ? "%" : ""),
2555 format_length, format_start, wanted_type_name, p);
2557 else
2559 if (arg_type)
2560 warning_at (loc, OPT_Wformat_,
2561 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2562 "but argument %d has type %qT",
2563 gettext (kind_descriptions[kind]),
2564 (kind == CF_KIND_FORMAT ? "%" : ""),
2565 format_length, format_start,
2566 wanted_type, p, arg_num, arg_type);
2567 else
2568 warning_at (loc, OPT_Wformat_,
2569 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2570 gettext (kind_descriptions[kind]),
2571 (kind == CF_KIND_FORMAT ? "%" : ""),
2572 format_length, format_start, wanted_type, p);
2577 /* Given a format_char_info array FCI, and a character C, this function
2578 returns the index into the conversion_specs where that specifier's
2579 data is located. The character must exist. */
2580 static unsigned int
2581 find_char_info_specifier_index (const format_char_info *fci, int c)
2583 unsigned i;
2585 for (i = 0; fci->format_chars; i++, fci++)
2586 if (strchr (fci->format_chars, c))
2587 return i;
2589 /* We shouldn't be looking for a non-existent specifier. */
2590 gcc_unreachable ();
2593 /* Given a format_length_info array FLI, and a character C, this
2594 function returns the index into the conversion_specs where that
2595 modifier's data is located. The character must exist. */
2596 static unsigned int
2597 find_length_info_modifier_index (const format_length_info *fli, int c)
2599 unsigned i;
2601 for (i = 0; fli->name; i++, fli++)
2602 if (strchr (fli->name, c))
2603 return i;
2605 /* We shouldn't be looking for a non-existent modifier. */
2606 gcc_unreachable ();
2609 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2610 use in GCC's __asm_fprintf__ custom format attribute. You must
2611 have set dynamic_format_types before calling this function. */
2612 static void
2613 init_dynamic_asm_fprintf_info (void)
2615 static tree hwi;
2617 if (!hwi)
2619 format_length_info *new_asm_fprintf_length_specs;
2620 unsigned int i;
2622 /* Find the underlying type for HOST_WIDE_INT. For the %w
2623 length modifier to work, one must have issued: "typedef
2624 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2625 prior to using that modifier. */
2626 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2627 if (!hwi)
2629 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2630 return;
2632 hwi = identifier_global_value (hwi);
2633 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2635 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2636 return;
2638 hwi = DECL_ORIGINAL_TYPE (hwi);
2639 gcc_assert (hwi);
2640 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2642 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2643 " or %<long long%>");
2644 return;
2647 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2648 new_asm_fprintf_length_specs = (format_length_info *)
2649 xmemdup (asm_fprintf_length_specs,
2650 sizeof (asm_fprintf_length_specs),
2651 sizeof (asm_fprintf_length_specs));
2653 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2654 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2655 if (hwi == long_integer_type_node)
2656 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2657 else if (hwi == long_long_integer_type_node)
2658 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2659 else
2660 gcc_unreachable ();
2662 /* Assign the new data for use. */
2663 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2664 new_asm_fprintf_length_specs;
2668 /* Determine the type of a "locus" in the code being compiled for use
2669 in GCC's __gcc_gfc__ custom format attribute. You must have set
2670 dynamic_format_types before calling this function. */
2671 static void
2672 init_dynamic_gfc_info (void)
2674 static tree locus;
2676 if (!locus)
2678 static format_char_info *gfc_fci;
2680 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2681 must have declared 'locus' prior to using this attribute. If
2682 we haven't seen this declarations then you shouldn't use the
2683 specifier requiring that type. */
2684 if ((locus = maybe_get_identifier ("locus")))
2686 locus = identifier_global_value (locus);
2687 if (locus)
2689 if (TREE_CODE (locus) != TYPE_DECL
2690 || TREE_TYPE (locus) == error_mark_node)
2692 error ("%<locus%> is not defined as a type");
2693 locus = 0;
2695 else
2696 locus = TREE_TYPE (locus);
2700 /* Assign the new data for use. */
2702 /* Handle the __gcc_gfc__ format specifics. */
2703 if (!gfc_fci)
2704 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2705 gfc_fci = (format_char_info *)
2706 xmemdup (gcc_gfc_char_table,
2707 sizeof (gcc_gfc_char_table),
2708 sizeof (gcc_gfc_char_table));
2709 if (locus)
2711 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2712 gfc_fci[i].types[0].type = &locus;
2713 gfc_fci[i].pointer_count = 1;
2718 /* Determine the types of "tree" and "location_t" in the code being
2719 compiled for use in GCC's diagnostic custom format attributes. You
2720 must have set dynamic_format_types before calling this function. */
2721 static void
2722 init_dynamic_diag_info (void)
2724 static tree t, loc, hwi;
2726 if (!loc || !t || !hwi)
2728 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2729 static format_length_info *diag_ls;
2730 unsigned int i;
2732 /* For the GCC-diagnostics custom format specifiers to work, one
2733 must have declared 'tree' and/or 'location_t' prior to using
2734 those attributes. If we haven't seen these declarations then
2735 you shouldn't use the specifiers requiring these types.
2736 However we don't force a hard ICE because we may see only one
2737 or the other type. */
2738 if ((loc = maybe_get_identifier ("location_t")))
2740 loc = identifier_global_value (loc);
2741 if (loc)
2743 if (TREE_CODE (loc) != TYPE_DECL)
2745 error ("%<location_t%> is not defined as a type");
2746 loc = 0;
2748 else
2749 loc = TREE_TYPE (loc);
2753 /* We need to grab the underlying 'union tree_node' so peek into
2754 an extra type level. */
2755 if ((t = maybe_get_identifier ("tree")))
2757 t = identifier_global_value (t);
2758 if (t)
2760 if (TREE_CODE (t) != TYPE_DECL)
2762 error ("%<tree%> is not defined as a type");
2763 t = 0;
2765 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2767 error ("%<tree%> is not defined as a pointer type");
2768 t = 0;
2770 else
2771 t = TREE_TYPE (TREE_TYPE (t));
2775 /* Find the underlying type for HOST_WIDE_INT. For the %w
2776 length modifier to work, one must have issued: "typedef
2777 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2778 prior to using that modifier. */
2779 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2781 hwi = identifier_global_value (hwi);
2782 if (hwi)
2784 if (TREE_CODE (hwi) != TYPE_DECL)
2786 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2787 hwi = 0;
2789 else
2791 hwi = DECL_ORIGINAL_TYPE (hwi);
2792 gcc_assert (hwi);
2793 if (hwi != long_integer_type_node
2794 && hwi != long_long_integer_type_node)
2796 error ("%<__gcc_host_wide_int__%> is not defined"
2797 " as %<long%> or %<long long%>");
2798 hwi = 0;
2804 /* Assign the new data for use. */
2806 /* All the GCC diag formats use the same length specs. */
2807 if (!diag_ls)
2808 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2809 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2810 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2811 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2812 diag_ls = (format_length_info *)
2813 xmemdup (gcc_diag_length_specs,
2814 sizeof (gcc_diag_length_specs),
2815 sizeof (gcc_diag_length_specs));
2816 if (hwi)
2818 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2819 i = find_length_info_modifier_index (diag_ls, 'w');
2820 if (hwi == long_integer_type_node)
2821 diag_ls[i].index = FMT_LEN_l;
2822 else if (hwi == long_long_integer_type_node)
2823 diag_ls[i].index = FMT_LEN_ll;
2824 else
2825 gcc_unreachable ();
2828 /* Handle the __gcc_diag__ format specifics. */
2829 if (!diag_fci)
2830 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2831 diag_fci = (format_char_info *)
2832 xmemdup (gcc_diag_char_table,
2833 sizeof (gcc_diag_char_table),
2834 sizeof (gcc_diag_char_table));
2835 if (t)
2837 i = find_char_info_specifier_index (diag_fci, 'K');
2838 diag_fci[i].types[0].type = &t;
2839 diag_fci[i].pointer_count = 1;
2842 /* Handle the __gcc_tdiag__ format specifics. */
2843 if (!tdiag_fci)
2844 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2845 tdiag_fci = (format_char_info *)
2846 xmemdup (gcc_tdiag_char_table,
2847 sizeof (gcc_tdiag_char_table),
2848 sizeof (gcc_tdiag_char_table));
2849 if (t)
2851 /* All specifiers taking a tree share the same struct. */
2852 i = find_char_info_specifier_index (tdiag_fci, 'D');
2853 tdiag_fci[i].types[0].type = &t;
2854 tdiag_fci[i].pointer_count = 1;
2855 i = find_char_info_specifier_index (tdiag_fci, 'K');
2856 tdiag_fci[i].types[0].type = &t;
2857 tdiag_fci[i].pointer_count = 1;
2860 /* Handle the __gcc_cdiag__ format specifics. */
2861 if (!cdiag_fci)
2862 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2863 cdiag_fci = (format_char_info *)
2864 xmemdup (gcc_cdiag_char_table,
2865 sizeof (gcc_cdiag_char_table),
2866 sizeof (gcc_cdiag_char_table));
2867 if (t)
2869 /* All specifiers taking a tree share the same struct. */
2870 i = find_char_info_specifier_index (cdiag_fci, 'D');
2871 cdiag_fci[i].types[0].type = &t;
2872 cdiag_fci[i].pointer_count = 1;
2873 i = find_char_info_specifier_index (cdiag_fci, 'K');
2874 cdiag_fci[i].types[0].type = &t;
2875 cdiag_fci[i].pointer_count = 1;
2878 /* Handle the __gcc_cxxdiag__ format specifics. */
2879 if (!cxxdiag_fci)
2880 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2881 cxxdiag_fci = (format_char_info *)
2882 xmemdup (gcc_cxxdiag_char_table,
2883 sizeof (gcc_cxxdiag_char_table),
2884 sizeof (gcc_cxxdiag_char_table));
2885 if (t)
2887 /* All specifiers taking a tree share the same struct. */
2888 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2889 cxxdiag_fci[i].types[0].type = &t;
2890 cxxdiag_fci[i].pointer_count = 1;
2891 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2892 cxxdiag_fci[i].types[0].type = &t;
2893 cxxdiag_fci[i].pointer_count = 1;
2898 #ifdef TARGET_FORMAT_TYPES
2899 extern const format_kind_info TARGET_FORMAT_TYPES[];
2900 #endif
2902 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2903 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2904 #endif
2905 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2906 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2907 #endif
2909 /* Attributes such as "printf" are equivalent to those such as
2910 "gnu_printf" unless this is overridden by a target. */
2911 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2913 { "gnu_printf", "printf" },
2914 { "gnu_scanf", "scanf" },
2915 { "gnu_strftime", "strftime" },
2916 { "gnu_strfmon", "strfmon" },
2917 { NULL, NULL }
2920 /* Translate to unified attribute name. This is used in decode_format_type and
2921 decode_format_attr. In attr_name the user specified argument is passed. It
2922 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2923 or the attr_name passed to this function, if there is no matching entry. */
2924 static const char *
2925 convert_format_name_to_system_name (const char *attr_name)
2927 int i;
2929 if (attr_name == NULL || *attr_name == 0
2930 || strncmp (attr_name, "gcc_", 4) == 0)
2931 return attr_name;
2932 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2933 TARGET_OVERRIDES_FORMAT_INIT ();
2934 #endif
2936 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2937 /* Check if format attribute is overridden by target. */
2938 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2939 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2941 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2943 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2944 attr_name))
2945 return attr_name;
2946 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2947 attr_name))
2948 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2951 #endif
2952 /* Otherwise default to gnu format. */
2953 for (i = 0;
2954 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2955 ++i)
2957 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2958 attr_name))
2959 return attr_name;
2960 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2961 attr_name))
2962 return gnu_target_overrides_format_attributes[i].named_attr_src;
2965 return attr_name;
2968 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2969 counting "name" and "__name__" as the same, false otherwise. */
2970 static bool
2971 cmp_attribs (const char *tattr_name, const char *attr_name)
2973 int alen = strlen (attr_name);
2974 int slen = (tattr_name ? strlen (tattr_name) : 0);
2975 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2976 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2978 attr_name += 2;
2979 alen -= 4;
2981 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2982 return false;
2983 return true;
2986 /* Handle a "format" attribute; arguments as in
2987 struct attribute_spec.handler. */
2988 tree
2989 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2990 int flags, bool *no_add_attrs)
2992 tree type = *node;
2993 function_format_info info;
2995 #ifdef TARGET_FORMAT_TYPES
2996 /* If the target provides additional format types, we need to
2997 add them to FORMAT_TYPES at first use. */
2998 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
3000 dynamic_format_types = XNEWVEC (format_kind_info,
3001 n_format_types + TARGET_N_FORMAT_TYPES);
3002 memcpy (dynamic_format_types, format_types_orig,
3003 sizeof (format_types_orig));
3004 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
3005 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
3007 format_types = dynamic_format_types;
3008 /* Provide a reference for the first potential external type. */
3009 first_target_format_type = n_format_types;
3010 n_format_types += TARGET_N_FORMAT_TYPES;
3012 #endif
3014 if (!decode_format_attr (args, &info, 0))
3016 *no_add_attrs = true;
3017 return NULL_TREE;
3020 if (prototype_p (type))
3022 if (!check_format_string (type, info.format_num, flags,
3023 no_add_attrs, info.format_type))
3024 return NULL_TREE;
3026 if (info.first_arg_num != 0)
3028 unsigned HOST_WIDE_INT arg_num = 1;
3029 function_args_iterator iter;
3030 tree arg_type;
3032 /* Verify that first_arg_num points to the last arg,
3033 the ... */
3034 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
3035 arg_num++;
3037 if (arg_num != info.first_arg_num)
3039 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
3040 error ("args to be formatted is not %<...%>");
3041 *no_add_attrs = true;
3042 return NULL_TREE;
3047 /* Check if this is a strftime variant. Just for this variant
3048 FMT_FLAG_ARG_CONVERT is not set. */
3049 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3050 && info.first_arg_num != 0)
3052 error ("strftime formats cannot format arguments");
3053 *no_add_attrs = true;
3054 return NULL_TREE;
3057 /* If this is a custom GCC-internal format type, we have to
3058 initialize certain bits at runtime. */
3059 if (info.format_type == asm_fprintf_format_type
3060 || info.format_type == gcc_gfc_format_type
3061 || info.format_type == gcc_diag_format_type
3062 || info.format_type == gcc_tdiag_format_type
3063 || info.format_type == gcc_cdiag_format_type
3064 || info.format_type == gcc_cxxdiag_format_type)
3066 /* Our first time through, we have to make sure that our
3067 format_type data is allocated dynamically and is modifiable. */
3068 if (!dynamic_format_types)
3069 format_types = dynamic_format_types = (format_kind_info *)
3070 xmemdup (format_types_orig, sizeof (format_types_orig),
3071 sizeof (format_types_orig));
3073 /* If this is format __asm_fprintf__, we have to initialize
3074 GCC's notion of HOST_WIDE_INT for checking %wd. */
3075 if (info.format_type == asm_fprintf_format_type)
3076 init_dynamic_asm_fprintf_info ();
3077 /* If this is format __gcc_gfc__, we have to initialize GCC's
3078 notion of 'locus' at runtime for %L. */
3079 else if (info.format_type == gcc_gfc_format_type)
3080 init_dynamic_gfc_info ();
3081 /* If this is one of the diagnostic attributes, then we have to
3082 initialize 'location_t' and 'tree' at runtime. */
3083 else if (info.format_type == gcc_diag_format_type
3084 || info.format_type == gcc_tdiag_format_type
3085 || info.format_type == gcc_cdiag_format_type
3086 || info.format_type == gcc_cxxdiag_format_type)
3087 init_dynamic_diag_info ();
3088 else
3089 gcc_unreachable ();
3092 return NULL_TREE;