Skip to content

Commit 9aba819

Browse files
committed
expose discovered composite types and aliases to parse callbacks
1 parent 7b95673 commit 9aba819

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

bindgen/callbacks.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! A public API for more fine-grained customization of bindgen behavior.
22
33
pub use crate::ir::analysis::DeriveTrait;
4+
use crate::ir::comp::CompKind;
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;
8+
use proc_macro2::Ident;
79
use std::fmt;
810

911
/// An enum to allow ignoring parsing of macros.
@@ -159,6 +161,27 @@ pub trait ParseCallbacks: fmt::Debug {
159161
fn wrap_as_variadic_fn(&self, _name: &str) -> Option<String> {
160162
None
161163
}
164+
165+
/// This will get called everytime a composite type is found with some information about it
166+
fn new_composite_found(
167+
&self,
168+
_id: usize,
169+
_kind: CompKind,
170+
_original_name: Option<&str>,
171+
_final_ident: &Ident,
172+
) {
173+
}
174+
175+
/// This will get called everytime an alias is found with some information about it
176+
fn new_alias_found(
177+
&self,
178+
_id: usize,
179+
_alias_name: &Ident,
180+
_alias_for: usize,
181+
) {
182+
}
183+
184+
// TODO add callback for ResolvedTypeRef
162185
}
163186

164187
/// Relevant information about a type to which new derive attributes will be added using

bindgen/codegen/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,15 @@ impl CodeGenerator for Type {
967967

968968
let rust_name = ctx.rust_ident(&name);
969969

970+
ctx.options().for_each_callback(|cb| {
971+
972+
cb.new_alias_found(
973+
item.id().as_usize(),
974+
&rust_name,
975+
inner_item.id().as_usize(),
976+
);
977+
});
978+
970979
let mut tokens = if let Some(comment) = item.comment(ctx) {
971980
attributes::doc(comment)
972981
} else {
@@ -2252,6 +2261,15 @@ impl CodeGenerator for CompInfo {
22522261

22532262
let is_rust_union = is_union && struct_layout.is_rust_union();
22542263

2264+
ctx.options().for_each_callback(|cb| {
2265+
cb.new_composite_found(
2266+
item.id().as_usize(),
2267+
self.kind(),
2268+
item.kind().expect_type().name(),
2269+
&canonical_ident,
2270+
);
2271+
});
2272+
22552273
// The custom derives callback may return a list of derive attributes;
22562274
// add them to the end of the list.
22572275
let custom_derives = ctx.options().all_callbacks(|cb| {

bindgen/ir/comp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::mem;
2222

2323
/// The kind of compound type.
2424
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
25-
pub(crate) enum CompKind {
25+
pub enum CompKind {
2626
/// A struct.
2727
Struct,
2828
/// A union.

bindgen/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ mod clang;
4646
mod diagnostics;
4747
mod features;
4848
mod ir;
49+
50+
pub use ir::comp::CompKind;
51+
4952
mod parse;
5053
mod regex_set;
5154

0 commit comments

Comments
 (0)