[PATCH] D95280: [YAML I/O] Fix bug in emission of empty sequence
Jonas Devlieghere via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 22 17:36:25 PST 2021
JDevlieghere created this revision.
JDevlieghere added reviewers: kledzik, arphaman, thegameg.
Herald added subscribers: dexonsmith, hiraditya.
JDevlieghere requested review of this revision.
Herald added a project: LLVM.
Don't emit an output dash for an empty sequence.
Take emitting a vector of strings for example:
std::vector<std::string> Strings = {"foo", "bar"};
LLVM_YAML_IS_SEQUENCE_VECTOR(std::string)
yout << Strings;
This emits the following YAML document.
---
- foo
- bar
...
However, when the vector is empty, the result is invalid:
---
- []
...
The problem is the output dash before the empty list. The correct output would be:
---
[]
...
This patch fixes that. I'm not too familiar with the YAML I/O implementation beyond the traits themselves, so I'm open to suggestions if you know a better way to fix this.
https://reviews.llvm.org/D95280
Files:
llvm/include/llvm/Support/YAMLTraits.h
llvm/lib/Support/YAMLTraits.cpp
llvm/unittests/Support/YAMLIOTest.cpp
Index: llvm/unittests/Support/YAMLIOTest.cpp
===================================================================
--- llvm/unittests/Support/YAMLIOTest.cpp
+++ 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) {
Index: llvm/lib/Support/YAMLTraits.cpp
===================================================================
--- llvm/lib/Support/YAMLTraits.cpp
+++ 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) {
Index: llvm/include/llvm/Support/YAMLTraits.h
===================================================================
--- llvm/include/llvm/Support/YAMLTraits.h
+++ 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);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95280.318696.patch
Type: text/x-patch
Size: 2391 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210123/c3be4cb1/attachment.bin>
More information about the llvm-commits
mailing list