diff options
author | Jason Merrill <jason@redhat.com> | 2025-04-10 18:16:37 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2025-04-11 09:53:40 -0400 |
commit | 4acdfb71d4fdaa43c2707ad7b2fb7b2b7bddfc42 (patch) | |
tree | fd375ef200d46d57efa5ab73dba0bc715f7b3332 | |
parent | 44478b69d70ff0095a1fd06392e380827de4688a (diff) | |
download | gcc-4acdfb71d4fdaa43c2707ad7b2fb7b2b7bddfc42.zip gcc-4acdfb71d4fdaa43c2707ad7b2fb7b2b7bddfc42.tar.gz gcc-4acdfb71d4fdaa43c2707ad7b2fb7b2b7bddfc42.tar.bz2 |
c++: avoid ARM -Wunused-value [PR114970]
Because of the __builtin_is_constant_evaluated, maybe_constant_init in
expand_default_init fails, so the constexpr constructor isn't folded until
cp_fold, which then calls cp_build_init_expr_for_ctor, which builds a
COMPOUND_EXPR in case the enclosing expression is relying on the ARM
behavior of returning 'this'.
As in other places, avoid -Wunused-value on artificial COMPOUND_EXPR.
PR c++/114970
gcc/cp/ChangeLog:
* cp-gimplify.cc (cp_build_init_expr_for_ctor): Suppress warnings on
return_this COMPOUND_EXPR.
gcc/testsuite/ChangeLog:
* g++.dg/opt/is_constant_evaluated4.C: New test.
-rw-r--r-- | gcc/cp/cp-gimplify.cc | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C | 20 |
2 files changed, 25 insertions, 2 deletions
diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 550cea29..f5625ab 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -1199,8 +1199,11 @@ cp_build_init_expr_for_ctor (tree call, tree init) tree s = build_fold_indirect_ref_loc (loc, a); init = cp_build_init_expr (s, init); if (return_this) - init = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (call), init, - fold_convert_loc (loc, TREE_TYPE (call), a)); + { + init = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (call), init, + fold_convert_loc (loc, TREE_TYPE (call), a)); + suppress_warning (init); + } return init; } diff --git a/gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C b/gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C new file mode 100644 index 0000000..9650004 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/is_constant_evaluated4.C @@ -0,0 +1,20 @@ +// PR c++/114970 +// { dg-do compile { target c++17 } } +// { dg-additional-options "-O -Wunused-value" } + +struct sv +{ + const char* str; + unsigned len; + + constexpr sv(const char *p): str(p), len(0) + { + if (__builtin_is_constant_evaluated ()) { len = 42; } + } +}; + +int main() +{ + sv s ("foo"); + return s.len; +} |