+2002-08-17 Joseph S. Myers <jsm@polyomino.org.uk>
+
+ * c-decl.c (flexible_array_type_p): New function.
+ (grokdeclarator, finish_struct): Use it.
+ * doc/extend.texi: Document constraints on use of structures with
+ flexible array members.
+
2002-08-17 Richard Sandiford <rsandifo@redhat.com>
* config/mips/t-coff, config/mips/t-elf, config/mips/t-isa3264,
static tree c_make_fname_decl PARAMS ((tree, int));
static void c_expand_body PARAMS ((tree, int, int));
static void warn_if_shadowing PARAMS ((tree, tree));
+static bool flexible_array_type_p PARAMS ((tree));
\f
/* States indicating how grokdeclarator() should handle declspecs marked
with __attribute__((deprecated)). An object declared as
return value;
}
\f
+/* Determine whether TYPE is a structure with a flexible array member,
+ or a union containing such a structure (possibly recursively). */
+
+static bool
+flexible_array_type_p (type)
+ tree type;
+{
+ tree x;
+ switch (TREE_CODE (type))
+ {
+ case RECORD_TYPE:
+ x = TYPE_FIELDS (type);
+ if (x == NULL_TREE)
+ return false;
+ while (TREE_CHAIN (x) != NULL_TREE)
+ x = TREE_CHAIN (x);
+ if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
+ && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
+ && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
+ && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
+ return true;
+ return false;
+ case UNION_TYPE:
+ for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
+ {
+ if (flexible_array_type_p (TREE_TYPE (x)))
+ return true;
+ }
+ return false;
+ default:
+ return false;
+ }
+}
+\f
/* Given declspecs and a declarator,
determine the name and type of the object declared
and construct a ..._DECL node for it.
type = error_mark_node;
}
+ if (pedantic && flexible_array_type_p (type))
+ pedwarn ("invalid use of structure with flexible array member");
+
if (size == error_mark_node)
type = error_mark_node;
else if (! saw_named_field)
error_with_decl (x, "flexible array member in otherwise empty struct");
}
+
+ if (pedantic && TREE_CODE (t) == RECORD_TYPE
+ && flexible_array_type_p (TREE_TYPE (x)))
+ pedwarn_with_decl (x, "invalid use of structure with flexible array member");
+
if (DECL_NAME (x))
saw_named_field = 1;
}
@item
Flexible array members may only appear as the last member of a
@code{struct} that is otherwise non-empty.
+
+@item
+A structure containing a flexible array member, or a union containing
+such a structure (possibly recursively), may not be a member of a
+structure or an element of an array. (However, these uses are
+permitted by GCC as extensions.)
@end itemize
GCC versions before 3.0 allowed zero-length arrays to be statically
+2002-08-17 Joseph S. Myers <jsm@polyomino.org.uk>
+
+ * gcc.dg/c90-flex-array-1.c, gcc.dg/c99-flex-array-3.c,
+ gcc.dg/c99-flex-array-4.c: New tests.
+
2002-08-16 Stan Shebs <shebs@apple.com>
* objc/execute/selector-1.m: Add __NEXT_RUNTIME__ case.
--- /dev/null
+/* Test for flexible array members. Test for rejection in C90 mode. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+struct flex { int a; int b[]; }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "ISO C90" "flexible array members not in C90" { target *-*-* } 6 } */
--- /dev/null
+/* Test for flexible array members. Test for where structures with
+ such members may not occur. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+struct flex { int a; int b[]; };
+union rf1 { struct flex a; int b; };
+union rf2 { int a; struct flex b; };
+union rf3 { int a; union rf1 b; };
+union rf4 { union rf2 a; int b; };
+
+/* The above structure and unions may not be members of structures or
+ elements of arrays (6.7.2.1#2). */
+
+struct t0 { struct flex a; }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "struct in struct" { target *-*-* } 16 } */
+struct t1 { union rf1 a; }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "union in struct" { target *-*-* } 18 } */
+struct t2 { union rf2 a; }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "union in struct" { target *-*-* } 20 } */
+struct t3 { union rf3 a; }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "recursive union in struct" { target *-*-* } 22 } */
+struct t4 { union rf4 a; }; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "recursive union in struct" { target *-*-* } 24 } */
+
+void f0 (struct flex[]); /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "struct in array" { target *-*-* } 27 } */
+void f1 (union rf1[]); /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "union in array" { target *-*-* } 29 } */
+void f2 (union rf2[]); /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "union in array" { target *-*-* } 31 } */
+void f3 (union rf3[]); /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "recursive union in array" { target *-*-* } 33 } */
+void f4 (union rf4[]); /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "recursive union in array" { target *-*-* } 35 } */
+
+struct flex a0[1]; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "struct in array" { target *-*-* } 38 } */
+union rf1 a1[1]; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "union in array" { target *-*-* } 40 } */
+union rf2 a2[1]; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "union in array" { target *-*-* } 42 } */
+union rf3 a3[1]; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "recursive union in array" { target *-*-* } 44 } */
+union rf4 a4[1]; /* { dg-bogus "warning" "warning in place of error" } */
+/* { dg-error "invalid use of structure" "recursive union in array" { target *-*-* } 46 } */
--- /dev/null
+/* Test for flexible array members. Test for agreement of offset and
+ structure size. This is expected to fail, because of a possible
+ defect in the standard. */
+/* Origin: http://gcc.gnu.org/ml/gcc/2002-05/msg02844.html
+ from Tony Finch <dot@dotat.at>, adapted to a testcase by Joseph Myers
+ <jsm28@cam.ac.uk>. See also WG14 reflector messages 9571-3. */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+#include <stddef.h>
+
+struct foo {
+ int a;
+ short b;
+ char pad[];
+};
+
+struct bar {
+ int a;
+ short b;
+ char pad[1024];
+};
+
+char x[(sizeof(struct foo) == offsetof(struct foo, pad)) ? 1 : -1]; /* { dg-bogus "negative" "sizeof != offsetof" { xfail *-*-* } } */
+char y[(offsetof(struct foo, pad) == offsetof(struct bar, pad)) ? 1 : -1];