[llvm] r274067 - [YAML] Fix YAML tags appearing before the start of sequence elements

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 28 14:10:26 PDT 2016


Author: cbieneman
Date: Tue Jun 28 16:10:26 2016
New Revision: 274067

URL: http://llvm.org/viewvc/llvm-project?rev=274067&view=rev
Log:
[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.

Modified:
    llvm/trunk/lib/ObjectYAML/MachOYAML.cpp
    llvm/trunk/lib/Support/YAMLTraits.cpp
    llvm/trunk/test/ObjectYAML/MachO/fat_macho_i386_x86_64.yaml

Modified: llvm/trunk/lib/ObjectYAML/MachOYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ObjectYAML/MachOYAML.cpp?rev=274067&r1=274066&r2=274067&view=diff
==============================================================================
--- llvm/trunk/lib/ObjectYAML/MachOYAML.cpp (original)
+++ llvm/trunk/lib/ObjectYAML/MachOYAML.cpp Tue Jun 28 16:10:26 2016
@@ -91,8 +91,8 @@ void MappingTraits<MachOYAML::Object>::m
   // For Fat files there will be a different tag so they can be differentiated.
   if (!IO.getContext()) {
     IO.setContext(&Object);
-    IO.mapTag("!mach-o", true);
   }
+  IO.mapTag("!mach-o", true);
   IO.mapRequired("FileHeader", Object.Header);
   IO.mapOptional("LoadCommands", Object.LoadCommands);
   IO.mapOptional("LinkEditData", Object.LinkEdit);

Modified: llvm/trunk/lib/Support/YAMLTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=274067&r1=274066&r2=274067&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLTraits.cpp (original)
+++ llvm/trunk/lib/Support/YAMLTraits.cpp Tue Jun 28 16:10:26 2016
@@ -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;
 }

Modified: llvm/trunk/test/ObjectYAML/MachO/fat_macho_i386_x86_64.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ObjectYAML/MachO/fat_macho_i386_x86_64.yaml?rev=274067&r1=274066&r2=274067&view=diff
==============================================================================
--- llvm/trunk/test/ObjectYAML/MachO/fat_macho_i386_x86_64.yaml (original)
+++ llvm/trunk/test/ObjectYAML/MachO/fat_macho_i386_x86_64.yaml Tue Jun 28 16:10:26 2016
@@ -52,7 +52,8 @@ Slices:
 #CHECK:     size:            15380
 #CHECK:     align:           12
 #CHECK: Slices:          
-#CHECK:   - FileHeader:      
+#CHECK:   - !mach-o
+#CHECK      FileHeader:      
 #CHECK:       magic:           0xFEEDFACE
 #CHECK:       cputype:         0x00000007
 #CHECK:       cpusubtype:      0x00000003
@@ -60,7 +61,8 @@ Slices:
 #CHECK:       ncmds:           0
 #CHECK:       sizeofcmds:      0
 #CHECK:       flags:           0x01218085
-#CHECK:   - FileHeader:      
+#CHECK:   - !mach-o
+#CHECK      FileHeader:        
 #CHECK:       magic:           0xFEEDFACF
 #CHECK:       cputype:         0x01000007
 #CHECK:       cpusubtype:      0x80000003




More information about the llvm-commits mailing list