xref: /dflybsd-src/contrib/gcc-8.0/gcc/c-family/c-format.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Check calls to formatted I/O functions (-Wformat).
2*38fd1498Szrj    Copyright (C) 1992-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #ifndef GCC_C_FORMAT_H
21*38fd1498Szrj #define GCC_C_FORMAT_H
22*38fd1498Szrj 
23*38fd1498Szrj /* The meaningfully distinct length modifiers for format checking recognized
24*38fd1498Szrj    by GCC.  */
25*38fd1498Szrj enum format_lengths
26*38fd1498Szrj {
27*38fd1498Szrj   FMT_LEN_none,
28*38fd1498Szrj   FMT_LEN_hh,
29*38fd1498Szrj   FMT_LEN_h,
30*38fd1498Szrj   FMT_LEN_l,
31*38fd1498Szrj   FMT_LEN_ll,
32*38fd1498Szrj   FMT_LEN_L,
33*38fd1498Szrj   FMT_LEN_z,
34*38fd1498Szrj   FMT_LEN_t,
35*38fd1498Szrj   FMT_LEN_j,
36*38fd1498Szrj   FMT_LEN_H,
37*38fd1498Szrj   FMT_LEN_D,
38*38fd1498Szrj   FMT_LEN_DD,
39*38fd1498Szrj   FMT_LEN_MAX
40*38fd1498Szrj };
41*38fd1498Szrj 
42*38fd1498Szrj 
43*38fd1498Szrj /* The standard versions in which various format features appeared.  */
44*38fd1498Szrj enum format_std_version
45*38fd1498Szrj {
46*38fd1498Szrj   STD_C89,
47*38fd1498Szrj   STD_C94,
48*38fd1498Szrj   STD_C9L, /* C99, but treat as C89 if -Wno-long-long.  */
49*38fd1498Szrj   STD_C99,
50*38fd1498Szrj   STD_EXT
51*38fd1498Szrj };
52*38fd1498Szrj 
53*38fd1498Szrj /* Flags that may apply to a particular kind of format checked by GCC.  */
54*38fd1498Szrj enum
55*38fd1498Szrj {
56*38fd1498Szrj   /* This format converts arguments of types determined by the
57*38fd1498Szrj      format string.  */
58*38fd1498Szrj   FMT_FLAG_ARG_CONVERT = 1,
59*38fd1498Szrj   /* The scanf allocation 'a' kludge applies to this format kind.  */
60*38fd1498Szrj   FMT_FLAG_SCANF_A_KLUDGE = 2,
61*38fd1498Szrj   /* A % during parsing a specifier is allowed to be a modified % rather
62*38fd1498Szrj      that indicating the format is broken and we are out-of-sync.  */
63*38fd1498Szrj   FMT_FLAG_FANCY_PERCENT_OK = 4,
64*38fd1498Szrj   /* With $ operand numbers, it is OK to reference the same argument more
65*38fd1498Szrj      than once.  */
66*38fd1498Szrj   FMT_FLAG_DOLLAR_MULTIPLE = 8,
67*38fd1498Szrj   /* This format type uses $ operand numbers (strfmon doesn't).  */
68*38fd1498Szrj   FMT_FLAG_USE_DOLLAR = 16,
69*38fd1498Szrj   /* Zero width is bad in this type of format (scanf).  */
70*38fd1498Szrj   FMT_FLAG_ZERO_WIDTH_BAD = 32,
71*38fd1498Szrj   /* Empty precision specification is OK in this type of format (printf).  */
72*38fd1498Szrj   FMT_FLAG_EMPTY_PREC_OK = 64,
73*38fd1498Szrj   /* Gaps are allowed in the arguments with $ operand numbers if all
74*38fd1498Szrj      arguments are pointers (scanf).  */
75*38fd1498Szrj   FMT_FLAG_DOLLAR_GAP_POINTER_OK = 128,
76*38fd1498Szrj   /* The format arg is an opaque object that will be parsed by an external
77*38fd1498Szrj      facility.  */
78*38fd1498Szrj   FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL = 256
79*38fd1498Szrj   /* Not included here: details of whether width or precision may occur
80*38fd1498Szrj      (controlled by width_char and precision_char); details of whether
81*38fd1498Szrj      '*' can be used for these (width_type and precision_type); details
82*38fd1498Szrj      of whether length modifiers can occur (length_char_specs).  */
83*38fd1498Szrj };
84*38fd1498Szrj 
85*38fd1498Szrj /* Structure describing a length modifier supported in format checking, and
86*38fd1498Szrj    possibly a doubled version such as "hh".  */
87*38fd1498Szrj struct format_length_info
88*38fd1498Szrj {
89*38fd1498Szrj   /* Name of the single-character length modifier. If prefixed by
90*38fd1498Szrj      a zero character, it describes a multi character length
91*38fd1498Szrj      modifier, like I64, I32, etc.  */
92*38fd1498Szrj   const char *name;
93*38fd1498Szrj   /* Index into a format_char_info.types array.  */
94*38fd1498Szrj   enum format_lengths index;
95*38fd1498Szrj   /* Standard version this length appears in.  */
96*38fd1498Szrj   enum format_std_version std;
97*38fd1498Szrj   /* Same, if the modifier can be repeated, or NULL if it can't.  */
98*38fd1498Szrj   const char *double_name;
99*38fd1498Szrj   enum format_lengths double_index;
100*38fd1498Szrj   enum format_std_version double_std;
101*38fd1498Szrj 
102*38fd1498Szrj   /* If this flag is set, just scalar width identity is checked, and
103*38fd1498Szrj      not the type identity itself.  */
104*38fd1498Szrj   int scalar_identity_flag;
105*38fd1498Szrj };
106*38fd1498Szrj 
107*38fd1498Szrj 
108*38fd1498Szrj /* Structure describing the combination of a conversion specifier
109*38fd1498Szrj    (or a set of specifiers which act identically) and a length modifier.  */
110*38fd1498Szrj struct format_type_detail
111*38fd1498Szrj {
112*38fd1498Szrj   /* The standard version this combination of length and type appeared in.
113*38fd1498Szrj      This is only relevant if greater than those for length and type
114*38fd1498Szrj      individually; otherwise it is ignored.  */
115*38fd1498Szrj   enum format_std_version std;
116*38fd1498Szrj   /* The name to use for the type, if different from that generated internally
117*38fd1498Szrj      (e.g., "signed size_t").  */
118*38fd1498Szrj   const char *name;
119*38fd1498Szrj   /* The type itself.  */
120*38fd1498Szrj   tree *type;
121*38fd1498Szrj };
122*38fd1498Szrj 
123*38fd1498Szrj 
124*38fd1498Szrj /* Macros to fill out tables of these.  */
125*38fd1498Szrj #define NOARGUMENTS	{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
126*38fd1498Szrj #define BADLEN	{ STD_C89, NULL, NULL }
127*38fd1498Szrj #define NOLENGTHS	{ BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
128*38fd1498Szrj 
129*38fd1498Szrj 
130*38fd1498Szrj /* Structure describing a format conversion specifier (or a set of specifiers
131*38fd1498Szrj    which act identically), and the length modifiers used with it.  */
132*38fd1498Szrj struct format_char_info
133*38fd1498Szrj {
134*38fd1498Szrj   const char *format_chars;
135*38fd1498Szrj   int pointer_count;
136*38fd1498Szrj   enum format_std_version std;
137*38fd1498Szrj   /* Types accepted for each length modifier.  */
138*38fd1498Szrj   format_type_detail types[FMT_LEN_MAX];
139*38fd1498Szrj   /* List of other modifier characters allowed with these specifiers.
140*38fd1498Szrj      This lists flags, and additionally "w" for width, "p" for precision
141*38fd1498Szrj      (right precision, for strfmon), "#" for left precision (strfmon),
142*38fd1498Szrj      "a" for scanf "a" allocation extension (not applicable in C99 mode),
143*38fd1498Szrj      "*" for scanf suppression, and "E" and "O" for those strftime
144*38fd1498Szrj      modifiers.  */
145*38fd1498Szrj   const char *flag_chars;
146*38fd1498Szrj   /* List of additional flags describing these conversion specifiers.
147*38fd1498Szrj      "c" for generic character pointers being allowed, "2" for strftime
148*38fd1498Szrj      two digit year formats, "3" for strftime formats giving two digit
149*38fd1498Szrj      years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
150*38fd1498Szrj      "o" if use of strftime "O" is a GNU extension beyond C99,
151*38fd1498Szrj      "W" if the argument is a pointer which is dereferenced and written into,
152*38fd1498Szrj      "R" if the argument is a pointer which is dereferenced and read from,
153*38fd1498Szrj      "i" for printf integer formats where the '0' flag is ignored with
154*38fd1498Szrj      precision, and "[" for the starting character of a scanf scanset,
155*38fd1498Szrj      "<" if the specifier introduces a quoted sequence (such as "%<"),
156*38fd1498Szrj      ">" if the specifier terminates a quoted sequence (such as "%>"),
157*38fd1498Szrj      "[" if the specifier introduces a color sequence (such as "%r"),
158*38fd1498Szrj      "]" if the specifier terminates a color sequence (such as "%R"),
159*38fd1498Szrj      "'" (single quote) if the specifier is expected to be quoted when
160*38fd1498Szrj      it appears outside a quoted sequence and unquoted otherwise (such
161*38fd1498Szrj      as the GCC internal printf format directive "%T"), and
162*38fd1498Szrj      "\"" (double quote) if the specifier is not expected to appear in
163*38fd1498Szrj      a quoted sequence (such as the GCC internal format directive "%K".  */
164*38fd1498Szrj   const char *flags2;
165*38fd1498Szrj   /* If this format conversion character consumes more than one argument,
166*38fd1498Szrj      CHAIN points to information about the next argument.  For later
167*38fd1498Szrj      arguments, only POINTER_COUNT, TYPES, and the "c", "R", and "W" flags
168*38fd1498Szrj      in FLAGS2 are used.  */
169*38fd1498Szrj   const struct format_char_info *chain;
170*38fd1498Szrj };
171*38fd1498Szrj 
172*38fd1498Szrj 
173*38fd1498Szrj /* Structure describing a flag accepted by some kind of format.  */
174*38fd1498Szrj struct format_flag_spec
175*38fd1498Szrj {
176*38fd1498Szrj   /* The flag character in question (0 for end of array).  */
177*38fd1498Szrj   int flag_char;
178*38fd1498Szrj   /* Zero if this entry describes the flag character in general, or a
179*38fd1498Szrj      nonzero character that may be found in flags2 if it describes the
180*38fd1498Szrj      flag when used with certain formats only.  If the latter, only
181*38fd1498Szrj      the first such entry found that applies to the current conversion
182*38fd1498Szrj      specifier is used; the values of 'name' and 'long_name' it supplies
183*38fd1498Szrj      will be used, if non-NULL and the standard version is higher than
184*38fd1498Szrj      the unpredicated one, for any pedantic warning.  For example, 'o'
185*38fd1498Szrj      for strftime formats (meaning 'O' is an extension over C99).  */
186*38fd1498Szrj   int predicate;
187*38fd1498Szrj   /* Nonzero if the next character after this flag in the format should
188*38fd1498Szrj      be skipped ('=' in strfmon), zero otherwise.  */
189*38fd1498Szrj   int skip_next_char;
190*38fd1498Szrj   /* True if the flag introduces quoting (as in GCC's %qE).  */
191*38fd1498Szrj   bool quoting;
192*38fd1498Szrj   /* The name to use for this flag in diagnostic messages.  For example,
193*38fd1498Szrj      N_("'0' flag"), N_("field width").  */
194*38fd1498Szrj   const char *name;
195*38fd1498Szrj   /* Long name for this flag in diagnostic messages; currently only used for
196*38fd1498Szrj      "ISO C does not support ...".  For example, N_("the 'I' printf flag").  */
197*38fd1498Szrj   const char *long_name;
198*38fd1498Szrj   /* The standard version in which it appeared.  */
199*38fd1498Szrj   enum format_std_version std;
200*38fd1498Szrj };
201*38fd1498Szrj 
202*38fd1498Szrj 
203*38fd1498Szrj /* Structure describing a combination of flags that is bad for some kind
204*38fd1498Szrj    of format.  */
205*38fd1498Szrj struct format_flag_pair
206*38fd1498Szrj {
207*38fd1498Szrj   /* The first flag character in question (0 for end of array).  */
208*38fd1498Szrj   int flag_char1;
209*38fd1498Szrj   /* The second flag character.  */
210*38fd1498Szrj   int flag_char2;
211*38fd1498Szrj   /* Nonzero if the message should say that the first flag is ignored with
212*38fd1498Szrj      the second, zero if the combination should simply be objected to.  */
213*38fd1498Szrj   int ignored;
214*38fd1498Szrj   /* Zero if this entry applies whenever this flag combination occurs,
215*38fd1498Szrj      a nonzero character from flags2 if it only applies in some
216*38fd1498Szrj      circumstances (e.g. 'i' for printf formats ignoring 0 with precision).  */
217*38fd1498Szrj   int predicate;
218*38fd1498Szrj };
219*38fd1498Szrj 
220*38fd1498Szrj 
221*38fd1498Szrj /* Structure describing a particular kind of format processed by GCC.  */
222*38fd1498Szrj struct format_kind_info
223*38fd1498Szrj {
224*38fd1498Szrj   /* The name of this kind of format, for use in diagnostics.  Also
225*38fd1498Szrj      the name of the attribute (without preceding and following __).  */
226*38fd1498Szrj   const char *name;
227*38fd1498Szrj   /* Specifications of the length modifiers accepted; possibly NULL.  */
228*38fd1498Szrj   const format_length_info *length_char_specs;
229*38fd1498Szrj   /* Details of the conversion specification characters accepted.  */
230*38fd1498Szrj   const format_char_info *conversion_specs;
231*38fd1498Szrj   /* String listing the flag characters that are accepted.  */
232*38fd1498Szrj   const char *flag_chars;
233*38fd1498Szrj   /* String listing modifier characters (strftime) accepted.  May be NULL.  */
234*38fd1498Szrj   const char *modifier_chars;
235*38fd1498Szrj   /* Details of the flag characters, including pseudo-flags.  */
236*38fd1498Szrj   const format_flag_spec *flag_specs;
237*38fd1498Szrj   /* Details of bad combinations of flags.  */
238*38fd1498Szrj   const format_flag_pair *bad_flag_pairs;
239*38fd1498Szrj   /* Flags applicable to this kind of format.  */
240*38fd1498Szrj   int flags;
241*38fd1498Szrj   /* Flag character to treat a width as, or 0 if width not used.  */
242*38fd1498Szrj   int width_char;
243*38fd1498Szrj   /* Flag character to treat a left precision (strfmon) as,
244*38fd1498Szrj      or 0 if left precision not used.  */
245*38fd1498Szrj   int left_precision_char;
246*38fd1498Szrj   /* Flag character to treat a precision (for strfmon, right precision) as,
247*38fd1498Szrj      or 0 if precision not used.  */
248*38fd1498Szrj   int precision_char;
249*38fd1498Szrj   /* If a flag character has the effect of suppressing the conversion of
250*38fd1498Szrj      an argument ('*' in scanf), that flag character, otherwise 0.  */
251*38fd1498Szrj   int suppression_char;
252*38fd1498Szrj   /* Flag character to treat a length modifier as (ignored if length
253*38fd1498Szrj      modifiers not used).  Need not be placed in flag_chars for conversion
254*38fd1498Szrj      specifiers, but is used to check for bad combinations such as length
255*38fd1498Szrj      modifier with assignment suppression in scanf.  */
256*38fd1498Szrj   int length_code_char;
257*38fd1498Szrj   /* Assignment-allocation flag character ('m' in scanf), otherwise 0.  */
258*38fd1498Szrj   int alloc_char;
259*38fd1498Szrj   /* Pointer to type of argument expected if '*' is used for a width,
260*38fd1498Szrj      or NULL if '*' not used for widths.  */
261*38fd1498Szrj   tree *width_type;
262*38fd1498Szrj   /* Pointer to type of argument expected if '*' is used for a precision,
263*38fd1498Szrj      or NULL if '*' not used for precisions.  */
264*38fd1498Szrj   tree *precision_type;
265*38fd1498Szrj };
266*38fd1498Szrj 
267*38fd1498Szrj #define T_I	&integer_type_node
268*38fd1498Szrj #define T89_I	{ STD_C89, NULL, T_I }
269*38fd1498Szrj #define T_L	&long_integer_type_node
270*38fd1498Szrj #define T89_L	{ STD_C89, NULL, T_L }
271*38fd1498Szrj #define T_LL	&long_long_integer_type_node
272*38fd1498Szrj #define T9L_LL	{ STD_C9L, NULL, T_LL }
273*38fd1498Szrj #define TEX_LL	{ STD_EXT, NULL, T_LL }
274*38fd1498Szrj #define T_S	&short_integer_type_node
275*38fd1498Szrj #define T89_S	{ STD_C89, NULL, T_S }
276*38fd1498Szrj #define T_UI	&unsigned_type_node
277*38fd1498Szrj #define T89_UI	{ STD_C89, NULL, T_UI }
278*38fd1498Szrj #define T_UL	&long_unsigned_type_node
279*38fd1498Szrj #define T89_UL	{ STD_C89, NULL, T_UL }
280*38fd1498Szrj #define T_ULL	&long_long_unsigned_type_node
281*38fd1498Szrj #define T9L_ULL	{ STD_C9L, NULL, T_ULL }
282*38fd1498Szrj #define TEX_ULL	{ STD_EXT, NULL, T_ULL }
283*38fd1498Szrj #define T_US	&short_unsigned_type_node
284*38fd1498Szrj #define T89_US	{ STD_C89, NULL, T_US }
285*38fd1498Szrj #define T_F	&float_type_node
286*38fd1498Szrj #define T89_F	{ STD_C89, NULL, T_F }
287*38fd1498Szrj #define T99_F	{ STD_C99, NULL, T_F }
288*38fd1498Szrj #define T_D	&double_type_node
289*38fd1498Szrj #define T89_D	{ STD_C89, NULL, T_D }
290*38fd1498Szrj #define T99_D	{ STD_C99, NULL, T_D }
291*38fd1498Szrj #define T_LD	&long_double_type_node
292*38fd1498Szrj #define T89_LD	{ STD_C89, NULL, T_LD }
293*38fd1498Szrj #define T99_LD	{ STD_C99, NULL, T_LD }
294*38fd1498Szrj #define T_C	&char_type_node
295*38fd1498Szrj #define T89_C	{ STD_C89, NULL, T_C }
296*38fd1498Szrj #define T_SC	&signed_char_type_node
297*38fd1498Szrj #define T99_SC	{ STD_C99, NULL, T_SC }
298*38fd1498Szrj #define T_UC	&unsigned_char_type_node
299*38fd1498Szrj #define T99_UC	{ STD_C99, NULL, T_UC }
300*38fd1498Szrj #define T_V	&void_type_node
301*38fd1498Szrj #define T89_G   { STD_C89, NULL, &local_gcall_ptr_node }
302*38fd1498Szrj #define T89_T   { STD_C89, NULL, &local_tree_type_node }
303*38fd1498Szrj #define T89_V	{ STD_C89, NULL, T_V }
304*38fd1498Szrj #define T_W	&wchar_type_node
305*38fd1498Szrj #define T94_W	{ STD_C94, "wchar_t", T_W }
306*38fd1498Szrj #define TEX_W	{ STD_EXT, "wchar_t", T_W }
307*38fd1498Szrj #define T_WI	&wint_type_node
308*38fd1498Szrj #define T94_WI	{ STD_C94, "wint_t", T_WI }
309*38fd1498Szrj #define TEX_WI	{ STD_EXT, "wint_t", T_WI }
310*38fd1498Szrj #define T_ST    &size_type_node
311*38fd1498Szrj #define T99_ST	{ STD_C99, "size_t", T_ST }
312*38fd1498Szrj #define T_SST   &signed_size_type_node
313*38fd1498Szrj #define T99_SST	{ STD_C99, "signed size_t", T_SST }
314*38fd1498Szrj #define T_PD    &ptrdiff_type_node
315*38fd1498Szrj #define T99_PD	{ STD_C99, "ptrdiff_t", T_PD }
316*38fd1498Szrj #define T_UPD   &unsigned_ptrdiff_type_node
317*38fd1498Szrj #define T99_UPD	{ STD_C99, "unsigned ptrdiff_t", T_UPD }
318*38fd1498Szrj #define T_IM    &intmax_type_node
319*38fd1498Szrj #define T99_IM	{ STD_C99, "intmax_t", T_IM }
320*38fd1498Szrj #define T_UIM   &uintmax_type_node
321*38fd1498Szrj #define T99_UIM	{ STD_C99, "uintmax_t", T_UIM }
322*38fd1498Szrj #define T_D32   &dfloat32_type_node
323*38fd1498Szrj #define TEX_D32 { STD_EXT, "_Decimal32", T_D32 }
324*38fd1498Szrj #define T_D64   &dfloat64_type_node
325*38fd1498Szrj #define TEX_D64 { STD_EXT, "_Decimal64", T_D64 }
326*38fd1498Szrj #define T_D128  &dfloat128_type_node
327*38fd1498Szrj #define TEX_D128 { STD_EXT, "_Decimal128", T_D128 }
328*38fd1498Szrj 
329*38fd1498Szrj /* Structure describing how format attributes such as "printf" are
330*38fd1498Szrj    interpreted as "gnu_printf" or "ms_printf" on a particular system.
331*38fd1498Szrj    TARGET_OVERRIDES_FORMAT_ATTRIBUTES is used to specify target-specific
332*38fd1498Szrj    defaults.  */
333*38fd1498Szrj struct target_ovr_attr
334*38fd1498Szrj {
335*38fd1498Szrj   /* The name of the to be copied format attribute. */
336*38fd1498Szrj   const char *named_attr_src;
337*38fd1498Szrj   /* The name of the to be overridden format attribute. */
338*38fd1498Szrj   const char *named_attr_dst;
339*38fd1498Szrj };
340*38fd1498Szrj 
341*38fd1498Szrj #endif /* GCC_C_FORMAT_H */
342