aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2022-04-11 14:50:14 -0400
committerJason Merrill <jason@redhat.com>2022-04-11 23:57:42 -0400
commit0899b22d9329c1e8fc12d6f0deee0f87dcc0163a (patch)
tree88d977d162577f8d8d05987acafed587faac8a25
parent4195fced8a13422db94e179404588d9d887a036a (diff)
downloadgcc-0899b22d9329c1e8fc12d6f0deee0f87dcc0163a.zip
gcc-0899b22d9329c1e8fc12d6f0deee0f87dcc0163a.tar.gz
gcc-0899b22d9329c1e8fc12d6f0deee0f87dcc0163a.tar.bz2
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.cc (check_initializer): Check TREE_SIDE_EFFECTS. gcc/testsuite/ChangeLog: * g++.dg/opt/const7.C: New test.
-rw-r--r--gcc/cp/decl.cc4
-rw-r--r--gcc/testsuite/g++.dg/opt/const7.C7
2 files changed, 11 insertions, 0 deletions
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index c136dbb..31cae4d 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7444,6 +7444,10 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **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 0000000..5bcf948
--- /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-symbol-section {b_var} {^\.(const|rodata)|\[RO\]} } }