From 202179958ff9c5993147377e169cef45b50c5591 Mon Sep 17 00:00:00 2001 From: Dan Dumont Date: Thu, 1 Dec 2022 17:41:52 -0500 Subject: [PATCH 1/8] custom derives after DeriveInfo --- bindgen-cli/options.rs | 67 ++++++++++++++++++++++++++++++++++++++++++ bindgen/callbacks.rs | 4 +++ bindgen/codegen/mod.rs | 6 +++- bindgen/lib.rs | 4 +-- bindgen/regex_set.rs | 5 ++++ 5 files changed, 83 insertions(+), 3 deletions(-) diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index c536ed6275..25972c16fe 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -340,6 +340,9 @@ struct BindgenCommand { /// Wrap unsafe operations in unsafe blocks. #[arg(long)] wrap_unsafe_ops: bool, + /// Derive custom traits on any kind of type. The value must be of the shape == where can be one of: Any, Struct, or Union. + #[arg(long, value_name = "DERIVE")] + with_derive_custom: Vec, /// Prints the version, and exits #[arg(short = 'V', long)] version: bool, @@ -456,6 +459,7 @@ where merge_extern_blocks, override_abi, wrap_unsafe_ops, + with_derive_custom, version, clang_args, } = command; @@ -894,5 +898,68 @@ where builder = builder.wrap_unsafe_ops(true); } + #[derive(Debug)] + struct CustomDeriveCallback { + derive: String, + kind: String, + regex: String, + } + + impl bindgen::callbacks::ParseCallbacks for CustomDeriveCallback { + fn add_derives( + &self, + info: &bindgen::callbacks::DeriveInfo<'_>, + ) -> Vec { + match self.kind.to_lowercase().as_str() { + "struct" => match info.kind { + Some(bindgen::ir::comp::CompKind::Struct) => {} // continue + _ => return vec![], // bail + }, + "union" => match info.kind { + Some(bindgen::ir::comp::CompKind::Union) => {} // continue + _ => return vec![], // bail + }, + "any" => {} // continue + other => { + panic!("Invalid custom derive: Unexpected kind '{}'", other) + } + } + + let mut re = bindgen::RegexSet::new(); + re.insert(self.regex.to_owned()); + re.build(false); + + match re.matches(info.name) { + true => vec![self.derive.to_owned()], + false => vec![], + } + } + } + + for custom_derive in with_derive_custom { + let mut values = custom_derive.rsplitn(3, "="); + let derive = values + .next() + .expect("Invalid custom derive: Expected 3 parts, found 0") + .to_owned(); + let kind = values + .next() + .expect("Invalid custom derive: Expected 3 parts, found 1") + .to_owned(); + let regex = values + .next() + .expect("Invalid custom derive: Expected 3 parts, found 2") + .to_owned(); + + builder = builder.parse_callbacks(Box::new(CustomDeriveCallback { + derive, + kind, + regex: regex, + })); + } + + // let derives: Vec = traits.into_iter().map(|t| t.to_string()).collect(); + // builder = builder.parse_callbacks(Box::new(CustomTraitCallback { derives })); + Ok((builder, output, verbose)) } diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index cba406cbd1..e6e9825afe 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -1,6 +1,7 @@ //! A public API for more fine-grained customization of bindgen behavior. pub use crate::ir::analysis::DeriveTrait; +use crate::ir::comp::CompKind; pub use crate::ir::derive::CanDerive as ImplementsTrait; pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue}; pub use crate::ir::int::IntKind; @@ -120,10 +121,13 @@ pub trait ParseCallbacks: fmt::Debug { /// Relevant information about a type to which new derive attributes will be added using /// [`ParseCallbacks::add_derives`]. +#[derive(Debug)] #[non_exhaustive] pub struct DeriveInfo<'a> { /// The name of the type. pub name: &'a str, + /// The kind of the type + pub kind: Option, } /// An struct providing information about the item being passed to `ParseCallbacks::generated_name_override`. diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 041d36691c..a2afb77e81 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2105,6 +2105,7 @@ impl CodeGenerator for CompInfo { let custom_derives = ctx.options().all_callbacks(|cb| { cb.add_derives(&crate::callbacks::DeriveInfo { name: &canonical_name, + kind: Some(self.kind()), }) }); // In most cases this will be a no-op, since custom_derives will be empty. @@ -3112,7 +3113,10 @@ impl CodeGenerator for Enum { // The custom derives callback may return a list of derive attributes; // add them to the end of the list. let custom_derives = ctx.options().all_callbacks(|cb| { - cb.add_derives(&crate::callbacks::DeriveInfo { name: &name }) + cb.add_derives(&crate::callbacks::DeriveInfo { + name: &name, + kind: None, + }) }); // In most cases this will be a no-op, since custom_derives will be empty. derives.extend(custom_derives.iter().map(|s| s.as_str())); diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 46444a93bb..61815e98f9 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -65,7 +65,7 @@ mod clang; mod codegen; mod deps; mod features; -mod ir; +pub mod ir; mod parse; mod regex_set; mod time; @@ -91,7 +91,7 @@ use crate::ir::context::{BindgenContext, ItemId}; pub use crate::ir::function::Abi; use crate::ir::item::Item; use crate::parse::ParseError; -use crate::regex_set::RegexSet; +pub use crate::regex_set::RegexSet; use std::borrow::Cow; use std::env; diff --git a/bindgen/regex_set.rs b/bindgen/regex_set.rs index 9f1e2251cd..7a78a9e232 100644 --- a/bindgen/regex_set.rs +++ b/bindgen/regex_set.rs @@ -16,6 +16,11 @@ pub struct RegexSet { } impl RegexSet { + /// Create a new RegexSet + pub fn new() -> RegexSet { + RegexSet { ..Default::default() } + } + /// Is this set empty? pub fn is_empty(&self) -> bool { self.items.is_empty() From c0cdd8c5c2fdcc97a227eab34115b70784a8c3af Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 9 Dec 2022 15:49:28 -0500 Subject: [PATCH 2/8] Introduce `TypeKind` instead of `CompKind` --- bindgen-cli/options.rs | 97 ++++++++++++++++++++---------------------- bindgen/callbacks.rs | 16 +++++-- bindgen/codegen/mod.rs | 17 +++++--- 3 files changed, 71 insertions(+), 59 deletions(-) diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 25972c16fe..42a56b9cc1 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -1,6 +1,7 @@ +use bindgen::callbacks::TypeKind; use bindgen::{ builder, AliasVariation, Builder, CodegenConfig, EnumVariation, - MacroTypeVariation, NonCopyUnionStyle, RustTarget, + MacroTypeVariation, NonCopyUnionStyle, RegexSet, RustTarget, DEFAULT_ANON_FIELDS_PREFIX, RUST_TARGET_STRINGS, }; use clap::Parser; @@ -340,9 +341,18 @@ struct BindgenCommand { /// Wrap unsafe operations in unsafe blocks. #[arg(long)] wrap_unsafe_ops: bool, - /// Derive custom traits on any kind of type. The value must be of the shape == where can be one of: Any, Struct, or Union. - #[arg(long, value_name = "DERIVE")] + /// Derive custom traits on any kind of type. The value must be of the shape = where is a coma-separated list of derive macros. + #[arg(long, value_name = "CUSTOM")] with_derive_custom: Vec, + /// Derive custom traits on a `struct`. The value must be of the shape = where is a coma-separated list of derive macros. + #[arg(long, value_name = "CUSTOM")] + with_derive_custom_struct: Vec, + /// Derive custom traits on an `enum. The value must be of the shape = where is a coma-separated list of derive macros. + #[arg(long, value_name = "CUSTOM")] + with_derive_custom_enum: Vec, + /// Derive custom traits on a `union`. The value must be of the shape = where is a coma-separated list of derive macros. + #[arg(long, value_name = "CUSTOM")] + with_derive_custom_union: Vec, /// Prints the version, and exits #[arg(short = 'V', long)] version: bool, @@ -460,6 +470,9 @@ where override_abi, wrap_unsafe_ops, with_derive_custom, + with_derive_custom_struct, + with_derive_custom_enum, + with_derive_custom_union, version, clang_args, } = command; @@ -900,9 +913,9 @@ where #[derive(Debug)] struct CustomDeriveCallback { - derive: String, - kind: String, - regex: String, + derives: Vec, + kind: Option, + regex_set: bindgen::RegexSet, } impl bindgen::callbacks::ParseCallbacks for CustomDeriveCallback { @@ -910,56 +923,38 @@ where &self, info: &bindgen::callbacks::DeriveInfo<'_>, ) -> Vec { - match self.kind.to_lowercase().as_str() { - "struct" => match info.kind { - Some(bindgen::ir::comp::CompKind::Struct) => {} // continue - _ => return vec![], // bail - }, - "union" => match info.kind { - Some(bindgen::ir::comp::CompKind::Union) => {} // continue - _ => return vec![], // bail - }, - "any" => {} // continue - other => { - panic!("Invalid custom derive: Unexpected kind '{}'", other) + if self.kind.map(|kind| kind == info.kind).unwrap_or(true) { + if self.regex_set.matches(info.name) { + return self.derives.clone(); } } - - let mut re = bindgen::RegexSet::new(); - re.insert(self.regex.to_owned()); - re.build(false); - - match re.matches(info.name) { - true => vec![self.derive.to_owned()], - false => vec![], - } + vec![] } } - for custom_derive in with_derive_custom { - let mut values = custom_derive.rsplitn(3, "="); - let derive = values - .next() - .expect("Invalid custom derive: Expected 3 parts, found 0") - .to_owned(); - let kind = values - .next() - .expect("Invalid custom derive: Expected 3 parts, found 1") - .to_owned(); - let regex = values - .next() - .expect("Invalid custom derive: Expected 3 parts, found 2") - .to_owned(); - - builder = builder.parse_callbacks(Box::new(CustomDeriveCallback { - derive, - kind, - regex: regex, - })); - } - - // let derives: Vec = traits.into_iter().map(|t| t.to_string()).collect(); - // builder = builder.parse_callbacks(Box::new(CustomTraitCallback { derives })); + for (custom_derives, kind) in [ + (with_derive_custom, None), + (with_derive_custom_struct, Some(TypeKind::Struct)), + (with_derive_custom_enum, Some(TypeKind::Enum)), + (with_derive_custom_union, Some(TypeKind::Union)), + ] { + for custom_derive in custom_derives { + let (regex, derives) = custom_derive + .rsplit_once("=") + .expect("Invalid custom derive argument: Missing `=`"); + let derives = derives.split(",").map(|s| s.to_owned()).collect(); + + let mut regex_set = RegexSet::new(); + regex_set.insert(regex); + regex_set.build(false); + + builder = builder.parse_callbacks(Box::new(CustomDeriveCallback { + derives, + kind, + regex_set, + })); + } + } Ok((builder, output, verbose)) } diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index e6e9825afe..99e1614e5d 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -1,7 +1,6 @@ //! A public API for more fine-grained customization of bindgen behavior. pub use crate::ir::analysis::DeriveTrait; -use crate::ir::comp::CompKind; pub use crate::ir::derive::CanDerive as ImplementsTrait; pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue}; pub use crate::ir::int::IntKind; @@ -126,8 +125,19 @@ pub trait ParseCallbacks: fmt::Debug { pub struct DeriveInfo<'a> { /// The name of the type. pub name: &'a str, - /// The kind of the type - pub kind: Option, + /// The kind of the type. + pub kind: TypeKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// The kind of the current type. +pub enum TypeKind { + /// The type is a Rust `struct`. + Struct, + /// The type is a Rust `enum`. + Enum, + /// The type is a Rust `union`. + Union, } /// An struct providing information about the item being passed to `ParseCallbacks::generated_name_override`. diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index a2afb77e81..6b24ae1bd0 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -18,6 +18,7 @@ use self::struct_layout::StructLayoutTracker; use super::BindgenOptions; +use crate::callbacks::{DeriveInfo, TypeKind as DeriveTypeKind}; use crate::ir::analysis::{HasVtable, Sizedness}; use crate::ir::annotations::FieldAccessorKind; use crate::ir::comp::{ @@ -2100,12 +2101,18 @@ impl CodeGenerator for CompInfo { let mut derives: Vec<_> = derivable_traits.into(); derives.extend(item.annotations().derives().iter().map(String::as_str)); + let is_rust_union = is_union && struct_layout.is_rust_union(); + // The custom derives callback may return a list of derive attributes; // add them to the end of the list. let custom_derives = ctx.options().all_callbacks(|cb| { - cb.add_derives(&crate::callbacks::DeriveInfo { + cb.add_derives(&DeriveInfo { name: &canonical_name, - kind: Some(self.kind()), + kind: if is_rust_union { + DeriveTypeKind::Union + } else { + DeriveTypeKind::Struct + }, }) }); // In most cases this will be a no-op, since custom_derives will be empty. @@ -2119,7 +2126,7 @@ impl CodeGenerator for CompInfo { attributes.push(attributes::must_use()); } - let mut tokens = if is_union && struct_layout.is_rust_union() { + let mut tokens = if is_rust_union { quote! { #( #attributes )* pub union #canonical_ident @@ -3113,9 +3120,9 @@ impl CodeGenerator for Enum { // The custom derives callback may return a list of derive attributes; // add them to the end of the list. let custom_derives = ctx.options().all_callbacks(|cb| { - cb.add_derives(&crate::callbacks::DeriveInfo { + cb.add_derives(&DeriveInfo { name: &name, - kind: None, + kind: DeriveTypeKind::Enum, }) }); // In most cases this will be a no-op, since custom_derives will be empty. From 225b118ddd9ea72918896b3ca6776a25b2e51b97 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 12 Dec 2022 14:53:55 -0500 Subject: [PATCH 3/8] Add tests --- .../expectations/tests/derive-custom-cli.rs | 115 ++++++++++++++++++ .../tests/headers/derive-custom-cli.h | 14 +++ 2 files changed, 129 insertions(+) create mode 100644 bindgen-tests/tests/expectations/tests/derive-custom-cli.rs create mode 100644 bindgen-tests/tests/headers/derive-custom-cli.h diff --git a/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs b/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs new file mode 100644 index 0000000000..b5813c1a2b --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs @@ -0,0 +1,115 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Debug, Default)] +pub struct foo_struct { + pub inner: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_foo_struct() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo_struct)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo_struct)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).inner) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo_struct), + "::", + stringify!(inner) + ) + ); +} +#[repr(u32)] +#[derive(Clone, Hash, PartialEq, Eq, Debug, Copy)] +pub enum foo_enum { + inner = 0, +} +#[repr(C)] +#[derive(Debug, Hash)] +pub union foo_union { + pub fst: ::std::mem::ManuallyDrop<::std::os::raw::c_int>, + pub snd: ::std::mem::ManuallyDrop, +} +#[test] +fn bindgen_test_layout_foo_union() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo_union)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo_union)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).fst) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo_union), + "::", + stringify!(fst) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).snd) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo_union), + "::", + stringify!(snd) + ) + ); +} +#[repr(C)] +pub struct non_matching { + pub inner: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_non_matching() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(non_matching)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(non_matching)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).inner) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(non_matching), + "::", + stringify!(inner) + ) + ); +} diff --git a/bindgen-tests/tests/headers/derive-custom-cli.h b/bindgen-tests/tests/headers/derive-custom-cli.h new file mode 100644 index 0000000000..c2890a181a --- /dev/null +++ b/bindgen-tests/tests/headers/derive-custom-cli.h @@ -0,0 +1,14 @@ +// bindgen-flags: --default-enum-style rust --default-non-copy-union-style manually_drop --no-default=".*" --no-hash=".*" --no-partialeq=".*" --no-debug=".*" --no-copy=".*" --with-derive-custom="foo.*=Debug" --with-derive-custom-struct="foo.*=Default" --with-derive-custom-enum="foo.*=Copy" --with-derive-custom-union="foo.*=Hash" +struct foo_struct { + int inner; +}; +enum foo_enum { + inner = 0 +}; +union foo_union { + int fst; + float snd; +}; +struct non_matching { + int inner; +}; From c60ea0492baf29272b3e34222a2e297d6225f26f Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 20 Jan 2023 13:50:46 -0500 Subject: [PATCH 4/8] Emit CLI flags for callbacks --- bindgen-cli/Cargo.toml | 2 +- bindgen-cli/options.rs | 34 ++++++++++++++++++++++++++++------ bindgen/Cargo.toml | 1 + bindgen/callbacks.rs | 6 ++++++ bindgen/lib.rs | 5 +++++ bindgen/regex_set.rs | 4 +++- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/bindgen-cli/Cargo.toml b/bindgen-cli/Cargo.toml index 76099eb060..b900d9406d 100644 --- a/bindgen-cli/Cargo.toml +++ b/bindgen-cli/Cargo.toml @@ -21,7 +21,7 @@ path = "main.rs" name = "bindgen" [dependencies] -bindgen = { path = "../bindgen", version = "=0.63.0" } +bindgen = { path = "../bindgen", version = "=0.63.0", features = ["cli"] } shlex = "1" clap = { version = "4", features = ["derive"] } env_logger = { version = "0.9.0", optional = true } diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 42a56b9cc1..26ff6ea138 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -919,14 +919,36 @@ where } impl bindgen::callbacks::ParseCallbacks for CustomDeriveCallback { + fn cli_args(&self) -> Vec { + let mut args = vec![]; + + let flag = match &self.kind { + None => "--with-derive-custom", + Some(TypeKind::Struct) => "--with-derive-custom-struct", + Some(TypeKind::Enum) => "--with-derive-custom-enum", + Some(TypeKind::Union) => "--with-derive-custom-union", + }; + + let derives = self.derives.join(","); + + for item in self.regex_set.get_items() { + args.extend_from_slice(&[ + flag.to_owned(), + format!("{}={}", item, derives), + ]); + } + + args + } + fn add_derives( &self, info: &bindgen::callbacks::DeriveInfo<'_>, ) -> Vec { - if self.kind.map(|kind| kind == info.kind).unwrap_or(true) { - if self.regex_set.matches(info.name) { - return self.derives.clone(); - } + if self.kind.map(|kind| kind == info.kind).unwrap_or(true) + && self.regex_set.matches(info.name) + { + return self.derives.clone(); } vec![] } @@ -940,9 +962,9 @@ where ] { for custom_derive in custom_derives { let (regex, derives) = custom_derive - .rsplit_once("=") + .rsplit_once('=') .expect("Invalid custom derive argument: Missing `=`"); - let derives = derives.split(",").map(|s| s.to_owned()).collect(); + let derives = derives.split(',').map(|s| s.to_owned()).collect(); let mut regex_set = RegexSet::new(); regex_set.insert(regex); diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml index 5a03d2481e..2292a5efe4 100644 --- a/bindgen/Cargo.toml +++ b/bindgen/Cargo.toml @@ -47,6 +47,7 @@ static = ["clang-sys/static"] runtime = ["clang-sys/runtime"] # Dynamically discover a `rustfmt` binary using the `which` crate which-rustfmt = ["which"] +cli = [] # These features only exist for CI testing -- don't use them if you're not hacking # on bindgen! diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index 99e1614e5d..dc20e2581e 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -25,6 +25,12 @@ impl Default for MacroParsingBehavior { /// A trait to allow configuring different kinds of types in different /// situations. pub trait ParseCallbacks: fmt::Debug { + #[cfg(feature = "cli")] + #[doc(hidden)] + fn cli_args(&self) -> Vec { + vec![] + } + /// This function will be run on every macro that is identified. fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior { MacroParsingBehavior::Default diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 61815e98f9..cf1486c2d2 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -653,6 +653,11 @@ impl Builder { output_vector.push("--wrap-unsafe-ops".into()); } + #[cfg(feature = "cli")] + for callbacks in &self.options.parse_callbacks { + output_vector.extend(callbacks.cli_args()); + } + // Add clang arguments output_vector.push("--".into()); diff --git a/bindgen/regex_set.rs b/bindgen/regex_set.rs index 7a78a9e232..6246dd255b 100644 --- a/bindgen/regex_set.rs +++ b/bindgen/regex_set.rs @@ -18,7 +18,9 @@ pub struct RegexSet { impl RegexSet { /// Create a new RegexSet pub fn new() -> RegexSet { - RegexSet { ..Default::default() } + RegexSet { + ..Default::default() + } } /// Is this set empty? From 847492730040802fff910c4395be42a56db07c8a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 20 Jan 2023 14:41:41 -0500 Subject: [PATCH 5/8] update changelog --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e70b118764..9850539fcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -156,13 +156,18 @@ ## Changed * Fixed name collisions when having a C `enum` and a `typedef` with the same name. - * The `ParseCallbacks::generated_name_override` now receives `ItemInfo<'_>` as + * The `ParseCallbacks::generated_name_override` method now receives `ItemInfo<'_>` as argument instead of a `&str`. * Updated the `clang-sys` crate version to 1.4.0 to support clang 15. * The return type is now ommited in signatures of functions returning `void`. * Updated the `clap` dependency for `bindgen-cli` to 4. * Rewrote the `bindgen-cli` argument parser which could introduce unexpected behavior changes. + * The `ParseCallbacks::add_derives` method now receives `DeriveInfo<'_>` as + argument instead of a `&str`. This type also includes the kind of target type. + * Added a new set of flags `--with-derive-custom`, + `--with-derive-custom-struct`, `--with-derive-custom-enum` and + `--with-derive-custom-enum` to add custom derives from the CLI. ## Removed From 3a70dba3d163ef2ba3f0c0213d8361873f07b363 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 20 Jan 2023 14:43:51 -0500 Subject: [PATCH 6/8] run rustfmt --- bindgen-cli/options.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 26ff6ea138..2b09d85fd9 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -945,8 +945,8 @@ where &self, info: &bindgen::callbacks::DeriveInfo<'_>, ) -> Vec { - if self.kind.map(|kind| kind == info.kind).unwrap_or(true) - && self.regex_set.matches(info.name) + if self.kind.map(|kind| kind == info.kind).unwrap_or(true) && + self.regex_set.matches(info.name) { return self.derives.clone(); } From 8c9e5eba0769c1534ed4c97cca0112eb2abb69ce Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 20 Jan 2023 14:55:06 -0500 Subject: [PATCH 7/8] fix tests --- bindgen-tests/tests/expectations/tests/derive-custom-cli.rs | 6 +++--- bindgen-tests/tests/headers/derive-custom-cli.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs b/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs index b5813c1a2b..36f84c7d93 100644 --- a/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs +++ b/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs @@ -6,7 +6,7 @@ )] #[repr(C)] -#[derive(Debug, Default)] +#[derive(Clone, Default)] pub struct foo_struct { pub inner: ::std::os::raw::c_int, } @@ -37,12 +37,12 @@ fn bindgen_test_layout_foo_struct() { ); } #[repr(u32)] -#[derive(Clone, Hash, PartialEq, Eq, Debug, Copy)] +#[derive(Clone, Hash, PartialEq, Eq, Copy)] pub enum foo_enum { inner = 0, } #[repr(C)] -#[derive(Debug, Hash)] +#[derive(Clone, Copy)] pub union foo_union { pub fst: ::std::mem::ManuallyDrop<::std::os::raw::c_int>, pub snd: ::std::mem::ManuallyDrop, diff --git a/bindgen-tests/tests/headers/derive-custom-cli.h b/bindgen-tests/tests/headers/derive-custom-cli.h index c2890a181a..9b65536f94 100644 --- a/bindgen-tests/tests/headers/derive-custom-cli.h +++ b/bindgen-tests/tests/headers/derive-custom-cli.h @@ -1,4 +1,4 @@ -// bindgen-flags: --default-enum-style rust --default-non-copy-union-style manually_drop --no-default=".*" --no-hash=".*" --no-partialeq=".*" --no-debug=".*" --no-copy=".*" --with-derive-custom="foo.*=Debug" --with-derive-custom-struct="foo.*=Default" --with-derive-custom-enum="foo.*=Copy" --with-derive-custom-union="foo.*=Hash" +// bindgen-flags: --default-enum-style rust --default-non-copy-union-style manually_drop --no-default=".*" --no-hash=".*" --no-partialeq=".*" --no-debug=".*" --no-copy=".*" --with-derive-custom="foo_[^e].*=Clone" --with-derive-custom-struct="foo.*=Default" --with-derive-custom-enum="foo.*=Copy" --with-derive-custom-union="foo.*=Copy" struct foo_struct { int inner; }; From cbfa03d13f6416d1e0a580fd4932a10c1522dd23 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 20 Jan 2023 15:04:36 -0500 Subject: [PATCH 8/8] fix features --- bindgen-tests/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen-tests/Cargo.toml b/bindgen-tests/Cargo.toml index 32694dad31..0678274fd6 100644 --- a/bindgen-tests/Cargo.toml +++ b/bindgen-tests/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" publish = false [dev-dependencies] -bindgen = { path = "../bindgen" } +bindgen = { path = "../bindgen", features = ["cli"] } diff = "0.1" shlex = "1" clap = { version = "4", features = ["derive"] }