re PR c++/71528 (multiple extern reference declarations produce uninitialized access)
authorJakub Jelinek <jakub@redhat.com>
Tue, 14 Jun 2016 19:55:08 +0000 (21:55 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 14 Jun 2016 19:55:08 +0000 (21:55 +0200)
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: r237458

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

index d006305bdc00bb942eda69f3fbbc37c1473396a6..cd5996bdd1d4a01f04b004a085a7c5ecd37fcb8b 100644 (file)
@@ -1,5 +1,9 @@
 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 20e7307ddad7bd5444201467b512ee8e0e9dfff4..a03e48ff6182410e75cd4df0f0ec8c4fafc29dca 100644 (file)
@@ -2066,6 +2066,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 0e3c9cd7738a90dc4cef4d10c476365e981068cc..4a55801352e1262b1956c7691221a459512a298e 100644 (file)
@@ -1,5 +1,8 @@
 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;