| 1 |
/* Test semantics of #pragma pack. |
| 2 |
Contributed by Mike Coleman <[email protected]> */ |
| 3 |
|
| 4 |
/* { dg-do compile { target { ! default_packed } } } */ |
| 5 |
|
| 6 |
/* Mainly we're just testing whether pushing and popping seem to be |
| 7 |
working correctly, and verifying the (alignment == 1) case, which |
| 8 |
is really the only reason anyone would use this pragma anyway. */ |
| 9 |
|
| 10 |
#include <stddef.h> |
| 11 |
|
| 12 |
/* generalized compile-time test expression */ |
| 13 |
#define test(n, expr) int test_##n [(expr) ? 1 : -1] |
| 14 |
|
| 15 |
/* Round V down to multiple of A */ |
| 16 |
#define floor(v,a) ((v) / (a) * (a)) |
| 17 |
|
| 18 |
/* Offset of field with alignment A in structure S after a field P of |
| 19 |
type PT */ |
| 20 |
#define offset(s,p,pt,a) \ |
| 21 |
floor ((offsetof(struct s, p) + sizeof (pt) + (a) - 1), a) |
| 22 |
|
| 23 |
/* regular minimum */ |
| 24 |
#define min(a,b) ((a) < (b) ? (a) : (b)) |
| 25 |
|
| 26 |
/* Check that field A (type AT) followed by field B (type BT) are |
| 27 |
packed according to P */ |
| 28 |
#define test_pack(n, a, at, b, bt, p) \ |
| 29 |
test(n, offsetof (struct SNAME, b) \ |
| 30 |
== min (offset (SNAME,a,at,__alignof__(bt)), \ |
| 31 |
offset (SNAME,a,at,p))) |
| 32 |
|
| 33 |
/* Test offset of field F in structs s1 and s2 are the same. */ |
| 34 |
#define test_offset(n, s1, s2, f) \ |
| 35 |
test (n, (offsetof(struct s1, f) == offsetof(struct s2, f))) |
| 36 |
|
| 37 |
#define SNAME s0 |
| 38 |
#include "pack-test-1.h" |
| 39 |
|
| 40 |
#undef SNAME |
| 41 |
#define SNAME s1 |
| 42 |
#pragma pack(push, p1, 1) |
| 43 |
#include "pack-test-1.h" |
| 44 |
|
| 45 |
void SNAME() { |
| 46 |
test_pack(0, f0, char, f1, double, 1); |
| 47 |
test_pack(1, f2, short, f3, double, 1); |
| 48 |
test_pack(2, f4, int, f5, double, 1); |
| 49 |
} |
| 50 |
|
| 51 |
#undef SNAME |
| 52 |
#define SNAME s2 |
| 53 |
#pragma pack(push, p2, 2) |
| 54 |
#include "pack-test-1.h" |
| 55 |
|
| 56 |
void SNAME() { |
| 57 |
test_pack(0, f0, char, f1, double, 2); |
| 58 |
test_pack(1, f2, short, f3, double, 2); |
| 59 |
test_pack(2, f4, int, f5, double, 2); |
| 60 |
} |
| 61 |
|
| 62 |
#undef SNAME |
| 63 |
#define SNAME s3 |
| 64 |
#pragma pack(push, p3, 4) |
| 65 |
#include "pack-test-1.h" |
| 66 |
|
| 67 |
void SNAME() { |
| 68 |
test_pack(0, f0, char, f1, double, 4); |
| 69 |
test_pack(1, f2, short, f3, double, 4); |
| 70 |
test_pack(2, f4, int, f5, double, 4); |
| 71 |
} |
| 72 |
|
| 73 |
#undef SNAME |
| 74 |
#define SNAME s4 |
| 75 |
#pragma pack(pop) |
| 76 |
#include "pack-test-1.h" |
| 77 |
|
| 78 |
void SNAME() { |
| 79 |
test_pack(0, f0, char, f1, double, 2); |
| 80 |
test_pack(1, f2, short, f3, double, 2); |
| 81 |
test_pack(2, f4, int, f5, double, 2); |
| 82 |
} |
| 83 |
|
| 84 |
#undef SNAME |
| 85 |
#define SNAME s5 |
| 86 |
#pragma pack(pop, p2) |
| 87 |
#include "pack-test-1.h" |
| 88 |
|
| 89 |
void SNAME() { |
| 90 |
test_pack(0, f0, char, f1, double, 1); |
| 91 |
test_pack(1, f2, short, f3, double, 1); |
| 92 |
test_pack(2, f4, int, f5, double, 1); |
| 93 |
} |
| 94 |
|
| 95 |
#undef SNAME |
| 96 |
#define SNAME s6 |
| 97 |
#pragma pack(pop, p1) |
| 98 |
#include "pack-test-1.h" |
| 99 |
|
| 100 |
void SNAME() { |
| 101 |
test_offset (0, s0, SNAME, f0); |
| 102 |
test_offset (1, s0, SNAME, f1); |
| 103 |
test_offset (2, s0, SNAME, f2); |
| 104 |
test_offset (3, s0, SNAME, f3); |
| 105 |
test_offset (4, s0, SNAME, f4); |
| 106 |
test_offset (5, s0, SNAME, f5); |
| 107 |
} |
| 108 |
|
| 109 |
#undef SNAME |
| 110 |
#define SNAME s7 |
| 111 |
#pragma pack(1) |
| 112 |
#include "pack-test-1.h" |
| 113 |
|
| 114 |
void SNAME() { |
| 115 |
test_pack(0, f0, char, f1, double, 1); |
| 116 |
test_pack(1, f2, short, f3, double, 1); |
| 117 |
test_pack(2, f4, int, f5, double, 1); |
| 118 |
} |
| 119 |
|
| 120 |
#undef SNAME |
| 121 |
#define SNAME s8 |
| 122 |
#pragma pack(push, p2, 2) |
| 123 |
#include "pack-test-1.h" |
| 124 |
|
| 125 |
void SNAME() { |
| 126 |
test_pack(0, f0, char, f1, double, 2); |
| 127 |
test_pack(1, f2, short, f3, double, 2); |
| 128 |
test_pack(2, f4, int, f5, double, 2); |
| 129 |
} |
| 130 |
|
| 131 |
#undef SNAME |
| 132 |
#define SNAME s9 |
| 133 |
#pragma pack(pop) |
| 134 |
#include "pack-test-1.h" |
| 135 |
|
| 136 |
void SNAME() { |
| 137 |
test_pack(0, f0, char, f1, double, 1); |
| 138 |
test_pack(1, f2, short, f3, double, 1); |
| 139 |
test_pack(2, f4, int, f5, double, 1); |
| 140 |
} |
| 141 |
|
| 142 |
#undef SNAME |
| 143 |
#define SNAME s10 |
| 144 |
#pragma pack() |
| 145 |
#include "pack-test-1.h" |
| 146 |
|
| 147 |
void SNAME() { |
| 148 |
test_offset (0, s0, SNAME, f0); |
| 149 |
test_offset (1, s0, SNAME, f1); |
| 150 |
test_offset (2, s0, SNAME, f2); |
| 151 |
test_offset (3, s0, SNAME, f3); |
| 152 |
test_offset (4, s0, SNAME, f4); |
| 153 |
test_offset (5, s0, SNAME, f5); |
| 154 |
} |