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