#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "flags.h"
#include "toplev.h"
#include "c-common.h"
#include "intl.h"
#include "diagnostic.h"
#include "langhooks.h"
#include "tm_p.h"
#include "c-format.h"
void
set_Wformat (int setting)
{
warn_format = setting;
warn_format_extra_args = setting;
warn_format_zero_length = setting;
if (setting != 1)
{
warn_format_nonliteral = setting;
warn_format_security = setting;
warn_format_y2k = setting;
}
if (setting)
warn_nonnull = setting;
}
enum format_type { printf_format_type, asm_fprintf_format_type,
gcc_diag_format_type, gcc_cdiag_format_type,
gcc_cxxdiag_format_type,
scanf_format_type, strftime_format_type,
strfmon_format_type, nsstring_format_type,
cfstring_format_type,
format_type_error = -1};
typedef struct function_format_info
{
int format_type;
unsigned HOST_WIDE_INT format_num;
unsigned HOST_WIDE_INT first_arg_num;
} function_format_info;
static bool decode_format_attr (tree, function_format_info *, int);
static int decode_format_type (const char *);
static bool check_format_string (tree argument,
unsigned HOST_WIDE_INT format_num,
int flags, bool *no_add_attrs);
static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
int validated_p);
tree
handle_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
tree args, int flags, bool *no_add_attrs)
{
tree type = *node;
tree format_num_expr = TREE_VALUE (args);
unsigned HOST_WIDE_INT format_num = 0;
tree argument;
if (!get_constant (format_num_expr, &format_num, 0))
{
error ("format string has invalid operand number");
*no_add_attrs = true;
return NULL_TREE;
}
argument = TYPE_ARG_TYPES (type);
if (argument)
{
if (!check_format_string (argument, format_num, flags, no_add_attrs))
return NULL_TREE;
}
if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
|| (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
!= char_type_node))
{
if (!(flags & (int) ATTR_FLAG_BUILT_IN))
error ("function does not return string type");
*no_add_attrs = true;
return NULL_TREE;
}
return NULL_TREE;
}
static bool
check_format_string (tree argument, unsigned HOST_WIDE_INT format_num,
int flags, bool *no_add_attrs)
{
unsigned HOST_WIDE_INT i;
for (i = 1; i != format_num; i++)
{
if (argument == 0)
break;
argument = TREE_CHAIN (argument);
}
if (!argument
|| TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
|| (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
!= char_type_node))
{
if (!(flags & (int) ATTR_FLAG_BUILT_IN))
error ("format string argument not a string type");
*no_add_attrs = true;
return false;
}
return true;
}
static bool
get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
{
while (TREE_CODE (expr) == NOP_EXPR
|| TREE_CODE (expr) == CONVERT_EXPR
|| TREE_CODE (expr) == NON_LVALUE_EXPR)
expr = TREE_OPERAND (expr, 0);
if (TREE_CODE (expr) != INTEGER_CST || TREE_INT_CST_HIGH (expr) != 0)
{
gcc_assert (!validated_p);
return false;
}
*value = TREE_INT_CST_LOW (expr);
return true;
}
static bool
decode_format_attr (tree args, function_format_info *info, int validated_p)
{
tree format_type_id = TREE_VALUE (args);
tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
tree first_arg_num_expr
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
{
gcc_assert (!validated_p);
error ("unrecognized format specifier");
return false;
}
else
{
const char *p = IDENTIFIER_POINTER (format_type_id);
info->format_type = decode_format_type (p);
if (info->format_type == format_type_error
|| (info->format_type == nsstring_format_type
&& !c_dialect_objc ()))
{
gcc_assert (!validated_p);
warning ("%qs is an unrecognized format function type", p);
return false;
}
}
if (!get_constant (format_num_expr, &info->format_num, validated_p))
{
error ("format string has invalid operand number");
return false;
}
if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
{
error ("%<...%> has invalid operand number");
return false;
}
if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
{
gcc_assert (!validated_p);
error ("format string argument follows the args to be formatted");
return false;
}
return true;
}
#define CPLUSPLUS_STD_VER STD_C94
#define C_STD_VER ((int) (c_dialect_cxx () \
? CPLUSPLUS_STD_VER \
: (flag_isoc99 \
? STD_C99 \
: (flag_isoc94 ? STD_C94 : STD_C89))))
#define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
? "ISO C++" \
: ((FEATURE_VER) == STD_EXT \
? "ISO C" \
: "ISO C90"))
#define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
? (warn_long_long ? STD_C99 : STD_C89) \
: (VER)))
typedef struct format_wanted_type
{
tree wanted_type;
const char *wanted_type_name;
int pointer_count;
int char_lenient_flag;
int writing_in_flag;
int reading_from_flag;
const char *name;
tree param;
int arg_num;
struct format_wanted_type *next;
} format_wanted_type;
static const format_length_info printf_length_specs[] =
{
{ "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
{ "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
{ "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
{ "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
{ "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
{ "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
{ "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
{ "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
{ NULL, 0, 0, NULL, 0, 0 }
};
static const format_length_info asm_fprintf_length_specs[] =
{
{ "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
{ "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
{ NULL, 0, 0, NULL, 0, 0 }
};
static const format_length_info gcc_diag_length_specs[] =
{
{ "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89 },
{ "w", FMT_LEN_none, STD_C89, NULL, 0, 0 },
{ NULL, 0, 0, NULL, 0, 0 }
};
#define gcc_cdiag_length_specs gcc_diag_length_specs
#define gcc_cxxdiag_length_specs gcc_diag_length_specs
static const format_length_info scanf_length_specs[] =
{
{ "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
{ "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L },
{ "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
{ "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
{ "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
{ "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
{ "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
{ NULL, 0, 0, NULL, 0, 0 }
};
static const format_length_info strfmon_length_specs[] =
{
{ "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
{ NULL, 0, 0, NULL, 0, 0 }
};
static const format_flag_spec printf_flag_specs[] =
{
{ ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
{ '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
{ '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
{ '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
{ '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
{ '\'', 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
{ 'I', 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
{ 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
{ 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
{ 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
{ 0, 0, 0, NULL, NULL, 0 }
};
static const format_flag_pair printf_flag_pairs[] =
{
{ ' ', '+', 1, 0 },
{ '0', '-', 1, 0 },
{ '0', 'p', 1, 'i' },
{ 0, 0, 0, 0 }
};
static const format_flag_spec asm_fprintf_flag_specs[] =
{
{ ' ', 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
{ '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
{ '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
{ '0', 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
{ '-', 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
{ 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
{ 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
{ 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
{ 0, 0, 0, NULL, NULL, 0 }
};
static const format_flag_pair asm_fprintf_flag_pairs[] =
{
{ ' ', '+', 1, 0 },
{ '0', '-', 1, 0 },
{ '0', 'p', 1, 'i' },
{ 0, 0, 0, 0 }
};
static const format_flag_pair gcc_diag_flag_pairs[] =
{
{ 0, 0, 0, 0 }
};
#define gcc_cdiag_flag_pairs gcc_diag_flag_pairs
#define gcc_cxxdiag_flag_pairs gcc_diag_flag_pairs
static const format_flag_spec gcc_diag_flag_specs[] =
{
{ 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
{ 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
{ 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
{ 0, 0, 0, NULL, NULL, 0 }
};
#define gcc_cdiag_flag_specs gcc_diag_flag_specs
static const format_flag_spec gcc_cxxdiag_flag_specs[] =
{
{ '+', 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
{ '#', 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
{ 'q', 0, 0, N_("'q' flag"), N_("the 'q' diagnostic flag"), STD_C89 },
{ 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
{ 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
{ 0, 0, 0, NULL, NULL, 0 }
};
static const format_flag_spec scanf_flag_specs[] =
{
{ '*', 0, 0, N_("assignment suppression"), N_("the assignment suppression scanf feature"), STD_C89 },
{ 'a', 0, 0, N_("'a' flag"), N_("the 'a' scanf flag"), STD_EXT },
{ 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
{ 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
{ '\'', 0, 0, N_("''' flag"), N_("the ''' scanf flag"), STD_EXT },
{ 'I', 0, 0, N_("'I' flag"), N_("the 'I' scanf flag"), STD_EXT },
{ 0, 0, 0, NULL, NULL, 0 }
};
static const format_flag_pair scanf_flag_pairs[] =
{
{ '*', 'L', 0, 0 },
{ 0, 0, 0, 0 }
};
static const format_flag_spec strftime_flag_specs[] =
{
{ '_', 0, 0, N_("'_' flag"), N_("the '_' strftime flag"), STD_EXT },
{ '-', 0, 0, N_("'-' flag"), N_("the '-' strftime flag"), STD_EXT },
{ '0', 0, 0, N_("'0' flag"), N_("the '0' strftime flag"), STD_EXT },
{ '^', 0, 0, N_("'^' flag"), N_("the '^' strftime flag"), STD_EXT },
{ '#', 0, 0, N_("'#' flag"), N_("the '#' strftime flag"), STD_EXT },
{ 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
{ 'E', 0, 0, N_("'E' modifier"), N_("the 'E' strftime modifier"), STD_C99 },
{ 'O', 0, 0, N_("'O' modifier"), N_("the 'O' strftime modifier"), STD_C99 },
{ 'O', 'o', 0, NULL, N_("the 'O' modifier"), STD_EXT },
{ 0, 0, 0, NULL, NULL, 0 }
};
static const format_flag_pair strftime_flag_pairs[] =
{
{ 'E', 'O', 0, 0 },
{ '_', '-', 0, 0 },
{ '_', '0', 0, 0 },
{ '-', '0', 0, 0 },
{ '^', '#', 0, 0 },
{ 0, 0, 0, 0 }
};
static const format_flag_spec strfmon_flag_specs[] =
{
{ '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 },
{ '^', 0, 0, N_("'^' flag"), N_("the '^' strfmon flag"), STD_C89 },
{ '+', 0, 0, N_("'+' flag"), N_("the '+' strfmon flag"), STD_C89 },
{ '(', 0, 0, N_("'(' flag"), N_("the '(' strfmon flag"), STD_C89 },
{ '!', 0, 0, N_("'!' flag"), N_("the '!' strfmon flag"), STD_C89 },
{ '-', 0, 0, N_("'-' flag"), N_("the '-' strfmon flag"), STD_C89 },
{ 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 },
{ '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 },
{ 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 },
{ 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 },
{ 0, 0, 0, NULL, NULL, 0 }
};
static const format_flag_pair strfmon_flag_pairs[] =
{
{ '+', '(', 0, 0 },
{ 0, 0, 0, 0 }
};
static const format_char_info print_char_table[] =
{
{ "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i", NULL },
{ "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i", NULL },
{ "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i", NULL },
{ "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
{ "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#I", "", NULL },
{ "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
{ "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c", NULL },
{ "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
{ "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'I", "", NULL },
{ "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL },
{ "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
{ "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL },
{ "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info asm_fprintf_char_table[] =
{
{ "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +", "i", NULL },
{ "oxX", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL },
{ "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0", "i", NULL },
{ "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL },
{ "O", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "R", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "I", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "L", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "U", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "r", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "", "", NULL },
{ "@", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info gcc_diag_char_table[] =
{
{ "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
{ "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
{ "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "J", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info gcc_cdiag_char_table[] =
{
{ "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
{ "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
{ "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "DEFJT", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info gcc_cxxdiag_char_table[] =
{
{ "di", 0, STD_C89, { T89_I, BADLEN, BADLEN, T89_L, T9L_LL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "ox", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "u", 0, STD_C89, { T89_UI, BADLEN, BADLEN, T89_UL, T9L_ULL, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "pq", "cR", NULL },
{ "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "c", NULL },
{ "H", 0, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "ADEFJTV",0,STD_C89,{ T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q+#", "", NULL },
{ "CLOPQ",0,STD_C89, { T89_I, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "q", "", NULL },
{ "<>'", 0, STD_C89, NOARGUMENTS, "", "", NULL },
{ "m", 0, STD_C89, NOARGUMENTS, "q", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info scan_char_table[] =
{
{ "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W", NULL },
{ "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W", NULL },
{ "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W", NULL },
{ "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
{ "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW", NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW", NULL },
{ "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[", NULL },
{ "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
{ "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W", NULL },
{ "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W", NULL },
{ "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W", NULL },
{ "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info time_char_table[] =
{
{ "ABZab", 0, STD_C89, NOLENGTHS, "^#", "", NULL },
{ "cx", 0, STD_C89, NOLENGTHS, "E", "3", NULL },
{ "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "", NULL },
{ "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o", NULL },
{ "p", 0, STD_C89, NOLENGTHS, "#", "", NULL },
{ "X", 0, STD_C89, NOLENGTHS, "E", "", NULL },
{ "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4", NULL },
{ "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o", NULL },
{ "%", 0, STD_C89, NOLENGTHS, "", "", NULL },
{ "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o", NULL },
{ "D", 0, STD_C99, NOLENGTHS, "", "2", NULL },
{ "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "", NULL },
{ "FRTnrt", 0, STD_C99, NOLENGTHS, "", "", NULL },
{ "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o", NULL },
{ "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o", NULL },
{ "h", 0, STD_C99, NOLENGTHS, "^#", "", NULL },
{ "z", 0, STD_C99, NOLENGTHS, "O", "o", NULL },
{ "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "", NULL },
{ "P", 0, STD_EXT, NOLENGTHS, "", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_char_info monetary_char_table[] =
{
{ "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "", NULL },
{ NULL, 0, 0, NOLENGTHS, NULL, NULL, NULL }
};
static const format_kind_info format_types_orig[] =
{
{ "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
printf_flag_specs, printf_flag_pairs,
FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
'w', 0, 'p', 0, 'L',
&integer_type_node, &integer_type_node
},
{ "asm_fprintf", asm_fprintf_length_specs, asm_fprintf_char_table, " +#0-", NULL,
asm_fprintf_flag_specs, asm_fprintf_flag_pairs,
FMT_FLAG_ARG_CONVERT|FMT_FLAG_EMPTY_PREC_OK,
'w', 0, 'p', 0, 'L',
NULL, NULL
},
{ "gcc_diag", gcc_diag_length_specs, gcc_diag_char_table, "q", NULL,
gcc_diag_flag_specs, gcc_diag_flag_pairs,
FMT_FLAG_ARG_CONVERT,
0, 0, 'p', 0, 'L',
NULL, &integer_type_node
},
{ "gcc_cdiag", gcc_cdiag_length_specs, gcc_cdiag_char_table, "q", NULL,
gcc_cdiag_flag_specs, gcc_cdiag_flag_pairs,
FMT_FLAG_ARG_CONVERT,
0, 0, 'p', 0, 'L',
NULL, &integer_type_node
},
{ "gcc_cxxdiag", gcc_cxxdiag_length_specs, gcc_cxxdiag_char_table, "q+#", NULL,
gcc_cxxdiag_flag_specs, gcc_cxxdiag_flag_pairs,
FMT_FLAG_ARG_CONVERT,
0, 0, 'p', 0, 'L',
NULL, &integer_type_node
},
{ "scanf", scanf_length_specs, scan_char_table, "*'I", NULL,
scanf_flag_specs, scanf_flag_pairs,
FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD|FMT_FLAG_DOLLAR_GAP_POINTER_OK,
'w', 0, 0, '*', 'L',
NULL, NULL
},
{ "strftime", NULL, time_char_table, "_-0^#", "EO",
strftime_flag_specs, strftime_flag_pairs,
FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0,
NULL, NULL
},
{ "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL,
strfmon_flag_specs, strfmon_flag_pairs,
FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L',
NULL, NULL
},
{ "NSString", NULL, NULL, NULL, NULL,
NULL, NULL,
FMT_FLAG_ARG_CONVERT, 0, 0, 0, 0, 0,
NULL, NULL
},
{ "CFString", NULL, NULL, NULL, NULL,
NULL, NULL,
FMT_FLAG_ARG_CONVERT, 0, 0, 0, 0, 0,
NULL, NULL
}
};
static const format_kind_info *format_types = format_types_orig;
static format_kind_info *dynamic_format_types;
static int n_format_types = ARRAY_SIZE (format_types_orig);
typedef struct
{
int number_non_literal;
int number_extra_args;
int number_dollar_extra_args;
int number_wide;
int number_empty;
int number_unterminated;
int number_other;
} format_check_results;
typedef struct
{
format_check_results *res;
function_format_info *info;
tree params;
} format_check_context;
static void check_format_info (function_format_info *, tree);
static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
static void check_format_info_main (format_check_results *,
function_format_info *,
const char *, int, tree,
unsigned HOST_WIDE_INT);
static void init_dollar_format_checking (int, tree);
static int maybe_read_dollar_number (const char **, int,
tree, tree *, const format_kind_info *);
static bool avoid_dollar_number (const char *);
static void finish_dollar_format_checking (format_check_results *, int);
static const format_flag_spec *get_flag_spec (const format_flag_spec *,
int, const char *);
static void check_format_types (format_wanted_type *, const char *, int);
static void format_type_warning (const char *, const char *, int, tree,
int, const char *, tree, int);
static int
decode_format_type (const char *s)
{
int i;
int slen;
slen = strlen (s);
for (i = 0; i < n_format_types; i++)
{
int alen;
if (!strcmp (s, format_types[i].name))
return i;
alen = strlen (format_types[i].name);
if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
&& s[slen - 1] == '_' && s[slen - 2] == '_'
&& !strncmp (s + 2, format_types[i].name, alen))
return i;
}
return format_type_error;
}
void
check_function_format (tree attrs, tree params)
{
tree a;
for (a = attrs; a; a = TREE_CHAIN (a))
{
if (is_attribute_p ("format", TREE_PURPOSE (a)))
{
function_format_info info;
decode_format_attr (TREE_VALUE (a), &info, 1);
check_format_info (&info, params);
if (warn_missing_format_attribute && info.first_arg_num == 0
&& (format_types[info.format_type].flags
& (int) FMT_FLAG_ARG_CONVERT))
{
tree c;
for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
c;
c = TREE_CHAIN (c))
if (is_attribute_p ("format", TREE_PURPOSE (c))
&& (decode_format_type (IDENTIFIER_POINTER
(TREE_VALUE (TREE_VALUE (c))))
== info.format_type))
break;
if (c == NULL_TREE)
{
tree args;
for (args = DECL_ARGUMENTS (current_function_decl);
args != 0;
args = TREE_CHAIN (args))
{
if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
&& (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
== char_type_node))
break;
}
if (args != 0)
warning ("function might be possible candidate for %qs format attribute",
format_types[info.format_type].name);
}
}
}
}
}
static char *dollar_arguments_used = NULL;
static char *dollar_arguments_pointer_p = NULL;
static int dollar_arguments_alloc = 0;
static int dollar_arguments_count;
static int dollar_first_arg_num;
static int dollar_max_arg_used;
static int dollar_format_warned;
static void
init_dollar_format_checking (int first_arg_num, tree params)
{
tree oparams = params;
dollar_first_arg_num = first_arg_num;
dollar_arguments_count = 0;
dollar_max_arg_used = 0;
dollar_format_warned = 0;
if (first_arg_num > 0)
{
while (params)
{
dollar_arguments_count++;
params = TREE_CHAIN (params);
}
}
if (dollar_arguments_alloc < dollar_arguments_count)
{
if (dollar_arguments_used)
free (dollar_arguments_used);
if (dollar_arguments_pointer_p)
free (dollar_arguments_pointer_p);
dollar_arguments_alloc = dollar_arguments_count;
dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
}
if (dollar_arguments_alloc)
{
memset (dollar_arguments_used, 0, dollar_arguments_alloc);
if (first_arg_num > 0)
{
int i = 0;
params = oparams;
while (params)
{
dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
== POINTER_TYPE);
params = TREE_CHAIN (params);
i++;
}
}
}
}
static int
maybe_read_dollar_number (const char **format,
int dollar_needed, tree params, tree *param_ptr,
const format_kind_info *fki)
{
int argnum;
int overflow_flag;
const char *fcp = *format;
if (!ISDIGIT (*fcp))
{
if (dollar_needed)
{
warning ("missing $ operand number in format");
return -1;
}
else
return 0;
}
argnum = 0;
overflow_flag = 0;
while (ISDIGIT (*fcp))
{
int nargnum;
nargnum = 10 * argnum + (*fcp - '0');
if (nargnum < 0 || nargnum / 10 != argnum)
overflow_flag = 1;
argnum = nargnum;
fcp++;
}
if (*fcp != '$')
{
if (dollar_needed)
{
warning ("missing $ operand number in format");
return -1;
}
else
return 0;
}
*format = fcp + 1;
if (pedantic && !dollar_format_warned)
{
warning ("%s does not support %%n$ operand number formats",
C_STD_NAME (STD_EXT));
dollar_format_warned = 1;
}
if (overflow_flag || argnum == 0
|| (dollar_first_arg_num && argnum > dollar_arguments_count))
{
warning ("operand number out of range in format");
return -1;
}
if (argnum > dollar_max_arg_used)
dollar_max_arg_used = argnum;
while (dollar_arguments_alloc < dollar_max_arg_used)
{
int nalloc;
nalloc = 2 * dollar_arguments_alloc + 16;
dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
nalloc);
dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
nalloc);
memset (dollar_arguments_used + dollar_arguments_alloc, 0,
nalloc - dollar_arguments_alloc);
dollar_arguments_alloc = nalloc;
}
if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
&& dollar_arguments_used[argnum - 1] == 1)
{
dollar_arguments_used[argnum - 1] = 2;
warning ("format argument %d used more than once in %s format",
argnum, fki->name);
}
else
dollar_arguments_used[argnum - 1] = 1;
if (dollar_first_arg_num)
{
int i;
*param_ptr = params;
for (i = 1; i < argnum && *param_ptr != 0; i++)
*param_ptr = TREE_CHAIN (*param_ptr);
gcc_assert (*param_ptr);
}
else
*param_ptr = 0;
return argnum;
}
static bool
avoid_dollar_number (const char *format)
{
if (!ISDIGIT (*format))
return false;
while (ISDIGIT (*format))
format++;
if (*format == '$')
{
warning ("$ operand number used after format without operand number");
return true;
}
return false;
}
static void
finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
{
int i;
bool found_pointer_gap = false;
for (i = 0; i < dollar_max_arg_used; i++)
{
if (!dollar_arguments_used[i])
{
if (pointer_gap_ok && (dollar_first_arg_num == 0
|| dollar_arguments_pointer_p[i]))
found_pointer_gap = true;
else
warning ("format argument %d unused before used argument %d in $-style format",
i + 1, dollar_max_arg_used);
}
}
if (found_pointer_gap
|| (dollar_first_arg_num
&& dollar_max_arg_used < dollar_arguments_count))
{
res->number_other--;
res->number_dollar_extra_args++;
}
}
static const format_flag_spec *
get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
{
int i;
for (i = 0; spec[i].flag_char != 0; i++)
{
if (spec[i].flag_char != flag)
continue;
if (predicates != NULL)
{
if (spec[i].predicate != 0
&& strchr (predicates, spec[i].predicate) != 0)
return &spec[i];
}
else if (spec[i].predicate == 0)
return &spec[i];
}
gcc_assert (predicates);
return NULL;
}
static void
objc_check_cfformat_arg (void *ctx, tree format_tree,
unsigned HOST_WIDE_INT ARG_UNUSED (arg_num))
{
format_check_results *res = ((format_check_context *)ctx)->res;
if (TREE_CODE (format_tree) != ADDR_EXPR)
{
res->number_non_literal++;
return;
}
#ifdef CFSTRING_TYPE_NODE
format_tree = TREE_OPERAND (format_tree, 0);
if (TREE_CODE (format_tree) != CONST_DECL
|| !CFSTRING_TYPE_NODE (TREE_TYPE (format_tree)))
#endif
{
res->number_non_literal++;
return;
}
}
static void
check_format_info (function_format_info *info, tree params)
{
format_check_context format_ctx;
unsigned HOST_WIDE_INT arg_num;
tree format_tree;
format_check_results res;
for (arg_num = 1; ; ++arg_num)
{
if (params == 0)
return;
if (arg_num == info->format_num)
break;
params = TREE_CHAIN (params);
}
format_tree = TREE_VALUE (params);
params = TREE_CHAIN (params);
if (format_tree == 0)
return;
res.number_non_literal = 0;
res.number_extra_args = 0;
res.number_dollar_extra_args = 0;
res.number_wide = 0;
res.number_empty = 0;
res.number_unterminated = 0;
res.number_other = 0;
format_ctx.res = &res;
format_ctx.info = info;
format_ctx.params = params;
check_function_arguments_recurse (
((c_dialect_objc ()
&& info->format_type == nsstring_format_type)
|| info->format_type == cfstring_format_type)
? objc_check_cfformat_arg
: check_format_arg, &format_ctx,
format_tree, arg_num);
if (res.number_non_literal > 0)
{
if (!(format_types[info->format_type].flags & (int) FMT_FLAG_ARG_CONVERT))
{
if (warn_format_nonliteral)
warning ("format not a string literal, format string not checked");
}
else if (info->first_arg_num != 0)
{
while (arg_num + 1 < info->first_arg_num)
{
if (params == 0)
break;
params = TREE_CHAIN (params);
++arg_num;
}
if (params == 0 && (warn_format_nonliteral || warn_format_security))
warning ("format not a string literal and no format arguments");
else if (warn_format_nonliteral)
warning ("format not a string literal, argument types not checked");
}
}
if (res.number_extra_args > 0 && res.number_non_literal == 0
&& res.number_other == 0 && warn_format_extra_args)
warning ("too many arguments for format");
if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0
&& res.number_other == 0 && warn_format_extra_args)
warning ("unused arguments in $-style format");
if (res.number_empty > 0 && res.number_non_literal == 0
&& res.number_other == 0 && warn_format_zero_length)
warning ("zero-length %s format string",
format_types[info->format_type].name);
if (res.number_wide > 0)
warning ("format is a wide character string");
if (res.number_unterminated > 0)
warning ("unterminated format string");
}
static void
check_format_arg (void *ctx, tree format_tree,
unsigned HOST_WIDE_INT arg_num)
{
format_check_context *format_ctx = (format_check_context *) ctx;
format_check_results *res = format_ctx->res;
function_format_info *info = format_ctx->info;
tree params = format_ctx->params;
int format_length;
HOST_WIDE_INT offset;
const char *format_chars;
tree array_size = 0;
tree array_init;
if (integer_zerop (format_tree))
{
while (arg_num + 1 < info->first_arg_num)
{
if (params == 0)
return;
params = TREE_CHAIN (params);
++arg_num;
}
if (params == 0)
res->number_other++;
else
res->number_extra_args++;
return;
}
offset = 0;
if (TREE_CODE (format_tree) == PLUS_EXPR)
{
tree arg0, arg1;
arg0 = TREE_OPERAND (format_tree, 0);
arg1 = TREE_OPERAND (format_tree, 1);
STRIP_NOPS (arg0);
STRIP_NOPS (arg1);
if (TREE_CODE (arg1) == INTEGER_CST)
format_tree = arg0;
else if (TREE_CODE (arg0) == INTEGER_CST)
{
format_tree = arg1;
arg1 = arg0;
}
else
{
res->number_non_literal++;
return;
}
if (!host_integerp (arg1, 0)
|| (offset = tree_low_cst (arg1, 0)) < 0)
{
res->number_non_literal++;
return;
}
}
if (TREE_CODE (format_tree) != ADDR_EXPR)
{
res->number_non_literal++;
return;
}
format_tree = TREE_OPERAND (format_tree, 0);
if (TREE_CODE (format_tree) == VAR_DECL
&& TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE
&& (array_init = decl_constant_value (format_tree)) != format_tree
&& TREE_CODE (array_init) == STRING_CST)
{
array_size = DECL_SIZE_UNIT (format_tree);
format_tree = array_init;
}
if (TREE_CODE (format_tree) != STRING_CST)
{
res->number_non_literal++;
return;
}
if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
{
res->number_wide++;
return;
}
format_chars = TREE_STRING_POINTER (format_tree);
format_length = TREE_STRING_LENGTH (format_tree);
if (array_size != 0)
{
gcc_assert (TREE_CODE (array_size) == INTEGER_CST);
if (host_integerp (array_size, 0))
{
HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size);
if (array_size_value > 0
&& array_size_value == (int) array_size_value
&& format_length > array_size_value)
format_length = array_size_value;
}
}
if (offset)
{
if (offset >= format_length)
{
res->number_non_literal++;
return;
}
format_chars += offset;
format_length -= offset;
}
if (format_length < 1)
{
res->number_unterminated++;
return;
}
if (format_length == 1)
{
res->number_empty++;
return;
}
if (format_chars[--format_length] != 0)
{
res->number_unterminated++;
return;
}
while (arg_num + 1 < info->first_arg_num)
{
if (params == 0)
return;
params = TREE_CHAIN (params);
++arg_num;
}
res->number_other++;
check_format_info_main (res, info, format_chars, format_length,
params, arg_num);
}
static void
check_format_info_main (format_check_results *res,
function_format_info *info, const char *format_chars,
int format_length, tree params,
unsigned HOST_WIDE_INT arg_num)
{
const char *orig_format_chars = format_chars;
tree first_fillin_param = params;
const format_kind_info *fki = &format_types[info->format_type];
const format_flag_spec *flag_specs = fki->flag_specs;
const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs;
int has_operand_number = -1;
init_dollar_format_checking (info->first_arg_num, first_fillin_param);
while (1)
{
int i;
int suppressed = FALSE;
const char *length_chars = NULL;
enum format_lengths length_chars_val = FMT_LEN_none;
enum format_std_version length_chars_std = STD_C89;
int format_char;
tree cur_param;
tree wanted_type;
int main_arg_num = 0;
tree main_arg_params = 0;
enum format_std_version wanted_type_std;
const char *wanted_type_name;
format_wanted_type width_wanted_type;
format_wanted_type precision_wanted_type;
format_wanted_type main_wanted_type;
format_wanted_type *first_wanted_type = NULL;
format_wanted_type *last_wanted_type = NULL;
const format_length_info *fli = NULL;
const format_char_info *fci = NULL;
char flag_chars[256];
int aflag = 0;
const char *format_start = format_chars;
if (*format_chars == 0)
{
if (format_chars - orig_format_chars != format_length)
warning ("embedded %<\\0%> in format");
if (info->first_arg_num != 0 && params != 0
&& has_operand_number <= 0)
{
res->number_other--;
res->number_extra_args++;
}
if (has_operand_number > 0)
finish_dollar_format_checking (res, fki->flags & (int) FMT_FLAG_DOLLAR_GAP_POINTER_OK);
return;
}
if (*format_chars++ != '%')
continue;
if (*format_chars == 0)
{
warning ("spurious trailing %<%%%> in format");
continue;
}
if (*format_chars == '%')
{
++format_chars;
continue;
}
flag_chars[0] = 0;
if ((fki->flags & (int) FMT_FLAG_USE_DOLLAR) && has_operand_number != 0)
{
int opnum;
opnum = maybe_read_dollar_number (&format_chars, 0,
first_fillin_param,
&main_arg_params, fki);
if (opnum == -1)
return;
else if (opnum > 0)
{
has_operand_number = 1;
main_arg_num = opnum + info->first_arg_num - 1;
}
}
else if (fki->flags & FMT_FLAG_USE_DOLLAR)
{
if (avoid_dollar_number (format_chars))
return;
}
while (*format_chars != 0
&& strchr (fki->flag_chars, *format_chars) != 0)
{
const format_flag_spec *s = get_flag_spec (flag_specs,
*format_chars, NULL);
if (strchr (flag_chars, *format_chars) != 0)
{
warning ("repeated %s in format", _(s->name));
}
else
{
i = strlen (flag_chars);
flag_chars[i++] = *format_chars;
flag_chars[i] = 0;
}
if (s->skip_next_char)
{
++format_chars;
if (*format_chars == 0)
{
warning ("missing fill character at end of strfmon format");
return;
}
}
++format_chars;
}
if (fki->width_char != 0)
{
if (fki->width_type != NULL && *format_chars == '*')
{
i = strlen (flag_chars);
flag_chars[i++] = fki->width_char;
flag_chars[i] = 0;
++format_chars;
if (has_operand_number != 0)
{
int opnum;
opnum = maybe_read_dollar_number (&format_chars,
has_operand_number == 1,
first_fillin_param,
¶ms, fki);
if (opnum == -1)
return;
else if (opnum > 0)
{
has_operand_number = 1;
arg_num = opnum + info->first_arg_num - 1;
}
else
has_operand_number = 0;
}
else
{
if (avoid_dollar_number (format_chars))
return;
}
if (info->first_arg_num != 0)
{
if (params == 0)
{
warning ("too few arguments for format");
return;
}
cur_param = TREE_VALUE (params);
if (has_operand_number <= 0)
{
params = TREE_CHAIN (params);
++arg_num;
}
width_wanted_type.wanted_type = *fki->width_type;
width_wanted_type.wanted_type_name = NULL;
width_wanted_type.pointer_count = 0;
width_wanted_type.char_lenient_flag = 0;
width_wanted_type.writing_in_flag = 0;
width_wanted_type.reading_from_flag = 0;
width_wanted_type.name = _("field width");
width_wanted_type.param = cur_param;
width_wanted_type.arg_num = arg_num;
width_wanted_type.next = NULL;
if (last_wanted_type != 0)
last_wanted_type->next = &width_wanted_type;
if (first_wanted_type == 0)
first_wanted_type = &width_wanted_type;
last_wanted_type = &width_wanted_type;
}
}
else
{
int non_zero_width_char = FALSE;
int found_width = FALSE;
while (ISDIGIT (*format_chars))
{
found_width = TRUE;
if (*format_chars != '0')
non_zero_width_char = TRUE;
++format_chars;
}
if (found_width && !non_zero_width_char &&
(fki->flags & (int) FMT_FLAG_ZERO_WIDTH_BAD))
warning ("zero width in %s format", fki->name);
if (found_width)
{
i = strlen (flag_chars);
flag_chars[i++] = fki->width_char;
flag_chars[i] = 0;
}
}
}
if (fki->left_precision_char != 0 && *format_chars == '#')
{
++format_chars;
i = strlen (flag_chars);
flag_chars[i++] = fki->left_precision_char;
flag_chars[i] = 0;
if (!ISDIGIT (*format_chars))
warning ("empty left precision in %s format", fki->name);
while (ISDIGIT (*format_chars))
++format_chars;
}
if (fki->precision_char != 0 && *format_chars == '.')
{
++format_chars;
i = strlen (flag_chars);
flag_chars[i++] = fki->precision_char;
flag_chars[i] = 0;
if (fki->precision_type != NULL && *format_chars == '*')
{
++format_chars;
if (has_operand_number != 0)
{
int opnum;
opnum = maybe_read_dollar_number (&format_chars,
has_operand_number == 1,
first_fillin_param,
¶ms, fki);
if (opnum == -1)
return;
else if (opnum > 0)
{
has_operand_number = 1;
arg_num = opnum + info->first_arg_num - 1;
}
else
has_operand_number = 0;
}
else
{
if (avoid_dollar_number (format_chars))
return;
}
if (info->first_arg_num != 0)
{
if (params == 0)
{
warning ("too few arguments for format");
return;
}
cur_param = TREE_VALUE (params);
if (has_operand_number <= 0)
{
params = TREE_CHAIN (params);
++arg_num;
}
precision_wanted_type.wanted_type = *fki->precision_type;
precision_wanted_type.wanted_type_name = NULL;
precision_wanted_type.pointer_count = 0;
precision_wanted_type.char_lenient_flag = 0;
precision_wanted_type.writing_in_flag = 0;
precision_wanted_type.reading_from_flag = 0;
precision_wanted_type.name = _("field precision");
precision_wanted_type.param = cur_param;
precision_wanted_type.arg_num = arg_num;
precision_wanted_type.next = NULL;
if (last_wanted_type != 0)
last_wanted_type->next = &precision_wanted_type;
if (first_wanted_type == 0)
first_wanted_type = &precision_wanted_type;
last_wanted_type = &precision_wanted_type;
}
}
else
{
if (!(fki->flags & (int) FMT_FLAG_EMPTY_PREC_OK)
&& !ISDIGIT (*format_chars))
warning ("empty precision in %s format", fki->name);
while (ISDIGIT (*format_chars))
++format_chars;
}
}
fli = fki->length_char_specs;
length_chars = NULL;
length_chars_val = FMT_LEN_none;
length_chars_std = STD_C89;
if (fli)
{
while (fli->name != 0 && fli->name[0] != *format_chars)
fli++;
if (fli->name != 0)
{
format_chars++;
if (fli->double_name != 0 && fli->name[0] == *format_chars)
{
format_chars++;
length_chars = fli->double_name;
length_chars_val = fli->double_index;
length_chars_std = fli->double_std;
}
else
{
length_chars = fli->name;
length_chars_val = fli->index;
length_chars_std = fli->std;
}
i = strlen (flag_chars);
flag_chars[i++] = fki->length_code_char;
flag_chars[i] = 0;
}
if (pedantic)
{
if (ADJ_STD (length_chars_std) > C_STD_VER)
warning ("%s does not support the %qs %s length modifier",
C_STD_NAME (length_chars_std), length_chars,
fki->name);
}
}
if (fki->modifier_chars != NULL)
{
while (*format_chars != 0
&& strchr (fki->modifier_chars, *format_chars) != 0)
{
if (strchr (flag_chars, *format_chars) != 0)
{
const format_flag_spec *s = get_flag_spec (flag_specs,
*format_chars, NULL);
warning ("repeated %s in format", _(s->name));
}
else
{
i = strlen (flag_chars);
flag_chars[i++] = *format_chars;
flag_chars[i] = 0;
}
++format_chars;
}
}
if (fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
{
if (*format_chars == 'a' && !flag_isoc99)
{
if (format_chars[1] == 's' || format_chars[1] == 'S'
|| format_chars[1] == '[')
{
i = strlen (flag_chars);
flag_chars[i++] = 'a';
flag_chars[i] = 0;
format_chars++;
}
}
}
format_char = *format_chars;
if (format_char == 0
|| (!(fki->flags & (int) FMT_FLAG_FANCY_PERCENT_OK)
&& format_char == '%'))
{
warning ("conversion lacks type at end of format");
continue;
}
format_chars++;
fci = fki->conversion_specs;
while (fci->format_chars != 0
&& strchr (fci->format_chars, format_char) == 0)
++fci;
if (fci->format_chars == 0)
{
if (ISGRAPH (format_char))
warning ("unknown conversion type character %qc in format",
format_char);
else
warning ("unknown conversion type character 0x%x in format",
format_char);
continue;
}
if (pedantic)
{
if (ADJ_STD (fci->std) > C_STD_VER)
warning ("%s does not support the %<%%%c%> %s format",
C_STD_NAME (fci->std), format_char, fki->name);
}
{
int d = 0;
for (i = 0; flag_chars[i] != 0; i++)
{
const format_flag_spec *s = get_flag_spec (flag_specs,
flag_chars[i], NULL);
flag_chars[i - d] = flag_chars[i];
if (flag_chars[i] == fki->length_code_char)
continue;
if (strchr (fci->flag_chars, flag_chars[i]) == 0)
{
warning ("%s used with %<%%%c%> %s format",
_(s->name), format_char, fki->name);
d++;
continue;
}
if (pedantic)
{
const format_flag_spec *t;
if (ADJ_STD (s->std) > C_STD_VER)
warning ("%s does not support %s",
C_STD_NAME (s->std), _(s->long_name));
t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std))
{
const char *long_name = (t->long_name != NULL
? t->long_name
: s->long_name);
if (ADJ_STD (t->std) > C_STD_VER)
warning ("%s does not support %s with the %<%%%c%> %s format",
C_STD_NAME (t->std), _(long_name),
format_char, fki->name);
}
}
}
flag_chars[i - d] = 0;
}
if ((fki->flags & (int) FMT_FLAG_SCANF_A_KLUDGE)
&& strchr (flag_chars, 'a') != 0)
aflag = 1;
if (fki->suppression_char
&& strchr (flag_chars, fki->suppression_char) != 0)
suppressed = 1;
for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
{
const format_flag_spec *s, *t;
if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
continue;
if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
continue;
if (bad_flag_pairs[i].predicate != 0
&& strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0)
continue;
s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
if (bad_flag_pairs[i].ignored)
{
if (bad_flag_pairs[i].predicate != 0)
warning ("%s ignored with %s and %<%%%c%> %s format",
_(s->name), _(t->name), format_char,
fki->name);
else
warning ("%s ignored with %s in %s format",
_(s->name), _(t->name), fki->name);
}
else
{
if (bad_flag_pairs[i].predicate != 0)
warning ("use of %s and %s together with %<%%%c%> %s format",
_(s->name), _(t->name), format_char,
fki->name);
else
warning ("use of %s and %s together in %s format",
_(s->name), _(t->name), fki->name);
}
}
if (warn_format_y2k)
{
int y2k_level = 0;
if (strchr (fci->flags2, '4') != 0)
if (strchr (flag_chars, 'E') != 0)
y2k_level = 3;
else
y2k_level = 2;
else if (strchr (fci->flags2, '3') != 0)
y2k_level = 3;
else if (strchr (fci->flags2, '2') != 0)
y2k_level = 2;
if (y2k_level == 3)
warning ("%<%%%c%> yields only last 2 digits of year in some locales",
format_char);
else if (y2k_level == 2)
warning ("%<%%%c%> yields only last 2 digits of year", format_char);
}
if (strchr (fci->flags2, '[') != 0)
{
if (*format_chars == '^')
++format_chars;
if (*format_chars == ']')
++format_chars;
while (*format_chars && *format_chars != ']')
++format_chars;
if (*format_chars != ']')
warning ("no closing %<]%> for %<%%[%> format");
}
wanted_type = 0;
wanted_type_name = 0;
if (fki->flags & (int) FMT_FLAG_ARG_CONVERT)
{
wanted_type = (fci->types[length_chars_val].type
? *fci->types[length_chars_val].type : 0);
wanted_type_name = fci->types[length_chars_val].name;
wanted_type_std = fci->types[length_chars_val].std;
if (wanted_type == 0)
{
warning ("use of %qs length modifier with %qc type character",
length_chars, format_char);
arg_num++;
if (params == 0)
{
warning ("too few arguments for format");
return;
}
params = TREE_CHAIN (params);
continue;
}
else if (pedantic
&& ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std)
&& ADJ_STD (wanted_type_std) > ADJ_STD (fci->std))
{
if (ADJ_STD (wanted_type_std) > C_STD_VER)
warning ("%s does not support the %<%%%s%c%> %s format",
C_STD_NAME (wanted_type_std), length_chars,
format_char, fki->name);
}
}
main_wanted_type.next = NULL;
if (info->first_arg_num == 0)
continue;
if ((fci->pointer_count == 0 && wanted_type == void_type_node)
|| suppressed)
{
if (main_arg_num != 0)
{
if (suppressed)
warning ("operand number specified with suppressed assignment");
else
warning ("operand number specified for format taking no argument");
}
}
else
{
format_wanted_type *wanted_type_ptr;
if (main_arg_num != 0)
{
arg_num = main_arg_num;
params = main_arg_params;
}
else
{
++arg_num;
if (has_operand_number > 0)
{
warning ("missing $ operand number in format");
return;
}
else
has_operand_number = 0;
}
wanted_type_ptr = &main_wanted_type;
while (fci)
{
if (params == 0)
{
warning ("too few arguments for format");
return;
}
cur_param = TREE_VALUE (params);
params = TREE_CHAIN (params);
wanted_type_ptr->wanted_type = wanted_type;
wanted_type_ptr->wanted_type_name = wanted_type_name;
wanted_type_ptr->pointer_count = fci->pointer_count + aflag;
wanted_type_ptr->char_lenient_flag = 0;
if (strchr (fci->flags2, 'c') != 0)
wanted_type_ptr->char_lenient_flag = 1;
wanted_type_ptr->writing_in_flag = 0;
wanted_type_ptr->reading_from_flag = 0;
if (aflag)
wanted_type_ptr->writing_in_flag = 1;
else
{
if (strchr (fci->flags2, 'W') != 0)
wanted_type_ptr->writing_in_flag = 1;
if (strchr (fci->flags2, 'R') != 0)
wanted_type_ptr->reading_from_flag = 1;
}
wanted_type_ptr->name = NULL;
wanted_type_ptr->param = cur_param;
wanted_type_ptr->arg_num = arg_num;
wanted_type_ptr->next = NULL;
if (last_wanted_type != 0)
last_wanted_type->next = wanted_type_ptr;
if (first_wanted_type == 0)
first_wanted_type = wanted_type_ptr;
last_wanted_type = wanted_type_ptr;
fci = fci->chain;
if (fci)
{
wanted_type_ptr = ggc_alloc (sizeof (main_wanted_type));
arg_num++;
wanted_type = *fci->types[length_chars_val].type;
wanted_type_name = fci->types[length_chars_val].name;
}
}
}
if (first_wanted_type != 0)
check_format_types (first_wanted_type, format_start,
format_chars - format_start);
if (main_wanted_type.next != NULL)
{
format_wanted_type *wanted_type_ptr = main_wanted_type.next;
while (wanted_type_ptr)
{
format_wanted_type *next = wanted_type_ptr->next;
ggc_free (wanted_type_ptr);
wanted_type_ptr = next;
}
}
}
}
static void
check_format_types (format_wanted_type *types, const char *format_start,
int format_length)
{
for (; types != 0; types = types->next)
{
tree cur_param;
tree cur_type;
tree orig_cur_type;
tree wanted_type;
int arg_num;
int i;
int char_type_flag;
cur_param = types->param;
cur_type = TREE_TYPE (cur_param);
if (cur_type == error_mark_node)
continue;
orig_cur_type = cur_type;
char_type_flag = 0;
wanted_type = types->wanted_type;
arg_num = types->arg_num;
gcc_assert (wanted_type);
gcc_assert (wanted_type != void_type_node || types->pointer_count);
if (types->pointer_count == 0)
wanted_type = lang_hooks.types.type_promotes_to (wanted_type);
wanted_type = TYPE_MAIN_VARIANT (wanted_type);
STRIP_NOPS (cur_param);
for (i = 0; i < types->pointer_count; ++i)
{
if (TREE_CODE (cur_type) == POINTER_TYPE)
{
cur_type = TREE_TYPE (cur_type);
if (cur_type == error_mark_node)
break;
if (types->writing_in_flag
&& i == 0
&& cur_param != 0
&& integer_zerop (cur_param))
warning ("writing through null pointer (argument %d)",
arg_num);
if (types->reading_from_flag
&& i == 0
&& cur_param != 0
&& integer_zerop (cur_param))
warning ("reading through null pointer (argument %d)",
arg_num);
if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
cur_param = TREE_OPERAND (cur_param, 0);
else
cur_param = 0;
if (types->writing_in_flag
&& i == 0
&& (TYPE_READONLY (cur_type)
|| (cur_param != 0
&& (CONSTANT_CLASS_P (cur_param)
|| (DECL_P (cur_param)
&& TREE_READONLY (cur_param))))))
warning ("writing into constant object (argument %d)",
arg_num);
if (i > 0
&& pedantic
&& (TYPE_READONLY (cur_type)
|| TYPE_VOLATILE (cur_type)
|| TYPE_RESTRICT (cur_type)))
warning ("extra type qualifiers in format argument "
"(argument %d)",
arg_num);
}
else
{
format_type_warning (types->name, format_start, format_length,
wanted_type, types->pointer_count,
types->wanted_type_name, orig_cur_type,
arg_num);
break;
}
}
if (i < types->pointer_count)
continue;
cur_type = TYPE_MAIN_VARIANT (cur_type);
if (types->char_lenient_flag)
char_type_flag = (cur_type == char_type_node
|| cur_type == signed_char_type_node
|| cur_type == unsigned_char_type_node);
if (wanted_type == cur_type)
continue;
if (wanted_type == void_type_node
&& (!pedantic || (i == 1 && char_type_flag)))
continue;
if (TREE_CODE (wanted_type) == INTEGER_TYPE
&& TREE_CODE (cur_type) == INTEGER_TYPE
&& (!pedantic || i == 0 || (i == 1 && char_type_flag))
&& (TYPE_UNSIGNED (wanted_type)
? wanted_type == c_common_unsigned_type (cur_type)
: wanted_type == c_common_signed_type (cur_type)))
continue;
if (wanted_type == char_type_node
&& (!pedantic || i < 2)
&& char_type_flag)
continue;
format_type_warning (types->name, format_start, format_length,
wanted_type, types->pointer_count,
types->wanted_type_name, orig_cur_type, arg_num);
}
}
static void
format_type_warning (const char *descr, const char *format_start,
int format_length, tree wanted_type, int pointer_count,
const char *wanted_type_name, tree arg_type, int arg_num)
{
char *p;
if (wanted_type_name
&& TYPE_NAME (arg_type)
&& TREE_CODE (TYPE_NAME (arg_type)) == TYPE_DECL
&& DECL_NAME (TYPE_NAME (arg_type))
&& !strcmp (wanted_type_name,
lang_hooks.decl_printable_name (TYPE_NAME (arg_type), 2)))
arg_type = TYPE_MAIN_VARIANT (arg_type);
p = alloca (pointer_count + 2);
if (pointer_count == 0)
p[0] = 0;
else if (c_dialect_cxx ())
{
memset (p, '*', pointer_count);
p[pointer_count] = 0;
}
else
{
p[0] = ' ';
memset (p + 1, '*', pointer_count);
p[pointer_count + 1] = 0;
}
arg_num = objc_message_selector () ? (arg_num-2) : arg_num;
if (wanted_type_name)
{
if (descr)
warning ("%s should have type %<%s%s%>, but argument %d has type %qT",
descr, wanted_type_name, p, arg_num, arg_type);
else
warning ("format %q.*s expects type %<%s%s%>, but argument %d has type %qT",
format_length, format_start, wanted_type_name, p,
arg_num, arg_type);
}
else
{
if (descr)
warning ("%s should have type %<%T%s%>, but argument %d has type %qT",
descr, wanted_type, p, arg_num, arg_type);
else
warning ("format %q.*s expects type %<%T%s%>, but argument %d has type %qT",
format_length, format_start, wanted_type, p, arg_num, arg_type);
}
}
static unsigned int
find_char_info_specifier_index (const format_char_info *fci, int c)
{
unsigned i;
for (i = 0; fci->format_chars; i++, fci++)
if (strchr (fci->format_chars, c))
return i;
gcc_unreachable ();
}
static unsigned int
find_length_info_modifier_index (const format_length_info *fli, int c)
{
unsigned i;
for (i = 0; fli->name; i++, fli++)
if (strchr (fli->name, c))
return i;
gcc_unreachable ();
}
static void
init_dynamic_asm_fprintf_info (void)
{
static tree hwi;
if (!hwi)
{
format_length_info *new_asm_fprintf_length_specs;
unsigned int i;
hwi = maybe_get_identifier ("__gcc_host_wide_int__");
if (!hwi)
{
error ("%<__gcc_host_wide_int__%> is not defined as a type");
return;
}
hwi = identifier_global_value (hwi);
if (!hwi || TREE_CODE (hwi) != TYPE_DECL)
{
error ("%<__gcc_host_wide_int__%> is not defined as a type");
return;
}
hwi = DECL_ORIGINAL_TYPE (hwi);
gcc_assert (hwi);
if (hwi != long_integer_type_node && hwi != long_long_integer_type_node)
{
error ("%<__gcc_host_wide_int__%> is not defined as %<long%>"
" or %<long long%>");
return;
}
new_asm_fprintf_length_specs = (format_length_info *)
xmemdup (asm_fprintf_length_specs,
sizeof (asm_fprintf_length_specs),
sizeof (asm_fprintf_length_specs));
i = find_length_info_modifier_index (new_asm_fprintf_length_specs, 'w');
if (hwi == long_integer_type_node)
new_asm_fprintf_length_specs[i].index = FMT_LEN_l;
else if (hwi == long_long_integer_type_node)
new_asm_fprintf_length_specs[i].index = FMT_LEN_ll;
else
gcc_unreachable ();
dynamic_format_types[asm_fprintf_format_type].length_char_specs =
new_asm_fprintf_length_specs;
}
}
static void
init_dynamic_diag_info (void)
{
static tree t, loc, hwi;
if (!loc || !t || !hwi)
{
static format_char_info *diag_fci, *cdiag_fci, *cxxdiag_fci;
static format_length_info *diag_ls;
unsigned int i;
if ((loc = maybe_get_identifier ("location_t")))
{
loc = identifier_global_value (loc);
if (loc)
{
if (TREE_CODE (loc) != TYPE_DECL)
{
error ("%<location_t%> is not defined as a type");
loc = 0;
}
else
loc = TREE_TYPE (loc);
}
}
if ((t = maybe_get_identifier ("tree")))
{
t = identifier_global_value (t);
if (t)
{
if (TREE_CODE (t) != TYPE_DECL)
{
error ("%<tree%> is not defined as a type");
t = 0;
}
else if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
{
error ("%<tree%> is not defined as a pointer type");
t = 0;
}
else
t = TREE_TYPE (TREE_TYPE (t));
}
}
if ((hwi = maybe_get_identifier ("__gcc_host_wide_int__")))
{
hwi = identifier_global_value (hwi);
if (hwi)
{
if (TREE_CODE (hwi) != TYPE_DECL)
{
error ("%<__gcc_host_wide_int__%> is not defined as a type");
hwi = 0;
}
else
{
hwi = DECL_ORIGINAL_TYPE (hwi);
gcc_assert (hwi);
if (hwi != long_integer_type_node
&& hwi != long_long_integer_type_node)
{
error ("%<__gcc_host_wide_int__%> is not defined"
" as %<long%> or %<long long%>");
hwi = 0;
}
}
}
}
if (!diag_ls)
dynamic_format_types[gcc_diag_format_type].length_char_specs =
dynamic_format_types[gcc_cdiag_format_type].length_char_specs =
dynamic_format_types[gcc_cxxdiag_format_type].length_char_specs =
diag_ls = (format_length_info *)
xmemdup (gcc_diag_length_specs,
sizeof (gcc_diag_length_specs),
sizeof (gcc_diag_length_specs));
if (hwi)
{
i = find_length_info_modifier_index (diag_ls, 'w');
if (hwi == long_integer_type_node)
diag_ls[i].index = FMT_LEN_l;
else if (hwi == long_long_integer_type_node)
diag_ls[i].index = FMT_LEN_ll;
else
gcc_unreachable ();
}
if (!diag_fci)
dynamic_format_types[gcc_diag_format_type].conversion_specs =
diag_fci = (format_char_info *)
xmemdup (gcc_diag_char_table,
sizeof (gcc_diag_char_table),
sizeof (gcc_diag_char_table));
if (loc)
{
i = find_char_info_specifier_index (diag_fci, 'H');
diag_fci[i].types[0].type = &loc;
diag_fci[i].pointer_count = 1;
}
if (t)
{
i = find_char_info_specifier_index (diag_fci, 'J');
diag_fci[i].types[0].type = &t;
diag_fci[i].pointer_count = 1;
}
if (!cdiag_fci)
dynamic_format_types[gcc_cdiag_format_type].conversion_specs =
cdiag_fci = (format_char_info *)
xmemdup (gcc_cdiag_char_table,
sizeof (gcc_cdiag_char_table),
sizeof (gcc_cdiag_char_table));
if (loc)
{
i = find_char_info_specifier_index (cdiag_fci, 'H');
cdiag_fci[i].types[0].type = &loc;
cdiag_fci[i].pointer_count = 1;
}
if (t)
{
i = find_char_info_specifier_index (cdiag_fci, 'D');
cdiag_fci[i].types[0].type = &t;
cdiag_fci[i].pointer_count = 1;
i = find_char_info_specifier_index (cdiag_fci, 'J');
cdiag_fci[i].types[0].type = &t;
cdiag_fci[i].pointer_count = 1;
}
if (!cxxdiag_fci)
dynamic_format_types[gcc_cxxdiag_format_type].conversion_specs =
cxxdiag_fci = (format_char_info *)
xmemdup (gcc_cxxdiag_char_table,
sizeof (gcc_cxxdiag_char_table),
sizeof (gcc_cxxdiag_char_table));
if (loc)
{
i = find_char_info_specifier_index (cxxdiag_fci, 'H');
cxxdiag_fci[i].types[0].type = &loc;
cxxdiag_fci[i].pointer_count = 1;
}
if (t)
{
i = find_char_info_specifier_index (cxxdiag_fci, 'D');
cxxdiag_fci[i].types[0].type = &t;
cxxdiag_fci[i].pointer_count = 1;
i = find_char_info_specifier_index (cxxdiag_fci, 'J');
cxxdiag_fci[i].types[0].type = &t;
cxxdiag_fci[i].pointer_count = 1;
}
}
}
#ifdef TARGET_FORMAT_TYPES
extern const format_kind_info TARGET_FORMAT_TYPES[];
#endif
tree
handle_format_attribute (tree *node, tree ARG_UNUSED (name), tree args,
int flags, bool *no_add_attrs)
{
tree type = *node;
function_format_info info;
tree argument;
#ifdef TARGET_FORMAT_TYPES
if (TARGET_FORMAT_TYPES != NULL && !dynamic_format_types)
{
dynamic_format_types = xmalloc ((n_format_types + TARGET_N_FORMAT_TYPES)
* sizeof (dynamic_format_types[0]));
memcpy (dynamic_format_types, format_types_orig,
sizeof (format_types_orig));
memcpy (&dynamic_format_types[n_format_types], TARGET_FORMAT_TYPES,
TARGET_N_FORMAT_TYPES * sizeof (dynamic_format_types[0]));
format_types = dynamic_format_types;
n_format_types += TARGET_N_FORMAT_TYPES;
}
#endif
if (!decode_format_attr (args, &info, 0))
{
*no_add_attrs = true;
return NULL_TREE;
}
argument = TYPE_ARG_TYPES (type);
if (argument)
{
if (c_dialect_objc () && info.format_type == nsstring_format_type)
{
if (!objc_check_format_nsstring (argument, info.format_num, no_add_attrs))
return NULL;
}
#ifdef CHECK_FORMAT_CFSTRING
else if (info.format_type == cfstring_format_type)
{
if (!CHECK_FORMAT_CFSTRING (argument, info.format_num, no_add_attrs))
return NULL;
}
#endif
else
if (!check_format_string (argument, info.format_num, flags,
no_add_attrs))
return NULL_TREE;
if (info.first_arg_num != 0)
{
unsigned HOST_WIDE_INT arg_num = 1;
while (argument)
arg_num++, argument = TREE_CHAIN (argument);
if (arg_num != info.first_arg_num)
{
if (!(flags & (int) ATTR_FLAG_BUILT_IN))
error ("args to be formatted is not %<...%>");
*no_add_attrs = true;
return NULL_TREE;
}
}
}
if (info.format_type == strftime_format_type && info.first_arg_num != 0)
{
error ("strftime formats cannot format arguments");
*no_add_attrs = true;
return NULL_TREE;
}
if (info.format_type == asm_fprintf_format_type
|| info.format_type == gcc_diag_format_type
|| info.format_type == gcc_cdiag_format_type
|| info.format_type == gcc_cxxdiag_format_type)
{
if (!dynamic_format_types)
format_types = dynamic_format_types = (format_kind_info *)
xmemdup (format_types_orig, sizeof (format_types_orig),
sizeof (format_types_orig));
if (info.format_type == asm_fprintf_format_type)
init_dynamic_asm_fprintf_info ();
else if (info.format_type == gcc_diag_format_type
|| info.format_type == gcc_cdiag_format_type
|| info.format_type == gcc_cxxdiag_format_type)
init_dynamic_diag_info ();
else
gcc_unreachable ();
}
return NULL_TREE;
}