1 // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin9 2 // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=arm-linux-gnueabihf 3 // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=aarch64-linux-gnu 4 // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-pc-linux-gnu 5 // RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-scei-ps4 6 // expected-no-diagnostics 7 #include <stddef.h> 8 9 #define CHECK_SIZE(kind, name, size) \ 10 extern int name##_1[sizeof(kind name) == size ? 1 : -1]; 11 #define CHECK_ALIGN(kind, name, size) \ 12 extern int name##_2[__alignof(kind name) == size ? 1 : -1]; 13 #define CHECK_OFFSET(kind, name, member, offset) \ 14 extern int name##_3[offsetof(kind name, member) == offset ? 1 : -1]; 15 16 // Zero-width bit-fields 17 struct a {char x; int : 0; char y;}; 18 #if defined(__arm__) || defined(__aarch64__) 19 CHECK_SIZE(struct, a, 8) 20 CHECK_ALIGN(struct, a, 4) 21 #else 22 CHECK_SIZE(struct, a, 5) 23 CHECK_ALIGN(struct, a, 1) 24 #endif 25 26 // Zero-width bit-fields with packed 27 struct __attribute__((packed)) a2 { short x : 9; char : 0; int y : 17; }; 28 CHECK_SIZE(struct, a2, 5) 29 CHECK_ALIGN(struct, a2, 1) 30 31 // Zero-width bit-fields at the end of packed struct 32 struct __attribute__((packed)) a3 { short x : 9; int : 0; }; 33 #if defined(__arm__) || defined(__aarch64__) 34 CHECK_SIZE(struct, a3, 4) 35 CHECK_ALIGN(struct, a3, 4) 36 #else 37 CHECK_SIZE(struct, a3, 4) 38 CHECK_ALIGN(struct, a3, 1) 39 #endif 40 41 // For comparison, non-zero-width bit-fields at the end of packed struct 42 struct __attribute__((packed)) a4 { short x : 9; int : 1; }; 43 CHECK_SIZE(struct, a4, 2) 44 CHECK_ALIGN(struct, a4, 1) 45 46 union b {char x; int : 0; char y;}; 47 #if defined(__arm__) || defined(__aarch64__) 48 CHECK_SIZE(union, b, 4) 49 CHECK_ALIGN(union, b, 4) 50 #else 51 CHECK_SIZE(union, b, 1) 52 CHECK_ALIGN(union, b, 1) 53 #endif 54 55 // Unnamed bit-field align 56 struct c {char x; int : 20;}; 57 #if defined(__arm__) || defined(__aarch64__) 58 CHECK_SIZE(struct, c, 4) 59 CHECK_ALIGN(struct, c, 4) 60 #else 61 CHECK_SIZE(struct, c, 4) 62 CHECK_ALIGN(struct, c, 1) 63 #endif 64 65 union d {char x; int : 20;}; 66 #if defined(__arm__) || defined(__aarch64__) 67 CHECK_SIZE(union, d, 4) 68 CHECK_ALIGN(union, d, 4) 69 #else 70 CHECK_SIZE(union, d, 3) 71 CHECK_ALIGN(union, d, 1) 72 #endif 73 74 // Bit-field packing 75 struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;}; 76 CHECK_SIZE(struct, e, 8) 77 CHECK_ALIGN(struct, e, 1) 78 79 // Alignment on bit-fields 80 struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;}; 81 CHECK_SIZE(struct, f, 24) 82 CHECK_ALIGN(struct, f, 8) 83 84 // Large structure (overflows i32, in bits). 85 struct s0 { 86 char a[0x32100000]; 87 int x:30, y:30; 88 }; 89 90 CHECK_SIZE(struct, s0, 0x32100008) 91 CHECK_ALIGN(struct, s0, 4) 92 93 // Bit-field with explicit align bigger than normal. 94 struct g0 { 95 char a; 96 __attribute__((aligned(16))) int b : 1; 97 char c; 98 }; 99 100 #if defined(__ORBIS__) 101 CHECK_SIZE(struct, g0, 16); 102 CHECK_ALIGN(struct, g0, 16); 103 CHECK_OFFSET(struct, g0, c, 2); 104 #else 105 CHECK_SIZE(struct, g0, 32); 106 CHECK_ALIGN(struct, g0, 16); 107 CHECK_OFFSET(struct, g0, c, 17); 108 #endif 109 110 // Bit-field with explicit align smaller than normal. 111 struct g1 { 112 char a; 113 __attribute__((aligned(2))) int b : 1; 114 char c; 115 }; 116 117 CHECK_SIZE(struct, g1, 4); 118 CHECK_ALIGN(struct, g1, 4); 119 #if defined(__ORBIS__) 120 CHECK_OFFSET(struct, g1, c, 2); 121 #else 122 CHECK_OFFSET(struct, g1, c, 3); 123 #endif 124 125 // Same as above but without explicit align. 126 struct g2 { 127 char a; 128 int b : 1; 129 char c; 130 }; 131 132 CHECK_SIZE(struct, g2, 4); 133 CHECK_ALIGN(struct, g2, 4); 134 CHECK_OFFSET(struct, g2, c, 2); 135 136 // Explicit attribute align on bit-field has precedence over packed attribute 137 // applied too the struct. 138 struct __attribute__((packed)) g3 { 139 char a; 140 __attribute__((aligned(16))) int b : 1; 141 char c; 142 }; 143 144 CHECK_ALIGN(struct, g3, 16); 145 #if defined(__ORBIS__) 146 CHECK_SIZE(struct, g3, 16); 147 CHECK_OFFSET(struct, g3, c, 2); 148 #else 149 CHECK_SIZE(struct, g3, 32); 150 CHECK_OFFSET(struct, g3, c, 17); 151 #endif 152 153 struct __attribute__((packed)) g4 { 154 char a; 155 __attribute__((aligned(2))) int b : 1; 156 char c; 157 }; 158 159 CHECK_SIZE(struct, g4, 4); 160 CHECK_ALIGN(struct, g4, 2); 161 #if defined(__ORBIS__) 162 CHECK_OFFSET(struct, g4, c, 2); 163 #else 164 CHECK_OFFSET(struct, g4, c, 3); 165 #endif 166 167 struct g5 { 168 char : 1; 169 __attribute__((aligned(1))) int n : 24; 170 }; 171 CHECK_SIZE(struct, g5, 4); 172 CHECK_ALIGN(struct, g5, 4); 173 174 struct __attribute__((packed)) g6 { 175 char : 1; 176 __attribute__((aligned(1))) int n : 24; 177 }; 178 CHECK_SIZE(struct, g6, 4); 179 CHECK_ALIGN(struct, g6, 1); 180 181 struct g7 { 182 char : 1; 183 __attribute__((aligned(1))) int n : 25; 184 }; 185 #if defined(__ORBIS__) 186 CHECK_SIZE(struct, g7, 4); 187 #else 188 CHECK_SIZE(struct, g7, 8); 189 #endif 190 CHECK_ALIGN(struct, g7, 4); 191 192 struct __attribute__((packed)) g8 { 193 char : 1; 194 __attribute__((aligned(1))) int n : 25; 195 }; 196 #if defined(__ORBIS__) 197 CHECK_SIZE(struct, g8, 4); 198 #else 199 CHECK_SIZE(struct, g8, 5); 200 #endif 201 CHECK_ALIGN(struct, g8, 1); 202 203 struct g9 { 204 __attribute__((aligned(1))) char a : 2, b : 2, c : 2, d : 2, e : 2; 205 int i; 206 }; 207 #if defined(__ORBIS__) 208 CHECK_SIZE(struct, g9, 8); 209 #else 210 CHECK_SIZE(struct, g9, 12); 211 #endif 212 CHECK_ALIGN(struct, g9, 4); 213 214 struct __attribute__((packed)) g10 { 215 __attribute__((aligned(1))) char a : 2, b : 2, c : 2, d : 2, e : 2; 216 int i; 217 }; 218 #if defined(__ORBIS__) 219 CHECK_SIZE(struct, g10, 6); 220 #else 221 CHECK_SIZE(struct, g10, 9); 222 #endif 223 CHECK_ALIGN(struct, g10, 1); 224 225 struct g11 { 226 char a; 227 __attribute__((aligned(1))) long long b : 62; 228 char c; 229 }; 230 #if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__) 231 CHECK_SIZE(struct, g11, 24); 232 CHECK_ALIGN(struct, g11, 8); 233 CHECK_OFFSET(struct, g11, c, 16); 234 #else 235 CHECK_SIZE(struct, g11, 16); 236 CHECK_ALIGN(struct, g11, 4); 237 CHECK_OFFSET(struct, g11, c, 12); 238 #endif 239 240 struct __attribute__((packed)) g12 { 241 char a; 242 __attribute__((aligned(1))) long long b : 62; 243 char c; 244 }; 245 CHECK_SIZE(struct, g12, 10); 246 CHECK_ALIGN(struct, g12, 1); 247 CHECK_OFFSET(struct, g12, c, 9); 248 249 struct g13 { 250 char a; 251 __attribute__((aligned(1))) long long : 0; 252 char c; 253 }; 254 #if defined(__arm__) || defined(__aarch64__) 255 CHECK_SIZE(struct, g13, 16); 256 CHECK_ALIGN(struct, g13, 8); 257 CHECK_OFFSET(struct, g13, c, 8); 258 #elif defined(__x86_64__) 259 CHECK_SIZE(struct, g13, 9); 260 CHECK_ALIGN(struct, g13, 1); 261 CHECK_OFFSET(struct, g13, c, 8); 262 #else 263 CHECK_SIZE(struct, g13, 5); 264 CHECK_ALIGN(struct, g13, 1); 265 CHECK_OFFSET(struct, g13, c, 4); 266 #endif 267 268 struct __attribute__((packed)) g14 { 269 char a; 270 __attribute__((aligned(1))) long long : 0; 271 char c; 272 }; 273 #if defined(__arm__) || defined(__aarch64__) 274 CHECK_SIZE(struct, g14, 16); 275 CHECK_ALIGN(struct, g14, 8); 276 CHECK_OFFSET(struct, g14, c, 8); 277 #elif defined(__x86_64__) 278 CHECK_SIZE(struct, g14, 9); 279 CHECK_ALIGN(struct, g14, 1); 280 CHECK_OFFSET(struct, g14, c, 8); 281 #else 282 CHECK_SIZE(struct, g14, 5); 283 CHECK_ALIGN(struct, g14, 1); 284 CHECK_OFFSET(struct, g14, c, 4); 285 #endif 286