llvm.org GIT mirror llvm / ed2188e
Reference gdb python type printer scripts from SmallString, SmallVector, ArrayRef, and StringRef git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271357 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 3 years ago
1 changed file(s) with 107 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 import gdb.printing
1 class SmallStringPrinter:
2 """Print an llvm::SmallString object."""
3
4 def __init__(self, val):
5 self.val = val
6
7 def to_string(self):
8 begin = self.val['BeginX']
9 end = self.val['EndX']
10 return begin.cast(gdb.lookup_type("char").pointer()).string(length = end - begin)
11
12 def display_hint (self):
13 return 'string'
14
15 class StringRefPrinter:
16 """Print an llvm::StringRef object."""
17
18 def __init__(self, val):
19 self.val = val
20
21 def to_string(self):
22 return self.val['Data'].string(length = self.val['Length'])
23
24 def display_hint (self):
25 return 'string'
26
27 class SmallVectorPrinter:
28 """Print an llvm::SmallVector object."""
29
30 class _iterator:
31 def __init__(self, begin, end):
32 self.cur = begin
33 self.end = end
34 self.count = 0
35
36 def __iter__(self):
37 return self
38
39 def next(self):
40 if self.cur == self.end:
41 raise StopIteration
42 count = self.count
43 self.count = self.count + 1
44 cur = self.cur
45 self.cur = self.cur + 1
46 return '[%d]' % count, cur.dereference()
47
48 def __init__(self, val):
49 self.val = val
50
51 def children(self):
52 t = self.val.type.template_argument(0).pointer()
53 begin = self.val['BeginX'].cast(t)
54 end = self.val['EndX'].cast(t)
55 return self._iterator(begin, end)
56
57 def to_string(self):
58 t = self.val.type.template_argument(0).pointer()
59 begin = self.val['BeginX'].cast(t)
60 end = self.val['EndX'].cast(t)
61 capacity = self.val['CapacityX'].cast(t)
62 return 'llvm::SmallVector of length %d, capacity %d' % (end - begin, capacity - begin)
63
64 def display_hint (self):
65 return 'array'
66
67 class ArrayRefPrinter:
68 """Print an llvm::ArrayRef object."""
69
70 class _iterator:
71 def __init__(self, begin, end):
72 self.cur = begin
73 self.end = end
74 self.count = 0
75
76 def __iter__(self):
77 return self
78
79 def next(self):
80 if self.cur == self.end:
81 raise StopIteration
82 count = self.count
83 self.count = self.count + 1
84 cur = self.cur
85 self.cur = self.cur + 1
86 return '[%d]' % count, cur.dereference()
87
88 def __init__(self, val):
89 self.val = val
90
91 def children(self):
92 data = self.val['Data']
93 return self._iterator(data, data + self.val['Length'])
94
95 def to_string(self):
96 return 'llvm::ArrayRef of length %d' % (self.val['Length'])
97
98 def display_hint (self):
99 return 'array'
100
101 pp = gdb.printing.RegexpCollectionPrettyPrinter("LLVMSupport")
102 pp.add_printer('llvm::SmallString', '^llvm::SmallString<.*>$', SmallStringPrinter)
103 pp.add_printer('llvm::StringRef', '^llvm::StringRef$', StringRefPrinter)
104 pp.add_printer('llvm::SmallVectorImpl', '^llvm::SmallVector(Impl)?<.*>$', SmallVectorPrinter)
105 pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter)
106 gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)