Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 1 | class StdVectorSynthProvider: |
Enrico Granata | c92eb40 | 2011-08-04 01:41:02 +0000 | [diff] [blame] | 2 | |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 3 | def __init__(self, valobj, dict): |
| 4 | self.valobj = valobj; |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 5 | self.update() # initialize this provider |
Enrico Granata | c92eb40 | 2011-08-04 01:41:02 +0000 | [diff] [blame] | 6 | |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 7 | def num_children(self): |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 8 | start_val = self.start.GetValueAsUnsigned(0) # read _M_start |
| 9 | finish_val = self.finish.GetValueAsUnsigned(0) # read _M_finish |
| 10 | end_val = self.end.GetValueAsUnsigned(0) # read _M_end_of_storage |
| 11 | |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 12 | # Before a vector has been constructed, it will contain bad values |
| 13 | # so we really need to be careful about the length we return since |
| 14 | # unitialized data can cause us to return a huge number. We need |
| 15 | # to also check for any of the start, finish or end of storage values |
| 16 | # being zero (NULL). If any are, then this vector has not been |
| 17 | # initialized yet and we should return zero |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 18 | |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 19 | # Make sure nothing is NULL |
| 20 | if start_val == 0 or finish_val == 0 or end_val == 0: |
| 21 | return 0 |
| 22 | # Make sure start is less than finish |
| 23 | if start_val >= finish_val: |
| 24 | return 0 |
| 25 | # Make sure finish is less than or equal to end of storage |
| 26 | if finish_val > end_val: |
| 27 | return 0 |
Enrico Granata | c92eb40 | 2011-08-04 01:41:02 +0000 | [diff] [blame] | 28 | |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 29 | # pointer arithmetic: (_M_finish - _M_start) would return the number of |
| 30 | # items of type T contained in the vector. because Python has no way to know |
| 31 | # that we want to subtract two pointers instead of two integers, we have to divide |
| 32 | # by sizeof(T) to be equivalent to the C++ pointer expression |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 33 | num_children = (finish_val-start_val)/self.data_size |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 34 | return num_children |
Enrico Granata | c92eb40 | 2011-08-04 01:41:02 +0000 | [diff] [blame] | 35 | |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 36 | # we assume we are getting children named [0] thru [N-1] |
| 37 | # if for some reason our child name is not in this format, |
| 38 | # do not bother to show it, and return an invalid value |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 39 | def get_child_index(self,name): |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 40 | try: |
| 41 | return int(name.lstrip('[').rstrip(']')) |
| 42 | except: |
| 43 | return -1; |
Enrico Granata | c92eb40 | 2011-08-04 01:41:02 +0000 | [diff] [blame] | 44 | |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 45 | def get_child_at_index(self,index): |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 46 | # LLDB itself should never query for children < 0, but this might come |
| 47 | # from someone asking for a nonexisting child and getting -1 as index |
| 48 | if index < 0: |
| 49 | return None |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 50 | if index >= self.num_children(): |
| 51 | return None; |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 52 | # *(_M_start + index), or equivalently _M_start[index] is C++ code to |
| 53 | # read the index-th item of the vector. in Python we must make an offset |
| 54 | # that is index * sizeof(T), and then grab the value at that offset from |
| 55 | # _M_start |
| 56 | offset = index * self.data_size # index * sizeof(T) |
| 57 | return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) # *(_M_start + index) |
Enrico Granata | c92eb40 | 2011-08-04 01:41:02 +0000 | [diff] [blame] | 58 | |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 59 | # an std::vector contains an object named _M_impl, which in turn contains |
| 60 | # three pointers, _M_start, _M_end and _M_end_of_storage. _M_start points to the |
| 61 | # beginning of the data area, _M_finish points to where the current vector elements |
| 62 | # finish, and _M_end_of_storage is the end of the currently alloc'ed memory portion |
| 63 | # (to allow resizing, a vector may allocate more memory than required) |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 64 | def update(self): |
| 65 | impl = self.valobj.GetChildMemberWithName('_M_impl') |
| 66 | self.start = impl.GetChildMemberWithName('_M_start') |
| 67 | self.finish = impl.GetChildMemberWithName('_M_finish') |
| 68 | self.end = impl.GetChildMemberWithName('_M_end_of_storage') |
Enrico Granata | 7e65503 | 2011-08-24 01:32:46 +0000 | [diff] [blame] | 69 | self.data_type = self.start.GetType().GetPointeeType() # _M_start is defined as a T* |
| 70 | self.data_size = self.data_type.GetByteSize() # sizeof(T) |
Enrico Granata | 7718f3f | 2011-08-04 02:35:14 +0000 | [diff] [blame] | 71 | |