Fix some issues with the gdb pretty printers for llvm::Twine

Still some pending bugs, but at least ironed some things out.
This commit is contained in:
David Blaikie 2021-11-04 20:45:29 -07:00
parent 1606022fab
commit def232915f
3 changed files with 26 additions and 17 deletions

View File

@ -24,7 +24,12 @@ llvm::Optional<int> OptionalNone(llvm::None);
llvm::SmallVector<int, 5> SmallVector = {10, 11, 12};
llvm::SmallString<5> SmallString("foo");
llvm::StringRef StringRef = "bar";
llvm::Twine Twine = llvm::Twine(SmallString) + StringRef;
// Should test std::string in Twine too, but it's currently broken because I
// don't know how to add 'str' and 'gdb.LazyString' (can't figure out any way to
// string-ify LazyString).
//std::string String = "foo";
llvm::Twine TempTwine = llvm::Twine(3) + StringRef;
llvm::Twine Twine = TempTwine + "baz";
llvm::PointerIntPair<int *, 1> PointerIntPair(IntPtr, 1);
struct alignas(8) Z {};

View File

@ -37,7 +37,7 @@ p SmallString
# CHECK: "bar"
p StringRef
# CHECK: "foobar"
# CHECK: "3barbaz"
p Twine
# CHECK: llvm::StringMap with 2 elements = {["foo"] = 123, ["bar"] = 456}

View File

@ -16,9 +16,6 @@ class Iterator:
def children(self):
return self
def escape_bytes(val, l):
return '"' + val.string(encoding='Latin-1', length=l).encode('unicode_escape').decode() + '"'
class SmallStringPrinter:
"""Print an llvm::SmallString object."""
@ -26,8 +23,12 @@ class SmallStringPrinter:
self.val = val
def to_string(self):
begin = self.val['BeginX']
return escape_bytes(begin.cast(gdb.lookup_type('char').pointer()), self.val['Size'])
data = self.val['BeginX'].cast(gdb.lookup_type('char').pointer())
length = self.val['Size']
return data.lazy_string(length=length)
def display_hint (self):
return 'string'
class StringRefPrinter:
"""Print an llvm::StringRef object."""
@ -36,7 +37,12 @@ class StringRefPrinter:
self.val = val
def to_string(self):
return escape_bytes(self.val['Data'], self.val['Length'])
data = self.val['Data']
length = self.val['Length']
return data.lazy_string(length=length)
def display_hint(self):
return 'string'
class SmallVectorPrinter(Iterator):
"""Print an llvm::SmallVector object."""
@ -300,12 +306,9 @@ class TwinePrinter:
if self.is_twine_kind(kind, 'PtrAndLengthKind'):
val = child['ptrAndLength']
return val['ptr'].string(encoding='Latin-1', length=val['length']).encode('unicode_escape').decode()
if self.is_twine_kind(kind, 'SmallStringKind'):
val = child['smallString'].dereference()
pp = SmallStringPrinter(val)
return pp.to_string()
data = val['ptr']
length = val['length']
return data.string(length=length)
if self.is_twine_kind(kind, 'CharKind'):
return chr(child['character'])
@ -340,11 +343,9 @@ class TwinePrinter:
def string_from_twine_object(self, twine):
'''Return the string representation of the Twine object twine.'''
lhs_str = ''
rhs_str = ''
lhs = twine['LHS']
rhs = twine['RHS']
lhs_kind = str(twine['LHSKind'])
rhs_kind = str(twine['RHSKind'])
@ -356,6 +357,9 @@ class TwinePrinter:
def to_string(self):
return self.string_from_twine_object(self._val)
def display_hint(self):
return 'string'
def get_pointer_int_pair(val):
"""Get tuple from llvm::PointerIntPair."""
info_name = val.type.template_argument(4).strip_typedefs().name