public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-10729] c++: rodata and defaulted ctor [PR104142]
Date: Thu, 12 May 2022 21:25:11 +0000 (GMT)	[thread overview]
Message-ID: <20220512212511.C8CA8385043C@sourceware.org> (raw)

https://gcc.gnu.org/g:cfa3a04be981f63af41f9a960df06e84c2634c5f

commit r10-10729-gcfa3a04be981f63af41f9a960df06e84c2634c5f
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Apr 11 14:50:14 2022 -0400

    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:
---
 gcc/cp/decl.c                     | 4 ++++
 gcc/testsuite/g++.dg/opt/const7.C | 7 +++++++
 2 files changed, 11 insertions(+)

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 5bd6c964a3a..a8fb9581cb2 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6941,6 +6941,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 00000000000..7f4cfe9c1ea
--- /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* } } }


                 reply	other threads:[~2022-05-12 21:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220512212511.C8CA8385043C@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).