@@ -207,30 +207,46 @@ def children(self):
207
207
yield "borrow" , self .borrow
208
208
209
209
210
- # Yield each key (and optionally value) from a BoxedNode.
211
- def children_of_node (boxed_node , height , want_values ):
210
+ # Yields children (in a provider's sense of the word) for a tree headed by a BoxedNode.
211
+ # In particular, yields each key/value pair in the node and in any child nodes.
212
+ def children_of_node (boxed_node , height ):
212
213
def cast_to_internal (node ):
213
- internal_type_name = str ( node .type .target ()) .replace ("LeafNode" , "InternalNode" , 1 )
214
+ internal_type_name = node .type .target (). name .replace ("LeafNode" , "InternalNode" , 1 )
214
215
internal_type = lookup_type (internal_type_name )
215
216
return node .cast (internal_type .pointer ())
216
217
217
218
node_ptr = unwrap_unique_or_non_null (boxed_node ["ptr" ])
218
- node_ptr = cast_to_internal (node_ptr ) if height > 0 else node_ptr
219
- leaf = node_ptr ["data" ] if height > 0 else node_ptr .dereference ()
219
+ leaf = node_ptr .dereference ()
220
220
keys = leaf ["keys" ]
221
- values = leaf ["vals" ]
221
+ vals = leaf ["vals" ]
222
+ edges = cast_to_internal (node_ptr )["edges" ] if height > 0 else None
222
223
length = int (leaf ["len" ])
223
224
224
225
for i in xrange (0 , length + 1 ):
225
226
if height > 0 :
226
- child_ptr = node_ptr [ " edges" ] [i ]["value" ]["value" ]
227
- for child in children_of_node (child_ptr , height - 1 , want_values ):
227
+ boxed_child_node = edges [i ]["value" ]["value" ]
228
+ for child in children_of_node (boxed_child_node , height - 1 ):
228
229
yield child
229
230
if i < length :
230
- if want_values :
231
- yield keys [i ]["value" ]["value" ], values [i ]["value" ]["value" ]
232
- else :
233
- yield keys [i ]["value" ]["value" ]
231
+ # Avoid "Cannot perform pointer math on incomplete type" on zero-sized arrays.
232
+ key = keys [i ]["value" ]["value" ] if keys .type .sizeof > 0 else None
233
+ val = vals [i ]["value" ]["value" ] if vals .type .sizeof > 0 else None
234
+ yield key , val
235
+
236
+
237
+ # Yields children for a BTreeMap.
238
+ def children_of_map (map ):
239
+ if map ["length" ] > 0 :
240
+ root = map ["root" ]
241
+ if root .type .name .startswith ("core::option::Option<" ):
242
+ root = root .cast (gdb .lookup_type (root .type .name [21 :- 1 ]))
243
+ boxed_root_node = root ["node" ]
244
+ height = root ["height" ]
245
+ for i , (key , val ) in enumerate (children_of_node (boxed_root_node , height )):
246
+ if key is not None :
247
+ yield "key{}" .format (i ), key
248
+ if val is not None :
249
+ yield "val{}" .format (i ), val
234
250
235
251
236
252
class StdBTreeSetProvider :
@@ -242,15 +258,8 @@ def to_string(self):
242
258
243
259
def children (self ):
244
260
inner_map = self .valobj ["map" ]
245
- if inner_map ["length" ] > 0 :
246
- root = inner_map ["root" ]
247
- if "core::option::Option<" in root .type .name :
248
- type_name = str (root .type .name ).replace ("core::option::Option<" , "" , 1 )[:- 1 ]
249
- root = root .cast (gdb .lookup_type (type_name ))
250
-
251
- node_ptr = root ["node" ]
252
- for i , child in enumerate (children_of_node (node_ptr , root ["height" ], False )):
253
- yield "[{}]" .format (i ), child
261
+ for child in children_of_map (inner_map ):
262
+ yield child
254
263
255
264
@staticmethod
256
265
def display_hint ():
@@ -265,16 +274,8 @@ def to_string(self):
265
274
return "BTreeMap(size={})" .format (self .valobj ["length" ])
266
275
267
276
def children (self ):
268
- if self .valobj ["length" ] > 0 :
269
- root = self .valobj ["root" ]
270
- if "core::option::Option<" in root .type .name :
271
- type_name = str (root .type .name ).replace ("core::option::Option<" , "" , 1 )[:- 1 ]
272
- root = root .cast (gdb .lookup_type (type_name ))
273
-
274
- node_ptr = root ["node" ]
275
- for i , child in enumerate (children_of_node (node_ptr , root ["height" ], True )):
276
- yield "key{}" .format (i ), child [0 ]
277
- yield "val{}" .format (i ), child [1 ]
277
+ for child in children_of_map (self .valobj ):
278
+ yield child
278
279
279
280
@staticmethod
280
281
def display_hint ():
0 commit comments