From: Jakub Jelinek Date: Tue, 9 Sep 2025 14:44:48 +0000 (+0200) Subject: c, c++: Allow &__real__ static_var in constant expressions [PR121678] X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6e4e6883d412ec5f4440a3385df51c90d4108227;p=thirdparty%2Fgcc.git c, c++: Allow &__real__ static_var in constant expressions [PR121678] When looking at constexpr references, I've noticed staticp handles COMPONENT_REFs and ARRAY_REFs (the latter if the index is INTEGER_CST), but not {REAL,IMAG}PART_EXPR. I think that is incorrect and causes rejection of constexpr (for C++) or static const (for C) addresses of __real__ or __imag__ parts of static vars. 2025-09-09 Jakub Jelinek PR c++/121678 * tree.cc (staticp): Handle REALPART_EXPR and IMAGPART_EXPR. * g++.dg/ext/pr121678.C: New test. * gcc.dg/pr121678.c: New test. --- diff --git a/gcc/testsuite/g++.dg/ext/pr121678.C b/gcc/testsuite/g++.dg/ext/pr121678.C new file mode 100644 index 00000000000..e283ea1c8fc --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/pr121678.C @@ -0,0 +1,7 @@ +// PR c++/121678 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +static constexpr _Complex double a = 1.0; +static constexpr double *r = &__real__ a; +static constexpr double *i = &__imag__ a; diff --git a/gcc/testsuite/gcc.dg/pr121678.c b/gcc/testsuite/gcc.dg/pr121678.c new file mode 100644 index 00000000000..afb6c208892 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr121678.c @@ -0,0 +1,7 @@ +/* PR c++/121678 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +static const _Complex double a = 1.0; +static const double *const r = &__real__ a; +static const double *const i = &__imag__ a; diff --git a/gcc/tree.cc b/gcc/tree.cc index 905c2d6657f..2a3ab23f1dc 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -3935,6 +3935,10 @@ staticp (tree arg) else return NULL; + case REALPART_EXPR: + case IMAGPART_EXPR: + return staticp (TREE_OPERAND (arg, 0)); + case COMPOUND_LITERAL_EXPR: return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;