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

source code of gcc/c-family/c-format.h