Commit b4c911ec authored by Eric Fiselier's avatar Eric Fiselier
Browse files

[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@google.com)
Reviewed as https://reviews.llvm.org/D73514
parent aa6ec19c
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -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();
+29 −0
Original line number Diff line number Diff line
@@ -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 @@ class StdStringPrinter(object):
        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 @@ class LibcxxPrettyPrinter(object):
        self.lookup = {
            "basic_string": StdStringPrinter,
            "string": StdStringPrinter,
            "string_view": StdStringViewPrinter,
            "tuple": StdTuplePrinter,
            "unique_ptr": StdUniquePtrPrinter,
            "shared_ptr": StdSharedPointerPrinter,