From 27ed1c5bb80c8da317cd80bdd50cb22189d1c6fb Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 12 Jul 2019 04:51:31 +0000 Subject: [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 --- llvm/lib/Support/YAMLTraits.cpp | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'llvm/lib/Support/YAMLTraits.cpp') diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp index 183da5b3900..09eb36943de 100644 --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -446,7 +446,8 @@ bool Output::outputting() { void Output::beginMapping() { StateStack.push_back(inMapFirstKey); - NeedsNewLine = true; + PaddingBeforeContainer = Padding; + Padding = "\n"; } bool Output::mapTag(StringRef Tag, bool Use) { @@ -474,7 +475,7 @@ bool Output::mapTag(StringRef Tag, bool Use) { } // Tags inside maps in sequences should act as keys in the map from a // formatting perspective, so we always want a newline in a sequence. - NeedsNewLine = true; + Padding = "\n"; } } return Use; @@ -482,8 +483,12 @@ bool Output::mapTag(StringRef Tag, bool Use) { void Output::endMapping() { // If we did not map anything, we should explicitly emit an empty map - if (StateStack.back() == inMapFirstKey) + if (StateStack.back() == inMapFirstKey) { + Padding = PaddingBeforeContainer; + newLineCheck(); output("{}"); + Padding = "\n"; + } StateStack.pop_back(); } @@ -548,14 +553,19 @@ void Output::endDocuments() { unsigned Output::beginSequence() { StateStack.push_back(inSeqFirstElement); - NeedsNewLine = true; + PaddingBeforeContainer = Padding; + Padding = "\n"; return 0; } void Output::endSequence() { // If we did not emit anything, we should explicitly emit an empty sequence - if (StateStack.back() == inSeqFirstElement) + if (StateStack.back() == inSeqFirstElement) { + Padding = PaddingBeforeContainer; + newLineCheck(); output("[]"); + Padding = "\n"; + } StateStack.pop_back(); } @@ -746,7 +756,7 @@ void Output::outputUpToEndOfLine(StringRef s) { output(s); if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) && !inFlowMapAnyKey(StateStack.back()))) - NeedsNewLine = true; + Padding = "\n"; } void Output::outputNewLine() { @@ -759,11 +769,13 @@ void Output::outputNewLine() { // void Output::newLineCheck() { - if (!NeedsNewLine) + if (Padding != "\n") { + output(Padding); + Padding = {}; return; - NeedsNewLine = false; - + } outputNewLine(); + Padding = {}; if (StateStack.size() == 0) return; @@ -797,9 +809,9 @@ void Output::paddedKey(StringRef key) { output(":"); const char *spaces = " "; if (key.size() < strlen(spaces)) - output(&spaces[key.size()]); + Padding = &spaces[key.size()]; else - output(" "); + Padding = " "; } void Output::flowKey(StringRef Key) { -- cgit v1.2.3