This is an archive of the discontinued LLVM Phabricator instance.

[YAML] Quote multiline string scalars
ClosedPublic

Authored by ilya-biryukov on May 29 2018, 3:38 AM.

Details

Summary

Otherwise, the YAML parser breaks when trying to read them back in
'key: multiline_string_value' cases.

This patch fixes a problem when serializing structs which contain multi-line strings.
E.g., if we try to serialize the following struct

{ "key1": "first line\nsecond line",
  "key2": "another string" }`

Before this patch, we got the YAML output that failed to parse:

key1: first line
second line
key2: another string

After the patch, we get:

key1: 'first line
second line'
key2: another string

Diff Detail

Repository
rL LLVM

Event Timeline

ilya-biryukov created this revision.
  • Fix formatting
ilya-biryukov edited the summary of this revision. (Show Details)May 29 2018, 4:03 AM

As discussed offline, it'd be nice to assert the actual serialized output in the test. At least in principle we want this to interop with *other* parsers.

include/llvm/Support/YAMLTraits.h
543 ↗(On Diff #148874)

allowed -> allowed in unquoted strings (for clarity)

548 ↗(On Diff #148874)

0xa and 0xd are both printable, so single-quoting should be sufficient

550 ↗(On Diff #148874)

NEL is U+0085, but that won't encode to a 0x85 byte (at least not under UTF-8). So mentioning 0x85 in this switch is a (pre-existing) bug, let's just remove it.

  • Add deserialized output to tests.
ilya-biryukov marked 3 inline comments as done.May 30 2018, 1:35 AM
  • Single-quote strings with \n and \r instead of double-quoting
ilya-biryukov retitled this revision from [YAML] Double-quote multiline string scalars to [YAML] Quote multiline string scalars.May 30 2018, 1:36 AM
ilya-biryukov edited the summary of this revision. (Show Details)
sammccall accepted this revision.May 30 2018, 2:13 AM

Thanks, LG!
comment nit to prove i read it ;)

include/llvm/Support/YAMLTraits.h
546 ↗(On Diff #149057)

this echoes the code a bit. what about:
LF/CR may delimit values and so require at least single quotes.

This revision is now accepted and ready to land.May 30 2018, 2:13 AM
ilya-biryukov marked an inline comment as done.
  • Addressed last nit: update the comment
include/llvm/Support/YAMLTraits.h
546 ↗(On Diff #149057)

Thanks!

This revision was automatically updated to reflect the committed changes.