This is an archive of the discontinued LLVM Phabricator instance.

[YAML I/O] Fix bug in emission of empty sequence
ClosedPublic

Authored by JDevlieghere on Jan 22 2021, 5:36 PM.

Details

Summary

Don't emit an output dash for an empty sequence.

Take emitting a vector of strings for example:

std::vector<std::string> Strings = {"foo", "bar"};
LLVM_YAML_IS_SEQUENCE_VECTOR(std::string)
yout << Strings;

This emits the following YAML document.

---
- foo
- bar
...

However, when the vector is empty, the result is valid YAML, but is now a sequence with one element consisting of an empty list.

---
- []
...

If we were to try to read this into a vector of strings again, it would fail.

YAML:2:4: error: not a mapping
- []

The problem is the output dash before the empty list. The correct output would be:

---
[]
...

This patch fixes that. I'm not too familiar with the YAML I/O implementation beyond the traits themselves, so I'm open to suggestions if you know a better way to fix this.

Diff Detail

Event Timeline

JDevlieghere created this revision.Jan 22 2021, 5:36 PM
JDevlieghere requested review of this revision.Jan 22 2021, 5:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2021, 5:36 PM
JDevlieghere edited the summary of this revision. (Show Details)Jan 22 2021, 5:39 PM
thegameg accepted this revision.Jan 22 2021, 6:03 PM

LGTM. Not sure if there's a cleaner way.

This revision is now accepted and ready to land.Jan 22 2021, 6:03 PM
This revision was automatically updated to reflect the committed changes.