From 6f444e45d3fd6c45fc34a79ac66bf46c20fd95b1 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 3 Jan 2025 17:59:57 +0100 Subject: [PATCH] varasm: Fix up array_size_for_constructor RAW_DATA_CST handling once again [PR118275] As the following testcases show (the latter only if I revert the temporary reversion of the C++ large array speedup), the FEs aren't really consistent in the type of array CONSTRUCTOR_ELTS indexes, it can be bitsizetype, but can be sizetype as well. Given that everything else is able to cope with it just fine, I'm using build_int_cst which also doesn't care what type it is, instead of bitsize_int, which I've used in PR117190 fix (previously it was size_int). 2025-01-03 Jakub Jelinek PR c++/118275 * varasm.cc (array_size_for_constructor): Use build_int_cst with TREE_TYPE (index) as first argument, instead of bitsize_int. * g++.dg/cpp/embed-18.C: New test. * g++.dg/ext/flexary41.C: New test. --- gcc/testsuite/g++.dg/cpp/embed-18.C | 15 +++++++++++++++ gcc/testsuite/g++.dg/ext/flexary41.C | 24 ++++++++++++++++++++++++ gcc/varasm.cc | 3 ++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp/embed-18.C create mode 100644 gcc/testsuite/g++.dg/ext/flexary41.C diff --git a/gcc/testsuite/g++.dg/cpp/embed-18.C b/gcc/testsuite/g++.dg/cpp/embed-18.C new file mode 100644 index 00000000000..c7ab8dc5757 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp/embed-18.C @@ -0,0 +1,15 @@ +// PR c++/118275 +// { dg-do compile } +// { dg-options "" } + +struct A { int a; char b[]; }; +void bar (A *); + +void +foo () +{ + static struct A a = { .a = 1, .b = { +#embed __FILE__ + } }; + bar (&a); +} diff --git a/gcc/testsuite/g++.dg/ext/flexary41.C b/gcc/testsuite/g++.dg/ext/flexary41.C new file mode 100644 index 00000000000..da80ba391b0 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/flexary41.C @@ -0,0 +1,24 @@ +// PR c++/118275 +// { dg-do compile } +// { dg-options "" } + +struct A { int a; char b[]; }; +void bar (A *); + +void +foo () +{ + static struct A a = { .a = 1, .b = { +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 + } }; + bar (&a); +} diff --git a/gcc/varasm.cc b/gcc/varasm.cc index 3538e2971fc..6d93fe97d7b 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -5648,7 +5648,8 @@ array_size_for_constructor (tree val) index = TREE_OPERAND (index, 1); if (value && TREE_CODE (value) == RAW_DATA_CST) index = size_binop (PLUS_EXPR, index, - bitsize_int (RAW_DATA_LENGTH (value) - 1)); + build_int_cst (TREE_TYPE (index), + RAW_DATA_LENGTH (value) - 1)); if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index)) max_index = index; }