Skip to content

Commit def2329

Browse files
committed
Fix some issues with the gdb pretty printers for llvm::Twine
Still some pending bugs, but at least ironed some things out.
1 parent 1606022 commit def2329

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

cross-project-tests/debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ llvm::Optional<int> OptionalNone(llvm::None);
2424
llvm::SmallVector<int, 5> SmallVector = {10, 11, 12};
2525
llvm::SmallString<5> SmallString("foo");
2626
llvm::StringRef StringRef = "bar";
27-
llvm::Twine Twine = llvm::Twine(SmallString) + StringRef;
27+
// Should test std::string in Twine too, but it's currently broken because I
28+
// don't know how to add 'str' and 'gdb.LazyString' (can't figure out any way to
29+
// string-ify LazyString).
30+
//std::string String = "foo";
31+
llvm::Twine TempTwine = llvm::Twine(3) + StringRef;
32+
llvm::Twine Twine = TempTwine + "baz";
2833
llvm::PointerIntPair<int *, 1> PointerIntPair(IntPtr, 1);
2934

3035
struct alignas(8) Z {};

cross-project-tests/debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.gdb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ p SmallString
3737
# CHECK: "bar"
3838
p StringRef
3939

40-
# CHECK: "foobar"
40+
# CHECK: "3barbaz"
4141
p Twine
4242

4343
# CHECK: llvm::StringMap with 2 elements = {["foo"] = 123, ["bar"] = 456}

llvm/utils/gdb-scripts/prettyprinters.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ def next(self):
1616
def children(self):
1717
return self
1818

19-
def escape_bytes(val, l):
20-
return '"' + val.string(encoding='Latin-1', length=l).encode('unicode_escape').decode() + '"'
21-
2219
class SmallStringPrinter:
2320
"""Print an llvm::SmallString object."""
2421

2522
def __init__(self, val):
2623
self.val = val
2724

2825
def to_string(self):
29-
begin = self.val['BeginX']
30-
return escape_bytes(begin.cast(gdb.lookup_type('char').pointer()), self.val['Size'])
26+
data = self.val['BeginX'].cast(gdb.lookup_type('char').pointer())
27+
length = self.val['Size']
28+
return data.lazy_string(length=length)
29+
30+
def display_hint (self):
31+
return 'string'
3132

3233
class StringRefPrinter:
3334
"""Print an llvm::StringRef object."""
@@ -36,7 +37,12 @@ def __init__(self, val):
3637
self.val = val
3738

3839
def to_string(self):
39-
return escape_bytes(self.val['Data'], self.val['Length'])
40+
data = self.val['Data']
41+
length = self.val['Length']
42+
return data.lazy_string(length=length)
43+
44+
def display_hint(self):
45+
return 'string'
4046

4147
class SmallVectorPrinter(Iterator):
4248
"""Print an llvm::SmallVector object."""
@@ -300,12 +306,9 @@ def string_from_child(self, child, kind):
300306

301307
if self.is_twine_kind(kind, 'PtrAndLengthKind'):
302308
val = child['ptrAndLength']
303-
return val['ptr'].string(encoding='Latin-1', length=val['length']).encode('unicode_escape').decode()
304-
305-
if self.is_twine_kind(kind, 'SmallStringKind'):
306-
val = child['smallString'].dereference()
307-
pp = SmallStringPrinter(val)
308-
return pp.to_string()
309+
data = val['ptr']
310+
length = val['length']
311+
return data.string(length=length)
309312

310313
if self.is_twine_kind(kind, 'CharKind'):
311314
return chr(child['character'])
@@ -340,11 +343,9 @@ def string_from_child(self, child, kind):
340343
def string_from_twine_object(self, twine):
341344
'''Return the string representation of the Twine object twine.'''
342345

343-
lhs_str = ''
344-
rhs_str = ''
345-
346346
lhs = twine['LHS']
347347
rhs = twine['RHS']
348+
348349
lhs_kind = str(twine['LHSKind'])
349350
rhs_kind = str(twine['RHSKind'])
350351

@@ -356,6 +357,9 @@ def string_from_twine_object(self, twine):
356357
def to_string(self):
357358
return self.string_from_twine_object(self._val)
358359

360+
def display_hint(self):
361+
return 'string'
362+
359363
def get_pointer_int_pair(val):
360364
"""Get tuple from llvm::PointerIntPair."""
361365
info_name = val.type.template_argument(4).strip_typedefs().name

0 commit comments

Comments
 (0)