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