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  <jakub@redhat.com>

	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.
This commit is contained in:
Jakub Jelinek 2025-01-03 17:59:57 +01:00 committed by Jakub Jelinek
parent 514577c66b
commit 6f444e45d3
3 changed files with 41 additions and 1 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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;
}