Skip to content

Commit 06dc332

Browse files
committed
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 07bbd04 commit 06dc332

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,13 +1099,19 @@ impl CodeGenerator for Type {
10991099
});
11001100
}
11011101

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

0 commit comments

Comments
 (0)