ORCA-C/Tests/Conformance/c11anonsu.c
Stephen Heumann 99e268e3b9 Implement support for anonymous structures and unions (C11).
Note that this implementation allows anonymous structures and unions to participate in initialization. That is, you can have a braced initializer list corresponding to an anonymous structure or union. Also, anonymous structures within unions follow the initialization rules for structures (and vice versa).

I think the better interpretation of the standard text is that anonymous structures and unions cannot participate in initialization as such, and instead their members are treated as members of the containing structure or union for purposes of initialization. However, all other compilers I am aware of allow anonymous structures and unions to participate in initialization, so I have implemented it that way too.
2022-10-16 18:44:19 -05:00

76 lines
1.6 KiB
C

/*
* Test anonymous structures and unions (C11).
*/
#include <stdio.h>
#include <stddef.h>
struct S {
int a;
union {
volatile struct {
long b;
char c;
};
double d;
};
} s1 = {1,2,3};
struct T {
int a;
union {
volatile struct {
long b;
char c;
} s;
double d;
} u;
};
int main(void) {
struct S *s1p = &s1;
if (s1.a != 1)
goto Fail;
if (s1.b != 2)
goto Fail;
if (s1.c != 3)
goto Fail;
s1.d = 123.5;
if (s1p->d != 123.5)
goto Fail;
struct S s2 = {4,5,6};
struct S *s2p = &s2;
if (s2.a != 4)
goto Fail;
if (s2.b != 5)
goto Fail;
if (s2.c != 6)
goto Fail;
s2.d = 123.5;
if (s2p->d != 123.5)
goto Fail;
if (sizeof(struct S) != sizeof(struct T))
goto Fail;
if (offsetof(struct S, a) != offsetof(struct T, a))
goto Fail;
if (offsetof(struct S, b) != offsetof(struct T, u.s.b))
goto Fail;
if (offsetof(struct S, c) != offsetof(struct T, u.s.c))
goto Fail;
if (offsetof(struct S, d) != offsetof(struct T, u.d))
goto Fail;
printf ("Passed Conformance Test c11anonsu\n");
return 0;
Fail:
printf ("Failed Conformance Test c11anonsu\n");
}