[libcxx-commits] [PATCH] D73514: [libcxx] Add a std::string_view pretty printer for libcxx.

Ali Tamur via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 27 17:24:41 PST 2020


tamur created this revision.
tamur added a project: libc++.
Herald added a reviewer: EricWF.
Herald added subscribers: libcxx-commits, ldionne, christof.
Herald added a reviewer: mclow.lists.

This adds a std::string_view pretty printer for libcxx and updates the gdb pretty printer test.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73514

Files:
  libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
  libcxx/utils/gdb/libcxx/printers.py


Index: libcxx/utils/gdb/libcxx/printers.py
===================================================================
--- libcxx/utils/gdb/libcxx/printers.py
+++ libcxx/utils/gdb/libcxx/printers.py
@@ -69,6 +69,8 @@
 _common_substitutions = [
     ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
      "std::string"),
+    ("std::basic_string_view<char, std::char_traits<char> >",
+     "std::string_view"),
 ]
 
 
@@ -243,6 +245,32 @@
         return "string"
 
 
+class StdStringViewPrinter(object):
+    """Print a std::string_view."""
+
+    def __init__(self, val):
+      self.val = val
+
+    def to_string(self):  # pylint: disable=g-bad-name
+      """GDB calls this to compute the pretty-printed form."""
+
+      ptr = self.val["__data"]
+      length = self.val["__size"]
+      print_length = length
+      # We print more than just a simple string (i.e. we also print
+      # "of length %d").  Thus we can't use the "string" display_hint,
+      # and thus we have to handle "print elements" ourselves.
+      # For reference sake, gdb ensures limit == None or limit > 0.
+      limit = gdb.parameter("print elements")
+      if limit is not None:
+        print_length = min(print_length, limit)
+      # FIXME: Passing ISO-8859-1 here isn't always correct.
+      string = ptr.string("ISO-8859-1", "ignore", print_length)
+      if length > print_length:
+        string += "..."
+      return "std::string_view of length %d: \"%s\"" % (length, string)
+
+
 class StdUniquePtrPrinter(object):
     """Print a std::unique_ptr."""
 
@@ -904,6 +932,7 @@
         self.lookup = {
             "basic_string": StdStringPrinter,
             "string": StdStringPrinter,
+            "string_view": StdStringViewPrinter,
             "tuple": StdTuplePrinter,
             "unique_ptr": StdUniquePtrPrinter,
             "shared_ptr": StdSharedPointerPrinter,
Index: libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
===================================================================
--- libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
+++ libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
@@ -153,6 +153,21 @@
                             "\"mehmet bizim dostumuz agzi kirik testimiz\"");
 }
 
+void string_view_test() {
+  std::string_view i_am_empty;
+  ComparePrettyPrintToChars(i_am_empty, "std::string_view of length 0: \"\"");
+
+  std::string source_string("to be or not to be");
+  std::string_view to_be(source_string);
+  ComparePrettyPrintToChars(
+      to_be, "std::string_view of length 18: \"to be or not to be\"");
+
+  const char char_arr[] = "what a wonderful world";
+  std::string_view wonderful(&char_arr[7], 9);
+  ComparePrettyPrintToChars(
+      wonderful, "std::string_view of length 9: \"wonderful\"");
+}
+
 void u16string_test() {
   std::u16string test0 = u"Hello World";
   ComparePrettyPrintToChars(test0, "u\"Hello World\"");
@@ -613,6 +628,7 @@
   framework_self_test();
 
   string_test();
+  string_view_test();
 
   u32string_test();
   tuple_test();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73514.240738.patch
Type: text/x-patch
Size: 3049 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200128/467b4ef0/attachment-0001.bin>


More information about the libcxx-commits mailing list