1"""
2Load into LLDB with:
3script import lldbDataFormatters
4type synthetic add -x "^llvm::SmallVectorImpl<.+>$" -l lldbDataFormatters.SmallVectorSynthProvider
5"""
6
7# Pretty printer for llvm::SmallVector/llvm::SmallVectorImpl
8class SmallVectorSynthProvider:
9    def __init__(self, valobj, dict):
10        self.valobj = valobj;
11        self.update() # initialize this provider
12
13    def num_children(self):
14        begin = self.begin.GetValueAsUnsigned(0)
15        end = self.end.GetValueAsUnsigned(0)
16        return (end - begin)/self.type_size
17
18    def get_child_index(self, name):
19        try:
20            return int(name.lstrip('[').rstrip(']'))
21        except:
22            return -1;
23
24    def get_child_at_index(self, index):
25        # Do bounds checking.
26        if index < 0:
27            return None
28        if index >= self.num_children():
29            return None;
30
31        offset = index * self.type_size
32        return self.begin.CreateChildAtOffset('['+str(index)+']',
33                                              offset, self.data_type)
34
35    def get_type_from_name(self):
36        import re
37        name = self.valobj.GetType().GetName()
38        # This class works with both SmallVectors and SmallVectorImpls.
39        res = re.match("^(llvm::)?SmallVectorImpl<(.+)>$", name)
40        if res:
41            return res.group(2)
42        res = re.match("^(llvm::)?SmallVector<(.+), \d+>$", name)
43        if res:
44            return res.group(2)
45        return None
46
47    def update(self):
48        self.begin = self.valobj.GetChildMemberWithName('BeginX')
49        self.end = self.valobj.GetChildMemberWithName('EndX')
50        data_type = self.get_type_from_name()
51        # FIXME: this sometimes returns an invalid type.
52        self.data_type = self.valobj.GetTarget().FindFirstType(data_type)
53        self.type_size = self.data_type.GetByteSize()
54