YAML I/O - Added default trait support for std:string. Making another attempt at...
authorJohn Thompson <John.Thompson.JTSoftware@gmail.com>
Tue, 19 Nov 2013 17:28:21 +0000 (17:28 +0000)
committerJohn Thompson <John.Thompson.JTSoftware@gmail.com>
Tue, 19 Nov 2013 17:28:21 +0000 (17:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@195134 91177308-0d34-0410-b5e6-96231b3b80d8

docs/YamlIO.rst
include/llvm/Support/YAMLTraits.h
lib/Support/YAMLTraits.cpp
unittests/Support/YAMLIOTest.cpp

index 3ecd03afb24d8871d2dd51f085cfa9806ff865bc..5ec0a01f8d82dd134bba1d2b470729c57a3618cb 100644 (file)
@@ -234,6 +234,7 @@ The following types have built-in support in YAML I/O:
 * float
 * double
 * StringRef
+* std::string
 * int64_t
 * int32_t
 * int16_t
index c19eb23c5cd02e05252eac80ada61b2e75c4907f..4d5b3792450a25ab66175d9cebca0927e675b37d 100644 (file)
@@ -538,6 +538,12 @@ struct ScalarTraits<StringRef> {
   static void output(const StringRef &, void*, llvm::raw_ostream &);
   static StringRef input(StringRef, void*, StringRef &);
 };
+template<>
+struct ScalarTraits<std::string> {
+  static void output(const std::string &, void*, llvm::raw_ostream &);
+  static StringRef input(StringRef, void*, std::string &);
+};
 
 template<>
 struct ScalarTraits<uint8_t> {
index 42bff96f73bcb31f26484ef210220b3152bd96d2..599a369558d4a9c01f6f9518950ea75b9f564198 100644 (file)
@@ -689,6 +689,17 @@ StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
   Val = Scalar;
   return StringRef();
 }
+void ScalarTraits<std::string>::output(const std::string &Val, void *,
+                                     raw_ostream &Out) {
+  Out << Val;
+}
+
+StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
+                                         std::string &Val) {
+  Val = Scalar.str();
+  return StringRef();
+}
 
 void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
                                    raw_ostream &Out) {
index 6c0b9e6151f20ec58dd1ab6fbbd9ec7d614de7d8..2b033b2f3017d8530972b2739822e45462be2912 100644 (file)
@@ -140,6 +140,7 @@ TEST(YAMLIO, TestSequenceMapWriteAndRead) {
 
 struct BuiltInTypes {
   llvm::StringRef str;
+  std::string stdstr;
   uint64_t        u64;
   uint32_t        u32;
   uint16_t        u16;
@@ -163,6 +164,7 @@ namespace yaml {
   struct MappingTraits<BuiltInTypes> {
     static void mapping(IO &io, BuiltInTypes& bt) {
       io.mapRequired("str",      bt.str);
+      io.mapRequired("stdstr",   bt.stdstr);
       io.mapRequired("u64",      bt.u64);
       io.mapRequired("u32",      bt.u32);
       io.mapRequired("u16",      bt.u16);
@@ -191,6 +193,7 @@ TEST(YAMLIO, TestReadBuiltInTypes) {
   BuiltInTypes map;
   Input yin("---\n"
             "str:      hello there\n"
+            "stdstr:   hello where?\n"
             "u64:      5000000000\n"
             "u32:      4000000000\n"
             "u16:      65000\n"
@@ -211,6 +214,7 @@ TEST(YAMLIO, TestReadBuiltInTypes) {
 
   EXPECT_FALSE(yin.error());
   EXPECT_TRUE(map.str.equals("hello there"));
+  EXPECT_TRUE(map.stdstr == "hello where?");
   EXPECT_EQ(map.u64, 5000000000ULL);
   EXPECT_EQ(map.u32, 4000000000U);
   EXPECT_EQ(map.u16, 65000);
@@ -237,6 +241,7 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) {
   {
     BuiltInTypes map;
     map.str = "one two";
+    map.stdstr = "three four";
     map.u64 = 6000000000ULL;
     map.u32 = 3000000000U;
     map.u16 = 50000;
@@ -265,6 +270,7 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) {
 
     EXPECT_FALSE(yin.error());
     EXPECT_TRUE(map.str.equals("one two"));
+    EXPECT_TRUE(map.stdstr == "three four");
     EXPECT_EQ(map.u64,      6000000000ULL);
     EXPECT_EQ(map.u32,      3000000000U);
     EXPECT_EQ(map.u16,      50000);
@@ -289,6 +295,11 @@ struct StringTypes {
   llvm::StringRef str3;
   llvm::StringRef str4;
   llvm::StringRef str5;
+  std::string stdstr1;
+  std::string stdstr2;
+  std::string stdstr3;
+  std::string stdstr4;
+  std::string stdstr5;
 };
 
 namespace llvm {
@@ -301,6 +312,11 @@ namespace yaml {
       io.mapRequired("str3",      st.str3);
       io.mapRequired("str4",      st.str4);
       io.mapRequired("str5",      st.str5);
+      io.mapRequired("stdstr1",   st.stdstr1);
+      io.mapRequired("stdstr2",   st.stdstr2);
+      io.mapRequired("stdstr3",   st.stdstr3);
+      io.mapRequired("stdstr4",   st.stdstr4);
+      io.mapRequired("stdstr5",   st.stdstr5);
     }
   };
 }
@@ -315,6 +331,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) {
     map.str3 = "`ccc";
     map.str4 = "@ddd";
     map.str5 = "";
+    map.stdstr1 = "'eee";
+    map.stdstr2 = "\"fff";
+    map.stdstr3 = "`ggg";
+    map.stdstr4 = "@hhh";
+    map.stdstr5 = "";
 
     llvm::raw_string_ostream ostr(intermediate);
     Output yout(ostr);
@@ -327,6 +348,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) {
   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
   EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
   EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
+  EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
+  EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
+  EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
+  EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
+  EXPECT_NE(std::string::npos, flowOut.find("''\n"));
 
   {
     Input yin(intermediate);
@@ -339,6 +365,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) {
     EXPECT_TRUE(map.str3.equals("`ccc"));
     EXPECT_TRUE(map.str4.equals("@ddd"));
     EXPECT_TRUE(map.str5.equals(""));
+    EXPECT_TRUE(map.stdstr1 == "'eee");
+    EXPECT_TRUE(map.stdstr2 == "\"fff");
+    EXPECT_TRUE(map.stdstr3 == "`ggg");
+    EXPECT_TRUE(map.stdstr4 == "@hhh");
+    EXPECT_TRUE(map.stdstr5 == "");
   }
 }