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