Skip to content

Commit

Permalink
YAML: Assign a value returned by the default constructor to the value…
Browse files Browse the repository at this point in the history
… 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

llvm-svn: 239972
  • Loading branch information
hyp committed Jun 17, 2015
1 parent 4d04afb commit 376fc70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions llvm/include/llvm/Support/YAMLTraits.h
Expand Up @@ -647,6 +647,8 @@ class IO {
if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
yamlize(*this, Val, Required);
this->postflightKey(SaveInfo);
} else if (UseDefault) {
Val = T();
}
}

Expand Down
28 changes: 28 additions & 0 deletions llvm/unittests/Support/YAMLIOTest.cpp
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 376fc70

Please sign in to comment.