public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: rodata and defaulted ctor [PR104142]
@ 2022-04-12  3:57 Jason Merrill
  2022-04-17  7:29 ` Andreas Schwab
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2022-04-12  3:57 UTC (permalink / raw)
  To: gcc-patches

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.

Tested x86_64-pc-linux-gnu, applying to trunk.

	PR c++/104142

gcc/cp/ChangeLog:

	* decl.cc (check_initializer): Check TREE_SIDE_EFFECTS.

gcc/testsuite/ChangeLog:

	* g++.dg/opt/const7.C: New test.
---
 gcc/cp/decl.cc                    | 4 ++++
 gcc/testsuite/g++.dg/opt/const7.C | 7 +++++++
 2 files changed, 11 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/opt/const7.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index c136dbbba1a..31cae4d1d36 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 00000000000..5bcf94897a8
--- /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\]} } }

base-commit: 4195fced8a13422db94e179404588d9d887a036a
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [pushed] c++: rodata and defaulted ctor [PR104142]
  2022-04-12  3:57 [pushed] c++: rodata and defaulted ctor [PR104142] Jason Merrill
@ 2022-04-17  7:29 ` Andreas Schwab
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Schwab @ 2022-04-17  7:29 UTC (permalink / raw)
  To: Jason Merrill via Gcc-patches

On Apr 11 2022, Jason Merrill via Gcc-patches wrote:

> diff --git a/gcc/testsuite/g++.dg/opt/const7.C b/gcc/testsuite/g++.dg/opt/const7.C
> new file mode 100644
> index 00000000000..5bcf94897a8
> --- /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\]} } }

That fails to handle .srodata.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-17  7:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-12  3:57 [pushed] c++: rodata and defaulted ctor [PR104142] Jason Merrill
2022-04-17  7:29 ` Andreas Schwab

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).