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

Eric Fiselier via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 29 10:04:40 PST 2020


Author: Eric Fiselier
Date: 2020-01-29T13:04:29-05:00
New Revision: b4c911eccc4cccdd8cb536dc003ff9d4eb3bdc70

URL: https://github.com/llvm/llvm-project/commit/b4c911eccc4cccdd8cb536dc003ff9d4eb3bdc70
DIFF: https://github.com/llvm/llvm-project/commit/b4c911eccc4cccdd8cb536dc003ff9d4eb3bdc70.diff

LOG: [libcxx] Add a std::string_view pretty printer for libcxx.

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

Patch by Ali Tamur (tamur at google.com)
Reviewed as https://reviews.llvm.org/D73514

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp b/libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
index 138af7e74bf1..3050cf15f35a 100644
--- a/libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
+++ b/libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp
@@ -153,6 +153,21 @@ void string_test() {
                             "\"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 @@ int main(int argc, char* argv[]) {
   framework_self_test();
 
   string_test();
+  string_view_test();
 
   u32string_test();
   tuple_test();

diff  --git a/libcxx/utils/gdb/libcxx/printers.py b/libcxx/utils/gdb/libcxx/printers.py
index b2d589424a38..4b724c94368d 100644
--- a/libcxx/utils/gdb/libcxx/printers.py
+++ b/libcxx/utils/gdb/libcxx/printers.py
@@ -69,6 +69,8 @@ def _remove_generics(typename):
 _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 @@ def display_hint(self):
         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 @@ def __init__(self, name):
         self.lookup = {
             "basic_string": StdStringPrinter,
             "string": StdStringPrinter,
+            "string_view": StdStringViewPrinter,
             "tuple": StdTuplePrinter,
             "unique_ptr": StdUniquePtrPrinter,
             "shared_ptr": StdSharedPointerPrinter,


        


More information about the libcxx-commits mailing list