Index: include/llvm/Support/YAMLTraits.h =================================================================== --- include/llvm/Support/YAMLTraits.h +++ include/llvm/Support/YAMLTraits.h @@ -1620,8 +1620,8 @@ bool NeedBitValueComma = false; bool NeedFlowSequenceComma = false; bool EnumerationMatchFound = false; - bool NeedsNewLine = false; bool WriteDefaultValues = false; + StringRef Padding; }; /// YAML I/O does conversion based on types. But often native data types Index: lib/Support/YAMLTraits.cpp =================================================================== --- lib/Support/YAMLTraits.cpp +++ lib/Support/YAMLTraits.cpp @@ -446,7 +446,7 @@ void Output::beginMapping() { StateStack.push_back(inMapFirstKey); - NeedsNewLine = true; + Padding = "\n"; } bool Output::mapTag(StringRef Tag, bool Use) { @@ -474,7 +474,7 @@ } // 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; + Padding = "\n"; } } return Use; @@ -548,7 +548,7 @@ unsigned Output::beginSequence() { StateStack.push_back(inSeqFirstElement); - NeedsNewLine = true; + Padding = "\n"; return 0; } @@ -746,7 +746,7 @@ output(s); if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) && !inFlowMapAnyKey(StateStack.back()))) - NeedsNewLine = true; + Padding = "\n"; } void Output::outputNewLine() { @@ -759,11 +759,13 @@ // void Output::newLineCheck() { - if (!NeedsNewLine) + Out << Padding; + if (Padding != "\n") { + Padding = {}; return; - NeedsNewLine = false; - - outputNewLine(); + } + Padding = {}; + Column = 0; if (StateStack.size() == 0) return; @@ -797,9 +799,9 @@ output(":"); const char *spaces = " "; if (key.size() < strlen(spaces)) - output(&spaces[key.size()]); + Padding = &spaces[key.size()]; else - output(" "); + Padding = " "; } void Output::flowKey(StringRef Key) { Index: unittests/Support/YAMLIOTest.cpp =================================================================== --- unittests/Support/YAMLIOTest.cpp +++ unittests/Support/YAMLIOTest.cpp @@ -2528,7 +2528,7 @@ ostr.flush(); EXPECT_EQ(1, Context.A); EXPECT_EQ("---\n" - "Simple: \n" + "Simple:\n" " B: 0\n" " C: 0\n" " Context: 1\n" @@ -2543,7 +2543,7 @@ ostr.flush(); EXPECT_EQ(2, Context.A); EXPECT_EQ("---\n" - "Simple: \n" + "Simple:\n" " B: 2\n" " C: 3\n" " Context: 2\n" @@ -2595,10 +2595,10 @@ xout << x; ostr.flush(); EXPECT_EQ("---\n" - "bar: \n" + "bar:\n" " foo: 3\n" " bar: 4\n" - "foo: \n" + "foo:\n" " foo: 1\n" " bar: 2\n" "...\n",