Skip to content

Commit 2269982

Browse files
nyurikpvdrz
authored andcommitted
Use field_visibility callback for new-type aliases
The `field_visibility` callback is now called in case of alias new-type and new-type-deref with the type name and field_name set to `"0"`
1 parent 492a942 commit 2269982

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

bindgen-tests/tests/expectations/tests/issue-2966.rs

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-flags: --default-alias-style=new_type
2+
// bindgen-parse-callbacks: type-visibility
3+
4+
typedef const char * pub_var1;
5+
typedef const char * pubcrate_var2;
6+
typedef const char * private_var3;

bindgen-tests/tests/parse_callbacks/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ struct FieldVisibility {
9393
}
9494

9595
/// Implements the `field_visibility` function of the trait by checking if the
96-
/// field name starts with `private_`. If it does it makes it private, if it
97-
/// doesn't it makes it public, taking into account the default visibility.
96+
/// field name starts with `private_`. If it does, it makes it private, if it
97+
/// doesn't, it makes it public, taking into account the default visibility.
9898
impl ParseCallbacks for FieldVisibility {
9999
fn field_visibility(
100100
&self,
@@ -113,6 +113,28 @@ impl ParseCallbacks for FieldVisibility {
113113
}
114114
}
115115

116+
#[derive(Debug)]
117+
struct TypeVisibility;
118+
119+
/// Implements the `field_visibility` function of the trait by checking the
120+
/// type name. Depending on name prefix, it will return a different visibility.
121+
impl ParseCallbacks for TypeVisibility {
122+
fn field_visibility(
123+
&self,
124+
FieldInfo { type_name, .. }: FieldInfo,
125+
) -> Option<FieldVisibilityKind> {
126+
if type_name.starts_with("private_") {
127+
Some(FieldVisibilityKind::Private)
128+
} else if type_name.starts_with("pubcrate_") {
129+
Some(FieldVisibilityKind::PublicCrate)
130+
} else if type_name.starts_with("pub_") {
131+
Some(FieldVisibilityKind::Public)
132+
} else {
133+
None
134+
}
135+
}
136+
}
137+
116138
#[derive(Debug)]
117139
pub(super) struct WrapAsVariadicFn;
118140

@@ -129,6 +151,7 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
129151
Box::new(BlocklistedTypeImplementsTrait)
130152
}
131153
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
154+
"type-visibility" => Box::new(TypeVisibility),
132155
call_back => {
133156
if let Some(prefix) =
134157
call_back.strip_prefix("remove-function-prefix-")

bindgen/codegen/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,13 +1100,21 @@ impl CodeGenerator for Type {
11001100
});
11011101
}
11021102

1103-
let access_spec =
1104-
access_specifier(ctx.options().default_visibility);
11051103
tokens.append_all(match alias_style {
11061104
AliasVariation::TypeAlias => quote! {
11071105
= #inner_rust_type ;
11081106
},
11091107
AliasVariation::NewType | AliasVariation::NewTypeDeref => {
1108+
let visibility = ctx
1109+
.options()
1110+
.last_callback(|cb| {
1111+
cb.field_visibility(FieldInfo {
1112+
type_name: &item.canonical_name(ctx),
1113+
field_name: "0",
1114+
})
1115+
})
1116+
.unwrap_or(ctx.options().default_visibility);
1117+
let access_spec = access_specifier(visibility);
11101118
quote! {
11111119
(#access_spec #inner_rust_type) ;
11121120
}

0 commit comments

Comments
 (0)