[llvm] [YAML] Make `std::array` available (PR #116059)
NAKAMURA Takumi via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 13 18:20:30 PST 2024
https://github.com/chapuni updated https://github.com/llvm/llvm-project/pull/116059
>From f71a854f74fe69c5d8cbb1915e6acd660fcb79d7 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 13 Nov 2024 22:55:31 +0900
Subject: [PATCH 1/2] Reformat
---
llvm/unittests/Support/YAMLIOTest.cpp | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index e10fe099a30adb..2d76b1509840f1 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -3368,15 +3368,14 @@ struct FixedArray {
namespace llvm {
namespace yaml {
- template <>
- struct MappingTraits<FixedArray> {
- static void mapping(IO &io, FixedArray& st) {
- MutableArrayRef<int> array = st.values;
- io.mapRequired("Values", array);
- }
- };
-}
-}
+template <> struct MappingTraits<FixedArray> {
+ static void mapping(IO &io, FixedArray &st) {
+ MutableArrayRef<int> array = st.values;
+ io.mapRequired("Values", array);
+ }
+};
+} // namespace yaml
+} // namespace llvm
TEST(YAMLIO, FixedSizeArray) {
FixedArray faval;
@@ -3423,5 +3422,4 @@ TEST(YAMLIO, FixedSizeArrayMismatch) {
// Error for too many elements.
EXPECT_TRUE(!!yin.error());
}
-
}
>From 9bb3a411692a7a09c06191e18923d75d544dc93e Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi <geek4civic at gmail.com>
Date: Wed, 13 Nov 2024 22:55:41 +0900
Subject: [PATCH 2/2] YAMLTraits.h: std::array
---
llvm/include/llvm/Support/YAMLTraits.h | 5 +++++
llvm/unittests/Support/YAMLIOTest.cpp | 29 +++++++++++++++++++++-----
2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 1d04783753d5cd..45d82f3ebefa9c 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -2006,6 +2006,11 @@ struct SequenceTraits<
std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
: SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
template <typename T, unsigned N>
+struct SequenceTraits<
+ std::array<T, N>,
+ std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
+ : SequenceTraitsImpl<std::array<T, N>, SequenceElementTraits<T>::flow> {};
+template <typename T, unsigned N>
struct SequenceTraits<
SmallVector<T, N>,
std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index 2d76b1509840f1..a7d1b338719f3b 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -3366,6 +3366,15 @@ struct FixedArray {
int values[4];
};
+struct StdArray {
+ StdArray() {
+ // Initialize to int max as a sentinel value.
+ for (auto &v : values)
+ v = std::numeric_limits<int>::max();
+ }
+ std::array<int, 4> values;
+};
+
namespace llvm {
namespace yaml {
template <> struct MappingTraits<FixedArray> {
@@ -3374,11 +3383,21 @@ template <> struct MappingTraits<FixedArray> {
io.mapRequired("Values", array);
}
};
+template <> struct MappingTraits<StdArray> {
+ static void mapping(IO &io, StdArray &st) {
+ io.mapRequired("Values", st.values);
+ }
+};
} // namespace yaml
} // namespace llvm
-TEST(YAMLIO, FixedSizeArray) {
- FixedArray faval;
+using TestTypes = ::testing::Types<FixedArray, StdArray>;
+
+template <typename T> class YAMLIO : public testing::Test {};
+TYPED_TEST_SUITE(YAMLIO, TestTypes, );
+
+TYPED_TEST(YAMLIO, FixedSizeArray) {
+ TypeParam faval;
Input yin("---\nValues: [ 1, 2, 3, 4 ]\n...\n");
yin >> faval;
@@ -3400,9 +3419,9 @@ TEST(YAMLIO, FixedSizeArray) {
ASSERT_EQ(serialized, expected);
}
-TEST(YAMLIO, FixedSizeArrayMismatch) {
+TYPED_TEST(YAMLIO, FixedSizeArrayMismatch) {
{
- FixedArray faval;
+ TypeParam faval;
Input yin("---\nValues: [ 1, 2, 3 ]\n...\n");
yin >> faval;
@@ -3415,7 +3434,7 @@ TEST(YAMLIO, FixedSizeArrayMismatch) {
}
{
- FixedArray faval;
+ TypeParam faval;
Input yin("---\nValues: [ 1, 2, 3, 4, 5 ]\n...\n");
yin >> faval;
More information about the llvm-commits
mailing list