diff --git a/libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp b/libcxx/test/pretty_printers/gdb_pretty_printer_test.sh.cpp --- 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 @@ "\"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(); diff --git a/libcxx/utils/gdb/libcxx/printers.py b/libcxx/utils/gdb/libcxx/printers.py --- a/libcxx/utils/gdb/libcxx/printers.py +++ b/libcxx/utils/gdb/libcxx/printers.py @@ -69,6 +69,8 @@ _common_substitutions = [ ("std::basic_string, std::allocator >", "std::string"), + ("std::basic_string_view >", + "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,