Skip to content

Commit 27ed1c5

Browse files
committedJul 12, 2019
[YAMLIO] Remove trailing spaces when outputting maps
llvm::yaml::Output::paddedKey unconditionally outputs spaces, which are superfluous if the value to be dumped is a sequence or map. Change `bool NeedsNewLine` to `StringRef Padding` so that it can be overridden to `\n` if the value is a sequence or map. An empty map/sequence is special. It is printed as `{}` or `[]` without a newline, while a non-empty map/sequence follows a newline. To handle this distinction, add another variable `PaddingBeforeContainer` and does the special handling in endMapping/endSequence. Reviewed By: grimar, jhenderson Differential Revision: https://reviews.llvm.org/D64566 llvm-svn: 365869
1 parent 320d679 commit 27ed1c5

File tree

7 files changed

+74
-24
lines changed

7 files changed

+74
-24
lines changed
 

‎llvm/include/llvm/Support/YAMLTraits.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1620,8 +1620,9 @@ class Output : public IO {
16201620
bool NeedBitValueComma = false;
16211621
bool NeedFlowSequenceComma = false;
16221622
bool EnumerationMatchFound = false;
1623-
bool NeedsNewLine = false;
16241623
bool WriteDefaultValues = false;
1624+
StringRef Padding;
1625+
StringRef PaddingBeforeContainer;
16251626
};
16261627

16271628
/// YAML I/O does conversion based on types. But often native data types

‎llvm/lib/Support/YAMLTraits.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ bool Output::outputting() {
446446

447447
void Output::beginMapping() {
448448
StateStack.push_back(inMapFirstKey);
449-
NeedsNewLine = true;
449+
PaddingBeforeContainer = Padding;
450+
Padding = "\n";
450451
}
451452

452453
bool Output::mapTag(StringRef Tag, bool Use) {
@@ -474,16 +475,20 @@ bool Output::mapTag(StringRef Tag, bool Use) {
474475
}
475476
// Tags inside maps in sequences should act as keys in the map from a
476477
// formatting perspective, so we always want a newline in a sequence.
477-
NeedsNewLine = true;
478+
Padding = "\n";
478479
}
479480
}
480481
return Use;
481482
}
482483

483484
void Output::endMapping() {
484485
// If we did not map anything, we should explicitly emit an empty map
485-
if (StateStack.back() == inMapFirstKey)
486+
if (StateStack.back() == inMapFirstKey) {
487+
Padding = PaddingBeforeContainer;
488+
newLineCheck();
486489
output("{}");
490+
Padding = "\n";
491+
}
487492
StateStack.pop_back();
488493
}
489494

@@ -548,14 +553,19 @@ void Output::endDocuments() {
548553

549554
unsigned Output::beginSequence() {
550555
StateStack.push_back(inSeqFirstElement);
551-
NeedsNewLine = true;
556+
PaddingBeforeContainer = Padding;
557+
Padding = "\n";
552558
return 0;
553559
}
554560

555561
void Output::endSequence() {
556562
// If we did not emit anything, we should explicitly emit an empty sequence
557-
if (StateStack.back() == inSeqFirstElement)
563+
if (StateStack.back() == inSeqFirstElement) {
564+
Padding = PaddingBeforeContainer;
565+
newLineCheck();
558566
output("[]");
567+
Padding = "\n";
568+
}
559569
StateStack.pop_back();
560570
}
561571

@@ -746,7 +756,7 @@ void Output::outputUpToEndOfLine(StringRef s) {
746756
output(s);
747757
if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
748758
!inFlowMapAnyKey(StateStack.back())))
749-
NeedsNewLine = true;
759+
Padding = "\n";
750760
}
751761

752762
void Output::outputNewLine() {
@@ -759,11 +769,13 @@ void Output::outputNewLine() {
759769
//
760770

761771
void Output::newLineCheck() {
762-
if (!NeedsNewLine)
772+
if (Padding != "\n") {
773+
output(Padding);
774+
Padding = {};
763775
return;
764-
NeedsNewLine = false;
765-
776+
}
766777
outputNewLine();
778+
Padding = {};
767779

768780
if (StateStack.size() == 0)
769781
return;
@@ -797,9 +809,9 @@ void Output::paddedKey(StringRef key) {
797809
output(":");
798810
const char *spaces = " ";
799811
if (key.size() < strlen(spaces))
800-
output(&spaces[key.size()]);
812+
Padding = &spaces[key.size()];
801813
else
802-
output(" ");
814+
Padding = " ";
803815
}
804816

805817
void Output::flowKey(StringRef Key) {

‎llvm/unittests/BinaryFormat/MsgPackDocumentTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ TEST(MsgPackDocument, TestOutputYAMLMap) {
127127
ASSERT_EQ(OStream.str(), "---\n"
128128
"bar: 2\n"
129129
"foo: 1\n"
130-
"qux: \n"
130+
"qux:\n"
131131
" baz: true\n"
132132
"...\n");
133133
}
@@ -147,7 +147,7 @@ TEST(MsgPackDocument, TestOutputYAMLMapHex) {
147147
ASSERT_EQ(OStream.str(), "---\n"
148148
"bar: 0x2\n"
149149
"foo: 1\n"
150-
"qux: \n"
150+
"qux:\n"
151151
" baz: true\n"
152152
"...\n");
153153
}

‎llvm/unittests/Support/YAMLIOTest.cpp

+43-6
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,7 @@ TEST(YAMLIO, TestMapWithContext) {
25282528
ostr.flush();
25292529
EXPECT_EQ(1, Context.A);
25302530
EXPECT_EQ("---\n"
2531-
"Simple: \n"
2531+
"Simple:\n"
25322532
" B: 0\n"
25332533
" C: 0\n"
25342534
" Context: 1\n"
@@ -2543,7 +2543,7 @@ TEST(YAMLIO, TestMapWithContext) {
25432543
ostr.flush();
25442544
EXPECT_EQ(2, Context.A);
25452545
EXPECT_EQ("---\n"
2546-
"Simple: \n"
2546+
"Simple:\n"
25472547
" B: 2\n"
25482548
" C: 3\n"
25492549
" Context: 2\n"
@@ -2556,13 +2556,22 @@ LLVM_YAML_IS_STRING_MAP(int)
25562556

25572557
TEST(YAMLIO, TestCustomMapping) {
25582558
std::map<std::string, int> x;
2559-
x["foo"] = 1;
2560-
x["bar"] = 2;
25612559

25622560
std::string out;
25632561
llvm::raw_string_ostream ostr(out);
25642562
Output xout(ostr, nullptr, 0);
25652563

2564+
xout << x;
2565+
ostr.flush();
2566+
EXPECT_EQ("---\n"
2567+
"{}\n"
2568+
"...\n",
2569+
out);
2570+
2571+
x["foo"] = 1;
2572+
x["bar"] = 2;
2573+
2574+
out.clear();
25662575
xout << x;
25672576
ostr.flush();
25682577
EXPECT_EQ("---\n"
@@ -2595,10 +2604,10 @@ TEST(YAMLIO, TestCustomMappingStruct) {
25952604
xout << x;
25962605
ostr.flush();
25972606
EXPECT_EQ("---\n"
2598-
"bar: \n"
2607+
"bar:\n"
25992608
" foo: 3\n"
26002609
" bar: 4\n"
2601-
"foo: \n"
2610+
"foo:\n"
26022611
" foo: 1\n"
26032612
" bar: 2\n"
26042613
"...\n",
@@ -2614,6 +2623,34 @@ TEST(YAMLIO, TestCustomMappingStruct) {
26142623
EXPECT_EQ(4, y["bar"].bar);
26152624
}
26162625

2626+
struct FooBarMapMap {
2627+
std::map<std::string, FooBar> fbm;
2628+
};
2629+
2630+
template <> struct MappingTraits<FooBarMapMap> {
2631+
static void mapping(IO &io, FooBarMapMap &x) {
2632+
io.mapRequired("fbm", x.fbm);
2633+
}
2634+
};
2635+
2636+
TEST(YAMLIO, TestEmptyMapWrite) {
2637+
FooBarMapMap cont;
2638+
std::string str;
2639+
llvm::raw_string_ostream OS(str);
2640+
Output yout(OS);
2641+
yout << cont;
2642+
EXPECT_EQ(OS.str(), "---\nfbm: {}\n...\n");
2643+
}
2644+
2645+
TEST(YAMLIO, TestEmptySequenceWrite) {
2646+
FooBarContainer cont;
2647+
std::string str;
2648+
llvm::raw_string_ostream OS(str);
2649+
Output yout(OS);
2650+
yout << cont;
2651+
EXPECT_EQ(OS.str(), "---\nfbs: []\n...\n");
2652+
}
2653+
26172654
static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
26182655
std::string out;
26192656
llvm::raw_string_ostream ostr(out);

‎llvm/unittests/TextAPI/ELFYAMLTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ TEST(ElfYamlTextAPI, YAMLWritesTBESymbols) {
149149
"--- !tapi-tbe\n"
150150
"TbeVersion: 1.0\n"
151151
"Arch: AArch64\n"
152-
"Symbols: \n"
152+
"Symbols:\n"
153153
" bar: { Type: Func, Weak: true }\n"
154154
" foo: { Type: NoType, Size: 99, Warning: Does nothing }\n"
155155
" nor: { Type: Func, Undefined: true }\n"
@@ -205,7 +205,7 @@ TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms) {
205205
"TbeVersion: 1.0\n"
206206
"SoName: nosyms.so\n"
207207
"Arch: x86_64\n"
208-
"NeededLibs: \n"
208+
"NeededLibs:\n"
209209
" - libc.so\n"
210210
" - libfoo.so\n"
211211
" - libbar.so\n"

‎llvm/unittests/TextAPI/TextStubV1Tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ TEST(TBDv1, WriteFile) {
159159
"compatibility-version: 0\n"
160160
"swift-version: 5\n"
161161
"objc-constraint: retain_release\n"
162-
"exports: \n"
162+
"exports:\n"
163163
" - archs: [ i386 ]\n"
164164
" symbols: [ _sym1 ]\n"
165165
" weak-def-symbols: [ _sym2 ]\n"

‎llvm/unittests/TextAPI/TextStubV2Tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ TEST(TBDv2, WriteFile) {
182182
"current-version: 1.2.3\n"
183183
"compatibility-version: 0\n"
184184
"swift-version: 5\n"
185-
"exports: \n"
185+
"exports:\n"
186186
" - archs: [ i386 ]\n"
187187
" symbols: [ _sym1 ]\n"
188188
" weak-def-symbols: [ _sym2 ]\n"

0 commit comments

Comments
 (0)
Please sign in to comment.