aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2019-01-16 01:51:12 +0000
committerEric Fiselier <eric@efcs.ca>2019-01-16 01:51:12 +0000
commitd108bf85b09e3f1355fea866d90e3dd2266d67b5 (patch)
treebedceef58ed8c43730f187f8adfffc4fcf38881b /libcxx/test
parentac855d3ea9cb53f8c34422f3ce43ac41113f6b7f (diff)
Move internal usages of `alignof`/`__alignof` to use `_LIBCPP_ALIGNOF`.
Summary: Starting in Clang 8.0 and GCC 8.0, `alignof` and `__alignof` return different values in same cases. Specifically `alignof` and `_Alignof` return the minimum alignment for a type, where as `__alignof` returns the preferred alignment. libc++ currently uses `__alignof` but means to use `alignof`. See llvm.org/PR39713 This patch introduces the macro `_LIBCPP_ALIGNOF` so we can control which spelling gets used. This patch does not introduce any ABI guard to provide the old behavior with newer compilers. However, if we decide that is needed, this patch makes it trivial to implement. I think we should commit this change immediately, and decide what we want to do about the ABI afterwards. Reviewers: ldionne, EricWF Reviewed By: ldionne, EricWF Subscribers: jyknight, christof, libcxx-commits Differential Revision: https://reviews.llvm.org/D54814 llvm-svn: 351289
Diffstat (limited to 'libcxx/test')
-rw-r--r--libcxx/test/libcxx/libcpp_alignof.pass.cpp37
-rw-r--r--libcxx/test/std/containers/sequences/array/size_and_alignment.pass.cpp2
-rw-r--r--libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp9
-rw-r--r--libcxx/test/std/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp8
-rw-r--r--libcxx/test/support/test_macros.h6
5 files changed, 52 insertions, 10 deletions
diff --git a/libcxx/test/libcxx/libcpp_alignof.pass.cpp b/libcxx/test/libcxx/libcpp_alignof.pass.cpp
new file mode 100644
index 000000000000..ba0df807e82d
--- /dev/null
+++ b/libcxx/test/libcxx/libcpp_alignof.pass.cpp
@@ -0,0 +1,37 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// Test that _LIBCPP_ALIGNOF acts the same as the C++11 keyword `alignof`, and
+// not as the GNU extension `__alignof`. The former returns the minimal required
+// alignment for a type, whereas the latter returns the preferred alignment.
+//
+// See llvm.org/PR39713
+
+#include <type_traits>
+#include "test_macros.h"
+
+template <class T>
+void test() {
+ static_assert(_LIBCPP_ALIGNOF(T) == std::alignment_of<T>::value, "");
+ static_assert(_LIBCPP_ALIGNOF(T) == TEST_ALIGNOF(T), "");
+#if TEST_STD_VER >= 11
+ static_assert(_LIBCPP_ALIGNOF(T) == alignof(T), "");
+#endif
+#ifdef TEST_COMPILER_CLANG
+ static_assert(_LIBCPP_ALIGNOF(T) == _Alignof(T), "");
+#endif
+}
+
+int main() {
+ test<int>();
+ test<long long>();
+ test<double>();
+ test<long double>();
+}
diff --git a/libcxx/test/std/containers/sequences/array/size_and_alignment.pass.cpp b/libcxx/test/std/containers/sequences/array/size_and_alignment.pass.cpp
index 966d063fe17a..d73182bd5cac 100644
--- a/libcxx/test/std/containers/sequences/array/size_and_alignment.pass.cpp
+++ b/libcxx/test/std/containers/sequences/array/size_and_alignment.pass.cpp
@@ -58,8 +58,6 @@ struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType2 {
char data[1000];
};
-//static_assert(sizeof(void*) == 4, "");
-
int main() {
test_type<char>();
test_type<int>();
diff --git a/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp b/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp
index d7e35a62f8f0..012741ff6c7d 100644
--- a/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp
+++ b/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/aligned_storage.pass.cpp
@@ -254,9 +254,6 @@ int main()
// Use alignof(std::max_align_t) below to find the max alignment instead of
// hardcoding it, because it's different on different platforms.
// (For example 8 on arm and 16 on x86.)
-#if TEST_STD_VER < 11
-#define alignof __alignof__
-#endif
{
typedef std::aligned_storage<16>::type T1;
#if TEST_STD_VER > 11
@@ -264,7 +261,7 @@ int main()
#endif
static_assert(std::is_trivial<T1>::value, "");
static_assert(std::is_standard_layout<T1>::value, "");
- static_assert(std::alignment_of<T1>::value == alignof(std::max_align_t),
+ static_assert(std::alignment_of<T1>::value == TEST_ALIGNOF(std::max_align_t),
"");
static_assert(sizeof(T1) == 16, "");
}
@@ -275,9 +272,9 @@ int main()
#endif
static_assert(std::is_trivial<T1>::value, "");
static_assert(std::is_standard_layout<T1>::value, "");
- static_assert(std::alignment_of<T1>::value == alignof(std::max_align_t),
+ static_assert(std::alignment_of<T1>::value == TEST_ALIGNOF(std::max_align_t),
"");
- static_assert(sizeof(T1) == 16 + alignof(std::max_align_t), "");
+ static_assert(sizeof(T1) == 16 + TEST_ALIGNOF(std::max_align_t), "");
}
{
typedef std::aligned_storage<10>::type T1;
diff --git a/libcxx/test/std/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp b/libcxx/test/std/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp
index 0f55db64719b..bd02da96978d 100644
--- a/libcxx/test/std/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp
+++ b/libcxx/test/std/utilities/meta/meta.unary.prop.query/alignment_of.pass.cpp
@@ -19,6 +19,9 @@
template <class T, unsigned A>
void test_alignment_of()
{
+ const unsigned AlignofResult = TEST_ALIGNOF(T);
+ static_assert( AlignofResult == A, "Golden value does not match result of alignof keyword");
+ static_assert( std::alignment_of<T>::value == AlignofResult, "");
static_assert( std::alignment_of<T>::value == A, "");
static_assert( std::alignment_of<const T>::value == A, "");
static_assert( std::alignment_of<volatile T>::value == A, "");
@@ -45,7 +48,10 @@ int main()
test_alignment_of<const int*, sizeof(intptr_t)>();
test_alignment_of<char[3], 1>();
test_alignment_of<int, 4>();
- test_alignment_of<double, 8>();
+ // The test case below is a hack. It's hard to detect what golden value
+ // we should expect. In most cases it should be 8. But in i386 builds
+ // with Clang >= 8 or GCC >= 8 the value is '4'.
+ test_alignment_of<double, TEST_ALIGNOF(double)>();
#if (defined(__ppc__) && !defined(__ppc64__))
test_alignment_of<bool, 4>(); // 32-bit PPC has four byte bool
#else
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index 5af072471228..2813d1ff0685 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -116,7 +116,11 @@
# define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
# endif
#else
-#define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
+#if defined(TEST_COMPILER_CLANG)
+# define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
+#else
+# define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
+#endif
#define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
#define TEST_CONSTEXPR
#define TEST_CONSTEXPR_CXX14