forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] Fix constexpr initialization of std::array<T, 0> (llvm#74667)
This patch fixes constexpr default initialization of empty arrays and improves the tests accordingly. Fixes llvm#74375
- Loading branch information
Showing
6 changed files
with
187 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
libcxx/test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <array> | ||
|
||
// template <class T, size_t N> | ||
// struct array | ||
|
||
// Make sure std::array<T, N> has the correct object size and alignment. | ||
// This test is mostly meant to catch subtle ABI-breaking regressions. | ||
|
||
// Ignore error about requesting a large alignment not being ABI compatible with older AIX systems. | ||
#if defined(_AIX) | ||
# pragma clang diagnostic ignored "-Waix-compat" | ||
#endif | ||
|
||
#include <array> | ||
#include <cstddef> | ||
#include <type_traits> | ||
#include <__type_traits/datasizeof.h> | ||
|
||
#include "test_macros.h" | ||
|
||
template <class T, std::size_t Size> | ||
struct MyArray { | ||
T elems[Size]; | ||
}; | ||
|
||
template <class T> | ||
void test_type() { | ||
{ | ||
using Array = std::array<T, 0>; | ||
LIBCPP_STATIC_ASSERT(sizeof(Array) == sizeof(T), ""); | ||
LIBCPP_STATIC_ASSERT(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), ""); | ||
LIBCPP_STATIC_ASSERT(sizeof(Array) == sizeof(T[1]), ""); | ||
LIBCPP_STATIC_ASSERT(sizeof(Array) == sizeof(MyArray<T, 1>), ""); | ||
LIBCPP_STATIC_ASSERT(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 1>), ""); | ||
static_assert(!std::is_empty<Array>::value, ""); | ||
|
||
// Make sure empty arrays don't have padding bytes | ||
LIBCPP_STATIC_ASSERT(std::__libcpp_datasizeof<Array>::value == sizeof(Array), ""); | ||
} | ||
|
||
{ | ||
using Array = std::array<T, 1>; | ||
static_assert(sizeof(Array) == sizeof(T), ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), ""); | ||
static_assert(sizeof(Array) == sizeof(T[1]), ""); | ||
static_assert(sizeof(Array) == sizeof(MyArray<T, 1>), ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 1>), ""); | ||
static_assert(!std::is_empty<Array>::value, ""); | ||
} | ||
|
||
{ | ||
using Array = std::array<T, 2>; | ||
static_assert(sizeof(Array) == sizeof(T) * 2, ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), ""); | ||
static_assert(sizeof(Array) == sizeof(T[2]), ""); | ||
static_assert(sizeof(Array) == sizeof(MyArray<T, 2>), ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 2>), ""); | ||
static_assert(!std::is_empty<Array>::value, ""); | ||
} | ||
|
||
{ | ||
using Array = std::array<T, 3>; | ||
static_assert(sizeof(Array) == sizeof(T) * 3, ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), ""); | ||
static_assert(sizeof(Array) == sizeof(T[3]), ""); | ||
static_assert(sizeof(Array) == sizeof(MyArray<T, 3>), ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 3>), ""); | ||
static_assert(!std::is_empty<Array>::value, ""); | ||
} | ||
|
||
{ | ||
using Array = std::array<T, 444>; | ||
static_assert(sizeof(Array) == sizeof(T) * 444, ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), ""); | ||
static_assert(sizeof(Array) == sizeof(T[444]), ""); | ||
static_assert(sizeof(Array) == sizeof(MyArray<T, 444>), ""); | ||
static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 444>), ""); | ||
static_assert(!std::is_empty<Array>::value, ""); | ||
} | ||
} | ||
|
||
struct Empty {}; | ||
|
||
struct Aggregate { | ||
int i; | ||
}; | ||
|
||
struct WithPadding { | ||
long double ld; | ||
char c; | ||
}; | ||
|
||
#if TEST_STD_VER >= 11 | ||
struct alignas(TEST_ALIGNOF(std::max_align_t) * 2) Overaligned1 {}; | ||
|
||
struct alignas(TEST_ALIGNOF(std::max_align_t) * 2) Overaligned2 { | ||
char data[1000]; | ||
}; | ||
|
||
struct alignas(TEST_ALIGNOF(std::max_align_t)) Overaligned3 { | ||
char data[1000]; | ||
}; | ||
|
||
struct alignas(8) Overaligned4 { | ||
char c; | ||
}; | ||
|
||
struct alignas(8) Overaligned5 {}; | ||
#endif | ||
|
||
void test() { | ||
test_type<char>(); | ||
test_type<short>(); | ||
test_type<int>(); | ||
test_type<long>(); | ||
test_type<long long>(); | ||
test_type<float>(); | ||
test_type<double>(); | ||
test_type<long double>(); | ||
test_type<char[1]>(); | ||
test_type<char[2]>(); | ||
test_type<char[3]>(); | ||
test_type<Empty>(); | ||
test_type<Aggregate>(); | ||
test_type<WithPadding>(); | ||
|
||
#if TEST_STD_VER >= 11 | ||
test_type<std::max_align_t>(); | ||
test_type<Overaligned1>(); | ||
test_type<Overaligned2>(); | ||
test_type<Overaligned3>(); | ||
test_type<Overaligned4>(); | ||
test_type<Overaligned5>(); | ||
#endif | ||
} |
78 changes: 0 additions & 78 deletions
78
libcxx/test/std/containers/sequences/array/size_and_alignment.pass.cpp
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters