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: EricWF

Subscribers: christof, libcxx-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@347787 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2018-11-28 18:16:02 +00:00
parent 581671fd26
commit 087f065cb0
10 changed files with 91 additions and 44 deletions

View File

@@ -1281,6 +1281,16 @@ _LIBCPP_FUNC_VIS extern "C" void __sanitizer_annotate_contiguous_container(
#define _LIBCPP_HAS_NO_COROUTINES #define _LIBCPP_HAS_NO_COROUTINES
#endif #endif
#ifndef _LIBCPP_CXX03_LANG
# define _LIBCPP_ALIGNOF(_Tp) alignof(_Tp)
#elif defined(_LIBCPP_COMPILER_CLANG)
# define _LIBCPP_ALIGNOF(_Tp) _Alignof(_Tp)
#else
// This definition is potentially buggy, but it's only taken with GCC in C++03,
// which we barely support anyway. See llvm.org/PR39713
# define _LIBCPP_ALIGNOF(_Tp) __alignof(_Tp)
#endif
// FIXME: Correct this macro when either (A) a feature test macro for the // FIXME: Correct this macro when either (A) a feature test macro for the
// spaceship operator is provided, or (B) a compiler provides a complete // spaceship operator is provided, or (B) a compiler provides a complete
// implementation. // implementation.

View File

@@ -55,14 +55,14 @@ public:
__allocated_ = true; __allocated_ = true;
return (pointer)&buf_; return (pointer)&buf_;
} }
return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp))); return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
} }
_LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n)
{ {
if (__p == (pointer)&buf_) if (__p == (pointer)&buf_)
__allocated_ = false; __allocated_ = false;
else else
_VSTD::__libcpp_deallocate(__p, __n * sizeof(_Tp), __alignof(_Tp)); _VSTD::__libcpp_deallocate(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
} }
_LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);} _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}

View File

@@ -214,7 +214,7 @@ public:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
_Promise& promise() const { _Promise& promise() const {
return *static_cast<_Promise*>( return *static_cast<_Promise*>(
__builtin_coro_promise(this->__handle_, __alignof(_Promise), false)); __builtin_coro_promise(this->__handle_, _LIBCPP_ALIGNOF(_Promise), false));
} }
public: public:
@@ -254,7 +254,7 @@ public:
coroutine_handle __tmp; coroutine_handle __tmp;
__tmp.__handle_ = __builtin_coro_promise( __tmp.__handle_ = __builtin_coro_promise(
_VSTD::addressof(const_cast<_RawPromise&>(__promise)), _VSTD::addressof(const_cast<_RawPromise&>(__promise)),
__alignof(_Promise), true); _LIBCPP_ALIGNOF(_Promise), true);
return __tmp; return __tmp;
} }
}; };
@@ -272,7 +272,7 @@ public:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_INLINE_VISIBILITY
_Promise& promise() const { _Promise& promise() const {
return *static_cast<_Promise*>( return *static_cast<_Promise*>(
__builtin_coro_promise(this->__handle_, __alignof(_Promise), false)); __builtin_coro_promise(this->__handle_, _LIBCPP_ALIGNOF(_Promise), false));
} }
_LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return true; } _LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return true; }

View File

@@ -1797,10 +1797,10 @@ public:
if (__n > max_size()) if (__n > max_size())
__throw_length_error("allocator<T>::allocate(size_t n)" __throw_length_error("allocator<T>::allocate(size_t n)"
" 'n' exceeds maximum supported size"); " 'n' exceeds maximum supported size");
return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp))); return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
} }
_LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
{_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), __alignof(_Tp));} {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
_LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
{return size_type(~0) / sizeof(_Tp);} {return size_type(~0) / sizeof(_Tp);}
#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
@@ -1898,10 +1898,10 @@ public:
if (__n > max_size()) if (__n > max_size())
__throw_length_error("allocator<const T>::allocate(size_t n)" __throw_length_error("allocator<const T>::allocate(size_t n)"
" 'n' exceeds maximum supported size"); " 'n' exceeds maximum supported size");
return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), __alignof(_Tp))); return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
} }
_LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
{_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), __alignof(_Tp));} {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
_LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
{return size_type(~0) / sizeof(_Tp);} {return size_type(~0) / sizeof(_Tp);}
#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
@@ -2017,7 +2017,7 @@ get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
while (__n > 0) while (__n > 0)
{ {
#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
if (__is_overaligned_for_new(__alignof(_Tp))) if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
{ {
std::align_val_t __al = std::align_val_t __al =
std::align_val_t(std::alignment_of<_Tp>::value); std::align_val_t(std::alignment_of<_Tp>::value);
@@ -2028,7 +2028,7 @@ get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
__n * sizeof(_Tp), nothrow)); __n * sizeof(_Tp), nothrow));
} }
#else #else
if (__is_overaligned_for_new(__alignof(_Tp))) if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
{ {
// Since aligned operator new is unavailable, return an empty // Since aligned operator new is unavailable, return an empty
// buffer rather than one with invalid alignment. // buffer rather than one with invalid alignment.
@@ -2052,7 +2052,7 @@ template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
void return_temporary_buffer(_Tp* __p) _NOEXCEPT void return_temporary_buffer(_Tp* __p) _NOEXCEPT
{ {
_VSTD::__libcpp_deallocate_unsized((void*)__p, __alignof(_Tp)); _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
} }
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)

View File

@@ -1649,7 +1649,7 @@ _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool has_unique_object_representations_v
// alignment_of // alignment_of
template <class _Tp> struct _LIBCPP_TEMPLATE_VIS alignment_of template <class _Tp> struct _LIBCPP_TEMPLATE_VIS alignment_of
: public integral_constant<size_t, __alignof__(_Tp)> {}; : public integral_constant<size_t, _LIBCPP_ALIGNOF(_Tp)> {};
#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES) #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
template <class _Tp> template <class _Tp>
@@ -1812,8 +1812,8 @@ struct __static_max<_I0, _I1, _In...>
template <size_t _Len, class _Type0, class ..._Types> template <size_t _Len, class _Type0, class ..._Types>
struct aligned_union struct aligned_union
{ {
static const size_t alignment_value = __static_max<__alignof__(_Type0), static const size_t alignment_value = __static_max<_LIBCPP_ALIGNOF(_Type0),
__alignof__(_Types)...>::value; _LIBCPP_ALIGNOF(_Types)...>::value;
static const size_t __len = __static_max<_Len, sizeof(_Type0), static const size_t __len = __static_max<_Len, sizeof(_Type0),
sizeof(_Types)...>::value; sizeof(_Types)...>::value;
typedef typename aligned_storage<__len, alignment_value>::type type; typedef typename aligned_storage<__len, alignment_value>::type type;

View File

@@ -2740,7 +2740,7 @@ __val_expr<_ValExpr>::operator valarray<__val_expr::result_type>() const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<result_type*>( static_cast<result_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(result_type), __alignof(result_type))); _VSTD::__libcpp_allocate(__n * sizeof(result_type), _LIBCPP_ALIGNOF(result_type)));
for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i) for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i)
::new (__r.__end_) result_type(__expr_[__i]); ::new (__r.__end_) result_type(__expr_[__i]);
} }
@@ -2758,7 +2758,7 @@ valarray<_Tp>::valarray(size_t __n)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2793,7 +2793,7 @@ valarray<_Tp>::valarray(const value_type* __p, size_t __n)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2819,7 +2819,7 @@ valarray<_Tp>::valarray(const valarray& __v)
if (__v.size()) if (__v.size())
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__v.size() * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__v.size() * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2857,7 +2857,7 @@ valarray<_Tp>::valarray(initializer_list<value_type> __il)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2887,7 +2887,7 @@ valarray<_Tp>::valarray(const slice_array<value_type>& __sa)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2915,7 +2915,7 @@ valarray<_Tp>::valarray(const gslice_array<value_type>& __ga)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2945,7 +2945,7 @@ valarray<_Tp>::valarray(const mask_array<value_type>& __ma)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -2975,7 +2975,7 @@ valarray<_Tp>::valarray(const indirect_array<value_type>& __ia)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@@ -3012,7 +3012,7 @@ valarray<_Tp>::__assign_range(const value_type* __f, const value_type* __l)
{ {
__clear(size()); __clear(size());
__begin_ = static_cast<value_type*>( __begin_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
__end_ = __begin_ + __n; __end_ = __begin_ + __n;
_VSTD::uninitialized_copy(__f, __l, __begin_); _VSTD::uninitialized_copy(__f, __l, __begin_);
} else { } else {
@@ -3268,7 +3268,7 @@ valarray<_Tp>::operator+() const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(+*__p); ::new (__r.__end_) value_type(+*__p);
} }
@@ -3286,7 +3286,7 @@ valarray<_Tp>::operator-() const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(-*__p); ::new (__r.__end_) value_type(-*__p);
} }
@@ -3304,7 +3304,7 @@ valarray<_Tp>::operator~() const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(~*__p); ::new (__r.__end_) value_type(~*__p);
} }
@@ -3321,7 +3321,7 @@ valarray<_Tp>::operator!() const
{ {
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<bool*>(_VSTD::__libcpp_allocate(__n * sizeof(bool), __alignof(bool))); static_cast<bool*>(_VSTD::__libcpp_allocate(__n * sizeof(bool), _LIBCPP_ALIGNOF(bool)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) bool(!*__p); ::new (__r.__end_) bool(!*__p);
} }
@@ -3642,7 +3642,7 @@ valarray<_Tp>::shift(int __i) const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
const value_type* __sb; const value_type* __sb;
value_type* __tb; value_type* __tb;
value_type* __te; value_type* __te;
@@ -3681,7 +3681,7 @@ valarray<_Tp>::cshift(int __i) const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
__i %= static_cast<int>(__n); __i %= static_cast<int>(__n);
const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i; const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i;
for (const value_type* __s = __m; __s != __end_; ++__r.__end_, ++__s) for (const value_type* __s = __m; __s != __end_; ++__r.__end_, ++__s)
@@ -3703,7 +3703,7 @@ valarray<_Tp>::apply(value_type __f(value_type)) const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(__f(*__p)); ::new (__r.__end_) value_type(__f(*__p));
} }
@@ -3721,7 +3721,7 @@ valarray<_Tp>::apply(value_type __f(const value_type&)) const
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ =
static_cast<value_type*>( static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(__f(*__p)); ::new (__r.__end_) value_type(__f(*__p));
} }
@@ -3736,7 +3736,7 @@ void valarray<_Tp>::__clear(size_t __capacity)
{ {
while (__end_ != __begin_) while (__end_ != __begin_)
(--__end_)->~value_type(); (--__end_)->~value_type();
_VSTD::__libcpp_deallocate(__begin_, __capacity * sizeof(value_type), __alignof(value_type)); _VSTD::__libcpp_deallocate(__begin_, __capacity * sizeof(value_type), _LIBCPP_ALIGNOF(value_type));
__begin_ = __end_ = nullptr; __begin_ = __end_ = nullptr;
} }
} }
@@ -3749,7 +3749,7 @@ valarray<_Tp>::resize(size_t __n, value_type __x)
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), __alignof(value_type))); _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {

View File

@@ -0,0 +1,38 @@
// -*- 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), "");
static_assert(alignof(T) == __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>();
}

View File

@@ -58,8 +58,6 @@ struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType2 {
char data[1000]; char data[1000];
}; };
//static_assert(sizeof(void*) == 4, "");
int main() { int main() {
test_type<char>(); test_type<char>();
test_type<int>(); test_type<int>();

View File

@@ -254,9 +254,6 @@ int main()
// Use alignof(std::max_align_t) below to find the max alignment instead of // Use alignof(std::max_align_t) below to find the max alignment instead of
// hardcoding it, because it's different on different platforms. // hardcoding it, because it's different on different platforms.
// (For example 8 on arm and 16 on x86.) // (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; typedef std::aligned_storage<16>::type T1;
#if TEST_STD_VER > 11 #if TEST_STD_VER > 11
@@ -264,7 +261,7 @@ int main()
#endif #endif
static_assert(std::is_trivial<T1>::value, ""); static_assert(std::is_trivial<T1>::value, "");
static_assert(std::is_standard_layout<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, ""); static_assert(sizeof(T1) == 16, "");
} }
@@ -275,9 +272,9 @@ int main()
#endif #endif
static_assert(std::is_trivial<T1>::value, ""); static_assert(std::is_trivial<T1>::value, "");
static_assert(std::is_standard_layout<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; typedef std::aligned_storage<10>::type T1;

View File

@@ -115,7 +115,11 @@
# define TEST_THROW_SPEC(...) throw(__VA_ARGS__) # define TEST_THROW_SPEC(...) throw(__VA_ARGS__)
# endif # endif
#else #else
#if defined(TEST_COMPILER_CLANG)
# define TEST_ALIGNOF(...) _Alignof(__VA_ARGS__)
#else
# define TEST_ALIGNOF(...) __alignof(__VA_ARGS__) # define TEST_ALIGNOF(...) __alignof(__VA_ARGS__)
#endif
#define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__))) #define TEST_ALIGNAS(...) __attribute__((__aligned__(__VA_ARGS__)))
#define TEST_CONSTEXPR #define TEST_CONSTEXPR
#define TEST_CONSTEXPR_CXX14 #define TEST_CONSTEXPR_CXX14