xref: /llvm-project/clang/test/SemaCXX/align-x86.cpp (revision adaf62ced2a106b9f16974f09ef6294583637288)
1 // RUN: %clang_cc1 -std=c++11 -triple i386-apple-darwin9 -fsyntax-only -verify %s
2 
3 using size_t = decltype(sizeof(0));
4 
5 struct complex_double {
6   double real;
7   double imag;
8 };
9 
10 template <typename T, size_t ABI, size_t Preferred>
11 struct check_alignment {
12   using type = T;
13   static type value;
14 
15   static_assert(__alignof__(value) == Preferred, "__alignof__(value) != Preferred");
16   static_assert(__alignof__(type) == Preferred, "__alignof__(type) != Preferred");
17   static_assert(alignof(type) == ABI, "alignof(type) != ABI");
18 };
19 
20 // PR3433
21 template struct check_alignment<double, 4, 8>;
22 template struct check_alignment<long long, 4, 8>;
23 template struct check_alignment<unsigned long long, 4, 8>;
24 template struct check_alignment<complex_double, 4, 4>;
25 
26 // PR6362
27 struct __attribute__((packed))
28 packed_struct {
29   unsigned int a;
30 } g_packedstruct;
31 template struct check_alignment<packed_struct, 1, 1>;
32 static_assert(__alignof__(g_packedstruct.a) == 1, "__alignof__(packed_struct.member) != 1");
33 
34 template struct check_alignment<double[3], 4, 8>;
35 
36 enum big_enum { x = 18446744073709551615ULL };
37 template struct check_alignment<big_enum, 4, 8>;
38 
39 // PR5637
40 
41 #define ALIGNED(x) __attribute__((aligned(x)))
42 
43 typedef ALIGNED(2) struct {
44   char a[3];
45 } aligned_before_struct;
46 
47 static_assert(sizeof(aligned_before_struct)       == 3, "");
48 static_assert(sizeof(aligned_before_struct[1])    == 4, ""); // expected-error {{size of array element}}
49 static_assert(sizeof(aligned_before_struct[2])    == 6, ""); // expected-error {{size of array element}}
50 static_assert(sizeof(aligned_before_struct[2][1]) == 8, ""); // expected-error {{size of array element}}
51 static_assert(sizeof(aligned_before_struct[1][2]) == 6, ""); // expected-error {{size of array element}}
52 
53 typedef struct ALIGNED(2) {
54   char a[3];
55 } aligned_after_struct;
56 
57 static_assert(sizeof(aligned_after_struct)       == 4, "");
58 static_assert(sizeof(aligned_after_struct[1])    == 4, "");
59 static_assert(sizeof(aligned_after_struct[2])    == 8, "");
60 static_assert(sizeof(aligned_after_struct[2][1]) == 8, "");
61 static_assert(sizeof(aligned_after_struct[1][2]) == 8, "");
62