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