diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-01-28 21:54:33 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-01-28 21:54:33 +0000 |
commit | 4c67834407ca6ab344dcf44fc599ad4938cfa96d (patch) | |
tree | d7d3838ffe094efc25517516290d26e484e2ca26 /test/Sema | |
parent | abf439731bc4f56df2df9e54d6c242e2c633f5ca (diff) |
Code generation support for C99 designated initializers.
The approach I've taken in this patch is relatively straightforward,
although the code itself is non-trivial. Essentially, as we process
an initializer list we build up a fully-explicit representation of the
initializer list, where each of the subobject initializations occurs
in order. Designators serve to "fill in" subobject initializations in
a non-linear way. The fully-explicit representation makes initializer
lists (both with and without designators) easy to grok for codegen and
later semantic analyses. We keep the syntactic form of the initializer
list linked into the AST for those clients interested in exactly what
the user wrote.
Known limitations:
- Designating a member of a union that isn't the first member may
result in bogus initialization (we warn about this)
- GNU array-range designators are not supported (we warn about this)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63242 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Sema')
-rw-r--r-- | test/Sema/array-init.c | 28 | ||||
-rw-r--r-- | test/Sema/designated-initializers.c | 30 | ||||
-rw-r--r-- | test/Sema/vector-init.c | 12 |
3 files changed, 63 insertions, 7 deletions
diff --git a/test/Sema/array-init.c b/test/Sema/array-init.c index 9d2a942410..3d2cf69127 100644 --- a/test/Sema/array-init.c +++ b/test/Sema/array-init.c @@ -1,4 +1,4 @@ -// RUN: clang -fsyntax-only -verify -pedantic %s +// RUN: clang -fsyntax-only -pedantic -verify %s extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}} @@ -101,6 +101,7 @@ void legal() { { 2, 3 }, { 4, 5, 6 } }; + int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1]; } unsigned char asso_values[] = { 34 }; @@ -134,15 +135,19 @@ typedef int AryT[]; void testTypedef() { AryT a = { 1, 2 }, b = { 3, 4, 5 }; + int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1]; + int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1]; } static char const xx[] = "test"; +int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1]; static char const yy[5] = "test"; static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}} void charArrays() { static char const test[] = "test"; + int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1]; static char const test2[] = { "weird stuff" }; static char const test3[] = { "test", "excess stuff" }; // expected-error{{excess elements in char array initializer}} @@ -171,6 +176,7 @@ void variableArrayInit() { float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}} float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}} char r3[][5] = {1,2,3,4,5,6}; +int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1]; char r3_2[sizeof r3 == 10 ? 1 : -1]; float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}} char r5[][5] = {"aa", "bbb", "ccccc"}; @@ -195,7 +201,7 @@ int bar (void) { return z.z; } struct s3 {void (*a)(void);} t5 = {autoStructTest}; -// GCC extension; flexible array init. Once this is implemented, the warning should be removed. +// 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-warning{{excess elements in array initializer}} union {char a; int b;} t7[] = {1, 2, 3}; @@ -238,3 +244,21 @@ struct soft_segment_descriptor gdt_segs[] = { static void sppp_ipv6cp_up(); const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct extension}} expected-warning{{excess elements in array initializer}} + +struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a GNU extension in C}} +typedef struct _Matrix Matrix; +void test_matrix() { + const Matrix mat1 = { + { { 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 13.0f, 14.0f, 15.0f, 16.0f } } + }; + + const Matrix mat2 = { + 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 13.0f, 14.0f, 15.0f, 16.0f + }; +} diff --git a/test/Sema/designated-initializers.c b/test/Sema/designated-initializers.c index fd4873193c..c9a0aa7f05 100644 --- a/test/Sema/designated-initializers.c +++ b/test/Sema/designated-initializers.c @@ -18,7 +18,8 @@ int iarray2[10] = { }; int iarray3[10] = { - [5 ... 12] = 2 // expected-error{{array designator index (12) exceeds array bounds (10)}} + [5 ... 12] = 2 // expected-error{{array designator index (12) exceeds array bounds (10)}}\ + // expected-warning{{GNU array-range designator extension is unsupported}} }; struct point { @@ -44,8 +45,8 @@ struct point array[10] = { struct point array2[10] = { [10].x = 2.0, // expected-error{{array designator index (10) exceeds array bounds (10)}} - [4 ... 5].y = 2.0, - [4 ... 6] = { .x = 3, .y = 4.0 } + [4 ... 5].y = 2.0, // expected-warning{{GNU array-range designator extension is unsupported}} + [4 ... 6] = { .x = 3, .y = 4.0 } // expected-warning{{GNU array-range designator extension is unsupported}} }; struct point array3[10] = { @@ -116,4 +117,25 @@ struct disklabel_ops disklabel64_ops = { // PR clang/3378 int bitwidth[] = { [(long long int)1] = 5, [(short int)2] = 2 }; int a[]= { [sizeof(int)] = 0 }; -int a2[]= { [0 ... sizeof(int)] = 0 }; +int a2[]= { [0 ... sizeof(int)] = 0 }; // expected-warning{{GNU array-range designator extension is unsupported}} + +// Test warnings about initializers overriding previous initializers +struct X { + int a, b, c; +}; + +int counter = 0; +int get8() { ++counter; return 8; } + +void test() { + struct X xs[] = { + [0] = (struct X){1, 2}, // expected-note{{previous initialization is here}} + [0].c = 3, // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}} + (struct X) {4, 5, 6}, // expected-note{{previous initialization is here}} + [1].b = get8(), // expected-warning{{subobject initialization overrides initialization of other fields within its enclosing subobject}} + [0].b = 8 + }; +} + +// FIXME: we need to +union { char c; long l; } u1 = { .l = 0xFFFF }; // expected-warning{{designated initialization of union member is broken}} diff --git a/test/Sema/vector-init.c b/test/Sema/vector-init.c index 1e2ba012c8..6913082228 100644 --- a/test/Sema/vector-init.c +++ b/test/Sema/vector-init.c @@ -1,5 +1,15 @@ -// RUN: clang %s -verify -fsyntax-only +// RUN: clang %s -fsyntax-only -verify typedef __attribute__(( ext_vector_type(4) )) float float4; +//typedef float float4 __attribute__((vector_size(16))); float4 foo = (float4){ 1.0, 2.0, 3.0, 4.0 }; + +float4 array[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; +int array_sizecheck[(sizeof(array) / sizeof(float4)) == 3? 1 : -1]; + +float4 array2[2] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, + 9.0 }; // expected-warning {{excess elements in array initializer}} + +float4 array3[2] = { {1.0, 2.0, 3.0}, 5.0, 6.0, 7.0, 8.0, + 9.0 }; // expected-warning {{excess elements in array initializer}} |