aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/constexpr.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2020-06-04 09:09:01 +0200
committerGiuliano Belinassi <giuliano.belinassi@usp.br>2020-08-17 13:10:05 -0300
commitcfcc31846f718581e6bbb85da522b3dbb3b4a492 (patch)
tree36a59458068e029332c50d247fbf7813e9aa4e88 /gcc/cp/constexpr.c
parent2aa0083d7fbf70550b9c7d1f8fa6005f33ca3663 (diff)
downloadgcc-cfcc31846f718581e6bbb85da522b3dbb3b4a492.zip
gcc-cfcc31846f718581e6bbb85da522b3dbb3b4a492.tar.gz
gcc-cfcc31846f718581e6bbb85da522b3dbb3b4a492.tar.bz2
c++: Reject some further reinterpret casts in constexpr [PR82304, PR95307]
cxx_eval_outermost_constant_expr had a check for reinterpret_casts from pointers (well, it checked from ADDR_EXPRs) to integral type, but that only caught such cases at the toplevel of expressions. As the comment said, it should be done even inside of the expressions, but at the point of the writing e.g. pointer differences used to be a problem. We now have POINTER_DIFF_EXPR, so this is no longer an issue. Had to do it just for CONVERT_EXPR, because the FE emits NOP_EXPR casts from pointers to integrals in various spots, e.g. for the PMR & 1 tests, though on NOP_EXPR we have the REINTERPRET_CAST_P bit that we do check, while on CONVERT_EXPR we don't. 2020-06-04 Jakub Jelinek <jakub@redhat.com> PR c++/82304 PR c++/95307 * constexpr.c (cxx_eval_constant_expression): Diagnose CONVERT_EXPR conversions from pointer types to arithmetic types here... (cxx_eval_outermost_constant_expr): ... instead of here. * g++.dg/template/pr79650.C: Expect different diagnostics and expect it on all lines that do pointer to integer casts. * g++.dg/cpp1y/constexpr-shift1.C: Expect different diagnostics. * g++.dg/cpp1y/constexpr-82304.C: New test. * g++.dg/cpp0x/constexpr-95307.C: New test.
Diffstat (limited to 'gcc/cp/constexpr.c')
-rw-r--r--gcc/cp/constexpr.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 959f025..c7d17e9 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -6210,6 +6210,18 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
if (VOID_TYPE_P (type))
return void_node;
+ if (TREE_CODE (t) == CONVERT_EXPR
+ && ARITHMETIC_TYPE_P (type)
+ && INDIRECT_TYPE_P (TREE_TYPE (op)))
+ {
+ if (!ctx->quiet)
+ error_at (loc,
+ "conversion from pointer type %qT to arithmetic type "
+ "%qT in a constant expression", TREE_TYPE (op), type);
+ *non_constant_p = true;
+ return t;
+ }
+
if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
op = cplus_expand_constant (op);
@@ -6811,19 +6823,6 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
non_constant_p = true;
}
- /* Technically we should check this for all subexpressions, but that
- runs into problems with our internal representation of pointer
- subtraction and the 5.19 rules are still in flux. */
- if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
- && ARITHMETIC_TYPE_P (TREE_TYPE (r))
- && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
- {
- if (!allow_non_constant)
- error ("conversion from pointer type %qT "
- "to arithmetic type %qT in a constant expression",
- TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
- non_constant_p = true;
- }
if (!non_constant_p && overflow_p)
non_constant_p = true;