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

Alex L arphaman at gmail.com
Wed Jun 17 18:07:18 PDT 2015


So apparently clang-format uses the current behavior where the optionals
aren't initialized if they're missing to parse the formatting styles that
are based on other styles.

I think it would be easier to just change the documentation for YAML to say
that it doesn't initialize the optional values that don't have a default
value if they are missing in the mapping.

Thoughts?
Alex

2015-06-17 14:45 GMT-07:00 Duncan P. N. Exon Smith <dexonsmith at apple.com>:

>
> > On 2015 Jun 16, at 16:37, Alex Lorenz <arphaman at gmail.com> wrote:
> >
> > Hi dexonsmith, bob.wilson, bogner,
> >
> > This patch 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
> when the appropriate key is not present in the YAML mapping.
> >
> > The current YAML documentation specifies that this should happen:
> http://llvm.org/docs/YamlIO.html#default-values
>
> LGTM.
>
> >
> > REPOSITORY
> >  rL LLVM
> >
> > http://reviews.llvm.org/D10492
> >
> > Files:
> >  include/llvm/Support/YAMLTraits.h
> >  unittests/Support/YAMLIOTest.cpp
> >
> > Index: include/llvm/Support/YAMLTraits.h
> > ===================================================================
> > --- include/llvm/Support/YAMLTraits.h
> > +++ 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();
> >     }
> >   }
> >
> > Index: unittests/Support/YAMLIOTest.cpp
> > ===================================================================
> > --- unittests/Support/YAMLIOTest.cpp
> > +++ 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);
> >
> > EMAIL PREFERENCES
> >  http://reviews.llvm.org/settings/panel/emailpreferences/
> > <D10492.27793.patch>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150617/75f7ed71/attachment.html>


More information about the llvm-commits mailing list