Skip to content

Commit 41135ca

Browse files
author
bors-servo
authored
Auto merge of #444 - emilio:no-comments, r=fitzgen
codegen: Add an option to skip comment generation. This is mostly a work around #426, until we implement the proper fix. r? @fitzgen
2 parents 5cc2506 + 78e425f commit 41135ca

File tree

10 files changed

+135
-24
lines changed

10 files changed

+135
-24
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ name = "bindgen"
1313
readme = "README.md"
1414
repository = "https://github.com/servo/rust-bindgen"
1515
documentation = "https://docs.rs/bindgen"
16-
version = "0.20.2"
16+
version = "0.20.3"
1717
build = "build.rs"
1818

1919
[badges]

src/codegen/mod.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,10 @@ impl CodeGenerator for Type {
534534
let rust_name = ctx.rust_ident(&name);
535535
let mut typedef = aster::AstBuilder::new().item().pub_();
536536

537-
if let Some(comment) = item.comment() {
538-
typedef = typedef.attr().doc(comment);
537+
if ctx.options().generate_comments {
538+
if let Some(comment) = item.comment() {
539+
typedef = typedef.attr().doc(comment);
540+
}
539541
}
540542

541543
// We prefer using `pub use` over `pub type` because of:
@@ -808,8 +810,10 @@ impl CodeGenerator for CompInfo {
808810

809811
let mut attributes = vec![];
810812
let mut needs_clone_impl = false;
811-
if let Some(comment) = item.comment() {
812-
attributes.push(attributes::doc(comment));
813+
if ctx.options().generate_comments {
814+
if let Some(comment) = item.comment() {
815+
attributes.push(attributes::doc(comment));
816+
}
813817
}
814818
if self.packed() {
815819
attributes.push(attributes::repr_list(&["C", "packed"]));
@@ -1007,8 +1011,10 @@ impl CodeGenerator for CompInfo {
10071011
};
10081012

10091013
let mut attrs = vec![];
1010-
if let Some(comment) = field.comment() {
1011-
attrs.push(attributes::doc(comment));
1014+
if ctx.options().generate_comments {
1015+
if let Some(comment) = field.comment() {
1016+
attrs.push(attributes::doc(comment));
1017+
}
10121018
}
10131019
let field_name = match field.name() {
10141020
Some(name) => ctx.rust_mangle(name).into_owned(),
@@ -1705,8 +1711,10 @@ impl CodeGenerator for Enum {
17051711
builder = builder.with_attr(attributes::repr("C"));
17061712
}
17071713

1708-
if let Some(comment) = item.comment() {
1709-
builder = builder.with_attr(attributes::doc(comment));
1714+
if ctx.options().generate_comments {
1715+
if let Some(comment) = item.comment() {
1716+
builder = builder.with_attr(attributes::doc(comment));
1717+
}
17101718
}
17111719

17121720
if !is_constified_enum {
@@ -2166,8 +2174,10 @@ impl CodeGenerator for Function {
21662174

21672175
let mut attributes = vec![];
21682176

2169-
if let Some(comment) = item.comment() {
2170-
attributes.push(attributes::doc(comment));
2177+
if ctx.options().generate_comments {
2178+
if let Some(comment) = item.comment() {
2179+
attributes.push(attributes::doc(comment));
2180+
}
21712181
}
21722182

21732183
if let Some(mangled) = mangled_name {

src/ir/context.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1155,15 +1155,15 @@ pub struct WhitelistedItemsIter<'ctx, 'gen>
11551155
{
11561156
ctx: &'ctx BindgenContext<'gen>,
11571157

1158-
// The set of whitelisted items we have seen. If you think of traversing
1159-
// whitelisted items like GC tracing, this is the mark bits, and contains
1160-
// both black and gray items.
1158+
/// The set of whitelisted items we have seen. If you think of traversing
1159+
/// whitelisted items like GC tracing, this is the mark bits, and contains
1160+
/// both black and gray items.
11611161
seen: ItemSet,
11621162

1163-
// The set of whitelisted items that we have seen but have yet to iterate
1164-
// over and collect transitive references from. To return to the GC analogy,
1165-
// this is the mark stack, containing the set of gray items which we have
1166-
// not finished tracing yet.
1163+
/// The set of whitelisted items that we have seen but have yet to iterate
1164+
/// over and collect transitive references from. To return to the GC analogy,
1165+
/// this is the mark stack, containing the set of gray items which we have
1166+
/// not finished tracing yet.
11671167
to_iterate: Vec<ItemId>,
11681168
}
11691169

@@ -1181,12 +1181,14 @@ impl<'ctx, 'gen> Iterator for WhitelistedItemsIter<'ctx, 'gen>
11811181
debug_assert!(self.seen.contains(&id));
11821182
debug_assert!(self.ctx.items.contains_key(&id));
11831183

1184-
let mut sub_types = ItemSet::new();
1185-
id.collect_types(self.ctx, &mut sub_types, &());
1184+
if self.ctx.options().whitelist_recursively {
1185+
let mut sub_types = ItemSet::new();
1186+
id.collect_types(self.ctx, &mut sub_types, &());
11861187

1187-
for id in sub_types {
1188-
if self.seen.insert(id) {
1189-
self.to_iterate.push(id);
1188+
for id in sub_types {
1189+
if self.seen.insert(id) {
1190+
self.to_iterate.push(id);
1191+
}
11901192
}
11911193
}
11921194

src/lib.rs

+33
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ impl Builder {
175175
self
176176
}
177177

178+
/// Whether the generated bindings should contain documentation comments or
179+
/// not.
180+
///
181+
/// This ideally will always be true, but it may need to be false until we
182+
/// implement some processing on comments to work around issues as described
183+
/// in:
184+
///
185+
/// https://github.com/servo/rust-bindgen/issues/426
186+
pub fn generate_comments(mut self, doit: bool) -> Self {
187+
self.options.generate_comments = doit;
188+
self
189+
}
190+
191+
/// Whether to whitelist types recursively or not. Defaults to true.
192+
///
193+
/// This can be used to get bindgen to generate _exactly_ the types you want
194+
/// in your bindings, and then import other types manually via other means
195+
/// (like `raw_line`).
196+
pub fn whitelist_recursively(mut self, doit: bool) -> Self {
197+
self.options.whitelist_recursively = doit;
198+
self
199+
}
200+
178201
/// Generate a C/C++ file that includes the header and has dummy uses of
179202
/// every type defined in the header.
180203
pub fn dummy_uses<T: Into<String>>(mut self, dummy_uses: T) -> Builder {
@@ -495,6 +518,7 @@ pub struct BindgenOptions {
495518
/// The input header file.
496519
pub input_header: Option<String>,
497520

521+
498522
/// Generate a dummy C/C++ file that includes the header and has dummy uses
499523
/// of all types defined therein. See the `uses` module for more.
500524
pub dummy_uses: Option<String>,
@@ -511,6 +535,13 @@ pub struct BindgenOptions {
511535
///
512536
/// See the builder method description for more details.
513537
pub conservative_inline_namespaces: bool,
538+
539+
/// Wether to keep documentation comments in the generated output. See the
540+
/// documentation for more details.
541+
pub generate_comments: bool,
542+
543+
/// Wether to whitelist types recursively. Defaults to true.
544+
pub whitelist_recursively: bool,
514545
}
515546

516547
impl BindgenOptions {
@@ -555,6 +586,8 @@ impl Default for BindgenOptions {
555586
type_chooser: None,
556587
codegen_config: CodegenConfig::all(),
557588
conservative_inline_namespaces: false,
589+
generate_comments: true,
590+
whitelist_recursively: true,
558591
}
559592
}
560593
}

src/options.rs

+15
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ pub fn builder_from_flags<I>(args: I)
4242
Arg::with_name("no-derive-debug")
4343
.long("no-derive-debug")
4444
.help("Avoid deriving Debug on any type."),
45+
Arg::with_name("no-doc-comments")
46+
.long("no-doc-comments")
47+
.help("Avoid including doc comments in the output, see: \
48+
https://github.com/servo/rust-bindgen/issues/426"),
49+
Arg::with_name("no-recursive-whitelist")
50+
.long("no-recursive-whitelist")
51+
.help("Avoid whitelisting types recursively"),
4552
Arg::with_name("builtins")
4653
.long("builtins")
4754
.help("Output bindings for builtin definitions, e.g. \
@@ -271,6 +278,14 @@ pub fn builder_from_flags<I>(args: I)
271278
builder = builder.no_convert_floats();
272279
}
273280

281+
if matches.is_present("no-doc-comments") {
282+
builder = builder.generate_comments(false);
283+
}
284+
285+
if matches.is_present("no-recursive-whitelist") {
286+
builder = builder.whitelist_recursively(false);
287+
}
288+
274289
if let Some(opaque_types) = matches.values_of("opaque-type") {
275290
for ty in opaque_types {
276291
builder = builder.opaque_type(ty);
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Debug, Copy)]
9+
pub struct Foo {
10+
pub s: ::std::os::raw::c_int,
11+
}
12+
#[test]
13+
fn bindgen_test_layout_Foo() {
14+
assert_eq!(::std::mem::size_of::<Foo>() , 4usize);
15+
assert_eq!(::std::mem::align_of::<Foo>() , 4usize);
16+
}
17+
impl Clone for Foo {
18+
fn clone(&self) -> Self { *self }
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
pub enum Bar {}
7+
8+
#[repr(C)]
9+
#[derive(Debug, Copy)]
10+
pub struct Foo {
11+
pub baz: *mut Bar,
12+
}
13+
#[test]
14+
fn bindgen_test_layout_Foo() {
15+
assert_eq!(::std::mem::size_of::<Foo>() , 8usize);
16+
assert_eq!(::std::mem::align_of::<Foo>() , 8usize);
17+
}
18+
impl Clone for Foo {
19+
fn clone(&self) -> Self { *self }
20+
}

tests/headers/no-comments.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --no-doc-comments
2+
3+
struct Foo {
4+
int s; /*!< Including this will prevent rustc for compiling it */
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --no-recursive-whitelist --whitelist-type "Foo" --raw-line "pub enum Bar {}"
2+
3+
struct Bar;
4+
5+
struct Foo {
6+
struct Bar* baz;
7+
};

0 commit comments

Comments
 (0)