[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
diff --git a/llvm/unittests/BinaryFormat/MsgPackDocumentTest.cpp b/llvm/unittests/BinaryFormat/MsgPackDocumentTest.cpp
index 77b7e29..00b1579 100644
--- a/llvm/unittests/BinaryFormat/MsgPackDocumentTest.cpp
+++ b/llvm/unittests/BinaryFormat/MsgPackDocumentTest.cpp
@@ -127,7 +127,7 @@
   ASSERT_EQ(OStream.str(), "---\n"
                            "bar:             2\n"
                            "foo:             1\n"
-                           "qux:             \n"
+                           "qux:\n"
                            "  baz:             true\n"
                            "...\n");
 }
@@ -147,7 +147,7 @@
   ASSERT_EQ(OStream.str(), "---\n"
                            "bar:             0x2\n"
                            "foo:             1\n"
-                           "qux:             \n"
+                           "qux:\n"
                            "  baz:             true\n"
                            "...\n");
 }
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index ef14352..a757040 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -2528,7 +2528,7 @@
   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 @@
   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 @@
 
 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 @@
   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 @@
   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 @@
   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);
diff --git a/llvm/unittests/TextAPI/ELFYAMLTest.cpp b/llvm/unittests/TextAPI/ELFYAMLTest.cpp
index 9c50a6b..8217507 100644
--- a/llvm/unittests/TextAPI/ELFYAMLTest.cpp
+++ b/llvm/unittests/TextAPI/ELFYAMLTest.cpp
@@ -149,7 +149,7 @@
       "--- !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 @@
                           "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"
diff --git a/llvm/unittests/TextAPI/TextStubV1Tests.cpp b/llvm/unittests/TextAPI/TextStubV1Tests.cpp
index 5ff15b9..165e58f 100644
--- a/llvm/unittests/TextAPI/TextStubV1Tests.cpp
+++ b/llvm/unittests/TextAPI/TextStubV1Tests.cpp
@@ -159,7 +159,7 @@
       "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"
diff --git a/llvm/unittests/TextAPI/TextStubV2Tests.cpp b/llvm/unittests/TextAPI/TextStubV2Tests.cpp
index f380f7f..b4b8ca9 100644
--- a/llvm/unittests/TextAPI/TextStubV2Tests.cpp
+++ b/llvm/unittests/TextAPI/TextStubV2Tests.cpp
@@ -182,7 +182,7 @@
       "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"