[PATCH] D30994: Add pretty-printer for llvm::Twine type

Simon Marchi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 15 13:16:27 PDT 2017


simark marked an inline comment as done.
simark added inline comments.


================
Comment at: utils/gdb-scripts/prettyprinters.py:205-219
+  def string_from_pretty_printer_lookup(self, val):
+    '''Lookup the default pretty-printer for val and use it.
+
+    If no pretty-printer is defined for the type of val, print an error and
+    return a placeholder string.'''
+
+    pp = gdb.default_visualizer(val)
----------------
dblaikie wrote:
> This seems more complicated than I would've expected, is str(val) insufficient?
That's what I did first. Actually, it was `s = str(val)[1:-1]`, because the return of `str(val)` contains quotes.

But I wanted to give a better experience (a good error message) when the std::string pretty printer is not loaded (which can happen easily, as they're a bit of a pain to set up sometimes).  Otherwise, you get something like this as part of the resulting string:

    "foostatic npos = <optimized out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x318e1f8 \"bar\"}baz"

Instead, the goal is to print a warning:

    (gdb) print twine
    $1 = No pretty printer for std::string found. The resulting Twine representation will be incomplete.
    "foo<std::string>baz"


What's broken is the

    self.string_from_pretty_printer_lookup(val).value().address.string()

I do lower.  I added that because the std::string pretty printer returns a gdb.LazyString, and that's the only way I've found to convert it to a Python string.  However, it doesn't work when we return the placeholder `<std::string>`.

The other thing is that a `<std::string>` placeholder looks very much like a C++ template, so it's probably not a very good choice.  I wanted to add something to the string to make the user realize that something should be there but is missing, but that format probably looks too much like some "valid" content.


Repository:
  rL LLVM

https://reviews.llvm.org/D30994





More information about the llvm-commits mailing list