diff mbox

Fix C duplicate field detection bug (PR 46889)

Message ID Pine.LNX.4.64.1012301824340.30514@digraph.polyomino.org.uk
State New
Headers show

Commit Message

Joseph Myers Dec. 30, 2010, 6:25 p.m. UTC
This patch fixes PR 46889, a failure to detect duplicate field names
with anonymous structs and unions in some cases.  When anonymous
structs or unions are in use, duplicate detection must use
detect_field_duplicates_hash, but a premature optimization meant that
the loop deciding whether to use this function failed to handle the
case of the first field being the only anonymous one.

Bootstrapped with no regressions on x86_64-unknown-linux-gnu.  Applied
to mainline.

2010-12-30  Joseph Myers  <joseph@codesourcery.com>

	PR c/46889
	* c-decl.c (detect_field_duplicates): Ensure hash is used for
	finding duplicates when first field is anonymous.

testsuite:
2010-12-30  Joseph Myers  <joseph@codesourcery.com>

	PR c/46889
	* gcc.dg/anon-struct-15.c: New test.
diff mbox

Patch

Index: testsuite/gcc.dg/anon-struct-15.c
===================================================================
--- testsuite/gcc.dg/anon-struct-15.c	(revision 0)
+++ testsuite/gcc.dg/anon-struct-15.c	(revision 0)
@@ -0,0 +1,16 @@ 
+/* Test diagnostics for duplicate field names involving anonymous
+   struct or union as first field.  PR 46889.  */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+struct foo {
+  union {
+    struct {
+      unsigned long time_stamp;
+    };
+    struct {
+      int *page;
+    };
+  };
+  int *page; /* { dg-error "duplicate member" } */
+};
Index: c-decl.c
===================================================================
--- c-decl.c	(revision 168340)
+++ c-decl.c	(working copy)
@@ -6805,11 +6805,9 @@  detect_field_duplicates (tree fieldlist)
 
   /* First, see if there are more than "a few" fields.
      This is trivially true if there are zero or one fields.  */
-  if (!fieldlist)
-    return;
-  x = DECL_CHAIN (fieldlist);
-  if (!x)
+  if (!fieldlist || !DECL_CHAIN (fieldlist))
     return;
+  x = fieldlist;
   do {
     timeout--;
     if (DECL_NAME (x) == NULL_TREE