From: Jason Merrill Date: Mon, 11 Apr 2022 18:50:14 +0000 (-0400) Subject: c++: rodata and defaulted ctor [PR104142] X-Git-Tag: releases/gcc-10.4.0~142 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cfa3a04be981f63af41f9a960df06e84c2634c5f;p=thirdparty%2Fgcc.git c++: rodata and defaulted ctor [PR104142] Trivial initialization shouldn't bump a variable out of .rodata; if the result of build_aggr_init is an empty STATEMENT_LIST, throw it away. PR c++/104142 gcc/cp/ChangeLog: * decl.c (check_initializer): Check TREE_SIDE_EFFECTS. gcc/testsuite/ChangeLog: * g++.dg/opt/const7.C: New test. --- diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 5bd6c964a3aa..a8fb9581cb28 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6941,6 +6941,10 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) if (init && init != error_mark_node) init_code = build2 (INIT_EXPR, type, decl, init); + if (init_code && !TREE_SIDE_EFFECTS (init_code) + && init_code != error_mark_node) + init_code = NULL_TREE; + if (init_code) { /* We might have set these in cp_finish_decl. */ diff --git a/gcc/testsuite/g++.dg/opt/const7.C b/gcc/testsuite/g++.dg/opt/const7.C new file mode 100644 index 000000000000..7f4cfe9c1ea3 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/const7.C @@ -0,0 +1,7 @@ +// PR c++/104142 +// { dg-do compile { target c++11 } } +// { dg-additional-options -Wunused-variable } + +struct B { B()=default; }; +static const B b_var; // { dg-bogus "" } +// { dg-final { scan-assembler "\\.rodata" { target i?86-*-linux* i?86-*-gnu* x86_64-*-linux* } } }