msformat-c.c revision 1.1
1/* Check calls to formatted I/O functions (-Wformat). 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2010 4 Free Software Foundation, Inc. 5 6This file is part of GCC. 7 8GCC is free software; you can redistribute it and/or modify it under 9the terms of the GNU General Public License as published by the Free 10Software Foundation; either version 3, or (at your option) any later 11version. 12 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY 14WARRANTY; without even the implied warranty of MERCHANTABILITY or 15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16for more details. 17 18You should have received a copy of the GNU General Public License 19along with GCC; see the file COPYING3. If not see 20<http://www.gnu.org/licenses/>. */ 21 22#include "config.h" 23#include "system.h" 24#include "coretypes.h" 25#include "tm.h" 26#include "tree.h" 27#include "flags.h" 28#include "c-common.h" 29#include "toplev.h" 30#include "intl.h" 31#include "diagnostic.h" 32#include "langhooks.h" 33#include "c-format.h" 34#include "alloc-pool.h" 35 36/* Mingw specific format attributes ms_printf, ms_scanf, and ms_strftime. */ 37 38static format_length_info ms_printf_length_specs[] = 39{ 40 { "h", FMT_LEN_h, STD_C89, NULL, FMT_LEN_none, STD_C89, 0 }, 41 { "l", FMT_LEN_l, STD_C89, NULL, FMT_LEN_none, STD_C89, 0 }, 42 { "I32", FMT_LEN_l, STD_EXT, NULL, FMT_LEN_none, STD_C89, 1 }, 43 { "I64", FMT_LEN_ll, STD_EXT, NULL, FMT_LEN_none, STD_C89, 1 }, 44 { "I", FMT_LEN_L, STD_EXT, NULL, FMT_LEN_none, STD_C89, 1 }, 45 { NULL, FMT_LEN_none, STD_C89, NULL, FMT_LEN_none, STD_C89, 0 } 46}; 47 48static const format_flag_spec ms_printf_flag_specs[] = 49{ 50 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 }, 51 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 }, 52 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 }, 53 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 }, 54 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 }, 55 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT }, 56 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 }, 57 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 }, 58 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 }, 59 { 0, 0, 0, NULL, NULL, STD_C89 } 60}; 61 62static const format_flag_pair ms_printf_flag_pairs[] = 63{ 64 { ' ', '+', 1, 0 }, 65 { '0', '-', 1, 0 }, { '0', 'p', 1, 'i' }, 66 { 0, 0, 0, 0 } 67}; 68 69static const format_flag_spec ms_scanf_flag_specs[] = 70{ 71 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 }, 72 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT }, 73 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 }, 74 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 }, 75 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT }, 76 { 0, 0, 0, NULL, NULL, STD_C89 } 77}; 78 79static const format_flag_pair ms_scanf_flag_pairs[] = 80{ 81 { '*', 'L', 0, 0 }, 82 { 0, 0, 0, 0 } 83}; 84 85static const format_flag_spec ms_strftime_flag_specs[] = 86{ 87 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT }, 88 { 0, 0, 0, NULL, NULL, STD_C89 } 89}; 90 91static const format_flag_pair ms_strftime_flag_pairs[] = 92{ 93 { 0, 0, 0, 0 } 94}; 95 96static const format_char_info ms_print_char_table[] = 97{ 98 /* C89 conversion specifiers. */ 99 { "di", 0, STD_C89, { T89_I, BADLEN, T89_S, T89_L, T9L_LL, T99_SST, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +'", "i", NULL }, 100 { "oxX", 0, STD_C89, { T89_UI, BADLEN, T89_US, T89_UL, T9L_ULL, T99_ST, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL }, 101 { "u", 0, STD_C89, { T89_UI, BADLEN, T89_US, T89_UL, T9L_ULL, T99_ST, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0'", "i", NULL }, 102 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "", NULL }, 103 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL }, 104 { "c", 0, STD_C89, { T89_I, BADLEN, T89_S, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL }, 105 { "s", 1, STD_C89, { T89_C, BADLEN, T89_S, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL }, 106 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL }, 107 { "n", 1, STD_C89, { T89_I, BADLEN, T89_S, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL }, 108 /* X/Open conversion specifiers. */ 109 { "C", 0, STD_EXT, { TEX_WI, BADLEN, T89_S, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL }, 110 { "S", 1, STD_EXT, { TEX_W, BADLEN, T89_S, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL }, 111 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL } 112}; 113 114static const format_char_info ms_scan_char_table[] = 115{ 116 /* C89 conversion specifiers. */ 117 { "di", 1, STD_C89, { T89_I, BADLEN, T89_S, T89_L, T9L_LL, T99_SST, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL }, 118 { "u", 1, STD_C89, { T89_UI, BADLEN, T89_US, T89_UL, T9L_ULL, T99_ST, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL }, 119 { "oxX", 1, STD_C89, { T89_UI, BADLEN, T89_US, T89_UL, T9L_ULL, T99_ST, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL }, 120 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL }, 121 { "c", 1, STD_C89, { T89_C, BADLEN, T89_S, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL }, 122 { "s", 1, STD_C89, { T89_C, BADLEN, T89_S, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL }, 123 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL }, 124 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL }, 125 { "n", 1, STD_C89, { T89_I, BADLEN, T89_S, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "W", NULL }, 126 /* X/Open conversion specifiers. */ 127 { "C", 1, STD_EXT, { TEX_W, BADLEN, T89_S, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL }, 128 { "S", 1, STD_EXT, { TEX_W, BADLEN, T89_S, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL }, 129 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL } 130}; 131 132static const format_char_info ms_time_char_table[] = 133{ 134 /* C89 conversion specifiers. */ 135 { "ABZab", 0, STD_C89, NOLENGTHS, "#", "", NULL }, 136 { "cx", 0, STD_C89, NOLENGTHS, "#", "3", NULL }, 137 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "#", "", NULL }, 138 { "j", 0, STD_C89, NOLENGTHS, "#", "", NULL }, 139 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL }, 140 { "X", 0, STD_C89, NOLENGTHS, "#", "", NULL }, 141 { "y", 0, STD_C89, NOLENGTHS, "#", "4", NULL }, 142 { "Y", 0, STD_C89, NOLENGTHS, "#", "", NULL }, 143 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL }, 144 /* C99 conversion specifiers. */ 145 { "z", 0, STD_C99, NOLENGTHS, "#", "", NULL }, 146 { NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL } 147}; 148 149EXPORTED_CONST format_kind_info mingw_format_attributes[3] = 150{ 151 { "ms_printf", ms_printf_length_specs, ms_print_char_table, " +#0-'", NULL, 152 ms_printf_flag_specs, ms_printf_flag_pairs, 153 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK, 154 'w', 0, 'p', 0, 'L', 0, 155 &integer_type_node, &integer_type_node 156 }, 157 { "ms_scanf", ms_printf_length_specs, ms_scan_char_table, "*'", NULL, 158 ms_scanf_flag_specs, ms_scanf_flag_pairs, 159 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK, 160 'w', 0, 0, '*', 'L', 0, 161 NULL, NULL 162 }, 163 { "ms_strftime", NULL, ms_time_char_table, "", "#", 164 ms_strftime_flag_specs, ms_strftime_flag_pairs, 165 FMT_FLAG_FANCY_PERCENT_OK, 0, 0, 0, 0, 0, 0, 166 NULL, NULL 167 } 168}; 169 170/* Default overrides for printf, scanf and strftime. */ 171EXPORTED_CONST target_ovr_attr mingw_format_attribute_overrides[4] = 172{ 173 { "ms_printf", "printf" }, 174 { "ms_scanf", "scanf" }, 175 { "ms_strftime", "strftime" } 176}; 177 178/* Setup for option Wpedantic-ms-format. */ 179 180#ifdef TARGET_OVERRIDES_FORMAT_INIT 181 182/* Make sure TARGET_OVERRIDES_FORMAT_INIT is prototyped. */ 183extern void TARGET_OVERRIDES_FORMAT_INIT (void); 184 185/* Helper. */ 186#define C89_OR_EXT (warn_pedantic_ms_format ? STD_EXT : STD_C89) 187 188void 189TARGET_OVERRIDES_FORMAT_INIT (void) 190{ 191 ms_printf_length_specs[2].std = C89_OR_EXT; /* I32 */ 192 ms_printf_length_specs[3].std = C89_OR_EXT; /* I64 */ 193 ms_printf_length_specs[4].std = C89_OR_EXT; /* I */ 194} 195 196#undef C89_OR_EXT 197 198#endif 199