aboutsummaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2023-02-08 23:41:47 +0000
committerJoseph Myers <joseph@codesourcery.com>2023-02-08 23:41:47 +0000
commit53678f7f794aa3ff99d9df87c02abe1caf4ec1d3 (patch)
treed8b3bdb44374933e867320b3ef02e4b0d4126fc6 /gcc/c
parent1a49390f3f6febc1f5539f76459c416dba83b4db (diff)
downloadgcc-53678f7f794aa3ff99d9df87c02abe1caf4ec1d3.zip
gcc-53678f7f794aa3ff99d9df87c02abe1caf4ec1d3.tar.gz
gcc-53678f7f794aa3ff99d9df87c02abe1caf4ec1d3.tar.bz2
c: Update checks on constexpr pointer initializers
WG14 has agreed a change of the rules on constexpr pointer initializers, so that a (constant) null value that is not a null pointer constant is accepted in that context, rather than only accepting null pointer constants. (In particular, this means that a constexpr variable of pointer type can be used to initializer another such variable.) Remove the null pointer constant restriction in GCC, instead checking just whether the value is null. Bootstrapped with no regressions for x86_64-pc-linux-gnu. gcc/c/ * c-typeck.cc (check_constexpr_init): Remove argument null_pointer_constant. Only check pointer initializers for being null. (digest_init): Update calls to check_constexpr_init. gcc/testsuite/ * gcc.dg/c2x-constexpr-1.c: Test initialization of constexpr pointers with null values that are not null pointer constants. * gcc.dg/c2x-constexpr-3.c: Test initialization of constexpr pointers with non-null values, not with null values that are not null pointer constants.
Diffstat (limited to 'gcc/c')
-rw-r--r--gcc/c/c-typeck.cc25
1 files changed, 10 insertions, 15 deletions
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 224a9cb..157b77e 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -8186,23 +8186,20 @@ constexpr_init_fits_real_type (tree type, tree init)
/* Check whether INIT (location LOC) is valid as a 'constexpr'
initializer for type TYPE, and give an error if not. INIT has
- already been folded and verified to be constant.
- NULL_POINTER_CONSTANT, INT_CONST_EXPR and ARITH_CONST_EXPR say
- whether it is a null pointer constant, integer constant expression
- or arithmetic constant expression, respectively. If TYPE is not a
- scalar type, this function does nothing. */
+ already been folded and verified to be constant. INT_CONST_EXPR
+ and ARITH_CONST_EXPR say whether it is an integer constant
+ expression or arithmetic constant expression, respectively. If
+ TYPE is not a scalar type, this function does nothing. */
static void
check_constexpr_init (location_t loc, tree type, tree init,
- bool null_pointer_constant, bool int_const_expr,
- bool arith_const_expr)
+ bool int_const_expr, bool arith_const_expr)
{
if (POINTER_TYPE_P (type))
{
- /* The initializer must be a null pointer constant. */
- if (!null_pointer_constant)
- error_at (loc, "%<constexpr%> pointer initializer is not a "
- "null pointer constant");
+ /* The initializer must be null. */
+ if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
+ error_at (loc, "%<constexpr%> pointer initializer is not null");
return;
}
if (INTEGRAL_TYPE_P (type))
@@ -8582,8 +8579,7 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype,
"initializer element is not a constant expression");
else if (require_constexpr)
check_constexpr_init (init_loc, type, inside_init,
- null_pointer_constant, int_const_expr,
- arith_const_expr);
+ int_const_expr, arith_const_expr);
/* Added to enable additional -Wsuggest-attribute=format warnings. */
if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
@@ -8638,8 +8634,7 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype,
"initializer element is not a constant expression");
else if (require_constexpr)
check_constexpr_init (init_loc, type, unconverted_init,
- null_pointer_constant, int_const_expr,
- arith_const_expr);
+ int_const_expr, arith_const_expr);
return inside_init;
}