[llvm] [YAML] fix output incorrect format for block scalar string (PR #132897)

Congcong Cai via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 25 01:21:34 PDT 2025


https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/132897

After outputting block scalar string, the indent will be wrong.
This patch fixes Padding after block scalar string to ensure the correct format of yaml.

The new added ut will fail in main.
```diff
@@ -3,4 +3,4 @@
     Just a block
     scalar doc
-scalar:          a
+  scalar:          a
 ...\n
```

>From bf4fcc71511fa06a6397d1b31080fda8112e5b33 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Mon, 17 Mar 2025 23:29:36 +0000
Subject: [PATCH] [YAML] fix output incorrect format for block scalar string

---
 llvm/lib/Support/YAMLTraits.cpp       | 4 ++--
 llvm/unittests/Support/YAMLIOTest.cpp | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index 28642e004c4f4..035828b594e84 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -725,18 +725,18 @@ void Output::blockScalarString(StringRef &S) {
   if (!StateStack.empty())
     newLineCheck();
   output(" |");
-  outputNewLine();
 
   unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
 
   auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
   for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
+    outputNewLine();
     for (unsigned I = 0; I < Indent; ++I) {
       output("  ");
     }
     output(*Lines);
-    outputNewLine();
   }
+  outputUpToEndOfLine("");
 }
 
 void Output::scalarTag(std::string &Tag) {
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index 3db1db57ad596..5a86555d08fb3 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -1298,7 +1298,7 @@ TEST(YAMLIO, TestScalarAfterBlockScalar) {
 - block_scalac:     |
     AA
     BB
-scalar:          a
+  scalar:          a
 ...
 )");
 }



More information about the llvm-commits mailing list