Skip to content

Commit

Permalink
Use original name when checking allowlist for anonymous enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Beekman authored and emilio committed Mar 22, 2021
1 parent af98fd4 commit 2a46e29
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
let mut prefix_path =
parent.path_for_allowlisting(self).clone();
enum_.variants().iter().any(|variant| {
prefix_path.push(variant.name().into());
prefix_path.push(
variant.name_for_allowlisting().into(),
);
let name = prefix_path[1..].join("::");
prefix_path.pop().unwrap();
self.options().allowlisted_vars.matches(&name)
Expand Down
15 changes: 13 additions & 2 deletions src/ir/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Enum {
}
});

let name = ctx
let new_name = ctx
.parse_callbacks()
.and_then(|callbacks| {
callbacks.enum_variant_name(type_name, &name, val)
Expand All @@ -130,10 +130,11 @@ impl Enum {
.last()
.cloned()
})
.unwrap_or(name);
.unwrap_or_else(|| name.clone());

let comment = cursor.raw_comment();
variants.push(EnumVariant::new(
new_name,
name,
comment,
val,
Expand Down Expand Up @@ -224,6 +225,9 @@ pub struct EnumVariant {
/// The name of the variant.
name: String,

/// The original name of the variant (without user mangling)
name_for_allowlisting: String,

/// An optional doc comment.
comment: Option<String>,

Expand Down Expand Up @@ -251,12 +255,14 @@ impl EnumVariant {
/// Construct a new enumeration variant from the given parts.
pub fn new(
name: String,
name_for_allowlisting: String,
comment: Option<String>,
val: EnumVariantValue,
custom_behavior: Option<EnumVariantCustomBehavior>,
) -> Self {
EnumVariant {
name,
name_for_allowlisting,
comment,
val,
custom_behavior,
Expand All @@ -268,6 +274,11 @@ impl EnumVariant {
&self.name
}

/// Get this variant's name.
pub fn name_for_allowlisting(&self) -> &str {
&self.name_for_allowlisting
}

/// Get this variant's value.
pub fn val(&self) -> EnumVariantValue {
self.val
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
pub type _bindgen_ty_1 = ::std::os::raw::c_uint;
6 changes: 6 additions & 0 deletions tests/headers/parsecb-anonymous-enum-variant-rename.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// bindgen-flags: --allowlist-var ^MyVal$
// bindgen-parse-callbacks: enum-variant-rename

enum {
MyVal = 0,
};
15 changes: 15 additions & 0 deletions tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
use bindgen::callbacks::ParseCallbacks;

#[derive(Debug)]
struct EnumVariantRename;

impl ParseCallbacks for EnumVariantRename {
fn enum_variant_name(
&self,
_enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: bindgen::callbacks::EnumVariantValue,
) -> Option<String> {
Some(format!("RENAMED_{}", original_variant_name))
}
}

pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
match cb {
"enum-variant-rename" => Box::new(EnumVariantRename),
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
}
}

0 comments on commit 2a46e29

Please sign in to comment.