Skip to content

Commit e401638

Browse files
authored
Rollup merge of rust-lang#53112 - fukatani:pretty-print-btreeset, r=michaelwoerister
pretty print BTreeSet I want pretty printing for BTreeSet. ```rust use std::collections::*; fn main() { let mut s = BTreeSet::new(); s.insert(5); s.insert(3); s.insert(7); s.remove(&3); println!("{:?}", s); } ``` ``` (gdb) b 9 (gdb) p s $1 = BTreeSet<i32> with 2 elements = {[0] = 5, [1] = 7} ``` This is analogy of pretty printing for C++ std::set.
2 parents f45f525 + 6e562d2 commit e401638

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/etc/debugger_pretty_printers_common.py

+22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
TYPE_KIND_REGULAR_UNION = 17
4949
TYPE_KIND_OS_STRING = 18
5050
TYPE_KIND_STD_VECDEQUE = 19
51+
TYPE_KIND_STD_BTREESET = 20
5152

5253
ENCODED_ENUM_PREFIX = "RUST$ENCODED$ENUM$"
5354
ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR"
@@ -71,6 +72,9 @@
7172
STD_VECDEQUE_FIELD_NAME_HEAD,
7273
STD_VECDEQUE_FIELD_NAME_BUF]
7374

75+
# std::collections::BTreeSet<> related constants
76+
STD_BTREESET_FIELD_NAMES = ["map"]
77+
7478
# std::String related constants
7579
STD_STRING_FIELD_NAMES = ["vec"]
7680

@@ -175,6 +179,11 @@ def __classify_struct(self):
175179
self.__conforms_to_field_layout(STD_VECDEQUE_FIELD_NAMES)):
176180
return TYPE_KIND_STD_VECDEQUE
177181

182+
# STD COLLECTION BTREESET
183+
if (unqualified_type_name.startswith("BTreeSet<") and
184+
self.__conforms_to_field_layout(STD_BTREESET_FIELD_NAMES)):
185+
return TYPE_KIND_STD_BTREESET
186+
178187
# STD STRING
179188
if (unqualified_type_name.startswith("String") and
180189
self.__conforms_to_field_layout(STD_STRING_FIELD_NAMES)):
@@ -358,6 +367,19 @@ def extract_tail_head_ptr_and_cap_from_std_vecdeque(vec_val):
358367
return (tail, head, data_ptr, capacity)
359368

360369

370+
def extract_length_and_ptr_from_std_btreeset(vec_val):
371+
assert vec_val.type.get_type_kind() == TYPE_KIND_STD_BTREESET
372+
map = vec_val.get_child_at_index(0)
373+
root = map.get_child_at_index(0)
374+
length = map.get_child_at_index(1).as_integer()
375+
node = root.get_child_at_index(0)
376+
ptr = node.get_child_at_index(0)
377+
unique_ptr_val = ptr.get_child_at_index(0)
378+
data_ptr = unique_ptr_val.get_child_at_index(0)
379+
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
380+
return (length, data_ptr)
381+
382+
361383
def extract_length_and_ptr_from_slice(slice_val):
362384
assert (slice_val.type.get_type_kind() == TYPE_KIND_SLICE or
363385
slice_val.type.get_type_kind() == TYPE_KIND_STR_SLICE)

src/etc/gdb_rust_pretty_printing.py

+26
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ def rust_pretty_printer_lookup_function(gdb_val):
127127
if type_kind == rustpp.TYPE_KIND_STD_VECDEQUE:
128128
return RustStdVecDequePrinter(val)
129129

130+
if type_kind == rustpp.TYPE_KIND_STD_BTREESET:
131+
return RustStdBTreeSetPrinter(val)
132+
130133
if type_kind == rustpp.TYPE_KIND_STD_STRING:
131134
return RustStdStringPrinter(val)
132135

@@ -299,6 +302,29 @@ def children(self):
299302
yield (str(index), (gdb_ptr + index).dereference())
300303

301304

305+
class RustStdBTreeSetPrinter(object):
306+
def __init__(self, val):
307+
self.__val = val
308+
309+
@staticmethod
310+
def display_hint():
311+
return "array"
312+
313+
def to_string(self):
314+
(length, data_ptr) = \
315+
rustpp.extract_length_and_ptr_from_std_btreeset(self.__val)
316+
return (self.__val.type.get_unqualified_type_name() +
317+
("(len: %i)" % length))
318+
319+
def children(self):
320+
(length, data_ptr) = \
321+
rustpp.extract_length_and_ptr_from_std_btreeset(self.__val)
322+
val = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3)
323+
gdb_ptr = val.get_wrapped_value()
324+
for index in xrange(length):
325+
yield (str(index), gdb_ptr[index])
326+
327+
302328
class RustStdStringPrinter(object):
303329
def __init__(self, val):
304330
self.__val = val
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-windows failing on win32 bot
12+
// ignore-freebsd: gdb package too new
13+
// ignore-android: FIXME(#10381)
14+
// compile-flags:-g
15+
// min-gdb-version 7.7
16+
// min-lldb-version: 310
17+
18+
// === GDB TESTS ===================================================================================
19+
20+
// gdb-command: run
21+
22+
// gdb-command: print btree_set
23+
// gdb-check:$1 = BTreeSet<i32>(len: 3) = {3, 5, 7}
24+
25+
// gdb-command: print vec_deque
26+
// gdb-check:$2 = VecDeque<i32>(len: 3, cap: 8) = {5, 3, 7}
27+
28+
#![allow(unused_variables)]
29+
use std::collections::BTreeSet;
30+
use std::collections::VecDeque;
31+
32+
33+
fn main() {
34+
35+
// BTreeSet
36+
let mut btree_set = BTreeSet::new();
37+
btree_set.insert(5);
38+
btree_set.insert(3);
39+
btree_set.insert(7);
40+
41+
// VecDeque
42+
let mut vec_deque = VecDeque::new();
43+
vec_deque.push_back(5);
44+
vec_deque.push_back(3);
45+
vec_deque.push_back(7);
46+
47+
zzz(); // #break
48+
}
49+
50+
fn zzz() { () }

0 commit comments

Comments
 (0)