diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-26 17:09:28 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-26 20:34:51 +0100 |
commit | 7d7c49aaa3b9b5c01d2afac70660575f1bb562b2 (patch) | |
tree | c7f0116fc39a1741a4abc0385d1c5fcb6d34b113 | |
parent | 5cc25067b27af15ba67be645235f9c90573d6313 (diff) |
codegen: Add an option to skip comment generation.
This is mostly a work around https://github.com/servo/rust-bindgen/issues/426,
until we implement the proper fix.
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/codegen/mod.rs | 30 | ||||
-rw-r--r-- | src/lib.rs | 19 | ||||
-rw-r--r-- | src/options.rs | 8 | ||||
-rw-r--r-- | tests/expectations/tests/no-comments.rs | 19 | ||||
-rw-r--r-- | tests/headers/no-comments.h | 5 |
7 files changed, 73 insertions, 12 deletions
@@ -1,6 +1,6 @@ [root] name = "bindgen" -version = "0.20.2" +version = "0.20.3" dependencies = [ "aster 0.38.0 (registry+https://github.com/rust-lang/crates.io-index)", "cexpr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -13,7 +13,7 @@ name = "bindgen" readme = "README.md" repository = "https://github.com/servo/rust-bindgen" documentation = "https://docs.rs/bindgen" -version = "0.20.2" +version = "0.20.3" build = "build.rs" [badges] diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index c92e95fa..b4ef8606 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -534,8 +534,10 @@ impl CodeGenerator for Type { let rust_name = ctx.rust_ident(&name); let mut typedef = aster::AstBuilder::new().item().pub_(); - if let Some(comment) = item.comment() { - typedef = typedef.attr().doc(comment); + if ctx.options().generate_comments { + if let Some(comment) = item.comment() { + typedef = typedef.attr().doc(comment); + } } // We prefer using `pub use` over `pub type` because of: @@ -808,8 +810,10 @@ impl CodeGenerator for CompInfo { let mut attributes = vec![]; let mut needs_clone_impl = false; - if let Some(comment) = item.comment() { - attributes.push(attributes::doc(comment)); + if ctx.options().generate_comments { + if let Some(comment) = item.comment() { + attributes.push(attributes::doc(comment)); + } } if self.packed() { attributes.push(attributes::repr_list(&["C", "packed"])); @@ -1007,8 +1011,10 @@ impl CodeGenerator for CompInfo { }; let mut attrs = vec![]; - if let Some(comment) = field.comment() { - attrs.push(attributes::doc(comment)); + if ctx.options().generate_comments { + if let Some(comment) = field.comment() { + attrs.push(attributes::doc(comment)); + } } let field_name = match field.name() { Some(name) => ctx.rust_mangle(name).into_owned(), @@ -1705,8 +1711,10 @@ impl CodeGenerator for Enum { builder = builder.with_attr(attributes::repr("C")); } - if let Some(comment) = item.comment() { - builder = builder.with_attr(attributes::doc(comment)); + if ctx.options().generate_comments { + if let Some(comment) = item.comment() { + builder = builder.with_attr(attributes::doc(comment)); + } } if !is_constified_enum { @@ -2166,8 +2174,10 @@ impl CodeGenerator for Function { let mut attributes = vec![]; - if let Some(comment) = item.comment() { - attributes.push(attributes::doc(comment)); + if ctx.options().generate_comments { + if let Some(comment) = item.comment() { + attributes.push(attributes::doc(comment)); + } } if let Some(mangled) = mangled_name { @@ -175,6 +175,19 @@ impl Builder { self } + /// Whether the generated bindings should contain documentation comments or + /// not. + /// + /// This ideally will always be true, but it may need to be false until we + /// implement some processing on comments to work around issues as described + /// in: + /// + /// https://github.com/servo/rust-bindgen/issues/426 + pub fn generate_comments(mut self, doit: bool) -> Self { + self.options.generate_comments = doit; + self + } + /// Generate a C/C++ file that includes the header and has dummy uses of /// every type defined in the header. pub fn dummy_uses<T: Into<String>>(mut self, dummy_uses: T) -> Builder { @@ -495,6 +508,7 @@ pub struct BindgenOptions { /// The input header file. pub input_header: Option<String>, + /// Generate a dummy C/C++ file that includes the header and has dummy uses /// of all types defined therein. See the `uses` module for more. pub dummy_uses: Option<String>, @@ -511,6 +525,10 @@ pub struct BindgenOptions { /// /// See the builder method description for more details. pub conservative_inline_namespaces: bool, + + /// Wether to keep documentation comments in the generated output. See the + /// documentation for more details. + pub generate_comments: bool, } impl BindgenOptions { @@ -555,6 +573,7 @@ impl Default for BindgenOptions { type_chooser: None, codegen_config: CodegenConfig::all(), conservative_inline_namespaces: false, + generate_comments: true, } } } diff --git a/src/options.rs b/src/options.rs index fa69ecba..7b18eb03 100644 --- a/src/options.rs +++ b/src/options.rs @@ -42,6 +42,10 @@ pub fn builder_from_flags<I>(args: I) Arg::with_name("no-derive-debug") .long("no-derive-debug") .help("Avoid deriving Debug on any type."), + Arg::with_name("no-doc-comments") + .long("no-doc-comments") + .help("Avoid including doc comments in the output, see: \ + https://github.com/servo/rust-bindgen/issues/426"), Arg::with_name("builtins") .long("builtins") .help("Output bindings for builtin definitions, e.g. \ @@ -271,6 +275,10 @@ pub fn builder_from_flags<I>(args: I) builder = builder.no_convert_floats(); } + if matches.is_present("no-doc-comments") { + builder = builder.generate_comments(false); + } + if let Some(opaque_types) = matches.values_of("opaque-type") { for ty in opaque_types { builder = builder.opaque_type(ty); diff --git a/tests/expectations/tests/no-comments.rs b/tests/expectations/tests/no-comments.rs new file mode 100644 index 00000000..8a4371e7 --- /dev/null +++ b/tests/expectations/tests/no-comments.rs @@ -0,0 +1,19 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Foo { + pub s: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_Foo() { + assert_eq!(::std::mem::size_of::<Foo>() , 4usize); + assert_eq!(::std::mem::align_of::<Foo>() , 4usize); +} +impl Clone for Foo { + fn clone(&self) -> Self { *self } +} diff --git a/tests/headers/no-comments.h b/tests/headers/no-comments.h new file mode 100644 index 00000000..1ddb1a3b --- /dev/null +++ b/tests/headers/no-comments.h @@ -0,0 +1,5 @@ +// bindgen-flags: --no-doc-comments + +struct Foo { + int s; /*!< Including this will prevent rustc for compiling it */ +}; |