[PATCH] D79745: [YAMLTraits] Add trait for char

Jonas Devlieghere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 11 16:13:37 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rGfab08bf4899e: [YAMLTraits] Add trait for char (authored by JDevlieghere).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79745/new/

https://reviews.llvm.org/D79745

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


Index: llvm/unittests/Support/YAMLIOTest.cpp
===================================================================
--- llvm/unittests/Support/YAMLIOTest.cpp
+++ llvm/unittests/Support/YAMLIOTest.cpp
@@ -333,6 +333,7 @@
   uint16_t        u16;
   uint8_t         u8;
   bool            b;
+  char            c;
   int64_t         s64;
   int32_t         s32;
   int16_t         s16;
@@ -357,6 +358,7 @@
       io.mapRequired("u16",      bt.u16);
       io.mapRequired("u8",       bt.u8);
       io.mapRequired("b",        bt.b);
+      io.mapRequired("c",        bt.c);
       io.mapRequired("s64",      bt.s64);
       io.mapRequired("s32",      bt.s32);
       io.mapRequired("s16",      bt.s16);
@@ -386,6 +388,7 @@
             "u16:      65000\n"
             "u8:       255\n"
             "b:        false\n"
+            "c:        'c'\n"
             "s64:      -5000000000\n"
             "s32:      -2000000000\n"
             "s16:      -32000\n"
@@ -396,7 +399,7 @@
             "h16:      0x8765\n"
             "h32:      0xFEDCBA98\n"
             "h64:      0xFEDCBA9876543210\n"
-           "...\n");
+            "...\n");
   yin >> map;
 
   EXPECT_FALSE(yin.error());
@@ -407,6 +410,7 @@
   EXPECT_EQ(map.u16, 65000);
   EXPECT_EQ(map.u8,  255);
   EXPECT_EQ(map.b,   false);
+  EXPECT_EQ(map.c,   'c');
   EXPECT_EQ(map.s64, -5000000000LL);
   EXPECT_EQ(map.s32, -2000000000L);
   EXPECT_EQ(map.s16, -32000);
@@ -434,6 +438,7 @@
     map.u16 = 50000;
     map.u8  = 254;
     map.b   = true;
+    map.c   = 'd';
     map.s64 = -6000000000LL;
     map.s32 = -2000000000;
     map.s16 = -32000;
@@ -463,6 +468,7 @@
     EXPECT_EQ(map.u16,      50000);
     EXPECT_EQ(map.u8,       254);
     EXPECT_EQ(map.b,        true);
+    EXPECT_EQ(map.c,        'd');
     EXPECT_EQ(map.s64,      -6000000000LL);
     EXPECT_EQ(map.s32,      -2000000000L);
     EXPECT_EQ(map.s16,      -32000);
Index: llvm/lib/Support/YAMLTraits.cpp
===================================================================
--- llvm/lib/Support/YAMLTraits.cpp
+++ llvm/lib/Support/YAMLTraits.cpp
@@ -864,6 +864,17 @@
   return "invalid boolean";
 }
 
+void ScalarTraits<char>::output(const char &Val, void *, raw_ostream &Out) {
+  Out << Val;
+}
+
+StringRef ScalarTraits<char>::input(StringRef Scalar, void *, char &Val) {
+  if (Scalar.size() != 1)
+    return "invalid character";
+  Val = Scalar[0];
+  return StringRef();
+}
+
 void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
                                      raw_ostream &Out) {
   Out << Val;
Index: llvm/include/llvm/Support/YAMLTraits.h
===================================================================
--- llvm/include/llvm/Support/YAMLTraits.h
+++ llvm/include/llvm/Support/YAMLTraits.h
@@ -1159,6 +1159,12 @@
   static QuotingType mustQuote(StringRef) { return QuotingType::None; }
 };
 
+template <> struct ScalarTraits<char> {
+  static void output(const char &, void *, raw_ostream &);
+  static StringRef input(StringRef, void *, char &);
+  static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
+};
+
 template<>
 struct ScalarTraits<StringRef> {
   static void output(const StringRef &, void *, raw_ostream &);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79745.263304.patch
Type: text/x-patch
Size: 3205 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200511/d82142b6/attachment.bin>


More information about the llvm-commits mailing list