2013-06-17 Jonathan Wakely <jwakely.gcc@gmail.com>
[official-gcc.git] / gcc / c-family / c-format.c
blobc11d93aa89d96c012a471606a53f156e868a03f8
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2013 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 "flags.h"
26 #include "c-common.h"
27 #include "c-objc.h"
28 #include "intl.h"
29 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "c-format.h"
32 #include "alloc-pool.h"
33 #include "c-target.h"
35 /* Handle attributes associated with format checking. */
37 /* This must be in the same order as format_types, except for
38 format_type_error. Target-specific format types do not have
39 matching enum values. */
40 enum format_type { printf_format_type, asm_fprintf_format_type,
41 gcc_diag_format_type, gcc_tdiag_format_type,
42 gcc_cdiag_format_type,
43 gcc_cxxdiag_format_type, gcc_gfc_format_type,
44 gcc_objc_string_format_type,
45 format_type_error = -1};
47 typedef struct function_format_info
49 int format_type; /* type of format (printf, scanf, etc.) */
50 unsigned HOST_WIDE_INT format_num; /* number of format argument */
51 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
52 } function_format_info;
54 static bool decode_format_attr (tree, function_format_info *, int);
55 static int decode_format_type (const char *);
57 static bool check_format_string (tree argument,
58 unsigned HOST_WIDE_INT format_num,
59 int flags, bool *no_add_attrs,
60 int expected_format_type);
61 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
62 int validated_p);
63 static const char *convert_format_name_to_system_name (const char *attr_name);
64 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
66 static int first_target_format_type;
67 static const char *format_name (int format_num);
68 static int format_flags (int format_num);
70 /* Check that we have a pointer to a string suitable for use as a format.
71 The default is to check for a char type.
72 For objective-c dialects, this is extended to include references to string
73 objects validated by objc_string_ref_type_p ().
74 Targets may also provide a string object type that can be used within c and
75 c++ and shared with their respective objective-c dialects. In this case the
76 reference to a format string is checked for validity via a hook.
78 The function returns true if strref points to any string type valid for the
79 language dialect and target. */
81 static bool
82 valid_stringptr_type_p (tree strref)
84 return (strref != NULL
85 && TREE_CODE (strref) == POINTER_TYPE
86 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
87 || objc_string_ref_type_p (strref)
88 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
91 /* Handle a "format_arg" attribute; arguments as in
92 struct attribute_spec.handler. */
93 tree
94 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
95 tree args, int flags, bool *no_add_attrs)
97 tree type = *node;
98 tree format_num_expr = TREE_VALUE (args);
99 unsigned HOST_WIDE_INT format_num = 0;
101 if (!get_constant (format_num_expr, &format_num, 0))
103 error ("format string has invalid operand number");
104 *no_add_attrs = true;
105 return NULL_TREE;
108 if (prototype_p (type))
110 /* The format arg can be any string reference valid for the language and
111 target. We cannot be more specific in this case. */
112 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
113 return NULL_TREE;
116 if (!valid_stringptr_type_p (TREE_TYPE (type)))
118 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
119 error ("function does not return string type");
120 *no_add_attrs = true;
121 return NULL_TREE;
124 return NULL_TREE;
127 /* Verify that the format_num argument is actually a string reference suitable,
128 for the language dialect and target (in case the format attribute is in
129 error). When we know the specific reference type expected, this is also
130 checked. */
131 static bool
132 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
133 int flags, bool *no_add_attrs, int expected_format_type)
135 unsigned HOST_WIDE_INT i;
136 bool is_objc_sref, is_target_sref, is_char_ref;
137 tree ref;
138 int fmt_flags;
139 function_args_iterator iter;
141 i = 1;
142 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
144 if (i == format_num)
145 break;
146 i++;
149 if (!ref
150 || !valid_stringptr_type_p (ref))
152 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
153 error ("format string argument is not a string type");
154 *no_add_attrs = true;
155 return false;
158 /* We only know that we want a suitable string reference. */
159 if (expected_format_type < 0)
160 return true;
162 /* Now check that the arg matches the expected type. */
163 is_char_ref =
164 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
166 fmt_flags = format_flags (expected_format_type);
167 is_objc_sref = is_target_sref = false;
168 if (!is_char_ref)
169 is_objc_sref = objc_string_ref_type_p (ref);
171 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
173 if (is_char_ref)
174 return true; /* OK, we expected a char and found one. */
175 else
177 /* We expected a char but found an extended string type. */
178 if (is_objc_sref)
179 error ("found a %<%s%> reference but the format argument should"
180 " be a string", format_name (gcc_objc_string_format_type));
181 else
182 error ("found a %qT but the format argument should be a string",
183 ref);
184 *no_add_attrs = true;
185 return false;
189 /* We expect a string object type as the format arg. */
190 if (is_char_ref)
192 error ("format argument should be a %<%s%> reference but"
193 " a string was found", format_name (expected_format_type));
194 *no_add_attrs = true;
195 return false;
198 /* We will assert that objective-c will support either its own string type
199 or the target-supplied variant. */
200 if (!is_objc_sref)
201 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
203 if (expected_format_type == (int) gcc_objc_string_format_type
204 && (is_objc_sref || is_target_sref))
205 return true;
207 /* We will allow a target string ref to match only itself. */
208 if (first_target_format_type
209 && expected_format_type >= first_target_format_type
210 && is_target_sref)
211 return true;
212 else
214 error ("format argument should be a %<%s%> reference",
215 format_name (expected_format_type));
216 *no_add_attrs = true;
217 return false;
220 gcc_unreachable ();
223 /* Verify EXPR is a constant, and store its value.
224 If validated_p is true there should be no errors.
225 Returns true on success, false otherwise. */
226 static bool
227 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
229 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
231 gcc_assert (!validated_p);
232 return false;
235 *value = TREE_INT_CST_LOW (expr);
237 return true;
240 /* Decode the arguments to a "format" attribute into a
241 function_format_info structure. It is already known that the list
242 is of the right length. If VALIDATED_P is true, then these
243 attributes have already been validated and must not be erroneous;
244 if false, it will give an error message. Returns true if the
245 attributes are successfully decoded, false otherwise. */
247 static bool
248 decode_format_attr (tree args, function_format_info *info, int validated_p)
250 tree format_type_id = TREE_VALUE (args);
251 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
252 tree first_arg_num_expr
253 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
255 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
257 gcc_assert (!validated_p);
258 error ("unrecognized format specifier");
259 return false;
261 else
263 const char *p = IDENTIFIER_POINTER (format_type_id);
265 p = convert_format_name_to_system_name (p);
267 info->format_type = decode_format_type (p);
269 if (!c_dialect_objc ()
270 && info->format_type == gcc_objc_string_format_type)
272 gcc_assert (!validated_p);
273 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
274 format_type_id);
275 info->format_type = format_type_error;
276 return false;
279 if (info->format_type == format_type_error)
281 gcc_assert (!validated_p);
282 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
283 format_type_id);
284 return false;
288 if (!get_constant (format_num_expr, &info->format_num, validated_p))
290 error ("format string has invalid operand number");
291 return false;
294 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
296 error ("%<...%> has invalid operand number");
297 return false;
300 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
302 gcc_assert (!validated_p);
303 error ("format string argument follows the args to be formatted");
304 return false;
307 return true;
310 /* Check a call to a format function against a parameter list. */
312 /* The C standard version C++ is treated as equivalent to
313 or inheriting from, for the purpose of format features supported. */
314 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
315 /* The C standard version we are checking formats against when pedantic. */
316 #define C_STD_VER ((int) (c_dialect_cxx () \
317 ? CPLUSPLUS_STD_VER \
318 : (flag_isoc99 \
319 ? STD_C99 \
320 : (flag_isoc94 ? STD_C94 : STD_C89))))
321 /* The name to give to the standard version we are warning about when
322 pedantic. FEATURE_VER is the version in which the feature warned out
323 appeared, which is higher than C_STD_VER. */
324 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
325 ? (cxx_dialect < cxx11 ? "ISO C++98" \
326 : "ISO C++11") \
327 : ((FEATURE_VER) == STD_EXT \
328 ? "ISO C" \
329 : "ISO C90"))
330 /* Adjust a C standard version, which may be STD_C9L, to account for
331 -Wno-long-long. Returns other standard versions unchanged. */
332 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
333 ? (warn_long_long ? STD_C99 : STD_C89) \
334 : (VER)))
336 /* Enum describing the kind of specifiers present in the format and
337 requiring an argument. */
338 enum format_specifier_kind {
339 CF_KIND_FORMAT,
340 CF_KIND_FIELD_WIDTH,
341 CF_KIND_FIELD_PRECISION
344 static const char *kind_descriptions[] = {
345 N_("format"),
346 N_("field width specifier"),
347 N_("field precision specifier")
350 /* Structure describing details of a type expected in format checking,
351 and the type to check against it. */
352 typedef struct format_wanted_type
354 /* The type wanted. */
355 tree wanted_type;
356 /* The name of this type to use in diagnostics. */
357 const char *wanted_type_name;
358 /* Should be type checked just for scalar width identity. */
359 int scalar_identity_flag;
360 /* The level of indirection through pointers at which this type occurs. */
361 int pointer_count;
362 /* Whether, when pointer_count is 1, to allow any character type when
363 pedantic, rather than just the character or void type specified. */
364 int char_lenient_flag;
365 /* Whether the argument, dereferenced once, is written into and so the
366 argument must not be a pointer to a const-qualified type. */
367 int writing_in_flag;
368 /* Whether the argument, dereferenced once, is read from and so
369 must not be a NULL pointer. */
370 int reading_from_flag;
371 /* The kind of specifier that this type is used for. */
372 enum format_specifier_kind kind;
373 /* The starting character of the specifier. This never includes the
374 initial percent sign. */
375 const char *format_start;
376 /* The length of the specifier. */
377 int format_length;
378 /* The actual parameter to check against the wanted type. */
379 tree param;
380 /* The argument number of that parameter. */
381 int arg_num;
382 /* The next type to check for this format conversion, or NULL if none. */
383 struct format_wanted_type *next;
384 } format_wanted_type;
386 /* Convenience macro for format_length_info meaning unused. */
387 #define NO_FMT NULL, FMT_LEN_none, STD_C89
389 static const format_length_info printf_length_specs[] =
391 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
392 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
393 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
394 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
395 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
396 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
397 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
398 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
399 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
400 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
401 { NO_FMT, NO_FMT, 0 }
404 /* Length specifiers valid for asm_fprintf. */
405 static const format_length_info asm_fprintf_length_specs[] =
407 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
408 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
409 { NO_FMT, NO_FMT, 0 }
412 /* Length specifiers valid for GCC diagnostics. */
413 static const format_length_info gcc_diag_length_specs[] =
415 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
416 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
417 { NO_FMT, NO_FMT, 0 }
420 /* The custom diagnostics all accept the same length specifiers. */
421 #define gcc_tdiag_length_specs gcc_diag_length_specs
422 #define gcc_cdiag_length_specs gcc_diag_length_specs
423 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
425 /* This differs from printf_length_specs only in that "Z" is not accepted. */
426 static const format_length_info scanf_length_specs[] =
428 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
429 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
430 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
431 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
432 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
433 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
434 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
435 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
436 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
437 { NO_FMT, NO_FMT, 0 }
441 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
442 make no sense for a format type not part of any C standard version. */
443 static const format_length_info strfmon_length_specs[] =
445 /* A GNU extension. */
446 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
447 { NO_FMT, NO_FMT, 0 }
451 /* For now, the Fortran front-end routines only use l as length modifier. */
452 static const format_length_info gcc_gfc_length_specs[] =
454 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
455 { NO_FMT, NO_FMT, 0 }
459 static const format_flag_spec printf_flag_specs[] =
461 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
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, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
465 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
466 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
467 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
468 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
469 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
470 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
471 { 0, 0, 0, NULL, NULL, STD_C89 }
475 static const format_flag_pair printf_flag_pairs[] =
477 { ' ', '+', 1, 0 },
478 { '0', '-', 1, 0 },
479 { '0', 'p', 1, 'i' },
480 { 0, 0, 0, 0 }
483 static const format_flag_spec asm_fprintf_flag_specs[] =
485 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
486 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
487 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
488 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
489 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
490 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
491 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
492 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
493 { 0, 0, 0, NULL, NULL, STD_C89 }
496 static const format_flag_pair asm_fprintf_flag_pairs[] =
498 { ' ', '+', 1, 0 },
499 { '0', '-', 1, 0 },
500 { '0', 'p', 1, 'i' },
501 { 0, 0, 0, 0 }
504 static const format_flag_pair gcc_diag_flag_pairs[] =
506 { 0, 0, 0, 0 }
509 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
510 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
511 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
513 static const format_flag_pair gcc_gfc_flag_pairs[] =
515 { 0, 0, 0, 0 }
518 static const format_flag_spec gcc_diag_flag_specs[] =
520 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
521 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
522 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
523 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
524 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
525 { 0, 0, 0, NULL, NULL, STD_C89 }
528 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
529 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
530 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
532 static const format_flag_spec scanf_flag_specs[] =
534 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
535 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
536 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
537 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
538 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
539 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
540 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
541 { 0, 0, 0, NULL, NULL, STD_C89 }
545 static const format_flag_pair scanf_flag_pairs[] =
547 { '*', 'L', 0, 0 },
548 { 'a', 'm', 0, 0 },
549 { 0, 0, 0, 0 }
553 static const format_flag_spec strftime_flag_specs[] =
555 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
556 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
557 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
558 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
559 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
560 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
561 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
562 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
563 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
564 { 0, 0, 0, NULL, NULL, STD_C89 }
568 static const format_flag_pair strftime_flag_pairs[] =
570 { 'E', 'O', 0, 0 },
571 { '_', '-', 0, 0 },
572 { '_', '0', 0, 0 },
573 { '-', '0', 0, 0 },
574 { '^', '#', 0, 0 },
575 { 0, 0, 0, 0 }
579 static const format_flag_spec strfmon_flag_specs[] =
581 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
582 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
583 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
584 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
585 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
586 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
587 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
588 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
589 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
590 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
591 { 0, 0, 0, NULL, NULL, STD_C89 }
594 static const format_flag_pair strfmon_flag_pairs[] =
596 { '+', '(', 0, 0 },
597 { 0, 0, 0, 0 }
601 static const format_char_info print_char_table[] =
603 /* C89 conversion specifiers. */
604 { "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 },
605 { "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 },
606 { "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 },
607 { "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 },
608 { "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 },
609 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
610 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
611 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
612 { "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 },
613 /* C99 conversion specifiers. */
614 { "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 },
615 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
616 /* X/Open conversion specifiers. */
617 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
618 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
619 /* GNU conversion specifiers. */
620 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
621 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
624 static const format_char_info asm_fprintf_char_table[] =
626 /* C89 conversion specifiers. */
627 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
628 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
629 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
630 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
631 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
633 /* asm_fprintf conversion specifiers. */
634 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
635 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
636 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
637 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
638 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
639 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
640 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
641 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
644 static const format_char_info gcc_diag_char_table[] =
646 /* C89 conversion specifiers. */
647 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
648 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
649 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
650 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
651 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
652 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
654 /* Custom conversion specifiers. */
656 /* These will require a "tree" at runtime. */
657 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
659 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
660 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
661 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
662 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
665 static const format_char_info gcc_tdiag_char_table[] =
667 /* C89 conversion specifiers. */
668 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
669 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
670 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
671 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
672 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
673 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
675 /* Custom conversion specifiers. */
677 /* These will require a "tree" at runtime. */
678 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
680 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
682 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
683 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
684 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
685 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
688 static const format_char_info gcc_cdiag_char_table[] =
690 /* C89 conversion specifiers. */
691 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
692 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
693 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
694 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
695 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
696 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
698 /* Custom conversion specifiers. */
700 /* These will require a "tree" at runtime. */
701 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
703 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
705 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
706 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
707 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
708 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
711 static const format_char_info gcc_cxxdiag_char_table[] =
713 /* C89 conversion specifiers. */
714 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
715 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
716 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
717 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
718 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
719 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
721 /* Custom conversion specifiers. */
723 /* These will require a "tree" at runtime. */
724 { "ADEFKSTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
726 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
728 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
729 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
731 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
732 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
733 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
734 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
737 static const format_char_info gcc_gfc_char_table[] =
739 /* C89 conversion specifiers. */
740 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
741 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
742 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
743 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
745 /* gfc conversion specifiers. */
747 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
749 /* This will require a "locus" at runtime. */
750 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
752 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
755 static const format_char_info scan_char_table[] =
757 /* C89 conversion specifiers. */
758 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
759 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
760 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
761 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
762 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
763 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
764 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
765 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
766 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
767 /* C99 conversion specifiers. */
768 { "F", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
769 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
770 /* X/Open conversion specifiers. */
771 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
772 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
773 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
776 static const format_char_info time_char_table[] =
778 /* C89 conversion specifiers. */
779 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
780 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
781 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
782 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
783 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
784 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
785 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
786 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
787 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
788 /* C99 conversion specifiers. */
789 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
790 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
791 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
792 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
793 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
794 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
795 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
796 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
797 /* GNU conversion specifiers. */
798 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
799 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
800 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
803 static const format_char_info monetary_char_table[] =
805 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
806 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
809 /* This must be in the same order as enum format_type. */
810 static const format_kind_info format_types_orig[] =
812 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
813 printf_flag_specs, printf_flag_pairs,
814 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
815 'w', 0, 'p', 0, 'L', 0,
816 &integer_type_node, &integer_type_node
818 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
819 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
820 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
821 'w', 0, 'p', 0, 'L', 0,
822 NULL, NULL
824 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
825 gcc_diag_flag_specs, gcc_diag_flag_pairs,
826 FMT_FLAG_ARG_CONVERT,
827 0, 0, 'p', 0, 'L', 0,
828 NULL, &integer_type_node
830 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
831 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
832 FMT_FLAG_ARG_CONVERT,
833 0, 0, 'p', 0, 'L', 0,
834 NULL, &integer_type_node
836 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
837 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
838 FMT_FLAG_ARG_CONVERT,
839 0, 0, 'p', 0, 'L', 0,
840 NULL, &integer_type_node
842 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
843 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
844 FMT_FLAG_ARG_CONVERT,
845 0, 0, 'p', 0, 'L', 0,
846 NULL, &integer_type_node
848 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "", NULL,
849 NULL, gcc_gfc_flag_pairs,
850 FMT_FLAG_ARG_CONVERT,
851 0, 0, 0, 0, 0, 0,
852 NULL, NULL
854 { "NSString", NULL, NULL, NULL, NULL,
855 NULL, NULL,
856 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
857 NULL, NULL
859 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
860 scanf_flag_specs, scanf_flag_pairs,
861 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
862 'w', 0, 0, '*', 'L', 'm',
863 NULL, NULL
865 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
866 strftime_flag_specs, strftime_flag_pairs,
867 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
868 NULL, NULL
870 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
871 strfmon_flag_specs, strfmon_flag_pairs,
872 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
873 NULL, NULL
877 /* This layer of indirection allows GCC to reassign format_types with
878 new data if necessary, while still allowing the original data to be
879 const. */
880 static const format_kind_info *format_types = format_types_orig;
881 /* We can modify this one. We also add target-specific format types
882 to the end of the array. */
883 static format_kind_info *dynamic_format_types;
885 static int n_format_types = ARRAY_SIZE (format_types_orig);
887 /* Structure detailing the results of checking a format function call
888 where the format expression may be a conditional expression with
889 many leaves resulting from nested conditional expressions. */
890 typedef struct
892 /* Number of leaves of the format argument that could not be checked
893 as they were not string literals. */
894 int number_non_literal;
895 /* Number of leaves of the format argument that were null pointers or
896 string literals, but had extra format arguments. */
897 int number_extra_args;
898 /* Number of leaves of the format argument that were null pointers or
899 string literals, but had extra format arguments and used $ operand
900 numbers. */
901 int number_dollar_extra_args;
902 /* Number of leaves of the format argument that were wide string
903 literals. */
904 int number_wide;
905 /* Number of leaves of the format argument that were empty strings. */
906 int number_empty;
907 /* Number of leaves of the format argument that were unterminated
908 strings. */
909 int number_unterminated;
910 /* Number of leaves of the format argument that were not counted above. */
911 int number_other;
912 } format_check_results;
914 typedef struct
916 format_check_results *res;
917 function_format_info *info;
918 tree params;
919 } format_check_context;
921 /* Return the format name (as specified in the original table) for the format
922 type indicated by format_num. */
923 static const char *
924 format_name (int format_num)
926 if (format_num >= 0 && format_num < n_format_types)
927 return format_types[format_num].name;
928 gcc_unreachable ();
931 /* Return the format flags (as specified in the original table) for the format
932 type indicated by format_num. */
933 static int
934 format_flags (int format_num)
936 if (format_num >= 0 && format_num < n_format_types)
937 return format_types[format_num].flags;
938 gcc_unreachable ();
941 static void check_format_info (function_format_info *, tree);
942 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
943 static void check_format_info_main (format_check_results *,
944 function_format_info *,
945 const char *, int, tree,
946 unsigned HOST_WIDE_INT, alloc_pool);
948 static void init_dollar_format_checking (int, tree);
949 static int maybe_read_dollar_number (const char **, int,
950 tree, tree *, const format_kind_info *);
951 static bool avoid_dollar_number (const char *);
952 static void finish_dollar_format_checking (format_check_results *, int);
954 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
955 int, const char *);
957 static void check_format_types (format_wanted_type *);
958 static void format_type_warning (format_wanted_type *, tree, tree);
960 /* Decode a format type from a string, returning the type, or
961 format_type_error if not valid, in which case the caller should print an
962 error message. */
963 static int
964 decode_format_type (const char *s)
966 int i;
967 int slen;
969 s = convert_format_name_to_system_name (s);
970 slen = strlen (s);
971 for (i = 0; i < n_format_types; i++)
973 int alen;
974 if (!strcmp (s, format_types[i].name))
975 return i;
976 alen = strlen (format_types[i].name);
977 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
978 && s[slen - 1] == '_' && s[slen - 2] == '_'
979 && !strncmp (s + 2, format_types[i].name, alen))
980 return i;
982 return format_type_error;
986 /* Check the argument list of a call to printf, scanf, etc.
987 ATTRS are the attributes on the function type. There are NARGS argument
988 values in the array ARGARRAY.
989 Also, if -Wsuggest-attribute=format,
990 warn for calls to vprintf or vscanf in functions with no such format
991 attribute themselves. */
993 void
994 check_function_format (tree attrs, int nargs, tree *argarray)
996 tree a;
998 /* See if this function has any format attributes. */
999 for (a = attrs; a; a = TREE_CHAIN (a))
1001 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1003 /* Yup; check it. */
1004 function_format_info info;
1005 decode_format_attr (TREE_VALUE (a), &info, 1);
1006 if (warn_format)
1008 /* FIXME: Rewrite all the internal functions in this file
1009 to use the ARGARRAY directly instead of constructing this
1010 temporary list. */
1011 tree params = NULL_TREE;
1012 int i;
1013 for (i = nargs - 1; i >= 0; i--)
1014 params = tree_cons (NULL_TREE, argarray[i], params);
1015 check_format_info (&info, params);
1017 if (warn_suggest_attribute_format && info.first_arg_num == 0
1018 && (format_types[info.format_type].flags
1019 & (int) FMT_FLAG_ARG_CONVERT))
1021 tree c;
1022 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1024 c = TREE_CHAIN (c))
1025 if (is_attribute_p ("format", TREE_PURPOSE (c))
1026 && (decode_format_type (IDENTIFIER_POINTER
1027 (TREE_VALUE (TREE_VALUE (c))))
1028 == info.format_type))
1029 break;
1030 if (c == NULL_TREE)
1032 /* Check if the current function has a parameter to which
1033 the format attribute could be attached; if not, it
1034 can't be a candidate for a format attribute, despite
1035 the vprintf-like or vscanf-like call. */
1036 tree args;
1037 for (args = DECL_ARGUMENTS (current_function_decl);
1038 args != 0;
1039 args = DECL_CHAIN (args))
1041 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1042 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1043 == char_type_node))
1044 break;
1046 if (args != 0)
1047 warning (OPT_Wsuggest_attribute_format, "function might "
1048 "be possible candidate for %qs format attribute",
1049 format_types[info.format_type].name);
1057 /* Variables used by the checking of $ operand number formats. */
1058 static char *dollar_arguments_used = NULL;
1059 static char *dollar_arguments_pointer_p = NULL;
1060 static int dollar_arguments_alloc = 0;
1061 static int dollar_arguments_count;
1062 static int dollar_first_arg_num;
1063 static int dollar_max_arg_used;
1064 static int dollar_format_warned;
1066 /* Initialize the checking for a format string that may contain $
1067 parameter number specifications; we will need to keep track of whether
1068 each parameter has been used. FIRST_ARG_NUM is the number of the first
1069 argument that is a parameter to the format, or 0 for a vprintf-style
1070 function; PARAMS is the list of arguments starting at this argument. */
1072 static void
1073 init_dollar_format_checking (int first_arg_num, tree params)
1075 tree oparams = params;
1077 dollar_first_arg_num = first_arg_num;
1078 dollar_arguments_count = 0;
1079 dollar_max_arg_used = 0;
1080 dollar_format_warned = 0;
1081 if (first_arg_num > 0)
1083 while (params)
1085 dollar_arguments_count++;
1086 params = TREE_CHAIN (params);
1089 if (dollar_arguments_alloc < dollar_arguments_count)
1091 free (dollar_arguments_used);
1092 free (dollar_arguments_pointer_p);
1093 dollar_arguments_alloc = dollar_arguments_count;
1094 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1095 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1097 if (dollar_arguments_alloc)
1099 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1100 if (first_arg_num > 0)
1102 int i = 0;
1103 params = oparams;
1104 while (params)
1106 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1107 == POINTER_TYPE);
1108 params = TREE_CHAIN (params);
1109 i++;
1116 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1117 is set, it is an error if one is not found; otherwise, it is OK. If
1118 such a number is found, check whether it is within range and mark that
1119 numbered operand as being used for later checking. Returns the operand
1120 number if found and within range, zero if no such number was found and
1121 this is OK, or -1 on error. PARAMS points to the first operand of the
1122 format; PARAM_PTR is made to point to the parameter referred to. If
1123 a $ format is found, *FORMAT is updated to point just after it. */
1125 static int
1126 maybe_read_dollar_number (const char **format,
1127 int dollar_needed, tree params, tree *param_ptr,
1128 const format_kind_info *fki)
1130 int argnum;
1131 int overflow_flag;
1132 const char *fcp = *format;
1133 if (!ISDIGIT (*fcp))
1135 if (dollar_needed)
1137 warning (OPT_Wformat_, "missing $ operand number in format");
1138 return -1;
1140 else
1141 return 0;
1143 argnum = 0;
1144 overflow_flag = 0;
1145 while (ISDIGIT (*fcp))
1147 int nargnum;
1148 nargnum = 10 * argnum + (*fcp - '0');
1149 if (nargnum < 0 || nargnum / 10 != argnum)
1150 overflow_flag = 1;
1151 argnum = nargnum;
1152 fcp++;
1154 if (*fcp != '$')
1156 if (dollar_needed)
1158 warning (OPT_Wformat_, "missing $ operand number in format");
1159 return -1;
1161 else
1162 return 0;
1164 *format = fcp + 1;
1165 if (pedantic && !dollar_format_warned)
1167 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1168 C_STD_NAME (STD_EXT));
1169 dollar_format_warned = 1;
1171 if (overflow_flag || argnum == 0
1172 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1174 warning (OPT_Wformat_, "operand number out of range in format");
1175 return -1;
1177 if (argnum > dollar_max_arg_used)
1178 dollar_max_arg_used = argnum;
1179 /* For vprintf-style functions we may need to allocate more memory to
1180 track which arguments are used. */
1181 while (dollar_arguments_alloc < dollar_max_arg_used)
1183 int nalloc;
1184 nalloc = 2 * dollar_arguments_alloc + 16;
1185 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1186 nalloc);
1187 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1188 nalloc);
1189 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1190 nalloc - dollar_arguments_alloc);
1191 dollar_arguments_alloc = nalloc;
1193 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1194 && dollar_arguments_used[argnum - 1] == 1)
1196 dollar_arguments_used[argnum - 1] = 2;
1197 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1198 argnum, fki->name);
1200 else
1201 dollar_arguments_used[argnum - 1] = 1;
1202 if (dollar_first_arg_num)
1204 int i;
1205 *param_ptr = params;
1206 for (i = 1; i < argnum && *param_ptr != 0; i++)
1207 *param_ptr = TREE_CHAIN (*param_ptr);
1209 /* This case shouldn't be caught here. */
1210 gcc_assert (*param_ptr);
1212 else
1213 *param_ptr = 0;
1214 return argnum;
1217 /* Ensure that FORMAT does not start with a decimal number followed by
1218 a $; give a diagnostic and return true if it does, false otherwise. */
1220 static bool
1221 avoid_dollar_number (const char *format)
1223 if (!ISDIGIT (*format))
1224 return false;
1225 while (ISDIGIT (*format))
1226 format++;
1227 if (*format == '$')
1229 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1230 return true;
1232 return false;
1236 /* Finish the checking for a format string that used $ operand number formats
1237 instead of non-$ formats. We check for unused operands before used ones
1238 (a serious error, since the implementation of the format function
1239 can't know what types to pass to va_arg to find the later arguments).
1240 and for unused operands at the end of the format (if we know how many
1241 arguments the format had, so not for vprintf). If there were operand
1242 numbers out of range on a non-vprintf-style format, we won't have reached
1243 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1244 pointers. */
1246 static void
1247 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1249 int i;
1250 bool found_pointer_gap = false;
1251 for (i = 0; i < dollar_max_arg_used; i++)
1253 if (!dollar_arguments_used[i])
1255 if (pointer_gap_ok && (dollar_first_arg_num == 0
1256 || dollar_arguments_pointer_p[i]))
1257 found_pointer_gap = true;
1258 else
1259 warning (OPT_Wformat_,
1260 "format argument %d unused before used argument %d in $-style format",
1261 i + 1, dollar_max_arg_used);
1264 if (found_pointer_gap
1265 || (dollar_first_arg_num
1266 && dollar_max_arg_used < dollar_arguments_count))
1268 res->number_other--;
1269 res->number_dollar_extra_args++;
1274 /* Retrieve the specification for a format flag. SPEC contains the
1275 specifications for format flags for the applicable kind of format.
1276 FLAG is the flag in question. If PREDICATES is NULL, the basic
1277 spec for that flag must be retrieved and must exist. If
1278 PREDICATES is not NULL, it is a string listing possible predicates
1279 for the spec entry; if an entry predicated on any of these is
1280 found, it is returned, otherwise NULL is returned. */
1282 static const format_flag_spec *
1283 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1285 int i;
1286 for (i = 0; spec[i].flag_char != 0; i++)
1288 if (spec[i].flag_char != flag)
1289 continue;
1290 if (predicates != NULL)
1292 if (spec[i].predicate != 0
1293 && strchr (predicates, spec[i].predicate) != 0)
1294 return &spec[i];
1296 else if (spec[i].predicate == 0)
1297 return &spec[i];
1299 gcc_assert (predicates);
1300 return NULL;
1304 /* Check the argument list of a call to printf, scanf, etc.
1305 INFO points to the function_format_info structure.
1306 PARAMS is the list of argument values. */
1308 static void
1309 check_format_info (function_format_info *info, tree params)
1311 format_check_context format_ctx;
1312 unsigned HOST_WIDE_INT arg_num;
1313 tree format_tree;
1314 format_check_results res;
1315 /* Skip to format argument. If the argument isn't available, there's
1316 no work for us to do; prototype checking will catch the problem. */
1317 for (arg_num = 1; ; ++arg_num)
1319 if (params == 0)
1320 return;
1321 if (arg_num == info->format_num)
1322 break;
1323 params = TREE_CHAIN (params);
1325 format_tree = TREE_VALUE (params);
1326 params = TREE_CHAIN (params);
1327 if (format_tree == 0)
1328 return;
1330 res.number_non_literal = 0;
1331 res.number_extra_args = 0;
1332 res.number_dollar_extra_args = 0;
1333 res.number_wide = 0;
1334 res.number_empty = 0;
1335 res.number_unterminated = 0;
1336 res.number_other = 0;
1338 format_ctx.res = &res;
1339 format_ctx.info = info;
1340 format_ctx.params = params;
1342 check_function_arguments_recurse (check_format_arg, &format_ctx,
1343 format_tree, arg_num);
1345 if (res.number_non_literal > 0)
1347 /* Functions taking a va_list normally pass a non-literal format
1348 string. These functions typically are declared with
1349 first_arg_num == 0, so avoid warning in those cases. */
1350 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1352 /* For strftime-like formats, warn for not checking the format
1353 string; but there are no arguments to check. */
1354 warning (OPT_Wformat_nonliteral,
1355 "format not a string literal, format string not checked");
1357 else if (info->first_arg_num != 0)
1359 /* If there are no arguments for the format at all, we may have
1360 printf (foo) which is likely to be a security hole. */
1361 while (arg_num + 1 < info->first_arg_num)
1363 if (params == 0)
1364 break;
1365 params = TREE_CHAIN (params);
1366 ++arg_num;
1368 if (params == 0 && warn_format_security)
1369 warning (OPT_Wformat_security,
1370 "format not a string literal and no format arguments");
1371 else if (params == 0 && warn_format_nonliteral)
1372 warning (OPT_Wformat_nonliteral,
1373 "format not a string literal and no format arguments");
1374 else
1375 warning (OPT_Wformat_nonliteral,
1376 "format not a string literal, argument types not checked");
1380 /* If there were extra arguments to the format, normally warn. However,
1381 the standard does say extra arguments are ignored, so in the specific
1382 case where we have multiple leaves (conditional expressions or
1383 ngettext) allow extra arguments if at least one leaf didn't have extra
1384 arguments, but was otherwise OK (either non-literal or checked OK).
1385 If the format is an empty string, this should be counted similarly to the
1386 case of extra format arguments. */
1387 if (res.number_extra_args > 0 && res.number_non_literal == 0
1388 && res.number_other == 0)
1389 warning (OPT_Wformat_extra_args, "too many arguments for format");
1390 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1391 && res.number_other == 0)
1392 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
1393 if (res.number_empty > 0 && res.number_non_literal == 0
1394 && res.number_other == 0)
1395 warning (OPT_Wformat_zero_length, "zero-length %s format string",
1396 format_types[info->format_type].name);
1398 if (res.number_wide > 0)
1399 warning (OPT_Wformat_, "format is a wide character string");
1401 if (res.number_unterminated > 0)
1402 warning (OPT_Wformat_, "unterminated format string");
1405 /* Callback from check_function_arguments_recurse to check a
1406 format string. FORMAT_TREE is the format parameter. ARG_NUM
1407 is the number of the format argument. CTX points to a
1408 format_check_context. */
1410 static void
1411 check_format_arg (void *ctx, tree format_tree,
1412 unsigned HOST_WIDE_INT arg_num)
1414 format_check_context *format_ctx = (format_check_context *) ctx;
1415 format_check_results *res = format_ctx->res;
1416 function_format_info *info = format_ctx->info;
1417 tree params = format_ctx->params;
1419 int format_length;
1420 HOST_WIDE_INT offset;
1421 const char *format_chars;
1422 tree array_size = 0;
1423 tree array_init;
1424 alloc_pool fwt_pool;
1426 if (integer_zerop (format_tree))
1428 /* Skip to first argument to check, so we can see if this format
1429 has any arguments (it shouldn't). */
1430 while (arg_num + 1 < info->first_arg_num)
1432 if (params == 0)
1433 return;
1434 params = TREE_CHAIN (params);
1435 ++arg_num;
1438 if (params == 0)
1439 res->number_other++;
1440 else
1441 res->number_extra_args++;
1443 return;
1446 offset = 0;
1447 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1449 tree arg0, arg1;
1451 arg0 = TREE_OPERAND (format_tree, 0);
1452 arg1 = TREE_OPERAND (format_tree, 1);
1453 STRIP_NOPS (arg0);
1454 STRIP_NOPS (arg1);
1455 if (TREE_CODE (arg1) == INTEGER_CST)
1456 format_tree = arg0;
1457 else
1459 res->number_non_literal++;
1460 return;
1462 if (!host_integerp (arg1, 0)
1463 || (offset = tree_low_cst (arg1, 0)) < 0)
1465 res->number_non_literal++;
1466 return;
1469 if (TREE_CODE (format_tree) != ADDR_EXPR)
1471 res->number_non_literal++;
1472 return;
1474 format_tree = TREE_OPERAND (format_tree, 0);
1475 if (format_types[info->format_type].flags
1476 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1478 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1479 /* We cannot examine this string here - but we can check that it is
1480 a valid type. */
1481 if (TREE_CODE (format_tree) != CONST_DECL
1482 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1483 || (*targetcm.string_object_ref_type_p)
1484 ((const_tree) TREE_TYPE (format_tree))))
1486 res->number_non_literal++;
1487 return;
1489 /* Skip to first argument to check. */
1490 while (arg_num + 1 < info->first_arg_num)
1492 if (params == 0)
1493 return;
1494 params = TREE_CHAIN (params);
1495 ++arg_num;
1497 /* So, we have a valid literal string object and one or more params.
1498 We need to use an external helper to parse the string into format
1499 info. For Objective-C variants we provide the resource within the
1500 objc tree, for target variants, via a hook. */
1501 if (objc_str)
1502 objc_check_format_arg (format_tree, params);
1503 else if (targetcm.check_string_object_format_arg)
1504 (*targetcm.check_string_object_format_arg) (format_tree, params);
1505 /* Else we can't handle it and retire quietly. */
1506 return;
1508 if (TREE_CODE (format_tree) == ARRAY_REF
1509 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
1510 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
1511 format_tree = TREE_OPERAND (format_tree, 0);
1512 if (TREE_CODE (format_tree) == VAR_DECL
1513 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1514 && (array_init = decl_constant_value (format_tree)) != format_tree
1515 && TREE_CODE (array_init) == STRING_CST)
1517 /* Extract the string constant initializer. Note that this may include
1518 a trailing NUL character that is not in the array (e.g.
1519 const char a[3] = "foo";). */
1520 array_size = DECL_SIZE_UNIT (format_tree);
1521 format_tree = array_init;
1523 if (TREE_CODE (format_tree) != STRING_CST)
1525 res->number_non_literal++;
1526 return;
1528 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1530 res->number_wide++;
1531 return;
1533 format_chars = TREE_STRING_POINTER (format_tree);
1534 format_length = TREE_STRING_LENGTH (format_tree);
1535 if (array_size != 0)
1537 /* Variable length arrays can't be initialized. */
1538 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1540 if (host_integerp (array_size, 0))
1542 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
1543 if (array_size_value > 0
1544 && array_size_value == (int) array_size_value
1545 && format_length > array_size_value)
1546 format_length = array_size_value;
1549 if (offset)
1551 if (offset >= format_length)
1553 res->number_non_literal++;
1554 return;
1556 format_chars += offset;
1557 format_length -= offset;
1559 if (format_length < 1 || format_chars[--format_length] != 0)
1561 res->number_unterminated++;
1562 return;
1564 if (format_length == 0)
1566 res->number_empty++;
1567 return;
1570 /* Skip to first argument to check. */
1571 while (arg_num + 1 < info->first_arg_num)
1573 if (params == 0)
1574 return;
1575 params = TREE_CHAIN (params);
1576 ++arg_num;
1578 /* Provisionally increment res->number_other; check_format_info_main
1579 will decrement it if it finds there are extra arguments, but this way
1580 need not adjust it for every return. */
1581 res->number_other++;
1582 fwt_pool = create_alloc_pool ("format_wanted_type pool",
1583 sizeof (format_wanted_type), 10);
1584 check_format_info_main (res, info, format_chars, format_length,
1585 params, arg_num, fwt_pool);
1586 free_alloc_pool (fwt_pool);
1590 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1591 is the NUL-terminated format string (which at this point may contain
1592 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1593 terminating NUL character). ARG_NUM is one less than the number of
1594 the first format argument to check; PARAMS points to that format
1595 argument in the list of arguments. */
1597 static void
1598 check_format_info_main (format_check_results *res,
1599 function_format_info *info, const char *format_chars,
1600 int format_length, tree params,
1601 unsigned HOST_WIDE_INT arg_num, alloc_pool fwt_pool)
1603 const char *orig_format_chars = format_chars;
1604 tree first_fillin_param = params;
1606 const format_kind_info *fki = &format_types[info->format_type];
1607 const format_flag_spec *flag_specs = fki->flag_specs;
1608 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1610 /* -1 if no conversions taking an operand have been found; 0 if one has
1611 and it didn't use $; 1 if $ formats are in use. */
1612 int has_operand_number = -1;
1614 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1616 while (*format_chars != 0)
1618 int i;
1619 int suppressed = FALSE;
1620 const char *length_chars = NULL;
1621 enum format_lengths length_chars_val = FMT_LEN_none;
1622 enum format_std_version length_chars_std = STD_C89;
1623 int format_char;
1624 tree cur_param;
1625 tree wanted_type;
1626 int main_arg_num = 0;
1627 tree main_arg_params = 0;
1628 enum format_std_version wanted_type_std;
1629 const char *wanted_type_name;
1630 format_wanted_type width_wanted_type;
1631 format_wanted_type precision_wanted_type;
1632 format_wanted_type main_wanted_type;
1633 format_wanted_type *first_wanted_type = NULL;
1634 format_wanted_type *last_wanted_type = NULL;
1635 const format_length_info *fli = NULL;
1636 const format_char_info *fci = NULL;
1637 char flag_chars[256];
1638 int alloc_flag = 0;
1639 int scalar_identity_flag = 0;
1640 const char *format_start;
1642 if (*format_chars++ != '%')
1643 continue;
1644 if (*format_chars == 0)
1646 warning (OPT_Wformat_, "spurious trailing %<%%%> in format");
1647 continue;
1649 if (*format_chars == '%')
1651 ++format_chars;
1652 continue;
1654 flag_chars[0] = 0;
1656 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1658 /* Possibly read a $ operand number at the start of the format.
1659 If one was previously used, one is required here. If one
1660 is not used here, we can't immediately conclude this is a
1661 format without them, since it could be printf %m or scanf %*. */
1662 int opnum;
1663 opnum = maybe_read_dollar_number (&format_chars, 0,
1664 first_fillin_param,
1665 &main_arg_params, fki);
1666 if (opnum == -1)
1667 return;
1668 else if (opnum > 0)
1670 has_operand_number = 1;
1671 main_arg_num = opnum + info->first_arg_num - 1;
1674 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1676 if (avoid_dollar_number (format_chars))
1677 return;
1680 /* Read any format flags, but do not yet validate them beyond removing
1681 duplicates, since in general validation depends on the rest of
1682 the format. */
1683 while (*format_chars != 0
1684 && strchr (fki->flag_chars, *format_chars) != 0)
1686 const format_flag_spec *s = get_flag_spec (flag_specs,
1687 *format_chars, NULL);
1688 if (strchr (flag_chars, *format_chars) != 0)
1690 warning (OPT_Wformat_, "repeated %s in format", _(s->name));
1692 else
1694 i = strlen (flag_chars);
1695 flag_chars[i++] = *format_chars;
1696 flag_chars[i] = 0;
1698 if (s->skip_next_char)
1700 ++format_chars;
1701 if (*format_chars == 0)
1703 warning (OPT_Wformat_, "missing fill character at end of strfmon format");
1704 return;
1707 ++format_chars;
1710 /* Read any format width, possibly * or *m$. */
1711 if (fki->width_char != 0)
1713 if (fki->width_type != NULL && *format_chars == '*')
1715 i = strlen (flag_chars);
1716 flag_chars[i++] = fki->width_char;
1717 flag_chars[i] = 0;
1718 /* "...a field width...may be indicated by an asterisk.
1719 In this case, an int argument supplies the field width..." */
1720 ++format_chars;
1721 if (has_operand_number != 0)
1723 int opnum;
1724 opnum = maybe_read_dollar_number (&format_chars,
1725 has_operand_number == 1,
1726 first_fillin_param,
1727 &params, fki);
1728 if (opnum == -1)
1729 return;
1730 else if (opnum > 0)
1732 has_operand_number = 1;
1733 arg_num = opnum + info->first_arg_num - 1;
1735 else
1736 has_operand_number = 0;
1738 else
1740 if (avoid_dollar_number (format_chars))
1741 return;
1743 if (info->first_arg_num != 0)
1745 if (params == 0)
1746 cur_param = NULL;
1747 else
1749 cur_param = TREE_VALUE (params);
1750 if (has_operand_number <= 0)
1752 params = TREE_CHAIN (params);
1753 ++arg_num;
1756 width_wanted_type.wanted_type = *fki->width_type;
1757 width_wanted_type.wanted_type_name = NULL;
1758 width_wanted_type.pointer_count = 0;
1759 width_wanted_type.char_lenient_flag = 0;
1760 width_wanted_type.scalar_identity_flag = 0;
1761 width_wanted_type.writing_in_flag = 0;
1762 width_wanted_type.reading_from_flag = 0;
1763 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1764 width_wanted_type.format_start = format_chars - 1;
1765 width_wanted_type.format_length = 1;
1766 width_wanted_type.param = cur_param;
1767 width_wanted_type.arg_num = arg_num;
1768 width_wanted_type.next = NULL;
1769 if (last_wanted_type != 0)
1770 last_wanted_type->next = &width_wanted_type;
1771 if (first_wanted_type == 0)
1772 first_wanted_type = &width_wanted_type;
1773 last_wanted_type = &width_wanted_type;
1776 else
1778 /* Possibly read a numeric width. If the width is zero,
1779 we complain if appropriate. */
1780 int non_zero_width_char = FALSE;
1781 int found_width = FALSE;
1782 while (ISDIGIT (*format_chars))
1784 found_width = TRUE;
1785 if (*format_chars != '0')
1786 non_zero_width_char = TRUE;
1787 ++format_chars;
1789 if (found_width && !non_zero_width_char &&
1790 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1791 warning (OPT_Wformat_, "zero width in %s format", fki->name);
1792 if (found_width)
1794 i = strlen (flag_chars);
1795 flag_chars[i++] = fki->width_char;
1796 flag_chars[i] = 0;
1801 /* Read any format left precision (must be a number, not *). */
1802 if (fki->left_precision_char != 0 && *format_chars == '#')
1804 ++format_chars;
1805 i = strlen (flag_chars);
1806 flag_chars[i++] = fki->left_precision_char;
1807 flag_chars[i] = 0;
1808 if (!ISDIGIT (*format_chars))
1809 warning (OPT_Wformat_, "empty left precision in %s format", fki->name);
1810 while (ISDIGIT (*format_chars))
1811 ++format_chars;
1814 /* Read any format precision, possibly * or *m$. */
1815 if (fki->precision_char != 0 && *format_chars == '.')
1817 ++format_chars;
1818 i = strlen (flag_chars);
1819 flag_chars[i++] = fki->precision_char;
1820 flag_chars[i] = 0;
1821 if (fki->precision_type != NULL && *format_chars == '*')
1823 /* "...a...precision...may be indicated by an asterisk.
1824 In this case, an int argument supplies the...precision." */
1825 ++format_chars;
1826 if (has_operand_number != 0)
1828 int opnum;
1829 opnum = maybe_read_dollar_number (&format_chars,
1830 has_operand_number == 1,
1831 first_fillin_param,
1832 &params, fki);
1833 if (opnum == -1)
1834 return;
1835 else if (opnum > 0)
1837 has_operand_number = 1;
1838 arg_num = opnum + info->first_arg_num - 1;
1840 else
1841 has_operand_number = 0;
1843 else
1845 if (avoid_dollar_number (format_chars))
1846 return;
1848 if (info->first_arg_num != 0)
1850 if (params == 0)
1851 cur_param = NULL;
1852 else
1854 cur_param = TREE_VALUE (params);
1855 if (has_operand_number <= 0)
1857 params = TREE_CHAIN (params);
1858 ++arg_num;
1861 precision_wanted_type.wanted_type = *fki->precision_type;
1862 precision_wanted_type.wanted_type_name = NULL;
1863 precision_wanted_type.pointer_count = 0;
1864 precision_wanted_type.char_lenient_flag = 0;
1865 precision_wanted_type.scalar_identity_flag = 0;
1866 precision_wanted_type.writing_in_flag = 0;
1867 precision_wanted_type.reading_from_flag = 0;
1868 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1869 precision_wanted_type.param = cur_param;
1870 precision_wanted_type.format_start = format_chars - 2;
1871 precision_wanted_type.format_length = 2;
1872 precision_wanted_type.arg_num = arg_num;
1873 precision_wanted_type.next = NULL;
1874 if (last_wanted_type != 0)
1875 last_wanted_type->next = &precision_wanted_type;
1876 if (first_wanted_type == 0)
1877 first_wanted_type = &precision_wanted_type;
1878 last_wanted_type = &precision_wanted_type;
1881 else
1883 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
1884 && !ISDIGIT (*format_chars))
1885 warning (OPT_Wformat_, "empty precision in %s format", fki->name);
1886 while (ISDIGIT (*format_chars))
1887 ++format_chars;
1891 format_start = format_chars;
1892 if (fki->alloc_char && fki->alloc_char == *format_chars)
1894 i = strlen (flag_chars);
1895 flag_chars[i++] = fki->alloc_char;
1896 flag_chars[i] = 0;
1897 format_chars++;
1900 /* Handle the scanf allocation kludge. */
1901 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
1903 if (*format_chars == 'a' && !flag_isoc99)
1905 if (format_chars[1] == 's' || format_chars[1] == 'S'
1906 || format_chars[1] == '[')
1908 /* 'a' is used as a flag. */
1909 i = strlen (flag_chars);
1910 flag_chars[i++] = 'a';
1911 flag_chars[i] = 0;
1912 format_chars++;
1917 /* Read any length modifier, if this kind of format has them. */
1918 fli = fki->length_char_specs;
1919 length_chars = NULL;
1920 length_chars_val = FMT_LEN_none;
1921 length_chars_std = STD_C89;
1922 scalar_identity_flag = 0;
1923 if (fli)
1925 while (fli->name != 0
1926 && strncmp (fli->name, format_chars, strlen (fli->name)))
1927 fli++;
1928 if (fli->name != 0)
1930 format_chars += strlen (fli->name);
1931 if (fli->double_name != 0 && fli->name[0] == *format_chars)
1933 format_chars++;
1934 length_chars = fli->double_name;
1935 length_chars_val = fli->double_index;
1936 length_chars_std = fli->double_std;
1938 else
1940 length_chars = fli->name;
1941 length_chars_val = fli->index;
1942 length_chars_std = fli->std;
1943 scalar_identity_flag = fli->scalar_identity_flag;
1945 i = strlen (flag_chars);
1946 flag_chars[i++] = fki->length_code_char;
1947 flag_chars[i] = 0;
1949 if (pedantic)
1951 /* Warn if the length modifier is non-standard. */
1952 if (ADJ_STD (length_chars_std) > C_STD_VER)
1953 warning (OPT_Wformat_,
1954 "%s does not support the %qs %s length modifier",
1955 C_STD_NAME (length_chars_std), length_chars,
1956 fki->name);
1960 /* Read any modifier (strftime E/O). */
1961 if (fki->modifier_chars != NULL)
1963 while (*format_chars != 0
1964 && strchr (fki->modifier_chars, *format_chars) != 0)
1966 if (strchr (flag_chars, *format_chars) != 0)
1968 const format_flag_spec *s = get_flag_spec (flag_specs,
1969 *format_chars, NULL);
1970 warning (OPT_Wformat_, "repeated %s in format", _(s->name));
1972 else
1974 i = strlen (flag_chars);
1975 flag_chars[i++] = *format_chars;
1976 flag_chars[i] = 0;
1978 ++format_chars;
1982 format_char = *format_chars;
1983 if (format_char == 0
1984 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
1985 && format_char == '%'))
1987 warning (OPT_Wformat_, "conversion lacks type at end of format");
1988 continue;
1990 format_chars++;
1991 fci = fki->conversion_specs;
1992 while (fci->format_chars != 0
1993 && strchr (fci->format_chars, format_char) == 0)
1994 ++fci;
1995 if (fci->format_chars == 0)
1997 if (ISGRAPH (format_char))
1998 warning (OPT_Wformat_, "unknown conversion type character %qc in format",
1999 format_char);
2000 else
2001 warning (OPT_Wformat_, "unknown conversion type character 0x%x in format",
2002 format_char);
2003 continue;
2005 if (pedantic)
2007 if (ADJ_STD (fci->std) > C_STD_VER)
2008 warning (OPT_Wformat_, "%s does not support the %<%%%c%> %s format",
2009 C_STD_NAME (fci->std), format_char, fki->name);
2012 /* Validate the individual flags used, removing any that are invalid. */
2014 int d = 0;
2015 for (i = 0; flag_chars[i] != 0; i++)
2017 const format_flag_spec *s = get_flag_spec (flag_specs,
2018 flag_chars[i], NULL);
2019 flag_chars[i - d] = flag_chars[i];
2020 if (flag_chars[i] == fki->length_code_char)
2021 continue;
2022 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2024 warning (OPT_Wformat_, "%s used with %<%%%c%> %s format",
2025 _(s->name), format_char, fki->name);
2026 d++;
2027 continue;
2029 if (pedantic)
2031 const format_flag_spec *t;
2032 if (ADJ_STD (s->std) > C_STD_VER)
2033 warning (OPT_Wformat_, "%s does not support %s",
2034 C_STD_NAME (s->std), _(s->long_name));
2035 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2036 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2038 const char *long_name = (t->long_name != NULL
2039 ? t->long_name
2040 : s->long_name);
2041 if (ADJ_STD (t->std) > C_STD_VER)
2042 warning (OPT_Wformat_,
2043 "%s does not support %s with the %<%%%c%> %s format",
2044 C_STD_NAME (t->std), _(long_name),
2045 format_char, fki->name);
2049 flag_chars[i - d] = 0;
2052 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2053 && strchr (flag_chars, 'a') != 0)
2054 alloc_flag = 1;
2055 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2056 alloc_flag = 1;
2058 if (fki->suppression_char
2059 && strchr (flag_chars, fki->suppression_char) != 0)
2060 suppressed = 1;
2062 /* Validate the pairs of flags used. */
2063 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2065 const format_flag_spec *s, *t;
2066 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2067 continue;
2068 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2069 continue;
2070 if (bad_flag_pairs[i].predicate != 0
2071 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2072 continue;
2073 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2074 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2075 if (bad_flag_pairs[i].ignored)
2077 if (bad_flag_pairs[i].predicate != 0)
2078 warning (OPT_Wformat_,
2079 "%s ignored with %s and %<%%%c%> %s format",
2080 _(s->name), _(t->name), format_char,
2081 fki->name);
2082 else
2083 warning (OPT_Wformat_, "%s ignored with %s in %s format",
2084 _(s->name), _(t->name), fki->name);
2086 else
2088 if (bad_flag_pairs[i].predicate != 0)
2089 warning (OPT_Wformat_,
2090 "use of %s and %s together with %<%%%c%> %s format",
2091 _(s->name), _(t->name), format_char,
2092 fki->name);
2093 else
2094 warning (OPT_Wformat_, "use of %s and %s together in %s format",
2095 _(s->name), _(t->name), fki->name);
2099 /* Give Y2K warnings. */
2100 if (warn_format_y2k)
2102 int y2k_level = 0;
2103 if (strchr (fci->flags2, '4') != 0)
2104 if (strchr (flag_chars, 'E') != 0)
2105 y2k_level = 3;
2106 else
2107 y2k_level = 2;
2108 else if (strchr (fci->flags2, '3') != 0)
2109 y2k_level = 3;
2110 else if (strchr (fci->flags2, '2') != 0)
2111 y2k_level = 2;
2112 if (y2k_level == 3)
2113 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2114 "year in some locales", format_char);
2115 else if (y2k_level == 2)
2116 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
2117 "year", format_char);
2120 if (strchr (fci->flags2, '[') != 0)
2122 /* Skip over scan set, in case it happens to have '%' in it. */
2123 if (*format_chars == '^')
2124 ++format_chars;
2125 /* Find closing bracket; if one is hit immediately, then
2126 it's part of the scan set rather than a terminator. */
2127 if (*format_chars == ']')
2128 ++format_chars;
2129 while (*format_chars && *format_chars != ']')
2130 ++format_chars;
2131 if (*format_chars != ']')
2132 /* The end of the format string was reached. */
2133 warning (OPT_Wformat_, "no closing %<]%> for %<%%[%> format");
2136 wanted_type = 0;
2137 wanted_type_name = 0;
2138 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2140 wanted_type = (fci->types[length_chars_val].type
2141 ? *fci->types[length_chars_val].type : 0);
2142 wanted_type_name = fci->types[length_chars_val].name;
2143 wanted_type_std = fci->types[length_chars_val].std;
2144 if (wanted_type == 0)
2146 warning (OPT_Wformat_,
2147 "use of %qs length modifier with %qc type character",
2148 length_chars, format_char);
2149 /* Heuristic: skip one argument when an invalid length/type
2150 combination is encountered. */
2151 arg_num++;
2152 if (params != 0)
2153 params = TREE_CHAIN (params);
2154 continue;
2156 else if (pedantic
2157 /* Warn if non-standard, provided it is more non-standard
2158 than the length and type characters that may already
2159 have been warned for. */
2160 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2161 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2163 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2164 warning (OPT_Wformat_,
2165 "%s does not support the %<%%%s%c%> %s format",
2166 C_STD_NAME (wanted_type_std), length_chars,
2167 format_char, fki->name);
2171 main_wanted_type.next = NULL;
2173 /* Finally. . .check type of argument against desired type! */
2174 if (info->first_arg_num == 0)
2175 continue;
2176 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2177 || suppressed)
2179 if (main_arg_num != 0)
2181 if (suppressed)
2182 warning (OPT_Wformat_, "operand number specified with "
2183 "suppressed assignment");
2184 else
2185 warning (OPT_Wformat_, "operand number specified for format "
2186 "taking no argument");
2189 else
2191 format_wanted_type *wanted_type_ptr;
2193 if (main_arg_num != 0)
2195 arg_num = main_arg_num;
2196 params = main_arg_params;
2198 else
2200 ++arg_num;
2201 if (has_operand_number > 0)
2203 warning (OPT_Wformat_, "missing $ operand number in format");
2204 return;
2206 else
2207 has_operand_number = 0;
2210 wanted_type_ptr = &main_wanted_type;
2211 while (fci)
2213 if (params == 0)
2214 cur_param = NULL;
2215 else
2217 cur_param = TREE_VALUE (params);
2218 params = TREE_CHAIN (params);
2221 wanted_type_ptr->wanted_type = wanted_type;
2222 wanted_type_ptr->wanted_type_name = wanted_type_name;
2223 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2224 wanted_type_ptr->char_lenient_flag = 0;
2225 if (strchr (fci->flags2, 'c') != 0)
2226 wanted_type_ptr->char_lenient_flag = 1;
2227 wanted_type_ptr->scalar_identity_flag = 0;
2228 if (scalar_identity_flag)
2229 wanted_type_ptr->scalar_identity_flag = 1;
2230 wanted_type_ptr->writing_in_flag = 0;
2231 wanted_type_ptr->reading_from_flag = 0;
2232 if (alloc_flag)
2233 wanted_type_ptr->writing_in_flag = 1;
2234 else
2236 if (strchr (fci->flags2, 'W') != 0)
2237 wanted_type_ptr->writing_in_flag = 1;
2238 if (strchr (fci->flags2, 'R') != 0)
2239 wanted_type_ptr->reading_from_flag = 1;
2241 wanted_type_ptr->kind = CF_KIND_FORMAT;
2242 wanted_type_ptr->param = cur_param;
2243 wanted_type_ptr->arg_num = arg_num;
2244 wanted_type_ptr->format_start = format_start;
2245 wanted_type_ptr->format_length = format_chars - format_start;
2246 wanted_type_ptr->next = NULL;
2247 if (last_wanted_type != 0)
2248 last_wanted_type->next = wanted_type_ptr;
2249 if (first_wanted_type == 0)
2250 first_wanted_type = wanted_type_ptr;
2251 last_wanted_type = wanted_type_ptr;
2253 fci = fci->chain;
2254 if (fci)
2256 wanted_type_ptr = (format_wanted_type *)
2257 pool_alloc (fwt_pool);
2258 arg_num++;
2259 wanted_type = *fci->types[length_chars_val].type;
2260 wanted_type_name = fci->types[length_chars_val].name;
2265 if (first_wanted_type != 0)
2266 check_format_types (first_wanted_type);
2269 if (format_chars - orig_format_chars != format_length)
2270 warning (OPT_Wformat_contains_nul, "embedded %<\\0%> in format");
2271 if (info->first_arg_num != 0 && params != 0
2272 && has_operand_number <= 0)
2274 res->number_other--;
2275 res->number_extra_args++;
2277 if (has_operand_number > 0)
2278 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2282 /* Check the argument types from a single format conversion (possibly
2283 including width and precision arguments). */
2284 static void
2285 check_format_types (format_wanted_type *types)
2287 for (; types != 0; types = types->next)
2289 tree cur_param;
2290 tree cur_type;
2291 tree orig_cur_type;
2292 tree wanted_type;
2293 int arg_num;
2294 int i;
2295 int char_type_flag;
2297 wanted_type = types->wanted_type;
2298 arg_num = types->arg_num;
2300 /* The following should not occur here. */
2301 gcc_assert (wanted_type);
2302 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2304 if (types->pointer_count == 0)
2305 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2307 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2309 cur_param = types->param;
2310 if (!cur_param)
2312 format_type_warning (types, wanted_type, NULL);
2313 continue;
2316 cur_type = TREE_TYPE (cur_param);
2317 if (cur_type == error_mark_node)
2318 continue;
2319 orig_cur_type = cur_type;
2320 char_type_flag = 0;
2322 STRIP_NOPS (cur_param);
2324 /* Check the types of any additional pointer arguments
2325 that precede the "real" argument. */
2326 for (i = 0; i < types->pointer_count; ++i)
2328 if (TREE_CODE (cur_type) == POINTER_TYPE)
2330 cur_type = TREE_TYPE (cur_type);
2331 if (cur_type == error_mark_node)
2332 break;
2334 /* Check for writing through a NULL pointer. */
2335 if (types->writing_in_flag
2336 && i == 0
2337 && cur_param != 0
2338 && integer_zerop (cur_param))
2339 warning (OPT_Wformat_, "writing through null pointer "
2340 "(argument %d)", arg_num);
2342 /* Check for reading through a NULL pointer. */
2343 if (types->reading_from_flag
2344 && i == 0
2345 && cur_param != 0
2346 && integer_zerop (cur_param))
2347 warning (OPT_Wformat_, "reading through null pointer "
2348 "(argument %d)", arg_num);
2350 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2351 cur_param = TREE_OPERAND (cur_param, 0);
2352 else
2353 cur_param = 0;
2355 /* See if this is an attempt to write into a const type with
2356 scanf or with printf "%n". Note: the writing in happens
2357 at the first indirection only, if for example
2358 void * const * is passed to scanf %p; passing
2359 const void ** is simply passing an incompatible type. */
2360 if (types->writing_in_flag
2361 && i == 0
2362 && (TYPE_READONLY (cur_type)
2363 || (cur_param != 0
2364 && (CONSTANT_CLASS_P (cur_param)
2365 || (DECL_P (cur_param)
2366 && TREE_READONLY (cur_param))))))
2367 warning (OPT_Wformat_, "writing into constant object "
2368 "(argument %d)", arg_num);
2370 /* If there are extra type qualifiers beyond the first
2371 indirection, then this makes the types technically
2372 incompatible. */
2373 if (i > 0
2374 && pedantic
2375 && (TYPE_READONLY (cur_type)
2376 || TYPE_VOLATILE (cur_type)
2377 || TYPE_RESTRICT (cur_type)))
2378 warning (OPT_Wformat_, "extra type qualifiers in format "
2379 "argument (argument %d)",
2380 arg_num);
2383 else
2385 format_type_warning (types, wanted_type, orig_cur_type);
2386 break;
2390 if (i < types->pointer_count)
2391 continue;
2393 cur_type = TYPE_MAIN_VARIANT (cur_type);
2395 /* Check whether the argument type is a character type. This leniency
2396 only applies to certain formats, flagged with 'c'.
2398 if (types->char_lenient_flag)
2399 char_type_flag = (cur_type == char_type_node
2400 || cur_type == signed_char_type_node
2401 || cur_type == unsigned_char_type_node);
2403 /* Check the type of the "real" argument, if there's a type we want. */
2404 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2405 continue;
2406 /* If we want 'void *', allow any pointer type.
2407 (Anything else would already have got a warning.)
2408 With -Wpedantic, only allow pointers to void and to character
2409 types. */
2410 if (wanted_type == void_type_node
2411 && (!pedantic || (i == 1 && char_type_flag)))
2412 continue;
2413 /* Don't warn about differences merely in signedness, unless
2414 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2415 target and not a character type, and for character types at
2416 a second level of indirection. */
2417 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2418 && TREE_CODE (cur_type) == INTEGER_TYPE
2419 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
2420 && (TYPE_UNSIGNED (wanted_type)
2421 ? wanted_type == c_common_unsigned_type (cur_type)
2422 : wanted_type == c_common_signed_type (cur_type)))
2423 continue;
2424 /* Likewise, "signed char", "unsigned char" and "char" are
2425 equivalent but the above test won't consider them equivalent. */
2426 if (wanted_type == char_type_node
2427 && (!pedantic || i < 2)
2428 && char_type_flag)
2429 continue;
2430 if (types->scalar_identity_flag
2431 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2432 || (INTEGRAL_TYPE_P (cur_type)
2433 && INTEGRAL_TYPE_P (wanted_type)))
2434 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2435 continue;
2436 /* Now we have a type mismatch. */
2437 format_type_warning (types, wanted_type, orig_cur_type);
2442 /* Give a warning about a format argument of different type from that
2443 expected. WANTED_TYPE is the type the argument should have, possibly
2444 stripped of pointer dereferences. The description (such as "field
2445 precision"), the placement in the format string, a possibly more
2446 friendly name of WANTED_TYPE, and the number of pointer dereferences
2447 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2448 or NULL if it is missing. */
2449 static void
2450 format_type_warning (format_wanted_type *type, tree wanted_type, tree arg_type)
2452 int kind = type->kind;
2453 const char *wanted_type_name = type->wanted_type_name;
2454 const char *format_start = type->format_start;
2455 int format_length = type->format_length;
2456 int pointer_count = type->pointer_count;
2457 int arg_num = type->arg_num;
2459 char *p;
2460 /* If ARG_TYPE is a typedef with a misleading name (for example,
2461 size_t but not the standard size_t expected by printf %zu), avoid
2462 printing the typedef name. */
2463 if (wanted_type_name
2464 && arg_type
2465 && TYPE_NAME (arg_type)
2466 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2467 && DECL_NAME (TYPE_NAME (arg_type))
2468 && !strcmp (wanted_type_name,
2469 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2470 arg_type = TYPE_MAIN_VARIANT (arg_type);
2471 /* The format type and name exclude any '*' for pointers, so those
2472 must be formatted manually. For all the types we currently have,
2473 this is adequate, but formats taking pointers to functions or
2474 arrays would require the full type to be built up in order to
2475 print it with %T. */
2476 p = (char *) alloca (pointer_count + 2);
2477 if (pointer_count == 0)
2478 p[0] = 0;
2479 else if (c_dialect_cxx ())
2481 memset (p, '*', pointer_count);
2482 p[pointer_count] = 0;
2484 else
2486 p[0] = ' ';
2487 memset (p + 1, '*', pointer_count);
2488 p[pointer_count + 1] = 0;
2491 if (wanted_type_name)
2493 if (arg_type)
2494 warning (OPT_Wformat_, "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2495 "but argument %d has type %qT",
2496 gettext (kind_descriptions[kind]),
2497 (kind == CF_KIND_FORMAT ? "%" : ""),
2498 format_length, format_start,
2499 wanted_type_name, p, arg_num, arg_type);
2500 else
2501 warning (OPT_Wformat_, "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2502 gettext (kind_descriptions[kind]),
2503 (kind == CF_KIND_FORMAT ? "%" : ""),
2504 format_length, format_start, wanted_type_name, p);
2506 else
2508 if (arg_type)
2509 warning (OPT_Wformat_, "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2510 "but argument %d has type %qT",
2511 gettext (kind_descriptions[kind]),
2512 (kind == CF_KIND_FORMAT ? "%" : ""),
2513 format_length, format_start,
2514 wanted_type, p, arg_num, arg_type);
2515 else
2516 warning (OPT_Wformat_, "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2517 gettext (kind_descriptions[kind]),
2518 (kind == CF_KIND_FORMAT ? "%" : ""),
2519 format_length, format_start, wanted_type, p);
2524 /* Given a format_char_info array FCI, and a character C, this function
2525 returns the index into the conversion_specs where that specifier's
2526 data is located. The character must exist. */
2527 static unsigned int
2528 find_char_info_specifier_index (const format_char_info *fci, int c)
2530 unsigned i;
2532 for (i = 0; fci->format_chars; i++, fci++)
2533 if (strchr (fci->format_chars, c))
2534 return i;
2536 /* We shouldn't be looking for a non-existent specifier. */
2537 gcc_unreachable ();
2540 /* Given a format_length_info array FLI, and a character C, this
2541 function returns the index into the conversion_specs where that
2542 modifier's data is located. The character must exist. */
2543 static unsigned int
2544 find_length_info_modifier_index (const format_length_info *fli, int c)
2546 unsigned i;
2548 for (i = 0; fli->name; i++, fli++)
2549 if (strchr (fli->name, c))
2550 return i;
2552 /* We shouldn't be looking for a non-existent modifier. */
2553 gcc_unreachable ();
2556 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2557 use in GCC's __asm_fprintf__ custom format attribute. You must
2558 have set dynamic_format_types before calling this function. */
2559 static void
2560 init_dynamic_asm_fprintf_info (void)
2562 static tree hwi;
2564 if (!hwi)
2566 format_length_info *new_asm_fprintf_length_specs;
2567 unsigned int i;
2569 /* Find the underlying type for HOST_WIDE_INT. For the %w
2570 length modifier to work, one must have issued: "typedef
2571 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2572 prior to using that modifier. */
2573 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2574 if (!hwi)
2576 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2577 return;
2579 hwi = identifier_global_value (hwi);
2580 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2582 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2583 return;
2585 hwi = DECL_ORIGINAL_TYPE (hwi);
2586 gcc_assert (hwi);
2587 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2589 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2590 " or %<long long%>");
2591 return;
2594 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2595 new_asm_fprintf_length_specs = (format_length_info *)
2596 xmemdup (asm_fprintf_length_specs,
2597 sizeof (asm_fprintf_length_specs),
2598 sizeof (asm_fprintf_length_specs));
2600 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2601 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2602 if (hwi == long_integer_type_node)
2603 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2604 else if (hwi == long_long_integer_type_node)
2605 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2606 else
2607 gcc_unreachable ();
2609 /* Assign the new data for use. */
2610 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2611 new_asm_fprintf_length_specs;
2615 /* Determine the type of a "locus" in the code being compiled for use
2616 in GCC's __gcc_gfc__ custom format attribute. You must have set
2617 dynamic_format_types before calling this function. */
2618 static void
2619 init_dynamic_gfc_info (void)
2621 static tree locus;
2623 if (!locus)
2625 static format_char_info *gfc_fci;
2627 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2628 must have declared 'locus' prior to using this attribute. If
2629 we haven't seen this declarations then you shouldn't use the
2630 specifier requiring that type. */
2631 if ((locus = maybe_get_identifier ("locus")))
2633 locus = identifier_global_value (locus);
2634 if (locus)
2636 if (TREE_CODE (locus) != TYPE_DECL
2637 || TREE_TYPE (locus) == error_mark_node)
2639 error ("%<locus%> is not defined as a type");
2640 locus = 0;
2642 else
2643 locus = TREE_TYPE (locus);
2647 /* Assign the new data for use. */
2649 /* Handle the __gcc_gfc__ format specifics. */
2650 if (!gfc_fci)
2651 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2652 gfc_fci = (format_char_info *)
2653 xmemdup (gcc_gfc_char_table,
2654 sizeof (gcc_gfc_char_table),
2655 sizeof (gcc_gfc_char_table));
2656 if (locus)
2658 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2659 gfc_fci[i].types[0].type = &locus;
2660 gfc_fci[i].pointer_count = 1;
2665 /* Determine the types of "tree" and "location_t" in the code being
2666 compiled for use in GCC's diagnostic custom format attributes. You
2667 must have set dynamic_format_types before calling this function. */
2668 static void
2669 init_dynamic_diag_info (void)
2671 static tree t, loc, hwi;
2673 if (!loc || !t || !hwi)
2675 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2676 static format_length_info *diag_ls;
2677 unsigned int i;
2679 /* For the GCC-diagnostics custom format specifiers to work, one
2680 must have declared 'tree' and/or 'location_t' prior to using
2681 those attributes. If we haven't seen these declarations then
2682 you shouldn't use the specifiers requiring these types.
2683 However we don't force a hard ICE because we may see only one
2684 or the other type. */
2685 if ((loc = maybe_get_identifier ("location_t")))
2687 loc = identifier_global_value (loc);
2688 if (loc)
2690 if (TREE_CODE (loc) != TYPE_DECL)
2692 error ("%<location_t%> is not defined as a type");
2693 loc = 0;
2695 else
2696 loc = TREE_TYPE (loc);
2700 /* We need to grab the underlying 'union tree_node' so peek into
2701 an extra type level. */
2702 if ((t = maybe_get_identifier ("tree")))
2704 t = identifier_global_value (t);
2705 if (t)
2707 if (TREE_CODE (t) != TYPE_DECL)
2709 error ("%<tree%> is not defined as a type");
2710 t = 0;
2712 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2714 error ("%<tree%> is not defined as a pointer type");
2715 t = 0;
2717 else
2718 t = TREE_TYPE (TREE_TYPE (t));
2722 /* Find the underlying type for HOST_WIDE_INT. For the %w
2723 length modifier to work, one must have issued: "typedef
2724 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2725 prior to using that modifier. */
2726 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2728 hwi = identifier_global_value (hwi);
2729 if (hwi)
2731 if (TREE_CODE (hwi) != TYPE_DECL)
2733 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2734 hwi = 0;
2736 else
2738 hwi = DECL_ORIGINAL_TYPE (hwi);
2739 gcc_assert (hwi);
2740 if (hwi != long_integer_type_node
2741 && hwi != long_long_integer_type_node)
2743 error ("%<__gcc_host_wide_int__%> is not defined"
2744 " as %<long%> or %<long long%>");
2745 hwi = 0;
2751 /* Assign the new data for use. */
2753 /* All the GCC diag formats use the same length specs. */
2754 if (!diag_ls)
2755 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2756 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2757 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2758 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2759 diag_ls = (format_length_info *)
2760 xmemdup (gcc_diag_length_specs,
2761 sizeof (gcc_diag_length_specs),
2762 sizeof (gcc_diag_length_specs));
2763 if (hwi)
2765 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2766 i = find_length_info_modifier_index (diag_ls, 'w');
2767 if (hwi == long_integer_type_node)
2768 diag_ls[i].index = FMT_LEN_l;
2769 else if (hwi == long_long_integer_type_node)
2770 diag_ls[i].index = FMT_LEN_ll;
2771 else
2772 gcc_unreachable ();
2775 /* Handle the __gcc_diag__ format specifics. */
2776 if (!diag_fci)
2777 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2778 diag_fci = (format_char_info *)
2779 xmemdup (gcc_diag_char_table,
2780 sizeof (gcc_diag_char_table),
2781 sizeof (gcc_diag_char_table));
2782 if (t)
2784 i = find_char_info_specifier_index (diag_fci, 'K');
2785 diag_fci[i].types[0].type = &t;
2786 diag_fci[i].pointer_count = 1;
2789 /* Handle the __gcc_tdiag__ format specifics. */
2790 if (!tdiag_fci)
2791 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2792 tdiag_fci = (format_char_info *)
2793 xmemdup (gcc_tdiag_char_table,
2794 sizeof (gcc_tdiag_char_table),
2795 sizeof (gcc_tdiag_char_table));
2796 if (t)
2798 /* All specifiers taking a tree share the same struct. */
2799 i = find_char_info_specifier_index (tdiag_fci, 'D');
2800 tdiag_fci[i].types[0].type = &t;
2801 tdiag_fci[i].pointer_count = 1;
2802 i = find_char_info_specifier_index (tdiag_fci, 'K');
2803 tdiag_fci[i].types[0].type = &t;
2804 tdiag_fci[i].pointer_count = 1;
2807 /* Handle the __gcc_cdiag__ format specifics. */
2808 if (!cdiag_fci)
2809 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
2810 cdiag_fci = (format_char_info *)
2811 xmemdup (gcc_cdiag_char_table,
2812 sizeof (gcc_cdiag_char_table),
2813 sizeof (gcc_cdiag_char_table));
2814 if (t)
2816 /* All specifiers taking a tree share the same struct. */
2817 i = find_char_info_specifier_index (cdiag_fci, 'D');
2818 cdiag_fci[i].types[0].type = &t;
2819 cdiag_fci[i].pointer_count = 1;
2820 i = find_char_info_specifier_index (cdiag_fci, 'K');
2821 cdiag_fci[i].types[0].type = &t;
2822 cdiag_fci[i].pointer_count = 1;
2825 /* Handle the __gcc_cxxdiag__ format specifics. */
2826 if (!cxxdiag_fci)
2827 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
2828 cxxdiag_fci = (format_char_info *)
2829 xmemdup (gcc_cxxdiag_char_table,
2830 sizeof (gcc_cxxdiag_char_table),
2831 sizeof (gcc_cxxdiag_char_table));
2832 if (t)
2834 /* All specifiers taking a tree share the same struct. */
2835 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
2836 cxxdiag_fci[i].types[0].type = &t;
2837 cxxdiag_fci[i].pointer_count = 1;
2838 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
2839 cxxdiag_fci[i].types[0].type = &t;
2840 cxxdiag_fci[i].pointer_count = 1;
2845 #ifdef TARGET_FORMAT_TYPES
2846 extern const format_kind_info TARGET_FORMAT_TYPES[];
2847 #endif
2849 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2850 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
2851 #endif
2852 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2853 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
2854 #endif
2856 /* Attributes such as "printf" are equivalent to those such as
2857 "gnu_printf" unless this is overridden by a target. */
2858 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
2860 { "gnu_printf", "printf" },
2861 { "gnu_scanf", "scanf" },
2862 { "gnu_strftime", "strftime" },
2863 { "gnu_strfmon", "strfmon" },
2864 { NULL, NULL }
2867 /* Translate to unified attribute name. This is used in decode_format_type and
2868 decode_format_attr. In attr_name the user specified argument is passed. It
2869 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2870 or the attr_name passed to this function, if there is no matching entry. */
2871 static const char *
2872 convert_format_name_to_system_name (const char *attr_name)
2874 int i;
2876 if (attr_name == NULL || *attr_name == 0
2877 || strncmp (attr_name, "gcc_", 4) == 0)
2878 return attr_name;
2879 #ifdef TARGET_OVERRIDES_FORMAT_INIT
2880 TARGET_OVERRIDES_FORMAT_INIT ();
2881 #endif
2883 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
2884 /* Check if format attribute is overridden by target. */
2885 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
2886 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
2888 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
2890 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
2891 attr_name))
2892 return attr_name;
2893 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
2894 attr_name))
2895 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
2898 #endif
2899 /* Otherwise default to gnu format. */
2900 for (i = 0;
2901 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
2902 ++i)
2904 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
2905 attr_name))
2906 return attr_name;
2907 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
2908 attr_name))
2909 return gnu_target_overrides_format_attributes[i].named_attr_src;
2912 return attr_name;
2915 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
2916 counting "name" and "__name__" as the same, false otherwise. */
2917 static bool
2918 cmp_attribs (const char *tattr_name, const char *attr_name)
2920 int alen = strlen (attr_name);
2921 int slen = (tattr_name ? strlen (tattr_name) : 0);
2922 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
2923 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
2925 attr_name += 2;
2926 alen -= 4;
2928 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
2929 return false;
2930 return true;
2933 /* Handle a "format" attribute; arguments as in
2934 struct attribute_spec.handler. */
2935 tree
2936 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
2937 int flags, bool *no_add_attrs)
2939 tree type = *node;
2940 function_format_info info;
2942 #ifdef TARGET_FORMAT_TYPES
2943 /* If the target provides additional format types, we need to
2944 add them to FORMAT_TYPES at first use. */
2945 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
2947 dynamic_format_types = XNEWVEC (format_kind_info,
2948 n_format_types + TARGET_N_FORMAT_TYPES);
2949 memcpy (dynamic_format_types, format_types_orig,
2950 sizeof (format_types_orig));
2951 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
2952 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
2954 format_types = dynamic_format_types;
2955 /* Provide a reference for the first potential external type. */
2956 first_target_format_type = n_format_types;
2957 n_format_types += TARGET_N_FORMAT_TYPES;
2959 #endif
2961 if (!decode_format_attr (args, &info, 0))
2963 *no_add_attrs = true;
2964 return NULL_TREE;
2967 if (prototype_p (type))
2969 if (!check_format_string (type, info.format_num, flags,
2970 no_add_attrs, info.format_type))
2971 return NULL_TREE;
2973 if (info.first_arg_num != 0)
2975 unsigned HOST_WIDE_INT arg_num = 1;
2976 function_args_iterator iter;
2977 tree arg_type;
2979 /* Verify that first_arg_num points to the last arg,
2980 the ... */
2981 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
2982 arg_num++;
2984 if (arg_num != info.first_arg_num)
2986 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
2987 error ("args to be formatted is not %<...%>");
2988 *no_add_attrs = true;
2989 return NULL_TREE;
2994 /* Check if this is a strftime variant. Just for this variant
2995 FMT_FLAG_ARG_CONVERT is not set. */
2996 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
2997 && info.first_arg_num != 0)
2999 error ("strftime formats cannot format arguments");
3000 *no_add_attrs = true;
3001 return NULL_TREE;
3004 /* If this is a custom GCC-internal format type, we have to
3005 initialize certain bits at runtime. */
3006 if (info.format_type == asm_fprintf_format_type
3007 || info.format_type == gcc_gfc_format_type
3008 || info.format_type == gcc_diag_format_type
3009 || info.format_type == gcc_tdiag_format_type
3010 || info.format_type == gcc_cdiag_format_type
3011 || info.format_type == gcc_cxxdiag_format_type)
3013 /* Our first time through, we have to make sure that our
3014 format_type data is allocated dynamically and is modifiable. */
3015 if (!dynamic_format_types)
3016 format_types = dynamic_format_types = (format_kind_info *)
3017 xmemdup (format_types_orig, sizeof (format_types_orig),
3018 sizeof (format_types_orig));
3020 /* If this is format __asm_fprintf__, we have to initialize
3021 GCC's notion of HOST_WIDE_INT for checking %wd. */
3022 if (info.format_type == asm_fprintf_format_type)
3023 init_dynamic_asm_fprintf_info ();
3024 /* If this is format __gcc_gfc__, we have to initialize GCC's
3025 notion of 'locus' at runtime for %L. */
3026 else if (info.format_type == gcc_gfc_format_type)
3027 init_dynamic_gfc_info ();
3028 /* If this is one of the diagnostic attributes, then we have to
3029 initialize 'location_t' and 'tree' at runtime. */
3030 else if (info.format_type == gcc_diag_format_type
3031 || info.format_type == gcc_tdiag_format_type
3032 || info.format_type == gcc_cdiag_format_type
3033 || info.format_type == gcc_cxxdiag_format_type)
3034 init_dynamic_diag_info ();
3035 else
3036 gcc_unreachable ();
3039 return NULL_TREE;