From 2a46e2924256b229653a558435259ca4968d5bd9 Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Wed, 10 Mar 2021 22:24:57 +0100 Subject: [PATCH] Use original name when checking allowlist for anonymous enum variants --- src/ir/context.rs | 4 +++- src/ir/enum_ty.rs | 15 +++++++++++++-- .../parsecb-anonymous-enum-variant-rename.rs | 9 +++++++++ .../parsecb-anonymous-enum-variant-rename.h | 6 ++++++ tests/parse_callbacks/mod.rs | 15 +++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs create mode 100644 tests/headers/parsecb-anonymous-enum-variant-rename.h diff --git a/src/ir/context.rs b/src/ir/context.rs index 6a0f07d924..a0d4de1e22 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -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) diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs index c6cd89ce97..15d4136802 100644 --- a/src/ir/enum_ty.rs +++ b/src/ir/enum_ty.rs @@ -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) @@ -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, @@ -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, @@ -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, val: EnumVariantValue, custom_behavior: Option, ) -> Self { EnumVariant { name, + name_for_allowlisting, comment, val, custom_behavior, @@ -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 diff --git a/tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs b/tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs new file mode 100644 index 0000000000..e615486ed8 --- /dev/null +++ b/tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs @@ -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; diff --git a/tests/headers/parsecb-anonymous-enum-variant-rename.h b/tests/headers/parsecb-anonymous-enum-variant-rename.h new file mode 100644 index 0000000000..9336cf8993 --- /dev/null +++ b/tests/headers/parsecb-anonymous-enum-variant-rename.h @@ -0,0 +1,6 @@ +// bindgen-flags: --allowlist-var ^MyVal$ +// bindgen-parse-callbacks: enum-variant-rename + +enum { + MyVal = 0, +}; diff --git a/tests/parse_callbacks/mod.rs b/tests/parse_callbacks/mod.rs index c60aaa1991..01993cfc77 100644 --- a/tests/parse_callbacks/mod.rs +++ b/tests/parse_callbacks/mod.rs @@ -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 { + Some(format!("RENAMED_{}", original_variant_name)) + } +} + pub fn lookup(cb: &str) -> Box { match cb { + "enum-variant-rename" => Box::new(EnumVariantRename), _ => panic!("Couldn't find name ParseCallbacks: {}", cb), } }