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