diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-02-04 22:46:25 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-02-04 22:46:25 +0000 |
commit | eeb15d499f032bb89773ddaca2d17475122a37bb (patch) | |
tree | e74f990d3f40d2c180b21d0e7f8b9a57148792c2 /test | |
parent | 98eb8a7a702b95183ed015706b1f1c66f5cb27a4 (diff) |
Implement semantic analysis for the GNU flexible array initialization
extension. The interaction with designated initializers is a
bit... interesting... but we follow GNU's lead and don't permit too
much crazy code in this area.
Also, make the "excess initializers" error message a bit more
informative.
Addresses PR2561: http://llvm.org/bugs/show_bug.cgi?id=2561
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63785 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r-- | test/Sema/array-init.c | 11 | ||||
-rw-r--r-- | test/Sema/flexible-array-init.c | 38 | ||||
-rw-r--r-- | test/SemaCXX/dcl_init_aggr.cpp | 2 |
3 files changed, 44 insertions, 7 deletions
diff --git a/test/Sema/array-init.c b/test/Sema/array-init.c index b10d60da3f..2c835bd54f 100644 --- a/test/Sema/array-init.c +++ b/test/Sema/array-init.c @@ -20,7 +20,7 @@ void func() { int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}} - int x4 = { 1, 2 }; // expected-warning{{braces around scalar initializer}} expected-error{{excess elements in array initializer}} + int x4 = { 1, 2 }; // expected-warning{{braces around scalar initializer}} expected-error{{excess elements in scalar initializer}} int y[4][3] = { { 1, 3, 5 }, @@ -53,7 +53,7 @@ void func() { void test() { int y1[3] = { - { 1, 2, 3 } // expected-warning{{braces around scalar initializer}} expected-error{{excess elements in array initializer}} + { 1, 2, 3 } // expected-warning{{braces around scalar initializer}} expected-error{{excess elements in scalar initializer}} }; int y3[4][3] = { { 1, 3, 5 }, @@ -201,14 +201,13 @@ int bar (void) { return z.z; } struct s3 {void (*a)(void);} t5 = {autoStructTest}; -// FIXME: GCC extension; flexible array init. Once this is implemented, the warning should be removed. // Note that clang objc implementation depends on this extension. -struct {int a; int b[];} t6 = {1, {1, 2, 3}}; //expected-error{{excess elements in array initializer}} +struct {int a; int b[];} t6 = {1, {1, 2, 3}}; union {char a; int b;} t7[] = {1, 2, 3}; int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1]; struct bittest{int : 31, a, :21, :12, b;}; -struct bittest bittestvar = {1, 2, 3, 4}; //expected-error{{excess elements in array initializer}} +struct bittest bittestvar = {1, 2, 3, 4}; //expected-error{{excess elements in struct initializer}} // Not completely sure what should happen here... int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}} @@ -243,7 +242,7 @@ struct soft_segment_descriptor gdt_segs[] = { }; static void sppp_ipv6cp_up(); -const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct extension}} expected-error{{excess elements in array initializer}} +const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct extension}} expected-error{{excess elements in struct initializer}} struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a GNU extension in C}} typedef struct _Matrix Matrix; diff --git a/test/Sema/flexible-array-init.c b/test/Sema/flexible-array-init.c new file mode 100644 index 0000000000..9ef6eb3bc0 --- /dev/null +++ b/test/Sema/flexible-array-init.c @@ -0,0 +1,38 @@ +// RUN: clang -fsyntax-only -verify %s +struct one { + int a; + int values[]; +} x = {5, {1, 2, 3}}; + +struct one x2 = { 5, 1, 2, 3 }; // expected-error{{excess elements in struct initializer}} + +void test() { + struct one x3 = {5, {1, 2, 3}}; +} + +struct foo { + int x; + int y[]; // expected-note{{initialized flexible array member 'y' is here}} +}; +struct bar { struct foo z; }; + +struct foo a = { 1, { 2, 3, 4 } }; // Valid. +struct bar b = { { 1, { 2, 3, 4 } } }; // expected-error{{non-empty initialization of flexible array member inside subobject}} +struct bar c = { { 1, { } } }; // Valid. +struct foo d[1] = { { 1, { 2, 3, 4 } } }; // expected-error{{'struct foo' may not be used as an array element due to flexible array member}} + +struct foo desig_foo = { .y = {2, 3, 4} }; +struct bar desig_bar = { .z.y = { } }; +struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{non-empty initialization of flexible array member inside subobject}} +struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}} + +struct point { + int x, y; +}; + +struct polygon { + int numpoints; + struct point points[]; // expected-note{{initialized flexible array member 'points' is here}} +}; +struct polygon poly = { + .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}} diff --git a/test/SemaCXX/dcl_init_aggr.cpp b/test/SemaCXX/dcl_init_aggr.cpp index e5015fab03..fbe7de1563 100644 --- a/test/SemaCXX/dcl_init_aggr.cpp +++ b/test/SemaCXX/dcl_init_aggr.cpp @@ -118,5 +118,5 @@ union u { int a; char* b; }; u u1 = { 1 }; u u2 = u1; u u3 = 1; // expected-error{{cannot initialize 'u3' with an rvalue of type 'int'}} -u u4 = { 0, "asdf" }; // expected-error{{excess elements in array initializer}} +u u4 = { 0, "asdf" }; // expected-error{{excess elements in union initializer}} u u5 = { "asdf" }; // expected-error{{incompatible type initializing 'char const [5]', expected 'int'}} |