llvm.org GIT mirror llvm / 312b28e
Add GDB pretty-printer for llvm::Twine type Patch by Simon Marchi! Differential Revision: https://reviews.llvm.org/D30994 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297889 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 2 years ago
1 changed file(s) with 108 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
192192 def display_hint(self):
193193 return 'map'
194194
195 class TwinePrinter:
196 "Print a Twine"
197
198 def __init__(self, val):
199 self._val = val
200
201 def display_hint(self):
202 return 'string'
203
204 def string_from_pretty_printer_lookup(self, val):
205 '''Lookup the default pretty-printer for val and use it.
206
207 If no pretty-printer is defined for the type of val, print an error and
208 return a placeholder string.'''
209
210 pp = gdb.default_visualizer(val)
211 if pp:
212 s = pp.to_string()
213
214 # The pretty-printer may return a LazyString instead of an actual Python
215 # string. Convert it to a Python string. However, GDB doesn't seem to
216 # register the LazyString type, so we can't check
217 # "type(s) == gdb.LazyString".
218 if 'LazyString' in type(s).__name__:
219 s = s.value().address.string()
220
221 else:
222 print(('No pretty printer for {} found. The resulting Twine ' +
223 'representation will be incomplete.').format(val.type.name))
224 s = '(missing {})'.format(val.type.name)
225
226 return s
227
228 def string_from_child(self, child, kind):
229 '''Return the string representation of the Twine::Child child.'''
230
231 if kind in ('llvm::Twine::EmptyKind', 'llvm::Twine::NullKind'):
232 return ''
233
234 if kind == 'llvm::Twine::TwineKind':
235 return self.string_from_twine_object(child['twine'].dereference())
236
237 if kind == 'llvm::Twine::CStringKind':
238 return child['cString'].string()
239
240 if kind == 'llvm::Twine::StdStringKind':
241 val = child['stdString'].dereference()
242 return self.string_from_pretty_printer_lookup(val)
243
244 if kind == 'llvm::Twine::StringRefKind':
245 val = child['stringRef'].dereference()
246 pp = StringRefPrinter(val)
247 return pp.to_string()
248
249 if kind == 'llvm::Twine::SmallStringKind':
250 val = child['smallString'].dereference()
251 pp = SmallStringPrinter(val)
252 return pp.to_string()
253
254 if kind == 'llvm::Twine::CharKind':
255 return chr(child['character'])
256
257 if kind == 'llvm::Twine::DecUIKind':
258 return str(child['decUI'])
259
260 if kind == 'llvm::Twine::DecIKind':
261 return str(child['decI'])
262
263 if kind == 'llvm::Twine::DecULKind':
264 return str(child['decUL'].dereference())
265
266 if kind == 'llvm::Twine::DecLKind':
267 return str(child['decL'].dereference())
268
269 if kind == 'llvm::Twine::DecULLKind':
270 return str(child['decULL'].dereference())
271
272 if kind == 'llvm::Twine::DecLLKind':
273 return str(child['decLL'].dereference())
274
275 if kind == 'llvm::Twine::UHexKind':
276 val = child['uHex'].dereference()
277 return hex(int(val))
278
279 print(('Unhandled NodeKind {} in Twine pretty-printer. The result will be '
280 'incomplete.').format(kind))
281
282 return '(unhandled {})'.format(kind)
283
284 def string_from_twine_object(self, twine):
285 '''Return the string representation of the Twine object twine.'''
286
287 lhs_str = ''
288 rhs_str = ''
289
290 lhs = twine['LHS']
291 rhs = twine['RHS']
292 lhs_kind = str(twine['LHSKind'])
293 rhs_kind = str(twine['RHSKind'])
294
295 lhs_str = self.string_from_child(lhs, lhs_kind)
296 rhs_str = self.string_from_child(rhs, rhs_kind)
297
298 return lhs_str + rhs_str
299
300 def to_string(self):
301 return self.string_from_twine_object(self._val)
195302
196303 pp = gdb.printing.RegexpCollectionPrettyPrinter("LLVMSupport")
197304 pp.add_printer('llvm::SmallString', '^llvm::SmallString<.*>$', SmallStringPrinter)
200307 pp.add_printer('llvm::ArrayRef', '^llvm::(Const)?ArrayRef<.*>$', ArrayRefPrinter)
201308 pp.add_printer('llvm::Optional', '^llvm::Optional<.*>$', OptionalPrinter)
202309 pp.add_printer('llvm::DenseMap', '^llvm::DenseMap<.*>$', DenseMapPrinter)
310 pp.add_printer('llvm::Twine', '^llvm::Twine$', TwinePrinter)
203311 gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)