[PATCH] D47468: [YAML] Double-quote multiline string scalars

Ilya Biryukov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 29 03:38:44 PDT 2018


ilya-biryukov created this revision.
ilya-biryukov added a reviewer: sammccall.

Otherwise, the YAML parser breaks when trying to read them back in
'key: multiline_string_value' cases.


Repository:
  rL LLVM

https://reviews.llvm.org/D47468

Files:
  include/llvm/Support/YAMLTraits.h
  unittests/Support/YAMLIOTest.cpp


Index: unittests/Support/YAMLIOTest.cpp
===================================================================
--- unittests/Support/YAMLIOTest.cpp
+++ unittests/Support/YAMLIOTest.cpp
@@ -250,6 +250,55 @@
 }
 
 
+struct WithStringField {
+  std::string str1;
+  std::string str2;
+  std::string str3;
+  std::string str4;
+};
+
+namespace llvm {
+namespace yaml {
+  template <>
+  struct MappingTraits<WithStringField> {
+    static void mapping(IO &io, WithStringField& fb) {
+      io.mapRequired("str1",    fb.str1);
+      io.mapRequired("str2",    fb.str2);
+      io.mapRequired("str3",    fb.str3);
+      io.mapRequired("str4",    fb.str4);
+    }
+  };
+}}
+
+TEST(YAMLIO, MultilineStrings) {
+  WithStringField Original;
+  Original.str1 = "a multiline string\nfoobarbaz";
+  Original.str2 = "another one\rfoobarbaz";
+  Original.str3 = "yet another one\tfoobarbaz";
+  Original.str4 = "a one-line string";
+
+  std::string Serialized;
+  {
+    llvm::raw_string_ostream OS(Serialized);
+    Output YOut(OS);
+    YOut << Original;
+  }
+
+  WithStringField Deserialized;
+  {
+    Input YIn(Serialized);
+    YIn >> Deserialized;
+    ASSERT_FALSE(YIn.error())
+        << "Parsing error occurred during deserialization. Serialized string:\n"
+        << Serialized;
+  }
+  EXPECT_EQ(Original.str1, Deserialized.str1);
+  EXPECT_EQ(Original.str2, Deserialized.str2);
+  EXPECT_EQ(Original.str3, Deserialized.str3);
+  EXPECT_EQ(Original.str4, Deserialized.str4);
+}
+
+
 //===----------------------------------------------------------------------===//
 //  Test built-in types
 //===----------------------------------------------------------------------===//
Index: include/llvm/Support/YAMLTraits.h
===================================================================
--- include/llvm/Support/YAMLTraits.h
+++ include/llvm/Support/YAMLTraits.h
@@ -540,12 +540,15 @@
     case '.':
     case ',':
     case ' ':
-    // TAB (0x9), LF (0xA), CR (0xD) and NEL (0x85) are allowed.
+    // TAB (0x9) is allowed.
     case 0x9:
+      continue;
+    // LF (0xA), CR (0xD) and NEL (0x85) cause parse errors when used as values
+    // in 'key: value' pairs unless double-quoted.
     case 0xA:
     case 0xD:
     case 0x85:
-      continue;
+      return QuotingType::Double;
     // DEL (0x7F) are excluded from the allowed character range.
     case 0x7F:
       return QuotingType::Double;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47468.148873.patch
Type: text/x-patch
Size: 2398 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180529/652931e8/attachment.bin>


More information about the llvm-commits mailing list