00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "system.h"
00024 #include "coretypes.h"
00025 #include "tm.h"
00026 #include "tree.h"
00027 #include "flags.h"
00028 #include "c-common.h"
00029 #include "toplev.h"
00030 #include "intl.h"
00031 #include "diagnostic.h"
00032 #include "langhooks.h"
00033 #include "c-format.h"
00034
00035
00036
00037 void
00038 set_Wformat (int setting)
00039 {
00040 warn_format = setting;
00041 warn_format_extra_args = setting;
00042 warn_format_zero_length = setting;
00043 if (setting != 1)
00044 {
00045 warn_format_nonliteral = setting;
00046 warn_format_security = setting;
00047 warn_format_y2k = setting;
00048 }
00049
00050 if (setting)
00051 warn_nonnull = setting;
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 enum format_type { printf_format_type, asm_fprintf_format_type,
00061 gcc_diag_format_type, gcc_tdiag_format_type,
00062 gcc_cdiag_format_type,
00063 gcc_cxxdiag_format_type, gcc_gfc_format_type,
00064 scanf_format_type, strftime_format_type,
00065 strfmon_format_type, format_type_error = -1};
00066
00067 typedef struct function_format_info
00068 {
00069 int format_type;
00070 unsigned HOST_WIDE_INT format_num;
00071 unsigned HOST_WIDE_INT first_arg_num;
00072 } function_format_info;
00073
00074 static bool decode_format_attr (tree, function_format_info *, int);
00075 static int decode_format_type (const char *);
00076
00077 static bool check_format_string (tree argument,
00078 unsigned HOST_WIDE_INT format_num,
00079 int flags, bool *no_add_attrs);
00080 static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
00081 int validated_p);
00082
00083
00084
00085
00086 tree
00087 handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
00088 tree args, int flags, bool *no_add_attrs)
00089 {
00090 tree type = *node;
00091 tree format_num_expr = TREE_VALUE (args);
00092 unsigned HOST_WIDE_INT format_num = 0;
00093 tree argument;
00094
00095 if (!get_constant (format_num_expr, &format_num, 0))
00096 {
00097 error ("format string has invalid operand number");
00098 *no_add_attrs = true;
00099 return NULL_TREE;
00100 }
00101
00102 argument = TYPE_ARG_TYPES (type);
00103 if (argument)
00104 {
00105 if (!check_format_string (argument, format_num, flags, no_add_attrs))
00106 return NULL_TREE;
00107 }
00108
00109 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
00110 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
00111 != char_type_node))
00112 {
00113 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
00114 error ("function does not return string type");
00115 *no_add_attrs = true;
00116 return NULL_TREE;
00117 }
00118
00119 return NULL_TREE;
00120 }
00121
00122
00123
00124 static bool
00125 check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
00126 int flags, bool *no_add_attrs)
00127 {
00128 unsigned HOST_WIDE_INT i;
00129
00130 for (i = 1; i != format_num; i++)
00131 {
00132 if (argument == 0)
00133 break;
00134 argument = TREE_CHAIN (argument);
00135 }
00136
00137 if (!argument
00138 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
00139 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
00140 != char_type_node))
00141 {
00142 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
00143 error ("format string argument not a string type");
00144 *no_add_attrs = true;
00145 return false;
00146 }
00147
00148 return true;
00149 }
00150
00151
00152
00153
00154 static bool
00155 get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
00156 {
00157 if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
00158 {
00159 gcc_assert (!validated_p);
00160 return false;
00161 }
00162
00163 *value = TREE_INT_CST_LOW (expr);
00164
00165 return true;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175 static bool
00176 decode_format_attr (tree args, function_format_info *info, int validated_p)
00177 {
00178 tree format_type_id = TREE_VALUE (args);
00179 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
00180 tree first_arg_num_expr
00181 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
00182
00183 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
00184 {
00185 gcc_assert (!validated_p);
00186 error ("unrecognized format specifier");
00187 return false;
00188 }
00189 else
00190 {
00191 const char *p = IDENTIFIER_POINTER (format_type_id);
00192
00193 info->format_type = decode_format_type (p);
00194
00195 if (info->format_type == format_type_error)
00196 {
00197 gcc_assert (!validated_p);
00198 warning (OPT_Wformat, "%qE is an unrecognized format function type",
00199 format_type_id);
00200 return false;
00201 }
00202 }
00203
00204 if (!get_constant (format_num_expr, &info->format_num, validated_p))
00205 {
00206 error ("format string has invalid operand number");
00207 return false;
00208 }
00209
00210 if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
00211 {
00212 error ("%<...%> has invalid operand number");
00213 return false;
00214 }
00215
00216 if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
00217 {
00218 gcc_assert (!validated_p);
00219 error ("format string argument follows the args to be formatted");
00220 return false;
00221 }
00222
00223 return true;
00224 }
00225
00226
00227
00228
00229
00230 #define CPLUSPLUS_STD_VER STD_C94
00231
00232 #define C_STD_VER ((int) (c_dialect_cxx () \
00233 ? CPLUSPLUS_STD_VER \
00234 : (flag_isoc99 \
00235 ? STD_C99 \
00236 : (flag_isoc94 ? STD_C94 : STD_C89))))
00237
00238
00239
00240 #define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
00241 ? "ISO C++" \
00242 : ((FEATURE_VER) == STD_EXT \
00243 ? "ISO C" \
00244 : "ISO C90"))
00245
00246
00247 #define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
00248 ? (warn_long_long ? STD_C99 : STD_C89) \
00249 : (VER)))
00250
00251
00252
00253 typedef struct format_wanted_type
00254 {
00255
00256 tree wanted_type;
00257
00258 const char *wanted_type_name;
00259
00260 int pointer_count;
00261
00262
00263 int char_lenient_flag;
00264
00265
00266 int writing_in_flag;
00267
00268
00269 int reading_from_flag;
00270
00271
00272
00273
00274 const char *name;
00275
00276 tree param;
00277
00278 int arg_num;
00279
00280 struct format_wanted_type *next;
00281 } format_wanted_type;
00282
00283
00284 static const format_length_info printf_length_specs[] =
00285 {
00286 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
00287 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
00288 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
00289 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
00290 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
00291 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
00292 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
00293 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
00294 { "H", FMT_LEN_H, STD_EXT, NULL, 0, 0 },
00295 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT },
00296 { NULL, 0, 0, NULL, 0, 0 }
00297 };
00298
00299
00300 static const format_length_info asm_fprintf_length_specs[] =
00301 {
00302 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
00303 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
00304 { NULL, 0, 0, NULL, 0, 0 }
00305 };
00306
00307
00308 static const format_length_info gcc_diag_length_specs[] =
00309 {
00310 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
00311 { "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
00312 { NULL, 0, 0, NULL, 0, 0 }
00313 };
00314
00315
00316 #define gcc_tdiag_length_specs gcc_diag_length_specs
00317 #define gcc_cdiag_length_specs gcc_diag_length_specs
00318 #define gcc_cxxdiag_length_specs gcc_diag_length_specs
00319
00320
00321 static const format_length_info scanf_length_specs[] =
00322 {
00323 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
00324 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
00325 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
00326 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
00327 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
00328 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
00329 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
00330 { "H", FMT_LEN_H, STD_EXT, NULL, 0, 0 },
00331 { "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT },
00332 { NULL, 0, 0, NULL, 0, 0 }
00333 };
00334
00335
00336
00337
00338 static const format_length_info strfmon_length_specs[] =
00339 {
00340
00341 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
00342 { NULL, 0, 0, NULL, 0, 0 }
00343 };
00344
00345 static const format_flag_spec printf_flag_specs[] =
00346 {
00347 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
00348 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
00349 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
00350 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
00351 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
00352 { '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
00353 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
00354 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
00355 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
00356 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
00357 { 0, 0, 0, NULL, NULL, 0 }
00358 };
00359
00360
00361 static const format_flag_pair printf_flag_pairs[] =
00362 {
00363 { ' ', '+', 1, 0 },
00364 { '0', '-', 1, 0 },
00365 { '0', 'p', 1, 'i' },
00366 { 0, 0, 0, 0 }
00367 };
00368
00369 static const format_flag_spec asm_fprintf_flag_specs[] =
00370 {
00371 { ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
00372 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
00373 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
00374 { '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
00375 { '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
00376 { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
00377 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
00378 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
00379 { 0, 0, 0, NULL, NULL, 0 }
00380 };
00381
00382 static const format_flag_pair asm_fprintf_flag_pairs[] =
00383 {
00384 { ' ', '+', 1, 0 },
00385 { '0', '-', 1, 0 },
00386 { '0', 'p', 1, 'i' },
00387 { 0, 0, 0, 0 }
00388 };
00389
00390 static const format_flag_pair gcc_diag_flag_pairs[] =
00391 {
00392 { 0, 0, 0, 0 }
00393 };
00394
00395 #define gcc_tdiag_flag_pairs gcc_diag_flag_pairs
00396 #define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
00397 #define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
00398
00399 static const format_flag_pair gcc_gfc_flag_pairs[] =
00400 {
00401 { 0, 0, 0, 0 }
00402 };
00403
00404 static const format_flag_spec gcc_diag_flag_specs[] =
00405 {
00406 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
00407 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
00408 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
00409 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
00410 { 0, 0, 0, NULL, NULL, 0 }
00411 };
00412
00413 #define gcc_tdiag_flag_specs gcc_diag_flag_specs
00414 #define gcc_cdiag_flag_specs gcc_diag_flag_specs
00415
00416 static const format_flag_spec gcc_cxxdiag_flag_specs[] =
00417 {
00418 { '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
00419 { '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
00420 { 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
00421 { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
00422 { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
00423 { 0, 0, 0, NULL, NULL, 0 }
00424 };
00425
00426 static const format_flag_spec scanf_flag_specs[] =
00427 {
00428 { '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
00429 { 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
00430 { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
00431 { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
00432 { '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
00433 { 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
00434 { 0, 0, 0, NULL, NULL, 0 }
00435 };
00436
00437
00438 static const format_flag_pair scanf_flag_pairs[] =
00439 {
00440 { '*', 'L', 0, 0 },
00441 { 0, 0, 0, 0 }
00442 };
00443
00444
00445 static const format_flag_spec strftime_flag_specs[] =
00446 {
00447 { '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
00448 { '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
00449 { '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
00450 { '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
00451 { '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
00452 { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
00453 { 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
00454 { 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
00455 { 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
00456 { 0, 0, 0, NULL, NULL, 0 }
00457 };
00458
00459
00460 static const format_flag_pair strftime_flag_pairs[] =
00461 {
00462 { 'E', 'O', 0, 0 },
00463 { '_', '-', 0, 0 },
00464 { '_', '0', 0, 0 },
00465 { '-', '0', 0, 0 },
00466 { '^', '#', 0, 0 },
00467 { 0, 0, 0, 0 }
00468 };
00469
00470
00471 static const format_flag_spec strfmon_flag_specs[] =
00472 {
00473 { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
00474 { '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
00475 { '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
00476 { '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
00477 { '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
00478 { '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
00479 { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
00480 { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
00481 { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
00482 { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
00483 { 0, 0, 0, NULL, NULL, 0 }
00484 };
00485
00486 static const format_flag_pair strfmon_flag_pairs[] =
00487 {
00488 { '+', '(', 0, 0 },
00489 { 0, 0, 0, 0 }
00490 };
00491
00492
00493 static const format_char_info print_char_table[] =
00494 {
00495
00496 { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "-wp0 +'I", "i", NULL },
00497 { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
00498 { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0'I", "i", NULL },
00499 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
00500 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#I", "", NULL },
00501 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
00502 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
00503 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
00504 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
00505
00506 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL },
00507 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
00508
00509 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
00510 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
00511
00512 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
00513 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00514 };
00515
00516 static const format_char_info asm_fprintf_char_table[] =
00517 {
00518
00519 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
00520 { "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
00521 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
00522 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
00523 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
00524
00525
00526 { "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00527 { "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00528 { "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00529 { "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00530 { "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00531 { "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
00532 { "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00533 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00534 };
00535
00536 static const format_char_info gcc_diag_char_table[] =
00537 {
00538
00539 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00540 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00541 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00542 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00543 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
00544 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
00545
00546
00547
00548
00549 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00550
00551
00552 { "J", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00553
00554 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00555 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
00556 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00557 };
00558
00559 static const format_char_info gcc_tdiag_char_table[] =
00560 {
00561
00562 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00563 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00564 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00565 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00566 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
00567 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
00568
00569
00570
00571
00572 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00573
00574
00575 { "DFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
00576
00577 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00578 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
00579 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00580 };
00581
00582 static const format_char_info gcc_cdiag_char_table[] =
00583 {
00584
00585 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00586 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00587 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00588 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00589 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
00590 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
00591
00592
00593
00594
00595 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00596
00597
00598 { "DEFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+", "", NULL },
00599
00600 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00601 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
00602 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00603 };
00604
00605 static const format_char_info gcc_cxxdiag_char_table[] =
00606 {
00607
00608 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00609 { "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00610 { "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00611 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00612 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
00613 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
00614
00615
00616
00617
00618 { "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00619
00620
00621 { "ADEFJTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
00622
00623
00624 { "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
00625
00626 { "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00627 { "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
00628 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00629 };
00630
00631 static const format_char_info gcc_gfc_char_table[] =
00632 {
00633
00634 { "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
00635 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
00636 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "cR", NULL },
00637
00638
00639
00640 { "C", 0, STD_C89, NOARGUMENTS, "", "", NULL },
00641
00642
00643 { "L", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "R", NULL },
00644
00645 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00646 };
00647
00648 static const format_char_info scan_char_table[] =
00649 {
00650
00651 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
00652 { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w'I", "W", NULL },
00653 { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
00654 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
00655 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL },
00656 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL },
00657 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL },
00658 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
00659 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL },
00660
00661 { "F", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "*w'", "W", NULL },
00662 { "aA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
00663
00664 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
00665 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL },
00666 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00667 };
00668
00669 static const format_char_info time_char_table[] =
00670 {
00671
00672 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
00673 { "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
00674 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
00675 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
00676 { "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
00677 { "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
00678 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
00679 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
00680 { "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
00681
00682 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
00683 { "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
00684 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
00685 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
00686 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
00687 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
00688 { "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
00689 { "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
00690
00691 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
00692 { "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
00693 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00694 };
00695
00696 static const format_char_info monetary_char_table[] =
00697 {
00698 { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
00699 { NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
00700 };
00701
00702
00703 static const format_kind_info format_types_orig[] =
00704 {
00705 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
00706 printf_flag_specs, printf_flag_pairs,
00707 FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
00708 'w', 0, 'p', 0, 'L',
00709 &integer_type_node, &integer_type_node
00710 },
00711 { "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
00712 asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
00713 FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
00714 'w', 0, 'p', 0, 'L',
00715 NULL, NULL
00716 },
00717 { "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q+", NULL,
00718 gcc_diag_flag_specs, gcc_diag_flag_pairs,
00719 FMT_FLAG_ARG_CONVERT,
00720 0, 0, 'p', 0, 'L',
00721 NULL, &integer_type_node
00722 },
00723 { "gcc_tdiag", gcc_tdiag_length_specs, gcc_tdiag_char_table, "q+", NULL,
00724 gcc_tdiag_flag_specs, gcc_tdiag_flag_pairs,
00725 FMT_FLAG_ARG_CONVERT,
00726 0, 0, 'p', 0, 'L',
00727 NULL, &integer_type_node
00728 },
00729 { "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q+", NULL,
00730 gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
00731 FMT_FLAG_ARG_CONVERT,
00732 0, 0, 'p', 0, 'L',
00733 NULL, &integer_type_node
00734 },
00735 { "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
00736 gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
00737 FMT_FLAG_ARG_CONVERT,
00738 0, 0, 'p', 0, 'L',
00739 NULL, &integer_type_node
00740 },
00741 { "gcc_gfc", NULL, gcc_gfc_char_table, "", NULL,
00742 NULL, gcc_gfc_flag_pairs,
00743 FMT_FLAG_ARG_CONVERT,
00744 0, 0, 0, 0, 0,
00745 NULL, NULL
00746 },
00747 { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
00748 scanf_flag_specs, scanf_flag_pairs,
00749 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
00750 'w', 0, 0, '*', 'L',
00751 NULL, NULL
00752 },
00753 { "strftime", NULL, time_char_table, "_-0^#", "EO",
00754 strftime_flag_specs, strftime_flag_pairs,
00755 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
00756 NULL, NULL
00757 },
00758 { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
00759 strfmon_flag_specs, strfmon_flag_pairs,
00760 FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
00761 NULL, NULL
00762 }
00763 };
00764
00765
00766
00767
00768 static const format_kind_info *format_types = format_types_orig;
00769
00770
00771 static format_kind_info *dynamic_format_types;
00772
00773 static int n_format_types = ARRAY_SIZE (format_types_orig);
00774
00775
00776
00777
00778 typedef struct
00779 {
00780
00781
00782 int number_non_literal;
00783
00784
00785 int number_extra_args;
00786
00787
00788
00789 int number_dollar_extra_args;
00790
00791
00792 int number_wide;
00793
00794 int number_empty;
00795
00796
00797 int number_unterminated;
00798
00799 int number_other;
00800 } format_check_results;
00801
00802 typedef struct
00803 {
00804 format_check_results *res;
00805 function_format_info *info;
00806 tree params;
00807 } format_check_context;
00808
00809 static void check_format_info (function_format_info *, tree);
00810 static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
00811 static void check_format_info_main (format_check_results *,
00812 function_format_info *,
00813 const char *, int, tree,
00814 unsigned HOST_WIDE_INT);
00815
00816 static void init_dollar_format_checking (int, tree);
00817 static int maybe_read_dollar_number (const char **, int,
00818 tree, tree *, const format_kind_info *);
00819 static bool avoid_dollar_number (const char *);
00820 static void finish_dollar_format_checking (format_check_results *, int);
00821
00822 static const format_flag_spec *get_flag_spec (const format_flag_spec *,
00823 int, const char *);
00824
00825 static void check_format_types (format_wanted_type *, const char *, int);
00826 static void format_type_warning (const char *, const char *, int, tree,
00827 int, const char *, tree, int);
00828
00829
00830
00831
00832 static int
00833 decode_format_type (const char *s)
00834 {
00835 int i;
00836 int slen;
00837 slen = strlen (s);
00838 for (i = 0; i < n_format_types; i++)
00839 {
00840 int alen;
00841 if (!strcmp (s, format_types[i].name))
00842 return i;
00843 alen = strlen (format_types[i].name);
00844 if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
00845 && s[slen - 1] == '_' && s[slen - 2] == '_'
00846 && !strncmp (s + 2, format_types[i].name, alen))
00847 return i;
00848 }
00849 return format_type_error;
00850 }
00851
00852
00853
00854
00855
00856
00857
00858
00859 void
00860 check_function_format (tree attrs, tree params)
00861 {
00862 tree a;
00863
00864
00865 for (a = attrs; a; a = TREE_CHAIN (a))
00866 {
00867 if (is_attribute_p ("format", TREE_PURPOSE (a)))
00868 {
00869
00870 function_format_info info;
00871 decode_format_attr (TREE_VALUE (a), &info, 1);
00872 if (warn_format)
00873 check_format_info (&info, params);
00874 if (warn_missing_format_attribute && info.first_arg_num == 0
00875 && (format_types[info.format_type].flags
00876 & (int) FMT_FLAG_ARG_CONVERT))
00877 {
00878 tree c;
00879 for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
00880 c;
00881 c = TREE_CHAIN (c))
00882 if (is_attribute_p ("format", TREE_PURPOSE (c))
00883 && (decode_format_type (IDENTIFIER_POINTER
00884 (TREE_VALUE (TREE_VALUE (c))))
00885 == info.format_type))
00886 break;
00887 if (c == NULL_TREE)
00888 {
00889
00890
00891
00892
00893 tree args;
00894 for (args = DECL_ARGUMENTS (current_function_decl);
00895 args != 0;
00896 args = TREE_CHAIN (args))
00897 {
00898 if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
00899 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
00900 == char_type_node))
00901 break;
00902 }
00903 if (args != 0)
00904 warning (OPT_Wmissing_format_attribute, "function might "
00905 "be possible candidate for %qs format attribute",
00906 format_types[info.format_type].name);
00907 }
00908 }
00909 }
00910 }
00911 }
00912
00913
00914
00915 static char *dollar_arguments_used = NULL;
00916 static char *dollar_arguments_pointer_p = NULL;
00917 static int dollar_arguments_alloc = 0;
00918 static int dollar_arguments_count;
00919 static int dollar_first_arg_num;
00920 static int dollar_max_arg_used;
00921 static int dollar_format_warned;
00922
00923
00924
00925
00926
00927
00928
00929 static void
00930 init_dollar_format_checking (int first_arg_num, tree params)
00931 {
00932 tree oparams = params;
00933
00934 dollar_first_arg_num = first_arg_num;
00935 dollar_arguments_count = 0;
00936 dollar_max_arg_used = 0;
00937 dollar_format_warned = 0;
00938 if (first_arg_num > 0)
00939 {
00940 while (params)
00941 {
00942 dollar_arguments_count++;
00943 params = TREE_CHAIN (params);
00944 }
00945 }
00946 if (dollar_arguments_alloc < dollar_arguments_count)
00947 {
00948 if (dollar_arguments_used)
00949 free (dollar_arguments_used);
00950 if (dollar_arguments_pointer_p)
00951 free (dollar_arguments_pointer_p);
00952 dollar_arguments_alloc = dollar_arguments_count;
00953 dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
00954 dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
00955 }
00956 if (dollar_arguments_alloc)
00957 {
00958 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
00959 if (first_arg_num > 0)
00960 {
00961 int i = 0;
00962 params = oparams;
00963 while (params)
00964 {
00965 dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
00966 == POINTER_TYPE);
00967 params = TREE_CHAIN (params);
00968 i++;
00969 }
00970 }
00971 }
00972 }
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984 static int
00985 maybe_read_dollar_number (const char **format,
00986 int dollar_needed, tree params, tree *param_ptr,
00987 const format_kind_info *fki)
00988 {
00989 int argnum;
00990 int overflow_flag;
00991 const char *fcp = *format;
00992 if (!ISDIGIT (*fcp))
00993 {
00994 if (dollar_needed)
00995 {
00996 warning (OPT_Wformat, "missing $ operand number in format");
00997 return -1;
00998 }
00999 else
01000 return 0;
01001 }
01002 argnum = 0;
01003 overflow_flag = 0;
01004 while (ISDIGIT (*fcp))
01005 {
01006 int nargnum;
01007 nargnum = 10 * argnum + (*fcp - '0');
01008 if (nargnum < 0 || nargnum / 10 != argnum)
01009 overflow_flag = 1;
01010 argnum = nargnum;
01011 fcp++;
01012 }
01013 if (*fcp != '$')
01014 {
01015 if (dollar_needed)
01016 {
01017 warning (OPT_Wformat, "missing $ operand number in format");
01018 return -1;
01019 }
01020 else
01021 return 0;
01022 }
01023 *format = fcp + 1;
01024 if (pedantic && !dollar_format_warned)
01025 {
01026 warning (OPT_Wformat, "%s does not support %%n$ operand number formats",
01027 C_STD_NAME (STD_EXT));
01028 dollar_format_warned = 1;
01029 }
01030 if (overflow_flag || argnum == 0
01031 || (dollar_first_arg_num && argnum > dollar_arguments_count))
01032 {
01033 warning (OPT_Wformat, "operand number out of range in format");
01034 return -1;
01035 }
01036 if (argnum > dollar_max_arg_used)
01037 dollar_max_arg_used = argnum;
01038
01039
01040 while (dollar_arguments_alloc < dollar_max_arg_used)
01041 {
01042 int nalloc;
01043 nalloc = 2 * dollar_arguments_alloc + 16;
01044 dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
01045 nalloc);
01046 dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
01047 nalloc);
01048 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
01049 nalloc - dollar_arguments_alloc);
01050 dollar_arguments_alloc = nalloc;
01051 }
01052 if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
01053 && dollar_arguments_used[argnum - 1] == 1)
01054 {
01055 dollar_arguments_used[argnum - 1] = 2;
01056 warning (OPT_Wformat, "format argument %d used more than once in %s format",
01057 argnum, fki->name);
01058 }
01059 else
01060 dollar_arguments_used[argnum - 1] = 1;
01061 if (dollar_first_arg_num)
01062 {
01063 int i;
01064 *param_ptr = params;
01065 for (i = 1; i < argnum && *param_ptr != 0; i++)
01066 *param_ptr = TREE_CHAIN (*param_ptr);
01067
01068
01069 gcc_assert (*param_ptr);
01070 }
01071 else
01072 *param_ptr = 0;
01073 return argnum;
01074 }
01075
01076
01077
01078
01079 static bool
01080 avoid_dollar_number (const char *format)
01081 {
01082 if (!ISDIGIT (*format))
01083 return false;
01084 while (ISDIGIT (*format))
01085 format++;
01086 if (*format == '$')
01087 {
01088 warning (OPT_Wformat, "$ operand number used after format without operand number");
01089 return true;
01090 }
01091 return false;
01092 }
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105 static void
01106 finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
01107 {
01108 int i;
01109 bool found_pointer_gap = false;
01110 for (i = 0; i < dollar_max_arg_used; i++)
01111 {
01112 if (!dollar_arguments_used[i])
01113 {
01114 if (pointer_gap_ok && (dollar_first_arg_num == 0
01115 || dollar_arguments_pointer_p[i]))
01116 found_pointer_gap = true;
01117 else
01118 warning (OPT_Wformat,
01119 "format argument %d unused before used argument %d in $-style format",
01120 i + 1, dollar_max_arg_used);
01121 }
01122 }
01123 if (found_pointer_gap
01124 || (dollar_first_arg_num
01125 && dollar_max_arg_used < dollar_arguments_count))
01126 {
01127 res->number_other--;
01128 res->number_dollar_extra_args++;
01129 }
01130 }
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141 static const format_flag_spec *
01142 get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
01143 {
01144 int i;
01145 for (i = 0; spec[i].flag_char != 0; i++)
01146 {
01147 if (spec[i].flag_char != flag)
01148 continue;
01149 if (predicates != NULL)
01150 {
01151 if (spec[i].predicate != 0
01152 && strchr (predicates, spec[i].predicate) != 0)
01153 return &spec[i];
01154 }
01155 else if (spec[i].predicate == 0)
01156 return &spec[i];
01157 }
01158 gcc_assert (predicates);
01159 return NULL;
01160 }
01161
01162
01163
01164
01165
01166
01167 static void
01168 check_format_info (function_format_info *info, tree params)
01169 {
01170 format_check_context format_ctx;
01171 unsigned HOST_WIDE_INT arg_num;
01172 tree format_tree;
01173 format_check_results res;
01174
01175
01176 for (arg_num = 1; ; ++arg_num)
01177 {
01178 if (params == 0)
01179 return;
01180 if (arg_num == info->format_num)
01181 break;
01182 params = TREE_CHAIN (params);
01183 }
01184 format_tree = TREE_VALUE (params);
01185 params = TREE_CHAIN (params);
01186 if (format_tree == 0)
01187 return;
01188
01189 res.number_non_literal = 0;
01190 res.number_extra_args = 0;
01191 res.number_dollar_extra_args = 0;
01192 res.number_wide = 0;
01193 res.number_empty = 0;
01194 res.number_unterminated = 0;
01195 res.number_other = 0;
01196
01197 format_ctx.res = &res;
01198 format_ctx.info = info;
01199 format_ctx.params = params;
01200
01201 check_function_arguments_recurse (check_format_arg, &format_ctx,
01202 format_tree, arg_num);
01203
01204 if (res.number_non_literal > 0)
01205 {
01206
01207
01208
01209 if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
01210 {
01211
01212
01213 warning (OPT_Wformat_nonliteral,
01214 "format not a string literal, format string not checked");
01215 }
01216 else if (info->first_arg_num != 0)
01217 {
01218
01219
01220 while (arg_num + 1 < info->first_arg_num)
01221 {
01222 if (params == 0)
01223 break;
01224 params = TREE_CHAIN (params);
01225 ++arg_num;
01226 }
01227 if (params == 0 && warn_format_security)
01228 warning (OPT_Wformat_security,
01229 "format not a string literal and no format arguments");
01230 else if (params == 0 && warn_format_nonliteral)
01231 warning (OPT_Wformat_nonliteral,
01232 "format not a string literal and no format arguments");
01233 else
01234 warning (OPT_Wformat_nonliteral,
01235 "format not a string literal, argument types not checked");
01236 }
01237 }
01238
01239
01240
01241
01242
01243
01244
01245
01246 if (res.number_extra_args > 0 && res.number_non_literal == 0
01247 && res.number_other == 0)
01248 warning (OPT_Wformat_extra_args, "too many arguments for format");
01249 if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
01250 && res.number_other == 0)
01251 warning (OPT_Wformat_extra_args, "unused arguments in $-style format");
01252 if (res.number_empty > 0 && res.number_non_literal == 0
01253 && res.number_other == 0)
01254 warning (OPT_Wformat_zero_length, "zero-length %s format string",
01255 format_types[info->format_type].name);
01256
01257 if (res.number_wide > 0)
01258 warning (OPT_Wformat, "format is a wide character string");
01259
01260 if (res.number_unterminated > 0)
01261 warning (OPT_Wformat, "unterminated format string");
01262 }
01263
01264
01265
01266
01267
01268
01269 static void
01270 check_format_arg (void *ctx, tree format_tree,
01271 unsigned HOST_WIDE_INT arg_num)
01272 {
01273 format_check_context *format_ctx = (format_check_context *) ctx;
01274 format_check_results *res = format_ctx->res;
01275 function_format_info *info = format_ctx->info;
01276 tree params = format_ctx->params;
01277
01278 int format_length;
01279 HOST_WIDE_INT offset;
01280 const char *format_chars;
01281 tree array_size = 0;
01282 tree array_init;
01283
01284 if (integer_zerop (format_tree))
01285 {
01286
01287
01288 while (arg_num + 1 < info->first_arg_num)
01289 {
01290 if (params == 0)
01291 return;
01292 params = TREE_CHAIN (params);
01293 ++arg_num;
01294 }
01295
01296 if (params == 0)
01297 res->number_other++;
01298 else
01299 res->number_extra_args++;
01300
01301 return;
01302 }
01303
01304 offset = 0;
01305 if (TREE_CODE (format_tree) == PLUS_EXPR)
01306 {
01307 tree arg0, arg1;
01308
01309 arg0 = TREE_OPERAND (format_tree, 0);
01310 arg1 = TREE_OPERAND (format_tree, 1);
01311 STRIP_NOPS (arg0);
01312 STRIP_NOPS (arg1);
01313 if (TREE_CODE (arg1) == INTEGER_CST)
01314 format_tree = arg0;
01315 else if (TREE_CODE (arg0) == INTEGER_CST)
01316 {
01317 format_tree = arg1;
01318 arg1 = arg0;
01319 }
01320 else
01321 {
01322 res->number_non_literal++;
01323 return;
01324 }
01325 if (!host_integerp (arg1, 0)
01326 || (offset = tree_low_cst (arg1, 0)) < 0)
01327 {
01328 res->number_non_literal++;
01329 return;
01330 }
01331 }
01332 if (TREE_CODE (format_tree) != ADDR_EXPR)
01333 {
01334 res->number_non_literal++;
01335 return;
01336 }
01337 format_tree = TREE_OPERAND (format_tree, 0);
01338 if (TREE_CODE (format_tree) == ARRAY_REF
01339 && host_integerp (TREE_OPERAND (format_tree, 1), 0)
01340 && (offset += tree_low_cst (TREE_OPERAND (format_tree, 1), 0)) >= 0)
01341 format_tree = TREE_OPERAND (format_tree, 0);
01342 if (TREE_CODE (format_tree) == VAR_DECL
01343 && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
01344 && (array_init = decl_constant_value (format_tree)) != format_tree
01345 && TREE_CODE (array_init) == STRING_CST)
01346 {
01347
01348
01349
01350 array_size = DECL_SIZE_UNIT (format_tree);
01351 format_tree = array_init;
01352 }
01353 if (TREE_CODE (format_tree) != STRING_CST)
01354 {
01355 res->number_non_literal++;
01356 return;
01357 }
01358 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
01359 {
01360 res->number_wide++;
01361 return;
01362 }
01363 format_chars = TREE_STRING_POINTER (format_tree);
01364 format_length = TREE_STRING_LENGTH (format_tree);
01365 if (array_size != 0)
01366 {
01367
01368 gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
01369
01370 if (host_integerp (array_size, 0))
01371 {
01372 HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
01373 if (array_size_value > 0
01374 && array_size_value == (int) array_size_value
01375 && format_length > array_size_value)
01376 format_length = array_size_value;
01377 }
01378 }
01379 if (offset)
01380 {
01381 if (offset >= format_length)
01382 {
01383 res->number_non_literal++;
01384 return;
01385 }
01386 format_chars += offset;
01387 format_length -= offset;
01388 }
01389 if (format_length < 1)
01390 {
01391 res->number_unterminated++;
01392 return;
01393 }
01394 if (format_length == 1)
01395 {
01396 res->number_empty++;
01397 return;
01398 }
01399 if (format_chars[--format_length] != 0)
01400 {
01401 res->number_unterminated++;
01402 return;
01403 }
01404
01405
01406 while (arg_num + 1 < info->first_arg_num)
01407 {
01408 if (params == 0)
01409 return;
01410 params = TREE_CHAIN (params);
01411 ++arg_num;
01412 }
01413
01414
01415
01416 res->number_other++;
01417 check_format_info_main (res, info, format_chars, format_length,
01418 params, arg_num);
01419 }
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429 static void
01430 check_format_info_main (format_check_results *res,
01431 function_format_info *info, const char *format_chars,
01432 int format_length, tree params,
01433 unsigned HOST_WIDE_INT arg_num)
01434 {
01435 const char *orig_format_chars = format_chars;
01436 tree first_fillin_param = params;
01437
01438 const format_kind_info *fki = &format_types[info->format_type];
01439 const format_flag_spec *flag_specs = fki->flag_specs;
01440 const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
01441
01442
01443
01444 int has_operand_number = -1;
01445
01446 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
01447
01448 while (1)
01449 {
01450 int i;
01451 int suppressed = FALSE;
01452 const char *length_chars = NULL;
01453 enum format_lengths length_chars_val = FMT_LEN_none;
01454 enum format_std_version length_chars_std = STD_C89;
01455 int format_char;
01456 tree cur_param;
01457 tree wanted_type;
01458 int main_arg_num = 0;
01459 tree main_arg_params = 0;
01460 enum format_std_version wanted_type_std;
01461 const char *wanted_type_name;
01462 format_wanted_type width_wanted_type;
01463 format_wanted_type precision_wanted_type;
01464 format_wanted_type main_wanted_type;
01465 format_wanted_type *first_wanted_type = NULL;
01466 format_wanted_type *last_wanted_type = NULL;
01467 const format_length_info *fli = NULL;
01468 const format_char_info *fci = NULL;
01469 char flag_chars[256];
01470 int aflag = 0;
01471 const char *format_start = format_chars;
01472 if (*format_chars == 0)
01473 {
01474 if (format_chars - orig_format_chars != format_length)
01475 warning (OPT_Wformat, "embedded %<\\0%> in format");
01476 if (info->first_arg_num != 0 && params != 0
01477 && has_operand_number <= 0)
01478 {
01479 res->number_other--;
01480 res->number_extra_args++;
01481 }
01482 if (has_operand_number > 0)
01483 finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
01484 return;
01485 }
01486 if (*format_chars++ != '%')
01487 continue;
01488 if (*format_chars == 0)
01489 {
01490 warning (OPT_Wformat, "spurious trailing %<%%%> in format");
01491 continue;
01492 }
01493 if (*format_chars == '%')
01494 {
01495 ++format_chars;
01496 continue;
01497 }
01498 flag_chars[0] = 0;
01499
01500 if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
01501 {
01502
01503
01504
01505
01506 int opnum;
01507 opnum = maybe_read_dollar_number (&format_chars, 0,
01508 first_fillin_param,
01509 &main_arg_params, fki);
01510 if (opnum == -1)
01511 return;
01512 else if (opnum > 0)
01513 {
01514 has_operand_number = 1;
01515 main_arg_num = opnum + info->first_arg_num - 1;
01516 }
01517 }
01518 else if (fki->flags & FMT_FLAG_USE_DOLLAR)
01519 {
01520 if (avoid_dollar_number (format_chars))
01521 return;
01522 }
01523
01524
01525
01526
01527 while (*format_chars != 0
01528 && strchr (fki->flag_chars, *format_chars) != 0)
01529 {
01530 const format_flag_spec *s = get_flag_spec (flag_specs,
01531 *format_chars, NULL);
01532 if (strchr (flag_chars, *format_chars) != 0)
01533 {
01534 warning (OPT_Wformat, "repeated %s in format", _(s->name));
01535 }
01536 else
01537 {
01538 i = strlen (flag_chars);
01539 flag_chars[i++] = *format_chars;
01540 flag_chars[i] = 0;
01541 }
01542 if (s->skip_next_char)
01543 {
01544 ++format_chars;
01545 if (*format_chars == 0)
01546 {
01547 warning (OPT_Wformat, "missing fill character at end of strfmon format");
01548 return;
01549 }
01550 }
01551 ++format_chars;
01552 }
01553
01554
01555 if (fki->width_char != 0)
01556 {
01557 if (fki->width_type != NULL && *format_chars == '*')
01558 {
01559 i = strlen (flag_chars);
01560 flag_chars[i++] = fki->width_char;
01561 flag_chars[i] = 0;
01562
01563
01564 ++format_chars;
01565 if (has_operand_number != 0)
01566 {
01567 int opnum;
01568 opnum = maybe_read_dollar_number (&format_chars,
01569 has_operand_number == 1,
01570 first_fillin_param,
01571 ¶ms, fki);
01572 if (opnum == -1)
01573 return;
01574 else if (opnum > 0)
01575 {
01576 has_operand_number = 1;
01577 arg_num = opnum + info->first_arg_num - 1;
01578 }
01579 else
01580 has_operand_number = 0;
01581 }
01582 else
01583 {
01584 if (avoid_dollar_number (format_chars))
01585 return;
01586 }
01587 if (info->first_arg_num != 0)
01588 {
01589 if (params == 0)
01590 {
01591 warning (OPT_Wformat, "too few arguments for format");
01592 return;
01593 }
01594 cur_param = TREE_VALUE (params);
01595 if (has_operand_number <= 0)
01596 {
01597 params = TREE_CHAIN (params);
01598 ++arg_num;
01599 }
01600 width_wanted_type.wanted_type = *fki->width_type;
01601 width_wanted_type.wanted_type_name = NULL;
01602 width_wanted_type.pointer_count = 0;
01603 width_wanted_type.char_lenient_flag = 0;
01604 width_wanted_type.writing_in_flag = 0;
01605 width_wanted_type.reading_from_flag = 0;
01606 width_wanted_type.name = _("field width");
01607 width_wanted_type.param = cur_param;
01608 width_wanted_type.arg_num = arg_num;
01609 width_wanted_type.next = NULL;
01610 if (last_wanted_type != 0)
01611 last_wanted_type->next = &width_wanted_type;
01612 if (first_wanted_type == 0)
01613 first_wanted_type = &width_wanted_type;
01614 last_wanted_type = &width_wanted_type;
01615 }
01616 }
01617 else
01618 {
01619
01620
01621 int non_zero_width_char = FALSE;
01622 int found_width = FALSE;
01623 while (ISDIGIT (*format_chars))
01624 {
01625 found_width = TRUE;
01626 if (*format_chars != '0')
01627 non_zero_width_char = TRUE;
01628 ++format_chars;
01629 }
01630 if (found_width && !non_zero_width_char &&
01631 (fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
01632 warning (OPT_Wformat, "zero width in %s format", fki->name);
01633 if (found_width)
01634 {
01635 i = strlen (flag_chars);
01636 flag_chars[i++] = fki->width_char;
01637 flag_chars[i] = 0;
01638 }
01639 }
01640 }
01641
01642
01643 if (fki->left_precision_char != 0 && *format_chars == '#')
01644 {
01645 ++format_chars;
01646 i = strlen (flag_chars);
01647 flag_chars[i++] = fki->left_precision_char;
01648 flag_chars[i] = 0;
01649 if (!ISDIGIT (*format_chars))
01650 warning (OPT_Wformat, "empty left precision in %s format", fki->name);
01651 while (ISDIGIT (*format_chars))
01652 ++format_chars;
01653 }
01654
01655
01656 if (fki->precision_char != 0 && *format_chars == '.')
01657 {
01658 ++format_chars;
01659 i = strlen (flag_chars);
01660 flag_chars[i++] = fki->precision_char;
01661 flag_chars[i] = 0;
01662 if (fki->precision_type != NULL && *format_chars == '*')
01663 {
01664
01665
01666 ++format_chars;
01667 if (has_operand_number != 0)
01668 {
01669 int opnum;
01670 opnum = maybe_read_dollar_number (&format_chars,
01671 has_operand_number == 1,
01672 first_fillin_param,
01673 ¶ms, fki);
01674 if (opnum == -1)
01675 return;
01676 else if (opnum > 0)
01677 {
01678 has_operand_number = 1;
01679 arg_num = opnum + info->first_arg_num - 1;
01680 }
01681 else
01682 has_operand_number = 0;
01683 }
01684 else
01685 {
01686 if (avoid_dollar_number (format_chars))
01687 return;
01688 }
01689 if (info->first_arg_num != 0)
01690 {
01691 if (params == 0)
01692 {
01693 warning (OPT_Wformat, "too few arguments for format");
01694 return;
01695 }
01696 cur_param = TREE_VALUE (params);
01697 if (has_operand_number <= 0)
01698 {
01699 params = TREE_CHAIN (params);
01700 ++arg_num;
01701 }
01702 precision_wanted_type.wanted_type = *fki->precision_type;
01703 precision_wanted_type.wanted_type_name = NULL;
01704 precision_wanted_type.pointer_count = 0;
01705 precision_wanted_type.char_lenient_flag = 0;
01706 precision_wanted_type.writing_in_flag = 0;
01707 precision_wanted_type.reading_from_flag = 0;
01708 precision_wanted_type.name = _("field precision");
01709 precision_wanted_type.param = cur_param;
01710 precision_wanted_type.arg_num = arg_num;
01711 precision_wanted_type.next = NULL;
01712 if (last_wanted_type != 0)
01713 last_wanted_type->next = &precision_wanted_type;
01714 if (first_wanted_type == 0)
01715 first_wanted_type = &precision_wanted_type;
01716 last_wanted_type = &precision_wanted_type;
01717 }
01718 }
01719 else
01720 {
01721 if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
01722 && !ISDIGIT (*format_chars))
01723 warning (OPT_Wformat, "empty precision in %s format", fki->name);
01724 while (ISDIGIT (*format_chars))
01725 ++format_chars;
01726 }
01727 }
01728
01729
01730 fli = fki->length_char_specs;
01731 length_chars = NULL;
01732 length_chars_val = FMT_LEN_none;
01733 length_chars_std = STD_C89;
01734 if (fli)
01735 {
01736 while (fli->name != 0 && fli->name[0] != *format_chars)
01737 fli++;
01738 if (fli->name != 0)
01739 {
01740 format_chars++;
01741 if (fli->double_name != 0 && fli->name[0] == *format_chars)
01742 {
01743 format_chars++;
01744 length_chars = fli->double_name;
01745 length_chars_val = fli->double_index;
01746 length_chars_std = fli->double_std;
01747 }
01748 else
01749 {
01750 length_chars = fli->name;
01751 length_chars_val = fli->index;
01752 length_chars_std = fli->std;
01753 }
01754 i = strlen (flag_chars);
01755 flag_chars[i++] = fki->length_code_char;
01756 flag_chars[i] = 0;
01757 }
01758 if (pedantic)
01759 {
01760
01761 if (ADJ_STD (length_chars_std) > C_STD_VER)
01762 warning (OPT_Wformat,
01763 "%s does not support the %qs %s length modifier",
01764 C_STD_NAME (length_chars_std), length_chars,
01765 fki->name);
01766 }
01767 }
01768
01769
01770 if (fki->modifier_chars != NULL)
01771 {
01772 while (*format_chars != 0
01773 && strchr (fki->modifier_chars, *format_chars) != 0)
01774 {
01775 if (strchr (flag_chars, *format_chars) != 0)
01776 {
01777 const format_flag_spec *s = get_flag_spec (flag_specs,
01778 *format_chars, NULL);
01779 warning (OPT_Wformat, "repeated %s in format", _(s->name));
01780 }
01781 else
01782 {
01783 i = strlen (flag_chars);
01784 flag_chars[i++] = *format_chars;
01785 flag_chars[i] = 0;
01786 }
01787 ++format_chars;
01788 }
01789 }
01790
01791
01792 if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
01793 {
01794 if (*format_chars == 'a' && !flag_isoc99)
01795 {
01796 if (format_chars[1] == 's' || format_chars[1] == 'S'
01797 || format_chars[1] == '[')
01798 {
01799
01800 i = strlen (flag_chars);
01801 flag_chars[i++] = 'a';
01802 flag_chars[i] = 0;
01803 format_chars++;
01804 }
01805 }
01806 }
01807
01808 format_char = *format_chars;
01809 if (format_char == 0
01810 || (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
01811 && format_char == '%'))
01812 {
01813 warning (OPT_Wformat, "conversion lacks type at end of format");
01814 continue;
01815 }
01816 format_chars++;
01817 fci = fki->conversion_specs;
01818 while (fci->format_chars != 0
01819 && strchr (fci->format_chars, format_char) == 0)
01820 ++fci;
01821 if (fci->format_chars == 0)
01822 {
01823 if (ISGRAPH (format_char))
01824 warning (OPT_Wformat, "unknown conversion type character %qc in format",
01825 format_char);
01826 else
01827 warning (OPT_Wformat, "unknown conversion type character 0x%x in format",
01828 format_char);
01829 continue;
01830 }
01831 if (pedantic)
01832 {
01833 if (ADJ_STD (fci->std) > C_STD_VER)
01834 warning (OPT_Wformat, "%s does not support the %<%%%c%> %s format",
01835 C_STD_NAME (fci->std), format_char, fki->name);
01836 }
01837
01838
01839 {
01840 int d = 0;
01841 for (i = 0; flag_chars[i] != 0; i++)
01842 {
01843 const format_flag_spec *s = get_flag_spec (flag_specs,
01844 flag_chars[i], NULL);
01845 flag_chars[i - d] = flag_chars[i];
01846 if (flag_chars[i] == fki->length_code_char)
01847 continue;
01848 if (strchr (fci->flag_chars, flag_chars[i]) == 0)
01849 {
01850 warning (OPT_Wformat, "%s used with %<%%%c%> %s format",
01851 _(s->name), format_char, fki->name);
01852 d++;
01853 continue;
01854 }
01855 if (pedantic)
01856 {
01857 const format_flag_spec *t;
01858 if (ADJ_STD (s->std) > C_STD_VER)
01859 warning (OPT_Wformat, "%s does not support %s",
01860 C_STD_NAME (s->std), _(s->long_name));
01861 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
01862 if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
01863 {
01864 const char *long_name = (t->long_name != NULL
01865 ? t->long_name
01866 : s->long_name);
01867 if (ADJ_STD (t->std) > C_STD_VER)
01868 warning (OPT_Wformat,
01869 "%s does not support %s with the %<%%%c%> %s format",
01870 C_STD_NAME (t->std), _(long_name),
01871 format_char, fki->name);
01872 }
01873 }
01874 }
01875 flag_chars[i - d] = 0;
01876 }
01877
01878 if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
01879 && strchr (flag_chars, 'a') != 0)
01880 aflag = 1;
01881
01882 if (fki->suppression_char
01883 && strchr (flag_chars, fki->suppression_char) != 0)
01884 suppressed = 1;
01885
01886
01887 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
01888 {
01889 const format_flag_spec *s, *t;
01890 if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
01891 continue;
01892 if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
01893 continue;
01894 if (bad_flag_pairs[i].predicate != 0
01895 && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
01896 continue;
01897 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
01898 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
01899 if (bad_flag_pairs[i].ignored)
01900 {
01901 if (bad_flag_pairs[i].predicate != 0)
01902 warning (OPT_Wformat,
01903 "%s ignored with %s and %<%%%c%> %s format",
01904 _(s->name), _(t->name), format_char,
01905 fki->name);
01906 else
01907 warning (OPT_Wformat, "%s ignored with %s in %s format",
01908 _(s->name), _(t->name), fki->name);
01909 }
01910 else
01911 {
01912 if (bad_flag_pairs[i].predicate != 0)
01913 warning (OPT_Wformat,
01914 "use of %s and %s together with %<%%%c%> %s format",
01915 _(s->name), _(t->name), format_char,
01916 fki->name);
01917 else
01918 warning (OPT_Wformat, "use of %s and %s together in %s format",
01919 _(s->name), _(t->name), fki->name);
01920 }
01921 }
01922
01923
01924 if (warn_format_y2k)
01925 {
01926 int y2k_level = 0;
01927 if (strchr (fci->flags2, '4') != 0)
01928 if (strchr (flag_chars, 'E') != 0)
01929 y2k_level = 3;
01930 else
01931 y2k_level = 2;
01932 else if (strchr (fci->flags2, '3') != 0)
01933 y2k_level = 3;
01934 else if (strchr (fci->flags2, '2') != 0)
01935 y2k_level = 2;
01936 if (y2k_level == 3)
01937 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
01938 "year in some locales", format_char);
01939 else if (y2k_level == 2)
01940 warning (OPT_Wformat_y2k, "%<%%%c%> yields only last 2 digits of "
01941 "year", format_char);
01942 }
01943
01944 if (strchr (fci->flags2, '[') != 0)
01945 {
01946
01947 if (*format_chars == '^')
01948 ++format_chars;
01949
01950
01951 if (*format_chars == ']')
01952 ++format_chars;
01953 while (*format_chars && *format_chars != ']')
01954 ++format_chars;
01955 if (*format_chars != ']')
01956
01957 warning (OPT_Wformat, "no closing %<]%> for %<%%[%> format");
01958 }
01959
01960 wanted_type = 0;
01961 wanted_type_name = 0;
01962 if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
01963 {
01964 wanted_type = (fci->types[length_chars_val].type
01965 ? *fci->types[length_chars_val].type : 0);
01966 wanted_type_name = fci->types[length_chars_val].name;
01967 wanted_type_std = fci->types[length_chars_val].std;
01968 if (wanted_type == 0)
01969 {
01970 warning (OPT_Wformat,
01971 "use of %qs length modifier with %qc type character",
01972 length_chars, format_char);
01973
01974
01975 arg_num++;
01976 if (params == 0)
01977 {
01978 warning (OPT_Wformat, "too few arguments for format");
01979 return;
01980 }
01981 params = TREE_CHAIN (params);
01982 continue;
01983 }
01984 else if (pedantic
01985
01986
01987
01988 && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
01989 && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
01990 {
01991 if (ADJ_STD (wanted_type_std) > C_STD_VER)
01992 warning (OPT_Wformat,
01993 "%s does not support the %<%%%s%c%> %s format",
01994 C_STD_NAME (wanted_type_std), length_chars,
01995 format_char, fki->name);
01996 }
01997 }
01998
01999 main_wanted_type.next = NULL;
02000
02001
02002 if (info->first_arg_num == 0)
02003 continue;
02004 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
02005 || suppressed)
02006 {
02007 if (main_arg_num != 0)
02008 {
02009 if (suppressed)
02010 warning (OPT_Wformat, "operand number specified with "
02011 "suppressed assignment");
02012 else
02013 warning (OPT_Wformat, "operand number specified for format "
02014 "taking no argument");
02015 }
02016 }
02017 else
02018 {
02019 format_wanted_type *wanted_type_ptr;
02020
02021 if (main_arg_num != 0)
02022 {
02023 arg_num = main_arg_num;
02024 params = main_arg_params;
02025 }
02026 else
02027 {
02028 ++arg_num;
02029 if (has_operand_number > 0)
02030 {
02031 warning (OPT_Wformat, "missing $ operand number in format");
02032 return;
02033 }
02034 else
02035 has_operand_number = 0;
02036 }
02037
02038 wanted_type_ptr = &main_wanted_type;
02039 while (fci)
02040 {
02041 if (params == 0)
02042 {
02043 warning (OPT_Wformat, "too few arguments for format");
02044 return;
02045 }
02046
02047 cur_param = TREE_VALUE (params);
02048 params = TREE_CHAIN (params);
02049
02050 wanted_type_ptr->wanted_type = wanted_type;
02051 wanted_type_ptr->wanted_type_name = wanted_type_name;
02052 wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
02053 wanted_type_ptr->char_lenient_flag = 0;
02054 if (strchr (fci->flags2, 'c') != 0)
02055 wanted_type_ptr->char_lenient_flag = 1;
02056 wanted_type_ptr->writing_in_flag = 0;
02057 wanted_type_ptr->reading_from_flag = 0;
02058 if (aflag)
02059 wanted_type_ptr->writing_in_flag = 1;
02060 else
02061 {
02062 if (strchr (fci->flags2, 'W') != 0)
02063 wanted_type_ptr->writing_in_flag = 1;
02064 if (strchr (fci->flags2, 'R') != 0)
02065 wanted_type_ptr->reading_from_flag = 1;
02066 }
02067 wanted_type_ptr->name = NULL;
02068 wanted_type_ptr->param = cur_param;
02069 wanted_type_ptr->arg_num = arg_num;
02070 wanted_type_ptr->next = NULL;
02071 if (last_wanted_type != 0)
02072 last_wanted_type->next = wanted_type_ptr;
02073 if (first_wanted_type == 0)
02074 first_wanted_type = wanted_type_ptr;
02075 last_wanted_type = wanted_type_ptr;
02076
02077 fci = fci->chain;
02078 if (fci)
02079 {
02080 wanted_type_ptr = GGC_NEW (format_wanted_type);
02081 arg_num++;
02082 wanted_type = *fci->types[length_chars_val].type;
02083 wanted_type_name = fci->types[length_chars_val].name;
02084 }
02085 }
02086 }
02087
02088 if (first_wanted_type != 0)
02089 check_format_types (first_wanted_type, format_start,
02090 format_chars - format_start);
02091
02092 if (main_wanted_type.next != NULL)
02093 {
02094 format_wanted_type *wanted_type_ptr = main_wanted_type.next;
02095 while (wanted_type_ptr)
02096 {
02097 format_wanted_type *next = wanted_type_ptr->next;
02098 ggc_free (wanted_type_ptr);
02099 wanted_type_ptr = next;
02100 }
02101 }
02102 }
02103 }
02104
02105
02106
02107
02108 static void
02109 check_format_types (format_wanted_type *types, const char *format_start,
02110 int format_length)
02111 {
02112 for (; types != 0; types = types->next)
02113 {
02114 tree cur_param;
02115 tree cur_type;
02116 tree orig_cur_type;
02117 tree wanted_type;
02118 int arg_num;
02119 int i;
02120 int char_type_flag;
02121 cur_param = types->param;
02122 cur_type = TREE_TYPE (cur_param);
02123 if (cur_type == error_mark_node)
02124 continue;
02125 orig_cur_type = cur_type;
02126 char_type_flag = 0;
02127 wanted_type = types->wanted_type;
02128 arg_num = types->arg_num;
02129
02130
02131 gcc_assert (wanted_type);
02132 gcc_assert (wanted_type != void_type_node || types->pointer_count);
02133
02134 if (types->pointer_count == 0)
02135 wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
02136
02137 wanted_type = TYPE_MAIN_VARIANT (wanted_type);
02138
02139 STRIP_NOPS (cur_param);
02140
02141
02142
02143 for (i = 0; i < types->pointer_count; ++i)
02144 {
02145 if (TREE_CODE (cur_type) == POINTER_TYPE)
02146 {
02147 cur_type = TREE_TYPE (cur_type);
02148 if (cur_type == error_mark_node)
02149 break;
02150
02151
02152 if (types->writing_in_flag
02153 && i == 0
02154 && cur_param != 0
02155 && integer_zerop (cur_param))
02156 warning (OPT_Wformat, "writing through null pointer "
02157 "(argument %d)", arg_num);
02158
02159
02160 if (types->reading_from_flag
02161 && i == 0
02162 && cur_param != 0
02163 && integer_zerop (cur_param))
02164 warning (OPT_Wformat, "reading through null pointer "
02165 "(argument %d)", arg_num);
02166
02167 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
02168 cur_param = TREE_OPERAND (cur_param, 0);
02169 else
02170 cur_param = 0;
02171
02172
02173
02174
02175
02176
02177 if (types->writing_in_flag
02178 && i == 0
02179 && (TYPE_READONLY (cur_type)
02180 || (cur_param != 0
02181 && (CONSTANT_CLASS_P (cur_param)
02182 || (DECL_P (cur_param)
02183 && TREE_READONLY (cur_param))))))
02184 warning (OPT_Wformat, "writing into constant object "
02185 "(argument %d)", arg_num);
02186
02187
02188
02189
02190 if (i > 0
02191 && pedantic
02192 && (TYPE_READONLY (cur_type)
02193 || TYPE_VOLATILE (cur_type)
02194 || TYPE_RESTRICT (cur_type)))
02195 warning (OPT_Wformat, "extra type qualifiers in format "
02196 "argument (argument %d)",
02197 arg_num);
02198
02199 }
02200 else
02201 {
02202 format_type_warning (types->name, format_start, format_length,
02203 wanted_type, types->pointer_count,
02204 types->wanted_type_name, orig_cur_type,
02205 arg_num);
02206 break;
02207 }
02208 }
02209
02210 if (i < types->pointer_count)
02211 continue;
02212
02213 cur_type = TYPE_MAIN_VARIANT (cur_type);
02214
02215
02216
02217
02218 if (types->char_lenient_flag)
02219 char_type_flag = (cur_type == char_type_node
02220 || cur_type == signed_char_type_node
02221 || cur_type == unsigned_char_type_node);
02222
02223
02224 if (lang_hooks.types_compatible_p (wanted_type, cur_type))
02225 continue;
02226
02227
02228
02229
02230 if (wanted_type == void_type_node
02231 && (!pedantic || (i == 1 && char_type_flag)))
02232 continue;
02233
02234
02235
02236
02237 if (TREE_CODE (wanted_type) == INTEGER_TYPE
02238 && TREE_CODE (cur_type) == INTEGER_TYPE
02239 && (!pedantic || i == 0 || (i == 1 && char_type_flag))
02240 && (TYPE_UNSIGNED (wanted_type)
02241 ? wanted_type == c_common_unsigned_type (cur_type)
02242 : wanted_type == c_common_signed_type (cur_type)))
02243 continue;
02244
02245
02246 if (wanted_type == char_type_node
02247 && (!pedantic || i < 2)
02248 && char_type_flag)
02249 continue;
02250
02251 format_type_warning (types->name, format_start, format_length,
02252 wanted_type, types->pointer_count,
02253 types->wanted_type_name, orig_cur_type, arg_num);
02254 }
02255 }
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266
02267
02268 static void
02269 format_type_warning (const char *descr, const char *format_start,
02270 int format_length, tree wanted_type, int pointer_count,
02271 const char *wanted_type_name, tree arg_type, int arg_num)
02272 {
02273 char *p;
02274
02275
02276
02277 if (wanted_type_name
02278 && TYPE_NAME (arg_type)
02279 && TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
02280 && DECL_NAME (TYPE_NAME (arg_type))
02281 && !strcmp (wanted_type_name,
02282 lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
02283 arg_type = TYPE_MAIN_VARIANT (arg_type);
02284
02285
02286
02287
02288
02289 p = (char *) alloca (pointer_count + 2);
02290 if (pointer_count == 0)
02291 p[0] = 0;
02292 else if (c_dialect_cxx ())
02293 {
02294 memset (p, '*', pointer_count);
02295 p[pointer_count] = 0;
02296 }
02297 else
02298 {
02299 p[0] = ' ';
02300 memset (p + 1, '*', pointer_count);
02301 p[pointer_count + 1] = 0;
02302 }
02303 if (wanted_type_name)
02304 {
02305 if (descr)
02306 warning (OPT_Wformat, "%s should have type %<%s%s%>, "
02307 "but argument %d has type %qT",
02308 descr, wanted_type_name, p, arg_num, arg_type);
02309 else
02310 warning (OPT_Wformat, "format %q.*s expects type %<%s%s%>, "
02311 "but argument %d has type %qT",
02312 format_length, format_start, wanted_type_name, p,
02313 arg_num, arg_type);
02314 }
02315 else
02316 {
02317 if (descr)
02318 warning (OPT_Wformat, "%s should have type %<%T%s%>, "
02319 "but argument %d has type %qT",
02320 descr, wanted_type, p, arg_num, arg_type);
02321 else
02322 warning (OPT_Wformat, "format %q.*s expects type %<%T%s%>, "
02323 "but argument %d has type %qT",
02324 format_length, format_start, wanted_type, p, arg_num, arg_type);
02325 }
02326 }
02327
02328
02329
02330
02331
02332 static unsigned int
02333 find_char_info_specifier_index (const format_char_info *fci, int c)
02334 {
02335 unsigned i;
02336
02337 for (i = 0; fci->format_chars; i++, fci++)
02338 if (strchr (fci->format_chars, c))
02339 return i;
02340
02341
02342 gcc_unreachable ();
02343 }
02344
02345
02346
02347
02348 static unsigned int
02349 find_length_info_modifier_index (const format_length_info *fli, int c)
02350 {
02351 unsigned i;
02352
02353 for (i = 0; fli->name; i++, fli++)
02354 if (strchr (fli->name, c))
02355 return i;
02356
02357
02358 gcc_unreachable ();
02359 }
02360
02361
02362
02363
02364 static void
02365 init_dynamic_asm_fprintf_info (void)
02366 {
02367 static tree hwi;
02368
02369 if (!hwi)
02370 {
02371 format_length_info *new_asm_fprintf_length_specs;
02372 unsigned int i;
02373
02374
02375
02376
02377
02378 hwi = maybe_get_identifier ("__gcc_host_wide_int__");
02379 if (!hwi)
02380 {
02381 error ("%<__gcc_host_wide_int__%> is not defined as a type");
02382 return;
02383 }
02384 hwi = identifier_global_value (hwi);
02385 if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
02386 {
02387 error ("%<__gcc_host_wide_int__%> is not defined as a type");
02388 return;
02389 }
02390 hwi = DECL_ORIGINAL_TYPE (hwi);
02391 gcc_assert (hwi);
02392 if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
02393 {
02394 error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
02395 " or %<long long%>");
02396 return;
02397 }
02398
02399
02400 new_asm_fprintf_length_specs = (format_length_info *)
02401 xmemdup (asm_fprintf_length_specs,
02402 sizeof (asm_fprintf_length_specs),
02403 sizeof (asm_fprintf_length_specs));
02404
02405
02406 i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
02407 if (hwi == long_integer_type_node)
02408 new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
02409 else if (hwi == long_long_integer_type_node)
02410 new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
02411 else
02412 gcc_unreachable ();
02413
02414
02415 dynamic_format_types[asm_fprintf_format_type].length_char_specs =
02416 new_asm_fprintf_length_specs;
02417 }
02418 }
02419
02420
02421
02422
02423 static void
02424 init_dynamic_gfc_info (void)
02425 {
02426 static tree locus;
02427
02428 if (!locus)
02429 {
02430 static format_char_info *gfc_fci;
02431
02432
02433
02434
02435
02436 if ((locus = maybe_get_identifier ("locus")))
02437 {
02438 locus = identifier_global_value (locus);
02439 if (locus)
02440 {
02441 if (TREE_CODE (locus) != TYPE_DECL)
02442 {
02443 error ("%<locus%> is not defined as a type");
02444 locus = 0;
02445 }
02446 else
02447 locus = TREE_TYPE (locus);
02448 }
02449 }
02450
02451
02452
02453
02454 if (!gfc_fci)
02455 dynamic_format_types[gcc_gfc_format_type].conversion_specs =
02456 gfc_fci = (format_char_info *)
02457 xmemdup (gcc_gfc_char_table,
02458 sizeof (gcc_gfc_char_table),
02459 sizeof (gcc_gfc_char_table));
02460 if (locus)
02461 {
02462 const unsigned i = find_char_info_specifier_index (gfc_fci, 'L');
02463 gfc_fci[i].types[0].type = &locus;
02464 gfc_fci[i].pointer_count = 1;
02465 }
02466 }
02467 }
02468
02469
02470
02471
02472 static void
02473 init_dynamic_diag_info (void)
02474 {
02475 static tree t, loc, hwi;
02476
02477 if (!loc || !t || !hwi)
02478 {
02479 static format_char_info *diag_fci, *tdiag_fci, *cdiag_fci, *cxxdiag_fci;
02480 static format_length_info *diag_ls;
02481 unsigned int i;
02482
02483
02484
02485
02486
02487
02488
02489 if ((loc = maybe_get_identifier ("location_t")))
02490 {
02491 loc = identifier_global_value (loc);
02492 if (loc)
02493 {
02494 if (TREE_CODE (loc) != TYPE_DECL)
02495 {
02496 error ("%<location_t%> is not defined as a type");
02497 loc = 0;
02498 }
02499 else
02500 loc = TREE_TYPE (loc);
02501 }
02502 }
02503
02504
02505
02506 if ((t = maybe_get_identifier ("tree")))
02507 {
02508 t = identifier_global_value (t);
02509 if (t)
02510 {
02511 if (TREE_CODE (t) != TYPE_DECL)
02512 {
02513 error ("%<tree%> is not defined as a type");
02514 t = 0;
02515 }
02516 else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
02517 {
02518 error ("%<tree%> is not defined as a pointer type");
02519 t = 0;
02520 }
02521 else
02522 t = TREE_TYPE (TREE_TYPE (t));
02523 }
02524 }
02525
02526
02527
02528
02529
02530 if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
02531 {
02532 hwi = identifier_global_value (hwi);
02533 if (hwi)
02534 {
02535 if (TREE_CODE (hwi) != TYPE_DECL)
02536 {
02537 error ("%<__gcc_host_wide_int__%> is not defined as a type");
02538 hwi = 0;
02539 }
02540 else
02541 {
02542 hwi = DECL_ORIGINAL_TYPE (hwi);
02543 gcc_assert (hwi);
02544 if (hwi != long_integer_type_node
02545 && hwi != long_long_integer_type_node)
02546 {
02547 error ("%<__gcc_host_wide_int__%> is not defined"
02548 " as %<long%> or %<long long%>");
02549 hwi = 0;
02550 }
02551 }
02552 }
02553 }
02554
02555
02556
02557
02558 if (!diag_ls)
02559 dynamic_format_types[gcc_diag_format_type].length_char_specs =
02560 dynamic_format_types[gcc_tdiag_format_type].length_char_specs =
02561 dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
02562 dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
02563 diag_ls = (format_length_info *)
02564 xmemdup (gcc_diag_length_specs,
02565 sizeof (gcc_diag_length_specs),
02566 sizeof (gcc_diag_length_specs));
02567 if (hwi)
02568 {
02569
02570 i = find_length_info_modifier_index (diag_ls, 'w');
02571 if (hwi == long_integer_type_node)
02572 diag_ls[i].index = FMT_LEN_l;
02573 else if (hwi == long_long_integer_type_node)
02574 diag_ls[i].index = FMT_LEN_ll;
02575 else
02576 gcc_unreachable ();
02577 }
02578
02579
02580 if (!diag_fci)
02581 dynamic_format_types[gcc_diag_format_type].conversion_specs =
02582 diag_fci = (format_char_info *)
02583 xmemdup (gcc_diag_char_table,
02584 sizeof (gcc_diag_char_table),
02585 sizeof (gcc_diag_char_table));
02586 if (loc)
02587 {
02588 i = find_char_info_specifier_index (diag_fci, 'H');
02589 diag_fci[i].types[0].type = &loc;
02590 diag_fci[i].pointer_count = 1;
02591 }
02592 if (t)
02593 {
02594 i = find_char_info_specifier_index (diag_fci, 'J');
02595 diag_fci[i].types[0].type = &t;
02596 diag_fci[i].pointer_count = 1;
02597 }
02598
02599
02600 if (!tdiag_fci)
02601 dynamic_format_types[gcc_tdiag_format_type].conversion_specs =
02602 tdiag_fci = (format_char_info *)
02603 xmemdup (gcc_tdiag_char_table,
02604 sizeof (gcc_tdiag_char_table),
02605 sizeof (gcc_tdiag_char_table));
02606 if (loc)
02607 {
02608 i = find_char_info_specifier_index (tdiag_fci, 'H');
02609 tdiag_fci[i].types[0].type = &loc;
02610 tdiag_fci[i].pointer_count = 1;
02611 }
02612 if (t)
02613 {
02614
02615 i = find_char_info_specifier_index (tdiag_fci, 'D');
02616 tdiag_fci[i].types[0].type = &t;
02617 tdiag_fci[i].pointer_count = 1;
02618 i = find_char_info_specifier_index (tdiag_fci, 'J');
02619 tdiag_fci[i].types[0].type = &t;
02620 tdiag_fci[i].pointer_count = 1;
02621 }
02622
02623
02624 if (!cdiag_fci)
02625 dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
02626 cdiag_fci = (format_char_info *)
02627 xmemdup (gcc_cdiag_char_table,
02628 sizeof (gcc_cdiag_char_table),
02629 sizeof (gcc_cdiag_char_table));
02630 if (loc)
02631 {
02632 i = find_char_info_specifier_index (cdiag_fci, 'H');
02633 cdiag_fci[i].types[0].type = &loc;
02634 cdiag_fci[i].pointer_count = 1;
02635 }
02636 if (t)
02637 {
02638
02639 i = find_char_info_specifier_index (cdiag_fci, 'D');
02640 cdiag_fci[i].types[0].type = &t;
02641 cdiag_fci[i].pointer_count = 1;
02642 i = find_char_info_specifier_index (cdiag_fci, 'J');
02643 cdiag_fci[i].types[0].type = &t;
02644 cdiag_fci[i].pointer_count = 1;
02645 }
02646
02647
02648 if (!cxxdiag_fci)
02649 dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
02650 cxxdiag_fci = (format_char_info *)
02651 xmemdup (gcc_cxxdiag_char_table,
02652 sizeof (gcc_cxxdiag_char_table),
02653 sizeof (gcc_cxxdiag_char_table));
02654 if (loc)
02655 {
02656 i = find_char_info_specifier_index (cxxdiag_fci, 'H');
02657 cxxdiag_fci[i].types[0].type = &loc;
02658 cxxdiag_fci[i].pointer_count = 1;
02659 }
02660 if (t)
02661 {
02662
02663 i = find_char_info_specifier_index (cxxdiag_fci, 'D');
02664 cxxdiag_fci[i].types[0].type = &t;
02665 cxxdiag_fci[i].pointer_count = 1;
02666 i = find_char_info_specifier_index (cxxdiag_fci, 'J');
02667 cxxdiag_fci[i].types[0].type = &t;
02668 cxxdiag_fci[i].pointer_count = 1;
02669 }
02670 }
02671 }
02672
02673 #ifdef TARGET_FORMAT_TYPES
02674 extern const format_kind_info TARGET_FORMAT_TYPES[];
02675 #endif
02676
02677
02678
02679 tree
02680 handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
02681 int flags, bool *no_add_attrs)
02682 {
02683 tree type = *node;
02684 function_format_info info;
02685 tree argument;
02686
02687 #ifdef TARGET_FORMAT_TYPES
02688
02689
02690 if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
02691 {
02692 dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
02693 * sizeof (dynamic_format_types[0]));
02694 memcpy (dynamic_format_types, format_types_orig,
02695 sizeof (format_types_orig));
02696 memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
02697 TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
02698
02699 format_types = dynamic_format_types;
02700 n_format_types += TARGET_N_FORMAT_TYPES;
02701 }
02702 #endif
02703
02704 if (!decode_format_attr (args, &info, 0))
02705 {
02706 *no_add_attrs = true;
02707 return NULL_TREE;
02708 }
02709
02710 argument = TYPE_ARG_TYPES (type);
02711 if (argument)
02712 {
02713 if (!check_format_string (argument, info.format_num, flags,
02714 no_add_attrs))
02715 return NULL_TREE;
02716
02717 if (info.first_arg_num != 0)
02718 {
02719 unsigned HOST_WIDE_INT arg_num = 1;
02720
02721
02722
02723 while (argument)
02724 arg_num++, argument = TREE_CHAIN (argument);
02725
02726 if (arg_num != info.first_arg_num)
02727 {
02728 if (!(flags & (int) ATTR_FLAG_BUILT_IN))
02729 error ("args to be formatted is not %<...%>");
02730 *no_add_attrs = true;
02731 return NULL_TREE;
02732 }
02733 }
02734 }
02735
02736 if (info.format_type == strftime_format_type && info.first_arg_num != 0)
02737 {
02738 error ("strftime formats cannot format arguments");
02739 *no_add_attrs = true;
02740 return NULL_TREE;
02741 }
02742
02743
02744
02745 if (info.format_type == asm_fprintf_format_type
02746 || info.format_type == gcc_gfc_format_type
02747 || info.format_type == gcc_diag_format_type
02748 || info.format_type == gcc_tdiag_format_type
02749 || info.format_type == gcc_cdiag_format_type
02750 || info.format_type == gcc_cxxdiag_format_type)
02751 {
02752
02753
02754 if (!dynamic_format_types)
02755 format_types = dynamic_format_types = (format_kind_info *)
02756 xmemdup (format_types_orig, sizeof (format_types_orig),
02757 sizeof (format_types_orig));
02758
02759
02760
02761 if (info.format_type == asm_fprintf_format_type)
02762 init_dynamic_asm_fprintf_info ();
02763
02764
02765 else if (info.format_type == gcc_gfc_format_type)
02766 init_dynamic_gfc_info ();
02767
02768
02769 else if (info.format_type == gcc_diag_format_type
02770 || info.format_type == gcc_tdiag_format_type
02771 || info.format_type == gcc_cdiag_format_type
02772 || info.format_type == gcc_cxxdiag_format_type)
02773 init_dynamic_diag_info ();
02774 else
02775 gcc_unreachable ();
02776 }
02777
02778 return NULL_TREE;
02779 }