Skip to content

Commit 7dd665c

Browse files
author
bors-servo
authored
Auto merge of #899 - bkchr:manual_debug_impl, r=fitzgen
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. Should solve: #875 Sorry for the extra noise in lib.rs, codegen/mod.rs etc, that was rustfmt.
2 parents 828d468 + f7345fa commit 7dd665c

14 files changed

+1640
-877
lines changed

src/codegen/derive_debug.rs

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
use ir::comp::{BitfieldUnit, CompKind, Field, FieldData, FieldMethods};
2+
use ir::context::BindgenContext;
3+
use ir::derive::CanTriviallyDeriveDebug;
4+
use ir::item::{HasTypeParamInArray, 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) => fd.impl_debug(ctx, ()),
32+
&Field::Bitfields(ref bu) => bu.impl_debug(ctx, ()),
33+
});
34+
35+
36+
for (i, (fstring, token)) in processed_fields.enumerate() {
37+
if i > 0 {
38+
format_string.push_str(", ");
39+
}
40+
if !token.is_empty() {
41+
tokens.push(TokenTree::Token(DUMMY_SP, Token::Comma));
42+
tokens.extend(token);
43+
}
44+
format_string.push_str(&fstring);
45+
}
46+
}
47+
}
48+
}
49+
50+
format_string.push_str(" }}");
51+
52+
let impl_ = quote_item!(ctx.ext_cx(),
53+
impl X {
54+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
55+
write!(f, $format_string $tokens)
56+
}
57+
});
58+
59+
match impl_.unwrap().node {
60+
ast::ItemKind::Impl(_, _, _, _, _, ref items) => items.clone(),
61+
_ => unreachable!(),
62+
}
63+
}
64+
65+
/// A trait for the things which we can codegen tokens that contribute towards a
66+
/// generated `impl Debug`.
67+
pub trait ImplDebug<'a> {
68+
/// Any extra parameter required by this a particular `ImplDebug` implementation.
69+
type Extra;
70+
71+
/// Generate a format string snippet to be included in the larger `impl Debug`
72+
/// format string, and the code to get the format string's interpolation values.
73+
fn impl_debug(
74+
&self,
75+
ctx: &BindgenContext,
76+
extra: Self::Extra,
77+
) -> Option<(String, Vec<TokenTree>)>;
78+
}
79+
80+
impl<'a> ImplDebug<'a> for FieldData {
81+
type Extra = ();
82+
83+
fn impl_debug(
84+
&self,
85+
ctx: &BindgenContext,
86+
_: Self::Extra,
87+
) -> Option<(String, Vec<TokenTree>)> {
88+
if let Some(name) = self.name() {
89+
ctx.resolve_item(self.ty()).impl_debug(ctx, name)
90+
} else {
91+
None
92+
}
93+
}
94+
}
95+
96+
impl<'a> ImplDebug<'a> for BitfieldUnit {
97+
type Extra = ();
98+
99+
fn impl_debug(
100+
&self,
101+
ctx: &BindgenContext,
102+
_: Self::Extra,
103+
) -> Option<(String, Vec<TokenTree>)> {
104+
let mut format_string = String::new();
105+
let mut tokens = Vec::new();
106+
for (i, bu) in self.bitfields().iter().enumerate() {
107+
if i > 0 {
108+
format_string.push_str(", ");
109+
tokens.push(TokenTree::Token(DUMMY_SP, Token::Comma));
110+
}
111+
format_string.push_str(&format!("{} : {{:?}}", bu.name()));
112+
let name_ident = ctx.rust_ident_raw(bu.name());
113+
tokens.extend(quote_tokens!(ctx.ext_cx(), self.$name_ident()));
114+
}
115+
116+
Some((format_string, tokens))
117+
}
118+
}
119+
120+
impl<'a> ImplDebug<'a> for Item {
121+
type Extra = &'a str;
122+
123+
fn impl_debug(
124+
&self,
125+
ctx: &BindgenContext,
126+
name: Self::Extra,
127+
) -> Option<(String, Vec<TokenTree>)> {
128+
let name_ident = ctx.rust_ident_raw(name);
129+
130+
let ty = match self.as_type() {
131+
Some(ty) => ty,
132+
None => {
133+
return None;
134+
}
135+
};
136+
137+
fn debug_print(
138+
ctx: &BindgenContext,
139+
name: &str,
140+
name_ident: ast::Ident,
141+
) -> Option<(String, Vec<TokenTree>)> {
142+
Some((
143+
format!("{}: {{:?}}", name),
144+
quote_tokens!(ctx.ext_cx(), self.$name_ident),
145+
))
146+
}
147+
148+
match *ty.kind() {
149+
// Handle the simple cases.
150+
TypeKind::Void |
151+
TypeKind::NullPtr |
152+
TypeKind::Int(..) |
153+
TypeKind::Float(..) |
154+
TypeKind::Complex(..) |
155+
TypeKind::Function(..) |
156+
TypeKind::Enum(..) |
157+
TypeKind::Reference(..) |
158+
TypeKind::BlockPointer |
159+
TypeKind::UnresolvedTypeRef(..) |
160+
TypeKind::ObjCInterface(..) |
161+
TypeKind::ObjCId |
162+
TypeKind::Comp(..) |
163+
TypeKind::ObjCSel => debug_print(ctx, name, name_ident),
164+
165+
TypeKind::TemplateInstantiation(ref inst) => {
166+
if inst.is_opaque(ctx, self) {
167+
Some((format!("{}: opaque", name), vec![]))
168+
} else {
169+
debug_print(ctx, name, name_ident)
170+
}
171+
}
172+
173+
// The generic is not required to implement Debug, so we can not debug print that type
174+
TypeKind::Named => {
175+
Some((format!("{}: Non-debuggable generic", name), vec![]))
176+
}
177+
178+
TypeKind::Array(_, len) => {
179+
// Generics are not required to implement Debug
180+
if self.has_type_param_in_array(ctx) {
181+
Some((
182+
format!("{}: Array with length {}", name, len),
183+
vec![],
184+
))
185+
} else if len < RUST_DERIVE_IN_ARRAY_LIMIT {
186+
// The simple case
187+
debug_print(ctx, name, name_ident)
188+
} else {
189+
// Let's implement our own print function
190+
Some((
191+
format!("{}: [{{}}]", name),
192+
quote_tokens!(
193+
ctx.ext_cx(),
194+
self.$name_ident
195+
.iter()
196+
.enumerate()
197+
.map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v))
198+
.collect::<String>()),
199+
))
200+
}
201+
}
202+
203+
TypeKind::ResolvedTypeRef(t) |
204+
TypeKind::TemplateAlias(t, _) |
205+
TypeKind::Alias(t) => {
206+
// We follow the aliases
207+
ctx.resolve_item(t).impl_debug(ctx, name)
208+
}
209+
210+
TypeKind::Pointer(inner) => {
211+
let inner_type = ctx.resolve_type(inner).canonical_type(ctx);
212+
match *inner_type.kind() {
213+
TypeKind::Function(ref sig)
214+
if !sig.can_trivially_derive_debug() =>
215+
{
216+
Some((format!("{}: FunctionPointer", name), vec![]))
217+
}
218+
_ => debug_print(ctx, name, name_ident),
219+
}
220+
}
221+
222+
TypeKind::Opaque => None,
223+
}
224+
}
225+
}

0 commit comments

Comments
 (0)