From 58cb41e617c5af9a01dff6d1e6cb80907a48f49d Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Sat, 19 Feb 2005 01:26:38 +0100
Subject: [PATCH] re PR c/20043 (transparent_union doesn't allow restrict
 qualifier removal)

	PR c/20043
	* c-typeck.c (composite_type): Handle quals in transparent unions.
	(type_lists_compatible_p): Likewise.

	* gcc.dg/transparent-union-1.c: New test.
	* gcc.dg/transparent-union-2.c: New test.

From-SVN: r95255
---
 gcc/ChangeLog                              |  6 ++
 gcc/c-typeck.c                             | 70 +++++++++++++-----
 gcc/testsuite/ChangeLog                    |  6 ++
 gcc/testsuite/gcc.dg/transparent-union-1.c | 83 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/transparent-union-2.c | 18 +++++
 5 files changed, 165 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/transparent-union-1.c
 create mode 100644 gcc/testsuite/gcc.dg/transparent-union-2.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index fb85c878ad65..760a480e6244 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2005-02-19  Jakub Jelinek  <jakub@redhat.com>
+
+	PR c/20043
+	* c-typeck.c (composite_type): Handle quals in transparent unions.
+	(type_lists_compatible_p): Likewise.
+
 2005-02-18  Stan Shebs  <shebs@apple.com>
 
 	* config/darwin.c (machopic_select_rtx_section): Don't put relocatable
diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c
index 3a202929e9fc..3a38d22923b5 100644
--- a/gcc/c-typeck.c
+++ b/gcc/c-typeck.c
@@ -375,29 +375,51 @@ composite_type (tree t1, tree t2)
 		&& TREE_VALUE (p1) != TREE_VALUE (p2))
 	      {
 		tree memb;
+		tree mv2 = TREE_VALUE (p2);
+		if (mv2 && mv2 != error_mark_node
+		    && TREE_CODE (mv2) != ARRAY_TYPE)
+		  mv2 = TYPE_MAIN_VARIANT (mv2);
 		for (memb = TYPE_FIELDS (TREE_VALUE (p1));
 		     memb; memb = TREE_CHAIN (memb))
-		  if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
-		    {
-		      TREE_VALUE (n) = TREE_VALUE (p2);
-		      if (pedantic)
-			pedwarn ("function types not truly compatible in ISO C");
-		      goto parm_done;
-		    }
+		  {
+		    tree mv3 = TREE_TYPE (memb);
+		    if (mv3 && mv3 != error_mark_node
+			&& TREE_CODE (mv3) != ARRAY_TYPE)
+		      mv3 = TYPE_MAIN_VARIANT (mv3);
+		    if (comptypes (mv3, mv2))
+		      {
+			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
+							 TREE_VALUE (p2));
+			if (pedantic)
+			  pedwarn ("function types not truly compatible in ISO C");
+			goto parm_done;
+		      }
+		  }
 	      }
 	    if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
 		&& TREE_VALUE (p2) != TREE_VALUE (p1))
 	      {
 		tree memb;
+		tree mv1 = TREE_VALUE (p1);
+		if (mv1 && mv1 != error_mark_node
+		    && TREE_CODE (mv1) != ARRAY_TYPE)
+		  mv1 = TYPE_MAIN_VARIANT (mv1);
 		for (memb = TYPE_FIELDS (TREE_VALUE (p2));
 		     memb; memb = TREE_CHAIN (memb))
-		  if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
-		    {
-		      TREE_VALUE (n) = TREE_VALUE (p1);
-		      if (pedantic)
-			pedwarn ("function types not truly compatible in ISO C");
-		      goto parm_done;
-		    }
+		  {
+		    tree mv3 = TREE_TYPE (memb);
+		    if (mv3 && mv3 != error_mark_node
+			&& TREE_CODE (mv3) != ARRAY_TYPE)
+		      mv3 = TYPE_MAIN_VARIANT (mv3);
+		    if (comptypes (mv3, mv1))
+		      {
+			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
+							 TREE_VALUE (p1));
+			if (pedantic)
+			  pedwarn ("function types not truly compatible in ISO C");
+			goto parm_done;
+		      }
+		  }
 	      }
 	    TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
 	  parm_done: ;
@@ -1122,8 +1144,14 @@ type_lists_compatible_p (tree args1, tree args2)
 	      tree memb;
 	      for (memb = TYPE_FIELDS (a1);
 		   memb; memb = TREE_CHAIN (memb))
-		if (comptypes (TREE_TYPE (memb), a2))
-		  break;
+		{
+		  tree mv3 = TREE_TYPE (memb);
+		  if (mv3 && mv3 != error_mark_node
+		      && TREE_CODE (mv3) != ARRAY_TYPE)
+		    mv3 = TYPE_MAIN_VARIANT (mv3);
+		  if (comptypes (mv3, mv2))
+		    break;
+		}
 	      if (memb == 0)
 		return 0;
 	    }
@@ -1137,8 +1165,14 @@ type_lists_compatible_p (tree args1, tree args2)
 	      tree memb;
 	      for (memb = TYPE_FIELDS (a2);
 		   memb; memb = TREE_CHAIN (memb))
-		if (comptypes (TREE_TYPE (memb), a1))
-		  break;
+		{
+		  tree mv3 = TREE_TYPE (memb);
+		  if (mv3 && mv3 != error_mark_node
+		      && TREE_CODE (mv3) != ARRAY_TYPE)
+		    mv3 = TYPE_MAIN_VARIANT (mv3);
+		  if (comptypes (mv3, mv1))
+		    break;
+		}
 	      if (memb == 0)
 		return 0;
 	    }
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4562225816fa..eaf57edcb3c4 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2005-02-19  Jakub Jelinek  <jakub@redhat.com>
+
+	PR c/20043
+	* gcc.dg/transparent-union-1.c: New test.
+	* gcc.dg/transparent-union-2.c: New test.
+
 2005-02-18  James E Wilson  <wilson@specifixinc.com>
 
 	PR tree-optimization/18977
diff --git a/gcc/testsuite/gcc.dg/transparent-union-1.c b/gcc/testsuite/gcc.dg/transparent-union-1.c
new file mode 100644
index 000000000000..a5be8ce91860
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/transparent-union-1.c
@@ -0,0 +1,83 @@
+/* PR c/20043 */
+/* { dg-compile } */
+/* { dg-options "-std=gnu99" } */
+
+extern void f0 (int *);
+extern void f0 (int *__restrict);
+
+extern void f1 (int *__restrict);
+extern void f1 (int *);
+
+typedef union { int *i; long *l; } U2
+  __attribute__((transparent_union));
+extern void f2 (U2);
+extern void f2 (int *);
+
+typedef union { int *__restrict i; long *__restrict l; } U3
+  __attribute__((transparent_union));
+extern void f3 (U3);
+extern void f3 (int *__restrict);
+
+extern void f4 (U3);
+extern void f4 (int *);
+
+extern void f5 (U2);
+extern void f5 (int *__restrict);
+
+typedef union { long *l; int *i; } U6
+  __attribute__((transparent_union));
+extern void f6 (U6);
+extern void f6 (int *);
+
+typedef union { long *__restrict l; int *__restrict i; } U7
+  __attribute__((transparent_union));
+extern void f7 (U7);
+extern void f7 (int *__restrict);
+
+extern void f8 (U7);
+extern void f8 (int *);
+
+extern void f9 (U6);
+extern void f9 (int *__restrict);
+
+extern void f10 (U2);
+extern void f11 (U3);
+extern void f12 (U6);
+extern void f13 (U7);
+
+int i;
+long l;
+
+int
+main (void)
+{
+  f0 (&i);
+  f0 (&l);	/* { dg-warning "warning: passing argument 1 of 'f0' from incompatible pointer type" } */
+  f1 (&i);
+  f1 (&l);	/* { dg-warning "warning: passing argument 1 of 'f1' from incompatible pointer type" } */
+  f2 (&i);
+  f2 (&l);	/* { dg-warning "warning: passing argument 1 of 'f2' from incompatible pointer type" } */
+  f3 (&i);
+  f3 (&l);	/* { dg-warning "warning: passing argument 1 of 'f3' from incompatible pointer type" } */
+  f4 (&i);
+  f4 (&l);	/* { dg-warning "warning: passing argument 1 of 'f4' from incompatible pointer type" } */
+  f5 (&i);
+  f5 (&l);	/* { dg-warning "warning: passing argument 1 of 'f5' from incompatible pointer type" } */
+  f6 (&i);
+  f6 (&l);	/* { dg-warning "warning: passing argument 1 of 'f6' from incompatible pointer type" } */
+  f7 (&i);
+  f7 (&l);	/* { dg-warning "warning: passing argument 1 of 'f7' from incompatible pointer type" } */
+  f8 (&i);
+  f8 (&l);	/* { dg-warning "warning: passing argument 1 of 'f8' from incompatible pointer type" } */
+  f9 (&i);
+  f9 (&l);	/* { dg-warning "warning: passing argument 1 of 'f9' from incompatible pointer type" } */
+  f10 (&i);
+  f10 (&l);
+  f11 (&i);
+  f11 (&l);
+  f12 (&i);
+  f12 (&l);
+  f13 (&i);
+  f13 (&l);
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/transparent-union-2.c b/gcc/testsuite/gcc.dg/transparent-union-2.c
new file mode 100644
index 000000000000..f466c4aa2be5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/transparent-union-2.c
@@ -0,0 +1,18 @@
+/* PR c/20043 */
+/* { dg-compile } */
+/* { dg-options "-std=gnu99" } */
+
+typedef union { int *i; long *l; } U
+  __attribute__((transparent_union));
+
+extern void f0 (U);		/* { dg-error "previous declaration" } */
+extern void f0 (void *);	/* { dg-error "conflicting types" } */
+
+extern void f1 (U);		/* { dg-error "previous declaration" } */
+extern void f1 (unsigned long);	/* { dg-error "conflicting types" } */
+
+extern void f2 (void *);	/* { dg-error "previous declaration" } */
+extern void f2 (U);		/* { dg-error "conflicting types" } */
+
+extern void f3 (unsigned long);	/* { dg-error "previous declaration" } */
+extern void f3 (U);		/* { dg-error "conflicting types" } */
-- 
GitLab