1 /* Test semantics of #pragma pack. 2 Contributed by Mike Coleman <mcoleman2@kc.rr.com> */ 3 4 /* { dg-do compile { target *-*-linux* *-*-cygwin* powerpc*-*-eabi* } } */ 5 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. 12 */ 13 14 #include <stddef.h> 15 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] 20 /* test a gap */ 21 #define testgap(n, a, b, val) test(n, gap(SNAME, a, b) == val) 22 23 #define SNAME s0 24 #include "pack-test-1.h" 25 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) 37 38 #undef SNAME 39 #define SNAME s1 40 #pragma pack(push, p1, 1) 41 #include "pack-test-1.h" 42 43 void SNAME() { 44 testgap(0, f1, f0, sizeof(char)); 45 testgap(1, f3, f2, sizeof(short)); 46 testgap(2, f5, f4, sizeof(int)); 47 } 48 49 #undef SNAME 50 #define SNAME s2 51 #pragma pack(push, p2, 2) 52 #include "pack-test-1.h" 53 54 void SNAME() { 55 testgap(0, f1, f0, sizeof(short)); 56 testgap(1, f3, f2, sizeof(short)); 57 testgap(2, f5, f4, sizeof(int)); 58 } 59 60 #undef SNAME 61 #define SNAME s3 62 #pragma pack(push, p3, 4) 63 #include "pack-test-1.h" 64 65 void SNAME() { 66 testgap(0, f1, f0, sizeof(int)); 67 testgap(1, f3, f2, sizeof(int)); 68 testgap(2, f5, f4, sizeof(int)); 69 } 70 71 #undef SNAME 72 #define SNAME s4 73 #pragma pack(pop) 74 #include "pack-test-1.h" 75 76 void SNAME() { 77 testgap(0, f1, f0, sizeof(short)); 78 testgap(1, f3, f2, sizeof(short)); 79 testgap(2, f5, f4, sizeof(int)); 80 } 81 82 #undef SNAME 83 #define SNAME s5 84 #pragma pack(pop, p2) 85 #include "pack-test-1.h" 86 87 void SNAME() { 88 testgap(0, f1, f0, sizeof(char)); 89 testgap(1, f3, f2, sizeof(short)); 90 testgap(2, f5, f4, sizeof(int)); 91 } 92 93 #undef SNAME 94 #define SNAME s6 95 #pragma pack(pop, p1) 96 #include "pack-test-1.h" 97 98 void SNAME() { 99 testgap(0, f1, f0, al1); 100 testgap(1, f3, f2, al3); 101 testgap(2, f5, f4, al5); 102 } 103 104 #undef SNAME 105 #define SNAME s7 106 #pragma pack(1) 107 #include "pack-test-1.h" 108 109 void SNAME() { 110 testgap(0, f1, f0, sizeof(char)); 111 testgap(1, f3, f2, sizeof(short)); 112 testgap(2, f5, f4, sizeof(int)); 113 } 114 115 #undef SNAME 116 #define SNAME s8 117 #pragma pack(push, p2, 2) 118 #include "pack-test-1.h" 119 120 void SNAME() { 121 testgap(0, f1, f0, sizeof(short)); 122 testgap(1, f3, f2, sizeof(short)); 123 testgap(2, f5, f4, sizeof(int)); 124 } 125 126 #undef SNAME 127 #define SNAME s9 128 #pragma pack(pop) 129 #include "pack-test-1.h" 130 131 void SNAME() { 132 testgap(0, f1, f0, sizeof(char)); 133 testgap(1, f3, f2, sizeof(short)); 134 testgap(2, f5, f4, sizeof(int)); 135 } 136 137 #undef SNAME 138 #define SNAME s10 139 #pragma pack() 140 #include "pack-test-1.h" 141 142 void SNAME() { 143 testgap(0, f1, f0, al1); 144 testgap(1, f3, f2, al3); 145 testgap(2, f5, f4, al5); 146 } 147