public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-10700] c++: Fix up handling of references to anon union members in initializers [PR53932]
@ 2023-05-02 20:13 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-05-02 20:13 UTC (permalink / raw)
  To: gcc-cvs

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

commit r11-10700-gbd34d694f4c060c92aee0e09edf4c7e1d83807e5
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Jan 19 23:27:34 2023 +0100

    c++: Fix up handling of references to anon union members in initializers [PR53932]
    
    For anonymous union members we create artificial VAR_DECLs which
    have DECL_VALUE_EXPR for the actual COMPONENT_REF.  That works
    just fine inside of functions (including global dynamic constructors),
    because during gimplification such VAR_DECLs are gimplified as
    their DECL_VALUE_EXPR.  This is also done during regimplification.
    
    But references to these artificial vars in DECL_INITIAL expressions
    aren't ever replaced by the DECL_VALUE_EXPRs, so we end up either
    with link failures like on the testcase below, or worse ICEs with
    LTO.
    
    The following patch fixes those during cp_fully_fold_init where we
    already walk all the trees (!data->genericize means that
    function rather than cp_fold_function).
    
    2023-01-19  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/53932
            * cp-gimplify.c (cp_fold_r): During cp_fully_fold_init replace
            DECL_ANON_UNION_VAR_P VAR_DECLs with their corresponding
            DECL_VALUE_EXPR.
    
            * g++.dg/init/pr53932.C: New test.
    
    (cherry picked from commit 9b9a989adc042b304572fd6d4ade46b47be6ccb8)

Diff:
---
 gcc/cp/cp-gimplify.c                | 45 ++++++++++++++++++++++++++++---------
 gcc/testsuite/g++.dg/init/pr53932.C | 25 +++++++++++++++++++++
 2 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 4eee9ff6eb6..a929e9f83d4 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -42,6 +42,14 @@ along with GCC; see the file COPYING3.  If not see
 #include "cgraph.h"
 #include "omp-general.h"
 
+struct cp_fold_data
+{
+  hash_set<tree> pset;
+  bool genericize; // called from cp_fold_function?
+
+  cp_fold_data (bool g): genericize (g) {}
+};
+
 /* Forward declarations.  */
 
 static tree cp_genericize_r (tree *, int *, void *);
@@ -468,8 +476,8 @@ cp_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 				  init, VEC_INIT_EXPR_VALUE_INIT (*expr_p),
 				  from_array,
 				  tf_warning_or_error);
-	hash_set<tree> pset;
-	cp_walk_tree (expr_p, cp_fold_r, &pset, NULL);
+	cp_fold_data data (/*genericize*/true);
+	cp_walk_tree (expr_p, cp_fold_r, &data, NULL);
 	cp_genericize_tree (expr_p, false);
 	copy_if_shared (expr_p);
 	ret = GS_OK;
@@ -875,14 +883,31 @@ struct cp_genericize_data
      GIMPLE-form.  */
 
 static tree
-cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data)
+cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
 {
-  tree stmt;
-  enum tree_code code;
+  cp_fold_data *data = (cp_fold_data*)data_;
+  tree stmt = *stmt_p;
+  enum tree_code code = TREE_CODE (stmt);
+
+  switch (code)
+    {
+    case VAR_DECL:
+      /* In initializers replace anon union artificial VAR_DECLs
+	 with their DECL_VALUE_EXPRs, as nothing will do it later.  */
+      if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize)
+	{
+	  *stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt));
+	  break;
+	}
+      break;
+
+    default:
+      break;
+    }
 
   *stmt_p = stmt = cp_fold (*stmt_p);
 
-  if (((hash_set<tree> *) data)->add (stmt))
+  if (data->pset.add (stmt))
     {
       /* Don't walk subtrees of stmts we've already walked once, otherwise
 	 we can have exponential complexity with e.g. lots of nested
@@ -949,8 +974,8 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data)
 void
 cp_fold_function (tree fndecl)
 {
-  hash_set<tree> pset;
-  cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &pset, NULL);
+  cp_fold_data data (/*genericize*/true);
+  cp_walk_tree (&DECL_SAVED_TREE (fndecl), cp_fold_r, &data, NULL);
 }
 
 /* Turn SPACESHIP_EXPR EXPR into GENERIC.  */
@@ -2218,8 +2243,8 @@ cp_fully_fold_init (tree x)
   if (processing_template_decl)
     return x;
   x = cp_fully_fold (x);
-  hash_set<tree> pset;
-  cp_walk_tree (&x, cp_fold_r, &pset, NULL);
+  cp_fold_data data (/*genericize*/false);
+  cp_walk_tree (&x, cp_fold_r, &data, NULL);
   return x;
 }
 
diff --git a/gcc/testsuite/g++.dg/init/pr53932.C b/gcc/testsuite/g++.dg/init/pr53932.C
new file mode 100644
index 00000000000..3b129e7d68a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/pr53932.C
@@ -0,0 +1,25 @@
+// PR c++/53932
+// { dg-do link }
+
+static union { int i; };
+int &r = i;
+int s = i;
+int *t = &i;
+
+void
+foo (int **p, int *q)
+{
+  static int &u = i;
+  static int v = i;
+  static int *w = &i;
+  int &x = i;
+  int y = i;
+  int *z = &i;
+  *p = &i;
+  *q = i;
+}
+
+int
+main ()
+{
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-02 20:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-02 20:13 [gcc r11-10700] c++: Fix up handling of references to anon union members in initializers [PR53932] Jakub Jelinek

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