xref: /netbsd-src/external/gpl3/gcc/dist/gcc/config/i386/msformat-c.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 /* Check calls to formatted I/O functions (-Wformat).
2    Copyright (C) 1992-2022 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 #define IN_TARGET_CODE 1
21 
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "c-family/c-common.h"
27 #include "intl.h"
28 #include "c-family/c-format.h"
29 
30 /* Mingw specific format attributes ms_printf, ms_scanf, and ms_strftime.  */
31 
32 static format_length_info ms_printf_length_specs[] =
33 {
34   { "h", FMT_LEN_h, STD_C89, NULL, FMT_LEN_none, STD_C89, 0 },
35   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
36   { "L", FMT_LEN_L, STD_C89, NULL, FMT_LEN_none, STD_C89, 1 },
37   { "I32", FMT_LEN_l, STD_EXT, NULL, FMT_LEN_none, STD_C89, 1 },
38   { "I64", FMT_LEN_ll, STD_EXT, NULL, FMT_LEN_none, STD_C89, 1 },
39   { "I", FMT_LEN_z, STD_EXT, NULL, FMT_LEN_none, STD_C89, 1 },
40   { NULL, FMT_LEN_none, STD_C89, NULL, FMT_LEN_none, STD_C89, 0 }
41 };
42 
43 static const format_flag_spec ms_printf_flag_specs[] =
44 {
45   { ' ',  0, 0, 0, N_("' ' flag"),        N_("the ' ' printf flag"),              STD_C89 },
46   { '+',  0, 0, 0, N_("'+' flag"),        N_("the '+' printf flag"),              STD_C89 },
47   { '#',  0, 0, 0, N_("'#' flag"),        N_("the '#' printf flag"),              STD_C89 },
48   { '0',  0, 0, 0, N_("'0' flag"),        N_("the '0' printf flag"),              STD_C89 },
49   { '-',  0, 0, 0, N_("'-' flag"),        N_("the '-' printf flag"),              STD_C89 },
50   { '\'', 0, 0, 0, N_("''' flag"),        N_("the ''' printf flag"),              STD_EXT },
51   { 'w',  0, 0, 0, N_("field width"),     N_("field width in printf format"),     STD_C89 },
52   { 'p',  0, 0, 0, N_("precision"),       N_("precision in printf format"),       STD_C89 },
53   { 'L',  0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
54   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
55 };
56 
57 static const format_flag_pair ms_printf_flag_pairs[] =
58 {
59   { ' ', '+', 1, 0   },
60   { '0', '-', 1, 0   }, { '0', 'p', 1, 'i' },
61   { 0, 0, 0, 0 }
62 };
63 
64 static const format_flag_spec ms_scanf_flag_specs[] =
65 {
66   { '*',  0, 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
67   { 'a',  0, 0, 0, N_("'a' flag"),               N_("the 'a' scanf flag"),                       STD_EXT },
68   { 'w',  0, 0, 0, N_("field width"),            N_("field width in scanf format"),              STD_C89 },
69   { 'L',  0, 0, 0, N_("length modifier"),        N_("length modifier in scanf format"),          STD_C89 },
70   { '\'', 0, 0, 0, N_("''' flag"),               N_("the ''' scanf flag"),                       STD_EXT },
71   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
72 };
73 
74 static const format_flag_pair ms_scanf_flag_pairs[] =
75 {
76   { '*', 'L', 0, 0 },
77   { 0, 0, 0, 0 }
78 };
79 
80 static const format_flag_spec ms_strftime_flag_specs[] =
81 {
82   { '#', 0,   0, 0, N_("'#' flag"),     N_("the '#' strftime flag"),          STD_EXT },
83   { 0, 0, 0, 0, NULL, NULL, STD_C89 }
84 };
85 
86 static const format_flag_pair ms_strftime_flag_pairs[] =
87 {
88   { 0, 0, 0, 0 }
89 };
90 
91 static const format_char_info ms_print_char_table[] =
92 {
93   /* C89 conversion specifiers.  */
94   { "di",  0, STD_C89, { T89_I,   BADLEN,  T89_S,   T89_L,   T9L_LL,  BADLEN, T99_SST, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0 +'",  "i",  NULL },
95   { "oxX", 0, STD_C89, { T89_UI,  BADLEN,  T89_US,  T89_UL,  T9L_ULL, BADLEN, T99_ST,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0#",    "i",  NULL },
96   { "u",   0, STD_C89, { T89_UI,  BADLEN,  T89_US,  T89_UL,  T9L_ULL, BADLEN, T99_ST,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0'",    "i",  NULL },
97   { "fgG", 0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_D,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0 +#'", "",   NULL },
98   { "eE",  0, STD_C89, { T89_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T89_D,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0 +#",  "",   NULL },
99   { "c",   0, STD_C89, { T89_I,   BADLEN,  T89_S,   T94_WI,  BADLEN,  BADLEN, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-w",       "",   NULL },
100   { "s",   1, STD_C89, { T89_C,   BADLEN,  T89_S,   T94_W,   BADLEN,  BADLEN, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp",      "cR", NULL },
101   { "p",   1, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-w",       "c",  NULL },
102   { "n",   1, STD_C89, { T89_I,   BADLEN,  T89_S,   T89_L,   T9L_LL,  BADLEN, BADLEN,  BADLEN,  T99_IM,  BADLEN,  BADLEN,  BADLEN }, "",         "W",  NULL },
103   /* C99 conversion specifiers.  */
104   { "aA",  0, STD_C99, { T99_D,   BADLEN,  BADLEN,  T99_D,   BADLEN,  T99_D,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp0 +#",  "",   NULL },
105   /* X/Open conversion specifiers.  */
106   { "C",   0, STD_EXT, { TEX_WI,  BADLEN,  T89_S,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-w",       "",   NULL },
107   { "S",   1, STD_EXT, { TEX_W,   BADLEN,  T89_S,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "-wp",      "R",  NULL },
108   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
109 };
110 
111 static const format_char_info ms_scan_char_table[] =
112 {
113   /* C89 conversion specifiers.  */
114   { "di",    1, STD_C89, { T89_I,   BADLEN,  T89_S,   T89_L,   T9L_LL,  BADLEN,  T99_SST, BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w'", "W",   NULL },
115   { "u",     1, STD_C89, { T89_UI,  BADLEN,  T89_US,  T89_UL,  T9L_ULL, BADLEN,  T99_ST,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w'", "W",   NULL },
116   { "oxX",   1, STD_C89, { T89_UI,  BADLEN,  T89_US,  T89_UL,  T9L_ULL, BADLEN,  T99_ST,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w",  "W",   NULL },
117   { "efgEG", 1, STD_C89, { T89_F,   BADLEN,  BADLEN,  T89_D,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w'", "W",   NULL },
118   { "c",     1, STD_C89, { T89_C,   BADLEN,  T89_S,   T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w",  "cW",  NULL },
119   { "s",     1, STD_C89, { T89_C,   BADLEN,  T89_S,   T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*aw", "cW",  NULL },
120   { "[",     1, STD_C89, { T89_C,   BADLEN,  BADLEN,  T94_W,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*aw", "cW[", NULL },
121   { "p",     2, STD_C89, { T89_V,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w",  "W",   NULL },
122   { "n",     1, STD_C89, { T89_I,   BADLEN,  T89_S,   T89_L,   T9L_LL,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "",    "W",   NULL },
123   /* X/Open conversion specifiers.  */
124   { "C",     1, STD_EXT, { TEX_W,   BADLEN,  T89_S,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*w",   "W",   NULL },
125   { "S",     1, STD_EXT, { TEX_W,   BADLEN,  T89_S,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN }, "*aw",  "W",   NULL },
126   { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
127 };
128 
129 static const format_char_info ms_time_char_table[] =
130 {
131   /* C89 conversion specifiers.  */
132   { "ABZab",		0, STD_C89, NOLENGTHS, "#",     "",   NULL },
133   { "cx",		0, STD_C89, NOLENGTHS, "#",      "3",  NULL },
134   { "HIMSUWdmw",	0, STD_C89, NOLENGTHS, "#",  "",   NULL },
135   { "j",		0, STD_C89, NOLENGTHS, "#",  "",  NULL },
136   { "p",		0, STD_C89, NOLENGTHS, "#",      "",   NULL },
137   { "X",		0, STD_C89, NOLENGTHS, "#",      "",   NULL },
138   { "y",		0, STD_C89, NOLENGTHS, "#", "4",  NULL },
139   { "Y",		0, STD_C89, NOLENGTHS, "#", "",  NULL },
140   { "%",		0, STD_C89, NOLENGTHS, "",       "",   NULL },
141   /* C99 conversion specifiers.  */
142   { "z",		0, STD_C99, NOLENGTHS, "#",      "",  NULL },
143   { NULL,		0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
144 };
145 
146 EXPORTED_CONST format_kind_info mingw_format_attributes[3] =
147 {
148   { "ms_printf",   ms_printf_length_specs,  ms_print_char_table, " +#0-'", NULL,
149     ms_printf_flag_specs, ms_printf_flag_pairs,
150     FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
151     'w', 0, 'p', 0, 'L', 0,
152     &integer_type_node, &integer_type_node
153   },
154   { "ms_scanf",    ms_printf_length_specs,   ms_scan_char_table,  "*'", NULL,
155     ms_scanf_flag_specs, ms_scanf_flag_pairs,
156     FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
157     'w', 0, 0, '*', 'L', 0,
158     NULL, NULL
159   },
160   { "ms_strftime", NULL,                 ms_time_char_table,  "", "#",
161     ms_strftime_flag_specs, ms_strftime_flag_pairs,
162     FMT_FLAG_FANCY_PERCENT_OK, 0, 0, 0, 0, 0, 0,
163     NULL, NULL
164   }
165 };
166 
167 /* Default overrides for printf, scanf and strftime.  */
168 EXPORTED_CONST target_ovr_attr mingw_format_attribute_overrides[4] =
169 {
170   { "ms_printf", "printf" },
171   { "ms_scanf", "scanf" },
172   { "ms_strftime", "strftime" }
173 };
174 
175 /* Setup for option Wpedantic-ms-format.  */
176 
177 #ifdef TARGET_OVERRIDES_FORMAT_INIT
178 
179 /* Make sure TARGET_OVERRIDES_FORMAT_INIT is prototyped.  */
180 extern void TARGET_OVERRIDES_FORMAT_INIT (void);
181 
182 /* Helper.  */
183 #define C89_OR_EXT (warn_pedantic_ms_format ? STD_EXT : STD_C89)
184 
185 void
TARGET_OVERRIDES_FORMAT_INIT(void)186 TARGET_OVERRIDES_FORMAT_INIT (void)
187 {
188   ms_printf_length_specs[3].std = C89_OR_EXT; /* I32 */
189   ms_printf_length_specs[4].std = C89_OR_EXT; /* I64 */
190   ms_printf_length_specs[5].std = C89_OR_EXT; /* I */
191 }
192 
193 #undef C89_OR_EXT
194 
195 #endif
196