Skip to content

Commit 980316e

Browse files
committed
[YAML] Recommit "Make std::array available (#116059)" with a fix.
`std::array` will be handled like `MutableArrayRef`; - Extending elements is not acceptable. - For applying fewer sequence, trailing elements will be initialized by default. Not like; - `std::array` is not the reference but holds values. Supposing to hold small count of elements. Changes since llvmorg-20-init-12117-g941f704f0892: - Use `size_t` for `N`, instead of `unsigned`. - include <array>
1 parent 748b028 commit 980316e

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Support/SourceMgr.h"
2424
#include "llvm/Support/YAMLParser.h"
2525
#include "llvm/Support/raw_ostream.h"
26+
#include <array>
2627
#include <cassert>
2728
#include <map>
2829
#include <memory>
@@ -2005,6 +2006,11 @@ struct SequenceTraits<
20052006
std::vector<T>,
20062007
std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
20072008
: SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
2009+
template <typename T, size_t N>
2010+
struct SequenceTraits<
2011+
std::array<T, N>,
2012+
std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
2013+
: SequenceTraitsImpl<std::array<T, N>, SequenceElementTraits<T>::flow> {};
20082014
template <typename T, unsigned N>
20092015
struct SequenceTraits<
20102016
SmallVector<T, N>,

llvm/unittests/Support/YAMLIOTest.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,6 +3366,15 @@ struct FixedArray {
33663366
int values[4];
33673367
};
33683368

3369+
struct StdArray {
3370+
StdArray() {
3371+
// Initialize to int max as a sentinel value.
3372+
for (auto &v : values)
3373+
v = std::numeric_limits<int>::max();
3374+
}
3375+
std::array<int, 4> values;
3376+
};
3377+
33693378
namespace llvm {
33703379
namespace yaml {
33713380
template <> struct MappingTraits<FixedArray> {
@@ -3374,11 +3383,21 @@ template <> struct MappingTraits<FixedArray> {
33743383
io.mapRequired("Values", array);
33753384
}
33763385
};
3386+
template <> struct MappingTraits<StdArray> {
3387+
static void mapping(IO &io, StdArray &st) {
3388+
io.mapRequired("Values", st.values);
3389+
}
3390+
};
33773391
} // namespace yaml
33783392
} // namespace llvm
33793393

3380-
TEST(YAMLIO, FixedSizeArray) {
3381-
FixedArray faval;
3394+
using TestTypes = ::testing::Types<FixedArray, StdArray>;
3395+
3396+
template <typename T> class YAMLIO : public testing::Test {};
3397+
TYPED_TEST_SUITE(YAMLIO, TestTypes, );
3398+
3399+
TYPED_TEST(YAMLIO, FixedSizeArray) {
3400+
TypeParam faval;
33823401
Input yin("---\nValues: [ 1, 2, 3, 4 ]\n...\n");
33833402
yin >> faval;
33843403

@@ -3400,9 +3419,9 @@ TEST(YAMLIO, FixedSizeArray) {
34003419
ASSERT_EQ(serialized, expected);
34013420
}
34023421

3403-
TEST(YAMLIO, FixedSizeArrayMismatch) {
3422+
TYPED_TEST(YAMLIO, FixedSizeArrayMismatch) {
34043423
{
3405-
FixedArray faval;
3424+
TypeParam faval;
34063425
Input yin("---\nValues: [ 1, 2, 3 ]\n...\n");
34073426
yin >> faval;
34083427

@@ -3415,7 +3434,7 @@ TEST(YAMLIO, FixedSizeArrayMismatch) {
34153434
}
34163435

34173436
{
3418-
FixedArray faval;
3437+
TypeParam faval;
34193438
Input yin("---\nValues: [ 1, 2, 3, 4, 5 ]\n...\n");
34203439
yin >> faval;
34213440

0 commit comments

Comments
 (0)