Debian Codegraph @

gcc/testsuite/g++.dg/ext/flexary39.C

raw
1 // PR c++/88578
2 // { dg-do run }
3 // { dg-options -Wno-pedantic }
4
5 #define STR(s) #s
6 #define ASSERT(exp) \
7 ((exp) ? (void)0 : (void)(__builtin_printf ("%s:%i: assertion %s failed\n", \
8 __FILE__, __LINE__, STR(exp)), \
9 __builtin_abort ()))
10
11 typedef int int32_t __attribute__((mode (__SI__)));
12
13 struct Ax { int32_t n, a[]; };
14 struct AAx { int32_t i; Ax ax; };
15
16 int32_t i = 12345678;
17
18 void
19 test ()
20 {
21 {
22 // OK. Does not assign any elements to flexible array.
23 Ax s = { 0 };
24 ASSERT (s.n == 0);
25 }
26 {
27 // OK only for statically allocated objects, otherwise error.
28 static Ax s = { 0, { } };
29 ASSERT (s.n == 0);
30 }
31 {
32 static Ax s = { 1, { 2 } };
33 ASSERT (s.n == 1 && s.a [0] == 2);
34 }
35 {
36 static Ax s = { 2, { 3, 4 } };
37 ASSERT (s.n = 2 && s.a [0] == 3 && s.a [1] == 4);
38 }
39 {
40 static Ax s = { 123, i };
41 ASSERT (s.n == 123 && s.a [0] == i);
42 }
43 {
44 static Ax s = { 456, { i } };
45 ASSERT (s.n == 456 && s.a [0] == i);
46 }
47 {
48 int32_t j = i + 1, k = j + 1;
49 static Ax s = { 3, { i, j, k } };
50 ASSERT (s.n == 3 && s.a [0] == i && s.a [1] == j && s.a [2] == k);
51 }
52
53 {
54 // OK. Does not assign any elements to flexible array.
55 AAx s = { 1, { 2 } };
56 ASSERT (s.i == 1 && s.ax.n == 2);
57 }
58 }
59
60 int
61 main ()
62 {
63 test ();
64 test ();
65 }