/* A suffix-identifier value doublet that represents user-defined literals
for C++-0x. */
+enum overflow_type {
+ OT_UNDERFLOW = -1,
+ OT_NONE,
+ OT_OVERFLOW
+};
+
struct GTY(()) tree_userdef_literal {
struct tree_base base;
tree suffix_id;
tree value;
tree num_string;
+ enum overflow_type overflow;
};
#define USERDEF_LITERAL_SUFFIX_ID(NODE) \
#define USERDEF_LITERAL_VALUE(NODE) \
(((struct tree_userdef_literal *)USERDEF_LITERAL_CHECK (NODE))->value)
+#define USERDEF_LITERAL_OVERFLOW(NODE) \
+ (((struct tree_userdef_literal *)USERDEF_LITERAL_CHECK (NODE))->overflow)
+
#define USERDEF_LITERAL_NUM_STRING(NODE) \
(((struct tree_userdef_literal *)USERDEF_LITERAL_CHECK (NODE))->num_string)
#define USERDEF_LITERAL_TYPE(NODE) \
(TREE_TYPE (USERDEF_LITERAL_VALUE (NODE)))
-extern tree build_userdef_literal (tree suffix_id, tree value, tree num_string);
+extern tree build_userdef_literal (tree suffix_id, tree value,
+ enum overflow_type overflow,
+ tree num_string);
extern void convert_vector_to_pointer_for_subscript (location_t, tree*, tree);
int pending_lang_change; /* If we need to switch languages - C++ only */
int c_header_level; /* depth in C headers - C++ only */
-static tree interpret_integer (const cpp_token *, unsigned int);
-static tree interpret_float (const cpp_token *, unsigned int, const char *);
+static tree interpret_integer (const cpp_token *, unsigned int,
+ enum overflow_type *);
+static tree interpret_float (const cpp_token *, unsigned int, const char *,
+ enum overflow_type *);
static tree interpret_fixed (const cpp_token *, unsigned int);
static enum integer_type_kind narrowest_unsigned_type
(unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT, unsigned int);
const cpp_token *tok;
enum cpp_ttype type;
unsigned char add_flags = 0;
+ enum overflow_type overflow = OT_NONE;
timevar_push (TV_CPP);
retry:
Set PURE_ZERO to pass this information to the C++ parser. */
if (tok->val.str.len == 1 && *tok->val.str.text == '0')
add_flags = PURE_ZERO;
- *value = interpret_integer (tok, flags);
+ *value = interpret_integer (tok, flags, &overflow);
break;
case CPP_N_FLOATING:
- *value = interpret_float (tok, flags, suffix);
+ *value = interpret_float (tok, flags, suffix, &overflow);
break;
default:
num_string = fix_string_type (num_string);
str = CONST_CAST (char *, TREE_STRING_POINTER (num_string));
str[len] = '\0';
- literal = build_userdef_literal (suffix_id, *value,
- num_string);
+ literal = build_userdef_literal (suffix_id, *value, overflow,
+ num_string);
*value = literal;
}
}
temp_tok.val.str.len -= strlen (suffix);
temp_tok.type = cpp_userdef_char_remove_type (type);
literal = build_userdef_literal (get_identifier (suffix),
- lex_charconst (&temp_tok), NULL_TREE);
+ lex_charconst (&temp_tok),
+ OT_NONE, NULL_TREE);
*value = literal;
}
break;
string = build_string (tok->val.str.len - strlen (suffix),
(const char *) tok->val.str.text);
literal = build_userdef_literal (get_identifier (suffix),
- string, NULL_TREE);
+ string, OT_NONE, NULL_TREE);
*value = literal;
}
break;
/* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */
static tree
-interpret_integer (const cpp_token *token, unsigned int flags)
+interpret_integer (const cpp_token *token, unsigned int flags,
+ enum overflow_type *overflow)
{
tree value, type;
enum integer_type_kind itk;
cpp_num integer;
cpp_options *options = cpp_get_options (parse_in);
+ *overflow = OT_NONE;
+
integer = cpp_interpret_integer (parse_in, token, flags);
integer = cpp_num_sign_extend (integer, options->precision);
+ if (integer.overflow)
+ *overflow = OT_OVERFLOW;
/* The type of a constant with a U suffix is straightforward. */
if (flags & CPP_N_UNSIGNED)
by cpplib. For C++0X SUFFIX may contain a user-defined literal suffix. */
static tree
interpret_float (const cpp_token *token, unsigned int flags,
- const char *suffix)
+ const char *suffix, enum overflow_type *overflow)
{
tree type;
tree const_type;
char *copy;
size_t copylen;
+ *overflow = OT_NONE;
+
/* Default (no suffix) depends on whether the FLOAT_CONST_DECIMAL64
pragma has been used and is either double or _Decimal64. Types
that are not allowed with decimal float default to double. */
if (REAL_VALUE_ISINF (real)
|| (const_type != type && REAL_VALUE_ISINF (real_trunc)))
{
- if (!MODE_HAS_INFINITIES (TYPE_MODE (type)))
- pedwarn (input_location, 0, "floating constant exceeds range of %qT", type);
- else
- warning (OPT_Woverflow, "floating constant exceeds range of %qT", type);
+ *overflow = OT_OVERFLOW;
+ if (!(flags & CPP_N_USERDEF))
+ {
+ if (!MODE_HAS_INFINITIES (TYPE_MODE (type)))
+ pedwarn (input_location, 0,
+ "floating constant exceeds range of %qT", type);
+ else
+ warning (OPT_Woverflow,
+ "floating constant exceeds range of %qT", type);
+ }
}
/* We also give a warning if the value underflows. */
else if (REAL_VALUES_EQUAL (real, dconst0)
- || (const_type != type && REAL_VALUES_EQUAL (real_trunc, dconst0)))
+ || (const_type != type
+ && REAL_VALUES_EQUAL (real_trunc, dconst0)))
{
REAL_VALUE_TYPE realvoidmode;
- int overflow = real_from_string (&realvoidmode, copy);
- if (overflow < 0 || !REAL_VALUES_EQUAL (realvoidmode, dconst0))
- warning (OPT_Woverflow, "floating constant truncated to zero");
+ int oflow = real_from_string (&realvoidmode, copy);
+ *overflow = (oflow == 0 ? OT_NONE
+ : (oflow < 0 ? OT_UNDERFLOW : OT_OVERFLOW));
+ if (!(flags & CPP_N_USERDEF))
+ {
+ if (oflow < 0 || !REAL_VALUES_EQUAL (realvoidmode, dconst0))
+ warning (OPT_Woverflow, "floating constant truncated to zero");
+ }
}
/* Create a node with determined type and value. */
if (have_suffix_p)
{
- tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
+ tree literal = build_userdef_literal (suffix_id, value,
+ OT_NONE, NULL_TREE);
tok->u.value = literal;
return cp_parser_userdef_string_literal (tok);
}
tree literal = token->u.value;
tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
tree value = USERDEF_LITERAL_VALUE (literal);
+ int overflow = USERDEF_LITERAL_OVERFLOW (literal);
tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
tree decl, result;
result = finish_call_expr (decl, &args, false, true, tf_none);
if (result != error_mark_node)
{
+ if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
+ warning_at (token->location, OPT_Woverflow,
+ "integer literal exceeds range of %qT type",
+ long_long_unsigned_type_node);
+ else
+ {
+ if (overflow > 0)
+ warning_at (token->location, OPT_Woverflow,
+ "floating literal exceeds range of %qT type",
+ long_double_type_node);
+ else if (overflow < 0)
+ warning_at (token->location, OPT_Woverflow,
+ "floating literal truncated to zero");
+ }
release_tree_vector (args);
return result;
}