Skip to content

Commit 7440755

Browse files
committed
feat: add custom attributes to fn items
1 parent 59a43e1 commit 7440755

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

bindgen/callbacks.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A public API for more fine-grained customization of bindgen behavior.
22
33
pub use crate::ir::analysis::DeriveTrait;
4+
pub use crate::ir::comp::MethodKind as AttributeMethodKind;
45
pub use crate::ir::derive::CanDerive as ImplementsTrait;
56
pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
67
pub use crate::ir::int::IntKind;
@@ -231,15 +232,30 @@ pub struct DeriveInfo<'a> {
231232
pub kind: TypeKind,
232233
}
233234

234-
/// Relevant information about a type to which new attributes will be added using
235+
/// Relevant information about an item to which new attributes will be added using
235236
/// [`ParseCallbacks::add_attributes`].
236237
#[derive(Debug)]
237238
#[non_exhaustive]
238239
pub struct AttributeInfo<'a> {
239-
/// The name of the type.
240+
/// The name of the item.
240241
pub name: &'a str,
241-
/// The kind of the type.
242-
pub kind: TypeKind,
242+
/// The kind of the item.
243+
pub kind: AttributeItemKind,
244+
/// The kind of a method item.
245+
pub method_kind: Option<AttributeMethodKind>
246+
}
247+
248+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
249+
/// The kind of the current item.
250+
pub enum AttributeItemKind {
251+
/// The item is a Rust `struct`.
252+
Struct,
253+
/// The item is a Rust `enum`.
254+
Enum,
255+
/// The item is a Rust `union`.
256+
Union,
257+
/// The item is a Rust `fn`.
258+
Function,
243259
}
244260

245261
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

bindgen/codegen/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use self::struct_layout::StructLayoutTracker;
2121
use super::BindgenOptions;
2222

2323
use crate::callbacks::{
24-
AttributeInfo, DeriveInfo, DiscoveredItem, DiscoveredItemId, FieldInfo,
25-
TypeKind as DeriveTypeKind,
24+
AttributeInfo, AttributeItemKind, DeriveInfo, DiscoveredItem,
25+
DiscoveredItemId, FieldInfo, TypeKind as DeriveTypeKind,
2626
};
2727
use crate::codegen::error::Error;
2828
use crate::ir::analysis::{HasVtable, Sizedness};
@@ -1070,7 +1070,8 @@ impl CodeGenerator for Type {
10701070
ctx.options().all_callbacks(|cb| {
10711071
cb.add_attributes(&AttributeInfo {
10721072
name: &name,
1073-
kind: DeriveTypeKind::Struct,
1073+
kind: AttributeItemKind::Struct,
1074+
method_kind: None,
10741075
})
10751076
});
10761077
attributes.extend(
@@ -2533,10 +2534,11 @@ impl CodeGenerator for CompInfo {
25332534
cb.add_attributes(&AttributeInfo {
25342535
name: &canonical_name,
25352536
kind: if is_rust_union {
2536-
DeriveTypeKind::Union
2537+
AttributeItemKind::Union
25372538
} else {
2538-
DeriveTypeKind::Struct
2539+
AttributeItemKind::Struct
25392540
},
2541+
method_kind: None,
25402542
})
25412543
});
25422544
attributes.extend(custom_attributes.iter().map(|s| s.parse().unwrap()));
@@ -3148,6 +3150,15 @@ impl Method {
31483150

31493151
let mut attrs = vec![attributes::inline()];
31503152

3153+
let custom_attributes = ctx.options().all_callbacks(|cb| {
3154+
cb.add_attributes(&AttributeInfo {
3155+
name: &name,
3156+
kind: AttributeItemKind::Function,
3157+
method_kind: Some(self.kind()),
3158+
})
3159+
});
3160+
attrs.extend(custom_attributes.iter().map(|s| s.parse().unwrap()));
3161+
31513162
if signature.must_use() {
31523163
attrs.push(attributes::must_use());
31533164
}
@@ -3728,7 +3739,8 @@ impl CodeGenerator for Enum {
37283739
let custom_attributes = ctx.options().all_callbacks(|cb| {
37293740
cb.add_attributes(&AttributeInfo {
37303741
name: &name,
3731-
kind: DeriveTypeKind::Enum,
3742+
kind: AttributeItemKind::Enum,
3743+
method_kind: None,
37323744
})
37333745
});
37343746
attrs.extend(custom_attributes.iter().map(|s| s.parse().unwrap()));

bindgen/ir/comp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) enum CompKind {
3232

3333
/// The kind of C++ method.
3434
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
35-
pub(crate) enum MethodKind {
35+
pub enum MethodKind {
3636
/// A constructor. We represent it as method for convenience, to avoid code
3737
/// duplication.
3838
Constructor,

bindgen/options/cli.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
builder,
55
callbacks::{
6-
AttributeInfo, DeriveInfo, ItemInfo, ParseCallbacks, TypeKind,
6+
AttributeInfo, AttributeItemKind, DeriveInfo, ItemInfo, ParseCallbacks, TypeKind,
77
},
88
features::{RustEdition, EARLIEST_STABLE_RUST},
99
regex_set::RegexSet,
@@ -477,7 +477,7 @@ struct BindgenCommand {
477477
/// Derive custom traits on a `union`. The CUSTOM value must be of the shape REGEX=DERIVE where DERIVE is a coma-separated list of derive macros.
478478
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_derive)]
479479
with_derive_custom_union: Vec<(Vec<String>, String)>,
480-
/// Add custom attributes on any kind of type. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
480+
/// Add custom attributes on any item. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
481481
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_attribute)]
482482
with_attribute_custom: Vec<(Vec<String>, String)>,
483483
/// Add custom attributes on a `struct`. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
@@ -489,6 +489,9 @@ struct BindgenCommand {
489489
/// Add custom attributes on a `union`. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
490490
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_attribute)]
491491
with_attribute_custom_union: Vec<(Vec<String>, String)>,
492+
/// Add custom attributes on an `fn`. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
493+
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_attribute)]
494+
with_attribute_custom_function: Vec<(Vec<String>, String)>,
492495
/// Generate wrappers for `static` and `static inline` functions.
493496
#[arg(long)]
494497
wrap_static_fns: bool,
@@ -645,6 +648,7 @@ where
645648
with_attribute_custom_struct,
646649
with_attribute_custom_enum,
647650
with_attribute_custom_union,
651+
with_attribute_custom_function,
648652
wrap_static_fns,
649653
wrap_static_fns_path,
650654
wrap_static_fns_suffix,
@@ -745,7 +749,7 @@ where
745749
#[derive(Debug)]
746750
struct CustomAttributeCallback {
747751
attributes: Vec<String>,
748-
kind: Option<TypeKind>,
752+
kind: Option<AttributeItemKind>,
749753
regex_set: RegexSet,
750754
}
751755

@@ -755,9 +759,10 @@ where
755759

756760
let flag = match &self.kind {
757761
None => "--with-attribute-custom",
758-
Some(TypeKind::Struct) => "--with-attribute-custom-struct",
759-
Some(TypeKind::Enum) => "--with-attribute-custom-enum",
760-
Some(TypeKind::Union) => "--with-attribute-custom-union",
762+
Some(AttributeItemKind::Struct) => "--with-attribute-custom-struct",
763+
Some(AttributeItemKind::Enum) => "--with-attribute-custom-enum",
764+
Some(AttributeItemKind::Union) => "--with-attribute-custom-union",
765+
Some(AttributeItemKind::Function) => "--with-attribute-custom-function",
761766
};
762767

763768
let attributes = self.attributes.join(",");
@@ -1010,19 +1015,24 @@ where
10101015
(with_attribute_custom, None, "--with-attribute-custom"),
10111016
(
10121017
with_attribute_custom_struct,
1013-
Some(TypeKind::Struct),
1018+
Some(AttributeItemKind::Struct),
10141019
"--with-attribute-custom-struct",
10151020
),
10161021
(
10171022
with_attribute_custom_enum,
1018-
Some(TypeKind::Enum),
1023+
Some(AttributeItemKind::Enum),
10191024
"--with-attribute-custom-enum",
10201025
),
10211026
(
10221027
with_attribute_custom_union,
1023-
Some(TypeKind::Union),
1028+
Some(AttributeItemKind::Union),
10241029
"--with-attribute-custom-union",
10251030
),
1031+
(
1032+
with_attribute_custom_function,
1033+
Some(AttributeItemKind::Function),
1034+
"--with-attribute-custom-function",
1035+
),
10261036
] {
10271037
#[cfg(feature = "experimental")]
10281038
let name = emit_diagnostics.then_some(_name);

0 commit comments

Comments
 (0)