summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/Support/YAMLTraits.h2
-rw-r--r--llvm/unittests/Support/YAMLIOTest.cpp28
2 files changed, 30 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index c04294a5e87..e06830c37cb 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -647,6 +647,8 @@ private:
if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
yamlize(*this, Val, Required);
this->postflightKey(SaveInfo);
+ } else if (UseDefault) {
+ Val = T();
}
}
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index e7affa1698d..0c791b7a020 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -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
@@ -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) {
FooBar doc;
Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages);
OpenPOWER on IntegriCloud