PR26547: alignof should return ABI alignment, not preferred alignment

Summary:
- Add `UETT_PreferredAlignOf` to account for the difference between `__alignof` and `alignof`
- `AlignOfType` now returns ABI alignment instead of preferred alignment iff clang-abi-compat > 7, and one uses _Alignof or alignof

Patch by Nicole Mazzuca!

Differential Revision: https://reviews.llvm.org/D53207

llvm-svn: 345419
diff --git a/clang/test/Headers/thumbv7-apple-ios-types.cpp b/clang/test/Headers/thumbv7-apple-ios-types.cpp
index e0f77c3..6f8a365 100644
--- a/clang/test/Headers/thumbv7-apple-ios-types.cpp
+++ b/clang/test/Headers/thumbv7-apple-ios-types.cpp
@@ -45,11 +45,11 @@
 static_assert(check_type<long, 4, 4>::value, "long is wrong");
 static_assert(check_type<unsigned long, 4, 4>::value, "unsigned long is wrong");
 
-static_assert(check_type<long long, 8, 8>::value, "long long is wrong");
-static_assert(check_type<unsigned long long, 8, 8>::value, "unsigned long long is wrong");
+static_assert(check_type<long long, 4, 8>::value, "long long is wrong");
+static_assert(check_type<unsigned long long, 4, 8>::value, "unsigned long long is wrong");
 
 static_assert(check_type<float, 4, 4>::value, "float is wrong");
-static_assert(check_type<double, 8, 8>::value, "double is wrong");
+static_assert(check_type<double, 4, 8>::value, "double is wrong");
 static_assert(check_type<long double, 4, 8>::value, "long double is wrong");
 
 static_assert(check_type<void *, 4, 4>::value, "'void *' is wrong");
diff --git a/clang/test/Sema/align-x86-abi7.c b/clang/test/Sema/align-x86-abi7.c
new file mode 100644
index 0000000..49ca66f
--- /dev/null
+++ b/clang/test/Sema/align-x86-abi7.c
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -std=c11 -triple i386-apple-darwin9 -fsyntax-only -verify -fclang-abi-compat=7 %s
+// expected-no-diagnostics
+
+#define STATIC_ASSERT(cond) _Static_assert(cond, #cond)
+
+// PR3433
+#define CHECK_ALIGNMENT(type, name, pref) \
+  type name; \
+  STATIC_ASSERT(__alignof__(name) == pref); \
+  STATIC_ASSERT(__alignof__(type) == pref); \
+  STATIC_ASSERT(_Alignof(type) == pref)
+
+CHECK_ALIGNMENT(double, g_double, 8);
+CHECK_ALIGNMENT(long long, g_longlong, 8);
+CHECK_ALIGNMENT(unsigned long long, g_ulonglong, 8);
+
+typedef double arr3double[3];
+CHECK_ALIGNMENT(arr3double, g_arr3double, 8);
+
+enum big_enum { x = 18446744073709551615ULL };
+CHECK_ALIGNMENT(enum big_enum, g_bigenum, 8);
\ No newline at end of file
diff --git a/clang/test/Sema/align-x86.c b/clang/test/Sema/align-x86.c
index e3b8c70..519cbe6 100644
--- a/clang/test/Sema/align-x86.c
+++ b/clang/test/Sema/align-x86.c
@@ -1,34 +1,33 @@
-// RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c11 -triple i386-apple-darwin9 -fsyntax-only -verify %s
 // expected-no-diagnostics
 
+#define STATIC_ASSERT(cond) _Static_assert(cond, #cond)
+
 // PR3433
-double g1;
-short chk1[__alignof__(g1) == 8 ? 1 : -1]; 
-short chk2[__alignof__(double) == 8 ? 1 : -1];
+#define CHECK_ALIGNMENT(type, name, abi, pref) \
+  type name; \
+  STATIC_ASSERT(__alignof__(name) == pref); \
+  STATIC_ASSERT(__alignof__(type) == pref); \
+  STATIC_ASSERT(_Alignof(type) == abi)
 
-long long g2;
-short chk1[__alignof__(g2) == 8 ? 1 : -1]; 
-short chk2[__alignof__(long long) == 8 ? 1 : -1];
-
-unsigned long long g5;
-short chk1[__alignof__(g5) == 8 ? 1 : -1]; 
-short chk2[__alignof__(unsigned long long) == 8 ? 1 : -1];
-
-_Complex double g3;
-short chk1[__alignof__(g3) == 8 ? 1 : -1]; 
-short chk2[__alignof__(_Complex double) == 8 ? 1 : -1];
+CHECK_ALIGNMENT(double, g_double, 4, 8);
+CHECK_ALIGNMENT(long long, g_longlong, 4, 8);
+CHECK_ALIGNMENT(unsigned long long, g_ulonglong, 4, 8);
+CHECK_ALIGNMENT(_Complex double, g_complexdouble, 4, 8);
 
 // PR6362
-struct __attribute__((packed)) {unsigned int a;} g4;
-short chk1[__alignof__(g4) == 1 ? 1 : -1];
-short chk2[__alignof__(g4.a) == 1 ? 1 : -1];
+struct __attribute__((packed))
+packed_struct {
+  unsigned int a;
+};
+CHECK_ALIGNMENT(struct packed_struct, g_packedstruct, 1, 1);
+STATIC_ASSERT(__alignof__(g_packedstruct.a) == 1);
 
-double g6[3];
-short chk1[__alignof__(g6) == 8 ? 1 : -1];
-short chk2[__alignof__(double[3]) == 8 ? 1 : -1];
+typedef double arr3double[3];
+CHECK_ALIGNMENT(arr3double, g_arr3double, 4, 8);
 
-enum { x = 18446744073709551615ULL } g7;
-short chk1[__alignof__(g7) == 8 ? 1 : -1];
+enum big_enum { x = 18446744073709551615ULL };
+CHECK_ALIGNMENT(enum big_enum, g_bigenum, 4, 8);
 
 // PR5637
 
@@ -36,20 +35,20 @@
 
 typedef ALIGNED(2) struct {
   char a[3];
-} T;
+} aligned_before_struct;
 
-short chk1[sizeof(T)       == 3 ? 1 : -1];
-short chk2[sizeof(T[1])    == 4 ? 1 : -1];
-short chk3[sizeof(T[2])    == 6 ? 1 : -1];
-short chk4[sizeof(T[2][1]) == 8 ? 1 : -1];
-short chk5[sizeof(T[1][2]) == 6 ? 1 : -1];
+STATIC_ASSERT(sizeof(aligned_before_struct)       == 3);
+STATIC_ASSERT(sizeof(aligned_before_struct[1])    == 4);
+STATIC_ASSERT(sizeof(aligned_before_struct[2])    == 6);
+STATIC_ASSERT(sizeof(aligned_before_struct[2][1]) == 8);
+STATIC_ASSERT(sizeof(aligned_before_struct[1][2]) == 6);
 
 typedef struct ALIGNED(2) {
   char a[3];
-} T2;
+} aligned_after_struct;
 
-short chk1[sizeof(T2)       == 4 ? 1 : -1];
-short chk2[sizeof(T2[1])    == 4 ? 1 : -1];
-short chk3[sizeof(T2[2])    == 8 ? 1 : -1];
-short chk4[sizeof(T2[2][1]) == 8 ? 1 : -1];
-short chk5[sizeof(T2[1][2]) == 8 ? 1 : -1];
+STATIC_ASSERT(sizeof(aligned_after_struct)       == 4);
+STATIC_ASSERT(sizeof(aligned_after_struct[1])    == 4);
+STATIC_ASSERT(sizeof(aligned_after_struct[2])    == 8);
+STATIC_ASSERT(sizeof(aligned_after_struct[2][1]) == 8);
+STATIC_ASSERT(sizeof(aligned_after_struct[1][2]) == 8);
diff --git a/clang/test/SemaCXX/align-x86-abi7.cpp b/clang/test/SemaCXX/align-x86-abi7.cpp
new file mode 100644
index 0000000..3088a13
--- /dev/null
+++ b/clang/test/SemaCXX/align-x86-abi7.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -std=c++11 -triple i386-apple-darwin9 -fsyntax-only -verify -fclang-abi-compat=7 %s
+// expected-no-diagnostics
+
+using size_t = decltype(sizeof(0));
+
+template <typename T, size_t Preferred>
+struct check_alignment {
+  using type = T;
+  static type value;
+
+  static_assert(__alignof__(value) == Preferred, "__alignof__(value) != Preferred");
+  static_assert(__alignof__(type) == Preferred, "__alignof__(type) != Preferred");
+  static_assert(alignof(type) == Preferred, "alignof(type) != Preferred");
+};
+
+// PR3433
+template struct check_alignment<double, 8>;
+template struct check_alignment<long long, 8>;
+template struct check_alignment<unsigned long long, 8>;
+
+// PR6362
+template struct check_alignment<double[3], 8>;
+
+enum big_enum { x = 18446744073709551615ULL };
+template struct check_alignment<big_enum, 8>;
diff --git a/clang/test/SemaCXX/align-x86.cpp b/clang/test/SemaCXX/align-x86.cpp
new file mode 100644
index 0000000..0c97fc2
--- /dev/null
+++ b/clang/test/SemaCXX/align-x86.cpp
@@ -0,0 +1,62 @@
+// RUN: %clang_cc1 -std=c++11 -triple i386-apple-darwin9 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+using size_t = decltype(sizeof(0));
+
+struct complex_double {
+  double real;
+  double imag;
+};
+
+template <typename T, size_t ABI, size_t Preferred>
+struct check_alignment {
+  using type = T;
+  static type value;
+
+  static_assert(__alignof__(value) == Preferred, "__alignof__(value) != Preferred");
+  static_assert(__alignof__(type) == Preferred, "__alignof__(type) != Preferred");
+  static_assert(alignof(type) == ABI, "alignof(type) != ABI");
+};
+
+// PR3433
+template struct check_alignment<double, 4, 8>;
+template struct check_alignment<long long, 4, 8>;
+template struct check_alignment<unsigned long long, 4, 8>;
+template struct check_alignment<complex_double, 4, 4>;
+
+// PR6362
+struct __attribute__((packed))
+packed_struct {
+  unsigned int a;
+} g_packedstruct;
+template struct check_alignment<packed_struct, 1, 1>;
+static_assert(__alignof__(g_packedstruct.a) == 1, "__alignof__(packed_struct.member) != 1");
+
+template struct check_alignment<double[3], 4, 8>;
+
+enum big_enum { x = 18446744073709551615ULL };
+template struct check_alignment<big_enum, 4, 8>;
+
+// PR5637
+
+#define ALIGNED(x) __attribute__((aligned(x)))
+
+typedef ALIGNED(2) struct {
+  char a[3];
+} aligned_before_struct;
+
+static_assert(sizeof(aligned_before_struct)       == 3, "");
+static_assert(sizeof(aligned_before_struct[1])    == 4, "");
+static_assert(sizeof(aligned_before_struct[2])    == 6, "");
+static_assert(sizeof(aligned_before_struct[2][1]) == 8, "");
+static_assert(sizeof(aligned_before_struct[1][2]) == 6, "");
+
+typedef struct ALIGNED(2) {
+  char a[3];
+} aligned_after_struct;
+
+static_assert(sizeof(aligned_after_struct)       == 4, "");
+static_assert(sizeof(aligned_after_struct[1])    == 4, "");
+static_assert(sizeof(aligned_after_struct[2])    == 8, "");
+static_assert(sizeof(aligned_after_struct[2][1]) == 8, "");
+static_assert(sizeof(aligned_after_struct[1][2]) == 8, "");
diff --git a/clang/test/SemaCXX/alignof.cpp b/clang/test/SemaCXX/alignof.cpp
index e3690ea..90526c0 100644
--- a/clang/test/SemaCXX/alignof.cpp
+++ b/clang/test/SemaCXX/alignof.cpp
@@ -4,14 +4,14 @@
 
 struct S0 {
   int x;
-  static const int test0 = __alignof__(x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
-  static const int test1 = __alignof__(S0::x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
-  auto test2() -> char(&)[__alignof__(x)]; // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
+  static const int test0 = __alignof__(x); // expected-error {{invalid application of '__alignof' to a field of a class still being defined}}
+  static const int test1 = __alignof__(S0::x); // expected-error {{invalid application of '__alignof' to a field of a class still being defined}}
+  auto test2() -> char(&)[__alignof__(x)]; // expected-error {{invalid application of '__alignof' to a field of a class still being defined}}
 };
 
 struct S1; // expected-note 6 {{forward declaration}}
 extern S1 s1;
-const int test3 = __alignof__(s1); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
+const int test3 = __alignof__(s1); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
 
 struct S2 {
   S2();
@@ -19,11 +19,11 @@
   int x;
 
   int test4 = __alignof__(x); // ok
-  int test5 = __alignof__(s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
+  int test5 = __alignof__(s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
 };
 
 const int test6 = __alignof__(S2::x);
-const int test7 = __alignof__(S2::s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
+const int test7 = __alignof__(S2::s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
 
 // Arguably, these should fail like the S1 cases do: the alignment of
 // 's2.x' should depend on the alignment of both x-within-S2 and
@@ -34,10 +34,10 @@
   S2 s2;
 
   static const int test8 = __alignof__(s2.x);
-  static const int test9 = __alignof__(s2.s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
+  static const int test9 = __alignof__(s2.s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
   auto test10() -> char(&)[__alignof__(s2.x)];
   static const int test11 = __alignof__(S3::s2.x);
-  static const int test12 = __alignof__(S3::s2.s); // expected-error {{invalid application of 'alignof' to an incomplete type 'S1'}}
+  static const int test12 = __alignof__(S3::s2.s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
   auto test13() -> char(&)[__alignof__(s2.x)];
 };
 
@@ -59,9 +59,9 @@
 };
 const int test8 = __alignof__(S5::x);
 
-long long int test14[2];
+int test14[2];
 
-static_assert(alignof(test14) == 8, "foo"); // expected-warning {{'alignof' applied to an expression is a GNU extension}}
+static_assert(alignof(test14) == 4, "foo"); // expected-warning {{'alignof' applied to an expression is a GNU extension}}
 
 // PR19992
 static_assert(alignof(int[]) == alignof(int), ""); // ok