diff options
Diffstat (limited to 'libcxx/test')
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 00000000000..ba0df807e82 --- /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 966d063fe17..d73182bd5ca 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 d7e35a62f8f..012741ff6c7 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 0f55db64719..bd02da96978 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 5af07247122..2813d1ff068 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 |