re PR c/36432 (“incompatible pointer type” with pointer to array as a struct member)
authorJoseph Myers <joseph@codesourcery.com>
Fri, 6 Feb 2009 20:12:10 +0000 (20:12 +0000)
committerJoseph Myers <jsm28@gcc.gnu.org>
Fri, 6 Feb 2009 20:12:10 +0000 (20:12 +0000)
PR c/36432
* c-decl.c (grokdeclarator): Don't treat [] declarators in fields
as indicating flexible array members unless the field itself is
being declarared as the incomplete array.

testsuite:
* gcc.dg/c90-flex-array-2.c, gcc.dg/c99-flex-array-6.c: New tests.

From-SVN: r143989

gcc/ChangeLog
gcc/c-decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/c90-flex-array-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c99-flex-array-6.c [new file with mode: 0644]

index f7ed966138f15490faaabedcdd60dce5a6012c83..fcbe406326b556a2347203b5aab15f759763901d 100644 (file)
@@ -1,3 +1,10 @@
+2009-02-06  Joseph Myers  <joseph@codesourcery.com>
+
+       PR c/36432
+       * c-decl.c (grokdeclarator): Don't treat [] declarators in fields
+       as indicating flexible array members unless the field itself is
+       being declarared as the incomplete array.
+
 2009-02-06  Jan Hubicka  <jh@suse.cz>
 
        PR tree-optimization/38844
index 35a9c4b826161536d6e60e3304bdd23147ad3fea..9fadad385c9221b2f2ab2e2b41bbc24952404dfc 100644 (file)
@@ -4389,6 +4389,7 @@ grokdeclarator (const struct c_declarator *declarator,
              }
            else if (decl_context == FIELD)
              {
+               bool flexible_array_member = false;
                if (array_parm_vla_unspec_p)
                  /* Field names can in fact have function prototype
                     scope so [*] is disallowed here through making
@@ -4396,13 +4397,23 @@ grokdeclarator (const struct c_declarator *declarator,
                     something other than a declaration with function
                     prototype scope.  */
                  size_varies = 1;
-               else if (pedantic && !flag_isoc99 && !in_system_header)
+               else
+                 {
+                   const struct c_declarator *t = declarator;
+                   while (t->kind == cdk_attrs)
+                     t = t->declarator;
+                   flexible_array_member = (t->kind == cdk_id);
+                 }
+               if (flexible_array_member
+                   && pedantic && !flag_isoc99 && !in_system_header)
                  pedwarn (input_location, OPT_pedantic,
                           "ISO C90 does not support flexible array members");
 
                /* ISO C99 Flexible array members are effectively
                   identical to GCC's zero-length array extension.  */
-               itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
+               if (flexible_array_member || array_parm_vla_unspec_p)
+                 itype = build_range_type (sizetype, size_zero_node,
+                                           NULL_TREE);
              }
            else if (decl_context == PARM)
              {
index dc1c009881e4d8922b43761e7c795e20b25324a1..f88b297c83bc2d055b8d26b6b0794b3102957024 100644 (file)
@@ -1,3 +1,8 @@
+2009-02-06  Joseph Myers  <joseph@codesourcery.com>
+
+       PR c/36432
+       * gcc.dg/c90-flex-array-2.c, gcc.dg/c99-flex-array-6.c: New tests.
+
 2009-02-05  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/39106
diff --git a/gcc/testsuite/gcc.dg/c90-flex-array-2.c b/gcc/testsuite/gcc.dg/c90-flex-array-2.c
new file mode 100644 (file)
index 0000000..425ce84
--- /dev/null
@@ -0,0 +1,15 @@
+/* [] does not indicate a flexible array member unless it is the field
+   itself being declared as an incomplete array type rather than a
+   pointer or other type derived from such a type.  PR 36432.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+void
+f (void)
+{
+  int a[3];
+  int (*p)[];
+  struct { int (*p)[]; } s;
+  p = &a;
+  s.p = &a;
+}
diff --git a/gcc/testsuite/gcc.dg/c99-flex-array-6.c b/gcc/testsuite/gcc.dg/c99-flex-array-6.c
new file mode 100644 (file)
index 0000000..468c4b3
--- /dev/null
@@ -0,0 +1,15 @@
+/* [] does not indicate a flexible array member unless it is the field
+   itself being declared as an incomplete array type rather than a
+   pointer or other type derived from such a type.  PR 36432.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+void
+f (void)
+{
+  int a[3];
+  int (*p)[];
+  struct { int (*p)[]; } s;
+  p = &a;
+  s.p = &a;
+}