Skip to content

Commit 3fd3062

Browse files
committed
codegen: Add an option to skip comment generation.
This is mostly a work around rust-lang#426, until we implement the proper fix.
1 parent 5cc2506 commit 3fd3062

File tree

6 files changed

+72
-11
lines changed

6 files changed

+72
-11
lines changed

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/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ 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+
178191
/// Generate a C/C++ file that includes the header and has dummy uses of
179192
/// every type defined in the header.
180193
pub fn dummy_uses<T: Into<String>>(mut self, dummy_uses: T) -> Builder {
@@ -495,6 +508,7 @@ pub struct BindgenOptions {
495508
/// The input header file.
496509
pub input_header: Option<String>,
497510

511+
498512
/// Generate a dummy C/C++ file that includes the header and has dummy uses
499513
/// of all types defined therein. See the `uses` module for more.
500514
pub dummy_uses: Option<String>,
@@ -511,6 +525,10 @@ pub struct BindgenOptions {
511525
///
512526
/// See the builder method description for more details.
513527
pub conservative_inline_namespaces: bool,
528+
529+
/// Wether to keep documentation comments in the generated output. See the
530+
/// documentation for more details.
531+
pub generate_comments: bool,
514532
}
515533

516534
impl BindgenOptions {
@@ -555,6 +573,7 @@ impl Default for BindgenOptions {
555573
type_chooser: None,
556574
codegen_config: CodegenConfig::all(),
557575
conservative_inline_namespaces: false,
576+
generate_comments: true,
558577
}
559578
}
560579
}

src/options.rs

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ 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"),
4549
Arg::with_name("builtins")
4650
.long("builtins")
4751
.help("Output bindings for builtin definitions, e.g. \
@@ -271,6 +275,10 @@ pub fn builder_from_flags<I>(args: I)
271275
builder = builder.no_convert_floats();
272276
}
273277

278+
if matches.is_present("no-doc-comments") {
279+
builder = builder.generate_comments(false);
280+
}
281+
274282
if let Some(opaque_types) = matches.values_of("opaque-type") {
275283
for ty in opaque_types {
276284
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+
}

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+
};

0 commit comments

Comments
 (0)