Skip to content

Commit

Permalink
codegen: Add an option to skip comment generation.
Browse files Browse the repository at this point in the history
This is mostly a work around rust-lang#426,
until we implement the proper fix.
  • Loading branch information
emilio committed Jan 26, 2017
1 parent 5cc2506 commit 7d7c49a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 20 additions & 10 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"]));
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,
Expand All @@ -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 {
Expand Down Expand Up @@ -555,6 +573,7 @@ impl Default for BindgenOptions {
type_chooser: None,
codegen_config: CodegenConfig::all(),
conservative_inline_namespaces: false,
generate_comments: true,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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. \
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/expectations/tests/no-comments.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
5 changes: 5 additions & 0 deletions tests/headers/no-comments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --no-doc-comments

struct Foo {
int s; /*!< Including this will prevent rustc for compiling it */
};

0 comments on commit 7d7c49a

Please sign in to comment.