llvm.org GIT mirror llvm / efed5fb
[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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274067 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
3 changed file(s) with 27 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
9090 // For Fat files there will be a different tag so they can be differentiated.
9191 if (!IO.getContext()) {
9292 IO.setContext(&Object);
93 IO.mapTag("!mach-o", true);
9493 }
94 IO.mapTag("!mach-o", true);
9595 IO.mapRequired("FileHeader", Object.Header);
9696 IO.mapOptional("LoadCommands", Object.LoadCommands);
9797 IO.mapOptional("LinkEditData", Object.LinkEdit);
422422
423423 bool Output::mapTag(StringRef Tag, bool Use) {
424424 if (Use) {
425 this->output(" ");
425 // If this tag is being written inside a sequence we should write the start
426 // of the sequence before writing the tag, otherwise the tag won't be
427 // attached to the element in the sequence, but rather the sequence itself.
428 bool SequenceElement =
429 StateStack.size() > 1 && (StateStack[StateStack.size() - 2] == inSeq ||
430 StateStack[StateStack.size() - 2] == inFlowSeq);
431 if (SequenceElement && StateStack.back() == inMapFirstKey) {
432 this->newLineCheck();
433 } else {
434 this->output(" ");
435 }
426436 this->output(Tag);
437 if (SequenceElement) {
438 // If we're writing the tag during the first element of a map, the tag
439 // takes the place of the first element in the sequence.
440 if (StateStack.back() == inMapFirstKey) {
441 StateStack.pop_back();
442 StateStack.push_back(inMapOtherKey);
443 }
444 // Tags inside maps in sequences should act as keys in the map from a
445 // formatting perspective, so we always want a newline in a sequence.
446 NeedsNewLine = true;
447 }
427448 }
428449 return Use;
429450 }
5151 #CHECK: size: 15380
5252 #CHECK: align: 12
5353 #CHECK: Slices:
54 #CHECK: - FileHeader:
54 #CHECK: - !mach-o
55 #CHECK FileHeader:
5556 #CHECK: magic: 0xFEEDFACE
5657 #CHECK: cputype: 0x00000007
5758 #CHECK: cpusubtype: 0x00000003
5960 #CHECK: ncmds: 0
6061 #CHECK: sizeofcmds: 0
6162 #CHECK: flags: 0x01218085
62 #CHECK: - FileHeader:
63 #CHECK: - !mach-o
64 #CHECK FileHeader:
6365 #CHECK: magic: 0xFEEDFACF
6466 #CHECK: cputype: 0x01000007
6567 #CHECK: cpusubtype: 0x80000003