OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [c-family/] [c-format.c] - Blame information for rev 780

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.