1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
From 8ce475543d784f8ce0f7e80f7faff39fa8eb92da Mon Sep 17 00:00:00 2001
From: Jason Merrill <jason@redhat.com>
Date: Mon, 11 Apr 2022 14:50:14 -0400
Subject: [PATCH] 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.
---
gcc/cp/decl.c | 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.c b/gcc/cp/decl.c
index bffbd004167..7b0872cd55e 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7227,6 +7227,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\]} } }
--
2.36.0.1.g2bbe56bd8d
|