[llvm] 980316e - [YAML] Recommit "Make `std::array` available (#116059)" with a fix.
NAKAMURA Takumi via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 14 03:16:05 PST 2024
Author: NAKAMURA Takumi
Date: 2024-11-14T20:15:46+09:00
New Revision: 980316ec85381f65c369cb650f25881e470857b7
URL: https://github.com/llvm/llvm-project/commit/980316ec85381f65c369cb650f25881e470857b7
DIFF: https://github.com/llvm/llvm-project/commit/980316ec85381f65c369cb650f25881e470857b7.diff
LOG: [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>
Added:
Modified:
llvm/include/llvm/Support/YAMLTraits.h
llvm/unittests/Support/YAMLIOTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 403584e52fed3b..eca26e90845bf6 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -23,6 +23,7 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/raw_ostream.h"
+#include <array>
#include <cassert>
#include <map>
#include <memory>
@@ -2005,6 +2006,11 @@ struct SequenceTraits<
std::vector<T>,
std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
: SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
+template <typename T, size_t 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>,
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