Skip to content

Commit f3ad6eb

Browse files
committed
Change to individual pretty printer classes, remove generic make_printer.
Summary: Follow-up from D72589. Reviewers: dblaikie Reviewed By: dblaikie Subscribers: merge_guards_bot, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D73609
1 parent b94d4b1 commit f3ad6eb

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ llvm::SmallString<5> SmallString("foo");
2424
llvm::StringRef StringRef = "bar";
2525
llvm::Twine Twine = llvm::Twine(SmallString) + StringRef;
2626
llvm::PointerIntPair<int *, 1> PointerIntPair(IntPtr, 1);
27-
llvm::PointerUnion<float *, int *> PointerUnion(IntPtr);
27+
28+
struct alignas(8) Z {};
29+
llvm::PointerUnion<Z *, int *> PointerUnion(IntPtr);
30+
31+
// No members which instantiate PointerUnionUIntTraits<Z *> (e.g. get<T *>())
32+
// are called, and this instance will therefore be raw-printed.
33+
llvm::PointerUnion<Z *, float *> RawPrintingPointerUnion(nullptr);
2834

2935
using IlistTag = llvm::ilist_tag<struct A>;
3036
using SimpleIlistTag = llvm::ilist_tag<struct B>;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: gdb -q -batch -n -iex 'source %llvm_src_root/utils/gdb-scripts/prettyprinters.py' -x %s %llvm_tools_dir/check-gdb-llvm-support | FileCheck %s
1+
# RUN: gdb -q -batch -n -iex 'source %llvm_src_root/utils/gdb-scripts/prettyprinters.py' -x %s %llvm_tools_dir/check-gdb-llvm-support | FileCheck %s --dump-input-on-failure
22

33
break main
44
run
@@ -39,12 +39,15 @@ p StringRef
3939
# CHECK: "\"foo\"\"bar\""
4040
p Twine
4141

42-
# CHECK: llvm::PointerIntPair<int *> = {pointer = 0xabc, value = 1}
42+
# CHECK: {pointer = 0xabc, value = 1}
4343
p PointerIntPair
4444

45-
# CHECK: llvm::PointerUnion containing int * = {pointer = 0xabc}
45+
# CHECK: Containing int * = {pointer = 0xabc}
4646
p PointerUnion
4747

48+
# CHECK: PointerUnionMembers<llvm::PointerUnion<Z*, float*>,
49+
p RawPrintingPointerUnion
50+
4851
# Switch to print pretty adds newlines to the following statements.
4952
set print pretty
5053

llvm/utils/gdb-scripts/prettyprinters.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -316,25 +316,12 @@ def string_from_twine_object(self, twine):
316316
def to_string(self):
317317
return self.string_from_twine_object(self._val)
318318

319-
def make_printer(string = None, children = None, hint = None):
320-
"""Create a printer from the parameters."""
321-
class Printer : pass
322-
printer = Printer()
323-
if string:
324-
setattr(printer, 'to_string', lambda: string)
325-
if children:
326-
setattr(printer, 'children', lambda: children)
327-
if hint:
328-
setattr(printer, 'display_hint', lambda: hint)
329-
return printer
330-
331319
def get_pointer_int_pair(val):
332320
"""Get tuple from llvm::PointerIntPair."""
333321
info_name = val.type.template_argument(4).strip_typedefs().name
334-
try:
335-
enum_type = gdb.lookup_type(info_name + '::MaskAndShiftConstants')
336-
except gdb.error:
337-
return (None, None)
322+
# Note: this throws a gdb.error if the info type is not used (by means of a
323+
# call to getPointer() or similar) in the current translation unit.
324+
enum_type = gdb.lookup_type(info_name + '::MaskAndShiftConstants')
338325
enum_dict = gdb.types.make_enum_dict(enum_type)
339326
ptr_mask = enum_dict[info_name + '::PointerBitMask']
340327
int_shift = enum_dict[info_name + '::IntShift']
@@ -344,26 +331,48 @@ def get_pointer_int_pair(val):
344331
value = ((pair_union >> int_shift) & int_mask)
345332
return (pointer, value)
346333

334+
class PointerIntPairPrinter:
335+
"""Print a PointerIntPair."""
336+
337+
def __init__(self, pointer, value):
338+
self.pointer = pointer
339+
self.value = value
340+
341+
def children(self):
342+
yield ('pointer', self.pointer)
343+
yield ('value', self.value)
344+
347345
def make_pointer_int_pair_printer(val):
348346
"""Factory for an llvm::PointerIntPair printer."""
349-
pointer, value = get_pointer_int_pair(val)
350-
if not pointer or not value:
351-
return None
347+
try:
348+
pointer, value = get_pointer_int_pair(val)
349+
except gdb.error:
350+
return None # If PointerIntPair cannot be analyzed, print as raw value.
352351
pointer_type = val.type.template_argument(0)
353352
value_type = val.type.template_argument(2)
354-
string = 'llvm::PointerIntPair<%s>' % pointer_type
355-
children = [('pointer', pointer.cast(pointer_type)),
356-
('value', value.cast(value_type))]
357-
return make_printer(string, children)
353+
return PointerIntPairPrinter(pointer.cast(pointer_type),
354+
value.cast(value_type))
355+
356+
class PointerUnionPrinter:
357+
"""Print a PointerUnion."""
358+
359+
def __init__(self, pointer):
360+
self.pointer = pointer
361+
362+
def children(self):
363+
yield ('pointer', self.pointer)
364+
365+
def to_string(self):
366+
return "Containing %s" % self.pointer.type
358367

359368
def make_pointer_union_printer(val):
360369
"""Factory for an llvm::PointerUnion printer."""
361-
pointer, value = get_pointer_int_pair(val['Val'])
362-
if not pointer or not value:
363-
return None
370+
try:
371+
pointer, value = get_pointer_int_pair(val['Val'])
372+
except gdb.error:
373+
return None # If PointerIntPair cannot be analyzed, print as raw value.
364374
pointer_type = val.type.template_argument(int(value))
365-
string = 'llvm::PointerUnion containing %s' % pointer_type
366-
return make_printer(string, [('pointer', pointer.cast(pointer_type))])
375+
return PointerUnionPrinter(pointer.cast(pointer_type))
367376

368377
class IlistNodePrinter:
369378
"""Print an llvm::ilist_node object."""

0 commit comments

Comments
 (0)