From cc6534d40136b8afb5dcd878e58d106f31b51dd2 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 1 Dec 2017 00:43:10 +0100 Subject: [PATCH] re PR c/83222 (Inconsistent "initializer element is not constant" error) PR c/83222 * c-tree.h (decl_constant_value_1): Declare. * c-typeck.c (decl_constant_value_1): New function. (decl_constant_value): Use it. * c-fold.c (c_fully_fold_internal): If in_init, use decl_constant_value_1 instead of decl_constant_value. * gcc.c-torture/compile/pr83222.c: New test. From-SVN: r255285 --- gcc/c/ChangeLog | 9 ++++++++ gcc/c/c-fold.c | 5 ++++- gcc/c/c-tree.h | 1 + gcc/c/c-typeck.c | 20 ++++++++++++------ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.c-torture/compile/pr83222.c | 21 +++++++++++++++++++ 6 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr83222.c diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 677b54cbfae..90db38a0419 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,12 @@ +2017-12-01 Jakub Jelinek + + PR c/83222 + * c-tree.h (decl_constant_value_1): Declare. + * c-typeck.c (decl_constant_value_1): New function. + (decl_constant_value): Use it. + * c-fold.c (c_fully_fold_internal): If in_init, use + decl_constant_value_1 instead of decl_constant_value. + 2017-11-30 Jakub Jelinek * c-parser.c (c_parser_postfix_expression): Use ; instead of ;;. diff --git a/gcc/c/c-fold.c b/gcc/c/c-fold.c index 88954627159..84ce543be75 100644 --- a/gcc/c/c-fold.c +++ b/gcc/c/c-fold.c @@ -167,7 +167,10 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, /* Except for variables which we can optimize to its initializer. */ if (VAR_P (expr) && !lval && (optimize || in_init)) { - ret = decl_constant_value (expr); + if (in_init) + ret = decl_constant_value_1 (expr); + else + ret = decl_constant_value (expr); /* Avoid unwanted tree sharing between the initializer and current function's body where the tree can be modified e.g. by the gimplifier. */ diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 5fb57f21bd1..cbc5e0e8bcc 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -640,6 +640,7 @@ extern struct c_expr default_function_array_read_conversion (location_t, struct c_expr); extern struct c_expr convert_lvalue_to_rvalue (location_t, struct c_expr, bool, bool); +extern tree decl_constant_value_1 (tree); extern void mark_exp_read (tree); extern tree composite_type (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t); diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 6846bc51d28..922266038af 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -1832,13 +1832,10 @@ c_size_in_bytes (const_tree type) /* Return either DECL or its known constant value (if it has one). */ tree -decl_constant_value (tree decl) +decl_constant_value_1 (tree decl) { - if (/* Don't change a variable array bound or initial value to a constant - in a place where a variable is invalid. Note that DECL_INITIAL - isn't valid for a PARM_DECL. */ - current_function_decl != NULL_TREE - && TREE_CODE (decl) != PARM_DECL + if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */ + TREE_CODE (decl) != PARM_DECL && !TREE_THIS_VOLATILE (decl) && TREE_READONLY (decl) && DECL_INITIAL (decl) != NULL_TREE @@ -1853,6 +1850,17 @@ decl_constant_value (tree decl) return decl; } +/* Return either DECL or its known constant value (if it has one). + Like the above, but always return decl outside of functions. */ + +tree +decl_constant_value (tree decl) +{ + /* Don't change a variable array bound or initial value to a constant + in a place where a variable is invalid. */ + return current_function_decl ? decl_constant_value_1 (decl) : decl; +} + /* Convert the array expression EXP to a pointer. */ static tree array_to_pointer_conversion (location_t loc, tree exp) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 045be1a4b54..74211182335 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-12-01 Jakub Jelinek + + PR c/83222 + * gcc.c-torture/compile/pr83222.c: New test. + 2017-12-01 Maxim Ostapenko PR sanitizer/81697 diff --git a/gcc/testsuite/gcc.c-torture/compile/pr83222.c b/gcc/testsuite/gcc.c-torture/compile/pr83222.c new file mode 100644 index 00000000000..ad40a3a11dd --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr83222.c @@ -0,0 +1,21 @@ +/* PR c/83222 */ + +const char a = 0x42; +const double b = (double) a; +const double c = a; +double d = (double) a; +double e = a; +const double f = 1 + (double) a; +const double g = 1 + a; +double h = 1 + (double) a; +double i = 1 + a; +double j[] = { (double) a, a, 1 + (double) a, 1 + a }; + +void +foo (void) +{ + static const double k = (double) a; + static const double l = a; + static const double m = 1 + (double) a; + static const double n = 1 + a; +} -- 2.30.2