[PATCH] YAML: Assign a value returned by the default constructor to the value in an optional mapping.

Alex Lorenz arphaman at gmail.com
Wed Jun 17 16:30:19 PDT 2015


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D10492

Files:
  llvm/trunk/include/llvm/Support/YAMLTraits.h
  llvm/trunk/unittests/Support/YAMLIOTest.cpp

Index: llvm/trunk/unittests/Support/YAMLIOTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp
@@ -68,6 +68,21 @@
 }
 }
 
+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, 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);
Index: llvm/trunk/include/llvm/Support/YAMLTraits.h
===================================================================
--- llvm/trunk/include/llvm/Support/YAMLTraits.h
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h
@@ -647,6 +647,8 @@
     if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
       yamlize(*this, Val, Required);
       this->postflightKey(SaveInfo);
+    } else if (UseDefault) {
+      Val = T();
     }
   }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10492.27892.patch
Type: text/x-patch
Size: 1422 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150617/8518ae04/attachment.bin>


More information about the llvm-commits mailing list