Skip to content

Commit c149774

Browse files
committed
[YAML] fix output incorrect format for block scalar string
1 parent 3b1e18c commit c149774

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

llvm/lib/Support/YAMLTraits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,18 +725,18 @@ void Output::blockScalarString(StringRef &S) {
725725
if (!StateStack.empty())
726726
newLineCheck();
727727
output(" |");
728-
outputNewLine();
729728

730729
unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
731730

732731
auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
733732
for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
733+
outputNewLine();
734734
for (unsigned I = 0; I < Indent; ++I) {
735735
output(" ");
736736
}
737737
output(*Lines);
738-
outputNewLine();
739738
}
739+
outputUpToEndOfLine("");
740740
}
741741

742742
void Output::scalarTag(std::string &Tag) {

llvm/unittests/Support/YAMLIOTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,36 @@ TEST(YAMLIO, TestReadWriteBlockScalarValue) {
12731273
}
12741274
}
12751275

1276+
struct V {
1277+
MultilineStringType doc;
1278+
std::string str;
1279+
};
1280+
template <> struct MappingTraits<V> {
1281+
static void mapping(IO &io, V &v) {
1282+
io.mapRequired("block_scalac", v.doc);
1283+
io.mapRequired("scalar", v.str);
1284+
}
1285+
};
1286+
template <> struct llvm::yaml::SequenceElementTraits<V> {
1287+
static const bool flow = false;
1288+
};
1289+
TEST(YAMLIO, TestScalarAfterBlockScalar) {
1290+
std::vector<V> v{V{}};
1291+
v[0].doc.str = "Just a block\nscalar doc";
1292+
v[0].str = "a";
1293+
std::string output;
1294+
llvm::raw_string_ostream ostr(output);
1295+
Output yout(ostr);
1296+
yout << v;
1297+
EXPECT_EQ(output, R"(---
1298+
- block_scalac: |
1299+
Just a block
1300+
scalar doc
1301+
scalar: a
1302+
...
1303+
)");
1304+
}
1305+
12761306
//===----------------------------------------------------------------------===//
12771307
// Test flow sequences
12781308
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)