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