1 /* Test semantics of #pragma pack.
2 Contributed by Mike Coleman <mcoleman2@kc.rr.com> */
4 /* { dg-do compile { target { { *-*-linux* *-*-cygwin* powerpc*-*-eabi* } && { ! default_packed } } } } */
6 /* We only test the alignment of char, short, and int, because these
7 are the only ones that are pretty certain to be the same across
8 platforms (and maybe not even those). Mainly we're just testing
9 whether pushing and popping seem to be working correctly, and
10 verifying the (alignment == 1) case, which is really the only
11 reason anyone would use this pragma anyway.
16 /* gap in bytes between fields a and b in struct s */
17 #define gap(s, a, b) (offsetof(struct s, a) - offsetof(struct s, b))
18 /* generalized compile-time test expression */
19 #define test(n, expr) int test_##n [(expr) ? 1 : -1]
21 #define testgap(n, a, b, val) test(n, gap(SNAME, a, b) == val)
24 #include "pack-test-1.h"
26 /* Save original alignment values. Can't use const ints because they
27 won't be expanded and we'll get bogus errors about variable length
28 arrays. (Possible bug in C front end?) Use s0, not SNAME, so these
29 won't change later. */
30 #define al1 gap(s0, f1, f0)
31 #define al2 gap(s0, f2, f1)
32 #define al3 gap(s0, f3, f2)
33 #define al4 gap(s0, f4, f3)
34 #define al5 gap(s0, f5, f4)
35 #define al6 gap(s0, f6, f5)
36 #define al7 gap(s0, f7, f6)
40 #pragma pack(push, p1, 1)
41 #include "pack-test-1.h"
44 testgap(0, f1
, f0
, sizeof(char));
45 testgap(1, f3
, f2
, sizeof(short));
46 testgap(2, f5
, f4
, sizeof(int));
51 #pragma pack(push, p2, 2)
52 #include "pack-test-1.h"
55 testgap(0, f1
, f0
, sizeof(short));
56 testgap(1, f3
, f2
, sizeof(short));
57 testgap(2, f5
, f4
, sizeof(int));
62 #pragma pack(push, p3, 4)
63 #include "pack-test-1.h"
66 testgap(0, f1
, f0
, sizeof(int));
67 testgap(1, f3
, f2
, sizeof(int));
68 testgap(2, f5
, f4
, sizeof(int));
74 #include "pack-test-1.h"
77 testgap(0, f1
, f0
, sizeof(short));
78 testgap(1, f3
, f2
, sizeof(short));
79 testgap(2, f5
, f4
, sizeof(int));
85 #include "pack-test-1.h"
88 testgap(0, f1
, f0
, sizeof(char));
89 testgap(1, f3
, f2
, sizeof(short));
90 testgap(2, f5
, f4
, sizeof(int));
96 #include "pack-test-1.h"
99 testgap(0, f1
, f0
, al1
);
100 testgap(1, f3
, f2
, al3
);
101 testgap(2, f5
, f4
, al5
);
107 #include "pack-test-1.h"
110 testgap(0, f1
, f0
, sizeof(char));
111 testgap(1, f3
, f2
, sizeof(short));
112 testgap(2, f5
, f4
, sizeof(int));
117 #pragma pack(push, p2, 2)
118 #include "pack-test-1.h"
121 testgap(0, f1
, f0
, sizeof(short));
122 testgap(1, f3
, f2
, sizeof(short));
123 testgap(2, f5
, f4
, sizeof(int));
129 #include "pack-test-1.h"
132 testgap(0, f1
, f0
, sizeof(char));
133 testgap(1, f3
, f2
, sizeof(short));
134 testgap(2, f5
, f4
, sizeof(int));
140 #include "pack-test-1.h"
143 testgap(0, f1
, f0
, al1
);
144 testgap(1, f3
, f2
, al3
);
145 testgap(2, f5
, f4
, al5
);