mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-11-23 16:19:52 +00:00
YAML: Assign a value returned by the default constructor to the value in an optional mapping.
This commit ensures that a value that's passed into YAML's IO mapOptional method is going to be assigned a value returned by the default constructor for that value's type when the appropriate key is not present in the YAML mapping. Reviewers: Duncan P. N. Exon Smith Differential Revision: http://reviews.llvm.org/D10492 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239972 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -647,6 +647,8 @@ private:
|
|||||||
if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
|
if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
|
||||||
yamlize(*this, Val, Required);
|
yamlize(*this, Val, Required);
|
||||||
this->postflightKey(SaveInfo);
|
this->postflightKey(SaveInfo);
|
||||||
|
} else if (UseDefault) {
|
||||||
|
Val = T();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,21 @@ namespace yaml {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct FooBarOptional {
|
||||||
|
int Foo;
|
||||||
|
int Bar;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
namespace yaml {
|
||||||
|
template <> struct MappingTraits<FooBarOptional> {
|
||||||
|
static void mapping(IO &YamlIO, FooBarOptional &Obj) {
|
||||||
|
YamlIO.mapRequired("foo", Obj.Foo);
|
||||||
|
YamlIO.mapOptional("bar", Obj.Bar);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Test the reading of a yaml mapping
|
// Test the reading of a yaml mapping
|
||||||
@@ -93,6 +108,19 @@ TEST(YAMLIO, TestMapRead) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(YAMLIO, TestMapReadOptional) {
|
||||||
|
FooBarOptional Doc;
|
||||||
|
Doc.Bar = 42;
|
||||||
|
{
|
||||||
|
Input In("---\nfoo: 3\n...\n");
|
||||||
|
In >> Doc;
|
||||||
|
|
||||||
|
EXPECT_FALSE(In.error());
|
||||||
|
EXPECT_EQ(Doc.Foo, 3);
|
||||||
|
EXPECT_EQ(Doc.Bar, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST(YAMLIO, TestMalformedMapRead) {
|
TEST(YAMLIO, TestMalformedMapRead) {
|
||||||
FooBar doc;
|
FooBar doc;
|
||||||
Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
|
Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
|
||||||
|
|||||||
Reference in New Issue
Block a user