Skip to content

[YAML] fix output incorrect format for block scalar string #131694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

HerrCai0907
Copy link
Contributor

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.

@@ -3,4 +3,4 @@
     Just a block
     scalar doc
-scalar:          a
+  scalar:          a
 ...\n

@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-llvm-support

Author: Congcong Cai (HerrCai0907)

Changes

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.

@@ -3,4 +3,4 @@
     Just a block
     scalar doc
-scalar:          a
+  scalar:          a
 ...\n

Full diff: https://github.com/llvm/llvm-project/pull/131694.diff

2 Files Affected:

  • (modified) llvm/lib/Support/YAMLTraits.cpp (+2-2)
  • (modified) llvm/unittests/Support/YAMLIOTest.cpp (+30)
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 c0e9c57a77f19..d3e181075737d 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -1273,6 +1273,36 @@ TEST(YAMLIO, TestReadWriteBlockScalarValue) {
   }
 }
 
+struct V {
+  MultilineStringType doc;
+  std::string str;
+};
+template <> struct MappingTraits<V> {
+  static void mapping(IO &io, V &v) {
+    io.mapRequired("block_scalac", v.doc);
+    io.mapRequired("scalar", v.str);
+  }
+};
+template <> struct llvm::yaml::SequenceElementTraits<V> {
+  static const bool flow = false;
+};
+TEST(YAMLIO, TestScalarAfterBlockScalar) {
+  std::vector<V> v{V{}};
+  v[0].doc.str = "Just a block\nscalar doc";
+  v[0].str = "a";
+  std::string output;
+  llvm::raw_string_ostream ostr(output);
+  Output yout(ostr);
+  yout << v;
+  EXPECT_EQ(output, R"(---
+- block_scalac:     |
+    Just a block
+    scalar doc
+  scalar:          a
+...
+)");
+}
+
 //===----------------------------------------------------------------------===//
 //  Test flow sequences
 //===----------------------------------------------------------------------===//

@thurstond
Copy link
Contributor

I'd suggest precommitting TestScalarAfterBlockScalar (with EXPECT_EQ changed to the current, erroneous output) as a separate patch, and then rebasing this patch on top of that; that would make it clearer what your fix accomplishes.

@arsenm arsenm requested review from JDevlieghere and hyp March 18, 2025 01:28
@HerrCai0907 HerrCai0907 changed the base branch from main to users/ccc03-18-_yaml_nfc_precommit_wrong_test_case March 18, 2025 11:00
@HerrCai0907
Copy link
Contributor Author

ping @thurstond

@arsenm
Copy link
Contributor

arsenm commented Mar 24, 2025

can you rebase to retrigger tests

@HerrCai0907 HerrCai0907 deleted the branch llvm:users/ccc03-18-_yaml_nfc_precommit_wrong_test_case March 25, 2025 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants