Skip to content

Commit b541f4c

Browse files
committed
Implements Debug trait for types which do not support derive Debug
For types that do not support derive Debug be implemented automatically by rust, we know can generate implementations of the Debug trait. This code generation is hidden behind the '--force-derive-debug' command-line flag.
1 parent 1ac2212 commit b541f4c

14 files changed

+1610
-877
lines changed

src/codegen/derive_debug.rs

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
use ir::comp::{BitfieldUnit, CompKind, Field, FieldData, FieldMethods};
2+
use ir::context::BindgenContext;
3+
use ir::derive::CanTriviallyDeriveDebug;
4+
use ir::item::{IsOpaque, Item, ItemCanonicalName};
5+
use ir::ty::{TypeKind, RUST_DERIVE_IN_ARRAY_LIMIT};
6+
use syntax::ast;
7+
use syntax::codemap::DUMMY_SP;
8+
use syntax::parse::token::Token;
9+
10+
use syntax::tokenstream::TokenTree;
11+
12+
pub fn gen_debug_impl(
13+
ctx: &BindgenContext,
14+
fields: &[Field],
15+
item: &Item,
16+
kind: CompKind,
17+
) -> Vec<ast::ImplItem> {
18+
let struct_name = item.canonical_name(ctx);
19+
let mut format_string = format!("{} {{{{ ", struct_name);
20+
let mut tokens: Vec<TokenTree> = Vec::new();
21+
22+
if item.is_opaque(ctx, &()) {
23+
format_string.push_str("opaque");
24+
} else {
25+
match kind {
26+
CompKind::Union => {
27+
format_string.push_str("union");
28+
}
29+
CompKind::Struct => {
30+
let processed_fields = fields.iter().filter_map(|f| match f {
31+
&Field::DataMember(ref fd) => {
32+
gen_field_data_debug_impl(ctx, fd)
33+
}
34+
&Field::Bitfields(ref bu) => {
35+
gen_bitfield_unit_debug_impl(ctx, bu)
36+
}
37+
});
38+
39+
40+
for (i, (fstring, token)) in processed_fields.enumerate() {
41+
if i > 0 {
42+
format_string.push_str(", ");
43+
}
44+
if !token.is_empty() {
45+
tokens.push(TokenTree::Token(DUMMY_SP, Token::Comma));
46+
tokens.extend(token);
47+
}
48+
format_string.push_str(&fstring);
49+
}
50+
}
51+
}
52+
}
53+
54+
format_string.push_str(" }}");
55+
56+
let impl_ = quote_item!(ctx.ext_cx(),
57+
impl X {
58+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
59+
write!(f, $format_string $tokens)
60+
}
61+
});
62+
63+
match impl_.unwrap().node {
64+
ast::ItemKind::Impl(_, _, _, _, _, ref items) => items.clone(),
65+
_ => unreachable!(),
66+
}
67+
}
68+
69+
fn gen_field_data_debug_impl(
70+
ctx: &BindgenContext,
71+
data: &FieldData,
72+
) -> Option<(String, Vec<TokenTree>)> {
73+
if let Some(name) = data.name() {
74+
gen_item_debug_impl(ctx, ctx.resolve_item(data.ty()), name)
75+
} else {
76+
None
77+
}
78+
}
79+
80+
fn gen_bitfield_unit_debug_impl(
81+
ctx: &BindgenContext,
82+
data: &BitfieldUnit,
83+
) -> Option<(String, Vec<TokenTree>)> {
84+
let mut format_string = String::new();
85+
let mut tokens = Vec::new();
86+
for (i, bu) in data.bitfields().iter().enumerate() {
87+
if i > 0 {
88+
format_string.push_str(", ");
89+
tokens.push(TokenTree::Token(DUMMY_SP, Token::Comma));
90+
}
91+
format_string.push_str(&format!("{} : {{:?}}", bu.name()));
92+
let name_ident = ctx.rust_ident_raw(bu.name());
93+
tokens.extend(quote_tokens!(ctx.ext_cx(), self.$name_ident()));
94+
}
95+
96+
Some((format_string, tokens))
97+
}
98+
99+
fn gen_item_debug_impl(
100+
ctx: &BindgenContext,
101+
item: &Item,
102+
name: &str,
103+
) -> Option<(String, Vec<TokenTree>)> {
104+
let name_ident = ctx.rust_ident_raw(name);
105+
106+
let ty = match item.as_type() {
107+
Some(ty) => ty,
108+
None => {
109+
return None;
110+
}
111+
};
112+
113+
fn debug_print(
114+
ctx: &BindgenContext,
115+
name: &str,
116+
name_ident: ast::Ident,
117+
) -> Option<(String, Vec<TokenTree>)> {
118+
Some((
119+
format!("{}: {{:?}}", name),
120+
quote_tokens!(ctx.ext_cx(), self.$name_ident),
121+
))
122+
}
123+
124+
match *ty.kind() {
125+
// Handle the simple cases.
126+
TypeKind::Void |
127+
TypeKind::NullPtr |
128+
TypeKind::Int(..) |
129+
TypeKind::Float(..) |
130+
TypeKind::Complex(..) |
131+
TypeKind::Function(..) |
132+
TypeKind::Enum(..) |
133+
TypeKind::Reference(..) |
134+
TypeKind::BlockPointer |
135+
TypeKind::UnresolvedTypeRef(..) |
136+
TypeKind::ObjCInterface(..) |
137+
TypeKind::ObjCId |
138+
TypeKind::Comp(..) |
139+
TypeKind::ObjCSel => debug_print(ctx, name, name_ident),
140+
141+
TypeKind::TemplateInstantiation(ref inst) => {
142+
if inst.is_opaque(ctx, item) {
143+
Some((format!("{}: opaque", name), vec![]))
144+
} else {
145+
debug_print(ctx, name, name_ident)
146+
}
147+
}
148+
149+
// The generic is not required to implement Debug, so we can not debug print that type
150+
TypeKind::Named => {
151+
Some((format!("{}: Non-debuggable generic", name), vec![]))
152+
}
153+
154+
TypeKind::Array(_, len) => {
155+
// Generics are not required to implement Debug
156+
if ctx.lookup_item_id_has_type_param_in_array(&item.id()) {
157+
Some((format!("{}: Array with length {}", name, len), vec![]))
158+
} else if len < RUST_DERIVE_IN_ARRAY_LIMIT {
159+
// The simple case
160+
debug_print(ctx, name, name_ident)
161+
} else {
162+
// Let's implement our own print function
163+
Some((
164+
format!("{}: [{{}}]", name),
165+
quote_tokens!(
166+
ctx.ext_cx(),
167+
self.$name_ident
168+
.iter()
169+
.enumerate()
170+
.map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v))
171+
.collect::<String>()),
172+
))
173+
}
174+
}
175+
176+
TypeKind::ResolvedTypeRef(t) |
177+
TypeKind::TemplateAlias(t, _) |
178+
TypeKind::Alias(t) => {
179+
// We follow the aliases
180+
gen_item_debug_impl(ctx, ctx.resolve_item(t), name)
181+
}
182+
183+
TypeKind::Pointer(inner) => {
184+
let inner_type = ctx.resolve_type(inner).canonical_type(ctx);
185+
match *inner_type.kind() {
186+
TypeKind::Function(ref sig)
187+
if !sig.can_trivially_derive_debug() =>
188+
{
189+
Some((format!("{}: FunctionPointer", name), vec![]))
190+
}
191+
_ => debug_print(ctx, name, name_ident),
192+
}
193+
}
194+
195+
TypeKind::Opaque => None,
196+
}
197+
}

0 commit comments

Comments
 (0)