Clang Project

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