Issue with designated initializers and vectors

The following code fails in clang:

typedef float __attribute__((vector_size (16))) v4f_t;

typedef union {
     struct {
         float x, y, z, w;
     }s;
     v4f_t v;
} vector_t;

vector_t foo(v4f_t p)
{
   vector_t v = {.v = p};
   return v;
}

europa:test holm$ clang -std=gnu99 -c test.c
test.c:13:22: error: incompatible type initializing 'v4f_t', expected 'float'
   vector_t v = {.v = p};
                      ^

I might be missing options to make this work, if so what flags should I add to clang (I want to support clang in my makefiles for my project as an optional compiler). The code builds fine with gcc and the same options.

Regards,
Mattias

Thanks for the report; fixed in r73292.

-Eli

Eli Friedman wrote:

Thanks for the report; fixed in r73292.

-Eli

Thanks works fine now :slight_smile:

Here is another one, the parser and sema works fine in this case but the code generator does something wrong I think. The following program segfaults when built with clang (tested on darwin x86 both 32 and 64 bit targets), but works fine with GCC.

It crashes on the line p.v[0] = a->v[0] * v.v;

typedef float __attribute__((vector_size (16))) v4f_t;

typedef union {
     float a[4];
     struct {
         float x, y, z, w;
     }s;
     v4f_t v;
} vector_t;

typedef union {
     float a[4][4];
     v4f_t v[4];
} matrix_t;

vector_t
foo(const matrix_t *a, const vector_t v)
{
   vector_t vr;
   matrix_t p;
   p.v[0] = a->v[0] * v.v;

   vr.v = p.v[0];

   return vr;
}

int
main(int argc, char **argv)
{
   matrix_t m;
   for (int i = 0 ; i < 4; i ++) {
     for (int j = 0 ; j < 4; j ++) {
       m.a[i][j] = 1.0;
     }
   }
   vector_t v = {.a = {1.0, 1.0, 1.0, 1.0}};

   vector_t r = foo(&m, v);

   return 0;
}

/ Mattias

Should be fixed for x86-32 in r73306; someone with an x86-64 setup
will need to figure out the equivalent fix there.

-Eli