2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / c-family / c-format.c
blob76dc56735df54f7cc0ec3398e199062dc798e279
1 /* Check calls to formatted I/O functions (-Wformat).
2 Copyright (C) 1992-2015 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 "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "flags.h"
29 #include "c-common.h"
30 #include "c-objc.h"
31 #include "intl.h"
32 #include "diagnostic-core.h"
33 #include "langhooks.h"
34 #include "c-format.h"
35 #include "alloc-pool.h"
36 #include "c-target.h"
38 /* Handle attributes associated with format checking. */
40 /* This must be in the same order as format_types, except for
41 format_type_error. Target-specific format types do not have
42 matching enum values. */
43 enum format_type { printf_format_type, asm_fprintf_format_type,
44 gcc_diag_format_type, gcc_tdiag_format_type,
45 gcc_cdiag_format_type,
46 gcc_cxxdiag_format_type, gcc_gfc_format_type,
47 gcc_objc_string_format_type,
48 format_type_error = -1};
50 typedef struct function_format_info
52 int format_type; /* type of format (printf, scanf, etc.) */
53 unsigned HOST_WIDE_INT format_num; /* number of format argument */
54 unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
55 } function_format_info;
57 static bool decode_format_attr (tree, function_format_info *, int);
58 static int decode_format_type (const char *);
60 static bool check_format_string (tree argument,
61 unsigned HOST_WIDE_INT format_num,
62 int flags, bool *no_add_attrs,
63 int expected_format_type);
64 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
65 int validated_p);
66 static const char *convert_format_name_to_system_name (const char *attr_name);
67 static bool cmp_attribs (const char *tattr_name, const char *attr_name);
69 static int first_target_format_type;
70 static const char *format_name (int format_num);
71 static int format_flags (int format_num);
73 /* Given a string S of length LINE_WIDTH, find the visual column
74 corresponding to OFFSET bytes. */
76 static unsigned int
77 location_column_from_byte_offset (const char *s, int line_width,
78 unsigned int offset)
80 const char * c = s;
81 if (*c != '"')
82 return 0;
84 c++, offset--;
85 while (offset > 0)
87 if (c - s >= line_width)
88 return 0;
90 switch (*c)
92 case '\\':
93 c++;
94 if (c - s >= line_width)
95 return 0;
96 switch (*c)
98 case '\\': case '\'': case '"': case '?':
99 case '(': case '{': case '[': case '%':
100 case 'a': case 'b': case 'f': case 'n':
101 case 'r': case 't': case 'v':
102 case 'e': case 'E':
103 c++, offset--;
104 break;
106 default:
107 return 0;
109 break;
111 case '"':
112 /* We found the end of the string too early. */
113 return 0;
115 default:
116 c++, offset--;
117 break;
120 return c - s;
123 /* Return a location that encodes the same location as LOC but shifted
124 by OFFSET bytes. */
126 static location_t
127 location_from_offset (location_t loc, int offset)
129 gcc_checking_assert (offset >= 0);
130 if (linemap_location_from_macro_expansion_p (line_table, loc)
131 || offset < 0)
132 return loc;
134 expanded_location s = expand_location_to_spelling_point (loc);
135 int line_width;
136 const char *line = location_get_source_line (s, &line_width);
137 if (line == NULL)
138 return loc;
139 line += s.column - 1 ;
140 line_width -= s.column - 1;
141 unsigned int column =
142 location_column_from_byte_offset (line, line_width, (unsigned) offset);
144 return linemap_position_for_loc_and_offset (line_table, loc, column);
147 /* Check that we have a pointer to a string suitable for use as a format.
148 The default is to check for a char type.
149 For objective-c dialects, this is extended to include references to string
150 objects validated by objc_string_ref_type_p ().
151 Targets may also provide a string object type that can be used within c and
152 c++ and shared with their respective objective-c dialects. In this case the
153 reference to a format string is checked for validity via a hook.
155 The function returns true if strref points to any string type valid for the
156 language dialect and target. */
158 static bool
159 valid_stringptr_type_p (tree strref)
161 return (strref != NULL
162 && TREE_CODE (strref) == POINTER_TYPE
163 && (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
164 || objc_string_ref_type_p (strref)
165 || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
168 /* Handle a "format_arg" attribute; arguments as in
169 struct attribute_spec.handler. */
170 tree
171 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
172 tree args, int flags, bool *no_add_attrs)
174 tree type = *node;
175 tree format_num_expr = TREE_VALUE (args);
176 unsigned HOST_WIDE_INT format_num = 0;
178 if (!get_constant (format_num_expr, &format_num, 0))
180 error ("format string has invalid operand number");
181 *no_add_attrs = true;
182 return NULL_TREE;
185 if (prototype_p (type))
187 /* The format arg can be any string reference valid for the language and
188 target. We cannot be more specific in this case. */
189 if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
190 return NULL_TREE;
193 if (!valid_stringptr_type_p (TREE_TYPE (type)))
195 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
196 error ("function does not return string type");
197 *no_add_attrs = true;
198 return NULL_TREE;
201 return NULL_TREE;
204 /* Verify that the format_num argument is actually a string reference suitable,
205 for the language dialect and target (in case the format attribute is in
206 error). When we know the specific reference type expected, this is also
207 checked. */
208 static bool
209 check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
210 int flags, bool *no_add_attrs, int expected_format_type)
212 unsigned HOST_WIDE_INT i;
213 bool is_objc_sref, is_target_sref, is_char_ref;
214 tree ref;
215 int fmt_flags;
216 function_args_iterator iter;
218 i = 1;
219 FOREACH_FUNCTION_ARGS (fntype, ref, iter)
221 if (i == format_num)
222 break;
223 i++;
226 if (!ref
227 || !valid_stringptr_type_p (ref))
229 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
230 error ("format string argument is not a string type");
231 *no_add_attrs = true;
232 return false;
235 /* We only know that we want a suitable string reference. */
236 if (expected_format_type < 0)
237 return true;
239 /* Now check that the arg matches the expected type. */
240 is_char_ref =
241 (TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
243 fmt_flags = format_flags (expected_format_type);
244 is_objc_sref = is_target_sref = false;
245 if (!is_char_ref)
246 is_objc_sref = objc_string_ref_type_p (ref);
248 if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
250 if (is_char_ref)
251 return true; /* OK, we expected a char and found one. */
252 else
254 /* We expected a char but found an extended string type. */
255 if (is_objc_sref)
256 error ("found a %<%s%> reference but the format argument should"
257 " be a string", format_name (gcc_objc_string_format_type));
258 else
259 error ("found a %qT but the format argument should be a string",
260 ref);
261 *no_add_attrs = true;
262 return false;
266 /* We expect a string object type as the format arg. */
267 if (is_char_ref)
269 error ("format argument should be a %<%s%> reference but"
270 " a string was found", format_name (expected_format_type));
271 *no_add_attrs = true;
272 return false;
275 /* We will assert that objective-c will support either its own string type
276 or the target-supplied variant. */
277 if (!is_objc_sref)
278 is_target_sref = (*targetcm.string_object_ref_type_p) ((const_tree) ref);
280 if (expected_format_type == (int) gcc_objc_string_format_type
281 && (is_objc_sref || is_target_sref))
282 return true;
284 /* We will allow a target string ref to match only itself. */
285 if (first_target_format_type
286 && expected_format_type >= first_target_format_type
287 && is_target_sref)
288 return true;
289 else
291 error ("format argument should be a %<%s%> reference",
292 format_name (expected_format_type));
293 *no_add_attrs = true;
294 return false;
297 gcc_unreachable ();
300 /* Verify EXPR is a constant, and store its value.
301 If validated_p is true there should be no errors.
302 Returns true on success, false otherwise. */
303 static bool
304 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
306 if (!tree_fits_uhwi_p (expr))
308 gcc_assert (!validated_p);
309 return false;
312 *value = TREE_INT_CST_LOW (expr);
314 return true;
317 /* Decode the arguments to a "format" attribute into a
318 function_format_info structure. It is already known that the list
319 is of the right length. If VALIDATED_P is true, then these
320 attributes have already been validated and must not be erroneous;
321 if false, it will give an error message. Returns true if the
322 attributes are successfully decoded, false otherwise. */
324 static bool
325 decode_format_attr (tree args, function_format_info *info, int validated_p)
327 tree format_type_id = TREE_VALUE (args);
328 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
329 tree first_arg_num_expr
330 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
332 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
334 gcc_assert (!validated_p);
335 error ("unrecognized format specifier");
336 return false;
338 else
340 const char *p = IDENTIFIER_POINTER (format_type_id);
342 p = convert_format_name_to_system_name (p);
344 info->format_type = decode_format_type (p);
346 if (!c_dialect_objc ()
347 && info->format_type == gcc_objc_string_format_type)
349 gcc_assert (!validated_p);
350 warning (OPT_Wformat_, "%qE is only allowed in Objective-C dialects",
351 format_type_id);
352 info->format_type = format_type_error;
353 return false;
356 if (info->format_type == format_type_error)
358 gcc_assert (!validated_p);
359 warning (OPT_Wformat_, "%qE is an unrecognized format function type",
360 format_type_id);
361 return false;
365 if (!get_constant (format_num_expr, &info->format_num, validated_p))
367 error ("format string has invalid operand number");
368 return false;
371 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
373 error ("%<...%> has invalid operand number");
374 return false;
377 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
379 gcc_assert (!validated_p);
380 error ("format string argument follows the args to be formatted");
381 return false;
384 return true;
387 /* Check a call to a format function against a parameter list. */
389 /* The C standard version C++ is treated as equivalent to
390 or inheriting from, for the purpose of format features supported. */
391 #define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
392 /* The C standard version we are checking formats against when pedantic. */
393 #define C_STD_VER ((int) (c_dialect_cxx () \
394 ? CPLUSPLUS_STD_VER \
395 : (flag_isoc99 \
396 ? STD_C99 \
397 : (flag_isoc94 ? STD_C94 : STD_C89))))
398 /* The name to give to the standard version we are warning about when
399 pedantic. FEATURE_VER is the version in which the feature warned out
400 appeared, which is higher than C_STD_VER. */
401 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
402 ? (cxx_dialect < cxx11 ? "ISO C++98" \
403 : "ISO C++11") \
404 : ((FEATURE_VER) == STD_EXT \
405 ? "ISO C" \
406 : "ISO C90"))
407 /* Adjust a C standard version, which may be STD_C9L, to account for
408 -Wno-long-long. Returns other standard versions unchanged. */
409 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
410 ? (warn_long_long ? STD_C99 : STD_C89) \
411 : (VER)))
413 /* Enum describing the kind of specifiers present in the format and
414 requiring an argument. */
415 enum format_specifier_kind {
416 CF_KIND_FORMAT,
417 CF_KIND_FIELD_WIDTH,
418 CF_KIND_FIELD_PRECISION
421 static const char *kind_descriptions[] = {
422 N_("format"),
423 N_("field width specifier"),
424 N_("field precision specifier")
427 /* Structure describing details of a type expected in format checking,
428 and the type to check against it. */
429 typedef struct format_wanted_type
431 /* The type wanted. */
432 tree wanted_type;
433 /* The name of this type to use in diagnostics. */
434 const char *wanted_type_name;
435 /* Should be type checked just for scalar width identity. */
436 int scalar_identity_flag;
437 /* The level of indirection through pointers at which this type occurs. */
438 int pointer_count;
439 /* Whether, when pointer_count is 1, to allow any character type when
440 pedantic, rather than just the character or void type specified. */
441 int char_lenient_flag;
442 /* Whether the argument, dereferenced once, is written into and so the
443 argument must not be a pointer to a const-qualified type. */
444 int writing_in_flag;
445 /* Whether the argument, dereferenced once, is read from and so
446 must not be a NULL pointer. */
447 int reading_from_flag;
448 /* The kind of specifier that this type is used for. */
449 enum format_specifier_kind kind;
450 /* The starting character of the specifier. This never includes the
451 initial percent sign. */
452 const char *format_start;
453 /* The length of the specifier. */
454 int format_length;
455 /* The actual parameter to check against the wanted type. */
456 tree param;
457 /* The argument number of that parameter. */
458 int arg_num;
459 /* The offset location of this argument with respect to the format
460 string location. */
461 unsigned int offset_loc;
462 /* The next type to check for this format conversion, or NULL if none. */
463 struct format_wanted_type *next;
464 } format_wanted_type;
466 /* Convenience macro for format_length_info meaning unused. */
467 #define NO_FMT NULL, FMT_LEN_none, STD_C89
469 static const format_length_info printf_length_specs[] =
471 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
472 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
473 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
474 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
475 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
476 { "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
477 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
478 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
479 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
480 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
481 { NO_FMT, NO_FMT, 0 }
484 /* Length specifiers valid for asm_fprintf. */
485 static const format_length_info asm_fprintf_length_specs[] =
487 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
488 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
489 { NO_FMT, NO_FMT, 0 }
492 /* Length specifiers valid for GCC diagnostics. */
493 static const format_length_info gcc_diag_length_specs[] =
495 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
496 { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
497 { NO_FMT, NO_FMT, 0 }
500 /* The custom diagnostics all accept the same length specifiers. */
501 #define gcc_tdiag_length_specs gcc_diag_length_specs
502 #define gcc_cdiag_length_specs gcc_diag_length_specs
503 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
505 /* This differs from printf_length_specs only in that "Z" is not accepted. */
506 static const format_length_info scanf_length_specs[] =
508 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
509 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
510 { "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
511 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
512 { "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
513 { "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
514 { "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
515 { "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
516 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
517 { NO_FMT, NO_FMT, 0 }
521 /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings
522 make no sense for a format type not part of any C standard version. */
523 static const format_length_info strfmon_length_specs[] =
525 /* A GNU extension. */
526 { "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
527 { NO_FMT, NO_FMT, 0 }
531 /* For now, the Fortran front-end routines only use l as length modifier. */
532 static const format_length_info gcc_gfc_length_specs[] =
534 { "l", FMT_LEN_l, STD_C89, NO_FMT, 0 },
535 { NO_FMT, NO_FMT, 0 }
539 static const format_flag_spec printf_flag_specs[] =
541 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
542 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
543 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
544 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
545 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
546 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
547 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
548 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
549 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
550 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
551 { 0, 0, 0, NULL, NULL, STD_C89 }
555 static const format_flag_pair printf_flag_pairs[] =
557 { ' ', '+', 1, 0 },
558 { '0', '-', 1, 0 },
559 { '0', 'p', 1, 'i' },
560 { 0, 0, 0, 0 }
563 static const format_flag_spec asm_fprintf_flag_specs[] =
565 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
566 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
567 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
568 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
569 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
570 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
571 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
572 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
573 { 0, 0, 0, NULL, NULL, STD_C89 }
576 static const format_flag_pair asm_fprintf_flag_pairs[] =
578 { ' ', '+', 1, 0 },
579 { '0', '-', 1, 0 },
580 { '0', 'p', 1, 'i' },
581 { 0, 0, 0, 0 }
584 static const format_flag_pair gcc_diag_flag_pairs[] =
586 { 0, 0, 0, 0 }
589 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
590 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
591 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
592 #define gcc_gfc_flag_pairs gcc_diag_flag_pairs
594 static const format_flag_spec gcc_diag_flag_specs[] =
596 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
597 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
598 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
599 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
600 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
601 { 0, 0, 0, NULL, NULL, STD_C89 }
604 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
605 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
606 #define gcc_cxxdiag_flag_specs gcc_diag_flag_specs
607 #define gcc_gfc_flag_specs gcc_diag_flag_specs
609 static const format_flag_spec scanf_flag_specs[] =
611 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
612 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
613 { 'm', 0, 0, N_("'m' flag"), N_("the 'm' scanf flag"), STD_EXT },
614 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
615 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
616 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
617 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
618 { 0, 0, 0, NULL, NULL, STD_C89 }
622 static const format_flag_pair scanf_flag_pairs[] =
624 { '*', 'L', 0, 0 },
625 { 'a', 'm', 0, 0 },
626 { 0, 0, 0, 0 }
630 static const format_flag_spec strftime_flag_specs[] =
632 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
633 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
634 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
635 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
636 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
637 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
638 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
639 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
640 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
641 { 0, 0, 0, NULL, NULL, STD_C89 }
645 static const format_flag_pair strftime_flag_pairs[] =
647 { 'E', 'O', 0, 0 },
648 { '_', '-', 0, 0 },
649 { '_', '0', 0, 0 },
650 { '-', '0', 0, 0 },
651 { '^', '#', 0, 0 },
652 { 0, 0, 0, 0 }
656 static const format_flag_spec strfmon_flag_specs[] =
658 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
659 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
660 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
661 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
662 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
663 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
664 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
665 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
666 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
667 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
668 { 0, 0, 0, NULL, NULL, STD_C89 }
671 static const format_flag_pair strfmon_flag_pairs[] =
673 { '+', '(', 0, 0 },
674 { 0, 0, 0, 0 }
678 static const format_char_info print_char_table[] =
680 /* C89 conversion specifiers. */
681 { "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 },
682 { "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 },
683 { "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 },
684 { "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 },
685 { "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 },
686 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
687 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
688 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
689 { "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 },
690 /* C99 conversion specifiers. */
691 { "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 },
692 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
693 /* X/Open conversion specifiers. */
694 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
695 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
696 /* GNU conversion specifiers. */
697 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
698 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
701 static const format_char_info asm_fprintf_char_table[] =
703 /* C89 conversion specifiers. */
704 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
705 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
706 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
707 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
708 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
710 /* asm_fprintf conversion specifiers. */
711 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
712 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
713 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
714 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
715 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
716 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
717 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
718 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
721 static const format_char_info gcc_diag_char_table[] =
723 /* C89 conversion specifiers. */
724 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
725 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
726 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
727 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
728 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
729 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
731 /* Custom conversion specifiers. */
733 /* These will require a "tree" at runtime. */
734 { "K", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
736 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
737 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
738 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
739 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
742 static const format_char_info gcc_tdiag_char_table[] =
744 /* C89 conversion specifiers. */
745 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
746 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
747 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
748 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
749 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
750 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
752 /* Custom conversion specifiers. */
754 /* These will require a "tree" at runtime. */
755 { "DFKTEV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
757 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
759 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
760 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
761 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
762 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
765 static const format_char_info gcc_cdiag_char_table[] =
767 /* C89 conversion specifiers. */
768 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
769 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
770 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
771 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
772 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
773 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
775 /* Custom conversion specifiers. */
777 /* These will require a "tree" at runtime. */
778 { "DEFKTV", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
780 { "v", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
782 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
783 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
784 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
785 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
788 static const format_char_info gcc_cxxdiag_char_table[] =
790 /* C89 conversion specifiers. */
791 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
792 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
793 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
794 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
795 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
796 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
798 /* Custom conversion specifiers. */
800 /* These will require a "tree" at runtime. */
801 { "ADEFKSTVX",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
803 { "v", 0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q#", "", NULL },
805 /* These accept either an 'int' or an 'enum tree_code' (which is handled as an 'int'.) */
806 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
808 { "r", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
809 { "<>'R",0, STD_C89, NOARGUMENTS, "", "", NULL },
810 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
811 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
814 static const format_char_info gcc_gfc_char_table[] =
816 /* C89 conversion specifiers. */
817 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
818 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
819 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
820 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "cR", NULL },
822 /* gfc conversion specifiers. */
824 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
826 /* This will require a "locus" at runtime. */
827 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
829 /* These will require nothing. */
830 { "<>",0, STD_C89, NOARGUMENTS, "", "", NULL },
831 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
834 static const format_char_info scan_char_table[] =
836 /* C89 conversion specifiers. */
837 { "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 },
838 { "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 },
839 { "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 },
840 { "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 },
841 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "cW", NULL },
842 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW", NULL },
843 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "cW[", NULL },
844 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
845 { "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 },
846 /* C99 conversion specifiers. */
847 { "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 },
848 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
849 /* X/Open conversion specifiers. */
850 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*mw", "W", NULL },
851 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*amw", "W", NULL },
852 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
855 static const format_char_info time_char_table[] =
857 /* C89 conversion specifiers. */
858 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
859 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
860 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
861 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
862 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
863 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
864 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
865 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
866 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
867 /* C99 conversion specifiers. */
868 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
869 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
870 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
871 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
872 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
873 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
874 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
875 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
876 /* GNU conversion specifiers. */
877 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
878 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
879 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
882 static const format_char_info monetary_char_table[] =
884 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
885 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
888 /* This must be in the same order as enum format_type. */
889 static const format_kind_info format_types_orig[] =
891 { "gnu_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
892 printf_flag_specs, printf_flag_pairs,
893 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
894 'w', 0, 'p', 0, 'L', 0,
895 &integer_type_node, &integer_type_node
897 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
898 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
899 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
900 'w', 0, 'p', 0, 'L', 0,
901 NULL, NULL
903 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+#", NULL,
904 gcc_diag_flag_specs, gcc_diag_flag_pairs,
905 FMT_FLAG_ARG_CONVERT,
906 0, 0, 'p', 0, 'L', 0,
907 NULL, &integer_type_node
909 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+#", NULL,
910 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
911 FMT_FLAG_ARG_CONVERT,
912 0, 0, 'p', 0, 'L', 0,
913 NULL, &integer_type_node
915 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+#", NULL,
916 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
917 FMT_FLAG_ARG_CONVERT,
918 0, 0, 'p', 0, 'L', 0,
919 NULL, &integer_type_node
921 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
922 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
923 FMT_FLAG_ARG_CONVERT,
924 0, 0, 'p', 0, 'L', 0,
925 NULL, &integer_type_node
927 { "gcc_gfc", gcc_gfc_length_specs, gcc_gfc_char_table, "q+#", NULL,
928 gcc_gfc_flag_specs, gcc_gfc_flag_pairs,
929 FMT_FLAG_ARG_CONVERT,
930 0, 0, 0, 0, 0, 0,
931 NULL, NULL
933 { "NSString", NULL, NULL, NULL, NULL,
934 NULL, NULL,
935 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
936 NULL, NULL
938 { "gnu_scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
939 scanf_flag_specs, scanf_flag_pairs,
940 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
941 'w', 0, 0, '*', 'L', 'm',
942 NULL, NULL
944 { "gnu_strftime", NULL, time_char_table, "_-0^#", "EO",
945 strftime_flag_specs, strftime_flag_pairs,
946 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, 0,
947 NULL, NULL
949 { "gnu_strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
950 strfmon_flag_specs, strfmon_flag_pairs,
951 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', 0,
952 NULL, NULL
956 /* This layer of indirection allows GCC to reassign format_types with
957 new data if necessary, while still allowing the original data to be
958 const. */
959 static const format_kind_info *format_types = format_types_orig;
960 /* We can modify this one. We also add target-specific format types
961 to the end of the array. */
962 static format_kind_info *dynamic_format_types;
964 static int n_format_types = ARRAY_SIZE (format_types_orig);
966 /* Structure detailing the results of checking a format function call
967 where the format expression may be a conditional expression with
968 many leaves resulting from nested conditional expressions. */
969 typedef struct
971 /* Number of leaves of the format argument that could not be checked
972 as they were not string literals. */
973 int number_non_literal;
974 /* Number of leaves of the format argument that were null pointers or
975 string literals, but had extra format arguments. */
976 int number_extra_args;
977 location_t extra_arg_loc;
978 /* Number of leaves of the format argument that were null pointers or
979 string literals, but had extra format arguments and used $ operand
980 numbers. */
981 int number_dollar_extra_args;
982 /* Number of leaves of the format argument that were wide string
983 literals. */
984 int number_wide;
985 /* Number of leaves of the format argument that were empty strings. */
986 int number_empty;
987 /* Number of leaves of the format argument that were unterminated
988 strings. */
989 int number_unterminated;
990 /* Number of leaves of the format argument that were not counted above. */
991 int number_other;
992 /* Location of the format string. */
993 location_t format_string_loc;
994 } format_check_results;
996 typedef struct
998 format_check_results *res;
999 function_format_info *info;
1000 tree params;
1001 } format_check_context;
1003 /* Return the format name (as specified in the original table) for the format
1004 type indicated by format_num. */
1005 static const char *
1006 format_name (int format_num)
1008 if (format_num >= 0 && format_num < n_format_types)
1009 return format_types[format_num].name;
1010 gcc_unreachable ();
1013 /* Return the format flags (as specified in the original table) for the format
1014 type indicated by format_num. */
1015 static int
1016 format_flags (int format_num)
1018 if (format_num >= 0 && format_num < n_format_types)
1019 return format_types[format_num].flags;
1020 gcc_unreachable ();
1023 static void check_format_info (function_format_info *, tree);
1024 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
1025 static void check_format_info_main (format_check_results *,
1026 function_format_info *,
1027 const char *, int, tree,
1028 unsigned HOST_WIDE_INT,
1029 pool_allocator<format_wanted_type> &);
1031 static void init_dollar_format_checking (int, tree);
1032 static int maybe_read_dollar_number (const char **, int,
1033 tree, tree *, const format_kind_info *);
1034 static bool avoid_dollar_number (const char *);
1035 static void finish_dollar_format_checking (format_check_results *, int);
1037 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
1038 int, const char *);
1040 static void check_format_types (location_t, format_wanted_type *);
1041 static void format_type_warning (location_t, format_wanted_type *, tree, tree);
1043 /* Decode a format type from a string, returning the type, or
1044 format_type_error if not valid, in which case the caller should print an
1045 error message. */
1046 static int
1047 decode_format_type (const char *s)
1049 int i;
1050 int slen;
1052 s = convert_format_name_to_system_name (s);
1053 slen = strlen (s);
1054 for (i = 0; i < n_format_types; i++)
1056 int alen;
1057 if (!strcmp (s, format_types[i].name))
1058 return i;
1059 alen = strlen (format_types[i].name);
1060 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
1061 && s[slen - 1] == '_' && s[slen - 2] == '_'
1062 && !strncmp (s + 2, format_types[i].name, alen))
1063 return i;
1065 return format_type_error;
1069 /* Check the argument list of a call to printf, scanf, etc.
1070 ATTRS are the attributes on the function type. There are NARGS argument
1071 values in the array ARGARRAY.
1072 Also, if -Wsuggest-attribute=format,
1073 warn for calls to vprintf or vscanf in functions with no such format
1074 attribute themselves. */
1076 void
1077 check_function_format (tree attrs, int nargs, tree *argarray)
1079 tree a;
1081 /* See if this function has any format attributes. */
1082 for (a = attrs; a; a = TREE_CHAIN (a))
1084 if (is_attribute_p ("format", TREE_PURPOSE (a)))
1086 /* Yup; check it. */
1087 function_format_info info;
1088 decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
1089 if (warn_format)
1091 /* FIXME: Rewrite all the internal functions in this file
1092 to use the ARGARRAY directly instead of constructing this
1093 temporary list. */
1094 tree params = NULL_TREE;
1095 int i;
1096 for (i = nargs - 1; i >= 0; i--)
1097 params = tree_cons (NULL_TREE, argarray[i], params);
1098 check_format_info (&info, params);
1100 if (warn_suggest_attribute_format && info.first_arg_num == 0
1101 && (format_types[info.format_type].flags
1102 & (int) FMT_FLAG_ARG_CONVERT))
1104 tree c;
1105 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
1107 c = TREE_CHAIN (c))
1108 if (is_attribute_p ("format", TREE_PURPOSE (c))
1109 && (decode_format_type (IDENTIFIER_POINTER
1110 (TREE_VALUE (TREE_VALUE (c))))
1111 == info.format_type))
1112 break;
1113 if (c == NULL_TREE)
1115 /* Check if the current function has a parameter to which
1116 the format attribute could be attached; if not, it
1117 can't be a candidate for a format attribute, despite
1118 the vprintf-like or vscanf-like call. */
1119 tree args;
1120 for (args = DECL_ARGUMENTS (current_function_decl);
1121 args != 0;
1122 args = DECL_CHAIN (args))
1124 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
1125 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
1126 == char_type_node))
1127 break;
1129 if (args != 0)
1130 warning (OPT_Wsuggest_attribute_format, "function might "
1131 "be possible candidate for %qs format attribute",
1132 format_types[info.format_type].name);
1140 /* Variables used by the checking of $ operand number formats. */
1141 static char *dollar_arguments_used = NULL;
1142 static char *dollar_arguments_pointer_p = NULL;
1143 static int dollar_arguments_alloc = 0;
1144 static int dollar_arguments_count;
1145 static int dollar_first_arg_num;
1146 static int dollar_max_arg_used;
1147 static int dollar_format_warned;
1149 /* Initialize the checking for a format string that may contain $
1150 parameter number specifications; we will need to keep track of whether
1151 each parameter has been used. FIRST_ARG_NUM is the number of the first
1152 argument that is a parameter to the format, or 0 for a vprintf-style
1153 function; PARAMS is the list of arguments starting at this argument. */
1155 static void
1156 init_dollar_format_checking (int first_arg_num, tree params)
1158 tree oparams = params;
1160 dollar_first_arg_num = first_arg_num;
1161 dollar_arguments_count = 0;
1162 dollar_max_arg_used = 0;
1163 dollar_format_warned = 0;
1164 if (first_arg_num > 0)
1166 while (params)
1168 dollar_arguments_count++;
1169 params = TREE_CHAIN (params);
1172 if (dollar_arguments_alloc < dollar_arguments_count)
1174 free (dollar_arguments_used);
1175 free (dollar_arguments_pointer_p);
1176 dollar_arguments_alloc = dollar_arguments_count;
1177 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
1178 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
1180 if (dollar_arguments_alloc)
1182 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1183 if (first_arg_num > 0)
1185 int i = 0;
1186 params = oparams;
1187 while (params)
1189 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
1190 == POINTER_TYPE);
1191 params = TREE_CHAIN (params);
1192 i++;
1199 /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1200 is set, it is an error if one is not found; otherwise, it is OK. If
1201 such a number is found, check whether it is within range and mark that
1202 numbered operand as being used for later checking. Returns the operand
1203 number if found and within range, zero if no such number was found and
1204 this is OK, or -1 on error. PARAMS points to the first operand of the
1205 format; PARAM_PTR is made to point to the parameter referred to. If
1206 a $ format is found, *FORMAT is updated to point just after it. */
1208 static int
1209 maybe_read_dollar_number (const char **format,
1210 int dollar_needed, tree params, tree *param_ptr,
1211 const format_kind_info *fki)
1213 int argnum;
1214 int overflow_flag;
1215 const char *fcp = *format;
1216 if (!ISDIGIT (*fcp))
1218 if (dollar_needed)
1220 warning (OPT_Wformat_, "missing $ operand number in format");
1221 return -1;
1223 else
1224 return 0;
1226 argnum = 0;
1227 overflow_flag = 0;
1228 while (ISDIGIT (*fcp))
1230 int nargnum;
1231 nargnum = 10 * argnum + (*fcp - '0');
1232 if (nargnum < 0 || nargnum / 10 != argnum)
1233 overflow_flag = 1;
1234 argnum = nargnum;
1235 fcp++;
1237 if (*fcp != '$')
1239 if (dollar_needed)
1241 warning (OPT_Wformat_, "missing $ operand number in format");
1242 return -1;
1244 else
1245 return 0;
1247 *format = fcp + 1;
1248 if (pedantic && !dollar_format_warned)
1250 warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
1251 C_STD_NAME (STD_EXT));
1252 dollar_format_warned = 1;
1254 if (overflow_flag || argnum == 0
1255 || (dollar_first_arg_num && argnum > dollar_arguments_count))
1257 warning (OPT_Wformat_, "operand number out of range in format");
1258 return -1;
1260 if (argnum > dollar_max_arg_used)
1261 dollar_max_arg_used = argnum;
1262 /* For vprintf-style functions we may need to allocate more memory to
1263 track which arguments are used. */
1264 while (dollar_arguments_alloc < dollar_max_arg_used)
1266 int nalloc;
1267 nalloc = 2 * dollar_arguments_alloc + 16;
1268 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
1269 nalloc);
1270 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
1271 nalloc);
1272 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
1273 nalloc - dollar_arguments_alloc);
1274 dollar_arguments_alloc = nalloc;
1276 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
1277 && dollar_arguments_used[argnum - 1] == 1)
1279 dollar_arguments_used[argnum - 1] = 2;
1280 warning (OPT_Wformat_, "format argument %d used more than once in %s format",
1281 argnum, fki->name);
1283 else
1284 dollar_arguments_used[argnum - 1] = 1;
1285 if (dollar_first_arg_num)
1287 int i;
1288 *param_ptr = params;
1289 for (i = 1; i < argnum && *param_ptr != 0; i++)
1290 *param_ptr = TREE_CHAIN (*param_ptr);
1292 /* This case shouldn't be caught here. */
1293 gcc_assert (*param_ptr);
1295 else
1296 *param_ptr = 0;
1297 return argnum;
1300 /* Ensure that FORMAT does not start with a decimal number followed by
1301 a $; give a diagnostic and return true if it does, false otherwise. */
1303 static bool
1304 avoid_dollar_number (const char *format)
1306 if (!ISDIGIT (*format))
1307 return false;
1308 while (ISDIGIT (*format))
1309 format++;
1310 if (*format == '$')
1312 warning (OPT_Wformat_, "$ operand number used after format without operand number");
1313 return true;
1315 return false;
1319 /* Finish the checking for a format string that used $ operand number formats
1320 instead of non-$ formats. We check for unused operands before used ones
1321 (a serious error, since the implementation of the format function
1322 can't know what types to pass to va_arg to find the later arguments).
1323 and for unused operands at the end of the format (if we know how many
1324 arguments the format had, so not for vprintf). If there were operand
1325 numbers out of range on a non-vprintf-style format, we won't have reached
1326 here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
1327 pointers. */
1329 static void
1330 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
1332 int i;
1333 bool found_pointer_gap = false;
1334 for (i = 0; i < dollar_max_arg_used; i++)
1336 if (!dollar_arguments_used[i])
1338 if (pointer_gap_ok && (dollar_first_arg_num == 0
1339 || dollar_arguments_pointer_p[i]))
1340 found_pointer_gap = true;
1341 else
1342 warning_at (res->format_string_loc, OPT_Wformat_,
1343 "format argument %d unused before used argument %d in $-style format",
1344 i + 1, dollar_max_arg_used);
1347 if (found_pointer_gap
1348 || (dollar_first_arg_num
1349 && dollar_max_arg_used < dollar_arguments_count))
1351 res->number_other--;
1352 res->number_dollar_extra_args++;
1357 /* Retrieve the specification for a format flag. SPEC contains the
1358 specifications for format flags for the applicable kind of format.
1359 FLAG is the flag in question. If PREDICATES is NULL, the basic
1360 spec for that flag must be retrieved and must exist. If
1361 PREDICATES is not NULL, it is a string listing possible predicates
1362 for the spec entry; if an entry predicated on any of these is
1363 found, it is returned, otherwise NULL is returned. */
1365 static const format_flag_spec *
1366 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
1368 int i;
1369 for (i = 0; spec[i].flag_char != 0; i++)
1371 if (spec[i].flag_char != flag)
1372 continue;
1373 if (predicates != NULL)
1375 if (spec[i].predicate != 0
1376 && strchr (predicates, spec[i].predicate) != 0)
1377 return &spec[i];
1379 else if (spec[i].predicate == 0)
1380 return &spec[i];
1382 gcc_assert (predicates);
1383 return NULL;
1387 /* Check the argument list of a call to printf, scanf, etc.
1388 INFO points to the function_format_info structure.
1389 PARAMS is the list of argument values. */
1391 static void
1392 check_format_info (function_format_info *info, tree params)
1394 format_check_context format_ctx;
1395 unsigned HOST_WIDE_INT arg_num;
1396 tree format_tree;
1397 format_check_results res;
1398 /* Skip to format argument. If the argument isn't available, there's
1399 no work for us to do; prototype checking will catch the problem. */
1400 for (arg_num = 1; ; ++arg_num)
1402 if (params == 0)
1403 return;
1404 if (arg_num == info->format_num)
1405 break;
1406 params = TREE_CHAIN (params);
1408 format_tree = TREE_VALUE (params);
1409 params = TREE_CHAIN (params);
1410 if (format_tree == 0)
1411 return;
1413 res.number_non_literal = 0;
1414 res.number_extra_args = 0;
1415 res.extra_arg_loc = UNKNOWN_LOCATION;
1416 res.number_dollar_extra_args = 0;
1417 res.number_wide = 0;
1418 res.number_empty = 0;
1419 res.number_unterminated = 0;
1420 res.number_other = 0;
1421 res.format_string_loc = input_location;
1423 format_ctx.res = &res;
1424 format_ctx.info = info;
1425 format_ctx.params = params;
1427 check_function_arguments_recurse (check_format_arg, &format_ctx,
1428 format_tree, arg_num);
1430 location_t loc = format_ctx.res->format_string_loc;
1432 if (res.number_non_literal > 0)
1434 /* Functions taking a va_list normally pass a non-literal format
1435 string. These functions typically are declared with
1436 first_arg_num == 0, so avoid warning in those cases. */
1437 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
1439 /* For strftime-like formats, warn for not checking the format
1440 string; but there are no arguments to check. */
1441 warning_at (loc, OPT_Wformat_nonliteral,
1442 "format not a string literal, format string not checked");
1444 else if (info->first_arg_num != 0)
1446 /* If there are no arguments for the format at all, we may have
1447 printf (foo) which is likely to be a security hole. */
1448 while (arg_num + 1 < info->first_arg_num)
1450 if (params == 0)
1451 break;
1452 params = TREE_CHAIN (params);
1453 ++arg_num;
1455 if (params == 0 && warn_format_security)
1456 warning_at (loc, OPT_Wformat_security,
1457 "format not a string literal and no format arguments");
1458 else if (params == 0 && warn_format_nonliteral)
1459 warning_at (loc, OPT_Wformat_nonliteral,
1460 "format not a string literal and no format arguments");
1461 else
1462 warning_at (loc, OPT_Wformat_nonliteral,
1463 "format not a string literal, argument types not checked");
1467 /* If there were extra arguments to the format, normally warn. However,
1468 the standard does say extra arguments are ignored, so in the specific
1469 case where we have multiple leaves (conditional expressions or
1470 ngettext) allow extra arguments if at least one leaf didn't have extra
1471 arguments, but was otherwise OK (either non-literal or checked OK).
1472 If the format is an empty string, this should be counted similarly to the
1473 case of extra format arguments. */
1474 if (res.number_extra_args > 0 && res.number_non_literal == 0
1475 && res.number_other == 0)
1477 if (res.extra_arg_loc == UNKNOWN_LOCATION)
1478 res.extra_arg_loc = loc;
1479 warning_at (res.extra_arg_loc, OPT_Wformat_extra_args,
1480 "too many arguments for format");
1482 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
1483 && res.number_other == 0)
1484 warning_at (loc, OPT_Wformat_extra_args, "unused arguments in $-style format");
1485 if (res.number_empty > 0 && res.number_non_literal == 0
1486 && res.number_other == 0)
1487 warning_at (loc, OPT_Wformat_zero_length, "zero-length %s format string",
1488 format_types[info->format_type].name);
1490 if (res.number_wide > 0)
1491 warning_at (loc, OPT_Wformat_, "format is a wide character string");
1493 if (res.number_unterminated > 0)
1494 warning_at (loc, OPT_Wformat_, "unterminated format string");
1497 /* Callback from check_function_arguments_recurse to check a
1498 format string. FORMAT_TREE is the format parameter. ARG_NUM
1499 is the number of the format argument. CTX points to a
1500 format_check_context. */
1502 static void
1503 check_format_arg (void *ctx, tree format_tree,
1504 unsigned HOST_WIDE_INT arg_num)
1506 format_check_context *format_ctx = (format_check_context *) ctx;
1507 format_check_results *res = format_ctx->res;
1508 function_format_info *info = format_ctx->info;
1509 tree params = format_ctx->params;
1511 int format_length;
1512 HOST_WIDE_INT offset;
1513 const char *format_chars;
1514 tree array_size = 0;
1515 tree array_init;
1517 if (TREE_CODE (format_tree) == VAR_DECL)
1519 /* Pull out a constant value if the front end didn't. */
1520 format_tree = decl_constant_value (format_tree);
1521 STRIP_NOPS (format_tree);
1524 if (integer_zerop (format_tree))
1526 /* Skip to first argument to check, so we can see if this format
1527 has any arguments (it shouldn't). */
1528 while (arg_num + 1 < info->first_arg_num)
1530 if (params == 0)
1531 return;
1532 params = TREE_CHAIN (params);
1533 ++arg_num;
1536 if (params == 0)
1537 res->number_other++;
1538 else
1540 if (res->number_extra_args == 0)
1541 res->extra_arg_loc = EXPR_LOC_OR_LOC (TREE_VALUE (params),
1542 input_location);
1543 res->number_extra_args++;
1545 return;
1548 offset = 0;
1549 if (TREE_CODE (format_tree) == POINTER_PLUS_EXPR)
1551 tree arg0, arg1;
1553 arg0 = TREE_OPERAND (format_tree, 0);
1554 arg1 = TREE_OPERAND (format_tree, 1);
1555 STRIP_NOPS (arg0);
1556 STRIP_NOPS (arg1);
1557 if (TREE_CODE (arg1) == INTEGER_CST)
1558 format_tree = arg0;
1559 else
1561 res->number_non_literal++;
1562 return;
1564 /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */
1565 if (!cst_and_fits_in_hwi (arg1))
1567 res->number_non_literal++;
1568 return;
1570 offset = int_cst_value (arg1);
1572 if (TREE_CODE (format_tree) != ADDR_EXPR)
1574 res->number_non_literal++;
1575 return;
1577 res->format_string_loc = EXPR_LOC_OR_LOC (format_tree, input_location);
1578 format_tree = TREE_OPERAND (format_tree, 0);
1579 if (format_types[info->format_type].flags
1580 & (int) FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL)
1582 bool objc_str = (info->format_type == gcc_objc_string_format_type);
1583 /* We cannot examine this string here - but we can check that it is
1584 a valid type. */
1585 if (TREE_CODE (format_tree) != CONST_DECL
1586 || !((objc_str && objc_string_ref_type_p (TREE_TYPE (format_tree)))
1587 || (*targetcm.string_object_ref_type_p)
1588 ((const_tree) TREE_TYPE (format_tree))))
1590 res->number_non_literal++;
1591 return;
1593 /* Skip to first argument to check. */
1594 while (arg_num + 1 < info->first_arg_num)
1596 if (params == 0)
1597 return;
1598 params = TREE_CHAIN (params);
1599 ++arg_num;
1601 /* So, we have a valid literal string object and one or more params.
1602 We need to use an external helper to parse the string into format
1603 info. For Objective-C variants we provide the resource within the
1604 objc tree, for target variants, via a hook. */
1605 if (objc_str)
1606 objc_check_format_arg (format_tree, params);
1607 else if (targetcm.check_string_object_format_arg)
1608 (*targetcm.check_string_object_format_arg) (format_tree, params);
1609 /* Else we can't handle it and retire quietly. */
1610 return;
1612 if (TREE_CODE (format_tree) == ARRAY_REF
1613 && tree_fits_shwi_p (TREE_OPERAND (format_tree, 1))
1614 && (offset += tree_to_shwi (TREE_OPERAND (format_tree, 1))) >= 0)
1615 format_tree = TREE_OPERAND (format_tree, 0);
1616 if (offset < 0)
1618 res->number_non_literal++;
1619 return;
1621 if (TREE_CODE (format_tree) == VAR_DECL
1622 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
1623 && (array_init = decl_constant_value (format_tree)) != format_tree
1624 && TREE_CODE (array_init) == STRING_CST)
1626 /* Extract the string constant initializer. Note that this may include
1627 a trailing NUL character that is not in the array (e.g.
1628 const char a[3] = "foo";). */
1629 array_size = DECL_SIZE_UNIT (format_tree);
1630 format_tree = array_init;
1632 if (TREE_CODE (format_tree) != STRING_CST)
1634 res->number_non_literal++;
1635 return;
1637 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
1639 res->number_wide++;
1640 return;
1642 format_chars = TREE_STRING_POINTER (format_tree);
1643 format_length = TREE_STRING_LENGTH (format_tree);
1644 if (array_size != 0)
1646 /* Variable length arrays can't be initialized. */
1647 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
1649 if (tree_fits_shwi_p (array_size))
1651 HOST_WIDE_INT array_size_value = tree_to_shwi (array_size);
1652 if (array_size_value > 0
1653 && array_size_value == (int) array_size_value
1654 && format_length > array_size_value)
1655 format_length = array_size_value;
1658 if (offset)
1660 if (offset >= format_length)
1662 res->number_non_literal++;
1663 return;
1665 format_chars += offset;
1666 format_length -= offset;
1668 if (format_length < 1 || format_chars[--format_length] != 0)
1670 res->number_unterminated++;
1671 return;
1673 if (format_length == 0)
1675 res->number_empty++;
1676 return;
1679 /* Skip to first argument to check. */
1680 while (arg_num + 1 < info->first_arg_num)
1682 if (params == 0)
1683 return;
1684 params = TREE_CHAIN (params);
1685 ++arg_num;
1687 /* Provisionally increment res->number_other; check_format_info_main
1688 will decrement it if it finds there are extra arguments, but this way
1689 need not adjust it for every return. */
1690 res->number_other++;
1691 pool_allocator <format_wanted_type> fwt_pool ("format_wanted_type pool", 10);
1692 check_format_info_main (res, info, format_chars, format_length,
1693 params, arg_num, fwt_pool);
1697 /* Do the main part of checking a call to a format function. FORMAT_CHARS
1698 is the NUL-terminated format string (which at this point may contain
1699 internal NUL characters); FORMAT_LENGTH is its length (excluding the
1700 terminating NUL character). ARG_NUM is one less than the number of
1701 the first format argument to check; PARAMS points to that format
1702 argument in the list of arguments. */
1704 static void
1705 check_format_info_main (format_check_results *res,
1706 function_format_info *info, const char *format_chars,
1707 int format_length, tree params,
1708 unsigned HOST_WIDE_INT arg_num,
1709 pool_allocator<format_wanted_type> &fwt_pool)
1711 const char *orig_format_chars = format_chars;
1712 tree first_fillin_param = params;
1714 const format_kind_info *fki = &format_types[info->format_type];
1715 const format_flag_spec *flag_specs = fki->flag_specs;
1716 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
1717 location_t format_string_loc = res->format_string_loc;
1719 /* -1 if no conversions taking an operand have been found; 0 if one has
1720 and it didn't use $; 1 if $ formats are in use. */
1721 int has_operand_number = -1;
1723 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
1725 while (*format_chars != 0)
1727 int i;
1728 int suppressed = FALSE;
1729 const char *length_chars = NULL;
1730 enum format_lengths length_chars_val = FMT_LEN_none;
1731 enum format_std_version length_chars_std = STD_C89;
1732 int format_char;
1733 tree cur_param;
1734 tree wanted_type;
1735 int main_arg_num = 0;
1736 tree main_arg_params = 0;
1737 enum format_std_version wanted_type_std;
1738 const char *wanted_type_name;
1739 format_wanted_type width_wanted_type;
1740 format_wanted_type precision_wanted_type;
1741 format_wanted_type main_wanted_type;
1742 format_wanted_type *first_wanted_type = NULL;
1743 format_wanted_type *last_wanted_type = NULL;
1744 const format_length_info *fli = NULL;
1745 const format_char_info *fci = NULL;
1746 char flag_chars[256];
1747 int alloc_flag = 0;
1748 int scalar_identity_flag = 0;
1749 const char *format_start;
1751 if (*format_chars++ != '%')
1752 continue;
1753 if (*format_chars == 0)
1755 warning_at (location_from_offset (format_string_loc,
1756 format_chars - orig_format_chars),
1757 OPT_Wformat_,
1758 "spurious trailing %<%%%> in format");
1759 continue;
1761 if (*format_chars == '%')
1763 ++format_chars;
1764 continue;
1766 flag_chars[0] = 0;
1768 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
1770 /* Possibly read a $ operand number at the start of the format.
1771 If one was previously used, one is required here. If one
1772 is not used here, we can't immediately conclude this is a
1773 format without them, since it could be printf %m or scanf %*. */
1774 int opnum;
1775 opnum = maybe_read_dollar_number (&format_chars, 0,
1776 first_fillin_param,
1777 &main_arg_params, fki);
1778 if (opnum == -1)
1779 return;
1780 else if (opnum > 0)
1782 has_operand_number = 1;
1783 main_arg_num = opnum + info->first_arg_num - 1;
1786 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
1788 if (avoid_dollar_number (format_chars))
1789 return;
1792 /* Read any format flags, but do not yet validate them beyond removing
1793 duplicates, since in general validation depends on the rest of
1794 the format. */
1795 while (*format_chars != 0
1796 && strchr (fki->flag_chars, *format_chars) != 0)
1798 const format_flag_spec *s = get_flag_spec (flag_specs,
1799 *format_chars, NULL);
1800 if (strchr (flag_chars, *format_chars) != 0)
1802 warning_at (location_from_offset (format_string_loc,
1803 format_chars + 1
1804 - orig_format_chars),
1805 OPT_Wformat_,
1806 "repeated %s in format", _(s->name));
1808 else
1810 i = strlen (flag_chars);
1811 flag_chars[i++] = *format_chars;
1812 flag_chars[i] = 0;
1814 if (s->skip_next_char)
1816 ++format_chars;
1817 if (*format_chars == 0)
1819 warning_at (format_string_loc, OPT_Wformat_,
1820 "missing fill character at end of strfmon format");
1821 return;
1824 ++format_chars;
1827 /* Read any format width, possibly * or *m$. */
1828 if (fki->width_char != 0)
1830 if (fki->width_type != NULL && *format_chars == '*')
1832 i = strlen (flag_chars);
1833 flag_chars[i++] = fki->width_char;
1834 flag_chars[i] = 0;
1835 /* "...a field width...may be indicated by an asterisk.
1836 In this case, an int argument supplies the field width..." */
1837 ++format_chars;
1838 if (has_operand_number != 0)
1840 int opnum;
1841 opnum = maybe_read_dollar_number (&format_chars,
1842 has_operand_number == 1,
1843 first_fillin_param,
1844 &params, fki);
1845 if (opnum == -1)
1846 return;
1847 else if (opnum > 0)
1849 has_operand_number = 1;
1850 arg_num = opnum + info->first_arg_num - 1;
1852 else
1853 has_operand_number = 0;
1855 else
1857 if (avoid_dollar_number (format_chars))
1858 return;
1860 if (info->first_arg_num != 0)
1862 if (params == 0)
1863 cur_param = NULL;
1864 else
1866 cur_param = TREE_VALUE (params);
1867 if (has_operand_number <= 0)
1869 params = TREE_CHAIN (params);
1870 ++arg_num;
1873 width_wanted_type.wanted_type = *fki->width_type;
1874 width_wanted_type.wanted_type_name = NULL;
1875 width_wanted_type.pointer_count = 0;
1876 width_wanted_type.char_lenient_flag = 0;
1877 width_wanted_type.scalar_identity_flag = 0;
1878 width_wanted_type.writing_in_flag = 0;
1879 width_wanted_type.reading_from_flag = 0;
1880 width_wanted_type.kind = CF_KIND_FIELD_WIDTH;
1881 width_wanted_type.format_start = format_chars - 1;
1882 width_wanted_type.format_length = 1;
1883 width_wanted_type.param = cur_param;
1884 width_wanted_type.arg_num = arg_num;
1885 width_wanted_type.offset_loc =
1886 format_chars - orig_format_chars;
1887 width_wanted_type.next = NULL;
1888 if (last_wanted_type != 0)
1889 last_wanted_type->next = &width_wanted_type;
1890 if (first_wanted_type == 0)
1891 first_wanted_type = &width_wanted_type;
1892 last_wanted_type = &width_wanted_type;
1895 else
1897 /* Possibly read a numeric width. If the width is zero,
1898 we complain if appropriate. */
1899 int non_zero_width_char = FALSE;
1900 int found_width = FALSE;
1901 while (ISDIGIT (*format_chars))
1903 found_width = TRUE;
1904 if (*format_chars != '0')
1905 non_zero_width_char = TRUE;
1906 ++format_chars;
1908 if (found_width && !non_zero_width_char &&
1909 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
1910 warning_at (format_string_loc, OPT_Wformat_,
1911 "zero width in %s format", fki->name);
1912 if (found_width)
1914 i = strlen (flag_chars);
1915 flag_chars[i++] = fki->width_char;
1916 flag_chars[i] = 0;
1921 /* Read any format left precision (must be a number, not *). */
1922 if (fki->left_precision_char != 0 && *format_chars == '#')
1924 ++format_chars;
1925 i = strlen (flag_chars);
1926 flag_chars[i++] = fki->left_precision_char;
1927 flag_chars[i] = 0;
1928 if (!ISDIGIT (*format_chars))
1929 warning_at (location_from_offset (format_string_loc,
1930 format_chars - orig_format_chars),
1931 OPT_Wformat_,
1932 "empty left precision in %s format", fki->name);
1933 while (ISDIGIT (*format_chars))
1934 ++format_chars;
1937 /* Read any format precision, possibly * or *m$. */
1938 if (fki->precision_char != 0 && *format_chars == '.')
1940 ++format_chars;
1941 i = strlen (flag_chars);
1942 flag_chars[i++] = fki->precision_char;
1943 flag_chars[i] = 0;
1944 if (fki->precision_type != NULL && *format_chars == '*')
1946 /* "...a...precision...may be indicated by an asterisk.
1947 In this case, an int argument supplies the...precision." */
1948 ++format_chars;
1949 if (has_operand_number != 0)
1951 int opnum;
1952 opnum = maybe_read_dollar_number (&format_chars,
1953 has_operand_number == 1,
1954 first_fillin_param,
1955 &params, fki);
1956 if (opnum == -1)
1957 return;
1958 else if (opnum > 0)
1960 has_operand_number = 1;
1961 arg_num = opnum + info->first_arg_num - 1;
1963 else
1964 has_operand_number = 0;
1966 else
1968 if (avoid_dollar_number (format_chars))
1969 return;
1971 if (info->first_arg_num != 0)
1973 if (params == 0)
1974 cur_param = NULL;
1975 else
1977 cur_param = TREE_VALUE (params);
1978 if (has_operand_number <= 0)
1980 params = TREE_CHAIN (params);
1981 ++arg_num;
1984 precision_wanted_type.wanted_type = *fki->precision_type;
1985 precision_wanted_type.wanted_type_name = NULL;
1986 precision_wanted_type.pointer_count = 0;
1987 precision_wanted_type.char_lenient_flag = 0;
1988 precision_wanted_type.scalar_identity_flag = 0;
1989 precision_wanted_type.writing_in_flag = 0;
1990 precision_wanted_type.reading_from_flag = 0;
1991 precision_wanted_type.kind = CF_KIND_FIELD_PRECISION;
1992 precision_wanted_type.param = cur_param;
1993 precision_wanted_type.format_start = format_chars - 2;
1994 precision_wanted_type.format_length = 2;
1995 precision_wanted_type.arg_num = arg_num;
1996 precision_wanted_type.offset_loc =
1997 format_chars - orig_format_chars;
1998 precision_wanted_type.next = NULL;
1999 if (last_wanted_type != 0)
2000 last_wanted_type->next = &precision_wanted_type;
2001 if (first_wanted_type == 0)
2002 first_wanted_type = &precision_wanted_type;
2003 last_wanted_type = &precision_wanted_type;
2006 else
2008 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
2009 && !ISDIGIT (*format_chars))
2010 warning_at (location_from_offset (format_string_loc,
2011 format_chars - orig_format_chars),
2012 OPT_Wformat_,
2013 "empty precision in %s format", fki->name);
2014 while (ISDIGIT (*format_chars))
2015 ++format_chars;
2019 format_start = format_chars;
2020 if (fki->alloc_char && fki->alloc_char == *format_chars)
2022 i = strlen (flag_chars);
2023 flag_chars[i++] = fki->alloc_char;
2024 flag_chars[i] = 0;
2025 format_chars++;
2028 /* Handle the scanf allocation kludge. */
2029 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2031 if (*format_chars == 'a' && !flag_isoc99)
2033 if (format_chars[1] == 's' || format_chars[1] == 'S'
2034 || format_chars[1] == '[')
2036 /* 'a' is used as a flag. */
2037 i = strlen (flag_chars);
2038 flag_chars[i++] = 'a';
2039 flag_chars[i] = 0;
2040 format_chars++;
2045 /* Read any length modifier, if this kind of format has them. */
2046 fli = fki->length_char_specs;
2047 length_chars = NULL;
2048 length_chars_val = FMT_LEN_none;
2049 length_chars_std = STD_C89;
2050 scalar_identity_flag = 0;
2051 if (fli)
2053 while (fli->name != 0
2054 && strncmp (fli->name, format_chars, strlen (fli->name)))
2055 fli++;
2056 if (fli->name != 0)
2058 format_chars += strlen (fli->name);
2059 if (fli->double_name != 0 && fli->name[0] == *format_chars)
2061 format_chars++;
2062 length_chars = fli->double_name;
2063 length_chars_val = fli->double_index;
2064 length_chars_std = fli->double_std;
2066 else
2068 length_chars = fli->name;
2069 length_chars_val = fli->index;
2070 length_chars_std = fli->std;
2071 scalar_identity_flag = fli->scalar_identity_flag;
2073 i = strlen (flag_chars);
2074 flag_chars[i++] = fki->length_code_char;
2075 flag_chars[i] = 0;
2077 if (pedantic)
2079 /* Warn if the length modifier is non-standard. */
2080 if (ADJ_STD (length_chars_std) > C_STD_VER)
2081 warning_at (format_string_loc, OPT_Wformat_,
2082 "%s does not support the %qs %s length modifier",
2083 C_STD_NAME (length_chars_std), length_chars,
2084 fki->name);
2088 /* Read any modifier (strftime E/O). */
2089 if (fki->modifier_chars != NULL)
2091 while (*format_chars != 0
2092 && strchr (fki->modifier_chars, *format_chars) != 0)
2094 if (strchr (flag_chars, *format_chars) != 0)
2096 const format_flag_spec *s = get_flag_spec (flag_specs,
2097 *format_chars, NULL);
2098 warning_at (location_from_offset (format_string_loc,
2099 format_chars
2100 - orig_format_chars),
2101 OPT_Wformat_,
2102 "repeated %s in format", _(s->name));
2104 else
2106 i = strlen (flag_chars);
2107 flag_chars[i++] = *format_chars;
2108 flag_chars[i] = 0;
2110 ++format_chars;
2114 format_char = *format_chars;
2115 if (format_char == 0
2116 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
2117 && format_char == '%'))
2119 warning_at (location_from_offset (format_string_loc,
2120 format_chars - orig_format_chars),
2121 OPT_Wformat_,
2122 "conversion lacks type at end of format");
2123 continue;
2125 format_chars++;
2126 fci = fki->conversion_specs;
2127 while (fci->format_chars != 0
2128 && strchr (fci->format_chars, format_char) == 0)
2129 ++fci;
2130 if (fci->format_chars == 0)
2132 if (ISGRAPH (format_char))
2133 warning_at (location_from_offset (format_string_loc,
2134 format_chars - orig_format_chars),
2135 OPT_Wformat_,
2136 "unknown conversion type character %qc in format",
2137 format_char);
2138 else
2139 warning_at (location_from_offset (format_string_loc,
2140 format_chars - orig_format_chars),
2141 OPT_Wformat_,
2142 "unknown conversion type character 0x%x in format",
2143 format_char);
2144 continue;
2146 if (pedantic)
2148 if (ADJ_STD (fci->std) > C_STD_VER)
2149 warning_at (location_from_offset (format_string_loc,
2150 format_chars - orig_format_chars),
2151 OPT_Wformat_,
2152 "%s does not support the %<%%%c%> %s format",
2153 C_STD_NAME (fci->std), format_char, fki->name);
2156 /* Validate the individual flags used, removing any that are invalid. */
2158 int d = 0;
2159 for (i = 0; flag_chars[i] != 0; i++)
2161 const format_flag_spec *s = get_flag_spec (flag_specs,
2162 flag_chars[i], NULL);
2163 flag_chars[i - d] = flag_chars[i];
2164 if (flag_chars[i] == fki->length_code_char)
2165 continue;
2166 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
2168 warning_at (location_from_offset (format_string_loc,
2169 format_chars
2170 - orig_format_chars),
2171 OPT_Wformat_, "%s used with %<%%%c%> %s format",
2172 _(s->name), format_char, fki->name);
2173 d++;
2174 continue;
2176 if (pedantic)
2178 const format_flag_spec *t;
2179 if (ADJ_STD (s->std) > C_STD_VER)
2180 warning_at (format_string_loc, OPT_Wformat_,
2181 "%s does not support %s",
2182 C_STD_NAME (s->std), _(s->long_name));
2183 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2184 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
2186 const char *long_name = (t->long_name != NULL
2187 ? t->long_name
2188 : s->long_name);
2189 if (ADJ_STD (t->std) > C_STD_VER)
2190 warning_at (format_string_loc, OPT_Wformat_,
2191 "%s does not support %s with the %<%%%c%> %s format",
2192 C_STD_NAME (t->std), _(long_name),
2193 format_char, fki->name);
2197 flag_chars[i - d] = 0;
2200 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
2201 && strchr (flag_chars, 'a') != 0)
2202 alloc_flag = 1;
2203 if (fki->alloc_char && strchr (flag_chars, fki->alloc_char) != 0)
2204 alloc_flag = 1;
2206 if (fki->suppression_char
2207 && strchr (flag_chars, fki->suppression_char) != 0)
2208 suppressed = 1;
2210 /* Validate the pairs of flags used. */
2211 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
2213 const format_flag_spec *s, *t;
2214 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2215 continue;
2216 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2217 continue;
2218 if (bad_flag_pairs[i].predicate != 0
2219 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2220 continue;
2221 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2222 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2223 if (bad_flag_pairs[i].ignored)
2225 if (bad_flag_pairs[i].predicate != 0)
2226 warning_at (format_string_loc, OPT_Wformat_,
2227 "%s ignored with %s and %<%%%c%> %s format",
2228 _(s->name), _(t->name), format_char,
2229 fki->name);
2230 else
2231 warning_at (format_string_loc, OPT_Wformat_,
2232 "%s ignored with %s in %s format",
2233 _(s->name), _(t->name), fki->name);
2235 else
2237 if (bad_flag_pairs[i].predicate != 0)
2238 warning_at (format_string_loc, OPT_Wformat_,
2239 "use of %s and %s together with %<%%%c%> %s format",
2240 _(s->name), _(t->name), format_char,
2241 fki->name);
2242 else
2243 warning_at (format_string_loc, OPT_Wformat_,
2244 "use of %s and %s together in %s format",
2245 _(s->name), _(t->name), fki->name);
2249 /* Give Y2K warnings. */
2250 if (warn_format_y2k)
2252 int y2k_level = 0;
2253 if (strchr (fci->flags2, '4') != 0)
2254 if (strchr (flag_chars, 'E') != 0)
2255 y2k_level = 3;
2256 else
2257 y2k_level = 2;
2258 else if (strchr (fci->flags2, '3') != 0)
2259 y2k_level = 3;
2260 else if (strchr (fci->flags2, '2') != 0)
2261 y2k_level = 2;
2262 if (y2k_level == 3)
2263 warning_at (format_string_loc, OPT_Wformat_y2k,
2264 "%<%%%c%> yields only last 2 digits of "
2265 "year in some locales", format_char);
2266 else if (y2k_level == 2)
2267 warning_at (format_string_loc, OPT_Wformat_y2k,
2268 "%<%%%c%> yields only last 2 digits of year",
2269 format_char);
2272 if (strchr (fci->flags2, '[') != 0)
2274 /* Skip over scan set, in case it happens to have '%' in it. */
2275 if (*format_chars == '^')
2276 ++format_chars;
2277 /* Find closing bracket; if one is hit immediately, then
2278 it's part of the scan set rather than a terminator. */
2279 if (*format_chars == ']')
2280 ++format_chars;
2281 while (*format_chars && *format_chars != ']')
2282 ++format_chars;
2283 if (*format_chars != ']')
2284 /* The end of the format string was reached. */
2285 warning_at (location_from_offset (format_string_loc,
2286 format_chars - orig_format_chars),
2287 OPT_Wformat_,
2288 "no closing %<]%> for %<%%[%> format");
2291 wanted_type = 0;
2292 wanted_type_name = 0;
2293 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
2295 wanted_type = (fci->types[length_chars_val].type
2296 ? *fci->types[length_chars_val].type : 0);
2297 wanted_type_name = fci->types[length_chars_val].name;
2298 wanted_type_std = fci->types[length_chars_val].std;
2299 if (wanted_type == 0)
2301 warning_at (location_from_offset (format_string_loc,
2302 format_chars - orig_format_chars),
2303 OPT_Wformat_,
2304 "use of %qs length modifier with %qc type character"
2305 " has either no effect or undefined behavior",
2306 length_chars, format_char);
2307 /* Heuristic: skip one argument when an invalid length/type
2308 combination is encountered. */
2309 arg_num++;
2310 if (params != 0)
2311 params = TREE_CHAIN (params);
2312 continue;
2314 else if (pedantic
2315 /* Warn if non-standard, provided it is more non-standard
2316 than the length and type characters that may already
2317 have been warned for. */
2318 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
2319 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
2321 if (ADJ_STD (wanted_type_std) > C_STD_VER)
2322 warning_at (location_from_offset (format_string_loc,
2323 format_chars - orig_format_chars),
2324 OPT_Wformat_,
2325 "%s does not support the %<%%%s%c%> %s format",
2326 C_STD_NAME (wanted_type_std), length_chars,
2327 format_char, fki->name);
2331 main_wanted_type.next = NULL;
2333 /* Finally. . .check type of argument against desired type! */
2334 if (info->first_arg_num == 0)
2335 continue;
2336 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2337 || suppressed)
2339 if (main_arg_num != 0)
2341 if (suppressed)
2342 warning_at (format_string_loc, OPT_Wformat_,
2343 "operand number specified with "
2344 "suppressed assignment");
2345 else
2346 warning_at (format_string_loc, OPT_Wformat_,
2347 "operand number specified for format "
2348 "taking no argument");
2351 else
2353 format_wanted_type *wanted_type_ptr;
2355 if (main_arg_num != 0)
2357 arg_num = main_arg_num;
2358 params = main_arg_params;
2360 else
2362 ++arg_num;
2363 if (has_operand_number > 0)
2365 warning_at (format_string_loc, OPT_Wformat_,
2366 "missing $ operand number in format");
2367 return;
2369 else
2370 has_operand_number = 0;
2373 wanted_type_ptr = &main_wanted_type;
2374 while (fci)
2376 if (params == 0)
2377 cur_param = NULL;
2378 else
2380 cur_param = TREE_VALUE (params);
2381 params = TREE_CHAIN (params);
2384 wanted_type_ptr->wanted_type = wanted_type;
2385 wanted_type_ptr->wanted_type_name = wanted_type_name;
2386 wanted_type_ptr->pointer_count = fci->pointer_count + alloc_flag;
2387 wanted_type_ptr->char_lenient_flag = 0;
2388 if (strchr (fci->flags2, 'c') != 0)
2389 wanted_type_ptr->char_lenient_flag = 1;
2390 wanted_type_ptr->scalar_identity_flag = 0;
2391 if (scalar_identity_flag)
2392 wanted_type_ptr->scalar_identity_flag = 1;
2393 wanted_type_ptr->writing_in_flag = 0;
2394 wanted_type_ptr->reading_from_flag = 0;
2395 if (alloc_flag)
2396 wanted_type_ptr->writing_in_flag = 1;
2397 else
2399 if (strchr (fci->flags2, 'W') != 0)
2400 wanted_type_ptr->writing_in_flag = 1;
2401 if (strchr (fci->flags2, 'R') != 0)
2402 wanted_type_ptr->reading_from_flag = 1;
2404 wanted_type_ptr->kind = CF_KIND_FORMAT;
2405 wanted_type_ptr->param = cur_param;
2406 wanted_type_ptr->arg_num = arg_num;
2407 wanted_type_ptr->format_start = format_start;
2408 wanted_type_ptr->format_length = format_chars - format_start;
2409 wanted_type_ptr->offset_loc = format_chars - orig_format_chars;
2410 wanted_type_ptr->next = NULL;
2411 if (last_wanted_type != 0)
2412 last_wanted_type->next = wanted_type_ptr;
2413 if (first_wanted_type == 0)
2414 first_wanted_type = wanted_type_ptr;
2415 last_wanted_type = wanted_type_ptr;
2417 fci = fci->chain;
2418 if (fci)
2420 wanted_type_ptr = fwt_pool.allocate ();
2421 arg_num++;
2422 wanted_type = *fci->types[length_chars_val].type;
2423 wanted_type_name = fci->types[length_chars_val].name;
2428 if (first_wanted_type != 0)
2429 check_format_types (format_string_loc, first_wanted_type);
2432 if (format_chars - orig_format_chars != format_length)
2433 warning_at (location_from_offset (format_string_loc,
2434 format_chars + 1 - orig_format_chars),
2435 OPT_Wformat_contains_nul,
2436 "embedded %<\\0%> in format");
2437 if (info->first_arg_num != 0 && params != 0
2438 && has_operand_number <= 0)
2440 res->number_other--;
2441 res->number_extra_args++;
2443 if (has_operand_number > 0)
2444 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
2448 /* Check the argument types from a single format conversion (possibly
2449 including width and precision arguments). LOC is the location of
2450 the format string. */
2451 static void
2452 check_format_types (location_t loc, format_wanted_type *types)
2454 for (; types != 0; types = types->next)
2456 tree cur_param;
2457 tree cur_type;
2458 tree orig_cur_type;
2459 tree wanted_type;
2460 int arg_num;
2461 int i;
2462 int char_type_flag;
2464 wanted_type = types->wanted_type;
2465 arg_num = types->arg_num;
2467 /* The following should not occur here. */
2468 gcc_assert (wanted_type);
2469 gcc_assert (wanted_type != void_type_node || types->pointer_count);
2471 if (types->pointer_count == 0)
2472 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
2474 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
2476 cur_param = types->param;
2477 if (!cur_param)
2479 format_type_warning (loc, types, wanted_type, NULL);
2480 continue;
2483 cur_type = TREE_TYPE (cur_param);
2484 if (cur_type == error_mark_node)
2485 continue;
2486 orig_cur_type = cur_type;
2487 char_type_flag = 0;
2489 STRIP_NOPS (cur_param);
2491 /* Check the types of any additional pointer arguments
2492 that precede the "real" argument. */
2493 for (i = 0; i < types->pointer_count; ++i)
2495 if (TREE_CODE (cur_type) == POINTER_TYPE)
2497 cur_type = TREE_TYPE (cur_type);
2498 if (cur_type == error_mark_node)
2499 break;
2501 /* Check for writing through a NULL pointer. */
2502 if (types->writing_in_flag
2503 && i == 0
2504 && cur_param != 0
2505 && integer_zerop (cur_param))
2506 warning (OPT_Wformat_, "writing through null pointer "
2507 "(argument %d)", arg_num);
2509 /* Check for reading through a NULL pointer. */
2510 if (types->reading_from_flag
2511 && i == 0
2512 && cur_param != 0
2513 && integer_zerop (cur_param))
2514 warning (OPT_Wformat_, "reading through null pointer "
2515 "(argument %d)", arg_num);
2517 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
2518 cur_param = TREE_OPERAND (cur_param, 0);
2519 else
2520 cur_param = 0;
2522 /* See if this is an attempt to write into a const type with
2523 scanf or with printf "%n". Note: the writing in happens
2524 at the first indirection only, if for example
2525 void * const * is passed to scanf %p; passing
2526 const void ** is simply passing an incompatible type. */
2527 if (types->writing_in_flag
2528 && i == 0
2529 && (TYPE_READONLY (cur_type)
2530 || (cur_param != 0
2531 && (CONSTANT_CLASS_P (cur_param)
2532 || (DECL_P (cur_param)
2533 && TREE_READONLY (cur_param))))))
2534 warning (OPT_Wformat_, "writing into constant object "
2535 "(argument %d)", arg_num);
2537 /* If there are extra type qualifiers beyond the first
2538 indirection, then this makes the types technically
2539 incompatible. */
2540 if (i > 0
2541 && pedantic
2542 && (TYPE_READONLY (cur_type)
2543 || TYPE_VOLATILE (cur_type)
2544 || TYPE_ATOMIC (cur_type)
2545 || TYPE_RESTRICT (cur_type)))
2546 warning (OPT_Wformat_, "extra type qualifiers in format "
2547 "argument (argument %d)",
2548 arg_num);
2551 else
2553 format_type_warning (loc, types, wanted_type, orig_cur_type);
2554 break;
2558 if (i < types->pointer_count)
2559 continue;
2561 cur_type = TYPE_MAIN_VARIANT (cur_type);
2563 /* Check whether the argument type is a character type. This leniency
2564 only applies to certain formats, flagged with 'c'. */
2565 if (types->char_lenient_flag)
2566 char_type_flag = (cur_type == char_type_node
2567 || cur_type == signed_char_type_node
2568 || cur_type == unsigned_char_type_node);
2570 /* Check the type of the "real" argument, if there's a type we want. */
2571 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
2572 continue;
2573 /* If we want 'void *', allow any pointer type.
2574 (Anything else would already have got a warning.)
2575 With -Wpedantic, only allow pointers to void and to character
2576 types. */
2577 if (wanted_type == void_type_node
2578 && (!pedantic || (i == 1 && char_type_flag)))
2579 continue;
2580 /* Don't warn about differences merely in signedness, unless
2581 -Wpedantic. With -Wpedantic, warn if the type is a pointer
2582 target and not a character type, and for character types at
2583 a second level of indirection. */
2584 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2585 && TREE_CODE (cur_type) == INTEGER_TYPE
2586 && ((!pedantic && !warn_format_signedness)
2587 || (i == 0 && !warn_format_signedness)
2588 || (i == 1 && char_type_flag))
2589 && (TYPE_UNSIGNED (wanted_type)
2590 ? wanted_type == c_common_unsigned_type (cur_type)
2591 : wanted_type == c_common_signed_type (cur_type)))
2592 continue;
2593 /* Don't warn about differences merely in signedness if we know
2594 that the current type is integer-promoted and its original type
2595 was unsigned such as that it is in the range of WANTED_TYPE. */
2596 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2597 && TREE_CODE (cur_type) == INTEGER_TYPE
2598 && warn_format_signedness
2599 && TYPE_UNSIGNED (wanted_type)
2600 && cur_param != NULL_TREE
2601 && TREE_CODE (cur_param) == NOP_EXPR)
2603 tree t = TREE_TYPE (TREE_OPERAND (cur_param, 0));
2604 if (TYPE_UNSIGNED (t)
2605 && cur_type == lang_hooks.types.type_promotes_to (t))
2606 continue;
2608 /* Likewise, "signed char", "unsigned char" and "char" are
2609 equivalent but the above test won't consider them equivalent. */
2610 if (wanted_type == char_type_node
2611 && (!pedantic || i < 2)
2612 && char_type_flag)
2613 continue;
2614 if (types->scalar_identity_flag
2615 && (TREE_CODE (cur_type) == TREE_CODE (wanted_type)
2616 || (INTEGRAL_TYPE_P (cur_type)
2617 && INTEGRAL_TYPE_P (wanted_type)))
2618 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type))
2619 continue;
2620 /* Now we have a type mismatch. */
2621 format_type_warning (loc, types, wanted_type, orig_cur_type);
2626 /* Give a warning at LOC about a format argument of different type from that
2627 expected. WANTED_TYPE is the type the argument should have, possibly
2628 stripped of pointer dereferences. The description (such as "field
2629 precision"), the placement in the format string, a possibly more
2630 friendly name of WANTED_TYPE, and the number of pointer dereferences
2631 are taken from TYPE. ARG_TYPE is the type of the actual argument,
2632 or NULL if it is missing. */
2633 static void
2634 format_type_warning (location_t loc, format_wanted_type *type,
2635 tree wanted_type, tree arg_type)
2637 int kind = type->kind;
2638 const char *wanted_type_name = type->wanted_type_name;
2639 const char *format_start = type->format_start;
2640 int format_length = type->format_length;
2641 int pointer_count = type->pointer_count;
2642 int arg_num = type->arg_num;
2643 unsigned int offset_loc = type->offset_loc;
2645 char *p;
2646 /* If ARG_TYPE is a typedef with a misleading name (for example,
2647 size_t but not the standard size_t expected by printf %zu), avoid
2648 printing the typedef name. */
2649 if (wanted_type_name
2650 && arg_type
2651 && TYPE_NAME (arg_type)
2652 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
2653 && DECL_NAME (TYPE_NAME (arg_type))
2654 && !strcmp (wanted_type_name,
2655 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
2656 arg_type = TYPE_MAIN_VARIANT (arg_type);
2657 /* The format type and name exclude any '*' for pointers, so those
2658 must be formatted manually. For all the types we currently have,
2659 this is adequate, but formats taking pointers to functions or
2660 arrays would require the full type to be built up in order to
2661 print it with %T. */
2662 p = (char *) alloca (pointer_count + 2);
2663 if (pointer_count == 0)
2664 p[0] = 0;
2665 else if (c_dialect_cxx ())
2667 memset (p, '*', pointer_count);
2668 p[pointer_count] = 0;
2670 else
2672 p[0] = ' ';
2673 memset (p + 1, '*', pointer_count);
2674 p[pointer_count + 1] = 0;
2677 loc = location_from_offset (loc, offset_loc);
2679 if (wanted_type_name)
2681 if (arg_type)
2682 warning_at (loc, OPT_Wformat_,
2683 "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
2684 "but argument %d has type %qT",
2685 gettext (kind_descriptions[kind]),
2686 (kind == CF_KIND_FORMAT ? "%" : ""),
2687 format_length, format_start,
2688 wanted_type_name, p, arg_num, arg_type);
2689 else
2690 warning_at (loc, OPT_Wformat_,
2691 "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
2692 gettext (kind_descriptions[kind]),
2693 (kind == CF_KIND_FORMAT ? "%" : ""),
2694 format_length, format_start, wanted_type_name, p);
2696 else
2698 if (arg_type)
2699 warning_at (loc, OPT_Wformat_,
2700 "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
2701 "but argument %d has type %qT",
2702 gettext (kind_descriptions[kind]),
2703 (kind == CF_KIND_FORMAT ? "%" : ""),
2704 format_length, format_start,
2705 wanted_type, p, arg_num, arg_type);
2706 else
2707 warning_at (loc, OPT_Wformat_,
2708 "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
2709 gettext (kind_descriptions[kind]),
2710 (kind == CF_KIND_FORMAT ? "%" : ""),
2711 format_length, format_start, wanted_type, p);
2716 /* Given a format_char_info array FCI, and a character C, this function
2717 returns the index into the conversion_specs where that specifier's
2718 data is located. The character must exist. */
2719 static unsigned int
2720 find_char_info_specifier_index (const format_char_info *fci, int c)
2722 unsigned i;
2724 for (i = 0; fci->format_chars; i++, fci++)
2725 if (strchr (fci->format_chars, c))
2726 return i;
2728 /* We shouldn't be looking for a non-existent specifier. */
2729 gcc_unreachable ();
2732 /* Given a format_length_info array FLI, and a character C, this
2733 function returns the index into the conversion_specs where that
2734 modifier's data is located. The character must exist. */
2735 static unsigned int
2736 find_length_info_modifier_index (const format_length_info *fli, int c)
2738 unsigned i;
2740 for (i = 0; fli->name; i++, fli++)
2741 if (strchr (fli->name, c))
2742 return i;
2744 /* We shouldn't be looking for a non-existent modifier. */
2745 gcc_unreachable ();
2748 /* Determine the type of HOST_WIDE_INT in the code being compiled for
2749 use in GCC's __asm_fprintf__ custom format attribute. You must
2750 have set dynamic_format_types before calling this function. */
2751 static void
2752 init_dynamic_asm_fprintf_info (void)
2754 static tree hwi;
2756 if (!hwi)
2758 format_length_info *new_asm_fprintf_length_specs;
2759 unsigned int i;
2761 /* Find the underlying type for HOST_WIDE_INT. For the %w
2762 length modifier to work, one must have issued: "typedef
2763 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2764 prior to using that modifier. */
2765 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
2766 if (!hwi)
2768 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2769 return;
2771 hwi = identifier_global_value (hwi);
2772 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
2774 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2775 return;
2777 hwi = DECL_ORIGINAL_TYPE (hwi);
2778 gcc_assert (hwi);
2779 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
2781 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
2782 " or %<long long%>");
2783 return;
2786 /* Create a new (writable) copy of asm_fprintf_length_specs. */
2787 new_asm_fprintf_length_specs = (format_length_info *)
2788 xmemdup (asm_fprintf_length_specs,
2789 sizeof (asm_fprintf_length_specs),
2790 sizeof (asm_fprintf_length_specs));
2792 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2793 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
2794 if (hwi == long_integer_type_node)
2795 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
2796 else if (hwi == long_long_integer_type_node)
2797 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
2798 else
2799 gcc_unreachable ();
2801 /* Assign the new data for use. */
2802 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
2803 new_asm_fprintf_length_specs;
2807 /* Determine the type of a "locus" in the code being compiled for use
2808 in GCC's __gcc_gfc__ custom format attribute. You must have set
2809 dynamic_format_types before calling this function. */
2810 static void
2811 init_dynamic_gfc_info (void)
2813 static tree locus;
2815 if (!locus)
2817 static format_char_info *gfc_fci;
2819 /* For the GCC __gcc_gfc__ custom format specifier to work, one
2820 must have declared 'locus' prior to using this attribute. If
2821 we haven't seen this declarations then you shouldn't use the
2822 specifier requiring that type. */
2823 if ((locus = maybe_get_identifier ("locus")))
2825 locus = identifier_global_value (locus);
2826 if (locus)
2828 if (TREE_CODE (locus) != TYPE_DECL
2829 || TREE_TYPE (locus) == error_mark_node)
2831 error ("%<locus%> is not defined as a type");
2832 locus = 0;
2834 else
2835 locus = TREE_TYPE (locus);
2839 /* Assign the new data for use. */
2841 /* Handle the __gcc_gfc__ format specifics. */
2842 if (!gfc_fci)
2843 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
2844 gfc_fci = (format_char_info *)
2845 xmemdup (gcc_gfc_char_table,
2846 sizeof (gcc_gfc_char_table),
2847 sizeof (gcc_gfc_char_table));
2848 if (locus)
2850 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
2851 gfc_fci[i].types[0].type = &locus;
2852 gfc_fci[i].pointer_count = 1;
2857 /* Determine the types of "tree" and "location_t" in the code being
2858 compiled for use in GCC's diagnostic custom format attributes. You
2859 must have set dynamic_format_types before calling this function. */
2860 static void
2861 init_dynamic_diag_info (void)
2863 static tree t, loc, hwi;
2865 if (!loc || !t || !hwi)
2867 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
2868 static format_length_info *diag_ls;
2869 unsigned int i;
2871 /* For the GCC-diagnostics custom format specifiers to work, one
2872 must have declared 'tree' and/or 'location_t' prior to using
2873 those attributes. If we haven't seen these declarations then
2874 you shouldn't use the specifiers requiring these types.
2875 However we don't force a hard ICE because we may see only one
2876 or the other type. */
2877 if ((loc = maybe_get_identifier ("location_t")))
2879 loc = identifier_global_value (loc);
2880 if (loc)
2882 if (TREE_CODE (loc) != TYPE_DECL)
2884 error ("%<location_t%> is not defined as a type");
2885 loc = 0;
2887 else
2888 loc = TREE_TYPE (loc);
2892 /* We need to grab the underlying 'union tree_node' so peek into
2893 an extra type level. */
2894 if ((t = maybe_get_identifier ("tree")))
2896 t = identifier_global_value (t);
2897 if (t)
2899 if (TREE_CODE (t) != TYPE_DECL)
2901 error ("%<tree%> is not defined as a type");
2902 t = 0;
2904 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
2906 error ("%<tree%> is not defined as a pointer type");
2907 t = 0;
2909 else
2910 t = TREE_TYPE (TREE_TYPE (t));
2914 /* Find the underlying type for HOST_WIDE_INT. For the %w
2915 length modifier to work, one must have issued: "typedef
2916 HOST_WIDE_INT __gcc_host_wide_int__;" in one's source code
2917 prior to using that modifier. */
2918 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
2920 hwi = identifier_global_value (hwi);
2921 if (hwi)
2923 if (TREE_CODE (hwi) != TYPE_DECL)
2925 error ("%<__gcc_host_wide_int__%> is not defined as a type");
2926 hwi = 0;
2928 else
2930 hwi = DECL_ORIGINAL_TYPE (hwi);
2931 gcc_assert (hwi);
2932 if (hwi != long_integer_type_node
2933 && hwi != long_long_integer_type_node)
2935 error ("%<__gcc_host_wide_int__%> is not defined"
2936 " as %<long%> or %<long long%>");
2937 hwi = 0;
2943 /* Assign the new data for use. */
2945 /* All the GCC diag formats use the same length specs. */
2946 if (!diag_ls)
2947 dynamic_format_types[gcc_diag_format_type].length_char_specs =
2948 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
2949 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
2950 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
2951 diag_ls = (format_length_info *)
2952 xmemdup (gcc_diag_length_specs,
2953 sizeof (gcc_diag_length_specs),
2954 sizeof (gcc_diag_length_specs));
2955 if (hwi)
2957 /* HOST_WIDE_INT must be one of 'long' or 'long long'. */
2958 i = find_length_info_modifier_index (diag_ls, 'w');
2959 if (hwi == long_integer_type_node)
2960 diag_ls[i].index = FMT_LEN_l;
2961 else if (hwi == long_long_integer_type_node)
2962 diag_ls[i].index = FMT_LEN_ll;
2963 else
2964 gcc_unreachable ();
2967 /* Handle the __gcc_diag__ format specifics. */
2968 if (!diag_fci)
2969 dynamic_format_types[gcc_diag_format_type].conversion_specs =
2970 diag_fci = (format_char_info *)
2971 xmemdup (gcc_diag_char_table,
2972 sizeof (gcc_diag_char_table),
2973 sizeof (gcc_diag_char_table));
2974 if (t)
2976 i = find_char_info_specifier_index (diag_fci, 'K');
2977 diag_fci[i].types[0].type = &t;
2978 diag_fci[i].pointer_count = 1;
2981 /* Handle the __gcc_tdiag__ format specifics. */
2982 if (!tdiag_fci)
2983 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
2984 tdiag_fci = (format_char_info *)
2985 xmemdup (gcc_tdiag_char_table,
2986 sizeof (gcc_tdiag_char_table),
2987 sizeof (gcc_tdiag_char_table));
2988 if (t)
2990 /* All specifiers taking a tree share the same struct. */
2991 i = find_char_info_specifier_index (tdiag_fci, 'D');
2992 tdiag_fci[i].types[0].type = &t;
2993 tdiag_fci[i].pointer_count = 1;
2994 i = find_char_info_specifier_index (tdiag_fci, 'K');
2995 tdiag_fci[i].types[0].type = &t;
2996 tdiag_fci[i].pointer_count = 1;
2999 /* Handle the __gcc_cdiag__ format specifics. */
3000 if (!cdiag_fci)
3001 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
3002 cdiag_fci = (format_char_info *)
3003 xmemdup (gcc_cdiag_char_table,
3004 sizeof (gcc_cdiag_char_table),
3005 sizeof (gcc_cdiag_char_table));
3006 if (t)
3008 /* All specifiers taking a tree share the same struct. */
3009 i = find_char_info_specifier_index (cdiag_fci, 'D');
3010 cdiag_fci[i].types[0].type = &t;
3011 cdiag_fci[i].pointer_count = 1;
3012 i = find_char_info_specifier_index (cdiag_fci, 'K');
3013 cdiag_fci[i].types[0].type = &t;
3014 cdiag_fci[i].pointer_count = 1;
3017 /* Handle the __gcc_cxxdiag__ format specifics. */
3018 if (!cxxdiag_fci)
3019 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
3020 cxxdiag_fci = (format_char_info *)
3021 xmemdup (gcc_cxxdiag_char_table,
3022 sizeof (gcc_cxxdiag_char_table),
3023 sizeof (gcc_cxxdiag_char_table));
3024 if (t)
3026 /* All specifiers taking a tree share the same struct. */
3027 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
3028 cxxdiag_fci[i].types[0].type = &t;
3029 cxxdiag_fci[i].pointer_count = 1;
3030 i = find_char_info_specifier_index (cxxdiag_fci, 'K');
3031 cxxdiag_fci[i].types[0].type = &t;
3032 cxxdiag_fci[i].pointer_count = 1;
3037 #ifdef TARGET_FORMAT_TYPES
3038 extern const format_kind_info TARGET_FORMAT_TYPES[];
3039 #endif
3041 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3042 extern const target_ovr_attr TARGET_OVERRIDES_FORMAT_ATTRIBUTES[];
3043 #endif
3044 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3045 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
3046 #endif
3048 /* Attributes such as "printf" are equivalent to those such as
3049 "gnu_printf" unless this is overridden by a target. */
3050 static const target_ovr_attr gnu_target_overrides_format_attributes[] =
3052 { "gnu_printf", "printf" },
3053 { "gnu_scanf", "scanf" },
3054 { "gnu_strftime", "strftime" },
3055 { "gnu_strfmon", "strfmon" },
3056 { NULL, NULL }
3059 /* Translate to unified attribute name. This is used in decode_format_type and
3060 decode_format_attr. In attr_name the user specified argument is passed. It
3061 returns the unified format name from TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3062 or the attr_name passed to this function, if there is no matching entry. */
3063 static const char *
3064 convert_format_name_to_system_name (const char *attr_name)
3066 int i;
3068 if (attr_name == NULL || *attr_name == 0
3069 || strncmp (attr_name, "gcc_", 4) == 0)
3070 return attr_name;
3071 #ifdef TARGET_OVERRIDES_FORMAT_INIT
3072 TARGET_OVERRIDES_FORMAT_INIT ();
3073 #endif
3075 #ifdef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
3076 /* Check if format attribute is overridden by target. */
3077 if (TARGET_OVERRIDES_FORMAT_ATTRIBUTES != NULL
3078 && TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT > 0)
3080 for (i = 0; i < TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT; ++i)
3082 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src,
3083 attr_name))
3084 return attr_name;
3085 if (cmp_attribs (TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_dst,
3086 attr_name))
3087 return TARGET_OVERRIDES_FORMAT_ATTRIBUTES[i].named_attr_src;
3090 #endif
3091 /* Otherwise default to gnu format. */
3092 for (i = 0;
3093 gnu_target_overrides_format_attributes[i].named_attr_src != NULL;
3094 ++i)
3096 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_src,
3097 attr_name))
3098 return attr_name;
3099 if (cmp_attribs (gnu_target_overrides_format_attributes[i].named_attr_dst,
3100 attr_name))
3101 return gnu_target_overrides_format_attributes[i].named_attr_src;
3104 return attr_name;
3107 /* Return true if TATTR_NAME and ATTR_NAME are the same format attribute,
3108 counting "name" and "__name__" as the same, false otherwise. */
3109 static bool
3110 cmp_attribs (const char *tattr_name, const char *attr_name)
3112 int alen = strlen (attr_name);
3113 int slen = (tattr_name ? strlen (tattr_name) : 0);
3114 if (alen > 4 && attr_name[0] == '_' && attr_name[1] == '_'
3115 && attr_name[alen - 1] == '_' && attr_name[alen - 2] == '_')
3117 attr_name += 2;
3118 alen -= 4;
3120 if (alen != slen || strncmp (tattr_name, attr_name, alen) != 0)
3121 return false;
3122 return true;
3125 /* Handle a "format" attribute; arguments as in
3126 struct attribute_spec.handler. */
3127 tree
3128 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
3129 int flags, bool *no_add_attrs)
3131 tree type = *node;
3132 function_format_info info;
3134 #ifdef TARGET_FORMAT_TYPES
3135 /* If the target provides additional format types, we need to
3136 add them to FORMAT_TYPES at first use. */
3137 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
3139 dynamic_format_types = XNEWVEC (format_kind_info,
3140 n_format_types + TARGET_N_FORMAT_TYPES);
3141 memcpy (dynamic_format_types, format_types_orig,
3142 sizeof (format_types_orig));
3143 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
3144 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
3146 format_types = dynamic_format_types;
3147 /* Provide a reference for the first potential external type. */
3148 first_target_format_type = n_format_types;
3149 n_format_types += TARGET_N_FORMAT_TYPES;
3151 #endif
3153 if (!decode_format_attr (args, &info, 0))
3155 *no_add_attrs = true;
3156 return NULL_TREE;
3159 if (prototype_p (type))
3161 if (!check_format_string (type, info.format_num, flags,
3162 no_add_attrs, info.format_type))
3163 return NULL_TREE;
3165 if (info.first_arg_num != 0)
3167 unsigned HOST_WIDE_INT arg_num = 1;
3168 function_args_iterator iter;
3169 tree arg_type;
3171 /* Verify that first_arg_num points to the last arg,
3172 the ... */
3173 FOREACH_FUNCTION_ARGS (type, arg_type, iter)
3174 arg_num++;
3176 if (arg_num != info.first_arg_num)
3178 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
3179 error ("args to be formatted is not %<...%>");
3180 *no_add_attrs = true;
3181 return NULL_TREE;
3186 /* Check if this is a strftime variant. Just for this variant
3187 FMT_FLAG_ARG_CONVERT is not set. */
3188 if ((format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) == 0
3189 && info.first_arg_num != 0)
3191 error ("strftime formats cannot format arguments");
3192 *no_add_attrs = true;
3193 return NULL_TREE;
3196 /* If this is a custom GCC-internal format type, we have to
3197 initialize certain bits at runtime. */
3198 if (info.format_type == asm_fprintf_format_type
3199 || info.format_type == gcc_gfc_format_type
3200 || info.format_type == gcc_diag_format_type
3201 || info.format_type == gcc_tdiag_format_type
3202 || info.format_type == gcc_cdiag_format_type
3203 || info.format_type == gcc_cxxdiag_format_type)
3205 /* Our first time through, we have to make sure that our
3206 format_type data is allocated dynamically and is modifiable. */
3207 if (!dynamic_format_types)
3208 format_types = dynamic_format_types = (format_kind_info *)
3209 xmemdup (format_types_orig, sizeof (format_types_orig),
3210 sizeof (format_types_orig));
3212 /* If this is format __asm_fprintf__, we have to initialize
3213 GCC's notion of HOST_WIDE_INT for checking %wd. */
3214 if (info.format_type == asm_fprintf_format_type)
3215 init_dynamic_asm_fprintf_info ();
3216 /* If this is format __gcc_gfc__, we have to initialize GCC's
3217 notion of 'locus' at runtime for %L. */
3218 else if (info.format_type == gcc_gfc_format_type)
3219 init_dynamic_gfc_info ();
3220 /* If this is one of the diagnostic attributes, then we have to
3221 initialize 'location_t' and 'tree' at runtime. */
3222 else if (info.format_type == gcc_diag_format_type
3223 || info.format_type == gcc_tdiag_format_type
3224 || info.format_type == gcc_cdiag_format_type
3225 || info.format_type == gcc_cxxdiag_format_type)
3226 init_dynamic_diag_info ();
3227 else
3228 gcc_unreachable ();
3231 return NULL_TREE;