2014-04-11 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / c-family / c-format.c
blob4c0313dc85fa90c4bb9cc1ff5716734540173aa1
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_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
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
514 static const format_flag_pair gcc_gfc_flag_pairs[] =
516 { 0, 0, 0, 0 }
519 static const format_flag_spec gcc_diag_flag_specs[] =
521 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
522 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
523 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
524 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
525 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
526 { 0, 0, 0, NULL, NULL, STD_C89 }
529 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
530 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
531 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
533 static const format_flag_spec scanf_flag_specs[] =
535 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
536 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
537 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
538 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
539 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
540 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
541 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
542 { 0, 0, 0, NULL, NULL, STD_C89 }
546 static const format_flag_pair scanf_flag_pairs[] =
548 { '*', 'L', 0, 0 },
549 { 'a', 'm', 0, 0 },
550 { 0, 0, 0, 0 }
554 static const format_flag_spec strftime_flag_specs[] =
556 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
557 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
558 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
559 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
560 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
561 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
562 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
563 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
564 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
565 { 0, 0, 0, NULL, NULL, STD_C89 }
569 static const format_flag_pair strftime_flag_pairs[] =
571 { 'E', 'O', 0, 0 },
572 { '_', '-', 0, 0 },
573 { '_', '0', 0, 0 },
574 { '-', '0', 0, 0 },
575 { '^', '#', 0, 0 },
576 { 0, 0, 0, 0 }
580 static const format_flag_spec strfmon_flag_specs[] =
582 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), 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 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
586 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
587 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
588 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
589 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
590 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
591 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
592 { 0, 0, 0, NULL, NULL, STD_C89 }
595 static const format_flag_pair strfmon_flag_pairs[] =
597 { '+', '(', 0, 0 },
598 { 0, 0, 0, 0 }
602 static const format_char_info print_char_table[] =
604 /* C89 conversion specifiers. */
605 { "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 },
606 { "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 },
607 { "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 },
608 { "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 },
609 { "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 },
610 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
611 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
612 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
613 { "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 },
614 /* C99 conversion specifiers. */
615 { "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 },
616 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
617 /* X/Open conversion specifiers. */
618 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
619 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
620 /* GNU conversion specifiers. */
621 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
622 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
625 static const format_char_info asm_fprintf_char_table[] =
627 /* C89 conversion specifiers. */
628 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
629 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
630 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
631 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
632 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
634 /* asm_fprintf conversion specifiers. */
635 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
636 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
637 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
638 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
639 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
640 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
641 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
642 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
645 static const format_char_info gcc_diag_char_table[] =
647 /* C89 conversion specifiers. */
648 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
649 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
650 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
651 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
652 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
653 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
655 /* Custom conversion specifiers. */
657 /* These will require a "tree" at runtime. */
658 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
660 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
661 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
662 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
663 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
666 static const format_char_info gcc_tdiag_char_table[] =
668 /* C89 conversion specifiers. */
669 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
670 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
671 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
672 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
673 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
674 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
676 /* Custom conversion specifiers. */
678 /* These will require a "tree" at runtime. */
679 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
681 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
683 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
684 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
685 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
686 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
689 static const format_char_info gcc_cdiag_char_table[] =
691 /* C89 conversion specifiers. */
692 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
693 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
694 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
695 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
696 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
697 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
699 /* Custom conversion specifiers. */
701 /* These will require a "tree" at runtime. */
702 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
704 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
706 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
707 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
708 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
709 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
712 static const format_char_info gcc_cxxdiag_char_table[] =
714 /* C89 conversion specifiers. */
715 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
716 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
717 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
718 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
719 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
720 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
722 /* Custom conversion specifiers. */
724 /* These will require a "tree" at runtime. */
725 { "ADEFKSTVX",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
727 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
729 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
730 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
732 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
733 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
734 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
735 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
738 static const format_char_info gcc_gfc_char_table[] =
740 /* C89 conversion specifiers. */
741 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
742 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
743 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
744 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
746 /* gfc conversion specifiers. */
748 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
750 /* This will require a "locus" at runtime. */
751 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
753 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
756 static const format_char_info scan_char_table[] =
758 /* C89 conversion specifiers. */
759 { "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 },
760 { "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 },
761 { "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 },
762 { "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 },
763 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
764 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
765 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
766 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
767 { "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 },
768 /* C99 conversion specifiers. */
769 { "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 },
770 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
771 /* X/Open conversion specifiers. */
772 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
773 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
774 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
777 static const format_char_info time_char_table[] =
779 /* C89 conversion specifiers. */
780 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
781 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
782 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
783 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
784 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
785 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
786 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
787 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
788 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
789 /* C99 conversion specifiers. */
790 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
791 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
792 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
793 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
794 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
795 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
796 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
797 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
798 /* GNU conversion specifiers. */
799 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
800 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
801 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
804 static const format_char_info monetary_char_table[] =
806 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
807 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
810 /* This must be in the same order as enum format_type. */
811 static const format_kind_info format_types_orig[] =
813 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
814 printf_flag_specs, printf_flag_pairs,
815 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
816 'w', 0, 'p', 0, 'L', 0,
817 &integer_type_node, &integer_type_node
819 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
820 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
821 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
822 'w', 0, 'p', 0, 'L', 0,
823 NULL, NULL
825 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
826 gcc_diag_flag_specs, gcc_diag_flag_pairs,
827 FMT_FLAG_ARG_CONVERT,
828 0, 0, 'p', 0, 'L', 0,
829 NULL, &integer_type_node
831 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
832 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
833 FMT_FLAG_ARG_CONVERT,
834 0, 0, 'p', 0, 'L', 0,
835 NULL, &integer_type_node
837 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
838 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
839 FMT_FLAG_ARG_CONVERT,
840 0, 0, 'p', 0, 'L', 0,
841 NULL, &integer_type_node
843 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
844 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
845 FMT_FLAG_ARG_CONVERT,
846 0, 0, 'p', 0, 'L', 0,
847 NULL, &integer_type_node
849 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
850 NULL, gcc_gfc_flag_pairs,
851 FMT_FLAG_ARG_CONVERT,
852 0, 0, 0, 0, 0, 0,
853 NULL, NULL
855 { "NSString", NULL, NULL, NULL, NULL,
856 NULL, NULL,
857 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
858 NULL, NULL
860 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
861 scanf_flag_specs, scanf_flag_pairs,
862 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
863 'w', 0, 0, '*', 'L', 'm',
864 NULL, NULL
866 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
867 strftime_flag_specs, strftime_flag_pairs,
868 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
869 NULL, NULL
871 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
872 strfmon_flag_specs, strfmon_flag_pairs,
873 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
874 NULL, NULL
878 /* This layer of indirection allows GCC to reassign format_types with
879 new data if necessary, while still allowing the original data to be
880 const. */
881 static const format_kind_info *format_types = format_types_orig;
882 /* We can modify this one. We also add target-specific format types
883 to the end of the array. */
884 static format_kind_info *dynamic_format_types;
886 static int n_format_types = ARRAY_SIZE (format_types_orig);
888 /* Structure detailing the results of checking a format function call
889 where the format expression may be a conditional expression with
890 many leaves resulting from nested conditional expressions. */
891 typedef struct
893 /* Number of leaves of the format argument that could not be checked
894 as they were not string literals. */
895 int number_non_literal;
896 /* Number of leaves of the format argument that were null pointers or
897 string literals, but had extra format arguments. */
898 int number_extra_args;
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 } format_check_results;
915 typedef struct
917 format_check_results *res;
918 function_format_info *info;
919 tree params;
920 } format_check_context;
922 /* Return the format name (as specified in the original table) for the format
923 type indicated by format_num. */
924 static const char *
925 format_name (int format_num)
927 if (format_num >= 0 && format_num < n_format_types)
928 return format_types[format_num].name;
929 gcc_unreachable ();
932 /* Return the format flags (as specified in the original table) for the format
933 type indicated by format_num. */
934 static int
935 format_flags (int format_num)
937 if (format_num >= 0 && format_num < n_format_types)
938 return format_types[format_num].flags;
939 gcc_unreachable ();
942 static void check_format_info (function_format_info *, tree);
943 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
944 static void check_format_info_main (format_check_results *,
945 function_format_info *,
946 const char *, int, tree,
947 unsigned HOST_WIDE_INT, alloc_pool);
949 static void init_dollar_format_checking (int, tree);
950 static int maybe_read_dollar_number (const char **, int,
951 tree, tree *, const format_kind_info *);
952 static bool avoid_dollar_number (const char *);
953 static void finish_dollar_format_checking (format_check_results *, int);
955 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
956 int, const char *);
958 static void check_format_types (format_wanted_type *);
959 static void format_type_warning (format_wanted_type *, tree, tree);
961 /* Decode a format type from a string, returning the type, or
962 format_type_error if not valid, in which case the caller should print an
963 error message. */
964 static int
965 decode_format_type (const char *s)
967 int i;
968 int slen;
970 s = convert_format_name_to_system_name (s);
971 slen = strlen (s);
972 for (i = 0; i < n_format_types; i++)
974 int alen;
975 if (!strcmp (s, format_types[i].name))
976 return i;
977 alen = strlen (format_types[i].name);
978 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
979 && s[slen - 1] == '_' && s[slen - 2] == '_'
980 && !strncmp (s + 2, format_types[i].name, alen))
981 return i;
983 return format_type_error;
987 /* Check the argument list of a call to printf, scanf, etc.
988 ATTRS are the attributes on the function type. There are NARGS argument
989 values in the array ARGARRAY.
990 Also, if -Wsuggest-attribute=format,
991 warn for calls to vprintf or vscanf in functions with no such format
992 attribute themselves. */
994 void
995 check_function_format (tree attrs, int nargs, tree *argarray)
997 tree a;
999 /* See if this function has any format attributes. */
1000 for (a = attrs; a; a = TREE_CHAIN (a))
1002 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1004 /* Yup; check it. */
1005 function_format_info info;
1006 decode_format_attr (TREE_VALUE (a), &info, 1);
1007 if (warn_format)
1009 /* FIXME: Rewrite all the internal functions in this file
1010 to use the ARGARRAY directly instead of constructing this
1011 temporary list. */
1012 tree params = NULL_TREE;
1013 int i;
1014 for (i = nargs - 1; i >= 0; i--)
1015 params = tree_cons (NULL_TREE, argarray[i], params);
1016 check_format_info (&info, params);
1018 if (warn_suggest_attribute_format && info.first_arg_num == 0
1019 && (format_types[info.format_type].flags
1020 & (int) FMT_FLAG_ARG_CONVERT))
1022 tree c;
1023 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1025 c = TREE_CHAIN (c))
1026 if (is_attribute_p ("format", TREE_PURPOSE (c))
1027 && (decode_format_type (IDENTIFIER_POINTER
1028 (TREE_VALUE (TREE_VALUE (c))))
1029 == info.format_type))
1030 break;
1031 if (c == NULL_TREE)
1033 /* Check if the current function has a parameter to which
1034 the format attribute could be attached; if not, it
1035 can't be a candidate for a format attribute, despite
1036 the vprintf-like or vscanf-like call. */
1037 tree args;
1038 for (args = DECL_ARGUMENTS (current_function_decl);
1039 args != 0;
1040 args = DECL_CHAIN (args))
1042 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1043 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1044 == char_type_node))
1045 break;
1047 if (args != 0)
1048 warning (OPT_Wsuggest_attribute_format, "function might "
1049 "be possible candidate for %qs format attribute",
1050 format_types[info.format_type].name);
1058 /* Variables used by the checking of $ operand number formats. */
1059 static char *dollar_arguments_used = NULL;
1060 static char *dollar_arguments_pointer_p = NULL;
1061 static int dollar_arguments_alloc = 0;
1062 static int dollar_arguments_count;
1063 static int dollar_first_arg_num;
1064 static int dollar_max_arg_used;
1065 static int dollar_format_warned;
1067 /* Initialize the checking for a format string that may contain $
1068 parameter number specifications; we will need to keep track of whether
1069 each parameter has been used. FIRST_ARG_NUM is the number of the first
1070 argument that is a parameter to the format, or 0 for a vprintf-style
1071 function; PARAMS is the list of arguments starting at this argument. */
1073 static void
1074 init_dollar_format_checking (int first_arg_num, tree params)
1076 tree oparams = params;
1078 dollar_first_arg_num = first_arg_num;
1079 dollar_arguments_count = 0;
1080 dollar_max_arg_used = 0;
1081 dollar_format_warned = 0;
1082 if (first_arg_num > 0)
1084 while (params)
1086 dollar_arguments_count++;
1087 params = TREE_CHAIN (params);
1090 if (dollar_arguments_alloc < dollar_arguments_count)
1092 free (dollar_arguments_used);
1093 free (dollar_arguments_pointer_p);
1094 dollar_arguments_alloc = dollar_arguments_count;
1095 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1096 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1098 if (dollar_arguments_alloc)
1100 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1101 if (first_arg_num > 0)
1103 int i = 0;
1104 params = oparams;
1105 while (params)
1107 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1108 == POINTER_TYPE);
1109 params = TREE_CHAIN (params);
1110 i++;
1117 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1118 is set, it is an error if one is not found; otherwise, it is OK. If
1119 such a number is found, check whether it is within range and mark that
1120 numbered operand as being used for later checking. Returns the operand
1121 number if found and within range, zero if no such number was found and
1122 this is OK, or -1 on error. PARAMS points to the first operand of the
1123 format; PARAM_PTR is made to point to the parameter referred to. If
1124 a $ format is found, *FORMAT is updated to point just after it. */
1126 static int
1127 maybe_read_dollar_number (const char **format,
1128 int dollar_needed, tree params, tree *param_ptr,
1129 const format_kind_info *fki)
1131 int argnum;
1132 int overflow_flag;
1133 const char *fcp = *format;
1134 if (!ISDIGIT (*fcp))
1136 if (dollar_needed)
1138 warning (OPT_Wformat_, "missing $ operand number in format");
1139 return -1;
1141 else
1142 return 0;
1144 argnum = 0;
1145 overflow_flag = 0;
1146 while (ISDIGIT (*fcp))
1148 int nargnum;
1149 nargnum = 10 * argnum + (*fcp - '0');
1150 if (nargnum < 0 || nargnum / 10 != argnum)
1151 overflow_flag = 1;
1152 argnum = nargnum;
1153 fcp++;
1155 if (*fcp != '$')
1157 if (dollar_needed)
1159 warning (OPT_Wformat_, "missing $ operand number in format");
1160 return -1;
1162 else
1163 return 0;
1165 *format = fcp + 1;
1166 if (pedantic && !dollar_format_warned)
1168 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1169 C_STD_NAME (STD_EXT));
1170 dollar_format_warned = 1;
1172 if (overflow_flag || argnum == 0
1173 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1175 warning (OPT_Wformat_, "operand number out of range in format");
1176 return -1;
1178 if (argnum > dollar_max_arg_used)
1179 dollar_max_arg_used = argnum;
1180 /* For vprintf-style functions we may need to allocate more memory to
1181 track which arguments are used. */
1182 while (dollar_arguments_alloc < dollar_max_arg_used)
1184 int nalloc;
1185 nalloc = 2 * dollar_arguments_alloc + 16;
1186 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1187 nalloc);
1188 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1189 nalloc);
1190 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1191 nalloc - dollar_arguments_alloc);
1192 dollar_arguments_alloc = nalloc;
1194 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1195 && dollar_arguments_used[argnum - 1] == 1)
1197 dollar_arguments_used[argnum - 1] = 2;
1198 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1199 argnum, fki->name);
1201 else
1202 dollar_arguments_used[argnum - 1] = 1;
1203 if (dollar_first_arg_num)
1205 int i;
1206 *param_ptr = params;
1207 for (i = 1; i < argnum && *param_ptr != 0; i++)
1208 *param_ptr = TREE_CHAIN (*param_ptr);
1210 /* This case shouldn't be caught here. */
1211 gcc_assert (*param_ptr);
1213 else
1214 *param_ptr = 0;
1215 return argnum;
1218 /* Ensure that FORMAT does not start with a decimal number followed by
1219 a $; give a diagnostic and return true if it does, false otherwise. */
1221 static bool
1222 avoid_dollar_number (const char *format)
1224 if (!ISDIGIT (*format))
1225 return false;
1226 while (ISDIGIT (*format))
1227 format++;
1228 if (*format == '$')
1230 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1231 return true;
1233 return false;
1237 /* Finish the checking for a format string that used $ operand number formats
1238 instead of non-$ formats. We check for unused operands before used ones
1239 (a serious error, since the implementation of the format function
1240 can't know what types to pass to va_arg to find the later arguments).
1241 and for unused operands at the end of the format (if we know how many
1242 arguments the format had, so not for vprintf). If there were operand
1243 numbers out of range on a non-vprintf-style format, we won't have reached
1244 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1245 pointers. */
1247 static void
1248 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1250 int i;
1251 bool found_pointer_gap = false;
1252 for (i = 0; i < dollar_max_arg_used; i++)
1254 if (!dollar_arguments_used[i])
1256 if (pointer_gap_ok && (dollar_first_arg_num == 0
1257 || dollar_arguments_pointer_p[i]))
1258 found_pointer_gap = true;
1259 else
1260 warning (OPT_Wformat_,
1261 "format argument %d unused before used argument %d in $-style format",
1262 i + 1, dollar_max_arg_used);
1265 if (found_pointer_gap
1266 || (dollar_first_arg_num
1267 && dollar_max_arg_used < dollar_arguments_count))
1269 res->number_other--;
1270 res->number_dollar_extra_args++;
1275 /* Retrieve the specification for a format flag. SPEC contains the
1276 specifications for format flags for the applicable kind of format.
1277 FLAG is the flag in question. If PREDICATES is NULL, the basic
1278 spec for that flag must be retrieved and must exist. If
1279 PREDICATES is not NULL, it is a string listing possible predicates
1280 for the spec entry; if an entry predicated on any of these is
1281 found, it is returned, otherwise NULL is returned. */
1283 static const format_flag_spec *
1284 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1286 int i;
1287 for (i = 0; spec[i].flag_char != 0; i++)
1289 if (spec[i].flag_char != flag)
1290 continue;
1291 if (predicates != NULL)
1293 if (spec[i].predicate != 0
1294 && strchr (predicates, spec[i].predicate) != 0)
1295 return &spec[i];
1297 else if (spec[i].predicate == 0)
1298 return &spec[i];
1300 gcc_assert (predicates);
1301 return NULL;
1305 /* Check the argument list of a call to printf, scanf, etc.
1306 INFO points to the function_format_info structure.
1307 PARAMS is the list of argument values. */
1309 static void
1310 check_format_info (function_format_info *info, tree params)
1312 format_check_context format_ctx;
1313 unsigned HOST_WIDE_INT arg_num;
1314 tree format_tree;
1315 format_check_results res;
1316 /* Skip to format argument. If the argument isn't available, there's
1317 no work for us to do; prototype checking will catch the problem. */
1318 for (arg_num = 1; ; ++arg_num)
1320 if (params == 0)
1321 return;
1322 if (arg_num == info->format_num)
1323 break;
1324 params = TREE_CHAIN (params);
1326 format_tree = TREE_VALUE (params);
1327 params = TREE_CHAIN (params);
1328 if (format_tree == 0)
1329 return;
1331 res.number_non_literal = 0;
1332 res.number_extra_args = 0;
1333 res.number_dollar_extra_args = 0;
1334 res.number_wide = 0;
1335 res.number_empty = 0;
1336 res.number_unterminated = 0;
1337 res.number_other = 0;
1339 format_ctx.res = &res;
1340 format_ctx.info = info;
1341 format_ctx.params = params;
1343 check_function_arguments_recurse (check_format_arg, &format_ctx,
1344 format_tree, arg_num);
1346 if (res.number_non_literal > 0)
1348 /* Functions taking a va_list normally pass a non-literal format
1349 string. These functions typically are declared with
1350 first_arg_num == 0, so avoid warning in those cases. */
1351 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1353 /* For strftime-like formats, warn for not checking the format
1354 string; but there are no arguments to check. */
1355 warning (OPT_Wformat_nonliteral,
1356 "format not a string literal, format string not checked");
1358 else if (info->first_arg_num != 0)
1360 /* If there are no arguments for the format at all, we may have
1361 printf (foo) which is likely to be a security hole. */
1362 while (arg_num + 1 < info->first_arg_num)
1364 if (params == 0)
1365 break;
1366 params = TREE_CHAIN (params);
1367 ++arg_num;
1369 if (params == 0 && warn_format_security)
1370 warning (OPT_Wformat_security,
1371 "format not a string literal and no format arguments");
1372 else if (params == 0 && warn_format_nonliteral)
1373 warning (OPT_Wformat_nonliteral,
1374 "format not a string literal and no format arguments");
1375 else
1376 warning (OPT_Wformat_nonliteral,
1377 "format not a string literal, argument types not checked");
1381 /* If there were extra arguments to the format, normally warn. However,
1382 the standard does say extra arguments are ignored, so in the specific
1383 case where we have multiple leaves (conditional expressions or
1384 ngettext) allow extra arguments if at least one leaf didn't have extra
1385 arguments, but was otherwise OK (either non-literal or checked OK).
1386 If the format is an empty string, this should be counted similarly to the
1387 case of extra format arguments. */
1388 if (res.number_extra_args > 0 && res.number_non_literal == 0
1389 && res.number_other == 0)
1390 warning (OPT_Wformat_extra_args, "too many arguments for format");
1391 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1392 && res.number_other == 0)
1393 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1394 if (res.number_empty > 0 && res.number_non_literal == 0
1395 && res.number_other == 0)
1396 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1397 format_types[info->format_type].name);
1399 if (res.number_wide > 0)
1400 warning (OPT_Wformat_, "format is a wide character string");
1402 if (res.number_unterminated > 0)
1403 warning (OPT_Wformat_, "unterminated format string");
1406 /* Callback from check_function_arguments_recurse to check a
1407 format string. FORMAT_TREE is the format parameter. ARG_NUM
1408 is the number of the format argument. CTX points to a
1409 format_check_context. */
1411 static void
1412 check_format_arg (void *ctx, tree format_tree,
1413 unsigned HOST_WIDE_INT arg_num)
1415 format_check_context *format_ctx = (format_check_context *) ctx;
1416 format_check_results *res = format_ctx->res;
1417 function_format_info *info = format_ctx->info;
1418 tree params = format_ctx->params;
1420 int format_length;
1421 HOST_WIDE_INT offset;
1422 const char *format_chars;
1423 tree array_size = 0;
1424 tree array_init;
1425 alloc_pool fwt_pool;
1427 if (integer_zerop (format_tree))
1429 /* Skip to first argument to check, so we can see if this format
1430 has any arguments (it shouldn't). */
1431 while (arg_num + 1 < info->first_arg_num)
1433 if (params == 0)
1434 return;
1435 params = TREE_CHAIN (params);
1436 ++arg_num;
1439 if (params == 0)
1440 res->number_other++;
1441 else
1442 res->number_extra_args++;
1444 return;
1447 offset = 0;
1448 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1450 tree arg0, arg1;
1452 arg0 = TREE_OPERAND (format_tree, 0);
1453 arg1 = TREE_OPERAND (format_tree, 1);
1454 STRIP_NOPS (arg0);
1455 STRIP_NOPS (arg1);
1456 if (TREE_CODE (arg1) == INTEGER_CST)
1457 format_tree = arg0;
1458 else
1460 res->number_non_literal++;
1461 return;
1463 if (!tree_fits_shwi_p (arg1)
1464 || (offset = tree_to_shwi (arg1)) < 0)
1466 res->number_non_literal++;
1467 return;
1470 if (TREE_CODE (format_tree) != ADDR_EXPR)
1472 res->number_non_literal++;
1473 return;
1475 format_tree = TREE_OPERAND (format_tree, 0);
1476 if (format_types[info->format_type].flags
1477 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1479 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1480 /* We cannot examine this string here - but we can check that it is
1481 a valid type. */
1482 if (TREE_CODE (format_tree) != CONST_DECL
1483 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1484 || (*targetcm.string_object_ref_type_p)
1485 ((const_tree) TREE_TYPE (format_tree))))
1487 res->number_non_literal++;
1488 return;
1490 /* Skip to first argument to check. */
1491 while (arg_num + 1 < info->first_arg_num)
1493 if (params == 0)
1494 return;
1495 params = TREE_CHAIN (params);
1496 ++arg_num;
1498 /* So, we have a valid literal string object and one or more params.
1499 We need to use an external helper to parse the string into format
1500 info. For Objective-C variants we provide the resource within the
1501 objc tree, for target variants, via a hook. */
1502 if (objc_str)
1503 objc_check_format_arg (format_tree, params);
1504 else if (targetcm.check_string_object_format_arg)
1505 (*targetcm.check_string_object_format_arg) (format_tree, params);
1506 /* Else we can't handle it and retire quietly. */
1507 return;
1509 if (TREE_CODE (format_tree) == ARRAY_REF
1510 && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1511 && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1512 format_tree = TREE_OPERAND (format_tree, 0);
1513 if (TREE_CODE (format_tree) == VAR_DECL
1514 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1515 && (array_init = decl_constant_value (format_tree)) != format_tree
1516 && TREE_CODE (array_init) == STRING_CST)
1518 /* Extract the string constant initializer. Note that this may include
1519 a trailing NUL character that is not in the array (e.g.
1520 const char a[3] = "foo";). */
1521 array_size = DECL_SIZE_UNIT (format_tree);
1522 format_tree = array_init;
1524 if (TREE_CODE (format_tree) != STRING_CST)
1526 res->number_non_literal++;
1527 return;
1529 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1531 res->number_wide++;
1532 return;
1534 format_chars = TREE_STRING_POINTER (format_tree);
1535 format_length = TREE_STRING_LENGTH (format_tree);
1536 if (array_size != 0)
1538 /* Variable length arrays can't be initialized. */
1539 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1541 if (tree_fits_shwi_p (array_size))
1543 HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1544 if (array_size_value > 0
1545 && array_size_value == (int) array_size_value
1546 && format_length > array_size_value)
1547 format_length = array_size_value;
1550 if (offset)
1552 if (offset >= format_length)
1554 res->number_non_literal++;
1555 return;
1557 format_chars += offset;
1558 format_length -= offset;
1560 if (format_length < 1 || format_chars[--format_length] != 0)
1562 res->number_unterminated++;
1563 return;
1565 if (format_length == 0)
1567 res->number_empty++;
1568 return;
1571 /* Skip to first argument to check. */
1572 while (arg_num + 1 < info->first_arg_num)
1574 if (params == 0)
1575 return;
1576 params = TREE_CHAIN (params);
1577 ++arg_num;
1579 /* Provisionally increment res->number_other; check_format_info_main
1580 will decrement it if it finds there are extra arguments, but this way
1581 need not adjust it for every return. */
1582 res->number_other++;
1583 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1584 sizeof (format_wanted_type), 10);
1585 check_format_info_main (res, info, format_chars, format_length,
1586 params, arg_num, fwt_pool);
1587 free_alloc_pool (fwt_pool);
1591 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1592 is the NUL-terminated format string (which at this point may contain
1593 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1594 terminating NUL character). ARG_NUM is one less than the number of
1595 the first format argument to check; PARAMS points to that format
1596 argument in the list of arguments. */
1598 static void
1599 check_format_info_main (format_check_results *res,
1600 function_format_info *info, const char *format_chars,
1601 int format_length, tree params,
1602 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1604 const char *orig_format_chars = format_chars;
1605 tree first_fillin_param = params;
1607 const format_kind_info *fki = &format_types[info->format_type];
1608 const format_flag_spec *flag_specs = fki->flag_specs;
1609 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1611 /* -1 if no conversions taking an operand have been found; 0 if one has
1612 and it didn't use $; 1 if $ formats are in use. */
1613 int has_operand_number = -1;
1615 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1617 while (*format_chars != 0)
1619 int i;
1620 int suppressed = FALSE;
1621 const char *length_chars = NULL;
1622 enum format_lengths length_chars_val = FMT_LEN_none;
1623 enum format_std_version length_chars_std = STD_C89;
1624 int format_char;
1625 tree cur_param;
1626 tree wanted_type;
1627 int main_arg_num = 0;
1628 tree main_arg_params = 0;
1629 enum format_std_version wanted_type_std;
1630 const char *wanted_type_name;
1631 format_wanted_type width_wanted_type;
1632 format_wanted_type precision_wanted_type;
1633 format_wanted_type main_wanted_type;
1634 format_wanted_type *first_wanted_type = NULL;
1635 format_wanted_type *last_wanted_type = NULL;
1636 const format_length_info *fli = NULL;
1637 const format_char_info *fci = NULL;
1638 char flag_chars[256];
1639 int alloc_flag = 0;
1640 int scalar_identity_flag = 0;
1641 const char *format_start;
1643 if (*format_chars++ != '%')
1644 continue;
1645 if (*format_chars == 0)
1647 warning (OPT_Wformat_, "spurious trailing %<%%%> in format");
1648 continue;
1650 if (*format_chars == '%')
1652 ++format_chars;
1653 continue;
1655 flag_chars[0] = 0;
1657 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1659 /* Possibly read a $ operand number at the start of the format.
1660 If one was previously used, one is required here. If one
1661 is not used here, we can't immediately conclude this is a
1662 format without them, since it could be printf %m or scanf %*. */
1663 int opnum;
1664 opnum = maybe_read_dollar_number (&format_chars, 0,
1665 first_fillin_param,
1666 &main_arg_params, fki);
1667 if (opnum == -1)
1668 return;
1669 else if (opnum > 0)
1671 has_operand_number = 1;
1672 main_arg_num = opnum + info->first_arg_num - 1;
1675 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1677 if (avoid_dollar_number (format_chars))
1678 return;
1681 /* Read any format flags, but do not yet validate them beyond removing
1682 duplicates, since in general validation depends on the rest of
1683 the format. */
1684 while (*format_chars != 0
1685 && strchr (fki->flag_chars, *format_chars) != 0)
1687 const format_flag_spec *s = get_flag_spec (flag_specs,
1688 *format_chars, NULL);
1689 if (strchr (flag_chars, *format_chars) != 0)
1691 warning (OPT_Wformat_, "repeated %s in format", _(s->name));
1693 else
1695 i = strlen (flag_chars);
1696 flag_chars[i++] = *format_chars;
1697 flag_chars[i] = 0;
1699 if (s->skip_next_char)
1701 ++format_chars;
1702 if (*format_chars == 0)
1704 warning (OPT_Wformat_, "missing fill character at end of strfmon format");
1705 return;
1708 ++format_chars;
1711 /* Read any format width, possibly * or *m$. */
1712 if (fki->width_char != 0)
1714 if (fki->width_type != NULL && *format_chars == '*')
1716 i = strlen (flag_chars);
1717 flag_chars[i++] = fki->width_char;
1718 flag_chars[i] = 0;
1719 /* "...a field width...may be indicated by an asterisk.
1720 In this case, an int argument supplies the field width..." */
1721 ++format_chars;
1722 if (has_operand_number != 0)
1724 int opnum;
1725 opnum = maybe_read_dollar_number (&format_chars,
1726 has_operand_number == 1,
1727 first_fillin_param,
1728 &params, fki);
1729 if (opnum == -1)
1730 return;
1731 else if (opnum > 0)
1733 has_operand_number = 1;
1734 arg_num = opnum + info->first_arg_num - 1;
1736 else
1737 has_operand_number = 0;
1739 else
1741 if (avoid_dollar_number (format_chars))
1742 return;
1744 if (info->first_arg_num != 0)
1746 if (params == 0)
1747 cur_param = NULL;
1748 else
1750 cur_param = TREE_VALUE (params);
1751 if (has_operand_number <= 0)
1753 params = TREE_CHAIN (params);
1754 ++arg_num;
1757 width_wanted_type.wanted_type = *fki->width_type;
1758 width_wanted_type.wanted_type_name = NULL;
1759 width_wanted_type.pointer_count = 0;
1760 width_wanted_type.char_lenient_flag = 0;
1761 width_wanted_type.scalar_identity_flag = 0;
1762 width_wanted_type.writing_in_flag = 0;
1763 width_wanted_type.reading_from_flag = 0;
1764 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1765 width_wanted_type.format_start = format_chars - 1;
1766 width_wanted_type.format_length = 1;
1767 width_wanted_type.param = cur_param;
1768 width_wanted_type.arg_num = arg_num;
1769 width_wanted_type.next = NULL;
1770 if (last_wanted_type != 0)
1771 last_wanted_type->next = &width_wanted_type;
1772 if (first_wanted_type == 0)
1773 first_wanted_type = &width_wanted_type;
1774 last_wanted_type = &width_wanted_type;
1777 else
1779 /* Possibly read a numeric width. If the width is zero,
1780 we complain if appropriate. */
1781 int non_zero_width_char = FALSE;
1782 int found_width = FALSE;
1783 while (ISDIGIT (*format_chars))
1785 found_width = TRUE;
1786 if (*format_chars != '0')
1787 non_zero_width_char = TRUE;
1788 ++format_chars;
1790 if (found_width && !non_zero_width_char &&
1791 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1792 warning (OPT_Wformat_, "zero width in %s format", fki->name);
1793 if (found_width)
1795 i = strlen (flag_chars);
1796 flag_chars[i++] = fki->width_char;
1797 flag_chars[i] = 0;
1802 /* Read any format left precision (must be a number, not *). */
1803 if (fki->left_precision_char != 0 && *format_chars == '#')
1805 ++format_chars;
1806 i = strlen (flag_chars);
1807 flag_chars[i++] = fki->left_precision_char;
1808 flag_chars[i] = 0;
1809 if (!ISDIGIT (*format_chars))
1810 warning (OPT_Wformat_, "empty left precision in %s format", fki->name);
1811 while (ISDIGIT (*format_chars))
1812 ++format_chars;
1815 /* Read any format precision, possibly * or *m$. */
1816 if (fki->precision_char != 0 && *format_chars == '.')
1818 ++format_chars;
1819 i = strlen (flag_chars);
1820 flag_chars[i++] = fki->precision_char;
1821 flag_chars[i] = 0;
1822 if (fki->precision_type != NULL && *format_chars == '*')
1824 /* "...a...precision...may be indicated by an asterisk.
1825 In this case, an int argument supplies the...precision." */
1826 ++format_chars;
1827 if (has_operand_number != 0)
1829 int opnum;
1830 opnum = maybe_read_dollar_number (&format_chars,
1831 has_operand_number == 1,
1832 first_fillin_param,
1833 &params, fki);
1834 if (opnum == -1)
1835 return;
1836 else if (opnum > 0)
1838 has_operand_number = 1;
1839 arg_num = opnum + info->first_arg_num - 1;
1841 else
1842 has_operand_number = 0;
1844 else
1846 if (avoid_dollar_number (format_chars))
1847 return;
1849 if (info->first_arg_num != 0)
1851 if (params == 0)
1852 cur_param = NULL;
1853 else
1855 cur_param = TREE_VALUE (params);
1856 if (has_operand_number <= 0)
1858 params = TREE_CHAIN (params);
1859 ++arg_num;
1862 precision_wanted_type.wanted_type = *fki->precision_type;
1863 precision_wanted_type.wanted_type_name = NULL;
1864 precision_wanted_type.pointer_count = 0;
1865 precision_wanted_type.char_lenient_flag = 0;
1866 precision_wanted_type.scalar_identity_flag = 0;
1867 precision_wanted_type.writing_in_flag = 0;
1868 precision_wanted_type.reading_from_flag = 0;
1869 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1870 precision_wanted_type.param = cur_param;
1871 precision_wanted_type.format_start = format_chars - 2;
1872 precision_wanted_type.format_length = 2;
1873 precision_wanted_type.arg_num = arg_num;
1874 precision_wanted_type.next = NULL;
1875 if (last_wanted_type != 0)
1876 last_wanted_type->next = &precision_wanted_type;
1877 if (first_wanted_type == 0)
1878 first_wanted_type = &precision_wanted_type;
1879 last_wanted_type = &precision_wanted_type;
1882 else
1884 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1885 && !ISDIGIT (*format_chars))
1886 warning (OPT_Wformat_, "empty precision in %s format", fki->name);
1887 while (ISDIGIT (*format_chars))
1888 ++format_chars;
1892 format_start = format_chars;
1893 if (fki->alloc_char && fki->alloc_char == *format_chars)
1895 i = strlen (flag_chars);
1896 flag_chars[i++] = fki->alloc_char;
1897 flag_chars[i] = 0;
1898 format_chars++;
1901 /* Handle the scanf allocation kludge. */
1902 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1904 if (*format_chars == 'a' && !flag_isoc99)
1906 if (format_chars[1] == 's' || format_chars[1] == 'S'
1907 || format_chars[1] == '[')
1909 /* 'a' is used as a flag. */
1910 i = strlen (flag_chars);
1911 flag_chars[i++] = 'a';
1912 flag_chars[i] = 0;
1913 format_chars++;
1918 /* Read any length modifier, if this kind of format has them. */
1919 fli = fki->length_char_specs;
1920 length_chars = NULL;
1921 length_chars_val = FMT_LEN_none;
1922 length_chars_std = STD_C89;
1923 scalar_identity_flag = 0;
1924 if (fli)
1926 while (fli->name != 0
1927 && strncmp (fli->name, format_chars, strlen (fli->name)))
1928 fli++;
1929 if (fli->name != 0)
1931 format_chars += strlen (fli->name);
1932 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1934 format_chars++;
1935 length_chars = fli->double_name;
1936 length_chars_val = fli->double_index;
1937 length_chars_std = fli->double_std;
1939 else
1941 length_chars = fli->name;
1942 length_chars_val = fli->index;
1943 length_chars_std = fli->std;
1944 scalar_identity_flag = fli->scalar_identity_flag;
1946 i = strlen (flag_chars);
1947 flag_chars[i++] = fki->length_code_char;
1948 flag_chars[i] = 0;
1950 if (pedantic)
1952 /* Warn if the length modifier is non-standard. */
1953 if (ADJ_STD (length_chars_std) > C_STD_VER)
1954 warning (OPT_Wformat_,
1955 "%s does not support the %qs %s length modifier",
1956 C_STD_NAME (length_chars_std), length_chars,
1957 fki->name);
1961 /* Read any modifier (strftime E/O). */
1962 if (fki->modifier_chars != NULL)
1964 while (*format_chars != 0
1965 && strchr (fki->modifier_chars, *format_chars) != 0)
1967 if (strchr (flag_chars, *format_chars) != 0)
1969 const format_flag_spec *s = get_flag_spec (flag_specs,
1970 *format_chars, NULL);
1971 warning (OPT_Wformat_, "repeated %s in format", _(s->name));
1973 else
1975 i = strlen (flag_chars);
1976 flag_chars[i++] = *format_chars;
1977 flag_chars[i] = 0;
1979 ++format_chars;
1983 format_char = *format_chars;
1984 if (format_char == 0
1985 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1986 && format_char == '%'))
1988 warning (OPT_Wformat_, "conversion lacks type at end of format");
1989 continue;
1991 format_chars++;
1992 fci = fki->conversion_specs;
1993 while (fci->format_chars != 0
1994 && strchr (fci->format_chars, format_char) == 0)
1995 ++fci;
1996 if (fci->format_chars == 0)
1998 if (ISGRAPH (format_char))
1999 warning (OPT_Wformat_, "unknown conversion type character %qc in format",
2000 format_char);
2001 else
2002 warning (OPT_Wformat_, "unknown conversion type character 0x%x in format",
2003 format_char);
2004 continue;
2006 if (pedantic)
2008 if (ADJ_STD (fci->std) > C_STD_VER)
2009 warning (OPT_Wformat_, "%s does not support the %<%%%c%> %s format",
2010 C_STD_NAME (fci->std), format_char, fki->name);
2013 /* Validate the individual flags used, removing any that are invalid. */
2015 int d = 0;
2016 for (i = 0; flag_chars[i] != 0; i++)
2018 const format_flag_spec *s = get_flag_spec (flag_specs,
2019 flag_chars[i], NULL);
2020 flag_chars[i - d] = flag_chars[i];
2021 if (flag_chars[i] == fki->length_code_char)
2022 continue;
2023 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2025 warning (OPT_Wformat_, "%s used with %<%%%c%> %s format",
2026 _(s->name), format_char, fki->name);
2027 d++;
2028 continue;
2030 if (pedantic)
2032 const format_flag_spec *t;
2033 if (ADJ_STD (s->std) > C_STD_VER)
2034 warning (OPT_Wformat_, "%s does not support %s",
2035 C_STD_NAME (s->std), _(s->long_name));
2036 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2037 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2039 const char *long_name = (t->long_name != NULL
2040 ? t->long_name
2041 : s->long_name);
2042 if (ADJ_STD (t->std) > C_STD_VER)
2043 warning (OPT_Wformat_,
2044 "%s does not support %s with the %<%%%c%> %s format",
2045 C_STD_NAME (t->std), _(long_name),
2046 format_char, fki->name);
2050 flag_chars[i - d] = 0;
2053 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2054 && strchr (flag_chars, 'a') != 0)
2055 alloc_flag = 1;
2056 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2057 alloc_flag = 1;
2059 if (fki->suppression_char
2060 && strchr (flag_chars, fki->suppression_char) != 0)
2061 suppressed = 1;
2063 /* Validate the pairs of flags used. */
2064 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2066 const format_flag_spec *s, *t;
2067 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2068 continue;
2069 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2070 continue;
2071 if (bad_flag_pairs[i].predicate != 0
2072 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2073 continue;
2074 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2075 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2076 if (bad_flag_pairs[i].ignored)
2078 if (bad_flag_pairs[i].predicate != 0)
2079 warning (OPT_Wformat_,
2080 "%s ignored with %s and %<%%%c%> %s format",
2081 _(s->name), _(t->name), format_char,
2082 fki->name);
2083 else
2084 warning (OPT_Wformat_, "%s ignored with %s in %s format",
2085 _(s->name), _(t->name), fki->name);
2087 else
2089 if (bad_flag_pairs[i].predicate != 0)
2090 warning (OPT_Wformat_,
2091 "use of %s and %s together with %<%%%c%> %s format",
2092 _(s->name), _(t->name), format_char,
2093 fki->name);
2094 else
2095 warning (OPT_Wformat_, "use of %s and %s together in %s format",
2096 _(s->name), _(t->name), fki->name);
2100 /* Give Y2K warnings. */
2101 if (warn_format_y2k)
2103 int y2k_level = 0;
2104 if (strchr (fci->flags2, '4') != 0)
2105 if (strchr (flag_chars, 'E') != 0)
2106 y2k_level = 3;
2107 else
2108 y2k_level = 2;
2109 else if (strchr (fci->flags2, '3') != 0)
2110 y2k_level = 3;
2111 else if (strchr (fci->flags2, '2') != 0)
2112 y2k_level = 2;
2113 if (y2k_level == 3)
2114 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2115 "year in some locales", format_char);
2116 else if (y2k_level == 2)
2117 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2118 "year", format_char);
2121 if (strchr (fci->flags2, '[') != 0)
2123 /* Skip over scan set, in case it happens to have '%' in it. */
2124 if (*format_chars == '^')
2125 ++format_chars;
2126 /* Find closing bracket; if one is hit immediately, then
2127 it's part of the scan set rather than a terminator. */
2128 if (*format_chars == ']')
2129 ++format_chars;
2130 while (*format_chars && *format_chars != ']')
2131 ++format_chars;
2132 if (*format_chars != ']')
2133 /* The end of the format string was reached. */
2134 warning (OPT_Wformat_, "no closing %<]%> for %<%%[%> format");
2137 wanted_type = 0;
2138 wanted_type_name = 0;
2139 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2141 wanted_type = (fci->types[length_chars_val].type
2142 ? *fci->types[length_chars_val].type : 0);
2143 wanted_type_name = fci->types[length_chars_val].name;
2144 wanted_type_std = fci->types[length_chars_val].std;
2145 if (wanted_type == 0)
2147 warning (OPT_Wformat_,
2148 "use of %qs length modifier with %qc type character",
2149 length_chars, format_char);
2150 /* Heuristic: skip one argument when an invalid length/type
2151 combination is encountered. */
2152 arg_num++;
2153 if (params != 0)
2154 params = TREE_CHAIN (params);
2155 continue;
2157 else if (pedantic
2158 /* Warn if non-standard, provided it is more non-standard
2159 than the length and type characters that may already
2160 have been warned for. */
2161 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2162 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2164 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2165 warning (OPT_Wformat_,
2166 "%s does not support the %<%%%s%c%> %s format",
2167 C_STD_NAME (wanted_type_std), length_chars,
2168 format_char, fki->name);
2172 main_wanted_type.next = NULL;
2174 /* Finally. . .check type of argument against desired type! */
2175 if (info->first_arg_num == 0)
2176 continue;
2177 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2178 || suppressed)
2180 if (main_arg_num != 0)
2182 if (suppressed)
2183 warning (OPT_Wformat_, "operand number specified with "
2184 "suppressed assignment");
2185 else
2186 warning (OPT_Wformat_, "operand number specified for format "
2187 "taking no argument");
2190 else
2192 format_wanted_type *wanted_type_ptr;
2194 if (main_arg_num != 0)
2196 arg_num = main_arg_num;
2197 params = main_arg_params;
2199 else
2201 ++arg_num;
2202 if (has_operand_number > 0)
2204 warning (OPT_Wformat_, "missing $ operand number in format");
2205 return;
2207 else
2208 has_operand_number = 0;
2211 wanted_type_ptr = &main_wanted_type;
2212 while (fci)
2214 if (params == 0)
2215 cur_param = NULL;
2216 else
2218 cur_param = TREE_VALUE (params);
2219 params = TREE_CHAIN (params);
2222 wanted_type_ptr->wanted_type = wanted_type;
2223 wanted_type_ptr->wanted_type_name = wanted_type_name;
2224 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2225 wanted_type_ptr->char_lenient_flag = 0;
2226 if (strchr (fci->flags2, 'c') != 0)
2227 wanted_type_ptr->char_lenient_flag = 1;
2228 wanted_type_ptr->scalar_identity_flag = 0;
2229 if (scalar_identity_flag)
2230 wanted_type_ptr->scalar_identity_flag = 1;
2231 wanted_type_ptr->writing_in_flag = 0;
2232 wanted_type_ptr->reading_from_flag = 0;
2233 if (alloc_flag)
2234 wanted_type_ptr->writing_in_flag = 1;
2235 else
2237 if (strchr (fci->flags2, 'W') != 0)
2238 wanted_type_ptr->writing_in_flag = 1;
2239 if (strchr (fci->flags2, 'R') != 0)
2240 wanted_type_ptr->reading_from_flag = 1;
2242 wanted_type_ptr->kind = CF_KIND_FORMAT;
2243 wanted_type_ptr->param = cur_param;
2244 wanted_type_ptr->arg_num = arg_num;
2245 wanted_type_ptr->format_start = format_start;
2246 wanted_type_ptr->format_length = format_chars - format_start;
2247 wanted_type_ptr->next = NULL;
2248 if (last_wanted_type != 0)
2249 last_wanted_type->next = wanted_type_ptr;
2250 if (first_wanted_type == 0)
2251 first_wanted_type = wanted_type_ptr;
2252 last_wanted_type = wanted_type_ptr;
2254 fci = fci->chain;
2255 if (fci)
2257 wanted_type_ptr = (format_wanted_type *)
2258 pool_alloc (fwt_pool);
2259 arg_num++;
2260 wanted_type = *fci->types[length_chars_val].type;
2261 wanted_type_name = fci->types[length_chars_val].name;
2266 if (first_wanted_type != 0)
2267 check_format_types (first_wanted_type);
2270 if (format_chars - orig_format_chars != format_length)
2271 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
2272 if (info->first_arg_num != 0 && params != 0
2273 && has_operand_number <= 0)
2275 res->number_other--;
2276 res->number_extra_args++;
2278 if (has_operand_number > 0)
2279 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2283 /* Check the argument types from a single format conversion (possibly
2284 including width and precision arguments). */
2285 static void
2286 check_format_types (format_wanted_type *types)
2288 for (; types != 0; types = types->next)
2290 tree cur_param;
2291 tree cur_type;
2292 tree orig_cur_type;
2293 tree wanted_type;
2294 int arg_num;
2295 int i;
2296 int char_type_flag;
2298 wanted_type = types->wanted_type;
2299 arg_num = types->arg_num;
2301 /* The following should not occur here. */
2302 gcc_assert (wanted_type);
2303 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2305 if (types->pointer_count == 0)
2306 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2308 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2310 cur_param = types->param;
2311 if (!cur_param)
2313 format_type_warning (types, wanted_type, NULL);
2314 continue;
2317 cur_type = TREE_TYPE (cur_param);
2318 if (cur_type == error_mark_node)
2319 continue;
2320 orig_cur_type = cur_type;
2321 char_type_flag = 0;
2323 STRIP_NOPS (cur_param);
2325 /* Check the types of any additional pointer arguments
2326 that precede the "real" argument. */
2327 for (i = 0; i < types->pointer_count; ++i)
2329 if (TREE_CODE (cur_type) == POINTER_TYPE)
2331 cur_type = TREE_TYPE (cur_type);
2332 if (cur_type == error_mark_node)
2333 break;
2335 /* Check for writing through a NULL pointer. */
2336 if (types->writing_in_flag
2337 && i == 0
2338 && cur_param != 0
2339 && integer_zerop (cur_param))
2340 warning (OPT_Wformat_, "writing through null pointer "
2341 "(argument %d)", arg_num);
2343 /* Check for reading through a NULL pointer. */
2344 if (types->reading_from_flag
2345 && i == 0
2346 && cur_param != 0
2347 && integer_zerop (cur_param))
2348 warning (OPT_Wformat_, "reading through null pointer "
2349 "(argument %d)", arg_num);
2351 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2352 cur_param = TREE_OPERAND (cur_param, 0);
2353 else
2354 cur_param = 0;
2356 /* See if this is an attempt to write into a const type with
2357 scanf or with printf "%n". Note: the writing in happens
2358 at the first indirection only, if for example
2359 void * const * is passed to scanf %p; passing
2360 const void ** is simply passing an incompatible type. */
2361 if (types->writing_in_flag
2362 && i == 0
2363 && (TYPE_READONLY (cur_type)
2364 || (cur_param != 0
2365 && (CONSTANT_CLASS_P (cur_param)
2366 || (DECL_P (cur_param)
2367 && TREE_READONLY (cur_param))))))
2368 warning (OPT_Wformat_, "writing into constant object "
2369 "(argument %d)", arg_num);
2371 /* If there are extra type qualifiers beyond the first
2372 indirection, then this makes the types technically
2373 incompatible. */
2374 if (i > 0
2375 && pedantic
2376 && (TYPE_READONLY (cur_type)
2377 || TYPE_VOLATILE (cur_type)
2378 || TYPE_ATOMIC (cur_type)
2379 || TYPE_RESTRICT (cur_type)))
2380 warning (OPT_Wformat_, "extra type qualifiers in format "
2381 "argument (argument %d)",
2382 arg_num);
2385 else
2387 format_type_warning (types, wanted_type, orig_cur_type);
2388 break;
2392 if (i < types->pointer_count)
2393 continue;
2395 cur_type = TYPE_MAIN_VARIANT (cur_type);
2397 /* Check whether the argument type is a character type. This leniency
2398 only applies to certain formats, flagged with 'c'.
2400 if (types->char_lenient_flag)
2401 char_type_flag = (cur_type == char_type_node
2402 || cur_type == signed_char_type_node
2403 || cur_type == unsigned_char_type_node);
2405 /* Check the type of the "real" argument, if there's a type we want. */
2406 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2407 continue;
2408 /* If we want 'void *', allow any pointer type.
2409 (Anything else would already have got a warning.)
2410 With -Wpedantic, only allow pointers to void and to character
2411 types. */
2412 if (wanted_type == void_type_node
2413 && (!pedantic || (i == 1 && char_type_flag)))
2414 continue;
2415 /* Don't warn about differences merely in signedness, unless
2416 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2417 target and not a character type, and for character types at
2418 a second level of indirection. */
2419 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2420 && TREE_CODE (cur_type) == INTEGER_TYPE
2421 && ((!pedantic && !warn_format_signedness)
2422 || (i == 0 && !warn_format_signedness)
2423 || (i == 1 && char_type_flag))
2424 && (TYPE_UNSIGNED (wanted_type)
2425 ? wanted_type == c_common_unsigned_type (cur_type)
2426 : wanted_type == c_common_signed_type (cur_type)))
2427 continue;
2428 /* Likewise, "signed char", "unsigned char" and "char" are
2429 equivalent but the above test won't consider them equivalent. */
2430 if (wanted_type == char_type_node
2431 && (!pedantic || i < 2)
2432 && char_type_flag)
2433 continue;
2434 if (types->scalar_identity_flag
2435 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2436 || (INTEGRAL_TYPE_P (cur_type)
2437 && INTEGRAL_TYPE_P (wanted_type)))
2438 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2439 continue;
2440 /* Now we have a type mismatch. */
2441 format_type_warning (types, wanted_type, orig_cur_type);
2446 /* Give a warning about a format argument of different type from that
2447 expected. WANTED_TYPE is the type the argument should have, possibly
2448 stripped of pointer dereferences. The description (such as "field
2449 precision"), the placement in the format string, a possibly more
2450 friendly name of WANTED_TYPE, and the number of pointer dereferences
2451 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2452 or NULL if it is missing. */
2453 static void
2454 format_type_warning (format_wanted_type *type, tree wanted_type, tree arg_type)
2456 int kind = type->kind;
2457 const char *wanted_type_name = type->wanted_type_name;
2458 const char *format_start = type->format_start;
2459 int format_length = type->format_length;
2460 int pointer_count = type->pointer_count;
2461 int arg_num = type->arg_num;
2463 char *p;
2464 /* If ARG_TYPE is a typedef with a misleading name (for example,
2465 size_t but not the standard size_t expected by printf %zu), avoid
2466 printing the typedef name. */
2467 if (wanted_type_name
2468 && arg_type
2469 && TYPE_NAME (arg_type)
2470 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2471 && DECL_NAME (TYPE_NAME (arg_type))
2472 && !strcmp (wanted_type_name,
2473 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2474 arg_type = TYPE_MAIN_VARIANT (arg_type);
2475 /* The format type and name exclude any '*' for pointers, so those
2476 must be formatted manually. For all the types we currently have,
2477 this is adequate, but formats taking pointers to functions or
2478 arrays would require the full type to be built up in order to
2479 print it with %T. */
2480 p = (char *) alloca (pointer_count + 2);
2481 if (pointer_count == 0)
2482 p[0] = 0;
2483 else if (c_dialect_cxx ())
2485 memset (p, '*', pointer_count);
2486 p[pointer_count] = 0;
2488 else
2490 p[0] = ' ';
2491 memset (p + 1, '*', pointer_count);
2492 p[pointer_count + 1] = 0;
2495 if (wanted_type_name)
2497 if (arg_type)
2498 warning (OPT_Wformat_, "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2499 "but argument %d has type %qT",
2500 gettext (kind_descriptions[kind]),
2501 (kind == CF_KIND_FORMAT ? "%" : ""),
2502 format_length, format_start,
2503 wanted_type_name, p, arg_num, arg_type);
2504 else
2505 warning (OPT_Wformat_, "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2506 gettext (kind_descriptions[kind]),
2507 (kind == CF_KIND_FORMAT ? "%" : ""),
2508 format_length, format_start, wanted_type_name, p);
2510 else
2512 if (arg_type)
2513 warning (OPT_Wformat_, "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2514 "but argument %d has type %qT",
2515 gettext (kind_descriptions[kind]),
2516 (kind == CF_KIND_FORMAT ? "%" : ""),
2517 format_length, format_start,
2518 wanted_type, p, arg_num, arg_type);
2519 else
2520 warning (OPT_Wformat_, "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2521 gettext (kind_descriptions[kind]),
2522 (kind == CF_KIND_FORMAT ? "%" : ""),
2523 format_length, format_start, wanted_type, p);
2528 /* Given a format_char_info array FCI, and a character C, this function
2529 returns the index into the conversion_specs where that specifier's
2530 data is located. The character must exist. */
2531 static unsigned int
2532 find_char_info_specifier_index (const format_char_info *fci, int c)
2534 unsigned i;
2536 for (i = 0; fci->format_chars; i++, fci++)
2537 if (strchr (fci->format_chars, c))
2538 return i;
2540 /* We shouldn't be looking for a non-existent specifier. */
2541 gcc_unreachable ();
2544 /* Given a format_length_info array FLI, and a character C, this
2545 function returns the index into the conversion_specs where that
2546 modifier's data is located. The character must exist. */
2547 static unsigned int
2548 find_length_info_modifier_index (const format_length_info *fli, int c)
2550 unsigned i;
2552 for (i = 0; fli->name; i++, fli++)
2553 if (strchr (fli->name, c))
2554 return i;
2556 /* We shouldn't be looking for a non-existent modifier. */
2557 gcc_unreachable ();
2560 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2561 use in GCC's __asm_fprintf__ custom format attribute. You must
2562 have set dynamic_format_types before calling this function. */
2563 static void
2564 init_dynamic_asm_fprintf_info (void)
2566 static tree hwi;
2568 if (!hwi)
2570 format_length_info *new_asm_fprintf_length_specs;
2571 unsigned int i;
2573 /* Find the underlying type for HOST_WIDE_INT. For the %w
2574 length modifier to work, one must have issued: "typedef
2575 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2576 prior to using that modifier. */
2577 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2578 if (!hwi)
2580 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2581 return;
2583 hwi = identifier_global_value (hwi);
2584 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2586 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2587 return;
2589 hwi = DECL_ORIGINAL_TYPE (hwi);
2590 gcc_assert (hwi);
2591 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2593 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2594 " or %<long long%>");
2595 return;
2598 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2599 new_asm_fprintf_length_specs = (format_length_info *)
2600 xmemdup (asm_fprintf_length_specs,
2601 sizeof (asm_fprintf_length_specs),
2602 sizeof (asm_fprintf_length_specs));
2604 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2605 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2606 if (hwi == long_integer_type_node)
2607 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2608 else if (hwi == long_long_integer_type_node)
2609 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2610 else
2611 gcc_unreachable ();
2613 /* Assign the new data for use. */
2614 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2615 new_asm_fprintf_length_specs;
2619 /* Determine the type of a "locus" in the code being compiled for use
2620 in GCC's __gcc_gfc__ custom format attribute. You must have set
2621 dynamic_format_types before calling this function. */
2622 static void
2623 init_dynamic_gfc_info (void)
2625 static tree locus;
2627 if (!locus)
2629 static format_char_info *gfc_fci;
2631 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2632 must have declared 'locus' prior to using this attribute. If
2633 we haven't seen this declarations then you shouldn't use the
2634 specifier requiring that type. */
2635 if ((locus = maybe_get_identifier ("locus")))
2637 locus = identifier_global_value (locus);
2638 if (locus)
2640 if (TREE_CODE (locus) != TYPE_DECL
2641 || TREE_TYPE (locus) == error_mark_node)
2643 error ("%<locus%> is not defined as a type");
2644 locus = 0;
2646 else
2647 locus = TREE_TYPE (locus);
2651 /* Assign the new data for use. */
2653 /* Handle the __gcc_gfc__ format specifics. */
2654 if (!gfc_fci)
2655 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2656 gfc_fci = (format_char_info *)
2657 xmemdup (gcc_gfc_char_table,
2658 sizeof (gcc_gfc_char_table),
2659 sizeof (gcc_gfc_char_table));
2660 if (locus)
2662 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2663 gfc_fci[i].types[0].type = &locus;
2664 gfc_fci[i].pointer_count = 1;
2669 /* Determine the types of "tree" and "location_t" in the code being
2670 compiled for use in GCC's diagnostic custom format attributes. You
2671 must have set dynamic_format_types before calling this function. */
2672 static void
2673 init_dynamic_diag_info (void)
2675 static tree t, loc, hwi;
2677 if (!loc || !t || !hwi)
2679 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2680 static format_length_info *diag_ls;
2681 unsigned int i;
2683 /* For the GCC-diagnostics custom format specifiers to work, one
2684 must have declared 'tree' and/or 'location_t' prior to using
2685 those attributes. If we haven't seen these declarations then
2686 you shouldn't use the specifiers requiring these types.
2687 However we don't force a hard ICE because we may see only one
2688 or the other type. */
2689 if ((loc = maybe_get_identifier ("location_t")))
2691 loc = identifier_global_value (loc);
2692 if (loc)
2694 if (TREE_CODE (loc) != TYPE_DECL)
2696 error ("%<location_t%> is not defined as a type");
2697 loc = 0;
2699 else
2700 loc = TREE_TYPE (loc);
2704 /* We need to grab the underlying 'union tree_node' so peek into
2705 an extra type level. */
2706 if ((t = maybe_get_identifier ("tree")))
2708 t = identifier_global_value (t);
2709 if (t)
2711 if (TREE_CODE (t) != TYPE_DECL)
2713 error ("%<tree%> is not defined as a type");
2714 t = 0;
2716 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2718 error ("%<tree%> is not defined as a pointer type");
2719 t = 0;
2721 else
2722 t = TREE_TYPE (TREE_TYPE (t));
2726 /* Find the underlying type for HOST_WIDE_INT. For the %w
2727 length modifier to work, one must have issued: "typedef
2728 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2729 prior to using that modifier. */
2730 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2732 hwi = identifier_global_value (hwi);
2733 if (hwi)
2735 if (TREE_CODE (hwi) != TYPE_DECL)
2737 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2738 hwi = 0;
2740 else
2742 hwi = DECL_ORIGINAL_TYPE (hwi);
2743 gcc_assert (hwi);
2744 if (hwi != long_integer_type_node
2745 && hwi != long_long_integer_type_node)
2747 error ("%<__gcc_host_wide_int__%> is not defined"
2748 " as %<long%> or %<long long%>");
2749 hwi = 0;
2755 /* Assign the new data for use. */
2757 /* All the GCC diag formats use the same length specs. */
2758 if (!diag_ls)
2759 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2760 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2761 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2762 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2763 diag_ls = (format_length_info *)
2764 xmemdup (gcc_diag_length_specs,
2765 sizeof (gcc_diag_length_specs),
2766 sizeof (gcc_diag_length_specs));
2767 if (hwi)
2769 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2770 i = find_length_info_modifier_index (diag_ls, 'w');
2771 if (hwi == long_integer_type_node)
2772 diag_ls[i].index = FMT_LEN_l;
2773 else if (hwi == long_long_integer_type_node)
2774 diag_ls[i].index = FMT_LEN_ll;
2775 else
2776 gcc_unreachable ();
2779 /* Handle the __gcc_diag__ format specifics. */
2780 if (!diag_fci)
2781 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2782 diag_fci = (format_char_info *)
2783 xmemdup (gcc_diag_char_table,
2784 sizeof (gcc_diag_char_table),
2785 sizeof (gcc_diag_char_table));
2786 if (t)
2788 i = find_char_info_specifier_index (diag_fci, 'K');
2789 diag_fci[i].types[0].type = &t;
2790 diag_fci[i].pointer_count = 1;
2793 /* Handle the __gcc_tdiag__ format specifics. */
2794 if (!tdiag_fci)
2795 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2796 tdiag_fci = (format_char_info *)
2797 xmemdup (gcc_tdiag_char_table,
2798 sizeof (gcc_tdiag_char_table),
2799 sizeof (gcc_tdiag_char_table));
2800 if (t)
2802 /* All specifiers taking a tree share the same struct. */
2803 i = find_char_info_specifier_index (tdiag_fci, 'D');
2804 tdiag_fci[i].types[0].type = &t;
2805 tdiag_fci[i].pointer_count = 1;
2806 i = find_char_info_specifier_index (tdiag_fci, 'K');
2807 tdiag_fci[i].types[0].type = &t;
2808 tdiag_fci[i].pointer_count = 1;
2811 /* Handle the __gcc_cdiag__ format specifics. */
2812 if (!cdiag_fci)
2813 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2814 cdiag_fci = (format_char_info *)
2815 xmemdup (gcc_cdiag_char_table,
2816 sizeof (gcc_cdiag_char_table),
2817 sizeof (gcc_cdiag_char_table));
2818 if (t)
2820 /* All specifiers taking a tree share the same struct. */
2821 i = find_char_info_specifier_index (cdiag_fci, 'D');
2822 cdiag_fci[i].types[0].type = &t;
2823 cdiag_fci[i].pointer_count = 1;
2824 i = find_char_info_specifier_index (cdiag_fci, 'K');
2825 cdiag_fci[i].types[0].type = &t;
2826 cdiag_fci[i].pointer_count = 1;
2829 /* Handle the __gcc_cxxdiag__ format specifics. */
2830 if (!cxxdiag_fci)
2831 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2832 cxxdiag_fci = (format_char_info *)
2833 xmemdup (gcc_cxxdiag_char_table,
2834 sizeof (gcc_cxxdiag_char_table),
2835 sizeof (gcc_cxxdiag_char_table));
2836 if (t)
2838 /* All specifiers taking a tree share the same struct. */
2839 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2840 cxxdiag_fci[i].types[0].type = &t;
2841 cxxdiag_fci[i].pointer_count = 1;
2842 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2843 cxxdiag_fci[i].types[0].type = &t;
2844 cxxdiag_fci[i].pointer_count = 1;
2849 #ifdef TARGET_FORMAT_TYPES
2850 extern const format_kind_info TARGET_FORMAT_TYPES[];
2851 #endif
2853 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2854 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2855 #endif
2856 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2857 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2858 #endif
2860 /* Attributes such as "printf" are equivalent to those such as
2861 "gnu_printf" unless this is overridden by a target. */
2862 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2864 { "gnu_printf", "printf" },
2865 { "gnu_scanf", "scanf" },
2866 { "gnu_strftime", "strftime" },
2867 { "gnu_strfmon", "strfmon" },
2868 { NULL, NULL }
2871 /* Translate to unified attribute name. This is used in decode_format_type and
2872 decode_format_attr. In attr_name the user specified argument is passed. It
2873 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2874 or the attr_name passed to this function, if there is no matching entry. */
2875 static const char *
2876 convert_format_name_to_system_name (const char *attr_name)
2878 int i;
2880 if (attr_name == NULL || *attr_name == 0
2881 || strncmp (attr_name, "gcc_", 4) == 0)
2882 return attr_name;
2883 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2884 TARGET_OVERRIDES_FORMAT_INIT ();
2885 #endif
2887 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2888 /* Check if format attribute is overridden by target. */
2889 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2890 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2892 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2894 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2895 attr_name))
2896 return attr_name;
2897 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2898 attr_name))
2899 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2902 #endif
2903 /* Otherwise default to gnu format. */
2904 for (i = 0;
2905 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2906 ++i)
2908 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2909 attr_name))
2910 return attr_name;
2911 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2912 attr_name))
2913 return gnu_target_overrides_format_attributes[i].named_attr_src;
2916 return attr_name;
2919 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2920 counting "name" and "__name__" as the same, false otherwise. */
2921 static bool
2922 cmp_attribs (const char *tattr_name, const char *attr_name)
2924 int alen = strlen (attr_name);
2925 int slen = (tattr_name ? strlen (tattr_name) : 0);
2926 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2927 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2929 attr_name += 2;
2930 alen -= 4;
2932 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2933 return false;
2934 return true;
2937 /* Handle a "format" attribute; arguments as in
2938 struct attribute_spec.handler. */
2939 tree
2940 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2941 int flags, bool *no_add_attrs)
2943 tree type = *node;
2944 function_format_info info;
2946 #ifdef TARGET_FORMAT_TYPES
2947 /* If the target provides additional format types, we need to
2948 add them to FORMAT_TYPES at first use. */
2949 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2951 dynamic_format_types = XNEWVEC (format_kind_info,
2952 n_format_types + TARGET_N_FORMAT_TYPES);
2953 memcpy (dynamic_format_types, format_types_orig,
2954 sizeof (format_types_orig));
2955 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2956 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2958 format_types = dynamic_format_types;
2959 /* Provide a reference for the first potential external type. */
2960 first_target_format_type = n_format_types;
2961 n_format_types += TARGET_N_FORMAT_TYPES;
2963 #endif
2965 if (!decode_format_attr (args, &info, 0))
2967 *no_add_attrs = true;
2968 return NULL_TREE;
2971 if (prototype_p (type))
2973 if (!check_format_string (type, info.format_num, flags,
2974 no_add_attrs, info.format_type))
2975 return NULL_TREE;
2977 if (info.first_arg_num != 0)
2979 unsigned HOST_WIDE_INT arg_num = 1;
2980 function_args_iterator iter;
2981 tree arg_type;
2983 /* Verify that first_arg_num points to the last arg,
2984 the ... */
2985 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
2986 arg_num++;
2988 if (arg_num != info.first_arg_num)
2990 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2991 error ("args to be formatted is not %<...%>");
2992 *no_add_attrs = true;
2993 return NULL_TREE;
2998 /* Check if this is a strftime variant. Just for this variant
2999 FMT_FLAG_ARG_CONVERT is not set. */
3000 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3001 && info.first_arg_num != 0)
3003 error ("strftime formats cannot format arguments");
3004 *no_add_attrs = true;
3005 return NULL_TREE;
3008 /* If this is a custom GCC-internal format type, we have to
3009 initialize certain bits at runtime. */
3010 if (info.format_type == asm_fprintf_format_type
3011 || info.format_type == gcc_gfc_format_type
3012 || info.format_type == gcc_diag_format_type
3013 || info.format_type == gcc_tdiag_format_type
3014 || info.format_type == gcc_cdiag_format_type
3015 || info.format_type == gcc_cxxdiag_format_type)
3017 /* Our first time through, we have to make sure that our
3018 format_type data is allocated dynamically and is modifiable. */
3019 if (!dynamic_format_types)
3020 format_types = dynamic_format_types = (format_kind_info *)
3021 xmemdup (format_types_orig, sizeof (format_types_orig),
3022 sizeof (format_types_orig));
3024 /* If this is format __asm_fprintf__, we have to initialize
3025 GCC's notion of HOST_WIDE_INT for checking %wd. */
3026 if (info.format_type == asm_fprintf_format_type)
3027 init_dynamic_asm_fprintf_info ();
3028 /* If this is format __gcc_gfc__, we have to initialize GCC's
3029 notion of 'locus' at runtime for %L. */
3030 else if (info.format_type == gcc_gfc_format_type)
3031 init_dynamic_gfc_info ();
3032 /* If this is one of the diagnostic attributes, then we have to
3033 initialize 'location_t' and 'tree' at runtime. */
3034 else if (info.format_type == gcc_diag_format_type
3035 || info.format_type == gcc_tdiag_format_type
3036 || info.format_type == gcc_cdiag_format_type
3037 || info.format_type == gcc_cxxdiag_format_type)
3038 init_dynamic_diag_info ();
3039 else
3040 gcc_unreachable ();
3043 return NULL_TREE;