[PATCH] Add std::string to YAML I/O built-in types

John Thompson john.thompson.jtsoftware at gmail.com
Wed Oct 23 11:41:38 PDT 2013


Hi kledzik, silvas,

I ran into a need for this in writing a new tool with YAML output where I store strings in objects using std:string.  It seemed like there should be support for std::string, so with Sean Silva's help, here's a patch for it.  Please let me know if someone else should be reviewing it.

Thanks.

-John


http://llvm-reviews.chandlerc.com/D2006

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

Index: include/llvm/Support/YAMLTraits.h
===================================================================
--- include/llvm/Support/YAMLTraits.h
+++ include/llvm/Support/YAMLTraits.h
@@ -1,4 +1,4 @@
-//===- llvm/Supporrt/YAMLTraits.h -------------------------------*- C++ -*-===//
+//===- llvm/Support/YAMLTraits.h -------------------------------*- C++ -*-===//
 //
 //                             The LLVM Linker
 //
@@ -539,6 +539,12 @@
 };
 
 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> {
   static void output(const uint8_t &, void*, llvm::raw_ostream &);
   static StringRef input(StringRef, void*, uint8_t &);
Index: lib/Support/YAMLTraits.cpp
===================================================================
--- lib/Support/YAMLTraits.cpp
+++ lib/Support/YAMLTraits.cpp
@@ -648,6 +648,17 @@
   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) {
   // use temp uin32_t because ostream thinks uint8_t is a character
Index: unittests/Support/YAMLIOTest.cpp
===================================================================
--- unittests/Support/YAMLIOTest.cpp
+++ unittests/Support/YAMLIOTest.cpp
@@ -130,6 +130,7 @@
 
 struct BuiltInTypes {
   llvm::StringRef str;
+  std::string stdstr;
   uint64_t        u64;
   uint32_t        u32;
   uint16_t        u16;
@@ -153,6 +154,7 @@
   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);
@@ -181,6 +183,7 @@
   BuiltInTypes map;
   Input yin("---\n"
             "str:      hello there\n"
+            "stdstr:   hello where?\n"
             "u64:      5000000000\n"
             "u32:      4000000000\n"
             "u16:      65000\n"
@@ -201,6 +204,7 @@
 
   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);
@@ -227,6 +231,7 @@
   {
     BuiltInTypes map;
     map.str = "one two";
+    map.stdstr = "three four";
     map.u64 = 6000000000ULL;
     map.u32 = 3000000000U;
     map.u16 = 50000;
@@ -255,6 +260,7 @@
 
     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);
@@ -279,6 +285,11 @@
   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 {
@@ -291,6 +302,11 @@
       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);
     }
   };
 }
@@ -305,6 +321,11 @@
     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);
@@ -317,6 +338,11 @@
   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);
@@ -329,6 +355,11 @@
     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 == "");
   }
 }
 
Index: docs/YamlIO.rst
===================================================================
--- docs/YamlIO.rst
+++ docs/YamlIO.rst
@@ -234,6 +234,7 @@
 * float
 * double
 * StringRef
+* std::string
 * int64_t
 * int32_t
 * int16_t
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2006.1.patch
Type: text/x-patch
Size: 5367 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131023/dd906346/attachment.bin>


More information about the llvm-commits mailing list