[llvm] r365869 - [YAMLIO] Remove trailing spaces when outputting maps

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 21:51:31 PDT 2019


Author: maskray
Date: Thu Jul 11 21:51:31 2019
New Revision: 365869

URL: http://llvm.org/viewvc/llvm-project?rev=365869&view=rev
Log:
[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

Modified:
    llvm/trunk/include/llvm/Support/YAMLTraits.h
    llvm/trunk/lib/Support/YAMLTraits.cpp
    llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp
    llvm/trunk/unittests/Support/YAMLIOTest.cpp
    llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp
    llvm/trunk/unittests/TextAPI/TextStubV1Tests.cpp
    llvm/trunk/unittests/TextAPI/TextStubV2Tests.cpp

Modified: llvm/trunk/include/llvm/Support/YAMLTraits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/YAMLTraits.h (original)
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h Thu Jul 11 21:51:31 2019
@@ -1620,8 +1620,9 @@ private:
   bool NeedBitValueComma = false;
   bool NeedFlowSequenceComma = false;
   bool EnumerationMatchFound = false;
-  bool NeedsNewLine = false;
   bool WriteDefaultValues = false;
+  StringRef Padding;
+  StringRef PaddingBeforeContainer;
 };
 
 /// YAML I/O does conversion based on types. But often native data types

Modified: llvm/trunk/lib/Support/YAMLTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLTraits.cpp (original)
+++ llvm/trunk/lib/Support/YAMLTraits.cpp Thu Jul 11 21:51:31 2019
@@ -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
       }
       // 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
 
 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(StringR
   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) {

Modified: llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp (original)
+++ llvm/trunk/unittests/BinaryFormat/MsgPackDocumentTest.cpp Thu Jul 11 21:51:31 2019
@@ -127,7 +127,7 @@ TEST(MsgPackDocument, TestOutputYAMLMap)
   ASSERT_EQ(OStream.str(), "---\n"
                            "bar:             2\n"
                            "foo:             1\n"
-                           "qux:             \n"
+                           "qux:\n"
                            "  baz:             true\n"
                            "...\n");
 }
@@ -147,7 +147,7 @@ TEST(MsgPackDocument, TestOutputYAMLMapH
   ASSERT_EQ(OStream.str(), "---\n"
                            "bar:             0x2\n"
                            "foo:             1\n"
-                           "qux:             \n"
+                           "qux:\n"
                            "  baz:             true\n"
                            "...\n");
 }

Modified: llvm/trunk/unittests/Support/YAMLIOTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Thu Jul 11 21:51:31 2019
@@ -2528,7 +2528,7 @@ TEST(YAMLIO, TestMapWithContext) {
   ostr.flush();
   EXPECT_EQ(1, Context.A);
   EXPECT_EQ("---\n"
-            "Simple:          \n"
+            "Simple:\n"
             "  B:               0\n"
             "  C:               0\n"
             "  Context:         1\n"
@@ -2543,7 +2543,7 @@ TEST(YAMLIO, TestMapWithContext) {
   ostr.flush();
   EXPECT_EQ(2, Context.A);
   EXPECT_EQ("---\n"
-            "Simple:          \n"
+            "Simple:\n"
             "  B:               2\n"
             "  C:               3\n"
             "  Context:         2\n"
@@ -2556,8 +2556,6 @@ LLVM_YAML_IS_STRING_MAP(int)
 
 TEST(YAMLIO, TestCustomMapping) {
   std::map<std::string, int> x;
-  x["foo"] = 1;
-  x["bar"] = 2;
 
   std::string out;
   llvm::raw_string_ostream ostr(out);
@@ -2566,6 +2564,17 @@ TEST(YAMLIO, TestCustomMapping) {
   xout << x;
   ostr.flush();
   EXPECT_EQ("---\n"
+            "{}\n"
+            "...\n",
+            out);
+
+  x["foo"] = 1;
+  x["bar"] = 2;
+
+  out.clear();
+  xout << x;
+  ostr.flush();
+  EXPECT_EQ("---\n"
             "bar:             2\n"
             "foo:             1\n"
             "...\n",
@@ -2595,10 +2604,10 @@ TEST(YAMLIO, TestCustomMappingStruct) {
   xout << x;
   ostr.flush();
   EXPECT_EQ("---\n"
-            "bar:             \n"
+            "bar:\n"
             "  foo:             3\n"
             "  bar:             4\n"
-            "foo:             \n"
+            "foo:\n"
             "  foo:             1\n"
             "  bar:             2\n"
             "...\n",
@@ -2614,6 +2623,34 @@ TEST(YAMLIO, TestCustomMappingStruct) {
   EXPECT_EQ(4, y["bar"].bar);
 }
 
+struct FooBarMapMap {
+  std::map<std::string, FooBar> fbm;
+};
+
+template <> struct MappingTraits<FooBarMapMap> {
+  static void mapping(IO &io, FooBarMapMap &x) {
+    io.mapRequired("fbm", x.fbm);
+  }
+};
+
+TEST(YAMLIO, TestEmptyMapWrite) {
+  FooBarMapMap cont;
+  std::string str;
+  llvm::raw_string_ostream OS(str);
+  Output yout(OS);
+  yout << cont;
+  EXPECT_EQ(OS.str(), "---\nfbm:             {}\n...\n");
+}
+
+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");
+}
+
 static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
   std::string out;
   llvm::raw_string_ostream ostr(out);

Modified: llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp (original)
+++ llvm/trunk/unittests/TextAPI/ELFYAMLTest.cpp Thu Jul 11 21:51:31 2019
@@ -149,7 +149,7 @@ TEST(ElfYamlTextAPI, YAMLWritesTBESymbol
       "--- !tapi-tbe\n"
       "TbeVersion:      1.0\n"
       "Arch:            AArch64\n"
-      "Symbols:         \n"
+      "Symbols:\n"
       "  bar:             { Type: Func, Weak: true }\n"
       "  foo:             { Type: NoType, Size: 99, Warning: Does nothing }\n"
       "  nor:             { Type: Func, Undefined: true }\n"
@@ -205,7 +205,7 @@ TEST(ElfYamlTextAPI, YAMLWritesNoTBESyms
                           "TbeVersion:      1.0\n"
                           "SoName:          nosyms.so\n"
                           "Arch:            x86_64\n"
-                          "NeededLibs:      \n"
+                          "NeededLibs:\n"
                           "  - libc.so\n"
                           "  - libfoo.so\n"
                           "  - libbar.so\n"

Modified: llvm/trunk/unittests/TextAPI/TextStubV1Tests.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/TextAPI/TextStubV1Tests.cpp?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/unittests/TextAPI/TextStubV1Tests.cpp (original)
+++ llvm/trunk/unittests/TextAPI/TextStubV1Tests.cpp Thu Jul 11 21:51:31 2019
@@ -159,7 +159,7 @@ TEST(TBDv1, WriteFile) {
       "compatibility-version: 0\n"
       "swift-version:   5\n"
       "objc-constraint: retain_release\n"
-      "exports:         \n"
+      "exports:\n"
       "  - archs:           [ i386 ]\n"
       "    symbols:         [ _sym1 ]\n"
       "    weak-def-symbols: [ _sym2 ]\n"

Modified: llvm/trunk/unittests/TextAPI/TextStubV2Tests.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/TextAPI/TextStubV2Tests.cpp?rev=365869&r1=365868&r2=365869&view=diff
==============================================================================
--- llvm/trunk/unittests/TextAPI/TextStubV2Tests.cpp (original)
+++ llvm/trunk/unittests/TextAPI/TextStubV2Tests.cpp Thu Jul 11 21:51:31 2019
@@ -182,7 +182,7 @@ TEST(TBDv2, WriteFile) {
       "current-version: 1.2.3\n"
       "compatibility-version: 0\n"
       "swift-version:   5\n"
-      "exports:         \n"
+      "exports:\n"
       "  - archs:           [ i386 ]\n"
       "    symbols:         [ _sym1 ]\n"
       "    weak-def-symbols: [ _sym2 ]\n"




More information about the llvm-commits mailing list