summaryrefslogtreecommitdiff
path: root/bindgen/codegen/mod.rs
diff options
context:
space:
mode:
authorDan Dumont <dan.dumont@hcl.com>2023-01-20 15:12:42 -0500
committerGitHub <noreply@github.com>2023-01-20 15:12:42 -0500
commitbca47cd9c2e718012f7f953be25bb3a6a9ca400b (patch)
tree432dc9180f96a0365c9c49f73158a26f51e727d1 /bindgen/codegen/mod.rs
parent190a017a100b00aeebe2c5e5acfd5ee699a655c9 (diff)
Implement cli option for custom derive (#2328)
* custom derives after DeriveInfo * Introduce `TypeKind` instead of `CompKind` * Add tests * Emit CLI flags for callbacks * update changelog * run rustfmt * fix tests * fix features Co-authored-by: Christian Poveda <christian.poveda@ferrous-systems.com>
Diffstat (limited to 'bindgen/codegen/mod.rs')
-rw-r--r--bindgen/codegen/mod.rs17
1 files changed, 14 insertions, 3 deletions
diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs
index 041d3669..6b24ae1b 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,11 +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: if is_rust_union {
+ DeriveTypeKind::Union
+ } else {
+ DeriveTypeKind::Struct
+ },
})
});
// In most cases this will be a no-op, since custom_derives will be empty.
@@ -2118,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
@@ -3112,7 +3120,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(&DeriveInfo {
+ name: &name,
+ kind: DeriveTypeKind::Enum,
+ })
});
// 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()));