1 //! A public API for more fine-grained customization of bindgen behavior.
3 pub use crate::ir::analysis::DeriveTrait;
4 pub use crate::ir::derive::CanDerive as ImplementsTrait;
5 pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
6 pub use crate::ir::int::IntKind;
9 /// An enum to allow ignoring parsing of macros.
10 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
11 pub enum MacroParsingBehavior {
12 /// Ignore the macro, generating no code for it, or anything that depends on
15 /// The default behavior bindgen would have otherwise.
19 impl Default for MacroParsingBehavior {
20 fn default() -> Self {
21 MacroParsingBehavior::Default
25 /// A trait to allow configuring different kinds of types in different
27 pub trait ParseCallbacks: fmt::Debug {
28 #[cfg(feature = "__cli")]
30 fn cli_args(&self) -> Vec<String> {
34 /// This function will be run on every macro that is identified.
35 fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
36 MacroParsingBehavior::Default
39 /// This function will run for every extern variable and function. The returned value determines
40 /// the name visible in the bindings.
41 fn generated_name_override(
43 _item_info: ItemInfo<'_>,
48 /// This function will run for every extern variable and function. The returned value determines
49 /// the link name in the bindings.
50 fn generated_link_name_override(
52 _item_info: ItemInfo<'_>,
57 /// The integer kind an integer macro should have, given a name and the
58 /// value of that macro, or `None` if you want the default to be chosen.
59 fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
63 /// This will be run on every string macro. The callback cannot influence the further
64 /// treatment of the macro, but may use the value to generate additional code or configuration.
65 fn str_macro(&self, _name: &str, _value: &[u8]) {}
67 /// This will be run on every function-like macro. The callback cannot
68 /// influence the further treatment of the macro, but may use the value to
69 /// generate additional code or configuration.
71 /// The first parameter represents the name and argument list (including the
72 /// parentheses) of the function-like macro. The second parameter represents
73 /// the expansion of the macro as a sequence of tokens.
74 fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}
76 /// This function should return whether, given an enum variant
77 /// name, and value, this enum variant will forcibly be a constant.
78 fn enum_variant_behavior(
80 _enum_name: Option<&str>,
81 _original_variant_name: &str,
82 _variant_value: EnumVariantValue,
83 ) -> Option<EnumVariantCustomBehavior> {
87 /// Allows to rename an enum variant, replacing `_original_variant_name`.
90 _enum_name: Option<&str>,
91 _original_variant_name: &str,
92 _variant_value: EnumVariantValue,
97 /// Allows to rename an item, replacing `_original_item_name`.
98 fn item_name(&self, _original_item_name: &str) -> Option<String> {
102 /// This will be called on every header filename passed to (`Builder::header`)[`crate::Builder::header`].
103 fn header_file(&self, _filename: &str) {}
105 /// This will be called on every file inclusion, with the full path of the included file.
106 fn include_file(&self, _filename: &str) {}
108 /// This will be called every time `bindgen` reads an environment variable whether it has any
110 fn read_env_var(&self, _key: &str) {}
112 /// This will be called to determine whether a particular blocklisted type
113 /// implements a trait or not. This will be used to implement traits on
114 /// other types containing the blocklisted type.
116 /// * `None`: use the default behavior
117 /// * `Some(ImplementsTrait::Yes)`: `_name` implements `_derive_trait`
118 /// * `Some(ImplementsTrait::Manually)`: any type including `_name` can't
119 /// derive `_derive_trait` but can implemented it manually
120 /// * `Some(ImplementsTrait::No)`: `_name` doesn't implement `_derive_trait`
121 fn blocklisted_type_implements_trait(
124 _derive_trait: DeriveTrait,
125 ) -> Option<ImplementsTrait> {
129 /// Provide a list of custom derive attributes.
131 /// If no additional attributes are wanted, this function should return an
133 fn add_derives(&self, _info: &DeriveInfo<'_>) -> Vec<String> {
137 /// Process a source code comment.
138 fn process_comment(&self, _comment: &str) -> Option<String> {
142 /// Potentially override the visibility of a composite type field.
144 /// Caution: This allows overriding standard C++ visibility inferred by
145 /// `respect_cxx_access_specs`.
148 _info: FieldInfo<'_>,
149 ) -> Option<crate::FieldVisibilityKind> {
153 /// Process a function name that as exactly one `va_list` argument
154 /// to be wrapped as a variadic function with the wrapped static function
157 /// The returned string is new function name.
158 #[cfg(feature = "experimental")]
159 fn wrap_as_variadic_fn(&self, _name: &str) -> Option<String> {
164 /// Relevant information about a type to which new derive attributes will be added using
165 /// [`ParseCallbacks::add_derives`].
168 pub struct DeriveInfo<'a> {
169 /// The name of the type.
171 /// The kind of the type.
175 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
176 /// The kind of the current type.
178 /// The type is a Rust `struct`.
180 /// The type is a Rust `enum`.
182 /// The type is a Rust `union`.
186 /// A struct providing information about the item being passed to [`ParseCallbacks::generated_name_override`].
188 pub struct ItemInfo<'a> {
189 /// The name of the item
195 /// An enum indicating the kind of item for an ItemInfo.
204 /// Relevant information about a field for which visibility can be determined using
205 /// [`ParseCallbacks::field_visibility`].
208 pub struct FieldInfo<'a> {
209 /// The name of the type.
210 pub type_name: &'a str,
211 /// The name of the field.
212 pub field_name: &'a str,