brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.7 KiB · 595b24d Raw
316 lines · c
1// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=i686-apple-darwin92// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=arm-linux-gnueabihf3// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=aarch64-linux-gnu4// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-pc-linux-gnu5// RUN: %clang_cc1 %s -fsyntax-only -verify -triple=x86_64-scei-ps46// RUN: %clang_cc1 %s -fsyntax-only -verify=checkms -triple=i686-apple-darwin9 -Wms-bitfield-padding7 8// expected-no-diagnostics9#include <stddef.h>10 11#define CHECK_SIZE(kind, name, size) \12  extern int name##_1[sizeof(kind name) == size ? 1 : -1];13#define CHECK_ALIGN(kind, name, size) \14  extern int name##_2[__alignof(kind name) == size ? 1 : -1];15#define CHECK_OFFSET(kind, name, member, offset) \16  extern int name##_3[offsetof(kind name, member) == offset ? 1 : -1];17 18// Zero-width bit-fields19struct a {char x; int : 0; char y;};20#if defined(__arm__) || defined(__aarch64__)21CHECK_SIZE(struct, a, 8)22CHECK_ALIGN(struct, a, 4)23#else24CHECK_SIZE(struct, a, 5)25CHECK_ALIGN(struct, a, 1)26#endif27 28// Zero-width bit-fields with packed29struct __attribute__((packed)) a2 {30  short x : 9; // #a2x31  char : 0; // #a2anon32  // checkms-warning@-1 {{bit-field '' of type 'char' has a different storage size than the preceding bit-field (1 vs 2 bytes) and will not be packed under the Microsoft ABI}}33  // checkms-note@#a2x {{preceding bit-field 'x' declared here with type 'short'}}34  int y : 17;35  // checkms-warning@-1 {{bit-field 'y' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the Microsoft ABI}}36  // checkms-note@#a2anon {{preceding bit-field '' declared here with type 'char'}}37};38 39CHECK_SIZE(struct, a2, 5)40CHECK_ALIGN(struct, a2, 1)41 42// Zero-width bit-fields at the end of packed struct43struct __attribute__((packed)) a3 {44  short x : 9; // #a3x45  int : 0;46  // checkms-warning@-1 {{bit-field '' of type 'int' has a different storage size than the preceding bit-field (4 vs 2 bytes) and will not be packed under the Microsoft ABI}}47  // checkms-note@#a3x {{preceding bit-field 'x' declared here with type 'short'}}48};49 50#if defined(__arm__) || defined(__aarch64__)51CHECK_SIZE(struct, a3, 4)52CHECK_ALIGN(struct, a3, 4)53#else54CHECK_SIZE(struct, a3, 4)55CHECK_ALIGN(struct, a3, 1)56#endif57 58// For comparison, non-zero-width bit-fields at the end of packed struct59struct __attribute__((packed)) a4 {60  short x : 9; // #a4x61  int : 1;62  // checkms-warning@-1 {{bit-field '' of type 'int' has a different storage size than the preceding bit-field (4 vs 2 bytes) and will not be packed under the Microsoft ABI}}63  // checkms-note@#a4x {{preceding bit-field 'x' declared here with type 'short'}}64};65CHECK_SIZE(struct, a4, 2)66CHECK_ALIGN(struct, a4, 1)67 68union b {char x; int : 0; char y;};69#if defined(__arm__) || defined(__aarch64__)70CHECK_SIZE(union, b, 4)71CHECK_ALIGN(union, b, 4)72#else73CHECK_SIZE(union, b, 1)74CHECK_ALIGN(union, b, 1)75#endif76 77// Unnamed bit-field align78struct c {char x; int : 20;};79#if defined(__arm__) || defined(__aarch64__)80CHECK_SIZE(struct, c, 4)81CHECK_ALIGN(struct, c, 4)82#else83CHECK_SIZE(struct, c, 4)84CHECK_ALIGN(struct, c, 1)85#endif86 87union d {char x; int : 20;};88#if defined(__arm__) || defined(__aarch64__)89CHECK_SIZE(union, d, 4)90CHECK_ALIGN(union, d, 4)91#else92CHECK_SIZE(union, d, 3)93CHECK_ALIGN(union, d, 1)94#endif95 96// Bit-field packing97struct __attribute__((packed)) e {int x : 4, y : 30, z : 30;};98CHECK_SIZE(struct, e, 8)99CHECK_ALIGN(struct, e, 1)100 101// Alignment on bit-fields102struct f {__attribute((aligned(8))) int x : 30, y : 30, z : 30;};103CHECK_SIZE(struct, f, 24)104CHECK_ALIGN(struct, f, 8)105 106// Large structure (overflows i32, in bits).107struct s0 {108  char a[0x32100000];109  int x:30, y:30;110};111 112CHECK_SIZE(struct, s0, 0x32100008)113CHECK_ALIGN(struct, s0, 4)114 115// Bit-field with explicit align bigger than normal.116struct g0 {117  char a;118  __attribute__((aligned(16))) int b : 1;119  char c;120};121 122#if defined(__ORBIS__)123CHECK_SIZE(struct, g0, 16);124CHECK_ALIGN(struct, g0, 16);125CHECK_OFFSET(struct, g0, c, 2);126#else127CHECK_SIZE(struct, g0, 32);128CHECK_ALIGN(struct, g0, 16);129CHECK_OFFSET(struct, g0, c, 17);130#endif131 132// Bit-field with explicit align smaller than normal.133struct g1 {134  char a;135  __attribute__((aligned(2))) int b : 1;136  char c;137};138 139CHECK_SIZE(struct, g1, 4);140CHECK_ALIGN(struct, g1, 4);141#if defined(__ORBIS__)142CHECK_OFFSET(struct, g1, c, 2);143#else144CHECK_OFFSET(struct, g1, c, 3);145#endif146 147// Same as above but without explicit align.148struct g2 {149  char a;150  int b : 1;151  char c;152};153 154CHECK_SIZE(struct, g2, 4);155CHECK_ALIGN(struct, g2, 4);156CHECK_OFFSET(struct, g2, c, 2);157 158// Explicit attribute align on bit-field has precedence over packed attribute159// applied too the struct.160struct __attribute__((packed)) g3 {161  char a;162  __attribute__((aligned(16))) int b : 1;163  char c;164};165 166CHECK_ALIGN(struct, g3, 16);167#if defined(__ORBIS__)168CHECK_SIZE(struct, g3, 16);169CHECK_OFFSET(struct, g3, c, 2);170#else171CHECK_SIZE(struct, g3, 32);172CHECK_OFFSET(struct, g3, c, 17);173#endif174 175struct __attribute__((packed)) g4 {176  char a;177  __attribute__((aligned(2))) int b : 1;178  char c;179};180 181CHECK_SIZE(struct, g4, 4);182CHECK_ALIGN(struct, g4, 2);183#if defined(__ORBIS__)184CHECK_OFFSET(struct, g4, c, 2);185#else186CHECK_OFFSET(struct, g4, c, 3);187#endif188 189struct g5 {190  char : 1; // #g5191  __attribute__((aligned(1))) int n : 24;192  // checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the Microsoft ABI}}193  // checkms-note@#g5 {{preceding bit-field '' declared here with type 'char'}}194};195CHECK_SIZE(struct, g5, 4);196CHECK_ALIGN(struct, g5, 4);197 198struct __attribute__((packed)) g6 {199  char : 1; // #g6200  __attribute__((aligned(1))) int n : 24;201  // checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the Microsoft ABI}}202  // checkms-note@#g6 {{preceding bit-field '' declared here with type 'char'}}203};204CHECK_SIZE(struct, g6, 4);205CHECK_ALIGN(struct, g6, 1);206 207struct g7 {208  char : 1; // #g7209  __attribute__((aligned(1))) int n : 25;210  // checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the Microsoft ABI}}211  // checkms-note@#g7 {{preceding bit-field '' declared here with type 'char'}}212};213#if defined(__ORBIS__)214CHECK_SIZE(struct, g7, 4);215#else216CHECK_SIZE(struct, g7, 8);217#endif218CHECK_ALIGN(struct, g7, 4);219 220struct __attribute__((packed)) g8 {221  char : 1; // #g8222  __attribute__((aligned(1))) int n : 25;223  // checkms-warning@-1 {{bit-field 'n' of type 'int' has a different storage size than the preceding bit-field (4 vs 1 bytes) and will not be packed under the Microsoft ABI}}224  // checkms-note@#g8 {{preceding bit-field '' declared here with type 'char'}}225};226#if defined(__ORBIS__)227CHECK_SIZE(struct, g8, 4);228#else229CHECK_SIZE(struct, g8, 5);230#endif231CHECK_ALIGN(struct, g8, 1);232 233struct g9 {234  __attribute__((aligned(1))) char a : 2, b : 2, c : 2, d : 2, e : 2;235  int i;236};237#if defined(__ORBIS__)238CHECK_SIZE(struct, g9, 8);239#else240CHECK_SIZE(struct, g9, 12);241#endif242CHECK_ALIGN(struct, g9, 4);243 244struct __attribute__((packed)) g10 {245  __attribute__((aligned(1))) char a : 2, b : 2, c : 2, d : 2, e : 2;246  int i;247};248#if defined(__ORBIS__)249CHECK_SIZE(struct, g10, 6);250#else251CHECK_SIZE(struct, g10, 9);252#endif253CHECK_ALIGN(struct, g10, 1);254 255struct g11 {256  char a;257  __attribute__((aligned(1))) long long b : 62;258  char c;259};260#if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__)261CHECK_SIZE(struct, g11, 24);262CHECK_ALIGN(struct, g11, 8);263CHECK_OFFSET(struct, g11, c, 16);264#else265CHECK_SIZE(struct, g11, 16);266CHECK_ALIGN(struct, g11, 4);267CHECK_OFFSET(struct, g11, c, 12);268#endif269 270struct __attribute__((packed)) g12 {271  char a;272  __attribute__((aligned(1))) long long b : 62;273  char c;274};275CHECK_SIZE(struct, g12, 10);276CHECK_ALIGN(struct, g12, 1);277CHECK_OFFSET(struct, g12, c, 9);278 279struct g13 {280  char a;281  __attribute__((aligned(1))) long long : 0;282  char c;283};284#if defined(__arm__) || defined(__aarch64__)285CHECK_SIZE(struct, g13, 16);286CHECK_ALIGN(struct, g13, 8);287CHECK_OFFSET(struct, g13, c, 8);288#elif defined(__x86_64__)289CHECK_SIZE(struct, g13, 9);290CHECK_ALIGN(struct, g13, 1);291CHECK_OFFSET(struct, g13, c, 8);292#else293CHECK_SIZE(struct, g13, 5);294CHECK_ALIGN(struct, g13, 1);295CHECK_OFFSET(struct, g13, c, 4);296#endif297 298struct __attribute__((packed)) g14 {299  char a;300  __attribute__((aligned(1))) long long : 0;301  char c;302};303#if defined(__arm__) || defined(__aarch64__)304CHECK_SIZE(struct, g14, 16);305CHECK_ALIGN(struct, g14, 8);306CHECK_OFFSET(struct, g14, c, 8);307#elif defined(__x86_64__)308CHECK_SIZE(struct, g14, 9);309CHECK_ALIGN(struct, g14, 1);310CHECK_OFFSET(struct, g14, c, 8);311#else312CHECK_SIZE(struct, g14, 5);313CHECK_ALIGN(struct, g14, 1);314CHECK_OFFSET(struct, g14, c, 4);315#endif316