diff options
author | Chris Bieneman <beanz@apple.com> | 2016-06-28 21:10:26 +0000 |
---|---|---|
committer | Chris Bieneman <beanz@apple.com> | 2016-06-28 21:10:26 +0000 |
commit | 92b2e8a2958e22dbec732b34d828ebe6a9f82b59 (patch) | |
tree | 45534734f85c759e4c512c37b679bad823ed177f /llvm/lib/Support/YAMLTraits.cpp | |
parent | 347db3e18ee7375cf038a8173880466fd0fe6312 (diff) | |
download | bcm5719-llvm-92b2e8a2958e22dbec732b34d828ebe6a9f82b59.tar.gz bcm5719-llvm-92b2e8a2958e22dbec732b34d828ebe6a9f82b59.zip |
[YAML] Fix YAML tags appearing before the start of sequence elements
Our existing yaml::Output code writes tags immediately when mapTag is called, without any state handling. This results in tags on sequence elements being written before the element itself. For example, we see this:
SomeArray: !elem_type
- key1: 1
key2: 2 !elem_type2
- key3: 3
key4: 4
We should instead see:
SomeArray:
- !elem_type
key1: 1
key2: 2
- !elem_type2
key3: 3
key4: 4
Our reader handles reading properly, so this bug only impacts writing yaml sequences with tagged elements.
As a test for this I've modified the Mach-O yaml encoding to allways apply the !mach-o tag when encoding MachOYAML::Object entries. This results in the !mach-o tag appearing as expected in dumped fat files.
llvm-svn: 274067
Diffstat (limited to 'llvm/lib/Support/YAMLTraits.cpp')
-rw-r--r-- | llvm/lib/Support/YAMLTraits.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp index 2aa6e9b7468..75fac20a8ed 100644 --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -423,8 +423,29 @@ void Output::beginMapping() { bool Output::mapTag(StringRef Tag, bool Use) { if (Use) { - this->output(" "); + // If this tag is being written inside a sequence we should write the start + // of the sequence before writing the tag, otherwise the tag won't be + // attached to the element in the sequence, but rather the sequence itself. + bool SequenceElement = + StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq || + StateStack[StateStack.size() - 2] == inFlowSeq); + if (SequenceElement && StateStack.back() == inMapFirstKey) { + this->newLineCheck(); + } else { + this->output(" "); + } this->output(Tag); + if (SequenceElement) { + // If we're writing the tag during the first element of a map, the tag + // takes the place of the first element in the sequence. + if (StateStack.back() == inMapFirstKey) { + StateStack.pop_back(); + StateStack.push_back(inMapOtherKey); + } + // Tags inside maps in sequences should act as keys in the map from a + // formatting perspective, so we always want a newline in a sequence. + NeedsNewLine = true; + } } return Use; } |