-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add custom attributes to fn
items
#3088
base: main
Are you sure you want to change the base?
Conversation
I'd love to add some tests as well but I'm not sure how I would do that, can't get anything other than basic tests to run. |
You can take a look at #2866, where I added some tests along with the support for custom attributes on other items ( |
I will be adding further changes to this PR. The I've made the following changes already:
- fn add_attributes(&self, _info: &AttributeInfo<'_>) -> Vec<String> { vec![] }
+ fn process_attributes(&self, _info: &AttributeInfo<'_>, _attributes: &mut HashSet<String>) {} Example usage: fn process_attributes(&self, info: &bindgen::callbacks::AttributeInfo<'_>, attrs: &mut HashSet<String>) {
match info.kind {
AttrKind::Var => {
attrs.insert(r#"#[cfg(feature="statics")]"#.to_owned());
}
AttrKind::Struct | AttrKind::Union | AttrKind::Enum => {
attrs.insert(r#"#[cfg(feature="structs")]"#.to_owned());
}
AttrKind::Function(_) => {
attrs.insert(r#"#[cfg(feature="functions")]"#.to_owned());
attrs.insert(if info.name.starts_with("NtUser") {
r#"#[link(name = "ntuser")]"#
} else {
r#"#[link(name = "ntdll")]"#
}.to_owned());
}
}
} Example output with #[cfg(feature = "functions")]
pub mod unsafe_extern_C_cfg_feature_functions_link_name_ntdll {
#[link(name = "ntdll")]
unsafe extern "C" {
pub fn NtCallbackReturn(
OutputBuffer: PVOID,
OutputLength: ULONG,
Status: NTSTATUS,
) -> NTSTATUS;
// ...
}
#[cfg(feature = "functions")]
pub use unsafe_extern_C_cfg_feature_functions_link_name_ntdll::*;
I still have to restore CLI functionality, we can possibly even add Progress can be tracked at https://github.com/oberrich/rust-bindgen/tree/alias for now, I will be merging them into this branch once it's ready for a review. |
I've rebased my changes onto upstream to simplify merging and cleaned up the commit history to make the changes more digestible. @jschwe, I noticed you've merged #3103, and I'm wondering about the usefulness of We could take a similar approach to The I'm open to feedback on the best approach—would love to hear any thoughts! |
Just to clarify: that PR only changed the behavior from using the last registered callback (where the implementation might very well be the default I find it very convenient to setup a default global implementation to process comments, e.g. converting doxygen to markdown, and then for specific header files specify another callback, which overrides the global implementation and perhaps works around some issues for that specific header file. I also don't really see a use case for layering different comment processing steps via different callbacks. Is there something that can't be done in one callback, or would be more convenient? I think it would be much harder to reason about, since the order of the steps matter.
I think that issue is easily fixable without any changes. Let me check.
For my use-case at least I would prefer getting the original comment, since parsing doxygen comments is easier, then the post-processed markdown I generate in my case. |
@emilio Since you're merging PRs kindly pining again for #3088 (comment) Looking forward to a preliminary review. |
This PR adds functionality to add custom attributes to function items (proposed in #2978).
I've implemented this by extending the
TypeKind
used byAttributeInfo
to include theFunction(FunctionKind)
variant (seeAttributeItemKind
).Unifying the callback this way is a breaking change when
--with_attribute_custom
or matching on thekind
field is used.