]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR c++/71528 (multiple extern reference declarations produce uninitializ...
authorJakub Jelinek <jakub@redhat.com>
Thu, 7 Jul 2016 12:45:44 +0000 (14:45 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 7 Jul 2016 12:45:44 +0000 (14:45 +0200)
Backported from mainline
2016-06-14  Jakub Jelinek  <jakub@redhat.com>

PR c++/71528
* decl.c (duplicate_decls): For DECL_INITIALIZED_P non-external
olddecl vars, preserve their TREE_READONLY bit.

* g++.dg/opt/pr71528.C: New test.

From-SVN: r238098

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/opt/pr71528.C [new file with mode: 0644]

index 4529d1f77b7d1a9f9d1c57db98d9be328cc29b1f..c735c821b35469aadf6bd4bf88e737d1a71d9c9a 100644 (file)
@@ -3,6 +3,10 @@
        Backported from mainline
        2016-06-14  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/71528
+       * decl.c (duplicate_decls): For DECL_INITIALIZED_P non-external
+       olddecl vars, preserve their TREE_READONLY bit.
+
        PR c++/71516
        * decl.c (complete_vars): Handle gracefully type == error_mark_node.
 
index c1747d6ad866f2e818242c391a00fa00b42c686e..ce0443ca4c05e792ad3f600eaae30d2be497b57e 100644 (file)
@@ -2023,6 +2023,14 @@ duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
       if (VAR_P (newdecl))
        {
          DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
+         /* For already initialized vars, TREE_READONLY could have been
+            cleared in cp_finish_decl, because the var needs runtime
+            initialization or destruction.  Make sure not to set
+            TREE_READONLY on it again.  */
+         if (DECL_INITIALIZED_P (olddecl)
+             && !DECL_EXTERNAL (olddecl)
+             && !TREE_READONLY (olddecl))
+           TREE_READONLY (newdecl) = 0;
          DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
          DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
            |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
index 59bbf239b20b1c607ed00d080773797c89cfbbc9..ed6444ef157edb14440243209b5c09a7cb5795df 100644 (file)
@@ -3,6 +3,9 @@
        Backported from mainline
        2016-06-14  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/71528
+       * g++.dg/opt/pr71528.C: New test.
+
        PR c++/71516
        * g++.dg/init/pr71516.C: New test.
 
diff --git a/gcc/testsuite/g++.dg/opt/pr71528.C b/gcc/testsuite/g++.dg/opt/pr71528.C
new file mode 100644 (file)
index 0000000..bfe0622
--- /dev/null
@@ -0,0 +1,23 @@
+// PR c++/71528
+// { dg-do run }
+// { dg-options "-O2" }
+
+extern int &x;
+int y;
+
+int &
+foo ()
+{
+  return y;
+}
+
+int &x = foo ();
+
+int
+main ()
+{
+  if (&x != &y)
+    __builtin_abort ();
+}
+
+extern int &x;