llvm.org GIT mirror llvm / 649e247
[gdb] Add pretty printer for Expected Reviewers: dblaikie Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D48512 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335554 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 2 years ago
1 changed file(s) with 50 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
0 import gdb.printing
1
2 class Iterator:
3 def __iter__(self):
4 return self
5
6 # Python 2 compatibility
7 def next(self):
8 return self.__next__()
9
10 def children(self):
11 return self
12
13
114 class SmallStringPrinter:
215 """Print an llvm::SmallString object."""
316
102115 def display_hint (self):
103116 return 'array'
104117
105 class OptionalPrinter:
118 class ExpectedPrinter(Iterator):
119 """Print an llvm::Expected object."""
120
121 def __init__(self, val):
122 self.val = val
123
124 def __next__(self):
125 val = self.val
126 if val is None:
127 raise StopIteration
128 self.val = None
129 if val['HasError']:
130 return ('error', val['ErrorStorage'].address.cast(
131 gdb.lookup_type('llvm::ErrorInfoBase').pointer()).dereference())
132 return ('value', val['TStorage'].address.cast(
133 val.type.template_argument(0).pointer()).dereference())
134
135 def to_string(self):
136 return 'llvm::Expected{}'.format(' is error' if self.val['HasError'] else '')
137
138 class OptionalPrinter(Iterator):
106139 """Print an llvm::Optional object."""
107140
108 def __init__(self, value):
109 self.value = value
110
111 class _iterator:
112 def __init__(self, member, empty):
113 self.member = member
114 self.done = empty
115
116 def __iter__(self):
117 return self
118
119 def next(self):
120 if self.done:
121 raise StopIteration
122 self.done = True
123 return ('value', self.member.dereference())
124
125 def children(self):
126 if not self.value['Storage']['hasVal']:
127 return self._iterator('', True)
128 return self._iterator(self.value['Storage']['storage']['buffer'].address.cast(
129 self.value.type.template_argument(0).pointer()), False)
130
131 def to_string(self):
132 return 'llvm::Optional is %sinitialized' % ('' if self.value['Storage']['hasVal'] else 'not ')
141 def __init__(self, val):
142 self.val = val
143
144 def __next__(self):
145 val = self.val
146 if val is None:
147 raise StopIteration
148 self.val = None
149 if not val['Storage']['hasVal']:
150 raise StopIteration
151 return ('value', val['Storage']['storage']['buffer'].address.cast(
152 val.type.template_argument(0).pointer()).dereference())
153
154 def to_string(self):
155 return 'llvm::Optional{}'.format('' if self.val['Storage']['hasVal'] else ' is not initialized')
133156
134157 class DenseMapPrinter:
135158 "Print a DenseMap"
314337 pp.add_printer('llvm::StringRef', '^llvm::StringRef$', StringRefPrinter)
315338 pp.add_printer('llvm::SmallVectorImpl', '^llvm::SmallVector(Impl)?<.*>$', SmallVectorPrinter)
316339 pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter)
340 pp.add_printer('llvm::Expected', '^llvm::Expected<.*>$', ExpectedPrinter)
317341 pp.add_printer('llvm::Optional', '^llvm::Optional<.*>$', OptionalPrinter)
318342 pp.add_printer('llvm::DenseMap', '^llvm::DenseMap<.*>$', DenseMapPrinter)
319343 pp.add_printer('llvm::Twine', '^llvm::Twine$', TwinePrinter)