diff --git a/docs.md b/docs.md index d8c863171..4e64c2b42 100644 --- a/docs.md +++ b/docs.md @@ -267,8 +267,6 @@ pub mod my_uninteresting_mod; // This won't be scanned by cbindgen. cbindgen will usually emit all items it finds, as instructed by the parse and export config sections. This annotation will make cbindgen skip this item from the output, while still being aware of it. This is useful for a) suppressing "Can't find" errors and b) emitting `struct my_struct` for types in a different header (rather than a bare `my_struct`). -There is no equivalent config for this annotation - by comparison, the export exclude config will cause cbindgen to not be aware of the item at all. - Note that cbindgen will still traverse `no-export` structs that are `repr(C)` to emit types present in the fields. You will need to manually exclude those types in your config if desired. ``` @@ -575,10 +573,18 @@ usize_is_size_t = true # default: [] include = ["MyOrphanStruct", "MyGreatTypeRename"] -# A list of items to not include in the generated bindings +# A list of items to not include in the generated bindings. The items won't get parsed +# at all. +# # default: [] exclude = ["Bad"] +# A list of items to not export in the generated bindings. The items will still get +# parsed. +# +# default: [] +no_export = ["Inner"] + # A prefix to add before the name of every item # default: no prefix is added prefix = "CAPI_" diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index 410b11f30..017bc51b2 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -363,6 +363,16 @@ impl Bindings { continue; } + if self + .config + .export + .no_export + .iter() + .any(|i| i == item.deref().export_name()) + { + continue; + } + out.new_line_if_not_start(); match *item { ItemContainer::Constant(..) => unreachable!(), diff --git a/src/bindgen/builder.rs b/src/bindgen/builder.rs index 4d2eee12b..2fb510e8d 100644 --- a/src/bindgen/builder.rs +++ b/src/bindgen/builder.rs @@ -179,6 +179,15 @@ impl Builder { self } + #[allow(unused)] + pub fn no_export_item>(mut self, item_name: S) -> Builder { + self.config + .export + .no_export + .push(String::from(item_name.as_ref())); + self + } + #[allow(unused)] pub fn rename_item>(mut self, from: S, to: S) -> Builder { self.config diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index a976d1d26..f607116ea 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -327,6 +327,8 @@ pub struct ExportConfig { pub include: Vec, /// A list of items to not include in the generated bindings pub exclude: Vec, + /// A list of items to not export in the generated bindings + pub no_export: Vec, /// Table of name conversions to apply to item names pub rename: HashMap, /// Table of raw strings to prepend to the body of items. diff --git a/tests/expectations/inner_no_export.compat.c b/tests/expectations/inner_no_export.compat.c new file mode 100644 index 000000000..07c2eb9c7 --- /dev/null +++ b/tests/expectations/inner_no_export.compat.c @@ -0,0 +1,20 @@ +#include +#include +#include + +/** + * Outer docs should be exported. + */ +typedef struct { + Inner *inner; +} Outer; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void root(const Outer *a); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/rust/inner_no_export.rs b/tests/rust/inner_no_export.rs new file mode 100644 index 000000000..fe45a2b63 --- /dev/null +++ b/tests/rust/inner_no_export.rs @@ -0,0 +1,14 @@ +/// Inner docs should not be exported. +pub struct Inner { + a: i32, +} + +/// Outer docs should be exported. +#[repr(C)] +pub struct Outer { + inner: *mut Inner, +} + +#[no_mangle] +pub extern "C" fn root(a: *const Outer) +{ } diff --git a/tests/rust/inner_no_export.toml b/tests/rust/inner_no_export.toml new file mode 100644 index 000000000..84b13ad8f --- /dev/null +++ b/tests/rust/inner_no_export.toml @@ -0,0 +1,12 @@ +cpp_compat = true +language = "C" +no_includes = true +sys_includes = ["stdint.h", "stddef.h", "stdbool.h"] + +[parse] +parse_deps = true + +[export] +no_export = [ + "Inner", +]