diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h --- a/llvm/include/llvm/Support/YAMLTraits.h +++ b/llvm/include/llvm/Support/YAMLTraits.h @@ -1586,7 +1586,7 @@ private: void output(StringRef s); void outputUpToEndOfLine(StringRef s); - void newLineCheck(); + void newLineCheck(bool EmptySequence = false); void outputNewLine(); void paddedKey(StringRef key); void flowKey(StringRef Key); diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -592,7 +592,7 @@ // If we did not emit anything, we should explicitly emit an empty sequence if (StateStack.back() == inSeqFirstElement) { Padding = PaddingBeforeContainer; - newLineCheck(); + newLineCheck(/*EmptySequence=*/true); output("[]"); Padding = "\n"; } @@ -798,7 +798,7 @@ // if seq in middle, use "- " if firstKey, else use " " // -void Output::newLineCheck() { +void Output::newLineCheck(bool EmptySequence) { if (Padding != "\n") { output(Padding); Padding = {}; @@ -807,7 +807,7 @@ outputNewLine(); Padding = {}; - if (StateStack.size() == 0) + if (StateStack.size() == 0 || EmptySequence) return; unsigned Indent = StateStack.size() - 1; @@ -831,7 +831,6 @@ if (OutputDash) { output("- "); } - } void Output::paddedKey(StringRef key) { diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/unittests/Support/YAMLIOTest.cpp @@ -2691,12 +2691,23 @@ } TEST(YAMLIO, TestEmptySequenceWrite) { - FooBarContainer cont; - std::string str; - llvm::raw_string_ostream OS(str); - Output yout(OS); - yout << cont; - EXPECT_EQ(OS.str(), "---\nfbs: []\n...\n"); + { + FooBarContainer cont; + std::string str; + llvm::raw_string_ostream OS(str); + Output yout(OS); + yout << cont; + EXPECT_EQ(OS.str(), "---\nfbs: []\n...\n"); + } + + { + FooBarSequence seq; + std::string str; + llvm::raw_string_ostream OS(str); + Output yout(OS); + yout << seq; + EXPECT_EQ(OS.str(), "---\n[]\n...\n"); + } } static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {