1// RUN: %clang_cc1 %s -fsyntax-only -verify 2// RUN: %clang_cc1 %s -fsyntax-only -triple=x86_64-windows-coff -verify 3 4// Packed structs. 5struct s { 6 char a; 7 int b __attribute__((packed)); 8 char c; 9 int d; 10}; 11 12extern int a1[sizeof(struct s) == 12 ? 1 : -1]; 13extern int a2[__alignof(struct s) == 4 ? 1 : -1]; 14 15struct __attribute__((packed)) packed_s { 16 char a; 17 int b __attribute__((packed)); 18 char c; 19 int d; 20}; 21 22extern int b1[sizeof(struct packed_s) == 10 ? 1 : -1]; 23extern int b2[__alignof(struct packed_s) == 1 ? 1 : -1]; 24 25struct fas { 26 char a; 27 int b[]; 28}; 29 30extern int c1[sizeof(struct fas) == 4 ? 1 : -1]; 31extern int c2[__alignof(struct fas) == 4 ? 1 : -1]; 32 33struct __attribute__((packed)) packed_fas { 34 char a; 35 int b[]; 36}; 37 38extern int d1[sizeof(struct packed_fas) == 1 ? 1 : -1]; 39extern int d2[__alignof(struct packed_fas) == 1 ? 1 : -1]; 40 41struct packed_after_fas { 42 char a; 43 int b[]; 44} __attribute__((packed)); 45 46extern int d1_2[sizeof(struct packed_after_fas) == 1 ? 1 : -1]; 47extern int d2_2[__alignof(struct packed_after_fas) == 1 ? 1 : -1]; 48 49// Alignment 50 51struct __attribute__((aligned(8))) as1 { 52 char c; 53}; 54 55extern int e1[sizeof(struct as1) == 8 ? 1 : -1]; 56extern int e2[__alignof(struct as1) == 8 ? 1 : -1]; 57 58struct __attribute__((aligned)) as1_2 { 59 char c; 60}; 61#ifdef __s390x__ 62extern int e1_2[sizeof(struct as1_2) == 8 ? 1 : -1]; 63extern int e2_2[__alignof(struct as1_2) == 8 ? 1 : -1]; 64#else 65extern int e1_2[sizeof(struct as1_2) == 16 ? 1 : -1]; 66extern int e2_2[__alignof(struct as1_2) == 16 ? 1 : -1]; 67#endif 68 69struct as2 { 70 char c; 71 int __attribute__((aligned(8))) a; 72}; 73 74extern int f1[sizeof(struct as2) == 16 ? 1 : -1]; 75extern int f2[__alignof(struct as2) == 8 ? 1 : -1]; 76 77struct __attribute__((packed)) as3 { 78 char c; 79 int a; 80 int __attribute__((aligned(8))) b; 81}; 82 83extern int g1[sizeof(struct as3) == 16 ? 1 : -1]; 84extern int g2[__alignof(struct as3) == 8 ? 1 : -1]; 85 86 87// rdar://5921025 88struct packedtest { 89 int ted_likes_cheese; 90 void *args[] __attribute__((packed)); 91}; 92 93// Packed union 94union __attribute__((packed)) au4 {char c; int x;}; 95extern int h1[sizeof(union au4) == 4 ? 1 : -1]; 96extern int h2[__alignof(union au4) == 1 ? 1 : -1]; 97 98// Aligned union 99union au5 {__attribute__((aligned(4))) char c;}; 100extern int h1[sizeof(union au5) == 4 ? 1 : -1]; 101extern int h2[__alignof(union au5) == 4 ? 1 : -1]; 102 103// Alignment+packed 104struct as6 {char c; __attribute__((packed, aligned(2))) int x;}; 105extern int i1[sizeof(struct as6) == 6 ? 1 : -1]; 106extern int i2[__alignof(struct as6) == 2 ? 1 : -1]; 107 108union au6 {char c; __attribute__((packed, aligned(2))) int x;}; 109extern int k1[sizeof(union au6) == 4 ? 1 : -1]; 110extern int k2[__alignof(union au6) == 2 ? 1 : -1]; 111 112// Check postfix attributes 113union au7 {char c; int x;} __attribute__((packed)); 114extern int l1[sizeof(union au7) == 4 ? 1 : -1]; 115extern int l2[__alignof(union au7) == 1 ? 1 : -1]; 116 117struct packed_fas2 { 118 char a; 119 int b[]; 120} __attribute__((packed)); 121 122extern int m1[sizeof(struct packed_fas2) == 1 ? 1 : -1]; 123extern int m2[__alignof(struct packed_fas2) == 1 ? 1 : -1]; 124 125// Attribute aligned can round down typedefs. PR9253 126typedef long long __attribute__((aligned(1))) nt; 127 128struct nS { 129 char buf_nr; 130 nt start_lba; 131}; 132 133#if defined(_WIN32) && !defined(__declspec) // _MSC_VER is unavailable in cc1. 134// Alignment doesn't affect packing in MS mode. 135extern int n1[sizeof(struct nS) == 16 ? 1 : -1]; 136extern int n2[__alignof(struct nS) == 8 ? 1 : -1]; 137#else 138extern int n1[sizeof(struct nS) == 9 ? 1 : -1]; 139extern int n2[__alignof(struct nS) == 1 ? 1 : -1]; 140#endif 141 142// Packed attribute shouldn't be ignored for bit-field of char types. 143// Note from GCC reference manual: The 4.1, 4.2 and 4.3 series of GCC ignore 144// the packed attribute on bit-fields of type char. This has been fixed in 145// GCC 4.4 but the change can lead to differences in the structure layout. 146// See the documentation of -Wpacked-bitfield-compat for more information. 147struct packed_chars { 148 char a:4; 149 char b:8 __attribute__ ((packed)); 150 // expected-warning@-1 {{'packed' attribute was ignored on bit-fields with single-byte alignment in older versions of GCC and Clang}} 151 char c:4; 152}; 153 154#if defined(_WIN32) && !defined(__declspec) // _MSC_VER is unavailable in cc1. 155// On Windows clang uses MSVC compatible layout in this case. 156extern int o1[sizeof(struct packed_chars) == 3 ? 1 : -1]; 157extern int o2[__alignof(struct packed_chars) == 1 ? 1 : -1]; 158#else 159extern int o1[sizeof(struct packed_chars) == 2 ? 1 : -1]; 160extern int o2[__alignof(struct packed_chars) == 1 ? 1 : -1]; 161#endif 162