diff options
author | Joseph Myers <jsm@polyomino.org.uk> | 2002-08-17 15:48:28 +0100 |
---|---|---|
committer | Joseph Myers <jsm28@gcc.gnu.org> | 2002-08-17 15:48:28 +0100 |
commit | 2984fe64968ad7140e84f8137c877c9e6d25136a (patch) | |
tree | 89955359e44fa2311d348937a438303874397fd2 | |
parent | fded6d781c8e005bc195fc047a7e480073871c35 (diff) | |
download | gcc-2984fe64968ad7140e84f8137c877c9e6d25136a.zip gcc-2984fe64968ad7140e84f8137c877c9e6d25136a.tar.gz gcc-2984fe64968ad7140e84f8137c877c9e6d25136a.tar.bz2 |
c-decl.c (flexible_array_type_p): New function.
* 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.
testsuite:
* gcc.dg/c90-flex-array-1.c, gcc.dg/c99-flex-array-3.c,
gcc.dg/c99-flex-array-4.c: New tests.
From-SVN: r56411
-rw-r--r-- | gcc/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/c-decl.c | 43 | ||||
-rw-r--r-- | gcc/doc/extend.texi | 6 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/c90-flex-array-1.c | 7 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/c99-flex-array-3.c | 47 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/c99-flex-array-4.c | 25 |
7 files changed, 140 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4510a07..8ebb481 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +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, diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 7a926cf..d61d955 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -282,6 +282,7 @@ static void layout_array_type PARAMS ((tree)); 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)); /* States indicating how grokdeclarator() should handle declspecs marked with __attribute__((deprecated)). An object declared as @@ -3357,6 +3358,40 @@ complete_array_type (type, initial_value, do_default) return value; } +/* 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; + } +} + /* Given declspecs and a declarator, determine the name and type of the object declared and construct a ..._DECL node for it. @@ -3953,6 +3988,9 @@ grokdeclarator (declarator, declspecs, decl_context, initialized) 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; @@ -5216,6 +5254,11 @@ finish_struct (t, fieldlist, attributes) 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; } diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 4814b35..32fabae 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1293,6 +1293,12 @@ of zero-length arrays, @code{sizeof} evaluates to zero. @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 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index bd0a52f..dad59ab 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +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. diff --git a/gcc/testsuite/gcc.dg/c90-flex-array-1.c b/gcc/testsuite/gcc.dg/c90-flex-array-1.c new file mode 100644 index 0000000..1eead19 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c90-flex-array-1.c @@ -0,0 +1,7 @@ +/* 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 } */ diff --git a/gcc/testsuite/gcc.dg/c99-flex-array-3.c b/gcc/testsuite/gcc.dg/c99-flex-array-3.c new file mode 100644 index 0000000..e8647f6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-flex-array-3.c @@ -0,0 +1,47 @@ +/* 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 } */ diff --git a/gcc/testsuite/gcc.dg/c99-flex-array-4.c b/gcc/testsuite/gcc.dg/c99-flex-array-4.c new file mode 100644 index 0000000..ab20cf0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-flex-array-4.c @@ -0,0 +1,25 @@ +/* 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]; |