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