Skip to content

Commit 2a546c8

Browse files
author
Jethro Beekman
committed
Use original name when checking allowlist for anonymous enum variants
1 parent 80b9c56 commit 2a546c8

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

src/ir/context.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
22992299
let mut prefix_path =
23002300
parent.path_for_allowlisting(self).clone();
23012301
enum_.variants().iter().any(|variant| {
2302-
prefix_path.push(variant.name().into());
2302+
prefix_path.push(
2303+
variant.name_for_allowlisting().into(),
2304+
);
23032305
let name = prefix_path[1..].join("::");
23042306
prefix_path.pop().unwrap();
23052307
self.options().allowlisted_vars.matches(&name)

src/ir/enum_ty.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Enum {
118118
}
119119
});
120120

121-
let name = ctx
121+
let new_name = ctx
122122
.parse_callbacks()
123123
.and_then(|callbacks| {
124124
callbacks.enum_variant_name(type_name, &name, val)
@@ -130,10 +130,11 @@ impl Enum {
130130
.last()
131131
.cloned()
132132
})
133-
.unwrap_or(name);
133+
.unwrap_or_else(|| name.clone());
134134

135135
let comment = cursor.raw_comment();
136136
variants.push(EnumVariant::new(
137+
new_name,
137138
name,
138139
comment,
139140
val,
@@ -224,6 +225,9 @@ pub struct EnumVariant {
224225
/// The name of the variant.
225226
name: String,
226227

228+
/// The original name of the variant (without user mangling)
229+
name_for_allowlisting: String,
230+
227231
/// An optional doc comment.
228232
comment: Option<String>,
229233

@@ -251,12 +255,14 @@ impl EnumVariant {
251255
/// Construct a new enumeration variant from the given parts.
252256
pub fn new(
253257
name: String,
258+
name_for_allowlisting: String,
254259
comment: Option<String>,
255260
val: EnumVariantValue,
256261
custom_behavior: Option<EnumVariantCustomBehavior>,
257262
) -> Self {
258263
EnumVariant {
259264
name,
265+
name_for_allowlisting,
260266
comment,
261267
val,
262268
custom_behavior,
@@ -268,6 +274,11 @@ impl EnumVariant {
268274
&self.name
269275
}
270276

277+
/// Get this variant's name.
278+
pub fn name_for_allowlisting(&self) -> &str {
279+
&self.name_for_allowlisting
280+
}
281+
271282
/// Get this variant's value.
272283
pub fn val(&self) -> EnumVariantValue {
273284
self.val
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
9+
pub type _bindgen_ty_1 = ::std::os::raw::c_uint;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-flags: --allowlist-var ^MyVal$
2+
// bindgen-parse-callbacks: enum-variant-rename
3+
4+
enum {
5+
MyVal = 0,
6+
};

tests/parse_callbacks/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
use bindgen::callbacks::ParseCallbacks;
22

3+
#[derive(Debug)]
4+
struct EnumVariantRename;
5+
6+
impl ParseCallbacks for EnumVariantRename {
7+
fn enum_variant_name(
8+
&self,
9+
_enum_name: Option<&str>,
10+
original_variant_name: &str,
11+
_variant_value: bindgen::callbacks::EnumVariantValue,
12+
) -> Option<String> {
13+
Some(format!("RENAMED_{}", original_variant_name))
14+
}
15+
}
16+
317
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
418
match cb {
19+
"enum-variant-rename" => Box::new(EnumVariantRename),
520
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
621
}
722
}

0 commit comments

Comments
 (0)